Package org.agrona.collections
Class Object2LongHashMap<K>
- java.lang.Object
-
- org.agrona.collections.Object2LongHashMap<K>
-
- Type Parameters:
K
- type of keys stored in theMap
- All Implemented Interfaces:
java.util.Map<K,java.lang.Long>
public class Object2LongHashMap<K> extends java.lang.Object implements java.util.Map<K,java.lang.Long>
Map
implementation specialised for long values using open addressing and linear probing for cache efficient access. The implementation is mirror copy ofLong2ObjectHashMap
and it also relies on missing value concept fromLong2LongHashMap
.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) class
Object2LongHashMap.AbstractIterator<T>
Base iterator implementation that contains basic logic of traversing the element in the backing array.class
Object2LongHashMap.EntryIterator
Iterator over entries which can provide unboxed access and optionally avoid allocation.class
Object2LongHashMap.EntrySet
Set of entries which can optionally cache iterators to avoid allocation.class
Object2LongHashMap.KeyIterator
Iterator over keys.class
Object2LongHashMap.KeySet
Set of keys that can optionally cache iterators to avoid allocation.class
Object2LongHashMap.ValueCollection
Collection of values which can optionally cache iterators to avoid allocation.class
Object2LongHashMap.ValueIterator
Iterator over values providing unboxed access viaObject2LongHashMap.ValueIterator.nextLong()
.
-
Field Summary
Fields Modifier and Type Field Description private Object2LongHashMap.EntrySet
entrySet
private K[]
keys
private Object2LongHashMap.KeySet
keySet
private float
loadFactor
(package private) static int
MIN_CAPACITY
private long
missingValue
private int
resizeThreshold
private boolean
shouldAvoidAllocation
private int
size
private Object2LongHashMap.ValueCollection
valueCollection
private long[]
values
-
Constructor Summary
Constructors Constructor Description Object2LongHashMap(int initialCapacity, float loadFactor, long missingValue)
Construct a new map allowing a configuration for initial capacity and load factor.Object2LongHashMap(int initialCapacity, float loadFactor, long missingValue, boolean shouldAvoidAllocation)
Construct a new map allowing a configuration for initial capacity and load factor.Object2LongHashMap(long missingValue)
Construct a map with default capacity and load factor.Object2LongHashMap(Object2LongHashMap<K> mapToCopy)
Copy construct a new map from an existing one.
-
Method Summary
All Methods Instance Methods Concrete 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.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)
long
compute(K key, ObjectLongToLongFunction<? 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).long
computeIfAbsent(K key, java.util.function.ToLongFunction<? super K> mappingFunction)
Get a value for a given key, or if it does not exist then default the value via aLongFunction
and put it in the map.long
computeIfPresent(K key, ObjectLongToLongFunction<? 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(java.lang.Object key)
Overloaded version ofMap.containsKey(Object)
that takes a primitive long key.boolean
containsValue(long value)
Overloaded version to avoid boxing.boolean
containsValue(java.lang.Object value)
Object2LongHashMap.EntrySet
entrySet()
boolean
equals(java.lang.Object o)
void
forEach(java.util.function.BiConsumer<? super K,? super java.lang.Long> action)
void
forEachLong(ObjLongConsumer<? 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.java.lang.Long
get(java.lang.Object key)
long
getOrDefault(java.lang.Object key, long defaultValue)
Returns the value to which the specified key is mapped, ordefaultValue
if this map contains no mapping for the key.long
getValue(K key)
Overloaded version ofMap.get(Object)
that takes a primitive long key.int
hashCode()
private void
increaseCapacity()
boolean
isEmpty()
Object2LongHashMap.KeySet
keySet()
float
loadFactor()
Get the load factor beyond which the map will increase size.long
merge(K key, long value, LongLongFunction remappingFunction)
If the specified key is not already associated with a value associates it with the given value.long
missingValue()
The value to be used as a null marker in the map.long
put(K key, long value)
Overloaded version ofMap.put(Object, Object)
that takes a primitive long key.java.lang.Long
put(K key, java.lang.Long value)
void
putAll(java.util.Map<? extends K,? extends java.lang.Long> map)
void
putAll(Object2LongHashMap<? extends K> map)
Puts all values from the given map to this map.long
putIfAbsent(K key, long 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.java.lang.Long
putIfAbsent(K key, java.lang.Long value)
private void
rehash(int newCapacity)
java.lang.Long
remove(java.lang.Object key)
boolean
remove(java.lang.Object key, long value)
Primitive overload of theMap.remove(Object, Object)
that avoids boxing on the value.boolean
remove(java.lang.Object key, java.lang.Object value)
long
removeKey(K key)
Overloaded version ofMap.remove(Object)
that takes a key and returns a primitive long value.long
replace(K key, long value)
Primitive specialised version ofMap.replace(Object, Object)
.boolean
replace(K key, long oldValue, long newValue)
Primitive specialised version ofMap.replace(Object, Object, Object)
.void
replaceAllLong(ObjectLongToLongFunction<? super K> 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.Long
valueOrNull(long value)
Object2LongHashMap.ValueCollection
values()
-
-
-
Field Detail
-
MIN_CAPACITY
static final int MIN_CAPACITY
- See Also:
- Constant Field Values
-
loadFactor
private final float loadFactor
-
missingValue
private final long missingValue
-
resizeThreshold
private int resizeThreshold
-
size
private int size
-
shouldAvoidAllocation
private final boolean shouldAvoidAllocation
-
keys
private K[] keys
-
values
private long[] values
-
valueCollection
private Object2LongHashMap.ValueCollection valueCollection
-
keySet
private Object2LongHashMap.KeySet keySet
-
entrySet
private Object2LongHashMap.EntrySet entrySet
-
-
Constructor Detail
-
Object2LongHashMap
public Object2LongHashMap(long missingValue)
Construct a map with default capacity and load factor.- Parameters:
missingValue
- value to be used as a null maker in the map
-
Object2LongHashMap
public Object2LongHashMap(int initialCapacity, float loadFactor, long 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.
-
Object2LongHashMap
public Object2LongHashMap(int initialCapacity, float loadFactor, long 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.
-
Object2LongHashMap
public Object2LongHashMap(Object2LongHashMap<K> mapToCopy)
Copy construct a new map from an existing one.- Parameters:
mapToCopy
- for construction.
-
-
Method Detail
-
missingValue
public long 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()
- Specified by:
size
in interfacejava.util.Map<K,java.lang.Long>
-
isEmpty
public boolean isEmpty()
- Specified by:
isEmpty
in interfacejava.util.Map<K,java.lang.Long>
-
containsKey
public boolean containsKey(java.lang.Object key)
Overloaded version ofMap.containsKey(Object)
that takes a primitive long key.- Specified by:
containsKey
in interfacejava.util.Map<K,java.lang.Long>
- Parameters:
key
- for indexing theMap
.- Returns:
- true if the key is found otherwise false.
-
containsValue
public boolean containsValue(java.lang.Object value)
- Specified by:
containsValue
in interfacejava.util.Map<K,java.lang.Long>
-
containsValue
public boolean containsValue(long value)
Overloaded version to avoid boxing.- Parameters:
value
- to check.- Returns:
- true if the collection contains the value.
-
getOrDefault
public long getOrDefault(java.lang.Object key, long 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
- 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
public java.lang.Long get(java.lang.Object key)
- Specified by:
get
in interfacejava.util.Map<K,java.lang.Long>
-
getValue
public long getValue(K key)
Overloaded version ofMap.get(Object)
that takes a primitive long key. Due to type erasure have to rename the method.- Parameters:
key
- for indexing theMap
.- Returns:
- the value if found otherwise missingValue.
-
computeIfAbsent
public long computeIfAbsent(K key, java.util.function.ToLongFunction<? super K> mappingFunction)
Get a value for a given key, or if it does not exist then default the value via aLongFunction
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
public long computeIfPresent(K key, ObjectLongToLongFunction<? 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.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
public long compute(K key, ObjectLongToLongFunction<? 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).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
public long merge(K key, long value, LongLongFunction remappingFunction)
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
public java.lang.Long put(K key, java.lang.Long value)
- Specified by:
put
in interfacejava.util.Map<K,java.lang.Long>
-
put
public long put(K key, long value)
Overloaded version ofMap.put(Object, Object)
that takes a primitive long key.- Parameters:
key
- for indexing theMap
.value
- to be inserted in theMap
.- Returns:
- the previous value if found otherwise missingValue.
-
putIfAbsent
public java.lang.Long putIfAbsent(K key, java.lang.Long value)
- Specified by:
putIfAbsent
in interfacejava.util.Map<K,java.lang.Long>
-
putIfAbsent
public long putIfAbsent(K key, long 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.- 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:
java.lang.IllegalArgumentException
- ifvalue == missingValue()
-
remove
public boolean remove(java.lang.Object key, java.lang.Object value)
- Specified by:
remove
in interfacejava.util.Map<K,java.lang.Long>
-
remove
public boolean remove(java.lang.Object key, long value)
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
public java.lang.Long remove(java.lang.Object key)
- Specified by:
remove
in interfacejava.util.Map<K,java.lang.Long>
-
removeKey
public long removeKey(K key)
Overloaded version ofMap.remove(Object)
that takes a key and returns a primitive long 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()
- Specified by:
clear
in interfacejava.util.Map<K,java.lang.Long>
-
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
public void putAll(java.util.Map<? extends K,? extends java.lang.Long> map)
- Specified by:
putAll
in interfacejava.util.Map<K,java.lang.Long>
-
putAll
public void putAll(Object2LongHashMap<? extends K> map)
Puts all values from the given map to this map.- Parameters:
map
- whose values to be added to this map.
-
keySet
public Object2LongHashMap.KeySet keySet()
- Specified by:
keySet
in interfacejava.util.Map<K,java.lang.Long>
-
values
public Object2LongHashMap.ValueCollection values()
- Specified by:
values
in interfacejava.util.Map<K,java.lang.Long>
-
entrySet
public Object2LongHashMap.EntrySet entrySet()
- Specified by:
entrySet
in interfacejava.util.Map<K,java.lang.Long>
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
equals
public boolean equals(java.lang.Object o)
- Specified by:
equals
in interfacejava.util.Map<K,java.lang.Long>
- Overrides:
equals
in classjava.lang.Object
-
hashCode
public int hashCode()
- Specified by:
hashCode
in interfacejava.util.Map<K,java.lang.Long>
- Overrides:
hashCode
in classjava.lang.Object
-
replace
public long replace(K key, long value)
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
public boolean replace(K key, long oldValue, long 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.
-
replaceAllLong
public void replaceAllLong(ObjectLongToLongFunction<? super K> 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
- the function to apply to each entry.
-
forEach
public void forEach(java.util.function.BiConsumer<? super K,? super java.lang.Long> action)
- Specified by:
forEach
in interfacejava.util.Map<K,java.lang.Long>
-
forEachLong
public void forEachLong(ObjLongConsumer<? 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.- 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
private java.lang.Long valueOrNull(long value)
-
-