Package org.apache.lucene.internal.hppc
Class IntObjectHashMap<VType>
java.lang.Object
org.apache.lucene.internal.hppc.IntObjectHashMap<VType>
- All Implemented Interfaces:
Cloneable
,Iterable<IntObjectHashMap.IntObjectCursor<VType>>
,Accountable
public class IntObjectHashMap<VType>
extends Object
implements Iterable<IntObjectHashMap.IntObjectCursor<VType>>, Accountable, Cloneable
A hash map of
int
to Object
, implemented using open addressing with
linear probing for collision resolution. Supports null values.
Mostly forked and trimmed from com.carrotsearch.hppc.IntObjectHashMap
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 class
Forked from HPPC, holding int index,key and valuefinal class
A view of the keys inside this hash map.final class
A view over the set of values of this map. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected int
The number of stored keys (assigned key slots), excluding the special "empty" key, if any (usesize()
instead).protected boolean
Special treatment for the "empty slot" key marker.protected int
Seed used to ensure the hash iteration order is different from an iteration to another.int[]
The array holding keys.protected double
The load factor forkeys
.protected int
Mask for slot scans inkeys
.protected int
Object[]
The array holding values.Fields inherited from interface org.apache.lucene.util.Accountable
NULL_ACCOUNTABLE
-
Constructor Summary
ConstructorsConstructorDescriptionNew instance with sane defaults.IntObjectHashMap
(int expectedElements) New instance with sane defaults.IntObjectHashMap
(int expectedElements, double loadFactor) New instance with the provided defaults.Create a hash map from all key-value pairs of another map. -
Method Summary
Modifier and TypeMethodDescriptionprotected void
allocateBuffers
(int arraySize) Allocate new internal buffers.protected void
allocateThenInsertThenRehash
(int slot, int pendingKey, VType 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.void
clear()
clone()
boolean
containsKey
(int key) void
ensureCapacity
(int expectedElements) Ensure this container can hold at least the given number of keys (entries) without resizing its buffers.protected boolean
equalElements
(IntObjectHashMap<?> other) Return true if all keys of some other container exist in this container.boolean
static <VType> IntObjectHashMap
<VType> from
(int[] keys, VType[] values) Creates a hash map from two index-aligned arrays of key-value pairs.get
(int key) getOrDefault
(int key, VType defaultValue) int
hashCode()
protected int
hashKey
(int key) Returns a hash code for the given key.boolean
indexExists
(int index) indexGet
(int index) void
indexInsert
(int index, int key, VType value) int
indexOf
(int key) indexRemove
(int index) indexReplace
(int index, VType newValue) boolean
isEmpty()
iterator()
keys()
Returns a specialized view of the keys of this associated container.protected int
Provides the next iteration seed used to build the iteration starting slot and offset increment.int
putAll
(Iterable<? extends IntObjectHashMap.IntObjectCursor<? extends VType>> iterable) boolean
putIfAbsent
(int key, VType value) Trove-inspired API method.long
Return the memory usage of this object in bytes.protected void
Rehash from old buffers to new buffers.void
release()
remove
(int key) protected void
shiftConflictingKeys
(int gapSlot) Shift all the slot-conflicting keys and values allocated to (and including)slot
.int
size()
toString()
Convert the contents of this map to a human-friendly string.values()
protected double
verifyLoadFactor
(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 Details
-
keys
public int[] keysThe array holding keys. -
values
The 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
-
IntObjectHashMap
public IntObjectHashMap()New instance with sane defaults. -
IntObjectHashMap
public IntObjectHashMap(int expectedElements) New instance with sane defaults.- Parameters:
expectedElements
- The expected number of elements guaranteed not to cause buffer expansion (inclusive).
-
IntObjectHashMap
public IntObjectHashMap(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)
.
-
IntObjectHashMap
Create a hash map from all key-value pairs of another map.
-
-
Method Details
-
put
-
putAll
-
putIfAbsent
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 ifkey
does not exist.- Returns:
true
ifkey
did not exist andvalue
was placed in the map.
-
remove
-
get
-
getOrDefault
-
containsKey
public boolean containsKey(int key) -
indexOf
public int indexOf(int key) -
indexExists
public boolean indexExists(int index) -
indexGet
-
indexReplace
-
indexInsert
-
indexRemove
-
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.
-
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
-
ramBytesUsed
public long ramBytesUsed()Description copied from interface:Accountable
Return the memory usage of this object in bytes. Negative values are illegal.- Specified by:
ramBytesUsed
in interfaceAccountable
-
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
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
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
.
-