public class UnorderedMap<K,V>
extends java.lang.Object
implements java.util.Map<K,V>, java.io.Serializable, java.lang.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.
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.
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).
Modifier and Type | Class and Description |
---|---|
class |
UnorderedMap.ValueCollection |
Modifier and Type | Field and 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 java.util.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 and Description |
---|
UnorderedMap()
Creates a new OrderedMap with initial expected 16 entries and 0.75f as load factor.
|
UnorderedMap(java.util.Collection<K> keyColl,
java.util.Collection<V> valueColl)
Creates a new OrderedMap using the elements of two parallel arrays.
|
UnorderedMap(java.util.Collection<K> keyColl,
java.util.Collection<V> valueColl,
float f)
Creates a new OrderedMap 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.
|
UnorderedMap(int expected)
Creates a new OrderedMap with 0.75f as load factor.
|
UnorderedMap(int expected,
CrossHash.IHasher hasher)
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(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,
CrossHash.IHasher hasher)
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(java.util.Map<? extends K,? extends V> m)
Creates a new OrderedMap with 0.75f as load factor copying a given one.
|
UnorderedMap(java.util.Map<? extends K,? extends V> m,
CrossHash.IHasher hasher)
Creates a new OrderedMap with 0.75f as load factor copying a given one.
|
UnorderedMap(java.util.Map<? extends K,? extends V> m,
float f)
Creates a new OrderedMap copying a given one.
|
UnorderedMap(java.util.Map<? extends K,? extends V> m,
float f,
CrossHash.IHasher hasher)
Creates a new OrderedMap copying a given one.
|
Modifier and Type | Method and 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 to
Math.ceil( expected / f ) . |
void |
clear() |
UnorderedMap<K,V> |
clone()
Returns a deep copy of this map.
|
boolean |
containsKey(java.lang.Object k) |
boolean |
containsValue(java.lang.Object v) |
V |
defaultReturnValue() |
void |
defaultReturnValue(V rv) |
java.util.Set<java.util.Map.Entry<K,V>> |
entrySet() |
boolean |
equals(java.lang.Object o) |
V |
get(java.lang.Object k) |
java.util.List<V> |
getMany(java.util.Collection<K> keys) |
V |
getOrDefault(java.lang.Object k,
V defaultValue) |
long |
hash64() |
int |
hashCode()
Returns a hash code for this map.
|
boolean |
isEmpty() |
java.util.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,
java.lang.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(java.lang.Object k) |
V |
put(K k,
V v) |
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.
|
void |
putAll(java.util.Map<? extends K,? extends V> m)
Puts all key-value pairs in the Map m into this OrderedMap.
|
V |
putIfAbsent(K key,
V value)
If the specified key is not already associated with a value (or is mapped
to
null ) associates it with the given value and returns
null , else returns the current value. |
UnorderedMap<K,V> |
putPairs(K k0,
V v0,
java.lang.Object... rest)
Given alternating key and value arguments in pairs, puts each key-value pair into this OrderedMap as if by
calling
put(Object, Object) repeatedly for each pair. |
protected void |
rehash(int newN)
Rehashes the map.
|
V |
remove(java.lang.Object k) |
boolean |
remove(java.lang.Object key,
java.lang.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() |
java.lang.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.
|
java.util.Collection<V> |
values() |
java.util.ArrayList<V> |
valuesAsList() |
protected K[] key
protected V[] value
protected int mask
protected boolean containsNullKey
protected int n
protected int maxFill
f
.protected int size
public final float f
protected volatile squidpony.squidmath.UnorderedMap.MapEntrySet entries
protected volatile squidpony.squidmath.UnorderedMap.KeySet keys
protected volatile java.util.Collection<V> values
protected V defRetValue
public static final int DEFAULT_INITIAL_SIZE
public static final float DEFAULT_LOAD_FACTOR
public static final float FAST_LOAD_FACTOR
public static final float VERY_FAST_LOAD_FACTOR
protected final CrossHash.IHasher hasher
public UnorderedMap(int expected, float f)
The actual table size will be the least power of two greater than expected
/f
.
expected
- the expected number of elements in the hash set.f
- the load factor.public UnorderedMap(int expected)
expected
- the expected number of elements in the OrderedMap.public UnorderedMap()
public UnorderedMap(java.util.Map<? extends K,? extends V> m, float f)
m
- a Map
to be copied into the new OrderedMap.f
- the load factor.public UnorderedMap(java.util.Map<? extends K,? extends V> m)
m
- a Map
to be copied into the new OrderedMap.public UnorderedMap(K[] keyArray, V[] valueArray, float f)
keyArray
- the array of keys of the new OrderedMap.valueArray
- the array of corresponding values in the new OrderedMap.f
- the load factor.java.lang.IllegalArgumentException
- if k
and v
have different lengths.public UnorderedMap(java.util.Collection<K> keyColl, java.util.Collection<V> valueColl)
keyColl
- the collection of keys of the new OrderedMap.valueColl
- the collection of corresponding values in the new OrderedMap.java.lang.IllegalArgumentException
- if k
and v
have different lengths.public UnorderedMap(java.util.Collection<K> keyColl, java.util.Collection<V> valueColl, float f)
keyColl
- the collection of keys of the new OrderedMap.valueColl
- the collection of corresponding values in the new OrderedMap.f
- the load factor.java.lang.IllegalArgumentException
- if k
and v
have different lengths.public UnorderedMap(K[] keyArray, V[] valueArray)
keyArray
- the array of keys of the new OrderedMap.valueArray
- the array of corresponding values in the new OrderedMap.java.lang.IllegalArgumentException
- if k
and v
have different lengths.public UnorderedMap(int expected, float f, CrossHash.IHasher hasher)
The actual table size will be the least power of two greater than expected
/f
.
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 implementationspublic UnorderedMap(int expected, CrossHash.IHasher hasher)
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 implementationspublic UnorderedMap(CrossHash.IHasher hasher)
public UnorderedMap(java.util.Map<? extends K,? extends V> m, float f, CrossHash.IHasher hasher)
m
- a Map
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 implementationspublic UnorderedMap(java.util.Map<? extends K,? extends V> m, CrossHash.IHasher hasher)
m
- a Map
to be copied into the new OrderedMap.hasher
- used to hash items; typically only needed when K is an array, where CrossHash has implementationspublic UnorderedMap(K[] keyArray, V[] valueArray, float f, CrossHash.IHasher hasher)
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 implementationsjava.lang.IllegalArgumentException
- if k
and v
have different lengths.public UnorderedMap(K[] keyArray, V[] valueArray, CrossHash.IHasher hasher)
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 implementationsjava.lang.IllegalArgumentException
- if k
and v
have different lengths.public void defaultReturnValue(V rv)
public V defaultReturnValue()
public void putAll(K[] keyArray, V[] valueArray)
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 keyArraypublic void putAll(java.util.Map<? extends K,? extends V> m)
protected final void shiftKeys(int pos)
pos
- a starting position.protected int positionOf(java.lang.Object k)
public boolean containsKey(java.lang.Object k)
public boolean containsValue(java.lang.Object v)
public java.util.ArrayList<V> valuesAsList()
public boolean trim()
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.
trim(int)
public boolean trim(int n)
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.
n
- the threshold for the trimming.trim()
protected void rehash(int newN)
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.
newN
- the new sizepublic UnorderedMap<K,V> clone()
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.
clone
in class java.lang.Object
public int hashCode()
equals()
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.public long hash64()
public static int maxFill(int n, float f)
n
- the size of the backing array.f
- the load factor.public static int arraySize(int expected, float f)
Math.ceil( expected / f )
.
reexpected
- the expected number of elements in a hash table.f
- the load factor.java.lang.IllegalArgumentException
- if the necessary size is larger than 230.public java.lang.String toString()
toString
in class java.lang.Object
public boolean equals(java.lang.Object o)
public V putIfAbsent(K key, V value)
null
) associates it with the given value and returns
null
, else returns the current value.putIfAbsent
in interface java.util.Map<K,V>
key
- key with which the specified value is to be associatedvalue
- value to be associated with the specified keynull
if there was no mapping for the key.
(A null
return can also indicate that the map
previously associated null
with the key.)public boolean remove(java.lang.Object key, java.lang.Object value)
public boolean replace(K key, V oldValue, V newValue)
public V replace(K key, V value)
replace
in interface java.util.Map<K,V>
key
- key with which the specified value is associatedvalue
- value to be associated with the specified keynull
if there was no mapping for the key.
(A null
return can also indicate that the map
previously associated null
with the key.)public UnorderedMap<K,V> putPairs(K k0, V v0, java.lang.Object... rest)
put(Object, Object)
repeatedly for each pair. This mimics the parameter syntax used for
makeMap(Object, Object, Object...)
, and can be used to retain that style of insertion after an
OrderedMap has been instantiated.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... elementspublic static <K,V> UnorderedMap<K,V> makeMap(K k0, V v0, java.lang.Object... rest)
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 v0k0
- 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... elementspublic static <K,V> UnorderedMap<K,V> makeMap()
Maker.<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.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 specifiedCopyright © Eben Howard 2012–2022. All rights reserved.