Package org.agrona.collections
Class Int2IntHashMap
- java.lang.Object
-
- org.agrona.collections.Int2IntHashMap
-
- All Implemented Interfaces:
java.util.Map<java.lang.Integer,java.lang.Integer>
public class Int2IntHashMap extends java.lang.Object implements java.util.Map<java.lang.Integer,java.lang.Integer>
An open-addressing with linear probing hash map specialised for primitive key and value pairs.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) class
Int2IntHashMap.AbstractIterator
Base iterator implementation.class
Int2IntHashMap.EntryIterator
Iterator over entries which supports access to unboxed keys and values.class
Int2IntHashMap.EntrySet
Set of entries which supports optionally cached iterators to avoid allocation.class
Int2IntHashMap.KeyIterator
Iterator over keys which supports access to unboxed keys viaInt2IntHashMap.KeyIterator.nextValue()
.class
Int2IntHashMap.KeySet
Set of keys which supports optional cached iterators to avoid allocation.class
Int2IntHashMap.ValueCollection
Collection of values which supports optionally cached iterators to avoid allocation.class
Int2IntHashMap.ValueIterator
Iterator over values which supports access to unboxed values.
-
Field Summary
Fields Modifier and Type Field Description private int[]
entries
private Int2IntHashMap.EntrySet
entrySet
private Int2IntHashMap.KeySet
keySet
private float
loadFactor
(package private) static int
MIN_CAPACITY
private int
missingValue
private int
resizeThreshold
private boolean
shouldAvoidAllocation
private int
size
private Int2IntHashMap.ValueCollection
values
-
Constructor Summary
Constructors Constructor Description Int2IntHashMap(int missingValue)
Int2IntHashMap(int initialCapacity, float loadFactor, int missingValue)
Int2IntHashMap(int initialCapacity, float loadFactor, int missingValue, boolean shouldAvoidAllocation)
Int2IntHashMap(Int2IntHashMap mapToCopy)
Copy construct a new map from an existing one.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description int
capacity()
Get the total capacity for the map to which the load factor will be a fraction of.private void
capacity(int newCapacity)
void
clear()
void
compact()
Compact the backing arrays by rehashing with a capacity just larger than current size and giving consideration to the load factor.private void
compactChain(int deleteKeyIndex)
int
compute(int key, java.util.function.IntBinaryOperator remappingFunction)
Primitive specialised version ofMap.compute(Object, BiFunction)
.int
computeIfAbsent(int key, java.util.function.IntUnaryOperator mappingFunction)
Primitive specialised version ofMap.computeIfAbsent(Object, Function)
.int
computeIfPresent(int key, java.util.function.IntBinaryOperator remappingFunction)
Primitive specialised version ofMap.computeIfPresent(Object, BiFunction)
.boolean
containsKey(int key)
Int primitive specialised containsKey.boolean
containsKey(java.lang.Object key)
boolean
containsValue(int value)
Does the map contain the value.boolean
containsValue(java.lang.Object value)
Int2IntHashMap.EntrySet
entrySet()
boolean
equals(java.lang.Object o)
void
forEach(java.util.function.BiConsumer<? super java.lang.Integer,? super java.lang.Integer> action)
void
forEachInt(IntIntConsumer consumer)
Primitive specialised forEach implementation.int
get(int key)
Get a value using provided key avoiding boxing.java.lang.Integer
get(java.lang.Object key)
int
getOrDefault(int key, int defaultValue)
Returns the value to which the specified key is mapped, ordefaultValue
if this map contains no mapping for the key.int
hashCode()
private void
increaseCapacity()
void
intForEach(IntIntConsumer consumer)
Deprecated.UseforEachInt(IntIntConsumer)
instead.boolean
isEmpty()
Int2IntHashMap.KeySet
keySet()
float
loadFactor()
Get the load factor applied for resize operations.int
maxValue()
Get the maximum value stored in the map.int
merge(int key, int value, IntIntFunction remappingFunction)
Primitive specialised version ofMap.merge(Object, Object, BiFunction)
.int
minValue()
Get the minimum value stored in the map.int
missingValue()
The value to be used as a null marker in the map.private static int
next(int index, int mask)
int
put(int key, int value)
Put a key value pair in the map.java.lang.Integer
put(java.lang.Integer key, java.lang.Integer value)
void
putAll(java.util.Map<? extends java.lang.Integer,? extends java.lang.Integer> map)
void
putAll(Int2IntHashMap map)
Put all values from the given map into this map without allocation.int
putIfAbsent(int key, int value)
Primitive specialised version ofMap.putIfAbsent(Object, Object)
method.java.lang.Integer
putIfAbsent(java.lang.Integer key, java.lang.Integer value)
private void
rehash(int newCapacity)
int
remove(int key)
Remove value from the map using given key avoiding boxing.boolean
remove(int key, int value)
Primitive specialised version ofMap.remove(Object, Object)
.java.lang.Integer
remove(java.lang.Object key)
boolean
remove(java.lang.Object key, java.lang.Object value)
int
replace(int key, int value)
Primitive specialised version ofMap.replace(Object, Object)
.boolean
replace(int key, int oldValue, int newValue)
Primitive specialised version ofMap.replace(Object, Object, Object)
.java.lang.Integer
replace(java.lang.Integer key, java.lang.Integer value)
boolean
replace(java.lang.Integer key, java.lang.Integer oldValue, java.lang.Integer newValue)
void
replaceAll(java.util.function.BiFunction<? super java.lang.Integer,? super java.lang.Integer,? extends java.lang.Integer> function)
void
replaceAllInt(IntIntFunction function)
Primitive specialised version ofMap.replaceAll(BiFunction)
.int
resizeThreshold()
Get the actual threshold which when reached the map will resize.int
size()
java.lang.String
toString()
private java.lang.Integer
valOrNull(int value)
Int2IntHashMap.ValueCollection
values()
-
-
-
Field Detail
-
MIN_CAPACITY
static final int MIN_CAPACITY
- See Also:
- Constant Field Values
-
loadFactor
private final float loadFactor
-
missingValue
private final int missingValue
-
resizeThreshold
private int resizeThreshold
-
size
private int size
-
shouldAvoidAllocation
private final boolean shouldAvoidAllocation
-
entries
private int[] entries
-
keySet
private Int2IntHashMap.KeySet keySet
-
values
private Int2IntHashMap.ValueCollection values
-
entrySet
private Int2IntHashMap.EntrySet entrySet
-
-
Constructor Detail
-
Int2IntHashMap
public Int2IntHashMap(int missingValue)
- Parameters:
missingValue
- for the map that represents null.
-
Int2IntHashMap
public Int2IntHashMap(int initialCapacity, float loadFactor, int missingValue)
- Parameters:
initialCapacity
- for the map to overrideMIN_CAPACITY
loadFactor
- for the map to overrideHashing.DEFAULT_LOAD_FACTOR
.missingValue
- for the map that represents null.
-
Int2IntHashMap
public Int2IntHashMap(int initialCapacity, float loadFactor, int missingValue, boolean shouldAvoidAllocation)
- Parameters:
initialCapacity
- for the map to overrideMIN_CAPACITY
loadFactor
- for the map to overrideHashing.DEFAULT_LOAD_FACTOR
.missingValue
- for the map that represents null.shouldAvoidAllocation
- should allocation be avoided by caching iterators and map entries.
-
Int2IntHashMap
public Int2IntHashMap(Int2IntHashMap mapToCopy)
Copy construct a new map from an existing one.- Parameters:
mapToCopy
- for construction.
-
-
Method Detail
-
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 applied for resize operations.- Returns:
- the load factor applied for resize operations.
-
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()
- Specified by:
size
in interfacejava.util.Map<java.lang.Integer,java.lang.Integer>
-
isEmpty
public boolean isEmpty()
- Specified by:
isEmpty
in interfacejava.util.Map<java.lang.Integer,java.lang.Integer>
-
getOrDefault
public int getOrDefault(int key, int defaultValue)
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
- to be returned if there is no value in the map for a givenkey
.- Returns:
- the value to which the specified key is mapped, or
defaultValue
if this map contains no mapping for the key.
-
get
public int get(int key)
Get a value using provided key avoiding boxing.- Parameters:
key
- lookup key.- Returns:
- value associated with the key or
missingValue()
if key is not found in the map.
-
put
public int put(int key, int value)
Put a key value pair in the map.- Parameters:
key
- lookup keyvalue
- new value, must not bemissingValue()
- Returns:
- previous value associated with the key, or
missingValue()
if none found - Throws:
java.lang.IllegalArgumentException
- if value ismissingValue()
-
putIfAbsent
public int putIfAbsent(int key, int value)
Primitive specialised version ofMap.putIfAbsent(Object, Object)
method.- Parameters:
key
- key with which the specified value is to be associatedvalue
- 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. - Throws:
java.lang.IllegalArgumentException
- if value ismissingValue()
-
increaseCapacity
private void increaseCapacity()
-
rehash
private void rehash(int newCapacity)
-
intForEach
@Deprecated public void intForEach(IntIntConsumer consumer)
Deprecated.UseforEachInt(IntIntConsumer)
instead.UseforEachInt(IntIntConsumer)
instead.- Parameters:
consumer
- a callback called for each key/value pair in the map.- See Also:
forEachInt(IntIntConsumer)
-
forEachInt
public void forEachInt(IntIntConsumer consumer)
Primitive specialised forEach implementation.NB: Renamed from forEach to avoid overloading on parameter types of lambda expression, which doesn't play well with type inference in lambda expressions.
- Parameters:
consumer
- a callback called for each key/value pair in the map.
-
containsKey
public boolean containsKey(int key)
Int primitive specialised containsKey.- Parameters:
key
- the key to check.- Returns:
- true if the map contains key as a key, false otherwise.
-
containsValue
public boolean containsValue(int value)
Does the map contain the value.- Parameters:
value
- to be tested against contained values.- Returns:
- true if contained otherwise value.
-
clear
public void clear()
- Specified by:
clear
in interfacejava.util.Map<java.lang.Integer,java.lang.Integer>
-
compact
public void compact()
Compact the backing arrays by rehashing with a capacity just larger than current size and giving consideration to the load factor.
-
computeIfAbsent
public int computeIfAbsent(int key, java.util.function.IntUnaryOperator mappingFunction)
Primitive specialised version ofMap.computeIfAbsent(Object, Function)
.- Parameters:
key
- to search on.mappingFunction
- to provide a value if the get returns null.- Returns:
- the value if found otherwise the missing value.
-
computeIfPresent
public int computeIfPresent(int key, java.util.function.IntBinaryOperator remappingFunction)
Primitive specialised version ofMap.computeIfPresent(Object, BiFunction)
.- Parameters:
key
- to search on.remappingFunction
- to compute a value if a mapping is found.- Returns:
- the updated value if a mapping was found, otherwise the missing value.
-
compute
public int compute(int key, java.util.function.IntBinaryOperator remappingFunction)
Primitive specialised version ofMap.compute(Object, BiFunction)
.- Parameters:
key
- to search on.remappingFunction
- to compute a value.- Returns:
- the updated value.
-
get
public java.lang.Integer get(java.lang.Object key)
- Specified by:
get
in interfacejava.util.Map<java.lang.Integer,java.lang.Integer>
-
put
public java.lang.Integer put(java.lang.Integer key, java.lang.Integer value)
- Specified by:
put
in interfacejava.util.Map<java.lang.Integer,java.lang.Integer>
-
forEach
public void forEach(java.util.function.BiConsumer<? super java.lang.Integer,? super java.lang.Integer> action)
- Specified by:
forEach
in interfacejava.util.Map<java.lang.Integer,java.lang.Integer>
-
containsKey
public boolean containsKey(java.lang.Object key)
- Specified by:
containsKey
in interfacejava.util.Map<java.lang.Integer,java.lang.Integer>
-
containsValue
public boolean containsValue(java.lang.Object value)
- Specified by:
containsValue
in interfacejava.util.Map<java.lang.Integer,java.lang.Integer>
-
putAll
public void putAll(java.util.Map<? extends java.lang.Integer,? extends java.lang.Integer> map)
- Specified by:
putAll
in interfacejava.util.Map<java.lang.Integer,java.lang.Integer>
-
putAll
public void putAll(Int2IntHashMap map)
Put all values from the given map into this map without allocation.- Parameters:
map
- whose value are to be added.
-
putIfAbsent
public java.lang.Integer putIfAbsent(java.lang.Integer key, java.lang.Integer value)
- Specified by:
putIfAbsent
in interfacejava.util.Map<java.lang.Integer,java.lang.Integer>
-
replace
public java.lang.Integer replace(java.lang.Integer key, java.lang.Integer value)
- Specified by:
replace
in interfacejava.util.Map<java.lang.Integer,java.lang.Integer>
-
replace
public boolean replace(java.lang.Integer key, java.lang.Integer oldValue, java.lang.Integer newValue)
- Specified by:
replace
in interfacejava.util.Map<java.lang.Integer,java.lang.Integer>
-
replaceAll
public void replaceAll(java.util.function.BiFunction<? super java.lang.Integer,? super java.lang.Integer,? extends java.lang.Integer> function)
- Specified by:
replaceAll
in interfacejava.util.Map<java.lang.Integer,java.lang.Integer>
-
keySet
public Int2IntHashMap.KeySet keySet()
- Specified by:
keySet
in interfacejava.util.Map<java.lang.Integer,java.lang.Integer>
-
values
public Int2IntHashMap.ValueCollection values()
- Specified by:
values
in interfacejava.util.Map<java.lang.Integer,java.lang.Integer>
-
entrySet
public Int2IntHashMap.EntrySet entrySet()
- Specified by:
entrySet
in interfacejava.util.Map<java.lang.Integer,java.lang.Integer>
-
remove
public java.lang.Integer remove(java.lang.Object key)
- Specified by:
remove
in interfacejava.util.Map<java.lang.Integer,java.lang.Integer>
-
remove
public boolean remove(java.lang.Object key, java.lang.Object value)
- Specified by:
remove
in interfacejava.util.Map<java.lang.Integer,java.lang.Integer>
-
remove
public int remove(int key)
Remove value from the map using given key avoiding boxing.- Parameters:
key
- whose mapping is to be removed from the map.- Returns:
- removed value or
missingValue()
if key was not found in the map.
-
remove
public boolean remove(int key, int value)
Primitive specialised version ofMap.remove(Object, Object)
.- 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.
-
merge
public int merge(int key, int value, IntIntFunction remappingFunction)
Primitive specialised version ofMap.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 or a null 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 as the result of this operation.
-
compactChain
private void compactChain(int deleteKeyIndex)
-
minValue
public int minValue()
Get the minimum value stored in the map. If the map is empty then it will returnmissingValue()
.- Returns:
- the minimum value stored in the map.
-
maxValue
public int maxValue()
Get the maximum value stored in the map. If the map is empty then it will returnmissingValue()
.- Returns:
- the maximum value stored in the map.
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
replace
public int replace(int key, int value)
Primitive specialised version ofMap.replace(Object, Object)
.- Parameters:
key
- key with which the specified value is associated.value
- 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
public boolean replace(int key, int oldValue, int newValue)
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
public void replaceAllInt(IntIntFunction function)
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
- to apply to each entry.
-
equals
public boolean equals(java.lang.Object o)
- Specified by:
equals
in interfacejava.util.Map<java.lang.Integer,java.lang.Integer>
- Overrides:
equals
in classjava.lang.Object
-
hashCode
public int hashCode()
- Specified by:
hashCode
in interfacejava.util.Map<java.lang.Integer,java.lang.Integer>
- Overrides:
hashCode
in classjava.lang.Object
-
next
private static int next(int index, int mask)
-
capacity
private void capacity(int newCapacity)
-
valOrNull
private java.lang.Integer valOrNull(int value)
-
-