public boolean containsKey(int key) { int length = table.length; int index = index(key, length); for (int start = index; ;) { Entry<V> e = table[index]; if (e == null) return false; if (e.key == key) return true; index = nextIndex(index, length); if (index == start) // Full table return false; } }
public V get(int key) { int length = table.length; int index = index(key, length); for (int start = index; ;) { Entry<V> e = table[index]; if (e == null) return null; if (e.key == key) return e.value; index = nextIndex(index, length); if (index == start) // Full table return null; } }
public void printDebugStats() { int optimal = 0; int total = 0; int totalSkew = 0; int maxSkew = 0; for (int i = 0; i < table.length; i++) { Entry<V> e = table[i]; if (e != null) { total++; int target = index(e.key, table.length); if (i == target) optimal++; else { int skew = Math.abs(i - target); if (skew > maxSkew) maxSkew = skew; totalSkew += skew; } } } System.out.println(" Size: " + size); System.out.println(" Real Size: " + total); System.out.println(" Optimal: " + optimal + " (" + (float) optimal * 100 / total + "%)"); System.out.println(" Average Distance: " + ((float) totalSkew / (total - optimal))); System.out.println(" Max Distance: " + maxSkew); }
@SuppressWarnings("unchecked") private void resize(int from) { int newLength = from << 1; // Can't get any bigger if (newLength > MAXIMUM_CAPACITY || newLength <= from) return; Entry<V>[] newTable = new Entry[newLength]; Entry<V>[] old = table; for (Entry<V> e : old) { if (e == null) continue; int index = index(e.key, newLength); while (newTable[index] != null) index = nextIndex(index, newLength); newTable[index] = e; } threshold = (int) (loadFactor * newLength); table = newTable; }
@SuppressWarnings("unchecked") private void putForCreate(int key, V value) { Entry<V>[] table = this.table; int length = table.length; int index = index(key, length); Entry<V> e = table[index]; while (e != null) { index = nextIndex(index, length); e = table[index]; } table[index] = new Entry<V>(key, value); }
private void relocate(int start) { Entry<V>[] table = this.table; int length = table.length; int current = nextIndex(start, length); for (; ;) { Entry<V> e = table[current]; if (e == null) return; // A Doug Lea variant of Knuth's Section 6.4 Algorithm R. // This provides a non-recursive method of relocating // entries to their optimal positions once a gap is created. int prefer = index(e.key, length); if ((current < prefer && (prefer <= start || start <= current)) || (prefer <= start && start <= current)) { table[start] = e; table[current] = null; start = current; } current = nextIndex(current, length); } }
public V remove(int key) { Entry<V>[] table = this.table; int length = table.length; int start = index(key, length); for (int index = start; ;) { Entry<V> e = table[index]; if (e == null) return null; if (e.key == key) { table[index] = null; relocate(index); size--; return e.value; } index = nextIndex(index, length); if (index == start) return null; } }
public V put(int key, V value) { Entry<V>[] table = this.table; int hash = key; int length = table.length; int index = index(hash, length); for (int start = index; ;) { Entry<V> e = table[index]; if (e == null) break; if (e.key == key) { table[index] = new Entry<V>(key, value); return e.value; } index = nextIndex(index, length); if (index == start) throw new IllegalStateException("Table is full!"); } table[index] = new Entry<V>(key, value); if (++size >= threshold) resize(length); return null; }
public V get(int key) { int length = table.length; int index = index(key, length); for (int start = index; ;) { Entry<V> e = table[index]; if (e == null) return null; if (e.key == key) return e.value; index = nextIndex(index, length); if (index == start) // Full table return null; } }
public boolean containsKey(int key) { int length = table.length; int index = index(key, length); for (int start = index; ;) { Entry<V> e = table[index]; if (e == null) return false; if (e.key == key) return true; index = nextIndex(index, length); if (index == start) // Full table return false; } }
public boolean containsKey(int key) { int length = table.length; int index = index(key, length); for (int start = index; ;) { Entry<V> e = table[index]; if (e == null) return false; if (e.key == key) return true; index = nextIndex(index, length); if (index == start) // Full table return false; } }
public V get(int key) { int length = table.length; int index = index(key, length); for (int start = index; ;) { Entry<V> e = table[index]; if (e == null) return null; if (e.key == key) return e.value; index = nextIndex(index, length); if (index == start) // Full table return null; } }
public V get(int key) { int length = table.length; int index = index(key, length); for (int start = index; ;) { Entry<V> e = table[index]; if (e == null) return null; if (e.key == key) return e.value; index = nextIndex(index, length); if (index == start) // Full table return null; } }
public V get(int key) { int length = table.length; int index = index(key, length); for (int start = index; ;) { Entry<V> e = table[index]; if (e == null) return null; if (e.key == key) return e.value; index = nextIndex(index, length); if (index == start) // Full table return null; } }
public V get(int key) { int length = table.length; int index = index(key, length); for (int start = index; ;) { Entry<V> e = table[index]; if (e == null) return null; if (e.key == key) return e.value; index = nextIndex(index, length); if (index == start) // Full table return null; } }
@SuppressWarnings("unchecked") private void putForCreate(int key, V value) { Entry<V>[] table = this.table; int length = table.length; int index = index(key, length); Entry<V> e = table[index]; while (e != null) { index = nextIndex(index, length); e = table[index]; } table[index] = new Entry<V>(key, value); }
@SuppressWarnings("unchecked") private void putForCreate(int key, V value) { Entry<V>[] table = this.table; int length = table.length; int index = index(key, length); Entry<V> e = table[index]; while (e != null) { index = nextIndex(index, length); e = table[index]; } table[index] = new Entry<V>(key, value); }
@SuppressWarnings("unchecked") private void putForCreate(int key, V value) { Entry<V>[] table = this.table; int length = table.length; int index = index(key, length); Entry<V> e = table[index]; while (e != null) { index = nextIndex(index, length); e = table[index]; } table[index] = new Entry<V>(key, value); }
@SuppressWarnings("unchecked") private void putForCreate(int key, V value) { Entry<V>[] table = this.table; int length = table.length; int index = index(key, length); Entry<V> e = table[index]; while (e != null) { index = nextIndex(index, length); e = table[index]; } table[index] = new Entry<V>(key, value); }
@SuppressWarnings("unchecked") private void putForCreate(int key, V value) { Entry<V>[] table = this.table; int length = table.length; int index = index(key, length); Entry<V> e = table[index]; while (e != null) { index = nextIndex(index, length); e = table[index]; } table[index] = new Entry<V>(key, value); }