public class IntIntHashMap extends Object implements Iterable<IntIntHashMap.IntIntCursor>, Cloneable
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
Modifier and Type | Class and Description |
---|---|
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.
|
Modifier and Type | Field and Description |
---|---|
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
|
int[] |
values
The array holding values.
|
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.
|
Modifier and Type | Method and Description |
---|---|
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.
|
finalize, getClass, notify, notifyAll, wait, wait, wait
forEach, spliterator
public static final int DEFAULT_EXPECTED_ELEMENTS
public static final float DEFAULT_LOAD_FACTOR
public static final float MIN_LOAD_FACTOR
public static final float MAX_LOAD_FACTOR
public static final int MIN_HASH_ARRAY_LENGTH
public static final int MAX_HASH_ARRAY_LENGTH
public int[] keys
public int[] values
protected int assigned
size()
instead).size()
protected int mask
keys
.protected int resizeAt
protected boolean hasEmptyKey
protected double loadFactor
keys
.protected int iterationSeed
public IntIntHashMap()
public IntIntHashMap(int expectedElements)
expectedElements
- The expected number of elements guaranteed not to cause buffer
expansion (inclusive).public IntIntHashMap(int expectedElements, double loadFactor)
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)
.public IntIntHashMap(Iterable<? extends IntIntHashMap.IntIntCursor> container)
public int put(int key, int value)
public int putAll(Iterable<? extends IntIntHashMap.IntIntCursor> iterable)
public boolean putIfAbsent(int key, int value)
if (!map.containsKey(key)) map.put(value);
key
- The key of the value to check.value
- The value to put if key
does not exist.true
if key
did not exist and value
was placed
in the map.public int putOrAdd(int key, int putValue, int incrementValue)
key
exists, putValue
is inserted into the map, otherwise any
existing value is incremented by additionValue
.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.key
(after changes).public int addTo(int key, int incrementValue)
incrementValue
to any existing value for the given key
or
inserts incrementValue
if key
did not previously exist.key
- The key of the value to adjust.incrementValue
- The value to put or add to the existing value if key
exists.key
(after changes).public int remove(int key)
public int get(int key)
public int getOrDefault(int key, int defaultValue)
public boolean containsKey(int key)
public int indexOf(int key)
public boolean indexExists(int index)
public int indexGet(int index)
public int indexReplace(int index, int newValue)
public void indexInsert(int index, int key, int value)
public int indexRemove(int index)
public void clear()
public void release()
public int size()
public boolean isEmpty()
protected boolean equalElements(IntIntHashMap other)
public void ensureCapacity(int expectedElements)
expectedElements
- The total number of keys, inclusive.protected int nextIterationSeed()
public Iterator<IntIntHashMap.IntIntCursor> iterator()
iterator
in interface Iterable<IntIntHashMap.IntIntCursor>
public IntIntHashMap.KeysContainer keys()
public IntIntHashMap.IntContainer values()
public IntIntHashMap clone()
public String toString()
public static IntIntHashMap from(int[] keys, int[] values)
protected int hashKey(int key)
The output from this function should evenly distribute keys across the entire integer range.
protected double verifyLoadFactor(double loadFactor)
protected void rehash(int[] fromKeys, int[] fromValues)
protected void allocateBuffers(int arraySize)
protected void allocateThenInsertThenRehash(int slot, int pendingKey, int pendingValue)
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.
protected void shiftConflictingKeys(int gapSlot)
slot
.Copyright © 2000-2021 Apache Software Foundation. All Rights Reserved.