Class ShortLongWormMap
- All Implemented Interfaces:
Accountable,Preallocable,ShortLongAssociativeContainer,ShortLongMap,Cloneable,Iterable<ShortLongCursor>
short to long, implemented using Worm Hashing strategy.
This strategy is appropriate for a medium sized map (less than 2M entries). It takes more time
to put entries in the map because it maintains chains of entries having the same hash. Then the
lookup speed is fast even if the map is heavy loaded or hashes are clustered. On average it takes
slightly less memory than ShortLongHashMap: slightly heavier but the load factor is higher
(it varies around 80%) so it enlarges later.
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionfinal classA view of the keys inside this map. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected intSeed used to ensure the hash iteration order is different from an iteration to another.short[]The array holding keys.byte[]abs(next[i])=offsetto next chained entry index.protected intMap size (number of entries).long[]The array holding values. -
Constructor Summary
ConstructorsConstructorDescriptionNew instance with sane defaults.ShortLongWormMap(int expectedElements) New instance with the provided defaults.ShortLongWormMap(ShortLongAssociativeContainer container) Creates a new instance from all key-value pairs of another container. -
Method Summary
Modifier and TypeMethodDescriptionlongaddTo(short key, long additionValue) An equivalent of callingprotected voidallocateBuffers(int capacity) voidclear()Clear all keys and values in the container.clone()Clones this map.booleancontainsKey(short key) Returnstrueif this container has an association to a value for the given key.voidensureCapacity(int expectedElements) Ensure this container can hold at least the given number of elements without resizing its buffers.booleanCompares the specified object with this set for equality.<T extends ShortLongProcedure>
TforEach(T procedure) Applies a given procedure to all keys-value pairs in this container.static ShortLongWormMapfrom(short[] keys, long[] values) Creates a new instance from two index-aligned arrays of key-value pairs.longget(short key) longgetOrDefault(short key, long defaultValue) inthashCode()protected inthashKey(short key) booleanindexExists(int index) longindexGet(int index) Returns the value associated with an existing key.voidindexInsert(int index, short key, long value) Inserts a key-value pair for a key that is not present in the map.intindexOf(short key) Returns a logical "index" of a given key that can be used to speed up follow-up value setters or getters in certain scenarios (conditional logic).longindexRemove(int index) Removes a key-value pair at an index previously acquired fromShortLongMap.indexOf(short).longindexReplace(int index, long newValue) Replaces the value associated with an existing key and returns any previous value stored for that key.booleanisEmpty()iterator()Returns a cursor over the entries (key-value pairs) in this map.keys()Returns a collection of keys of this container.protected intProvides the next iteration seed used to build the iteration starting slot and offset increment.longnoValue()The value returned when there is no value associated to a key in this map.longput(short key, long value) Place a given key and value in the container.intputAll(ShortLongAssociativeContainer container) Puts all keys from another container to this map, replacing the values of existing keys, if such keys are present.intputAll(Iterable<? extends ShortLongCursor> iterable) Puts all keys from an iterable cursor to this map, replacing the values of existing keys, if such keys are present.booleanputIfAbsent(short key, long value) If the specified key is not already associated with a value, associates it with the given value.longputOrAdd(short key, long putValue, long incrementValue) Ifkeyexists,putValueis inserted into the map, otherwise any existing value is incremented byadditionValue.longAllocated memory estimationlongBytes that is actually been usedvoidrelease()Removes all elements from the collection and additionally releases any internal buffers.longremove(short key) Remove all values at the given key.intremoveAll(ShortLongPredicate predicate) Removes all keys (and associated values) for which the predicate returnstrue.intremoveAll(ShortPredicate predicate) Removes all keys (and associated values) for which the predicate returnstrue.intremoveAll(ShortContainer other) Removes all keys (and associated values) present in a given container.intsize()toString()values()Returns a container view of all values present in this container.visualizeKeyDistribution(int characters) Visually depict the distribution of keys.Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.lang.Iterable
forEach, spliterator
-
Field Details
-
keys
public short[] keysThe array holding keys. -
values
public long[] valuesThe array holding values. -
next
public byte[] nextabs(next[i])=offsetto next chained entry index.next[i]=0for free bucket.The offset is always forward, and the array is considered circular, meaning that an entry at the end of the array may point to an entry at the beginning with a positive offset.
The offset is always forward, but the sign of the offset encodes head/tail of chain.
next[i] > 0 for the first head-of-chain entry (within [1,WormUtil.maxOffset(int)]),next[i] < 0 for the subsequent tail-of-chain entries (within [-WormUtil.maxOffset(int),-1]. For the last entry in the chain,abs(next[i])=WormUtil.END_OF_CHAIN. -
size
protected int sizeMap size (number of entries). -
iterationSeed
protected int iterationSeedSeed used to ensure the hash iteration order is different from an iteration to another.
-
-
Constructor Details
-
ShortLongWormMap
public ShortLongWormMap()New instance with sane defaults. -
ShortLongWormMap
public ShortLongWormMap(int expectedElements) New instance with the provided defaults.There is no load factor parameter as this map enlarges automatically. In practice the load factor varies around 80% (between 75% and 90%). The load factor is 100% for tiny maps.
- Parameters:
expectedElements- The expected number of elements. The capacity of the map is calculated based on it.
-
ShortLongWormMap
Creates a new instance from all key-value pairs of another container.
-
-
Method Details
-
from
Creates a new instance from two index-aligned arrays of key-value pairs. -
clone
Clones this map. The cloning operation is efficient because it copies directly the internal arrays, without having to put entries in the cloned map. The cloned map has the same entries and the same capacity as this map. -
noValue
public long noValue()The value returned when there is no value associated to a key in this map. This method can be extended to change it. -
size
public int size()- Specified by:
sizein interfaceShortLongAssociativeContainer- Returns:
- Returns the current size (number of assigned keys) in the container.
-
isEmpty
public boolean isEmpty()- Specified by:
isEmptyin interfaceShortLongAssociativeContainer- Returns:
- Return
trueif this hash map contains no assigned keys.
-
get
public long get(short key) - Specified by:
getin interfaceShortLongMap- Returns:
- Returns the value associated with the given key or the default
value for the value type, if the key is not associated with any
value. For numeric value types, this default value is 0, for
object types it is
null.
-
getOrDefault
public long getOrDefault(short key, long defaultValue) - Specified by:
getOrDefaultin interfaceShortLongMap- Returns:
- Returns the value associated with the given key or the provided default value if the key is not associated with any value.
-
put
public long put(short key, long value) Place a given key and value in the container.- Specified by:
putin interfaceShortLongMap- Returns:
- The value previously stored under the given key in the map is returned.
-
putAll
Puts all keys from another container to this map, replacing the values of existing keys, if such keys are present.- Specified by:
putAllin interfaceShortLongMap- Returns:
- Returns the number of keys added to the map as a result of this call (not previously present in the map). Values of existing keys are overwritten.
-
putAll
Puts all keys from an iterable cursor to this map, replacing the values of existing keys, if such keys are present.- Specified by:
putAllin interfaceShortLongMap- Returns:
- Returns the number of keys added to the map as a result of this call (not previously present in the map). Values of existing keys are overwritten.
-
putOrAdd
public long putOrAdd(short key, long putValue, long incrementValue) Ifkeyexists,putValueis inserted into the map, otherwise any existing value is incremented byadditionValue.- Specified by:
putOrAddin interfaceShortLongMap- Parameters:
key- The key of the value to adjust.putValue- The value to put ifkeydoes not exist.incrementValue- The value to add to the existing value ifkeyexists.- Returns:
- Returns the current value associated with
key(after changes).
-
addTo
public long addTo(short key, long additionValue) An equivalent of callingputOrAdd(key, additionValue, additionValue);
- Specified by:
addToin interfaceShortLongMap- Parameters:
key- The key of the value to adjust.additionValue- The value to put or add to the existing value ifkeyexists.- Returns:
- Returns the current value associated with
key(after changes).
-
putIfAbsent
public boolean putIfAbsent(short key, long value) Description copied from interface:ShortLongMapIf the specified key is not already associated with a value, associates it with the given value.- Specified by:
putIfAbsentin interfaceShortLongMap- Parameters:
key- The key of the value to check.value- The value to put ifkeydoes not exist.- Returns:
trueifkeydid not exist andvaluewas placed in the map.
-
remove
public long remove(short key) Remove all values at the given key. The default value for the key type is returned if the value does not exist in the map.- Specified by:
removein interfaceShortLongMap
-
removeAll
Removes all keys (and associated values) present in a given container. An alias to:keys().removeAll(container)
but with no additional overhead.- Specified by:
removeAllin interfaceShortLongAssociativeContainer- Returns:
- Returns the number of elements actually removed as a result of this call.
-
removeAll
Removes all keys (and associated values) for which the predicate returnstrue.- Specified by:
removeAllin interfaceShortLongAssociativeContainer- Returns:
- Returns the number of elements actually removed as a result of this call.
-
removeAll
Removes all keys (and associated values) for which the predicate returnstrue.- Specified by:
removeAllin interfaceShortLongAssociativeContainer- Returns:
- Returns the number of elements actually removed as a result of this call.
-
forEach
Applies a given procedure to all keys-value pairs in this container. Returns the argument (any subclass ofShortLongProcedure. This lets the caller to call methods of the argument by chaining the call (even if the argument is an anonymous type) to retrieve computed values, for example.- Specified by:
forEachin interfaceShortLongAssociativeContainer
-
forEach
Applies a given predicate to all keys-value pairs in this container. Returns the argument (any subclass ofShortLongPredicate. This lets the caller to call methods of the argument by chaining the call (even if the argument is an anonymous type) to retrieve computed values, for example. The iteration is continued as long as the predicate returnstrue.- Specified by:
forEachin interfaceShortLongAssociativeContainer
-
keys
Returns a collection of keys of this container. The returned collection is a view over the key set and any modifications (if allowed) introduced to the collection will propagate to the associative container immediately.- Specified by:
keysin interfaceShortLongAssociativeContainer
-
values
Returns a container view of all values present in this container. The returned collection is a view over the key set and any modifications (if allowed) introduced to the collection will propagate to the associative container immediately.- Specified by:
valuesin interfaceShortLongAssociativeContainer
-
iterator
Returns a cursor over the entries (key-value pairs) in this map. The iterator is implemented as a cursor and it returns the same cursor instance on every call toIterator.next(). To read the current key and value use the cursor's public fields. An example is shown below.for (IntShortCursor c : intShortMap) { System.out.println("index=" + c.index + " key=" + c.key + " value=" + c.value); }The
indexfield inside the cursor gives the internal index inside the container's implementation. The interpretation of this index depends on to the container.- Specified by:
iteratorin interfaceIterable<ShortLongCursor>- Specified by:
iteratorin interfaceShortLongAssociativeContainer
-
containsKey
public boolean containsKey(short key) Returnstrueif this container has an association to a value for the given key.- Specified by:
containsKeyin interfaceShortLongAssociativeContainer
-
clear
public void clear()Clear all keys and values in the container.- Specified by:
clearin interfaceShortLongMap- See Also:
-
release
public void release()Removes all elements from the collection and additionally releases any internal buffers. Typically, if the object is to be reused, a simpleShortLongMap.clear()should be a better alternative since it'll avoid reallocation.- Specified by:
releasein interfaceShortLongMap- See Also:
-
equals
Compares the specified object with this set for equality. Returnstrueif and only if the specified object is also aShortLongMapand both objects contains exactly the same key-value pairs.- Specified by:
equalsin interfaceShortLongMap- Overrides:
equalsin classObject
-
hashCode
public int hashCode()- Specified by:
hashCodein interfaceShortLongMap- Overrides:
hashCodein classObject- Returns:
- A hash code of elements stored in the map. The hash code is defined as a sum of hash codes of keys and values stored within the set). Because sum is commutative, this ensures that different order of elements in a set does not affect the hash code.
-
hashKey
protected int hashKey(short key) -
indexOf
public int indexOf(short key) Returns a logical "index" of a given key that can be used to speed up follow-up value setters or getters in certain scenarios (conditional logic). The semantics of "indexes" are not strictly defined. Indexes may (and typically won't be) contiguous. The index is valid only between map modifications (it will not be affected by read-only operations like iteration or value retrievals).- Specified by:
indexOfin interfaceShortLongMap- Parameters:
key- The key to locate in the map.- Returns:
- A non-negative value of the logical "index" of the key in the map or a negative value if the key did not exist.
- See Also:
-
indexExists
public boolean indexExists(int index) - Specified by:
indexExistsin interfaceShortLongMap- Parameters:
index- The index of a given key, as returned fromShortLongMap.indexOf(short).- Returns:
- Returns
trueif the index corresponds to an existing key or false otherwise. This is equivalent to checking whether the index is a positive value (existing keys) or a negative value (non-existing keys). - See Also:
-
indexGet
public long indexGet(int index) Returns the value associated with an existing key.- Specified by:
indexGetin interfaceShortLongMap- Parameters:
index- The index of an existing key.- Returns:
- Returns the value currently associated with the key.
- See Also:
-
indexReplace
public long indexReplace(int index, long newValue) Replaces the value associated with an existing key and returns any previous value stored for that key.- Specified by:
indexReplacein interfaceShortLongMap- Parameters:
index- The index of an existing key.- Returns:
- Returns the previous value associated with the key.
- See Also:
-
indexInsert
public void indexInsert(int index, short key, long value) Inserts a key-value pair for a key that is not present in the map. This method may help in avoiding double recalculation of the key's hash.- Specified by:
indexInsertin interfaceShortLongMap- Parameters:
index- The index of a previously non-existing key, as returned fromShortLongMap.indexOf(short).- See Also:
-
indexRemove
public long indexRemove(int index) Removes a key-value pair at an index previously acquired fromShortLongMap.indexOf(short).- Specified by:
indexRemovein interfaceShortLongMap- Parameters:
index- The index of the key to remove, as returned fromShortLongMap.indexOf(short).- Returns:
- Returns the previous value associated with the key.
- See Also:
-
toString
-
ensureCapacity
public void ensureCapacity(int expectedElements) Ensure this container can hold at least the given number of elements without resizing its buffers.- Specified by:
ensureCapacityin interfacePreallocable- Parameters:
expectedElements- The total number of elements, inclusive.
-
visualizeKeyDistribution
Visually depict the distribution of keys.- Specified by:
visualizeKeyDistributionin interfaceShortLongMap- Parameters:
characters- The number of characters to "squeeze" the entire buffer into.- Returns:
- Returns a sequence of characters where '.' depicts an empty fragment of the internal buffer and 'X' depicts full or nearly full capacity within the buffer's range and anything between 1 and 9 is between.
-
ramBytesAllocated
public long ramBytesAllocated()Allocated memory estimation- Specified by:
ramBytesAllocatedin interfaceAccountable- Returns:
- Ram allocated in bytes
-
ramBytesUsed
public long ramBytesUsed()Bytes that is actually been used- Specified by:
ramBytesUsedin interfaceAccountable- Returns:
- Ram used in bytes
-
allocateBuffers
protected void allocateBuffers(int capacity) -
nextIterationSeed
protected int nextIterationSeed()Provides the next iteration seed used to build the iteration starting slot and offset increment. This method does not need to be synchronized, what matters is that each thread gets a sequence of varying seeds.
-