Guest User

Untitled

a guest
Dec 18th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 KB | None | 0 0
  1. package hashash;
  2.  
  3.  
  4. public class IntIntLinearProbing {
  5.  
  6. private final int INITIAL_SIZE = 8;
  7. private final double DEFAULT_LOAD_FACTOR = .7;
  8. private int modulo = INITIAL_SIZE - 1;
  9. int[] keys;
  10. int[] values;
  11. int keyCount;
  12. int threshold = (int) (INITIAL_SIZE * DEFAULT_LOAD_FACTOR);
  13.  
  14. public IntIntLinearProbing() {
  15. keys = new int[INITIAL_SIZE];
  16. values = new int[INITIAL_SIZE];
  17. }
  18.  
  19. public IntIntLinearProbing(int size) {
  20. keys = new int[size];
  21. values = new int[size];
  22. threshold = (int) (size * DEFAULT_LOAD_FACTOR);
  23. }
  24.  
  25. private int hash(int key) {
  26. return key & modulo;
  27. }
  28.  
  29. private int locate(int key) {
  30. int slot = hash(key);
  31. while (true) {
  32. if (values[slot] == 0)
  33. return -slot;
  34. if (keys[slot] == key)
  35. return slot;
  36. slot = (slot + 1) & modulo;
  37. }
  38. }
  39.  
  40. public void increment(int key) {
  41. increment(key, 1);
  42. }
  43.  
  44. public int get(int key) {
  45. int l = locate(key);
  46. return l < 0 ? 0 : values[l];
  47. }
  48.  
  49. public void decrement(int key) {
  50. increment(key, -1);
  51. }
  52.  
  53. public void increment(int key, int amount) {
  54. int k = locate(key);
  55. if (k < 0) {
  56. k = -k;
  57. if (keyCount == threshold) {
  58. expand();
  59. }
  60. values[k] = amount;
  61. keys[k] = key;
  62. keyCount++;
  63. } else {
  64. values[k] += amount;
  65. if (values[k] == 0)
  66. keyCount--;
  67.  
  68. }
  69. }
  70.  
  71. public void remove(int key) {
  72. int k = locate(key);
  73. if (k < 0)
  74. return;
  75. values[k] = 0;
  76. keyCount--;
  77. }
  78.  
  79. private void expand() {
  80. IntIntLinearProbing h = new IntIntLinearProbing(values.length * 2);
  81. for (int i = 0; i < keys.length; i++) {
  82. if (values[i] != 0) {
  83. h.set(keys[i], values[i]);
  84. }
  85. }
  86. this.values = h.values;
  87. this.keys = h.keys;
  88. this.keyCount = h.keyCount;
  89. this.modulo = h.modulo;
  90. }
  91.  
  92. private void set(int key, int value) {
  93. int loc = locate(key);
  94. if (loc > 0) {
  95. values[loc] = value;
  96. return;
  97. }
  98. if (keyCount == threshold) {
  99. expand();
  100. }
  101. loc = -loc;
  102. keys[loc] = key;
  103. values[loc] = value;
  104. keyCount++;
  105. }
  106.  
  107.  
  108. public static void main(String[] args) {
  109. int[] testVals = new int[10000];
  110. for (int i = 0; i < testVals.length; i++) {
  111. testVals[i] = i;
  112. }
  113.  
  114. for (int i = 0; i < 100; i++) {
  115. IntIntLinearProbing k = new IntIntLinearProbing();
  116. for (int testVal : testVals) {
  117. k.set(testVal, 1);
  118. System.out.println("testVal = " + testVal);
  119. }
  120. for (int testVal : testVals) {
  121. k.get(testVal);
  122. }
  123. }
  124. }
  125.  
  126.  
  127. }
Add Comment
Please, Sign In to add comment