Class IntHashSet
java.lang.Object
org.kingdoms.utils.internal.integer.IntHashSet
- All Implemented Interfaces:
Iterable<IntHashSet.IntCursor>
A hash set of
ints, implemented using using open addressing
with linear probing for collision resolution.- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected final classAn iterator implementation foriterator().static final class -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected intThe number of stored keys (assigned key slots), excluding the special "empty" key, if any.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 hash array holding keys.protected doubleThe load factor forkeys.protected intMask for slot scans inkeys.protected int -
Constructor Summary
ConstructorsConstructorDescriptionNew instance with sane defaults.IntHashSet(int expectedElements) New instance with sane defaults.IntHashSet(int expectedElements, double loadFactor) New instance with the provided defaults. -
Method Summary
Modifier and TypeMethodDescriptionbooleanadd(int key) protected voidallocateBuffers(int arraySize) Allocate new internal buffers.protected voidallocateThenInsertThenRehash(int slot, int pendingKey) This method is invoked when there is a new key to be inserted into the buffer but there is not enough empty slots to do so.voidclear()booleancontains(int key) voidensureCapacity(int expectedElements) Ensure this container can hold at least the given number of elements without resizing its buffers.booleaninthashCode()protected inthashKey(int key) Returns a hash code for the given key.intindexOf(int key) Returns a logical "index" of a given key that can be used to speed up follow-up logic in certain scenarios (conditional logic).booleanisEmpty()iterator()protected intProvides the next iteration seed used to build the iteration starting slot and offset increment.protected voidrehash(int[] fromKeys) Rehash from old buffers to new buffers.voidrelease()booleanremove(int key) protected voidshiftConflictingKeys(int gapSlot) Shift all the slot-conflicting keys allocated to (and including)slot.intsize()int[]toArray()toString()Convert the contents of this container to a human-friendly string.protected doubleverifyLoadFactor(double loadFactor) Validate load factor range and return it.Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.lang.Iterable
forEach, spliterator
-
Field Details
-
keys
public int[] keysThe hash array holding keys. -
assigned
protected int assignedThe number of stored keys (assigned key slots), excluding the special "empty" key, if any.- 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
-
IntHashSet
public IntHashSet()New instance with sane defaults.- See Also:
-
IntHashSet
public IntHashSet(int expectedElements) New instance with sane defaults.- See Also:
-
IntHashSet
public IntHashSet(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
-
toString
Convert the contents of this container to a human-friendly string. -
add
public boolean add(int key) -
toArray
public int[] toArray() -
remove
public boolean remove(int key) -
contains
public boolean contains(int key) -
clear
public void clear() -
release
public void release() -
isEmpty
public boolean isEmpty() -
ensureCapacity
public void ensureCapacity(int expectedElements) Ensure this container can hold at least the given number of elements without resizing its buffers.- Parameters:
expectedElements- The total number of elements, inclusive.
-
size
public int size() -
hashCode
public int hashCode() -
equals
-
iterator
- Specified by:
iteratorin interfaceIterable<IntHashSet.IntCursor>
-
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. -
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.
-
indexOf
public int indexOf(int key) Returns a logical "index" of a given key that can be used to speed up follow-up logic in certain scenarios (conditional logic).The semantics of "indexes" are not strictly defined. Indexes may (and typically won't be) contiguous.
The index is valid only between modifications (it will not be affected by read-only operations)
- Parameters:
key- The key to locate in the set.- Returns:
- A non-negative value of the logical "index" of the key in the set or a negative value if the key did not exist.
-
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) 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) This method is invoked when there is a new key to be inserted into the buffer 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 allocated to (and including)slot.
-