private boolean insertHelper1(AnyType x) { final int COUNT_LIMIT = 100; while (true) { int lastPos = -1; int pos; for (int count = 0; count < COUNT_LIMIT; count++) { for (int i = 0; i < numHashFunctions; i++) { pos = myhash(x, i); if (array[pos] == null) { array[pos] = x; currentSize++; return true; } } // none of the spots are available. Kick out a random one int i = 0; do { pos = myhash(x, r.nextInt(numHashFunctions)); } while (pos == lastPos && i++ < 5); AnyType tmp = array[lastPos = pos]; array[pos] = x; x = tmp; } if (++rehashes > ALLOWED_REHASHES) { expand(); // Make the table bigger rehashes = 0; } else rehash(); } }
private boolean insertHelper2(AnyType x) { final int COUNT_LIMIT = 100; while (true) { for (int count = 0; count < COUNT_LIMIT; count++) { int pos = myhash(x, count % numHashFunctions); AnyType tmp = array[pos]; array[pos] = x; if (tmp == null) return true; else x = tmp; } if (++rehashes > ALLOWED_REHASHES) { expand(); // Make the table bigger rehashes = 0; } else rehash(); } }
private void rehash() { // System.out.println( "NEW HASH FUNCTIONS " + array.length ); hashFunctions.generateNewFunctions(); rehash(array.length); }
private void expand() { rehash((int) (array.length / MAX_LOAD)); }