- All Implemented Interfaces:
Accountable,IntCollection,IntContainer,IntLookupContainer,IntSet,Preallocable,Cloneable,Iterable<IntCursor>
ints, implemented using open addressing with linear probing for
collision resolution.- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected final classAn iterator implementation foriterator(). -
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.IntHashSet(IntContainer container) New instance copying elements from anotherIntContainer. -
Method Summary
Modifier and TypeMethodDescriptionbooleanadd(int key) Addskto the set.final intaddAll(int... elements) Adds all elements from the given list (vararg) to this set.intaddAll(IntContainer container) Adds all elements from the givenIntContainerto this set.intAdds all elements from the given iterable to this set.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()Removes all elements from this collection.clone()booleancontains(int key) Lookup a given element in the container.voidensureCapacity(int expectedElements) Ensure this container can hold at least the given number of elements without resizing its buffers.boolean<T extends IntProcedure>
TforEach(T procedure) Applies aprocedureto all container elements.static IntHashSetfrom(int... elements) Create a set from a variable number of arguments or an array ofint.inthashCode()protected inthashKey(int key) Returns a hash code for the given key.booleanindexExists(int index) intindexGet(int index) Returns the exact value of the existing key.voidindexInsert(int index, int key) Inserts a key for an index that is not present in the set.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).voidindexRemove(int index) Removes a key at an index previously acquired fromindexOf(int).intindexReplace(int index, int equivalentKey) Replaces the existing equivalent key with the given one and returns any previous value stored for that key.booleanisEmpty()Shortcut forsize() == 0.iterator()Returns an iterator to a cursor traversing the collection.protected intProvides the next iteration seed used to build the iteration starting slot and offset increment.longAllocated memory estimationlongBytes that is actually been usedprotected voidrehash(int[] fromKeys) Rehash from old buffers to new buffers.voidrelease()Removes all elements from the collection and additionally releases any internal buffers.booleanremove(int key) An alias for the (preferred)removeAll(int).intremoveAll(int key) Removes all occurrences ofefrom this collection.intremoveAll(IntContainer other) Removes all keys present in a given container.intDefault implementation uses a predicate for removal.intremoveAll(IntPredicate predicate) Removes all elements in this collection for which the given predicate returnstrue.intDefault implementation uses a predicate for retaining.intretainAll(IntPredicate predicate) Default implementation redirects toIntCollection.removeAll(IntPredicate)and negates the predicate.protected voidshiftConflictingKeys(int gapSlot) Shift all the slot-conflicting keys allocated to (and including)slot.intsize()Return the current number of elements in this container.int[]toArray()Default implementation of copying to an array.toString()Convert the contents of this container to a human-friendly string.protected doubleverifyLoadFactor(double loadFactor) Validate load factor range and return it.visualizeKeyDistribution(int characters) Visually depict the distribution of keys.Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface com.carrotsearch.hppc.IntCollection
removeAll, retainAll, retainAllMethods 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).
-
IntHashSet
New instance copying elements from anotherIntContainer.
-
-
Method Details
-
add
public boolean add(int key) Addskto the set. -
addAll
public final int addAll(int... elements) Adds all elements from the given list (vararg) to this set.- Returns:
- Returns the number of elements actually added as a result of this call (not previously present in the set).
-
addAll
Adds all elements from the givenIntContainerto this set. -
addAll
Adds all elements from the given iterable to this set.- Returns:
- Returns the number of elements actually added as a result of this call (not previously present in the set).
-
toArray
public int[] toArray()Default implementation of copying to an array.- Specified by:
toArrayin interfaceIntContainer
-
remove
public boolean remove(int key) An alias for the (preferred)removeAll(int). -
removeAll
public int removeAll(int key) Removes all occurrences ofefrom this collection.- Specified by:
removeAllin interfaceIntCollection- Parameters:
key- Element to be removed from this collection, if present.- Returns:
- The number of removed elements as a result of this call.
-
removeAll
Removes all keys present in a given container.- Returns:
- Returns the number of elements actually removed as a result of this call.
-
removeAll
Removes all elements in this collection for which the given predicate returnstrue.- Specified by:
removeAllin interfaceIntCollection- Returns:
- Returns the number of removed elements.
-
contains
public boolean contains(int key) Lookup a given element in the container. This operation has no speed guarantees (may be linear with respect to the size of this container).- Specified by:
containsin interfaceIntContainer- Specified by:
containsin interfaceIntLookupContainer- Returns:
- Returns
trueif this container has an element equal toe.
-
clear
public void clear()Removes all elements from this collection.- Specified by:
clearin interfaceIntCollection- See Also:
-
release
public void release()Removes all elements from the collection and additionally releases any internal buffers. Typically, if the object is to be reused, a simpleIntCollection.clear()should be a better alternative since it'll avoid reallocation.- Specified by:
releasein interfaceIntCollection- See Also:
-
isEmpty
public boolean isEmpty()Shortcut forsize() == 0.- Specified by:
isEmptyin interfaceIntContainer
-
ensureCapacity
public void ensureCapacity(int expectedElements) Ensure this container can hold at least the given number of elements without resizing its buffers.- Specified by:
ensureCapacityin interfacePreallocable- Parameters:
expectedElements- The total number of elements, inclusive.
-
size
public int size()Return the current number of elements in this container. The time for calculating the container's size may takeO(n)time, although implementing classes should try to maintain the current size and return in constant time.- Specified by:
sizein interfaceIntContainer
-
hashCode
public int hashCode() -
equals
-
clone
-
iterator
Returns an iterator to a cursor traversing the collection. The order of traversal is not defined. More than one cursor may be active at a time. The behavior of iterators is undefined if structural changes are made to the underlying collection.The iterator is implemented as a cursor and it returns the same cursor instance on every call to
Iterator.next()(to avoid boxing of primitive types). To read the current list's value (or index in the list) use the cursor's public fields. An example is shown below.for (IntCursor<int> c : container) { System.out.println("index=" + c.index + " value=" + c.value); }- Specified by:
iteratorin interfaceIntContainer- Specified by:
iteratorin interfaceIterable<IntCursor>
-
ramBytesAllocated
public long ramBytesAllocated()Description copied from interface:AccountableAllocated memory estimation- Specified by:
ramBytesAllocatedin interfaceAccountable- Returns:
- Ram allocated in bytes
-
ramBytesUsed
public long ramBytesUsed()Description copied from interface:AccountableBytes that is actually been used- Specified by:
ramBytesUsedin interfaceAccountable- Returns:
- Ram used in bytes
-
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. -
forEach
Applies aprocedureto all container elements. Returns the argument (any subclass ofIntProcedure. This lets the caller to call methods of the argument by chaining the call (even if the argument is an anonymous type) to retrieve computed values, for example (IntContainer):int count = container.forEach(new IntProcedure() { int count; // this is a field declaration in an anonymous class. public void apply(int value) { count++; } }).count;- Specified by:
forEachin interfaceIntContainer
-
forEach
Applies apredicateto container elements as long, as the predicate returnstrue. The iteration is interrupted otherwise.- Specified by:
forEachin interfaceIntContainer
-
from
Create a set from a variable number of arguments or an array ofint. The elements are copied from the argument to the internal buffer. -
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.
- See Also:
-
indexExists
public boolean indexExists(int index) - Parameters:
index- The index of a given key, as returned fromindexOf(int).- Returns:
- Returns
trueif the index corresponds to an existing key or false otherwise. This is equivalent to checking whether the index is a positive value (existing keys) or a negative value (non-existing keys). - See Also:
-
indexGet
public int indexGet(int index) Returns the exact value of the existing key. This method makes sense for sets of objects which define custom key-equality relationship.- Parameters:
index- The index of an existing key.- Returns:
- Returns the equivalent key currently stored in the set.
- Throws:
AssertionError- If assertions are enabled and the index does not correspond to an existing key.- See Also:
-
indexReplace
public int indexReplace(int index, int equivalentKey) Replaces the existing equivalent key with the given one and returns any previous value stored for that key.- Parameters:
index- The index of an existing key.equivalentKey- The key to put in the set as a replacement. Must be equivalent to the key currently stored at the provided index.- Returns:
- Returns the previous key stored in the set.
- Throws:
AssertionError- If assertions are enabled and the index does not correspond to an existing key.- See Also:
-
indexInsert
public void indexInsert(int index, int key) Inserts a key for an index that is not present in the set. This method may help in avoiding double recalculation of the key's hash.- Parameters:
index- The index of a previously non-existing key, as returned fromindexOf(int).- Throws:
AssertionError- If assertions are enabled and the index does not correspond to an existing key.- See Also:
-
indexRemove
public void indexRemove(int index) Removes a key at an index previously acquired fromindexOf(int).- Parameters:
index- The index of the key to remove, as returned fromindexOf(int).- Throws:
AssertionError- If assertions are enabled and the index does not correspond to an existing key.- See Also:
-
visualizeKeyDistribution
Description copied from interface:IntSetVisually depict the distribution of keys.- Specified by:
visualizeKeyDistributionin interfaceIntSet- Parameters:
characters- The number of characters to "squeeze" the entire buffer into.- Returns:
- Returns a sequence of characters where '.' depicts an empty fragment of the internal buffer and 'X' depicts full or nearly full capacity within the buffer's range and anything between 1 and 9 is between.
-
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. -
removeAll
Default implementation uses a predicate for removal.- Specified by:
removeAllin interfaceIntCollection- Returns:
- Returns the number of removed elements.
-
retainAll
Default implementation uses a predicate for retaining.- Specified by:
retainAllin interfaceIntCollection- Returns:
- Returns the number of removed elements.
-
retainAll
Default implementation redirects toIntCollection.removeAll(IntPredicate)and negates the predicate.- Specified by:
retainAllin interfaceIntCollection- Returns:
- Returns the number of removed elements.
-
toString
Convert the contents of this container to a human-friendly string.
-