/** * Overloaded version of {@link Map#containsKey(Object)} that takes a primitive long key. * * @param key for indexing the {@link Map} * @return true if the key is found otherwise false. */ public boolean containsKey(final long key) { int index = longHash(key, mask); while (null != values[index]) { if (key == keys[index]) { return true; } index = ++index & mask; } return false; }
/** * Overloaded version of {@link Map#containsKey(Object)} that takes a primitive long key. * * @param key for indexing the {@link Map} * @return true if the key is found otherwise false. */ public boolean containsKey(final long key) { int index = longHash(key, mask); while (null != values[index]) { if (key == keys[index]) { return true; } index = ++index & mask; } return false; }
private void compactChain(int deleteIndex) { int index = deleteIndex; while (true) { index = ++index & mask; if (null == values[index]) { return; } final int hash = longHash(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; } } }
private void compactChain(int deleteIndex) { int index = deleteIndex; while (true) { index = ++index & mask; if (null == values[index]) { return; } final int hash = longHash(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; } } }
/** * Overloaded version of {@link Map#get(Object)} that takes a primitive long key. * * @param key for indexing the {@link Map} * @return the value if found otherwise null */ @SuppressWarnings("unchecked") public V get(final long key) { int index = longHash(key, mask); Object value; while (null != (value = values[index])) { if (key == keys[index]) { return (V) value; } index = ++index & mask; } return null; }
/** * Overloaded version of {@link Map#get(Object)} that takes a primitive long key. * * @param key for indexing the {@link Map} * @return the value if found otherwise null */ @SuppressWarnings("unchecked") public V get(final long key) { int index = longHash(key, mask); Object value; while (null != (value = values[index])) { if (key == keys[index]) { return (V) value; } index = ++index & mask; } return null; }
private void rehash(final int newCapacity) { if (1 != Integer.bitCount(newCapacity)) { throw new IllegalStateException("New capacity must be a power of two"); } capacity = newCapacity; mask = newCapacity - 1; resizeThreshold = (int) (newCapacity * loadFactor); final long[] tempKeys = new long[capacity]; final Object[] tempValues = new Object[capacity]; 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 = longHash(key, mask); while (null != tempValues[newHash]) { newHash = ++newHash & mask; } tempKeys[newHash] = key; tempValues[newHash] = value; } } keys = tempKeys; values = tempValues; }
private void rehash(final int newCapacity) { if (1 != Integer.bitCount(newCapacity)) { throw new IllegalStateException("New capacity must be a power of two"); } capacity = newCapacity; mask = newCapacity - 1; resizeThreshold = (int) (newCapacity * loadFactor); final long[] tempKeys = new long[capacity]; final Object[] tempValues = new Object[capacity]; 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 = longHash(key, mask); while (null != tempValues[newHash]) { newHash = ++newHash & mask; } tempKeys[newHash] = key; tempValues[newHash] = value; } } keys = tempKeys; values = tempValues; }
/** * {@inheritDoc} */ public boolean contains(final long value) { int index = longHash(value, mask); while (values[index] != missingValue) { if (values[index] == value) { return true; } index = next(index); } return false; }
/** * {@inheritDoc} */ public boolean contains(final long value) { int index = longHash(value, mask); while (values[index] != missingValue) { if (values[index] == value) { return true; } index = next(index); } return false; }
private void compactChain(int deleteIndex) { final long[] values = this.values; int index = deleteIndex; while (true) { index = next(index); if (values[index] == missingValue) { return; } final int hash = longHash(values[index], mask); if ((index < hash && (hash <= deleteIndex || deleteIndex <= index)) || (hash <= deleteIndex && deleteIndex <= index) ) { values[deleteIndex] = values[index]; values[index] = missingValue; deleteIndex = index; } } }
private void compactChain(int deleteIndex) { final long[] values = this.values; int index = deleteIndex; while (true) { index = next(index); if (values[index] == missingValue) { return; } final int hash = longHash(values[index], mask); if ((index < hash && (hash <= deleteIndex || deleteIndex <= index)) || (hash <= deleteIndex && deleteIndex <= index) ) { values[deleteIndex] = values[index]; values[index] = missingValue; deleteIndex = index; } } }
/** * Overloaded version of {@link Map#remove(Object)} that takes a primitive long key. * * @param key for indexing the {@link Map} * @return the value if found otherwise null */ @SuppressWarnings("unchecked") public V remove(final long key) { int index = longHash(key, mask); Object value; while (null != (value = values[index])) { if (key == keys[index]) { values[index] = null; --size; compactChain(index); return (V) value; } index = ++index & mask; } return null; }
/** * Overloaded version of {@link Map#remove(Object)} that takes a primitive long key. * * @param key for indexing the {@link Map} * @return the value if found otherwise null */ @SuppressWarnings("unchecked") public V remove(final long key) { int index = longHash(key, mask); Object value; while (null != (value = values[index])) { if (key == keys[index]) { values[index] = null; --size; compactChain(index); return (V) value; } index = ++index & mask; } return null; }
/** * Primitive specialised overload of {this#add(Long)} * * @param value the value to add * @return true if the collection has changed, false otherwise */ public boolean add(final long value) { if (size == capacity) { throw new IllegalStateException("This LongHashSet of capacity " + capacity + " is full"); } int index = longHash(value, mask); while (values[index] != missingValue) { if (values[index] == value) { return false; } index = next(index); } values[index] = value; size++; return true; }
/** * Primitive specialised overload of {this#add(Long)} * * @param value the value to add * @return true if the collection has changed, false otherwise */ public boolean add(final long value) { if (size == capacity) { throw new IllegalStateException("This LongHashSet of capacity " + capacity + " is full"); } int index = longHash(value, mask); while (values[index] != missingValue) { if (values[index] == value) { return false; } index = next(index); } values[index] = value; size++; return true; }
/** * An long 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 long value) { int index = longHash(value, mask); while (values[index] != missingValue) { if (values[index] == value) { values[index] = missingValue; compactChain(index); size--; return true; } index = next(index); } return false; }
/** * An long 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 long value) { int index = longHash(value, mask); while (values[index] != missingValue) { if (values[index] == value) { values[index] = missingValue; compactChain(index); size--; return true; } index = next(index); } return false; }
/** * Overloaded version of {@link Map#put(Object, Object)} that takes a primitive long key. * * @param key for indexing the {@link Map} * @param value to be inserted in the {@link Map} * @return the previous value if found otherwise null */ @SuppressWarnings("unchecked") public V put(final long key, final V value) { checkNotNull(value, "Value cannot be null"); V oldValue = null; int index = longHash(key, mask); while (null != values[index]) { if (key == keys[index]) { oldValue = (V) values[index]; break; } index = ++index & mask; } if (null == oldValue) { ++size; keys[index] = key; } values[index] = value; if (size > resizeThreshold) { increaseCapacity(); } return oldValue; }
/** * Overloaded version of {@link Map#put(Object, Object)} that takes a primitive long key. * * @param key for indexing the {@link Map} * @param value to be inserted in the {@link Map} * @return the previous value if found otherwise null */ @SuppressWarnings("unchecked") public V put(final long key, final V value) { checkNotNull(value, "Value cannot be null"); V oldValue = null; int index = longHash(key, mask); while (null != values[index]) { if (key == keys[index]) { oldValue = (V) values[index]; break; } index = ++index & mask; } if (null == oldValue) { ++size; keys[index] = key; } values[index] = value; if (size > resizeThreshold) { increaseCapacity(); } return oldValue; }