Package org.agrona.collections
Class Object2IntHashMap<K>
java.lang.Object
org.agrona.collections.Object2IntHashMap<K>
- Type Parameters:
K
- type of keys stored in theMap
Map
implementation specialised for int values using open addressing and
linear probing for cache efficient access. The implementation is mirror copy of Int2ObjectHashMap
and it also relies on missing value concept from Int2IntHashMap
.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescription(package private) class
Base iterator implementation that contains basic logic of traversing the element in the backing array.final class
Iterator over entries which can provide unboxed access and optionally avoid allocation.final class
Set of entries which can optionally cache iterators to avoid allocation.final class
Iterator over keys.final class
Set of keys that can optionally cache iterators to avoid allocation.final class
Collection of values which can optionally cache iterators to avoid allocation.final class
Iterator over values providing unboxed access viaObject2IntHashMap.ValueIterator.nextInt()
. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate Object2IntHashMap<K>.EntrySet
private K[]
private Object2IntHashMap<K>.KeySet
private final float
(package private) static final int
private final int
private int
private final boolean
private int
private Object2IntHashMap<K>.ValueCollection
private int[]
-
Constructor Summary
ConstructorsConstructorDescriptionObject2IntHashMap
(int missingValue) Construct a map with default capacity and load factor.Object2IntHashMap
(int initialCapacity, float loadFactor, int missingValue) Construct a new map allowing a configuration for initial capacity and load factor.Object2IntHashMap
(int initialCapacity, float loadFactor, int missingValue, boolean shouldAvoidAllocation) Construct a new map allowing a configuration for initial capacity and load factor.Object2IntHashMap
(Object2IntHashMap<K> mapToCopy) Copy construct a new map from an existing one. -
Method Summary
Modifier and TypeMethodDescriptionint
capacity()
Get the total capacity for the map to which the load factor will be a fraction of.void
clear()
void
compact()
Compact theMap
backing arrays by rehashing with a capacity just larger than current size and giving consideration to the load factor.private void
compactChain
(int deleteIndex) int
compute
(K key, ObjectIntToIntFunction<? super K> remappingFunction) Attempts to compute a mapping for the specified key and its current mapped value (or missingValue if there is no current mapping).int
computeIfAbsent
(K key, ToIntFunction<? super K> mappingFunction) Get a value for a given key, or if it does not exist then default the value via aIntFunction
and put it in the map.int
computeIfPresent
(K key, ObjectIntToIntFunction<? super K> remappingFunction) If the value for the specified key is present, attempts to compute a new mapping given the key and its current mapped value.boolean
containsKey
(Object key) Overloaded version ofMap.containsKey(Object)
that takes a primitive int key.boolean
containsValue
(int value) Overloaded version to avoid boxing.boolean
containsValue
(Object value) entrySet()
boolean
void
forEach
(BiConsumer<? super K, ? super Integer> action) void
forEachInt
(ObjIntConsumer<? super K> action) Performs the given action for each entry in this map until all entries have been processed or the action throws an exception.int
getOrDefault
(Object key, int defaultValue) Returns the value to which the specified key is mapped, ordefaultValue
if this map contains no mapping for the key.int
Overloaded version ofMap.get(Object)
that takes a primitive int key.int
hashCode()
private void
boolean
isEmpty()
keySet()
float
Get the load factor beyond which the map will increase size.int
merge
(K key, int value, IntIntFunction remappingFunction) If the specified key is not already associated with a value associates it with the given value.int
The value to be used as a null marker in the map.int
Overloaded version ofMap.put(Object, Object)
that takes a primitive int key.void
void
putAll
(Object2IntHashMap<? extends K> map) Puts all values from the given map to this map.int
putIfAbsent
(K key, int value) If the specified key is not already associated with a value associates it with the given value and returnsmissingValue()
, else returns the current value.putIfAbsent
(K key, Integer value) private void
rehash
(int newCapacity) boolean
Primitive overload of theMap.remove(Object, Object)
that avoids boxing on the value.boolean
int
Overloaded version ofMap.remove(Object)
that takes a key and returns a primitive int value.int
Primitive specialised version ofMap.replace(Object, Object)
.boolean
Primitive specialised version ofMap.replace(Object, Object, Object)
.void
replaceAllInt
(ObjectIntToIntFunction<? super K> function) Primitive specialised version ofMap.replaceAll(BiFunction)
.int
Get the actual threshold which when reached the map will resize.int
size()
toString()
private Integer
valueOrNull
(int value) values()
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.util.Map
compute, computeIfAbsent, computeIfPresent, getOrDefault, merge, replace, replace, replaceAll
-
Field Details
-
MIN_CAPACITY
static final int MIN_CAPACITY- See Also:
-
loadFactor
private final float loadFactor -
missingValue
private final int missingValue -
resizeThreshold
private int resizeThreshold -
size
private int size -
shouldAvoidAllocation
private final boolean shouldAvoidAllocation -
keys
-
values
private int[] values -
valueCollection
-
keySet
-
entrySet
-
-
Constructor Details
-
Object2IntHashMap
public Object2IntHashMap(int missingValue) Construct a map with default capacity and load factor.- Parameters:
missingValue
- value to be used as a null maker in the map
-
Object2IntHashMap
public Object2IntHashMap(int initialCapacity, float loadFactor, int missingValue) Construct a new map allowing a configuration for initial capacity and load factor.- Parameters:
initialCapacity
- for the backing array.loadFactor
- limit for resizing on puts.missingValue
- value to be used as a null marker in the map.
-
Object2IntHashMap
public Object2IntHashMap(int initialCapacity, float loadFactor, int missingValue, boolean shouldAvoidAllocation) Construct a new map allowing a configuration for initial capacity and load factor.- Parameters:
initialCapacity
- for the backing array.loadFactor
- limit for resizing on puts.missingValue
- value to be used as a null marker in the map.shouldAvoidAllocation
- should allocation be avoided by caching iterators and map entries.
-
Object2IntHashMap
Copy construct a new map from an existing one.- Parameters:
mapToCopy
- for construction.
-
-
Method Details
-
missingValue
public int missingValue()The value to be used as a null marker in the map.- Returns:
- value to be used as a null marker in the map.
-
loadFactor
public float loadFactor()Get the load factor beyond which the map will increase size.- Returns:
- load factor for when the map should increase size.
-
capacity
public int capacity()Get the total capacity for the map to which the load factor will be a fraction of.- Returns:
- the total capacity for the map.
-
resizeThreshold
public int resizeThreshold()Get the actual threshold which when reached the map will resize. This is a function of the current capacity and load factor.- Returns:
- the threshold when the map will resize.
-
size
public int size() -
isEmpty
public boolean isEmpty() -
containsKey
Overloaded version ofMap.containsKey(Object)
that takes a primitive int key.- Specified by:
containsKey
in interfaceMap<K,
Integer> - Parameters:
key
- for indexing theMap
.- Returns:
- true if the key is found otherwise false.
-
containsValue
- Specified by:
containsValue
in interfaceMap<K,
Integer>
-
containsValue
public boolean containsValue(int value) Overloaded version to avoid boxing.- Parameters:
value
- to check.- Returns:
- true if the collection contains the value.
-
getOrDefault
Returns the value to which the specified key is mapped, ordefaultValue
if this map contains no mapping for the key.- Parameters:
key
- whose associated value is to be returned.defaultValue
- the default mapping of the key.- Returns:
- the value to which the specified key is mapped, or
defaultValue
if this map contains no mapping for the key.
-
get
-
getValue
Overloaded version ofMap.get(Object)
that takes a primitive int key. Due to type erasure have to rename the method.- Parameters:
key
- for indexing theMap
.- Returns:
- the value if found otherwise missingValue.
-
computeIfAbsent
Get a value for a given key, or if it does not exist then default the value via aIntFunction
and put it in the map.Primitive specialized version of
Map.computeIfAbsent(K, java.util.function.Function<? super K, ? extends V>)
.- Parameters:
key
- to search on.mappingFunction
- to provide a value if the get returns missingValue.- Returns:
- old value if found otherwise the newly computed value.
-
computeIfPresent
If the value for the specified key is present, attempts to compute a new mapping given the key and its current mapped value.If the function returns missingValue, the mapping is removed.
Primitive specialized version of
Map.computeIfPresent(Object, BiFunction)
.- Parameters:
key
- to search on.remappingFunction
- to provide a value if the get returns missingValue.- Returns:
- the new value associated with the specified key, or missingValue if none.
-
compute
Attempts to compute a mapping for the specified key and its current mapped value (or missingValue if there is no current mapping).If the function returns missingValue, the mapping is removed (or remains absent if initially absent).
Primitive specialized version of
Map.compute(Object, BiFunction)
.- Parameters:
key
- to search on.remappingFunction
- to provide a value if the get returns missingValue.- Returns:
- the new value associated with the specified key, or missingValue if none.
-
merge
If the specified key is not already associated with a value associates it with the given value. Otherwise, replaces the associated value with the results of the given remapping function, or removes if the result ismissingValue()
. This method may be of use when combining multiple mapped values for a key. If the function returnsmissingValue()
the mapping is removed.Primitive specialized version of
Map.merge(Object, Object, BiFunction)
.- Parameters:
key
- with which the resulting value is to be associated.value
- to be merged with the existing value associated with the key or, if no existing value is associated with the key, to be associated with the key.remappingFunction
- the function to recompute a value if present.- Returns:
- the new value associated with the specified key, or
missingValue()
if no value is associated with the key.
-
put
-
put
Overloaded version ofMap.put(Object, Object)
that takes a primitive int key. -
putIfAbsent
- Specified by:
putIfAbsent
in interfaceMap<K,
Integer>
-
putIfAbsent
If the specified key is not already associated with a value associates it with the given value and returnsmissingValue()
, else returns the current value.- Parameters:
key
- with which the specified value is to be associated.value
- to be associated with the specified key.- Returns:
- the existing value associated with the specified key, or
missingValue()
if there was no mapping for the key. - Throws:
IllegalArgumentException
- ifvalue == missingValue()
-
remove
-
remove
Primitive overload of theMap.remove(Object, Object)
that avoids boxing on the value.- Parameters:
key
- with which the specified value is associated.value
- expected to be associated with the specified key.- Returns:
true
if the value was removed.
-
remove
-
removeKey
Overloaded version ofMap.remove(Object)
that takes a key and returns a primitive int value. Due to type erasure have to rename the method.- Parameters:
key
- for indexing theMap
.- Returns:
- the value if found otherwise missingValue.
-
clear
public void clear() -
compact
public void compact()Compact theMap
backing arrays by rehashing with a capacity just larger than current size and giving consideration to the load factor. -
putAll
-
putAll
Puts all values from the given map to this map.- Parameters:
map
- whose values to be added to this map.
-
keySet
-
values
-
entrySet
-
toString
-
equals
-
hashCode
public int hashCode() -
replace
Primitive specialised version ofMap.replace(Object, Object)
.- Parameters:
key
- with which the specified value is associated.value
- to be associated with the specified key.- Returns:
- the previous value associated with the specified key, or
missingValue()
if there was no mapping for the key.
-
replace
Primitive specialised version ofMap.replace(Object, Object, Object)
.- Parameters:
key
- key with which the specified value is associated.oldValue
- value expected to be associated with the specified key.newValue
- value to be associated with the specified key.- Returns:
true
if the value was replaced.
-
replaceAllInt
Primitive specialised version ofMap.replaceAll(BiFunction)
.NB: Renamed from replaceAll to avoid overloading on parameter types of lambda expression, which doesn't play well with type inference in lambda expressions.
- Parameters:
function
- the function to apply to each entry.
-
forEach
-
forEachInt
Performs the given action for each entry in this map until all entries have been processed or the action throws an exception.- Parameters:
action
- to be performed for each entry.
-
increaseCapacity
private void increaseCapacity() -
rehash
private void rehash(int newCapacity) -
compactChain
private void compactChain(int deleteIndex) -
valueOrNull
-