Package org.agrona.collections
Class Object2IntCounterMap<K>
java.lang.Object
org.agrona.collections.Object2IntCounterMap<K>
- Type Parameters:
K- the type of keys maintained by this map.
An open-addressing with linear probing hash map specialised for object and primitive counter pairs.
A counter map views counters which hit
initialValue as deleted.
This means that changing a counter may impact size().-
Constructor Summary
ConstructorsConstructorDescriptionObject2IntCounterMap(int initialValue) Construct a new counter map with the initial value for the counter provided.Object2IntCounterMap(int initialCapacity, float loadFactor, int initialValue) Construct a new counter map with the initial value for the counter provided. -
Method Summary
Modifier and TypeMethodDescriptionintAdd amount to the current value associated with this key.intcapacity()Get the total capacity for the map to which the load factor will be a fraction of.voidclear()Clear out all entries.voidcompact()Compact the backing arrays by rehashing with a capacity just larger than current size and giving consideration to the load factor.intcomputeIfAbsent(K key, ToIntFunction<? super K> mappingFunction) Tryget(Object)a value for a key and if not present then apply mapping function.booleancontainsKey(K key) Does the map contain a value for a given key which is notinitialValue().booleancontainsValue(int value) Iterate over the values to see if any match the provided value.intdecrementAndGet(K key) Convenience version ofaddAndGet(Object, int)(key, -1).voidforEach(ObjIntConsumer<K> consumer) Iterate over all value in the map which are not atinitialValue().intGet the value of a counter associated with a key orinitialValue()if not found.intAdd amount to the current value associated with this key.intgetAndDecrement(K key) Convenience version ofgetAndAdd(Object, int)(key, -1).intgetAndIncrement(K key) Convenience version ofgetAndAdd(Object, int)(key, 1).intincrementAndGet(K key) Convenience version ofaddAndGet(Object, int)(key, 1).intThe value to be used as a null marker in the map.booleanisEmpty()Is the map empty.floatGet the load factor applied for resize operations.intmaxValue()Get the maximum value stored in the map.intminValue()Get the minimum value stored in the map.intPut the value for a key in the map.intRemove a counter value for a given key.intGet the actual threshold which when reached the map will resize.intsize()The current size of the map which at not atinitialValue().toString()
-
Constructor Details
-
Object2IntCounterMap
public Object2IntCounterMap(int initialValue) Construct a new counter map with the initial value for the counter provided.- Parameters:
initialValue- to be used for each counter.
-
Object2IntCounterMap
public Object2IntCounterMap(int initialCapacity, float loadFactor, int initialValue) Construct a new counter map with the initial value for the counter provided.- Parameters:
initialCapacity- of the map.loadFactor- applied for resize operations.initialValue- to be used for each counter.
-
-
Method Details
-
initialValue
public int initialValue()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.
-
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.
-
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.
-
size
public int size()The current size of the map which at not atinitialValue().- Returns:
- map size, counters at
initialValue()are not counted.
-
isEmpty
public boolean isEmpty()Is the map empty.- Returns:
- size == 0
-
get
Get the value of a counter associated with a key orinitialValue()if not found.- Parameters:
key- lookup key.- Returns:
- counter value associated with key or
initialValue()if not found.
-
put
Put the value for a key in the map.- Parameters:
key- lookup key.value- new value, must not be initialValue.- Returns:
- current counter value associated with key, or
initialValue()if none found. - Throws:
IllegalArgumentException- if value isinitialValue().
-
incrementAndGet
Convenience version ofaddAndGet(Object, int)(key, 1).- Parameters:
key- for the counter.- Returns:
- the new value.
-
decrementAndGet
Convenience version ofaddAndGet(Object, int)(key, -1).- Parameters:
key- for the counter.- Returns:
- the new value.
-
addAndGet
Add amount to the current value associated with this key. If no such value exists useinitialValue()as current value and associate key withinitialValue()+ amount unless amount is 0, in which case map remains unchanged.- Parameters:
key- new or existing.amount- to be added.- Returns:
- the new value associated with the specified key, or
initialValue()+ amount if there was no mapping for the key.
-
getAndIncrement
Convenience version ofgetAndAdd(Object, int)(key, 1).- Parameters:
key- for the counter.- Returns:
- the old value.
-
getAndDecrement
Convenience version ofgetAndAdd(Object, int)(key, -1).- Parameters:
key- for the counter.- Returns:
- the old value.
-
getAndAdd
Add amount to the current value associated with this key. If no such value exists useinitialValue()as current value and associate key withinitialValue()+ amount unless amount is 0, in which case map remains unchanged.- Parameters:
key- new or existing.amount- to be added.- Returns:
- the previous value associated with the specified key, or
initialValue()if there was no mapping for the key.
-
forEach
Iterate over all value in the map which are not atinitialValue().- Parameters:
consumer- called for each key/value pair in the map.
-
containsKey
Does the map contain a value for a given key which is notinitialValue().- Parameters:
key- the key to check.- Returns:
- true if the map contains the key with a value other than
initialValue(), false otherwise.
-
containsValue
public boolean containsValue(int value) Iterate over the values to see if any match the provided value.If value provided is
initialValue()then it will always return false.- Parameters:
value- the key to check.- Returns:
- true if the map contains value as a mapped value, false otherwise.
-
clear
public void clear()Clear out all entries. -
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
Tryget(Object)a value for a key and if not present then apply mapping 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.
-
remove
Remove a counter value for a given key.- Parameters:
key- to be removed.- Returns:
- old value for key.
-
minValue
public int minValue()Get the minimum value stored in the map. If the map is empty then it will returninitialValue()- 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 returninitialValue()- Returns:
- the maximum value stored in the map.
-
toString
-