private void rehash(final int newCapacity) { final int mask = newCapacity - 1; resizeThreshold = (int)(newCapacity * loadFactor); final long[] tempKeys = new long[newCapacity]; final Object[] tempValues = new Object[newCapacity]; for (int i = 0, size = values.length; i < size; i++) { final Object value = values[i]; if (null != value) { final long key = keys[i]; int newHash = Hashing.hash(key, mask); while (null != tempValues[newHash]) { newHash = ++newHash & mask; } tempKeys[newHash] = key; tempValues[newHash] = value; } } keys = tempKeys; values = tempValues; }
@SuppressWarnings("FinalParameters") private void compactChain(int deleteIndex) { final int mask = values.length - 1; int index = deleteIndex; while (true) { index = ++index & mask; if (null == values[index]) { break; } final long key = keys[index]; final int hash = Hashing.hash(key, mask); if ((index < hash && (hash <= deleteIndex || deleteIndex <= index)) || (hash <= deleteIndex && deleteIndex <= index)) { keys[deleteIndex] = key; values[deleteIndex] = values[index]; values[index] = null; deleteIndex = index; } } }
/** * Retrieve a value from the map. * * @param keyPartA for the key * @param keyPartB for the key * @return value matching the key if found or null if not found. */ @SuppressWarnings("unchecked") public V get(final int keyPartA, final int keyPartB) { final long key = compoundKey(keyPartA, keyPartB); final int mask = values.length - 1; int index = Hashing.hash(key, mask); Object value; while (null != (value = values[index])) { if (key == keys[index]) { break; } index = ++index & mask; } return (V)value; }
@SuppressWarnings("unchecked") protected V getMapped(final int key) { @DoNotSub final int mask = values.length - 1; @DoNotSub int index = Hashing.hash(key, mask); Object value; while (null != (value = values[index])) { if (key == keys[index]) { break; } index = ++index & mask; } return (V)value; }
/** * Overloaded version of {@link Map#get(Object)} that takes a primitive int key. * Due to type erasure have to rename the method * * @param key for indexing the {@link Map} * @return the value if found otherwise missingValue */ public int getValue(final K key) { @DoNotSub final int mask = values.length - 1; @DoNotSub int index = Hashing.hash(key, mask); int value; while (missingValue != (value = values[index])) { if (key.equals(keys[index])) { break; } index = ++index & mask; } return value; }
/** * {@inheritDoc} * Overloaded version of {@link Map#containsKey(Object)} that takes a primitive int key. * * @param key for indexing the {@link Map} * @return true if the key is found otherwise false. */ public boolean containsKey(final Object key) { @DoNotSub final int mask = values.length - 1; @DoNotSub int index = Hashing.hash(key, mask); boolean found = false; while (missingValue != values[index]) { if (key.equals(keys[index])) { found = true; break; } index = ++index & mask; } return found; }
/** * Overloaded version of {@link Map#containsKey(Object)} that takes a primitive int key. * * @param key for indexing the {@link Map} * @return true if the key is found otherwise false. */ public boolean containsKey(final int key) { @DoNotSub final int mask = values.length - 1; @DoNotSub int index = Hashing.hash(key, mask); boolean found = false; while (null != values[index]) { if (key == keys[index]) { found = true; break; } index = ++index & mask; } return found; }
/** * {@inheritDoc} */ @DoNotSub public int hashCode() { @DoNotSub int hashCode = 0; for (@DoNotSub int i = 0; i < size; i++) { final int value = elements[i]; hashCode = 31 * hashCode + (value == nullValue ? 0 : Hashing.hash(value)); } return hashCode; }
/** * Remove a value from the map and return the value. * * @param keyPartA for the key * @param keyPartB for the key * @return the previous value if found otherwise null */ @SuppressWarnings("unchecked") public V remove(final int keyPartA, final int keyPartB) { final long key = compoundKey(keyPartA, keyPartB); final int mask = values.length - 1; int index = Hashing.hash(key, mask); Object value; while (null != (value = values[index])) { if (key == keys[index]) { values[index] = null; --size; compactChain(index); break; } index = ++index & mask; } return (V)value; }
/** * Overloaded version of {@link Map#remove(Object)} that takes a primitive int key. * Due to type erasure have to rename the method * * @param key for indexing the {@link Map} * @return the value if found otherwise missingValue */ public int removeKey(final K key) { @DoNotSub final int mask = values.length - 1; @DoNotSub int index = Hashing.hash(key, mask); int value; while (missingValue != (value = values[index])) { if (key.equals(keys[index])) { keys[index] = null; values[index] = missingValue; --size; compactChain(index); break; } index = ++index & mask; } return value; }
private void rehash(@DoNotSub final int newCapacity) { @DoNotSub final int capacity = newCapacity; @DoNotSub final int mask = newCapacity - 1; resizeThreshold = (int)(newCapacity * loadFactor); // @DoNotSub final int[] tempValues = new int[capacity]; Arrays.fill(tempValues, MISSING_VALUE); for (final int value : values) { if (value != MISSING_VALUE) { @DoNotSub int newHash = Hashing.hash(value, mask); while (tempValues[newHash] != MISSING_VALUE) { newHash = ++newHash & mask; } tempValues[newHash] = value; } } values = tempValues; }
/** * {@inheritDoc} */ public boolean contains(final int value) { if (value == MISSING_VALUE) { return containsMissingValue; } final int[] values = this.values; @DoNotSub final int mask = values.length - 1; @DoNotSub int index = Hashing.hash(value, mask); while (values[index] != MISSING_VALUE) { if (values[index] == value) { return true; } index = next(index, mask); } return false; }
/** * Overloaded version of {@link Map#containsKey(Object)} that takes a primitive int key. * * @param key for indexing the {@link Map} * @return true if the key is found otherwise false. */ public boolean containsKey(final int key) { boolean found = false; @DoNotSub final int setNumber = Hashing.hash(key, mask); @DoNotSub final int setBeginIndex = setNumber << setSizeShift; for (@DoNotSub int i = setBeginIndex, setEndIndex = setBeginIndex + setSize; i < setEndIndex; i++) { if (null == values[i]) { break; } if (key == keys[i]) { found = true; break; } } return found; }
private void rehash(@DoNotSub final int newCapacity) { @DoNotSub final int mask = newCapacity - 1; /* @DoNotSub */ resizeThreshold = (int)(newCapacity * loadFactor); @SuppressWarnings("unchecked") final K[] tempKeys = (K[])new Object[newCapacity]; final int[] tempValues = new int[newCapacity]; Arrays.fill(tempValues, missingValue); for (@DoNotSub int i = 0, size = values.length; i < size; i++) { final int value = values[i]; if (missingValue != value) { final K key = keys[i]; @DoNotSub int index = Hashing.hash(key, mask); while (missingValue != tempValues[index]) { index = ++index & mask; } tempKeys[index] = key; tempValues[index] = value; } } keys = tempKeys; values = tempValues; }
@SuppressWarnings("FinalParameters") private void compactChain(@DoNotSub int deleteIndex) { @DoNotSub final int mask = values.length - 1; @DoNotSub int index = deleteIndex; while (true) { index = ++index & mask; if (null == values[index]) { break; } @DoNotSub final int hash = Hashing.hash(keys[index], mask); if ((index < hash && (hash <= deleteIndex || deleteIndex <= index)) || (hash <= deleteIndex && deleteIndex <= index)) { keys[deleteIndex] = keys[index]; values[deleteIndex] = values[index]; values[index] = null; deleteIndex = index; } } }
@SuppressWarnings("FinalParameters") private void compactChain(@DoNotSub int deleteIndex) { @DoNotSub final int mask = values.length - 1; @DoNotSub int index = deleteIndex; while (true) { index = ++index & mask; if (missingValue == values[index]) { break; } @DoNotSub final int hash = Hashing.hash(keys[index], mask); if ((index < hash && (hash <= deleteIndex || deleteIndex <= index)) || (hash <= deleteIndex && deleteIndex <= index)) { keys[deleteIndex] = keys[index]; values[deleteIndex] = values[index]; keys[index] = null; values[index] = missingValue; deleteIndex = index; } } }
private void rehash(@DoNotSub final int newCapacity) { @DoNotSub final int mask = newCapacity - 1; /* @DoNotSub */ resizeThreshold = (int)(newCapacity * loadFactor); final int[] tempKeys = new int[newCapacity]; final Object[] tempValues = new Object[newCapacity]; for (@DoNotSub int i = 0, size = values.length; i < size; i++) { final Object value = values[i]; if (null != value) { final int key = keys[i]; @DoNotSub int index = Hashing.hash(key, mask); while (null != tempValues[index]) { index = ++index & mask; } tempKeys[index] = key; tempValues[index] = value; } } keys = tempKeys; values = tempValues; }
/** * Overloaded version of {@link Map#remove(Object)} that takes a primitive int key. * * @param key for indexing the {@link Map} * @return the value if found otherwise null */ public V remove(final int key) { @DoNotSub final int mask = values.length - 1; @DoNotSub int index = Hashing.hash(key, mask); Object value; while (null != (value = values[index])) { if (key == keys[index]) { values[index] = null; --size; compactChain(index); break; } index = ++index & mask; } return unmapNullValue(value); }
/** * An int specialised version of {this#remove(Object)}. * * @param value the value to remove * @return true if the value was present, false otherwise */ public boolean remove(final int value) { if (value == MISSING_VALUE) { final boolean previousContainsMissingValue = this.containsMissingValue; containsMissingValue = false; return previousContainsMissingValue; } final int[] values = this.values; @DoNotSub final int mask = values.length - 1; @DoNotSub int index = Hashing.hash(value, mask); while (values[index] != MISSING_VALUE) { if (values[index] == value) { values[index] = MISSING_VALUE; compactChain(index); sizeOfArrayValues--; return true; } index = next(index, mask); } return false; }
@SuppressWarnings("FinalParameters") @DoNotSub void compactChain(int deleteIndex) { final int[] values = this.values; @DoNotSub final int mask = values.length - 1; @DoNotSub int index = deleteIndex; while (true) { index = next(index, mask); if (values[index] == MISSING_VALUE) { return; } @DoNotSub final int hash = Hashing.hash(values[index], mask); if ((index < hash && (hash <= deleteIndex || deleteIndex <= index)) || (hash <= deleteIndex && deleteIndex <= index)) { values[deleteIndex] = values[index]; values[index] = MISSING_VALUE; deleteIndex = index; } } }