IntHashSet (Lucene 10.5.1 core API)
Description: declaration: package: org.apache.lucene.internal.hppc, class: IntHashSet

Package org.apache.lucene.internal.hppc

# Class IntHashSet

java.lang.Object
org.apache.lucene.internal.hppc.IntHashSet

All Implemented Interfaces:
  `Cloneable`, `Iterable<IntCursor>`, `Accountable`

public class IntHashSet extends Object implements Iterable<IntCursor>, Accountable, Cloneable
A hash set of `int`s, implemented using open addressing with linear probing for collision resolution.

Mostly forked and trimmed from com.carrotsearch.hppc.IntHashSet

github: https://github.com/carrotsearch/hppc release 0.10.0

NOTE: This API is for internal purposes only and might change in incompatible ways in the next release.

- ## Nested Class Summary

  Nested Classes
  Modifier and Type
  Class
  Description
  `protected final class`
  `IntHashSet.EntryIterator`
  An iterator implementation for `iterator()`.

- ## Field Summary

  Fields
  Modifier and Type
  Field
  Description
  `protected int`
  `assigned`
  The number of stored keys (assigned key slots), excluding the special "empty" key, if any.
  `protected boolean`
  `hasEmptyKey`
  Special treatment for the "empty slot" key marker.
  `protected int`
  `iterationSeed`
  Seed used to ensure the hash iteration order is different from an iteration to another.
  `int[]`
  `keys`
  The hash array holding keys.
  `protected double`
  `loadFactor`
  The load factor for `keys`.
  `protected int`
  `mask`
  Mask for slot scans in `keys`.
  `protected int`
  `resizeAt`
  Expand (rehash) `keys` when `assigned` hits this value.

  ### Fields inherited from interface org.apache.lucene.util.Accountable

  `NULL_ACCOUNTABLE`

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `IntHashSet()`
  New instance with sane defaults.
  `IntHashSet(int expectedElements)`
  New instance with sane defaults.
  `IntHashSet(int expectedElements, double loadFactor)`
  New instance with the provided defaults.
  `IntHashSet(Collection<Integer> collection)`
  New instance copying elements from another collection.
  `IntHashSet(IntHashSet set)`
  New instance copying elements from another set.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `boolean`
  `add(int key)`
  `final int`
  `addAll(int... elements)`
  Adds all elements from the given list (vararg) to this set.
  `int`
  `addAll(Iterable<? extends IntCursor> iterable)`
  Adds all elements from the given iterable to this set.
  `int`
  `addAll(Collection<Integer> collection)`
  `int`
  `addAll(IntHashSet set)`
  Adds all elements from the given set to this set.
  `protected void`
  `allocateBuffers(int arraySize)`
  Allocate new internal buffers.
  `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.
  `void`
  `clear()`
  `IntHashSet`
  `clone()`
  `boolean`
  `contains(int key)`
  `void`
  `ensureCapacity(int expectedElements)`
  Ensure this container can hold at least the given number of elements without resizing its buffers.
  `boolean`
  `equals(Object obj)`
  `static IntHashSet`
  `from(int... elements)`
  Create a set from a variable number of arguments or an array of `int`.
  `int`
  `hashCode()`
  `protected int`
  `hashKey(int key)`
  Returns a hash code for the given key.
  `boolean`
  `indexExists(int index)`
  `int`
  `indexGet(int index)`
  Returns the exact value of the existing key.
  `void`
  `indexInsert(int index, int key)`
  Inserts a key for an index that is not present in the set.
  `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).
  `void`
  `indexRemove(int index)`
  Removes a key at an index previously acquired from `indexOf(int)`.
  `int`
  `indexReplace(int index, int equivalentKey)`
  Replaces the existing equivalent key with the given one and returns any previous value stored for that key.
  `boolean`
  `isEmpty()`
  `Iterator<IntCursor>`
  `iterator()`
  `protected int`
  `nextIterationSeed()`
  Provides the next iteration seed used to build the iteration starting slot and offset increment.
  `long`
  `ramBytesUsed()`
  Returns an estimate of the JVM heap memory used by this object in bytes.
  `protected void`
  `rehash(int[] fromKeys)`
  Rehash from old buffers to new buffers.
  `void`
  `release()`
  `boolean`
  `remove(int key)`
  An alias for the (preferred) `removeAll(org.apache.lucene.internal.hppc.IntHashSet)`.
  `int`
  `removeAll(IntHashSet other)`
  Removes all keys present in a given container.
  `protected void`
  `shiftConflictingKeys(int gapSlot)`
  Shift all the slot-conflicting keys allocated to (and including) `slot`.
  `int`
  `size()`
  `int[]`
  `toArray()`
  `protected double`
  `verifyLoadFactor(double loadFactor)`
  Validate load factor range and return it.

  ### Methods inherited from class java.lang.Object

  `finalize, getClass, notify, notifyAll, toString, wait, wait, wait`

  ### Methods inherited from interface org.apache.lucene.util.Accountable

  `getChildResources`

  ### Methods inherited from interface java.lang.Iterable

  `forEach, spliterator`

- ## Field Details

  - ### keys

    public int[] 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 in `keys`.

  - ### 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`.

  - ### iterationSeed

    protected int iterationSeed
    Seed used to ensure the hash iteration order is different from an iteration to another.

- ## Constructor Details

  - ### IntHashSet

    public IntHashSet()
    New instance with sane defaults.

  - ### IntHashSet

    public IntHashSet(int expectedElements)
    New instance with sane defaults.
    Parameters:
      `expectedElements` - The expected number of elements guaranteed not to cause a rehash (inclusive).

  - ### 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 by `verifyLoadFactor(double)`.

  - ### IntHashSet

    public IntHashSet(IntHashSet set)
    New instance copying elements from another set.

  - ### IntHashSet

    public IntHashSet(Collection<Integer> collection)
    New instance copying elements from another collection.

- ## Method Details

  - ### add

    public boolean add(int key)

  - ### 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

    public int addAll(IntHashSet set)
    Adds all elements from the given set 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 IntCursor> 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).

  - ### addAll

    public int addAll(Collection<Integer> collection)

  - ### toArray

    public int[] toArray()

  - ### remove

    public boolean remove(int key)
    An alias for the (preferred) `removeAll(org.apache.lucene.internal.hppc.IntHashSet)`.

  - ### removeAll

    public int removeAll(IntHashSet other)
    Removes all keys present in a given container.
    Returns:
      Returns the number of elements actually removed as a result of this call.

  - ### 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()
    Overrides:
      `hashCode` in class `Object`

  - ### equals

    public boolean equals(Object obj)
    Overrides:
      `equals` in class `Object`

  - ### clone

    public IntHashSet clone()
    Overrides:
      `clone` in class `Object`

  - ### iterator

    public Iterator<IntCursor> iterator()
    Specified by:
      `iterator` in interface `Iterable<IntCursor>`

  - ### ramBytesUsed

    public long ramBytesUsed()
    Description copied from interface: `Accountable`
    Returns an estimate of the JVM heap memory used by this object in bytes. The method name uses "ram" for historical reasons; only JVM heap memory should be reported. Off-heap resources such as memory-mapped files or native direct buffers should not be included. Negative values are illegal.
    Specified by:
      `ramBytesUsed` in interface `Accountable`

  - ### 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.

  - ### from

    public static IntHashSet from(int... elements)
    Create a set from a variable number of arguments or an array of `int`. 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(int)`
      - `indexGet(int)`
      - `indexInsert(int, int)`
      - `indexReplace(int, int)`

  - ### indexExists

    public boolean indexExists(int index)
    Parameters:
      `index` - The index of a given key, as returned from `indexOf(int)`.
    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(int)`

  - ### 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:
      - `indexOf(int)`

  - ### 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:
      - `indexOf(int)`

  - ### 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 from `indexOf(int)`.
    Throws:
      `AssertionError` - If assertions are enabled and the index does not correspond to an existing key.
    See Also:
      - `indexOf(int)`

  - ### indexRemove

    public void indexRemove(int index)
    Removes a key at an index previously acquired from `indexOf(int)`.
    Parameters:
      `index` - The index of the key to remove, as returned from `indexOf(int)`.
    Throws:
      `AssertionError` - If assertions are enabled and the index does not correspond to an existing key.
    See Also:
      - `indexOf(int)`

  - ### 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`.