Package org.apache.lucene.internal.hppc
Class LongFloatHashMap
- java.lang.Object
-
- org.apache.lucene.internal.hppc.LongFloatHashMap
-
- All Implemented Interfaces:
Cloneable,Iterable<LongFloatHashMap.LongFloatCursor>,Accountable
public class LongFloatHashMap extends Object implements Iterable<LongFloatHashMap.LongFloatCursor>, Accountable, Cloneable
A hash map oflongtofloat, implemented using open addressing with linear probing for collision resolution.Mostly forked and trimmed from com.carrotsearch.hppc.LongFloatHashMap
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 Classes Modifier and Type Class Description classLongFloatHashMap.KeysContainerA view of the keys inside this hash map.static classLongFloatHashMap.LongFloatCursorForked from HPPC, holding int index,key and valueclassLongFloatHashMap.ValuesContainerA view over the set of values of this map.
-
Field Summary
Fields Modifier and Type Field Description protected intassignedThe number of stored keys (assigned key slots), excluding the special "empty" key, if any (usesize()instead).protected booleanhasEmptyKeySpecial treatment for the "empty slot" key marker.protected intiterationSeedSeed used to ensure the hash iteration order is different from an iteration to another.long[]keysThe array holding keys.protected doubleloadFactorThe load factor forkeys.protected intmaskMask for slot scans inkeys.protected intresizeAtfloat[]valuesThe array holding values.-
Fields inherited from interface org.apache.lucene.util.Accountable
NULL_ACCOUNTABLE
-
-
Constructor Summary
Constructors Constructor Description LongFloatHashMap()New instance with sane defaults.LongFloatHashMap(int expectedElements)New instance with sane defaults.LongFloatHashMap(int expectedElements, double loadFactor)New instance with the provided defaults.LongFloatHashMap(LongFloatHashMap map)Create a hash map from all key-value pairs of another map.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description floataddTo(long key, float incrementValue)AddsincrementValueto any existing value for the givenkeyor insertsincrementValueifkeydid not previously exist.protected voidallocateBuffers(int arraySize)Allocate new internal buffers.protected voidallocateThenInsertThenRehash(int slot, long pendingKey, float 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()LongFloatHashMapclone()booleancontainsKey(long key)voidensureCapacity(int expectedElements)Ensure this container can hold at least the given number of keys (entries) without resizing its buffers.protected booleanequalElements(LongFloatHashMap other)Return true if all keys of some other container exist in this container.booleanequals(Object obj)static LongFloatHashMapfrom(long[] keys, float[] values)Creates a hash map from two index-aligned arrays of key-value pairs.floatget(long key)floatgetOrDefault(long key, float defaultValue)inthashCode()protected inthashKey(long key)Returns a hash code for the given key.booleanindexExists(int index)floatindexGet(int index)voidindexInsert(int index, long key, float value)intindexOf(long key)floatindexRemove(int index)floatindexReplace(int index, float newValue)booleanisEmpty()Iterator<LongFloatHashMap.LongFloatCursor>iterator()LongFloatHashMap.KeysContainerkeys()Returns a specialized view of the keys of this associated container.protected intnextIterationSeed()Provides the next iteration seed used to build the iteration starting slot and offset increment.floatput(long key, float value)intputAll(Iterable<? extends LongFloatHashMap.LongFloatCursor> iterable)booleanputIfAbsent(long key, float value)Trove-inspired API method.floatputOrAdd(long key, float putValue, float incrementValue)Ifkeyexists,putValueis inserted into the map, otherwise any existing value is incremented byadditionValue.longramBytesUsed()Return the memory usage of this object in bytes.protected voidrehash(long[] fromKeys, float[] fromValues)Rehash from old buffers to new buffers.voidrelease()floatremove(long key)protected voidshiftConflictingKeys(int gapSlot)Shift all the slot-conflicting keys and values allocated to (and including)slot.intsize()StringtoString()Convert the contents of this map to a human-friendly string.LongFloatHashMap.ValuesContainervalues()protected doubleverifyLoadFactor(double loadFactor)Validate load factor range and return it.-
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface org.apache.lucene.util.Accountable
getChildResources
-
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
-
-
-
Field Detail
-
keys
public long[] keys
The array holding keys.
-
values
public float[] values
The array holding values.
-
assigned
protected int assigned
The number of stored keys (assigned key slots), excluding the special "empty" key, if any (usesize()instead).- See Also:
size()
-
mask
protected int mask
Mask for slot scans inkeys.
-
resizeAt
protected int resizeAt
-
hasEmptyKey
protected boolean hasEmptyKey
Special treatment for the "empty slot" key marker.
-
loadFactor
protected double loadFactor
The load factor forkeys.
-
iterationSeed
protected int iterationSeed
Seed used to ensure the hash iteration order is different from an iteration to another.
-
-
Constructor Detail
-
LongFloatHashMap
public LongFloatHashMap()
New instance with sane defaults.
-
LongFloatHashMap
public LongFloatHashMap(int expectedElements)
New instance with sane defaults.- Parameters:
expectedElements- The expected number of elements guaranteed not to cause buffer expansion (inclusive).
-
LongFloatHashMap
public LongFloatHashMap(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).
-
LongFloatHashMap
public LongFloatHashMap(LongFloatHashMap map)
Create a hash map from all key-value pairs of another map.
-
-
Method Detail
-
put
public float put(long key, float value)
-
putAll
public int putAll(Iterable<? extends LongFloatHashMap.LongFloatCursor> iterable)
-
putIfAbsent
public boolean putIfAbsent(long key, float value)Trove-inspired API method. An equivalent of the following code:if (!map.containsKey(key)) map.put(value);
- Parameters:
key- The key of the value to check.value- The value to put ifkeydoes not exist.- Returns:
trueifkeydid not exist andvaluewas placed in the map.
-
putOrAdd
public float putOrAdd(long key, float putValue, float 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 float addTo(long key, float 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 float remove(long key)
-
get
public float get(long key)
-
getOrDefault
public float getOrDefault(long key, float defaultValue)
-
containsKey
public boolean containsKey(long key)
-
indexOf
public int indexOf(long key)
-
indexExists
public boolean indexExists(int index)
-
indexGet
public float indexGet(int index)
-
indexReplace
public float indexReplace(int index, float newValue)
-
indexInsert
public void indexInsert(int index, long key, float value)
-
indexRemove
public float indexRemove(int index)
-
clear
public void clear()
-
release
public void release()
-
size
public int size()
-
isEmpty
public boolean isEmpty()
-
equalElements
protected boolean equalElements(LongFloatHashMap other)
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.
-
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.
-
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
-
iterator
public Iterator<LongFloatHashMap.LongFloatCursor> iterator()
- Specified by:
iteratorin interfaceIterable<LongFloatHashMap.LongFloatCursor>
-
keys
public LongFloatHashMap.KeysContainer keys()
Returns a specialized view of the keys of this associated container.
-
values
public LongFloatHashMap.ValuesContainer values()
- Returns:
- Returns a container with all values stored in this map.
-
clone
public LongFloatHashMap clone()
-
toString
public String toString()
Convert the contents of this map to a human-friendly string.
-
from
public static LongFloatHashMap from(long[] keys, float[] values)
Creates a hash map from two index-aligned arrays of key-value pairs.
-
hashKey
protected int hashKey(long 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(long[] fromKeys, float[] 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, long pendingKey, float 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.
-
-