Class IntHashMap<VType>
java.lang.Object
org.kingdoms.utils.internal.integer.IntHashMap<VType>
- All Implemented Interfaces:
Iterable<VType>
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected intThe number of stored keys (assigned key slots), excluding the special "empty" key, if any (usesize()instead).protected boolean7 Special 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 intObject[]The array holding values. -
Constructor Summary
ConstructorsConstructorDescriptionNew instance with sane defaults.IntHashMap(int expectedElements) New instance with sane defaults.IntHashMap(int expectedElements, double loadFactor) New instance with the provided defaults. -
Method Summary
Modifier and TypeMethodDescriptionprotected voidallocateBuffers(int arraySize) Allocate new internal buffers.protected voidallocateThenInsertThenRehash(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.voidclear()booleancontainsKey(int key) voidensureCapacity(int expectedElements) Ensure this container can hold at least the given number of keys (entries) without resizing its buffers.booleanget(int key) getOrDefault(int key, VType defaultValue) inthashCode()protected inthashKey(int key) Returns a hash code for the given key.booleanisEmpty()iterator()protected voidRehash from old buffers to new buffers.voidrelease()remove(int key) protected voidshiftConflictingKeys(int gapSlot) Shift all the slot-conflicting keys and values allocated to (and including)slot.intsize()protected doubleverifyLoadFactor(double loadFactor) Validate load factor range and return it.Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, waitMethods 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 hasEmptyKey7 Special 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
-
IntHashMap
public IntHashMap()New instance with sane defaults. -
IntHashMap
public IntHashMap(int expectedElements) New instance with sane defaults.- Parameters:
expectedElements- The expected number of elements guaranteed not to cause buffer expansion (inclusive).
-
IntHashMap
public IntHashMap(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).
-
-
Method Details
-
put
-
remove
-
get
-
getOrDefault
-
containsKey
public boolean containsKey(int key) -
clear
public void clear() -
release
public void release() -
size
public int size() -
isEmpty
public boolean isEmpty() -
hashCode
public int hashCode() -
equals
-
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.
-
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. -
iterator
-