org.apache.lucene.util.hppc

## Class IntIntHashMap

- java.lang.Object
- - org.apache.lucene.util.hppc.IntIntHashMap

All Implemented Interfaces:
  Cloneable, Iterable<IntIntHashMap.IntIntCursor>

    public class IntIntHashMap
    extends Object
    implements Iterable<IntIntHashMap.IntIntCursor>, Cloneable

A hash map of `int` to `int`, implemented using open addressing with linear probing for collision resolution.

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

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

### Nested Class Summary

Nested Classes
`static class` `IntIntHashMap.AbstractIterator<E>` Simplifies the implementation of iterators a bit.
`static class` `IntIntHashMap.BufferAllocationException` BufferAllocationException forked from HPPC
`class` `IntIntHashMap.IntContainer` IntCursor iterable with size and toArray function implemented
`class` `IntIntHashMap.IntCursor` Forked from HPPC, holding int index and int value
`class` `IntIntHashMap.IntIntCursor` Forked from HPPC, holding int index,key and value
`class` `IntIntHashMap.KeysContainer` A view of the keys inside this hash map.

### Field Summary

Fields
`protected int` `assigned` The number of stored keys (assigned key slots), excluding the special "empty" key, if any (use `size()` instead).
`static int` `DEFAULT_EXPECTED_ELEMENTS`
`static float` `DEFAULT_LOAD_FACTOR`
`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`.
`static int` `MAX_HASH_ARRAY_LENGTH` Maximum array size for hash containers (power-of-two and still allocable in Java, not a negative int).
`static float` `MAX_LOAD_FACTOR` Maximum sane load factor (1 empty slot per 100).
`static int` `MIN_HASH_ARRAY_LENGTH` Minimum hash buffer size.
`static float` `MIN_LOAD_FACTOR` Minimal sane load factor (99 empty slots per 100).
`protected int` `resizeAt` Expand (rehash) `keys` when `assigned` hits this value.
`int[]` `values` The array holding values.

### Constructor Summary

Constructors
Constructor and Description
`IntIntHashMap()` New instance with sane defaults.
`IntIntHashMap(int expectedElements)` New instance with sane defaults.
`IntIntHashMap(int expectedElements, double loadFactor)` New instance with the provided defaults.
`IntIntHashMap(Iterable<? extends IntIntHashMap.IntIntCursor> container)` Create a hash map from all key-value pairs of another container.

### Method Summary

All Methods Static Methods Instance Methods Concrete Methods
`int` `addTo(int key, int 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, int 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()`
`IntIntHashMap` `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(IntIntHashMap other)` Return true if all keys of some other container exist in this container.
`boolean` `equals(Object obj)`
`static IntIntHashMap` `from(int[] keys, int[] values)` Creates a hash map from two index-aligned arrays of key-value pairs.
`int` `get(int key)`
`int` `getOrDefault(int key, int defaultValue)`
`int` `hashCode()`
`protected int` `hashKey(int key)` Returns a hash code for the given key.
`boolean` `indexExists(int index)`
`int` `indexGet(int index)`
`void` `indexInsert(int index, int key, int value)`
`int` `indexOf(int key)`
`int` `indexRemove(int index)`
`int` `indexReplace(int index, int newValue)`
`boolean` `isEmpty()`
`Iterator<IntIntHashMap.IntIntCursor>` `iterator()`
`IntIntHashMap.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.
`int` `put(int key, int value)`
`int` `putAll(Iterable<? extends IntIntHashMap.IntIntCursor> iterable)`
`boolean` `putIfAbsent(int key, int value)` Trove-inspired API method.
`int` `putOrAdd(int key, int putValue, int incrementValue)` If `key` exists, `putValue` is inserted into the map, otherwise any existing value is incremented by `additionValue`.
`protected void` `rehash(int[] fromKeys, int[] fromValues)` Rehash from old buffers to new buffers.
`void` `release()`
`int` `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.
`IntIntHashMap.IntContainer` `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 java.lang.Iterable

`forEach, spliterator`

### Field Detail

#### DEFAULT_EXPECTED_ELEMENTS

    public static final int DEFAULT_EXPECTED_ELEMENTS

See Also:
  Constant Field Values

#### DEFAULT_LOAD_FACTOR

    public static final float DEFAULT_LOAD_FACTOR

See Also:
  Constant Field Values

#### MIN_LOAD_FACTOR

    public static final float MIN_LOAD_FACTOR

Minimal sane load factor (99 empty slots per 100).

See Also:
  Constant Field Values

#### MAX_LOAD_FACTOR

    public static final float MAX_LOAD_FACTOR

Maximum sane load factor (1 empty slot per 100).

See Also:
  Constant Field Values

#### MIN_HASH_ARRAY_LENGTH

    public static final int MIN_HASH_ARRAY_LENGTH

Minimum hash buffer size.

See Also:
  Constant Field Values

#### MAX_HASH_ARRAY_LENGTH

    public static final int MAX_HASH_ARRAY_LENGTH

Maximum array size for hash containers (power-of-two and still allocable in Java, not a negative int).

See Also:
  Constant Field Values

#### keys

    public int[] keys

The array holding keys.

#### values

    public int[] 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 Detail

#### IntIntHashMap

    public IntIntHashMap()

New instance with sane defaults.

#### IntIntHashMap

    public IntIntHashMap(int expectedElements)

New instance with sane defaults.

Parameters:
  `expectedElements` - The expected number of elements guaranteed not to cause buffer expansion (inclusive).

#### IntIntHashMap

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

#### IntIntHashMap

    public IntIntHashMap(Iterable<? extends IntIntHashMap.IntIntCursor> container)

Create a hash map from all key-value pairs of another container.

### Method Detail

#### put

    public int put(int key,
                   int value)

#### putAll

    public int putAll(Iterable<? extends IntIntHashMap.IntIntCursor> iterable)

#### putIfAbsent

    public boolean putIfAbsent(int key,
                               int 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 int putOrAdd(int key,
                        int putValue,
                        int 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 int addTo(int key,
                     int 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 int remove(int key)

#### get

    public int get(int key)

#### getOrDefault

    public int getOrDefault(int key,
                            int defaultValue)

#### containsKey

    public boolean containsKey(int key)

#### indexOf

    public int indexOf(int key)

#### indexExists

    public boolean indexExists(int index)

#### indexGet

    public int indexGet(int index)

#### indexReplace

    public int indexReplace(int index,
                            int newValue)

#### indexInsert

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

#### indexRemove

    public int 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(IntIntHashMap 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.

#### iterator

    public Iterator<IntIntHashMap.IntIntCursor> iterator()

Specified by:
  `iterator` in interface `Iterable<IntIntHashMap.IntIntCursor>`

#### keys

    public IntIntHashMap.KeysContainer keys()

Returns a specialized view of the keys of this associated container.

#### values

    public IntIntHashMap.IntContainer values()

Returns:
  Returns a container with all values stored in this map.

#### clone

    public IntIntHashMap 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 IntIntHashMap from(int[] keys,
                                     int[] 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,
                          int[] 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,
                                                int 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`.