Package com.carrotsearch.hppc
Class ByteHashSet
- java.lang.Object
-
- com.carrotsearch.hppc.AbstractByteCollection
-
- com.carrotsearch.hppc.ByteHashSet
-
- All Implemented Interfaces:
ByteCollection,ByteContainer,ByteLookupContainer,ByteSet,Preallocable,java.lang.Cloneable,java.lang.Iterable<ByteCursor>
- Direct Known Subclasses:
ByteScatterSet
@Generated(date="2024-05-16T12:00:00+0000", value="KTypeHashSet.java") public class ByteHashSet extends AbstractByteCollection implements ByteLookupContainer, ByteSet, Preallocable, java.lang.CloneableA hash set ofbytes, implemented using using open addressing with linear probing for collision resolution.Note: read about important differences between hash and scatter sets.
- See Also:
ByteScatterSet, HPPC interfaces diagram
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected classByteHashSet.EntryIteratorAn iterator implementation foriterator().
-
Field Summary
Fields Modifier and Type Field Description protected intassignedThe number of stored keys (assigned key slots), excluding the special "empty" key, if any.protected booleanhasEmptyKeySpecial treatment for the "empty slot" key marker.protected intkeyMixerWe perturb hash values with a container-unique seed to avoid problems with nearly-sorted-by-hash values on iterations.byte[]keysThe hash array holding keys.protected doubleloadFactorThe load factor forkeys.protected intmaskMask for slot scans inkeys.protected HashOrderMixingStrategyorderMixerPer-instance hash order mixing strategy.protected intresizeAt
-
Constructor Summary
Constructors Constructor Description ByteHashSet()New instance with sane defaults.ByteHashSet(int expectedElements)New instance with sane defaults.ByteHashSet(int expectedElements, double loadFactor)New instance with sane defaults.ByteHashSet(int expectedElements, double loadFactor, HashOrderMixingStrategy orderMixer)New instance with the provided defaults.ByteHashSet(ByteContainer container)New instance copying elements from anotherByteContainer.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanadd(byte key)Addskto the set.intaddAll(byte... elements)Adds all elements from the given list (vararg) to this set.intaddAll(ByteContainer container)Adds all elements from the givenByteContainerto this set.intaddAll(java.lang.Iterable<? extends ByteCursor> iterable)Adds all elements from the given iterable to this set.protected voidallocateBuffers(int arraySize)Allocate new internal buffers.protected voidallocateThenInsertThenRehash(int slot, byte 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.ByteHashSetclone()booleancontains(byte 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.booleanequals(java.lang.Object obj)<T extends ByteProcedure>
TforEach(T procedure)Applies aprocedureto all container elements.static ByteHashSetfrom(byte... elements)Create a set from a variable number of arguments or an array ofbyte.inthashCode()protected inthashKey(byte key)Returns a hash code for the given key.booleanindexExists(int index)byteindexGet(int index)Returns the exact value of the existing key.voidindexInsert(int index, byte key)Inserts a key for an index that is not present in the set.intindexOf(byte key)Returns a logical "index" of a given key that can be used to speed up follow-up logic in certain scenarios (conditional logic).byteindexReplace(int index, byte equivalentKey)Replaces the existing equivalent key with the given one and returns any previous value stored for that key.booleanisEmpty()Shortcut forsize() == 0.java.util.Iterator<ByteCursor>iterator()Returns an iterator to a cursor traversing the collection.protected voidrehash(byte[] fromKeys)Rehash from old buffers to new buffers.voidrelease()Removes all elements from the collection and additionally releases any internal buffers.booleanremove(byte key)An alias for the (preferred)removeAll(byte).intremoveAll(byte key)Removes all occurrences ofefrom this collection.intremoveAll(BytePredicate predicate)Removes all elements in this collection for which the given predicate returnstrue.private booleansameKeys(ByteSet other)Return true if all keys of some other container exist in this container.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.byte[]toArray()Default implementation of copying to an array.protected doubleverifyLoadFactor(double loadFactor)Validate load factor range and return it.java.lang.StringvisualizeKeyDistribution(int characters)Visually depict the distribution of keys.-
Methods inherited from class com.carrotsearch.hppc.AbstractByteCollection
removeAll, retainAll, retainAll, toString
-
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface com.carrotsearch.hppc.ByteCollection
removeAll, retainAll, retainAll
-
-
-
-
Field Detail
-
keys
public byte[] keys
The hash array holding keys.
-
assigned
protected int assigned
The number of stored keys (assigned key slots), excluding the special "empty" key, if any.- See Also:
size(),hasEmptyKey
-
mask
protected int mask
Mask for slot scans inkeys.
-
keyMixer
protected int keyMixer
We perturb hash values with a container-unique seed to avoid problems with nearly-sorted-by-hash values on iterations.- See Also:
hashKey(byte), "http://issues.carrot2.org/browse/HPPC-80", "http://issues.carrot2.org/browse/HPPC-103"
-
resizeAt
protected int resizeAt
-
hasEmptyKey
protected boolean hasEmptyKey
Special treatment for the "empty slot" key marker.
-
loadFactor
protected double loadFactor
The load factor forkeys.
-
orderMixer
protected HashOrderMixingStrategy orderMixer
Per-instance hash order mixing strategy.- See Also:
keyMixer
-
-
Constructor Detail
-
ByteHashSet
public ByteHashSet()
New instance with sane defaults.
-
ByteHashSet
public ByteHashSet(int expectedElements)
New instance with sane defaults.
-
ByteHashSet
public ByteHashSet(int expectedElements, double loadFactor)New instance with sane defaults.
-
ByteHashSet
public ByteHashSet(int expectedElements, double loadFactor, HashOrderMixingStrategy orderMixer)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).orderMixer- Hash key order mixing strategy. SeeHashOrderMixingfor predefined implementations. Use constant mixers only if you understand the potential consequences.
-
ByteHashSet
public ByteHashSet(ByteContainer container)
New instance copying elements from anotherByteContainer.
-
-
Method Detail
-
add
public boolean add(byte key)
Addskto the set.
-
addAll
public final int addAll(byte... 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
public int addAll(ByteContainer container)
Adds all elements from the givenByteContainerto this set.- Returns:
- Returns the number of elements actually added as a result of this call (not previously present in the set).
-
addAll
public int addAll(java.lang.Iterable<? extends ByteCursor> iterable)
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 byte[] toArray()
Default implementation of copying to an array.- Specified by:
toArrayin interfaceByteContainer- Overrides:
toArrayin classAbstractByteCollection
-
remove
public boolean remove(byte key)
An alias for the (preferred)removeAll(byte).
-
removeAll
public int removeAll(byte key)
Removes all occurrences ofefrom this collection.- Specified by:
removeAllin interfaceByteCollection- Parameters:
key- Element to be removed from this collection, if present.- Returns:
- The number of removed elements as a result of this call.
-
removeAll
public int removeAll(BytePredicate predicate)
Removes all elements in this collection for which the given predicate returnstrue.- Specified by:
removeAllin interfaceByteCollection- Returns:
- Returns the number of removed elements.
-
contains
public boolean contains(byte 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 interfaceByteContainer- Specified by:
containsin interfaceByteLookupContainer- Returns:
- Returns
trueif this container has an element equal toe.
-
clear
public void clear()
Removes all elements from this collection.- Specified by:
clearin interfaceByteCollection- See Also:
ByteCollection.release()
-
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 simpleByteCollection.clear()should be a better alternative since it'll avoid reallocation.- Specified by:
releasein interfaceByteCollection- See Also:
ByteCollection.clear()
-
isEmpty
public boolean isEmpty()
Shortcut forsize() == 0.- Specified by:
isEmptyin interfaceByteContainer
-
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 interfaceByteContainer
-
hashCode
public int hashCode()
- Overrides:
hashCodein classjava.lang.Object
-
equals
public boolean equals(java.lang.Object obj)
- Overrides:
equalsin classjava.lang.Object
-
sameKeys
private boolean sameKeys(ByteSet other)
Return true if all keys of some other container exist in this container.
-
clone
public ByteHashSet clone()
- Overrides:
clonein classjava.lang.Object
-
iterator
public java.util.Iterator<ByteCursor> 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 (ByteCursor<byte> c : container) { System.out.println("index=" + c.index + " value=" + c.value); }- Specified by:
iteratorin interfaceByteContainer- Specified by:
iteratorin interfacejava.lang.Iterable<ByteCursor>
-
forEach
public <T extends ByteProcedure> T forEach(T procedure)
Applies aprocedureto all container elements. Returns the argument (any subclass ofByteProcedure. 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 interfaceByteContainer
-
forEach
public <T extends BytePredicate> T forEach(T predicate)
Applies apredicateto container elements as long, as the predicate returnstrue. The iteration is interrupted otherwise.- Specified by:
forEachin interfaceByteContainer
-
from
public static ByteHashSet from(byte... elements)
Create a set from a variable number of arguments or an array ofbyte. The elements are copied from the argument to the internal buffer.
-
hashKey
protected int hashKey(byte key)
Returns a hash code for the given key. The default implementation mixes the hash of the key withkeyMixerto differentiate hash order of keys between hash containers. Helps alleviate problems resulting from linear conflict resolution in open addressing. The output from this function should evenly distribute keys across the entire integer range.
-
indexOf
public int indexOf(byte 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(int),indexGet(int),indexInsert(int, byte),indexReplace(int, byte)
-
indexExists
public boolean indexExists(int index)
- Parameters:
index- The index of a given key, as returned fromindexOf(byte).- 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:
indexOf(byte)
-
indexGet
public byte 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:
java.lang.AssertionError- If assertions are enabled and the index does not correspond to an existing key.- See Also:
indexOf(byte)
-
indexReplace
public byte indexReplace(int index, byte 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:
java.lang.AssertionError- If assertions are enabled and the index does not correspond to an existing key.- See Also:
indexOf(byte)
-
indexInsert
public void indexInsert(int index, byte 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(byte).- Throws:
java.lang.AssertionError- If assertions are enabled and the index does not correspond to an existing key.- See Also:
indexOf(byte)
-
visualizeKeyDistribution
public java.lang.String visualizeKeyDistribution(int characters)
Description copied from interface:ByteSetVisually depict the distribution of keys.- Specified by:
visualizeKeyDistributionin interfaceByteSet- 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(byte[] 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, byte 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.
-
-