Class LongWormSet
- java.lang.Object
-
- com.carrotsearch.hppc.LongWormSet
-
- All Implemented Interfaces:
Accountable
,LongCollection
,LongContainer
,LongLookupContainer
,LongSet
,Preallocable
,java.lang.Cloneable
,java.lang.Iterable<LongCursor>
@Generated(date="2021-06-08T13:12:55+0200", value="KTypeWormSet.java") public class LongWormSet extends java.lang.Object implements LongLookupContainer, LongSet, Preallocable, java.lang.Cloneable, Accountable
A hash set oflong
s, implemented using Worm Hashing strategy.This strategy is appropriate for a medium sized set (less than 2M keys). It takes more time to put keys in the set because it maintains chains of keys having the same hash. Then the lookup speed is fast even if the set is heavy loaded or hashes are clustered. On average it takes slightly more memory than
LongHashSet
: heavier but the load factor is higher (it varies around 80%) so it enlarges later.- See Also:
- HPPC interfaces diagram
-
-
Field Summary
Fields Modifier and Type Field Description protected int
iterationSeed
Seed used to ensure the hash iteration order is different from an iteration to another.long[]
keys
The array holding keys.byte[]
next
abs(next[i])=offset
to next chained entry index.protected int
size
Set size (number of entries).
-
Constructor Summary
Constructors Constructor Description LongWormSet()
New instance with sane defaults.LongWormSet(int expectedElements)
New instance with the provided defaults.LongWormSet(LongContainer container)
Creates a new instance from all elements of another container.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
add(long key)
Addsk
to the set.int
addAll(long... elements)
Adds all elements from the given list (vararg) to this set.int
addAll(LongContainer container)
Adds all elements from the givenLongContainer
to this set.int
addAll(java.lang.Iterable<? extends LongCursor> iterable)
Adds all elements from the given iterable to this set.protected void
allocateBuffers(int capacity)
void
clear()
Removes all elements from this collection.LongWormSet
clone()
Clones this set.boolean
contains(long key)
Lookup a given element in the container.void
ensureCapacity(int expectedElements)
Ensure this container can hold at least the given number of elements without resizing its buffers.boolean
equals(java.lang.Object o)
<T extends LongProcedure>
TforEach(T procedure)
Applies aprocedure
to all container elements.static LongWormSet
from(long... elements)
Create a set from a variable number of arguments or an array oflong
.int
hashCode()
protected int
hashKey(long key)
boolean
indexExists(int index)
long
indexGet(int index)
Returns the exact value of the existing key.void
indexInsert(int index, long key)
Inserts a key for an index that is not present in the set.int
indexOf(long key)
Returns a logical "index" of a given key that can be used to speed up follow-up logic in certain scenarios (conditional logic).void
indexRemove(int index)
Removes a key at an index previously acquired fromindexOf(long)
.long
indexReplace(int index, long equivalentKey)
Replaces the existing equivalent key with the given one and returns any previous value stored for that key.boolean
isEmpty()
Shortcut forsize() == 0
.java.util.Iterator<LongCursor>
iterator()
Returns an iterator to a cursor traversing the collection.protected int
nextIterationSeed()
Provides the next iteration seed used to build the iteration starting slot and offset increment.long
ramBytesAllocated()
Allocated memory estimationlong
ramBytesUsed()
Bytes that is actually been usedvoid
release()
Removes all elements from the collection and additionally releases any internal buffers.boolean
remove(long key)
An alias for the (preferred)removeAll(long)
.int
removeAll(long key)
Removes all occurrences ofe
from this collection.int
removeAll(LongContainer other)
Removes all keys present in a given container.int
removeAll(LongLookupContainer c)
Default implementation uses a predicate for removal.int
removeAll(LongPredicate predicate)
Removes all elements in this collection for which the given predicate returnstrue
.int
retainAll(LongLookupContainer c)
Default implementation uses a predicate for retaining.int
retainAll(LongPredicate predicate)
Default implementation redirects toLongCollection.removeAll(LongPredicate)
and negates the predicate.int
size()
Return the current number of elements in this container.long[]
toArray()
Default implementation of copying to an array.java.lang.String
toString()
Convert the contents of this container to a human-friendly string.java.lang.String
visualizeKeyDistribution(int characters)
Visually depict the distribution of keys.-
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface com.carrotsearch.hppc.LongCollection
removeAll, retainAll, retainAll
-
Methods inherited from interface com.carrotsearch.hppc.LongContainer
toArray
-
-
-
-
Field Detail
-
keys
public long[] keys
The array holding keys.
-
next
public byte[] next
abs(next[i])=offset
to next chained entry index.next[i]=0
for free bucket.The offset is always forward, and the array is considered circular, meaning that an entry at the end of the array may point to an entry at the beginning with a positive offset.
The offset is always forward, but the sign of the offset encodes head/tail of chain.
next
[i] > 0 for the first head-of-chain entry (within [1,WormUtil.maxOffset(int)
]),next
[i] < 0 for the subsequent tail-of-chain entries (within [-WormUtil.maxOffset(int)
,-1]. For the last entry in the chain,abs(next[i])=
WormUtil.END_OF_CHAIN
.
-
size
protected int size
Set size (number of entries).
-
iterationSeed
protected int iterationSeed
Seed used to ensure the hash iteration order is different from an iteration to another.
-
-
Constructor Detail
-
LongWormSet
public LongWormSet()
New instance with sane defaults.
-
LongWormSet
public LongWormSet(int expectedElements)
New instance with the provided defaults.There is no load factor parameter as this set enlarges automatically. In practice the load factor varies around 80% (between 75% and 90%). The load factor is 100% for tiny sets.
- Parameters:
expectedElements
- The expected number of elements. The capacity of the set is calculated based on it.
-
LongWormSet
public LongWormSet(LongContainer container)
Creates a new instance from all elements of another container.
-
-
Method Detail
-
from
public static LongWormSet from(long... elements)
Create a set from a variable number of arguments or an array oflong
. The elements are copied from the argument to the internal buffer.
-
clone
public LongWormSet clone()
Clones this set. The cloning operation is efficient because it copies directly the internal arrays, without having to put elements in the cloned set. The cloned set has the same elements and the same capacity as this set.- Overrides:
clone
in classjava.lang.Object
- Returns:
- A shallow copy of this set.
-
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:
size
in interfaceLongContainer
-
isEmpty
public boolean isEmpty()
Shortcut forsize() == 0
.- Specified by:
isEmpty
in interfaceLongContainer
-
contains
public boolean contains(long 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:
contains
in interfaceLongContainer
- Specified by:
contains
in interfaceLongLookupContainer
- Returns:
- Returns
true
if this container has an element equal toe
.
-
add
public boolean add(long key)
Addsk
to the set.
-
addAll
public final int addAll(long... 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(LongContainer container)
Adds all elements from the givenLongContainer
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(java.lang.Iterable<? extends LongCursor> 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).
-
remove
public boolean remove(long key)
An alias for the (preferred)removeAll(long)
.
-
removeAll
public int removeAll(long key)
Removes all occurrences ofe
from this collection.- Specified by:
removeAll
in interfaceLongCollection
- 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(LongContainer other)
Removes all keys present in a given container.- Returns:
- Returns the number of elements actually removed as a result of this call.
-
removeAll
public int removeAll(LongPredicate predicate)
Removes all elements in this collection for which the given predicate returnstrue
.- Specified by:
removeAll
in interfaceLongCollection
- Returns:
- Returns the number of removed elements.
-
forEach
public <T extends LongProcedure> T forEach(T procedure)
Applies aprocedure
to all container elements. Returns the argument (any subclass ofLongProcedure
. 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:
forEach
in interfaceLongContainer
-
forEach
public <T extends LongPredicate> T forEach(T predicate)
Applies apredicate
to container elements as long, as the predicate returnstrue
. The iteration is interrupted otherwise.- Specified by:
forEach
in interfaceLongContainer
-
iterator
public java.util.Iterator<LongCursor> 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 (LongCursor<long> c : container) { System.out.println("index=" + c.index + " value=" + c.value); }
- Specified by:
iterator
in interfacejava.lang.Iterable<LongCursor>
- Specified by:
iterator
in interfaceLongContainer
-
clear
public void clear()
Removes all elements from this collection.- Specified by:
clear
in interfaceLongCollection
- See Also:
LongCollection.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 simpleLongCollection.clear()
should be a better alternative since it'll avoid reallocation.- Specified by:
release
in interfaceLongCollection
- See Also:
LongCollection.clear()
-
equals
public boolean equals(java.lang.Object o)
- Overrides:
equals
in classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
-
hashKey
protected int hashKey(long key)
-
indexOf
public int indexOf(long 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, long)
,indexReplace(int, long)
-
indexExists
public boolean indexExists(int index)
- Parameters:
index
- The index of a given key, as returned fromindexOf(long)
.- Returns:
- Returns
true
if 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(long)
-
indexGet
public long 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(long)
-
indexReplace
public long indexReplace(int index, long 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(long)
-
indexInsert
public void indexInsert(int index, long 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(long)
.- Throws:
java.lang.AssertionError
- If assertions are enabled and the index does not correspond to an existing key.- See Also:
indexOf(long)
-
indexRemove
public void indexRemove(int index)
Removes a key at an index previously acquired fromindexOf(long)
.- Parameters:
index
- The index of the key to remove, as returned fromindexOf(long)
.- Throws:
java.lang.AssertionError
- If assertions are enabled and the index does not correspond to an existing key.- See Also:
indexOf(long)
-
toString
public java.lang.String toString()
Convert the contents of this container to a human-friendly string.
-
ensureCapacity
public void ensureCapacity(int expectedElements)
Ensure this container can hold at least the given number of elements without resizing its buffers.- Specified by:
ensureCapacity
in interfacePreallocable
- Parameters:
expectedElements
- The total number of elements, inclusive.
-
visualizeKeyDistribution
public java.lang.String visualizeKeyDistribution(int characters)
Visually depict the distribution of keys.- Specified by:
visualizeKeyDistribution
in interfaceLongSet
- 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.
-
ramBytesAllocated
public long ramBytesAllocated()
Allocated memory estimation- Specified by:
ramBytesAllocated
in interfaceAccountable
- Returns:
- Ram allocated in bytes
-
ramBytesUsed
public long ramBytesUsed()
Bytes that is actually been used- Specified by:
ramBytesUsed
in interfaceAccountable
- Returns:
- Ram used in bytes
-
allocateBuffers
protected void allocateBuffers(int capacity)
-
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.
-
removeAll
public int removeAll(LongLookupContainer c)
Default implementation uses a predicate for removal.- Specified by:
removeAll
in interfaceLongCollection
- Returns:
- Returns the number of removed elements.
-
retainAll
public int retainAll(LongLookupContainer c)
Default implementation uses a predicate for retaining.- Specified by:
retainAll
in interfaceLongCollection
- Returns:
- Returns the number of removed elements.
-
retainAll
public int retainAll(LongPredicate predicate)
Default implementation redirects toLongCollection.removeAll(LongPredicate)
and negates the predicate.- Specified by:
retainAll
in interfaceLongCollection
- Returns:
- Returns the number of removed elements.
-
toArray
public long[] toArray()
Default implementation of copying to an array.- Specified by:
toArray
in interfaceLongContainer
-
-