Guest User

Untitled

a guest
Dec 15th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.15 KB | None | 0 0
  1. package adt.hashtable.open;
  2.  
  3. import adt.hashtable.hashfunction.HashFunctionClosedAddressMethod;
  4. import adt.hashtable.hashfunction.HashFunctionLinearProbing;
  5.  
  6. public class HashtableOpenAddressLinearProbingImpl<T extends Storable> extends
  7. AbstractHashtableOpenAddress<T> {
  8.  
  9. public HashtableOpenAddressLinearProbingImpl(int size,
  10. HashFunctionClosedAddressMethod method) {
  11. super(size);
  12. hashFunction = new HashFunctionLinearProbing<T>(size, method);
  13. this.initiateInternalTable(size);
  14. }
  15.  
  16. @Override
  17. public void insert(T element) {
  18. if(this.isFull()) {
  19. throw new HashtableOverflowException();
  20. }
  21.  
  22. if(element != null) {
  23. if(search(element) == null) {
  24. int i = 0;
  25. int hash = hashFunction(element, i);
  26. while(i < super.capacity()) {
  27. if(table[hash] == null || deletedElement.equals(element)) {
  28. table[hash] = element;
  29. super.elements++;
  30. return;
  31. } else {
  32. i++;
  33. hash = hashFunction(element, i);
  34. super.COLLISIONS++;
  35. }
  36. }
  37. }
  38. }
  39. }
  40.  
  41. @Override
  42. public void remove(T element) {
  43. if(element != null) {
  44. if(indexOf(element) != -1) {
  45. int hash = indexOf(element);
  46. this.table[hash] = super.deletedElement;
  47. super.elements--;
  48. }
  49. }
  50. }
  51.  
  52. @Override
  53. public T search(T element) {
  54. if(element != null) {
  55. int i = 0;
  56. int hash = hashFunction(element, i);
  57. if(!this.isEmpty()) {
  58. while(i < super.capacity() && this.table[hash] != null && !deletedElement.equals(element)) {
  59. if(table[hash].equals(element)) {
  60. return element;
  61. } else {
  62. i++;
  63. hash = hashFunction(element, i);
  64. }
  65. }
  66. }
  67. }
  68. return null;
  69. }
  70.  
  71. @Override
  72. public int indexOf(T element) {
  73. if(element != null) {
  74. if(!this.isEmpty()) {
  75. int i = 0;
  76. int hash = hashFunction(element, i);
  77. while(i < super.capacity() && this.table[hash] != null && !deletedElement.equals(element)) {
  78. if(table[hash].equals(element)) {
  79. return hash;
  80. } else {
  81. i++;
  82. hash = hashFunction(element, i);
  83. }
  84. }
  85. }
  86. }
  87. return -1;
  88. }
  89.  
  90. private int hashFunction(T element, int probe) {
  91. int hashIndex = ((HashFunctionLinearProbing<T>) super.hashFunction).hash(element, probe);
  92. return hashIndex;
  93. }
  94.  
  95. }
Add Comment
Please, Sign In to add comment