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

Package org.apache.lucene.internal.hppc

# Class IntFloatHashMap

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

All Implemented Interfaces:
  `Cloneable`, `Iterable<IntFloatHashMap.IntFloatCursor>`, `Accountable`

public class IntFloatHashMap extends Object implements Iterable<IntFloatHashMap.IntFloatCursor>, Accountable, Cloneable
A hash map of `int` to `float`, implemented using open addressing with linear probing for collision resolution.

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

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
  `static final class`
  `IntFloatHashMap.IntFloatCursor`
  Forked from HPPC, holding int index,key and value
  `final class`
  `IntFloatHashMap.KeysContainer`
  A view of the keys inside this hash map.
  `final class`
  `IntFloatHashMap.ValuesContainer`
  A view over the set of values of this map.

- ## 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 (use `size()` instead).
  `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 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.
  `float[]`
  `values`
  The array holding values.

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

  `NULL_ACCOUNTABLE`

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `IntFloatHashMap()`
  New instance with sane defaults.
  `IntFloatHashMap(int expectedElements)`
  New instance with sane defaults.
  `IntFloatHashMap(int expectedElements, double loadFactor)`
  New instance with the provided defaults.
  `IntFloatHashMap(IntFloatHashMap map)`
  Create a hash map from all key-value pairs of another map.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `float`
  `addTo(int key, float incrementValue)`
  Adds `incrementValue` to any existing value for the given `key` or inserts `incrementValue` if `key` did not previously exist.
  `protected void`
  `allocateBuffers(int arraySize)`
  Allocate new internal buffers.
  `protected void`
  `allocateThenInsertThenRehash(int slot, int pendingKey, float pendingValue)`
  This method is invoked when there is a new key/ value pair to be inserted into the buffers but there is not enough empty slots to do so.
  `void`
  `clear()`
  `IntFloatHashMap`
  `clone()`
  `boolean`
  `containsKey(int key)`
  `void`
  `ensureCapacity(int expectedElements)`
  Ensure this container can hold at least the given number of keys (entries) without resizing its buffers.
  `protected boolean`
  `equalElements(IntFloatHashMap other)`
  Return true if all keys of some other container exist in this container.
  `boolean`
  `equals(Object obj)`
  `static IntFloatHashMap`
  `from(int[] keys, float[] values)`
  Creates a hash map from two index-aligned arrays of key-value pairs.
  `float`
  `get(int key)`
  `float`
  `getOrDefault(int key, float defaultValue)`
  `int`
  `hashCode()`
  `protected int`
  `hashKey(int key)`
  Returns a hash code for the given key.
  `boolean`
  `indexExists(int index)`
  `float`
  `indexGet(int index)`
  `void`
  `indexInsert(int index, int key, float value)`
  `int`
  `indexOf(int key)`
  `float`
  `indexRemove(int index)`
  `float`
  `indexReplace(int index, float newValue)`
  `boolean`
  `isEmpty()`
  `Iterator<IntFloatHashMap.IntFloatCursor>`
  `iterator()`
  `IntFloatHashMap.KeysContainer`
  `keys()`
  Returns a specialized view of the keys of this associated container.
  `protected int`
  `nextIterationSeed()`
  Provides the next iteration seed used to build the iteration starting slot and offset increment.
  `float`
  `put(int key, float value)`
  `int`
  `putAll(Iterable<? extends IntFloatHashMap.IntFloatCursor> iterable)`
  `boolean`
  `putIfAbsent(int key, float value)`
  Trove-inspired API method.
  `float`
  `putOrAdd(int key, float putValue, float incrementValue)`
  If `key` exists, `putValue` is inserted into the map, otherwise any existing value is incremented by `additionValue`.
  `long`
  `ramBytesUsed()`
  Returns an estimate of the JVM heap memory used by this object in bytes.
  `protected void`
  `rehash(int[] fromKeys, float[] fromValues)`
  Rehash from old buffers to new buffers.
  `void`
  `release()`
  `float`
  `remove(int key)`
  `protected void`
  `shiftConflictingKeys(int gapSlot)`
  Shift all the slot-conflicting keys and values allocated to (and including) `slot`.
  `int`
  `size()`
  `String`
  `toString()`
  Convert the contents of this map to a human-friendly string.
  `IntFloatHashMap.ValuesContainer`
  `values()`
  `protected double`
  `verifyLoadFactor(double loadFactor)`
  Validate load factor range and return it.

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

  `finalize, getClass, notify, notifyAll, 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 array holding keys.

  - ### values

    public float[] values
    The array holding values.

  - ### assigned

    protected int assigned
    The number of stored keys (assigned key slots), excluding the special "empty" key, if any (use `size()` instead).
    See Also:
      - `size()`

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

  - ### IntFloatHashMap

    public IntFloatHashMap()
    New instance with sane defaults.

  - ### IntFloatHashMap

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

  - ### IntFloatHashMap

    public IntFloatHashMap(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)`.

  - ### IntFloatHashMap

    public IntFloatHashMap(IntFloatHashMap map)
    Create a hash map from all key-value pairs of another map.

- ## Method Details

  - ### put

    public float put(int key, float value)

  - ### putAll

    public int putAll(Iterable<? extends IntFloatHashMap.IntFloatCursor> iterable)

  - ### putIfAbsent

    public boolean putIfAbsent(int key, float value)
    Trove-inspired API method. An equivalent of the following code:

         if (!map.containsKey(key)) map.put(value);

    Parameters:
      `key` - The key of the value to check.
      `value` - The value to put if `key` does not exist.
    Returns:
      `true` if `key` did not exist and `value` was placed in the map.

  - ### putOrAdd

    public float putOrAdd(int key, float putValue, float incrementValue)
    If `key` exists, `putValue` is inserted into the map, otherwise any existing value is incremented by `additionValue`.
    Parameters:
      `key` - The key of the value to adjust.
      `putValue` - The value to put if `key` does not exist.
      `incrementValue` - The value to add to the existing value if `key` exists.
    Returns:
      Returns the current value associated with `key` (after changes).

  - ### addTo

    public float addTo(int key, float incrementValue)
    Adds `incrementValue` to any existing value for the given `key` or inserts `incrementValue` if `key` did not previously exist.
    Parameters:
      `key` - The key of the value to adjust.
      `incrementValue` - The value to put or add to the existing value if `key` exists.
    Returns:
      Returns the current value associated with `key` (after changes).

  - ### remove

    public float remove(int key)

  - ### get

    public float get(int key)

  - ### getOrDefault

    public float getOrDefault(int key, float defaultValue)

  - ### containsKey

    public boolean containsKey(int key)

  - ### indexOf

    public int indexOf(int key)

  - ### indexExists

    public boolean indexExists(int index)

  - ### indexGet

    public float indexGet(int index)

  - ### indexReplace

    public float indexReplace(int index, float newValue)

  - ### indexInsert

    public void indexInsert(int index, int key, float value)

  - ### indexRemove

    public float indexRemove(int index)

  - ### clear

    public void clear()

  - ### release

    public void release()

  - ### size

    public int size()

  - ### isEmpty

    public boolean isEmpty()

  - ### hashCode

    public int hashCode()
    Overrides:
      `hashCode` in class `Object`

  - ### equals

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

  - ### equalElements

    protected boolean equalElements(IntFloatHashMap other)
    Return true if all keys of some other container exist in this container.

  - ### ensureCapacity

    public void ensureCapacity(int expectedElements)
    Ensure this container can hold at least the given number of keys (entries) without resizing its buffers.
    Parameters:
      `expectedElements` - The total number of keys, inclusive.

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

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

  - ### iterator

    public Iterator<IntFloatHashMap.IntFloatCursor> iterator()
    Specified by:
      `iterator` in interface `Iterable<IntFloatHashMap.IntFloatCursor>`

  - ### keys

    public IntFloatHashMap.KeysContainer keys()
    Returns a specialized view of the keys of this associated container.

  - ### values

    public IntFloatHashMap.ValuesContainer values()
    Returns:
      Returns a container with all values stored in this map.

  - ### clone

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

  - ### toString

    public String toString()
    Convert the contents of this map to a human-friendly string.
    Overrides:
      `toString` in class `Object`

  - ### from

    public static IntFloatHashMap from(int[] keys, float[] values)
    Creates a hash map from two index-aligned arrays of key-value pairs.

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

  - ### 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, float[] fromValues)
    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, float pendingValue)
    This method is invoked when there is a new key/ value pair to be inserted into the buffers 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 and values allocated to (and including) `slot`.