Class UnorderedMap<K,V>
- All Implemented Interfaces:
Serializable
,Cloneable
,Map<K,V>
public class UnorderedMap<K,V> extends Object implements Map<K,V>, Serializable, Cloneable
HashMap
unless you need array keys. Originally from fastutil
as Object2ObjectOpenCustomHashMap but modified to support SquidLib's CrossHash.IHasher
interface for custom hashing instead of fastutil's Strategy interface.
Instances of this class use a hash table to represent a map. The table is filled up to a specified load factor, and then doubled in size to accommodate new entries. If the table is emptied below one fourth of the load factor, it is halved in size. However, halving is not performed when deleting entries from an iterator, as it would interfere with the iteration process.
Note that
clear()
does not modify the hash table size. Rather, a family of trimming methods lets you control the size of the table; this is particularly useful if
you reuse instances of this class.
You can pass a
CrossHash.IHasher
instance such as CrossHash.generalHasher
as an extra parameter to
most of this class' constructors, which allows the OrderedMap to use arrays (usually primitive arrays) as keys. If
you expect only one type of array, you can use an instance like CrossHash.intHasher
to hash int arrays, or
the aforementioned generalHasher to hash most kinds of arrays (it can't handle most multi-dimensional arrays well).
If you aren't using arrays as keys, you don't need to give an IHasher to the constructor and can ignore this feature
most of the time. However, the default IHasher this uses if none is specified performs a small but significant
"mixing" step to make the default generated hashCode() implementation many classes use into a higher-quality
random-like value. This isn't always optimal; if you plan to insert 1000 sequential Integer keys with some small
amount of random Integers after them, then the mixing actually increases the likelihood of a collision and takes time
to calculate. You could use a very simple IHasher in that case, relying on the fact that only Integers will be added:
new CrossHash.IHasher() { public int hash(Object data) { return (int)data; } public boolean areEqual(Object left, Object right) { return Objects.equals(left, right); } };This is just one example of a case where a custom IHasher can be useful for performance reasons; there are also cases where an IHasher is needed to enforce hashing by identity or by value, which affect program logic. Note that the given IHasher is likely to be sub-optimal for many situations with Integer keys, and you may want to try a few different approaches if you know OrderedMap is a bottleneck in your application. If the IHasher is a performance problem, it will be at its worst if the OrderedMap needs to resize, and thus rehash, many times; this won't happen if the capacity is set correctly when the OrderedMap is created (with the capacity equal to or greater than the maximum number of entries that will be added).
Thank you, Sebastiano Vigna, for making FastUtil available to the public with such high quality.
See https://github.com/vigna/fastutil for the original library.
- Author:
- Sebastiano Vigna (responsible for all the hard parts), Tommy Ettinger (mostly responsible for squashing several layers of parent classes into one monster class)
- See Also:
- Serialized Form
-
Nested Class Summary
Nested Classes Modifier and Type Class Description class
UnorderedMap.ValueCollection
-
Field Summary
Fields Modifier and Type Field Description protected boolean
containsNullKey
Whether this set contains the key zero.static int
DEFAULT_INITIAL_SIZE
The initial default size of a hash table.static float
DEFAULT_LOAD_FACTOR
The default load factor of a hash table.protected V
defRetValue
Default return value.protected squidpony.squidmath.UnorderedMap.MapEntrySet
entries
Cached set of entries.float
f
The acceptable load factor.static float
FAST_LOAD_FACTOR
The load factor for a (usually small) table that is meant to be particularly fast.protected CrossHash.IHasher
hasher
protected K[]
key
The array of keys.protected squidpony.squidmath.UnorderedMap.KeySet
keys
Cached set of keys.protected int
mask
The mask for wrapping a position counter.protected int
maxFill
Threshold after which we rehash.protected int
n
The current table size.protected int
size
Number of entries in the set (including the key zero, if present).protected V[]
value
The array of values.protected Collection<V>
values
Cached collection of values.static float
VERY_FAST_LOAD_FACTOR
The load factor for a (usually very small) table that is meant to be extremely fast. -
Constructor Summary
Constructors Constructor Description UnorderedMap()
Creates a new OrderedMap with initial expected 16 entries and 0.75f as load factor.UnorderedMap(int expected)
Creates a new OrderedMap with 0.75f as load factor.UnorderedMap(int expected, float f)
Creates a new OrderedMap.UnorderedMap(int expected, float f, CrossHash.IHasher hasher)
Creates a new OrderedMap.UnorderedMap(int expected, CrossHash.IHasher hasher)
Creates a new OrderedMap with 0.75f as load factor.UnorderedMap(Collection<K> keyColl, Collection<V> valueColl)
Creates a new OrderedMap using the elements of two parallel arrays.UnorderedMap(Collection<K> keyColl, Collection<V> valueColl, float f)
Creates a new OrderedMap using the elements of two parallel arrays.UnorderedMap(Map<? extends K,? extends V> m)
Creates a new OrderedMap with 0.75f as load factor copying a given one.UnorderedMap(Map<? extends K,? extends V> m, float f)
Creates a new OrderedMap copying a given one.UnorderedMap(Map<? extends K,? extends V> m, float f, CrossHash.IHasher hasher)
Creates a new OrderedMap copying a given one.UnorderedMap(Map<? extends K,? extends V> m, CrossHash.IHasher hasher)
Creates a new OrderedMap with 0.75f as load factor copying a given one.UnorderedMap(K[] keyArray, V[] valueArray)
Creates a new OrderedMap with 0.75f as load factor using the elements of two parallel arrays.UnorderedMap(K[] keyArray, V[] valueArray, float f)
Creates a new OrderedMap using the elements of two parallel arrays.UnorderedMap(K[] keyArray, V[] valueArray, float f, CrossHash.IHasher hasher)
Creates a new OrderedMap using the elements of two parallel arrays.UnorderedMap(K[] keyArray, V[] valueArray, CrossHash.IHasher hasher)
Creates a new OrderedMap with 0.75f as load factor using the elements of two parallel arrays.UnorderedMap(CrossHash.IHasher hasher)
Creates a new OrderedMap with initial expected 16 entries and 0.75f as load factor. -
Method Summary
Modifier and Type Method Description static int
arraySize(int expected, float f)
Returns the least power of two smaller than or equal to 230 and larger than or equal toMath.ceil( expected / f )
.void
clear()
UnorderedMap<K,V>
clone()
Returns a deep copy of this map.boolean
containsKey(Object k)
boolean
containsValue(Object v)
V
defaultReturnValue()
void
defaultReturnValue(V rv)
Set<Map.Entry<K,V>>
entrySet()
boolean
equals(Object o)
V
get(Object k)
List<V>
getMany(Collection<K> keys)
V
getOrDefault(Object k, V defaultValue)
long
hash64()
int
hashCode()
Returns a hash code for this map.boolean
isEmpty()
Set<K>
keySet()
static <K, V> UnorderedMap<K,V>
makeMap()
Makes an empty OrderedMap (OM); needs key and value types to be specified in order to work.static <K, V> UnorderedMap<K,V>
makeMap(K k0, V v0, Object... rest)
Makes an OrderedMap (OM) with the given load factor (which should be between 0.1 and 0.9), key and value types inferred from the types of k0 and v0, and considers all remaining parameters key-value pairs, casting the Objects at positions 0, 2, 4...static int
maxFill(int n, float f)
Returns the maximum number of entries that can be filled before rehashing.protected int
positionOf(Object k)
V
put(K k, V v)
void
putAll(Map<? extends K,? extends V> m)
Puts all key-value pairs in the Map m into this OrderedMap.void
putAll(K[] keyArray, V[] valueArray)
Puts the first key in keyArray with the first value in valueArray, then the second in each and so on.V
putIfAbsent(K key, V value)
If the specified key is not already associated with a value (or is mapped tonull
) associates it with the given value and returnsnull
, else returns the current value.UnorderedMap<K,V>
putPairs(K k0, V v0, Object... rest)
Given alternating key and value arguments in pairs, puts each key-value pair into this OrderedMap as if by callingput(Object, Object)
repeatedly for each pair.protected void
rehash(int newN)
Rehashes the map.V
remove(Object k)
boolean
remove(Object key, Object value)
Removes the entry for the specified key only if it is currently mapped to the specified value.V
replace(K key, V value)
Replaces the entry for the specified key only if it is currently mapped to some value.boolean
replace(K key, V oldValue, V newValue)
Replaces the entry for the specified key only if currently mapped to the specified value.protected void
shiftKeys(int pos)
Shifts left entries with the specified hash code, starting at the specified position, and empties the resulting free entry.int
size()
String
toString()
boolean
trim()
Rehashes the map, making the table as small as possible.boolean
trim(int n)
Rehashes this map if the table is too large.Collection<V>
values()
ArrayList<V>
valuesAsList()
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.util.Map
compute, computeIfAbsent, computeIfPresent, forEach, merge, replaceAll
-
Field Details
-
key
The array of keys. -
value
The array of values. -
mask
The mask for wrapping a position counter. -
containsNullKey
Whether this set contains the key zero. -
n
The current table size. -
maxFill
Threshold after which we rehash. It must be the table size timesf
. -
size
Number of entries in the set (including the key zero, if present). -
f
The acceptable load factor. -
entries
Cached set of entries. -
keys
Cached set of keys. -
values
Cached collection of values. -
defRetValue
Default return value. -
DEFAULT_INITIAL_SIZE
The initial default size of a hash table.- See Also:
- Constant Field Values
-
DEFAULT_LOAD_FACTOR
The default load factor of a hash table.- See Also:
- Constant Field Values
-
FAST_LOAD_FACTOR
The load factor for a (usually small) table that is meant to be particularly fast.- See Also:
- Constant Field Values
-
VERY_FAST_LOAD_FACTOR
The load factor for a (usually very small) table that is meant to be extremely fast.- See Also:
- Constant Field Values
-
hasher
-
-
Constructor Details
-
UnorderedMap
Creates a new OrderedMap.The actual table size will be the least power of two greater than
expected
/f
.- Parameters:
expected
- the expected number of elements in the hash set.f
- the load factor.
-
UnorderedMap
Creates a new OrderedMap with 0.75f as load factor.- Parameters:
expected
- the expected number of elements in the OrderedMap.
-
UnorderedMap
public UnorderedMap()Creates a new OrderedMap with initial expected 16 entries and 0.75f as load factor. -
UnorderedMap
Creates a new OrderedMap copying a given one.- Parameters:
m
- aMap
to be copied into the new OrderedMap.f
- the load factor.
-
UnorderedMap
Creates a new OrderedMap with 0.75f as load factor copying a given one.- Parameters:
m
- aMap
to be copied into the new OrderedMap.
-
UnorderedMap
Creates a new OrderedMap using the elements of two parallel arrays.- Parameters:
keyArray
- the array of keys of the new OrderedMap.valueArray
- the array of corresponding values in the new OrderedMap.f
- the load factor.- Throws:
IllegalArgumentException
- ifk
andv
have different lengths.
-
UnorderedMap
Creates a new OrderedMap using the elements of two parallel arrays.- Parameters:
keyColl
- the collection of keys of the new OrderedMap.valueColl
- the collection of corresponding values in the new OrderedMap.- Throws:
IllegalArgumentException
- ifk
andv
have different lengths.
-
UnorderedMap
Creates a new OrderedMap using the elements of two parallel arrays.- Parameters:
keyColl
- the collection of keys of the new OrderedMap.valueColl
- the collection of corresponding values in the new OrderedMap.f
- the load factor.- Throws:
IllegalArgumentException
- ifk
andv
have different lengths.
-
UnorderedMap
Creates a new OrderedMap with 0.75f as load factor using the elements of two parallel arrays.- Parameters:
keyArray
- the array of keys of the new OrderedMap.valueArray
- the array of corresponding values in the new OrderedMap.- Throws:
IllegalArgumentException
- ifk
andv
have different lengths.
-
UnorderedMap
Creates a new OrderedMap.The actual table size will be the least power of two greater than
expected
/f
.- Parameters:
expected
- the expected number of elements in the hash set.f
- the load factor.hasher
- used to hash items; typically only needed when K is an array, where CrossHash has implementations
-
UnorderedMap
Creates a new OrderedMap with 0.75f as load factor.- Parameters:
expected
- the expected number of elements in the OrderedMap.hasher
- used to hash items; typically only needed when K is an array, where CrossHash has implementations
-
UnorderedMap
Creates a new OrderedMap with initial expected 16 entries and 0.75f as load factor. -
UnorderedMap
Creates a new OrderedMap copying a given one.- Parameters:
m
- aMap
to be copied into the new OrderedMap.f
- the load factor.hasher
- used to hash items; typically only needed when K is an array, where CrossHash has implementations
-
UnorderedMap
Creates a new OrderedMap with 0.75f as load factor copying a given one.- Parameters:
m
- aMap
to be copied into the new OrderedMap.hasher
- used to hash items; typically only needed when K is an array, where CrossHash has implementations
-
UnorderedMap
Creates a new OrderedMap using the elements of two parallel arrays.- Parameters:
keyArray
- the array of keys of the new OrderedMap.valueArray
- the array of corresponding values in the new OrderedMap.f
- the load factor.hasher
- used to hash items; typically only needed when K is an array, where CrossHash has implementations- Throws:
IllegalArgumentException
- ifk
andv
have different lengths.
-
UnorderedMap
Creates a new OrderedMap with 0.75f as load factor using the elements of two parallel arrays.- Parameters:
keyArray
- the array of keys of the new OrderedMap.valueArray
- the array of corresponding values in the new OrderedMap.hasher
- used to hash items; typically only needed when K is an array, where CrossHash has implementations- Throws:
IllegalArgumentException
- ifk
andv
have different lengths.
-
-
Method Details
-
defaultReturnValue
-
defaultReturnValue
-
putAll
Puts the first key in keyArray with the first value in valueArray, then the second in each and so on. The entries are all appended to the end of the iteration order, unless a key was already present. Then, its value is changed at the existing position in the iteration order. If the lengths of the two arrays are not equal, this puts a number of entries equal to the lesser length. If either array is null, this returns without performing any changes.- Parameters:
keyArray
- an array of K keys that should usually have the same length as valueArrayvalueArray
- an array of V values that should usually have the same length as keyArray
-
putAll
Puts all key-value pairs in the Map m into this OrderedMap. The entries are all appended to the end of the iteration order, unless a key was already present. Then, its value is changed at the existing position in the iteration order. This can take any kind of Map, including unordered HashMap objects; if the Map does not have stable ordering, the order in which entries will be appended is not stable either. For this reason, OrderedMap, LinkedHashMap, and TreeMap (or other SortedMap implementations) will work best when order matters. -
put
-
shiftKeys
Shifts left entries with the specified hash code, starting at the specified position, and empties the resulting free entry.- Parameters:
pos
- a starting position.
-
remove
-
get
-
getOrDefault
- Specified by:
getOrDefault
in interfaceMap<K,V>
-
positionOf
-
containsKey
- Specified by:
containsKey
in interfaceMap<K,V>
-
containsValue
- Specified by:
containsValue
in interfaceMap<K,V>
-
clear
-
size
-
isEmpty
-
entrySet
-
keySet
-
values
-
valuesAsList
-
trim
Rehashes the map, making the table as small as possible.This method rehashes the table to the smallest size satisfying the load factor. It can be used when the set will not be changed anymore, so to optimize access speed and size.
If the table size is already the minimum possible, this method does nothing.
- Returns:
- true if there was enough memory to trim the map.
- See Also:
trim(int)
-
trim
Rehashes this map if the table is too large.Let N be the smallest table size that can hold
max(n,
entries, still satisfying the load factor. If the current table size is smaller than or equal to N, this method does nothing. Otherwise, it rehashes this map in a table of size N.size()
)This method is useful when reusing maps. Clearing a map leaves the table size untouched. If you are reusing a map many times, you can call this method with a typical size to avoid keeping around a very large table just because of a few large transient maps.
- Parameters:
n
- the threshold for the trimming.- Returns:
- true if there was enough memory to trim the map.
- See Also:
trim()
-
rehash
Rehashes the map.This method implements the basic rehashing strategy, and may be overriden by subclasses implementing different rehashing strategies (e.g., disk-based rehashing). However, you should not override this method unless you understand the internal workings of this class.
- Parameters:
newN
- the new size
-
clone
Returns a deep copy of this map.This method performs a deep copy of this OrderedMap; the data stored in the map, however, is not cloned. Note that this makes a difference only for object keys.
-
hashCode
Returns a hash code for this map. This method overrides the generic method provided by the superclass. Sinceequals()
is not overriden, it is important that the value returned by this method is the same value as the one returned by the overriden method. -
hash64
-
maxFill
Returns the maximum number of entries that can be filled before rehashing.- Parameters:
n
- the size of the backing array.f
- the load factor.- Returns:
- the maximum number of entries before rehashing.
-
arraySize
Returns the least power of two smaller than or equal to 230 and larger than or equal toMath.ceil( expected / f )
. re- Parameters:
expected
- the expected number of elements in a hash table.f
- the load factor.- Returns:
- the minimum possible size for a backing array.
- Throws:
IllegalArgumentException
- if the necessary size is larger than 230.
-
toString
-
equals
-
getMany
-
putIfAbsent
If the specified key is not already associated with a value (or is mapped tonull
) associates it with the given value and returnsnull
, else returns the current value.- Specified by:
putIfAbsent
in interfaceMap<K,V>
- 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
null
if there was no mapping for the key. (Anull
return can also indicate that the map previously associatednull
with the key.)
-
remove
Removes the entry for the specified key only if it is currently mapped to the specified value. -
replace
Replaces the entry for the specified key only if currently mapped to the specified value. The position in the iteration order is retained. -
replace
Replaces the entry for the specified key only if it is currently mapped to some value. Preserves the existing key's position in the iteration order.- Specified by:
replace
in interfaceMap<K,V>
- Parameters:
key
- key with which the specified value is associatedvalue
- value to be associated with the specified key- Returns:
- the previous value associated with the specified key, or
null
if there was no mapping for the key. (Anull
return can also indicate that the map previously associatednull
with the key.)
-
putPairs
Given alternating key and value arguments in pairs, puts each key-value pair into this OrderedMap as if by callingput(Object, Object)
repeatedly for each pair. This mimics the parameter syntax used formakeMap(Object, Object, Object...)
, and can be used to retain that style of insertion after an OrderedMap has been instantiated.- Parameters:
k0
- the first key to addv0
- the first value to addrest
- an array or vararg of keys and values in pairs; should contain alternating K, V, K, V... elements- Returns:
- this, after adding all viable key-value pairs given
-
makeMap
Makes an OrderedMap (OM) with the given load factor (which should be between 0.1 and 0.9), key and value types inferred from the types of k0 and v0, and considers all remaining parameters key-value pairs, casting the Objects at positions 0, 2, 4... etc. to K and the objects at positions 1, 3, 5... etc. to V. If rest has an odd-number length, then it discards the last item. If any pair of items in rest cannot be cast to the correct type of K or V, then this inserts nothing for that pair. This is similar to the makeOM method in the Maker class, but does not allow setting the load factor (since that extra parameter can muddle how javac figures out which generic types the map should use), nor does it log debug information if a cast fails. The result should be the same otherwise.
This is named makeMap to indicate that it expects key and value parameters, unlike a Set or List. This convention may be extended to other data structures that also have static methods for instantiation.- Type Parameters:
K
- the type of keys in the returned OrderedMap; if not specified, will be inferred from k0V
- the type of values in the returned OrderedMap; if not specified, will be inferred from v0- Parameters:
k0
- the first key; used to infer the types of other keys if generic parameters aren't specified.v0
- the first value; used to infer the types of other values if generic parameters aren't specified.rest
- an array or vararg of keys and values in pairs; should contain alternating K, V, K, V... elements- Returns:
- a freshly-made OrderedMap with K keys and V values, using k0, v0, and the contents of rest to fill it
-
makeMap
Makes an empty OrderedMap (OM); needs key and value types to be specified in order to work. For an empty OrderedMap with String keys and Coord values, you could useMaker.<String, Coord>makeOM()
. Using the new keyword is probably just as easy in this case; this method is provided for completeness relative to makeMap() with 2 or more parameters.- Type Parameters:
K
- the type of keys in the returned OrderedMap; cannot be inferred and must be specifiedV
- the type of values in the returned OrderedMap; cannot be inferred and must be specified- Returns:
- an empty OrderedMap with the given key and value types.
-