Package cern.colt.map
Class OpenLongObjectHashMap<T>
- java.lang.Object
-
- cern.colt.PersistentObject
-
- cern.colt.map.AbstractMap
-
- cern.colt.map.AbstractLongObjectMap<T>
-
- cern.colt.map.OpenLongObjectHashMap<T>
-
- All Implemented Interfaces:
Serializable
,Cloneable
public class OpenLongObjectHashMap<T> extends AbstractLongObjectMap<T>
Hash map holding (key,value) associations of type(long-->Object)
; Automatically grows and shrinks as needed; Implemented using open addressing with double hashing. First see the package summary and javadoc tree view to get the broad picture. Overrides many methods for performance reasons only.- Version:
- 1.0, 09/24/99
- Author:
- wolfgang.hoschek@cern.ch
- See Also:
HashMap
, Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description protected static byte
FREE
protected int
freeEntries
The number of table entries in state==FREE.protected static byte
FULL
protected static byte
REMOVED
protected byte[]
state
The state of each hash table entry (FREE, FULL, REMOVED).protected long[]
table
The hash table keys.protected T[]
values
The hash table values.-
Fields inherited from class cern.colt.map.AbstractMap
defaultCapacity, defaultMaxLoadFactor, defaultMinLoadFactor, distinct, highWaterMark, lowWaterMark, maxLoadFactor, minLoadFactor
-
-
Constructor Summary
Constructors Constructor Description OpenLongObjectHashMap()
Constructs an empty map with default capacity and default load factors.OpenLongObjectHashMap(int initialCapacity)
Constructs an empty map with the specified initial capacity and default load factors.OpenLongObjectHashMap(int initialCapacity, double minLoadFactor, double maxLoadFactor)
Constructs an empty map with the specified initial capacity and the specified minimum and maximum load factor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
clear()
Removes all (key,value) associations from the receiver.Object
clone()
Returns a deep copy of the receiver.boolean
containsKey(long key)
Returnstrue
if the receiver contains the specified key.boolean
containsValue(T value)
Returnstrue
if the receiver contains the specified value.void
ensureCapacity(int minCapacity)
Ensures that the receiver can hold at least the specified number of associations without needing to allocate new internal memory.boolean
forEachKey(LongProcedure procedure)
Applies a procedure to each key of the receiver, if any.boolean
forEachPair(LongObjectProcedure<T> procedure)
Applies a procedure to each (key,value) pair of the receiver, if any.T
get(long key)
Returns the value associated with the specified key.protected int
indexOfInsertion(long key)
protected int
indexOfKey(long key)
protected int
indexOfValue(T value)
long
keyOf(T value)
Returns the first key the given value is associated with.void
keys(LongArrayList list)
Fills all keys contained in the receiver into the specified list.void
pairsMatching(LongObjectProcedure condition, LongArrayList keyList, ObjectArrayList valueList)
Fills all pairs satisfying a given condition into the specified lists.boolean
put(long key, T value)
Associates the given key with the given value.protected void
rehash(int newCapacity)
Rehashes the contents of the receiver into a new table with a smaller or larger capacity.boolean
removeKey(long key)
Removes the given key with its associated element from the receiver, if present.protected void
setUp(int initialCapacity, double minLoadFactor, double maxLoadFactor)
Initializes the receiver.void
trimToSize()
Trims the capacity of the receiver to be the receiver's current size.void
values(ObjectArrayList list)
Fills all values contained in the receiver into the specified list.-
Methods inherited from class cern.colt.map.AbstractLongObjectMap
copy, equals, keys, keysSortedByValue, pairsSortedByKey, pairsSortedByValue, toString, toStringByValue, values
-
Methods inherited from class cern.colt.map.AbstractMap
chooseGrowCapacity, chooseHighWaterMark, chooseLowWaterMark, chooseMeanCapacity, chooseShrinkCapacity, isEmpty, nextPrime, size
-
-
-
-
Field Detail
-
table
protected long[] table
The hash table keys.
-
values
protected T[] values
The hash table values.
-
state
protected byte[] state
The state of each hash table entry (FREE, FULL, REMOVED).
-
freeEntries
protected int freeEntries
The number of table entries in state==FREE.
-
FREE
protected static final byte FREE
- See Also:
- Constant Field Values
-
FULL
protected static final byte FULL
- See Also:
- Constant Field Values
-
REMOVED
protected static final byte REMOVED
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
OpenLongObjectHashMap
public OpenLongObjectHashMap()
Constructs an empty map with default capacity and default load factors.
-
OpenLongObjectHashMap
public OpenLongObjectHashMap(int initialCapacity)
Constructs an empty map with the specified initial capacity and default load factors.- Parameters:
initialCapacity
- the initial capacity of the map.- Throws:
IllegalArgumentException
- if the initial capacity is less than zero.
-
OpenLongObjectHashMap
public OpenLongObjectHashMap(int initialCapacity, double minLoadFactor, double maxLoadFactor)
Constructs an empty map with the specified initial capacity and the specified minimum and maximum load factor.- Parameters:
initialCapacity
- the initial capacity.minLoadFactor
- the minimum load factor.maxLoadFactor
- the maximum load factor.- Throws:
IllegalArgumentException
- ifinitialCapacity < 0 || (minLoadFactor < 0.0 || minLoadFactor >= 1.0) || (maxLoadFactor <= 0.0 || maxLoadFactor >= 1.0) || (minLoadFactor >= maxLoadFactor)
.
-
-
Method Detail
-
clear
public void clear()
Removes all (key,value) associations from the receiver. Implicitly callstrimToSize()
.- Specified by:
clear
in classAbstractMap
-
clone
public Object clone()
Returns a deep copy of the receiver.- Overrides:
clone
in classPersistentObject
- Returns:
- a deep copy of the receiver.
-
containsKey
public boolean containsKey(long key)
Returnstrue
if the receiver contains the specified key.- Overrides:
containsKey
in classAbstractLongObjectMap<T>
- Returns:
true
if the receiver contains the specified key.
-
containsValue
public boolean containsValue(T value)
Returnstrue
if the receiver contains the specified value.- Overrides:
containsValue
in classAbstractLongObjectMap<T>
- Returns:
true
if the receiver contains the specified value.
-
ensureCapacity
public void ensureCapacity(int minCapacity)
Ensures that the receiver can hold at least the specified number of associations without needing to allocate new internal memory. If necessary, allocates new internal memory and increases the capacity of the receiver.This method never need be called; it is for performance tuning only. Calling this method before
put()
ing a large number of associations boosts performance, because the receiver will grow only once instead of potentially many times and hash collisions get less probable.- Overrides:
ensureCapacity
in classAbstractMap
- Parameters:
minCapacity
- the desired minimum capacity.
-
forEachKey
public boolean forEachKey(LongProcedure procedure)
Applies a procedure to each key of the receiver, if any. Note: Iterates over the keys in no particular order. Subclasses can define a particular order, for example, "sorted by key". All methods which can be expressed in terms of this method (most methods can) must guarantee to use the same order defined by this method, even if it is no particular order. This is necessary so that, for example, methodskeys
andvalues
will yield association pairs, not two uncorrelated lists.- Specified by:
forEachKey
in classAbstractLongObjectMap<T>
- Parameters:
procedure
- the procedure to be applied. Stops iteration if the procedure returnsfalse
, otherwise continues.- Returns:
false
if the procedure stopped before all keys where iterated over,true
otherwise.
-
forEachPair
public boolean forEachPair(LongObjectProcedure<T> procedure)
Applies a procedure to each (key,value) pair of the receiver, if any. Iteration order is guaranteed to be identical to the order used by methodforEachKey(LongProcedure)
.- Overrides:
forEachPair
in classAbstractLongObjectMap<T>
- Parameters:
procedure
- the procedure to be applied. Stops iteration if the procedure returnsfalse
, otherwise continues.- Returns:
false
if the procedure stopped before all keys where iterated over,true
otherwise.
-
get
public T get(long key)
Returns the value associated with the specified key. It is often a good idea to first check withcontainsKey(long)
whether the given key has a value associated or not, i.e. whether there exists an association for the given key or not.- Specified by:
get
in classAbstractLongObjectMap<T>
- Parameters:
key
- the key to be searched for.- Returns:
- the value associated with the specified key;
null
if no such key is present.
-
indexOfInsertion
protected int indexOfInsertion(long key)
- Parameters:
key
- the key to be added to the receiver.- Returns:
- the index where the key would need to be inserted, if it is not already contained. Returns -index-1 if the key is already contained at slot index. Therefore, if the returned index < 0, then it is already contained at slot -index-1. If the returned index >= 0, then it is NOT already contained and should be inserted at slot index.
-
indexOfKey
protected int indexOfKey(long key)
- Parameters:
key
- the key to be searched in the receiver.- Returns:
- the index where the key is contained in the receiver, returns -1 if the key was not found.
-
indexOfValue
protected int indexOfValue(T value)
- Parameters:
value
- the value to be searched in the receiver.- Returns:
- the index where the value is contained in the receiver, returns -1 if the value was not found.
-
keyOf
public long keyOf(T value)
Returns the first key the given value is associated with. It is often a good idea to first check withcontainsValue(Object)
whether there exists an association from a key to this value. Search order is guaranteed to be identical to the order used by methodforEachKey(LongProcedure)
.- Overrides:
keyOf
in classAbstractLongObjectMap<T>
- Parameters:
value
- the value to search for.- Returns:
- the first key for which holds
get(key) == value
; returnsLong.MIN_VALUE
if no such key exists.
-
keys
public void keys(LongArrayList list)
Fills all keys contained in the receiver into the specified list. Fills the list, starting at index 0. After this call returns the specified list has a new size that equalsthis.size()
. Iteration order is guaranteed to be identical to the order used by methodforEachKey(LongProcedure)
.This method can be used to iterate over the keys of the receiver.
- Overrides:
keys
in classAbstractLongObjectMap<T>
- Parameters:
list
- the list to be filled, can have any size.
-
pairsMatching
public void pairsMatching(LongObjectProcedure condition, LongArrayList keyList, ObjectArrayList valueList)
Fills all pairs satisfying a given condition into the specified lists. Fills into the lists, starting at index 0. After this call returns the specified lists both have a new size, the number of pairs satisfying the condition. Iteration order is guaranteed to be identical to the order used by methodforEachKey(LongProcedure)
.Example:
LongObjectProcedure condition = new LongObjectProcedure() { // match even keys only public boolean apply(long key, Object value) { return key%2==0; } } keys = (8,7,6), values = (1,2,2) --> keyList = (6,8), valueList = (2,1)
- Overrides:
pairsMatching
in classAbstractLongObjectMap<T>
- Parameters:
condition
- the condition to be matched. Takes the current key as first and the current value as second argument.keyList
- the list to be filled with keys, can have any size.valueList
- the list to be filled with values, can have any size.
-
put
public boolean put(long key, T value)
Associates the given key with the given value. Replaces any old(key,someOtherValue)
association, if existing.- Specified by:
put
in classAbstractLongObjectMap<T>
- Parameters:
key
- the key the value shall be associated with.value
- the value to be associated.- Returns:
true
if the receiver did not already contain such a key;false
if the receiver did already contain such a key - the new value has now replaced the formerly associated value.
-
rehash
protected void rehash(int newCapacity)
Rehashes the contents of the receiver into a new table with a smaller or larger capacity. This method is called automatically when the number of keys in the receiver exceeds the high water mark or falls below the low water mark.
-
removeKey
public boolean removeKey(long key)
Removes the given key with its associated element from the receiver, if present.- Specified by:
removeKey
in classAbstractLongObjectMap<T>
- Parameters:
key
- the key to be removed from the receiver.- Returns:
true
if the receiver contained the specified key,false
otherwise.
-
setUp
protected void setUp(int initialCapacity, double minLoadFactor, double maxLoadFactor)
Initializes the receiver.- Overrides:
setUp
in classAbstractMap
- Parameters:
initialCapacity
- the initial capacity of the receiver.minLoadFactor
- the minLoadFactor of the receiver.maxLoadFactor
- the maxLoadFactor of the receiver.- Throws:
IllegalArgumentException
- ifinitialCapacity < 0 || (minLoadFactor < 0.0 || minLoadFactor >= 1.0) || (maxLoadFactor <= 0.0 || maxLoadFactor >= 1.0) || (minLoadFactor >= maxLoadFactor)
.
-
trimToSize
public void trimToSize()
Trims the capacity of the receiver to be the receiver's current size. Releases any superfluous internal memory. An application can use this operation to minimize the storage of the receiver.- Overrides:
trimToSize
in classAbstractMap
-
values
public void values(ObjectArrayList list)
Fills all values contained in the receiver into the specified list. Fills the list, starting at index 0. After this call returns the specified list has a new size that equalsthis.size()
. Iteration order is guaranteed to be identical to the order used by methodforEachKey(LongProcedure)
.This method can be used to iterate over the values of the receiver.
- Overrides:
values
in classAbstractLongObjectMap<T>
- Parameters:
list
- the list to be filled, can have any size.
-
-