Package org.apache.lucene.internal.hppc
Class IntLongHashMap
java.lang.Object
org.apache.lucene.internal.hppc.IntLongHashMap
- All Implemented Interfaces:
Cloneable,Iterable<IntLongHashMap.IntLongCursor>,Accountable
public class IntLongHashMap
extends Object
implements Iterable<IntLongHashMap.IntLongCursor>, Accountable, Cloneable
A hash map of
int to long, implemented using open addressing with
linear probing for collision resolution.
Mostly forked and trimmed from com.carrotsearch.hppc.IntLongHashMap
github: https://github.com/carrotsearch/hppc release 0.10.0
- NOTE: This API is for internal purposes only and might change in incompatible ways in the next release.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classForked from HPPC, holding int index,key and valuefinal classA view of the keys inside this hash map.final classA view over the set of values of this map. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected intThe number of stored keys (assigned key slots), excluding the special "empty" key, if any (usesize()instead).protected booleanSpecial treatment for the "empty slot" key marker.protected intSeed used to ensure the hash iteration order is different from an iteration to another.int[]The array holding keys.protected doubleThe load factor forkeys.protected intMask for slot scans inkeys.protected intlong[]The array holding values.Fields inherited from interface org.apache.lucene.util.Accountable
NULL_ACCOUNTABLE -
Constructor Summary
ConstructorsConstructorDescriptionNew instance with sane defaults.IntLongHashMap(int expectedElements) New instance with sane defaults.IntLongHashMap(int expectedElements, double loadFactor) New instance with the provided defaults.IntLongHashMap(IntLongHashMap container) Create a hash map from all key-value pairs of another container. -
Method Summary
Modifier and TypeMethodDescriptionlongaddTo(int key, long incrementValue) AddsincrementValueto any existing value for the givenkeyor insertsincrementValueifkeydid not previously exist.protected voidallocateBuffers(int arraySize) Allocate new internal buffers.protected voidallocateThenInsertThenRehash(int slot, int pendingKey, long pendingValue) This method is invoked when there is a new key/ value pair to be inserted into the buffers but there is not enough empty slots to do so.voidclear()clone()booleancontainsKey(int key) voidensureCapacity(int expectedElements) Ensure this container can hold at least the given number of keys (entries) without resizing its buffers.protected booleanequalElements(IntLongHashMap other) Return true if all keys of some other container exist in this container.booleanstatic IntLongHashMapfrom(int[] keys, long[] values) Creates a hash map from two index-aligned arrays of key-value pairs.longget(int key) longgetOrDefault(int key, long defaultValue) inthashCode()protected inthashKey(int key) Returns a hash code for the given key.booleanindexExists(int index) longindexGet(int index) voidindexInsert(int index, int key, long value) intindexOf(int key) longindexRemove(int index) longindexReplace(int index, long newValue) booleanisEmpty()iterator()keys()Returns a specialized view of the keys of this associated container.protected intProvides the next iteration seed used to build the iteration starting slot and offset increment.longput(int key, long value) intputAll(Iterable<? extends IntLongHashMap.IntLongCursor> iterable) Puts all key/value pairs from a given iterable into this map.booleanputIfAbsent(int key, long value) If the specified key is not already associated with a value, associates it with the given value.longputOrAdd(int key, long putValue, long incrementValue) Ifkeyexists,putValueis inserted into the map, otherwise any existing value is incremented byadditionValue.longReturn the memory usage of this object in bytes.protected voidrehash(int[] fromKeys, long[] fromValues) Rehash from old buffers to new buffers.voidrelease()longremove(int key) Remove all values at the given key.protected voidshiftConflictingKeys(int gapSlot) Shift all the slot-conflicting keys and values allocated to (and including)slot.intsize()toString()Convert the contents of this map to a human-friendly string.values()protected doubleverifyLoadFactor(double loadFactor) Validate load factor range and return it.Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface org.apache.lucene.util.Accountable
getChildResourcesMethods inherited from interface java.lang.Iterable
forEach, spliterator
-
Field Details
-
keys
public int[] keysThe array holding keys. -
values
public long[] valuesThe array holding values. -
assigned
protected int assignedThe number of stored keys (assigned key slots), excluding the special "empty" key, if any (usesize()instead).- See Also:
-
mask
protected int maskMask for slot scans inkeys. -
resizeAt
protected int resizeAt -
hasEmptyKey
protected boolean hasEmptyKeySpecial treatment for the "empty slot" key marker. -
loadFactor
protected double loadFactorThe load factor forkeys. -
iterationSeed
protected int iterationSeedSeed used to ensure the hash iteration order is different from an iteration to another.
-
-
Constructor Details
-
IntLongHashMap
public IntLongHashMap()New instance with sane defaults. -
IntLongHashMap
public IntLongHashMap(int expectedElements) New instance with sane defaults.- Parameters:
expectedElements- The expected number of elements guaranteed not to cause buffer expansion (inclusive).
-
IntLongHashMap
public IntLongHashMap(int expectedElements, double loadFactor) New instance with the provided defaults.- Parameters:
expectedElements- The expected number of elements guaranteed not to cause a rehash (inclusive).loadFactor- The load factor for internal buffers. Insane load factors (zero, full capacity) are rejected byverifyLoadFactor(double).
-
IntLongHashMap
Create a hash map from all key-value pairs of another container.
-
-
Method Details
-
put
public long put(int key, long value) -
putIfAbsent
public boolean putIfAbsent(int key, long value) If the specified key is not already associated with a value, associates it with the given value.- Returns:
trueifkeydid not exist andvaluewas placed in the map,falseotherwise.
-
putAll
Puts all key/value pairs from a given iterable into this map. -
putOrAdd
public long putOrAdd(int key, long putValue, long incrementValue) Ifkeyexists,putValueis inserted into the map, otherwise any existing value is incremented byadditionValue.- Parameters:
key- The key of the value to adjust.putValue- The value to put ifkeydoes not exist.incrementValue- The value to add to the existing value ifkeyexists.- Returns:
- Returns the current value associated with
key(after changes).
-
addTo
public long addTo(int key, long incrementValue) AddsincrementValueto any existing value for the givenkeyor insertsincrementValueifkeydid not previously exist.- Parameters:
key- The key of the value to adjust.incrementValue- The value to put or add to the existing value ifkeyexists.- Returns:
- Returns the current value associated with
key(after changes).
-
remove
public long remove(int key) Remove all values at the given key. The default value for the key type is returned if the value does not exist in the map. -
get
public long get(int key) -
getOrDefault
public long getOrDefault(int key, long defaultValue) -
containsKey
public boolean containsKey(int key) -
indexOf
public int indexOf(int key) -
indexExists
public boolean indexExists(int index) -
indexGet
public long indexGet(int index) -
indexReplace
public long indexReplace(int index, long newValue) -
indexInsert
public void indexInsert(int index, int key, long value) -
indexRemove
public long indexRemove(int index) -
clear
public void clear() -
release
public void release() -
size
public int size() -
isEmpty
public boolean isEmpty() -
hashCode
public int hashCode() -
equals
-
equalElements
Return true if all keys of some other container exist in this container. -
ensureCapacity
public void ensureCapacity(int expectedElements) Ensure this container can hold at least the given number of keys (entries) without resizing its buffers.- Parameters:
expectedElements- The total number of keys, inclusive.
-
ramBytesUsed
public long ramBytesUsed()Description copied from interface:AccountableReturn the memory usage of this object in bytes. Negative values are illegal.- Specified by:
ramBytesUsedin interfaceAccountable
-
nextIterationSeed
protected int nextIterationSeed()Provides the next iteration seed used to build the iteration starting slot and offset increment. This method does not need to be synchronized, what matters is that each thread gets a sequence of varying seeds. -
iterator
- Specified by:
iteratorin interfaceIterable<IntLongHashMap.IntLongCursor>
-
keys
Returns a specialized view of the keys of this associated container. -
values
- Returns:
- Returns a container with all values stored in this map.
-
clone
-
toString
Convert the contents of this map to a human-friendly string. -
from
Creates a hash map from two index-aligned arrays of key-value pairs. -
hashKey
protected int hashKey(int key) Returns a hash code for the given key.The output from this function should evenly distribute keys across the entire integer range.
-
verifyLoadFactor
protected double verifyLoadFactor(double loadFactor) Validate load factor range and return it. Override and suppress if you need insane load factors. -
rehash
protected void rehash(int[] fromKeys, long[] fromValues) Rehash from old buffers to new buffers. -
allocateBuffers
protected void allocateBuffers(int arraySize) Allocate new internal buffers. This method attempts to allocate and assign internal buffers atomically (either allocations succeed or not). -
allocateThenInsertThenRehash
protected void allocateThenInsertThenRehash(int slot, int pendingKey, long pendingValue) This method is invoked when there is a new key/ value pair to be inserted into the buffers but there is not enough empty slots to do so.New buffers are allocated. If this succeeds, we know we can proceed with rehashing so we assign the pending element to the previous buffer (possibly violating the invariant of having at least one empty slot) and rehash all keys, substituting new buffers at the end.
-
shiftConflictingKeys
protected void shiftConflictingKeys(int gapSlot) Shift all the slot-conflicting keys and values allocated to (and including)slot.
-