Advertisement
Guest User

Untitled

a guest
Jun 24th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.75 KB | None | 0 0
  1. package adt.hashtable.closed;
  2.  
  3. import java.util.ArrayList;
  4.  
  5. import adt.hashtable.hashfunction.HashFunction;
  6. import adt.hashtable.hashfunction.HashFunctionClosedAddressMethod;
  7. import adt.hashtable.hashfunction.HashFunctionDivisionMethod;
  8. import adt.hashtable.hashfunction.HashFunctionFactory;
  9. import adt.hashtable.hashfunction.HashFunctionMultiplicationMethod;
  10. import util.Util;
  11.  
  12. public class HashtableClosedAddressImpl<T> extends AbstractHashtableClosedAddress<T> {
  13.  
  14. /**
  15. * A hash table with closed address works with a hash function with closed
  16. * address. Such a function can follow one of these methods: DIVISION or
  17. * MULTIPLICATION. In the DIVISION method, it is useful to change the size
  18. * of the table to an integer that is prime. This can be achieved by
  19. * producing such a prime number that is bigger and close to the desired
  20. * size.
  21. *
  22. * For doing that, you have auxiliary methods: Util.isPrime and
  23. * getPrimeAbove as documented bellow.
  24. *
  25. * The length of the internal table must be the immediate prime number
  26. * greater than the given size. For example, if size=10 then the length must
  27. * be 11. If size=20, the length must be 23. You must implement this idea in
  28. * the auxiliary method getPrimeAbove(int size) and use it.
  29. *
  30. * @param desiredSize
  31. * @param method
  32. */
  33.  
  34. @SuppressWarnings({ "rawtypes", "unchecked" })
  35. public HashtableClosedAddressImpl(int desiredSize, HashFunctionClosedAddressMethod method) {
  36. int realSize = desiredSize;
  37.  
  38. if (method == HashFunctionClosedAddressMethod.DIVISION) {
  39. realSize = this.getPrimeAbove(desiredSize); // real size must the
  40. // the immediate prime
  41. // above
  42. }
  43. initiateInternalTable(realSize);
  44. HashFunction function = HashFunctionFactory.createHashFunction(method, realSize);
  45. this.hashFunction = function;
  46. }
  47.  
  48. // AUXILIARY
  49. /**
  50. * It returns the prime number that is closest (and greater) to the given
  51. * number. You can use the method Util.isPrime to check if a number is
  52. * prime.
  53. */
  54. int getPrimeAbove(int number) {
  55. int result = number;
  56. while (!Util.isPrime(result)){
  57. result++;
  58. }
  59. return result;
  60. }
  61.  
  62. @Override
  63. public void insert(T element) {
  64. if (element != null){
  65. int valor = 0;
  66. if (hashFunction instanceof HashFunctionDivisionMethod){
  67. valor = ((HashFunctionDivisionMethod)this.hashFunction).hash(element);
  68. } else if (hashFunction instanceof HashFunctionMultiplicationMethod){
  69. valor = ((HashFunctionMultiplicationMethod)this.hashFunction).hash(element);
  70. }
  71. insert(valor, element);
  72. }
  73. }
  74.  
  75. private void insert(int valor, T element){
  76. if (table[valor] == null){
  77. table[valor] = new ArrayList();
  78. } else {
  79. super.COLLISIONS++;
  80. }
  81. ((ArrayList)table[valor]).add(element);
  82. }
  83.  
  84. @Override
  85. public void remove(T element) {
  86. if (element != null) {
  87. int valor = 0;
  88. if (hashFunction instanceof HashFunctionDivisionMethod){
  89. valor = ((HashFunctionDivisionMethod)this.hashFunction).hash(element);
  90. } else if (hashFunction instanceof HashFunctionMultiplicationMethod){
  91. valor = ((HashFunctionMultiplicationMethod)this.hashFunction).hash(element);
  92. }
  93. remove(valor, element);
  94. }
  95. }
  96.  
  97. private void remove(int valor, T element){
  98. if (search(element) != null){
  99. ((ArrayList)table[valor]).remove(element);
  100. }
  101. }
  102.  
  103. @Override
  104. public T search(T element) {
  105. T result = null;
  106. if (hashFunction instanceof HashFunctionDivisionMethod){
  107. int valor = ((HashFunctionDivisionMethod)this.hashFunction).hash(element);
  108. result = search(valor, element);
  109. } else if (hashFunction instanceof HashFunctionMultiplicationMethod){
  110. int valor = ((HashFunctionMultiplicationMethod)this.hashFunction).hash(element);
  111. result = search(valor, element);
  112. }
  113. return result;
  114. }
  115.  
  116. private T search(int valor, T element){
  117. T result = null;
  118. if (table[valor] != null){
  119. ArrayList array = (ArrayList) table[valor];
  120. for (int i = 0; i < array.size(); i++){
  121. if (element.equals(array.get(i))){
  122. result = (T) array.get(i);
  123. }
  124. }
  125. }
  126. return result;
  127. }
  128. @Override
  129. public int indexOf(T element) {
  130. int valor = -1;
  131. if (hashFunction instanceof HashFunctionDivisionMethod){
  132. if (search(element) != null){
  133. valor = ((HashFunctionDivisionMethod)this.hashFunction).hash(element);
  134. }
  135. } else if (hashFunction instanceof HashFunctionMultiplicationMethod){
  136. if (search(element) != null){
  137. valor = ((HashFunctionMultiplicationMethod)this.hashFunction).hash(element);
  138. }
  139. }
  140. return valor;
  141. }
  142.  
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement