/** * 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") private V getMapped(final Object key) { Objects.requireNonNull(key); final Object[] entries = this.entries; final int mask = entries.length - 1; int index = Hashing.evenHash(key.hashCode(), mask); Object value = null; while (entries[index + 1] != null) { if (entries[index] == key || entries[index].equals(key)) { value = entries[index + 1]; break; } index = next(index, mask); } return (V)value; }
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; }
public long incRefToStreamAndSession(final int streamId, final int sessionId) { return refCountByStreamIdAndSessionIdMap.incrementAndGet(Hashing.compoundKey(streamId, sessionId)); }
@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; } } }
public long decRefToStreamAndSession(final int streamId, final int sessionId) { final long key = Hashing.compoundKey(streamId, sessionId); final long count = refCountByStreamIdAndSessionIdMap.decrementAndGet(key); if (-1 == count) { refCountByStreamIdAndSessionIdMap.remove(key); throw new IllegalStateException( "could not find stream Id + session Id to decrement: " + streamId + " " + sessionId); } return count; }
@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; }
@SuppressWarnings("FinalParameters") private void compactChain(int deleteKeyIndex) { final Object[] entries = this.entries; final int mask = entries.length - 1; int keyIndex = deleteKeyIndex; while (true) { keyIndex = next(keyIndex, mask); if (entries[keyIndex + 1] == null) { break; } final int hash = Hashing.evenHash(entries[keyIndex].hashCode(), mask); if ((keyIndex < hash && (hash <= deleteKeyIndex || deleteKeyIndex <= keyIndex)) || (hash <= deleteKeyIndex && deleteKeyIndex <= keyIndex)) { entries[deleteKeyIndex] = entries[keyIndex]; entries[deleteKeyIndex + 1] = entries[keyIndex + 1]; entries[keyIndex] = null; entries[keyIndex + 1] = null; deleteKeyIndex = keyIndex; } } }
/** * 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; }
public long incRefToStreamAndSession(final int streamId, final int sessionId) { return refCountByStreamIdAndSessionIdMap.incrementAndGet(Hashing.compoundKey(streamId, sessionId)); }
/** * 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; }
private void rehash(final int newCapacity) { final Object[] oldEntries = entries; final int length = entries.length; capacity(newCapacity); final Object[] newEntries = entries; final int mask = entries.length - 1; for (int keyIndex = 0; keyIndex < length; keyIndex += 2) { final Object value = oldEntries[keyIndex + 1]; if (value != null) { final Object key = oldEntries[keyIndex]; int index = Hashing.evenHash(key.hashCode(), mask); while (newEntries[index + 1] != null) { index = next(index, mask); } newEntries[index] = key; newEntries[index + 1] = value; } } }
public V put(final int keyPartA, final int keyPartB, final V value) final long key = compoundKey(keyPartA, keyPartB); int index = Hashing.hash(key, mask);
public long incRefToStreamAndSession(final int streamId, final int sessionId) { return refCountByStreamIdAndSessionIdMap.incrementAndGet(Hashing.compoundKey(streamId, sessionId)); }
/** * {@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; }
/** * {@inheritDoc} */ public V remove(final Object key) { final Object[] entries = this.entries; final int mask = entries.length - 1; int keyIndex = Hashing.evenHash(key.hashCode(), mask); Object oldValue = null; while (entries[keyIndex + 1] != null) { if (entries[keyIndex] == key || entries[keyIndex].equals(key)) { oldValue = entries[keyIndex + 1]; entries[keyIndex] = null; entries[keyIndex + 1] = null; size--; compactChain(keyIndex); break; } keyIndex = next(keyIndex, mask); } return unmapNullValue(oldValue); }
/** * 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; }
public long decRefToStreamAndSession(final int streamId, final int sessionId) { final long key = Hashing.compoundKey(streamId, sessionId); final long count = refCountByStreamIdAndSessionIdMap.decrementAndGet(key); if (-1 == count) { refCountByStreamIdAndSessionIdMap.remove(key); throw new IllegalStateException( "could not find stream Id + session Id to decrement: " + streamId + " " + sessionId); } return count; }
/** * 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; }
int index = Hashing.evenHash(key.hashCode(), mask); Object oldValue = null;