Class LongHashSet

All Implemented Interfaces:
LongCollection, LongContainer, LongLookupContainer, LongSet, Preallocable, Cloneable, Iterable<LongCursor>
Direct Known Subclasses:
LongScatterSet

@Generated(date="2024-05-16T08:18:11+0000", value="KTypeHashSet.java") public class LongHashSet extends AbstractLongCollection implements LongLookupContainer, LongSet, Preallocable, Cloneable
A hash set of longs, implemented using using open addressing with linear probing for collision resolution.

Note: read about important differences between hash and scatter sets.

See Also:
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    protected final class 
    An iterator implementation for iterator().
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected int
    The number of stored keys (assigned key slots), excluding the special "empty" key, if any.
    protected boolean
    Special treatment for the "empty slot" key marker.
    protected int
    We perturb hash values with a container-unique seed to avoid problems with nearly-sorted-by-hash values on iterations.
    long[]
    The hash array holding keys.
    protected double
    The load factor for keys.
    protected int
    Mask for slot scans in keys.
    Per-instance hash order mixing strategy.
    protected int
    Expand (rehash) keys when assigned hits this value.
  • Constructor Summary

    Constructors
    Constructor
    Description
    New instance with sane defaults.
    LongHashSet(int expectedElements)
    New instance with sane defaults.
    LongHashSet(int expectedElements, double loadFactor)
    New instance with sane defaults.
    LongHashSet(int expectedElements, double loadFactor, HashOrderMixingStrategy orderMixer)
    New instance with the provided defaults.
    New instance copying elements from another LongContainer.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    add(long key)
    Adds k to the set.
    final int
    addAll(long... elements)
    Adds all elements from the given list (vararg) to this set.
    int
    addAll(LongContainer container)
    Adds all elements from the given LongContainer to this set.
    int
    addAll(Iterable<? extends LongCursor> iterable)
    Adds all elements from the given iterable to this set.
    protected void
    allocateBuffers(int arraySize)
    Allocate new internal buffers.
    protected void
    allocateThenInsertThenRehash(int slot, long 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.
    void
    Removes all elements from this collection.
    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
    <T extends LongProcedure>
    T
    forEach(T procedure)
    Applies a procedure to all container elements.
    from(long... elements)
    Create a set from a variable number of arguments or an array of long.
    int
    protected int
    hashKey(long key)
    Returns a hash code for the given 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).
    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
    Shortcut for size() == 0.
    Returns an iterator to a cursor traversing the collection.
    protected void
    rehash(long[] fromKeys)
    Rehash from old buffers to new buffers.
    void
    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 of e from this collection.
    int
    Removes all elements in this collection for which the given predicate returns true.
    private boolean
    Return true if all keys of some other container exist in this container.
    protected void
    shiftConflictingKeys(int gapSlot)
    Shift all the slot-conflicting keys allocated to (and including) slot.
    int
    Return the current number of elements in this container.
    long[]
    Default implementation of copying to an array.
    protected double
    verifyLoadFactor(double loadFactor)
    Validate load factor range and return it.
    visualizeKeyDistribution(int characters)
    Visually depict the distribution of keys.

    Methods inherited from class com.carrotsearch.hppc.AbstractLongCollection

    removeAll, retainAll, retainAll, toString

    Methods inherited from class java.lang.Object

    finalize, getClass, notify, notifyAll, wait, wait, wait

    Methods inherited from interface java.lang.Iterable

    forEach, spliterator

    Methods inherited from interface com.carrotsearch.hppc.LongCollection

    removeAll, retainAll, retainAll
  • Field Details

    • keys

      public long[] 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:
    • mask

      protected int mask
      Mask for slot scans in keys.
    • 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(long)
      • "http://issues.carrot2.org/browse/HPPC-80"
      • "http://issues.carrot2.org/browse/HPPC-103"
    • resizeAt

      protected int resizeAt
      Expand (rehash) keys when assigned hits this value.
    • hasEmptyKey

      protected boolean hasEmptyKey
      Special treatment for the "empty slot" key marker.
    • loadFactor

      protected double loadFactor
      The load factor for keys.
    • orderMixer

      protected HashOrderMixingStrategy orderMixer
      Per-instance hash order mixing strategy.
      See Also:
  • Constructor Details

  • Method Details

    • add

      public boolean add(long key)
      Adds k to the set.
      Specified by:
      add in interface LongSet
      Returns:
      Returns true if this element was not part of the set before. Returns false if an equal element is part of the set, and replaces the existing equal element with the argument (if keys are object types).
    • 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 given LongContainer 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(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).
    • toArray

      public long[] toArray()
      Default implementation of copying to an array.
      Specified by:
      toArray in interface LongContainer
      Overrides:
      toArray in class AbstractLongCollection
    • remove

      public boolean remove(long key)
      An alias for the (preferred) removeAll(long).
    • removeAll

      public int removeAll(long key)
      Removes all occurrences of e from this collection.
      Specified by:
      removeAll in interface LongCollection
      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(LongPredicate predicate)
      Removes all elements in this collection for which the given predicate returns true.
      Specified by:
      removeAll in interface LongCollection
      Returns:
      Returns the number of removed elements.
    • 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 interface LongContainer
      Specified by:
      contains in interface LongLookupContainer
      Returns:
      Returns true if this container has an element equal to e.
    • clear

      public void clear()
      Removes all elements from this collection.
      Specified by:
      clear in interface LongCollection
      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 simple LongCollection.clear() should be a better alternative since it'll avoid reallocation.
      Specified by:
      release in interface LongCollection
      See Also:
    • isEmpty

      public boolean isEmpty()
      Shortcut for size() == 0.
      Specified by:
      isEmpty in interface LongContainer
    • 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 interface Preallocable
      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 take O(n) time, although implementing classes should try to maintain the current size and return in constant time.
      Specified by:
      size in interface LongContainer
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • sameKeys

      private boolean sameKeys(LongSet other)
      Return true if all keys of some other container exist in this container.
    • clone

      public LongHashSet clone()
      Overrides:
      clone in class Object
    • iterator

      public 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 interface Iterable<LongCursor>
      Specified by:
      iterator in interface LongContainer
    • forEach

      public <T extends LongProcedure> T forEach(T procedure)
      Applies a procedure to all container elements. Returns the argument (any subclass of LongProcedure. 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 interface LongContainer
    • forEach

      public <T extends LongPredicate> T forEach(T predicate)
      Applies a predicate to container elements as long, as the predicate returns true. The iteration is interrupted otherwise.
      Specified by:
      forEach in interface LongContainer
    • from

      public static LongHashSet from(long... elements)
      Create a set from a variable number of arguments or an array of long. The elements are copied from the argument to the internal buffer.
    • hashKey

      protected int hashKey(long key)
      Returns a hash code for the given key. The default implementation mixes the hash of the key with keyMixer to 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(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

      public boolean indexExists(int index)
      Parameters:
      index - The index of a given key, as returned from indexOf(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:
    • 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:
      AssertionError - If assertions are enabled and the index does not correspond to an existing key.
      See Also:
    • 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:
      AssertionError - If assertions are enabled and the index does not correspond to an existing key.
      See Also:
    • 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 from indexOf(long).
      Throws:
      AssertionError - If assertions are enabled and the index does not correspond to an existing key.
      See Also:
    • visualizeKeyDistribution

      public String visualizeKeyDistribution(int characters)
      Description copied from interface: LongSet
      Visually depict the distribution of keys.
      Specified by:
      visualizeKeyDistribution in interface LongSet
      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(long[] 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, long 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.