public class OrderedMap<K,V>
extends java.lang.Object
implements java.util.SortedMap<K,V>, java.io.Serializable, java.lang.Cloneable
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.
IntVLA
, and is modifiable with this
class' reorder(int...)
and shuffle(IRNG)
methods, among other tools. It may be preferable to avoid instantiating an Iterator object and instead
use a normal int-based for loop with getAt(int)
called in each iteration. Though this doesn't allow easy deletion of items during iteration, it may be the
fastest way to iterate through an OrderedMap.
firstKey()
without
having to create an iterator; however, this class partially violates the SortedMap
contract because all submap methods throw an exception and comparator()
returns always
null
.
getAndMoveToFirst()
, make it easy to use instances of this class as a cache (e.g., with LRU policy).
OrderedSet
can be used like a list of unique elements, keeping
order like a list does but also allowing rapid checks for whether an item exists in the OrderedSet, and OrderedMap
can be used like that but with values associated as well (where OrderedSet uses contains(), OrderedMap uses
containsKey()). You can also set the key and value at a position with putAt(Object, Object, int)
, or alter
the key while keeping its value and index the same with alter(Object, Object)
. Reordering works here too,
both with completely random orders from shuffle(IRNG)
or with a previously-generated ordering from
reorder(int...)
(you can produce such an ordering for a given size and reuse it across multiple Ordered data
structures with IRNG.randomOrdering(int)
). Note that putAt() and removeAt(int)
do not run in constant
time, and depending on the point of insertion/removal, they are likely to run in linear time (but also note that most
insertion-ordered Maps and Sets don't allow insertion or removal at anywhere but the beginning or end of the order).
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 |
OrderedMap.KeyIterator
An iterator on keys.
|
class |
OrderedMap.KeySet |
class |
OrderedMap.MapEntrySet |
class |
OrderedMap.ValueCollection |
class |
OrderedMap.ValueIterator
An iterator on values.
|
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 OrderedMap.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 OrderedMap.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 IntVLA |
order
An IntVLA (variable-length int sequence) that stores the positions in the key array of specific keys, with the
positions in insertion order.
|
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 |
---|
OrderedMap()
Creates a new OrderedMap with initial expected 16 entries and 0.75f as load factor.
|
OrderedMap(java.util.Collection<K> keyColl,
java.util.Collection<V> valueColl)
Creates a new OrderedMap using the elements of two parallel arrays.
|
OrderedMap(java.util.Collection<K> keyColl,
java.util.Collection<V> valueColl,
float f)
Creates a new OrderedMap using the elements of two parallel arrays.
|
OrderedMap(CrossHash.IHasher hasher)
Creates a new OrderedMap with initial expected 16 entries and 0.75f as load factor.
|
OrderedMap(int expected)
Creates a new OrderedMap with 0.75f as load factor.
|
OrderedMap(int expected,
CrossHash.IHasher hasher)
Creates a new OrderedMap with 0.75f as load factor.
|
OrderedMap(int expected,
float f)
Creates a new OrderedMap.
|
OrderedMap(int expected,
float f,
CrossHash.IHasher hasher)
Creates a new OrderedMap.
|
OrderedMap(K[] keyArray,
V[] valueArray)
Creates a new OrderedMap with 0.75f as load factor using the elements of two parallel arrays.
|
OrderedMap(K[] keyArray,
V[] valueArray,
CrossHash.IHasher hasher)
Creates a new OrderedMap with 0.75f as load factor using the elements of two parallel arrays.
|
OrderedMap(K[] keyArray,
V[] valueArray,
float f)
Creates a new OrderedMap using the elements of two parallel arrays.
|
OrderedMap(K[] keyArray,
V[] valueArray,
float f,
CrossHash.IHasher hasher)
Creates a new OrderedMap using the elements of two parallel arrays.
|
OrderedMap(java.util.Map<? extends K,? extends V> m)
Creates a new OrderedMap with 0.75f as load factor copying a given one.
|
OrderedMap(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.
|
OrderedMap(java.util.Map<? extends K,? extends V> m,
float f)
Creates a new OrderedMap copying a given one.
|
OrderedMap(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 |
---|---|
V |
alter(K original,
K replacement)
Swaps a key, original, for another key, replacement, while keeping replacement at the same point in the iteration
order as original and keeping it associated with the same value (which also keeps its iteration index).
|
V |
alterAt(int index,
K replacement)
Changes the K at the given index to replacement while keeping replacement at the same point in the ordering.
|
V |
alterAtCarefully(int index,
K replacement)
Changes the K at the given index to replacement while keeping replacement at the same point in the ordering.
|
V |
alterCarefully(K original,
K replacement)
Swaps a key, original, for another key, replacement, while keeping replacement at the same point in the iteration
order as original and keeping it associated with the same value (which also keeps its iteration index).
|
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() |
OrderedMap<K,V> |
clone()
Returns a deep copy of this map.
|
java.util.Comparator<? super K> |
comparator() |
boolean |
containsKey(java.lang.Object k) |
boolean |
containsValue(java.lang.Object v) |
V |
defaultReturnValue() |
void |
defaultReturnValue(V rv) |
void |
ensureCapacity(int capacity) |
java.util.Map.Entry<K,V> |
entryAt(int idx)
Gets the key-value Map.Entry at the given index in the iteration order in constant time (random-access).
|
java.util.SortedSet<java.util.Map.Entry<K,V>> |
entrySet() |
boolean |
equals(java.lang.Object o) |
K |
firstKey()
Returns the first key of this map in iteration order.
|
protected int |
fixOrder(int i)
Modifies the ordering so that the given entry is removed.
|
protected void |
fixOrder(int s,
int d)
Modifies the ordering for a shift from s to d.
|
V |
get(java.lang.Object k) |
V |
getAndMoveToFirst(K k)
Returns the value to which the given key is mapped; if the key is
present, it is moved to the first position of the iteration order.
|
V |
getAndMoveToLast(K k)
Returns the value to which the given key is mapped; if the key is
present, it is moved to the last position of the iteration order.
|
V |
getAt(int idx)
Gets the value at the given index in the iteration order in constant time (random-access).
|
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.
|
java.util.SortedMap<K,V> |
headMap(K to) |
int |
indexOf(java.lang.Object k)
Gets the position in the ordering of the given key, though not as efficiently as some data structures can do it
(e.g.
|
boolean |
isEmpty() |
K |
keyAt(int idx)
Gets the key at the given index in the iteration order in constant time (random-access).
|
OrderedSet<K> |
keysAsOrderedSet() |
java.util.SortedSet<K> |
keySet() |
K |
lastKey()
Returns the last key of this map in iteration order.
|
static <K,V> OrderedMap<K,V> |
makeMap()
Makes an empty OrderedMap (OM); needs key and value types to be specified in order to work.
|
static <K,V> OrderedMap<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 static <K> int |
objectUnwrap(java.util.Iterator<? extends K> i,
K[] array)
Unwraps an iterator into an array.
|
protected static <K> int |
objectUnwrap(java.util.Iterator<? extends K> i,
K[] array,
int offset,
int max)
Unwraps an iterator into an array starting at a given offset for a given number of elements.
|
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 |
putAndMoveToFirst(K k,
V v)
Adds a pair to the map; if the key is already present, it is moved to the
first position of the iteration order.
|
V |
putAndMoveToLast(K k,
V v)
Adds a pair to the map; if the key is already present, it is moved to the
last position of the iteration order.
|
V |
putAt(K k,
V v,
int idx) |
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. |
OrderedMap<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. |
java.util.Map.Entry<K,V> |
randomEntry(IRNG rng)
Gets a random entry from this OrderedMap in constant time, using the given IRNG to generate a random number.
|
K |
randomKey(IRNG rng)
Gets a random key from this OrderedMap in constant time, using the given IRNG to generate a random number.
|
V |
randomValue(IRNG rng)
Gets a random value from this OrderedMap in constant time, using the given IRNG to generate a random number.
|
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 |
removeAt(int idx)
Removes the key and value at the given index in the iteration order in not-exactly constant time (though it still
should be efficient).
|
protected V |
removeEntry(int pos) |
V |
removeFirst()
Removes the mapping associated with the first key in iteration order.
|
V |
removeLast()
Removes the mapping associated with the last key in iteration order.
|
protected V |
removeNullEntry() |
OrderedMap<K,V> |
reorder(int... ordering)
Given an array or varargs of replacement indices for this OrderedMap's iteration order, reorders this so the
first item in the returned version is the same as
getAt(ordering[0]) (with some care taken for negative
or too-large indices), the second item in the returned version is the same as getAt(ordering[1]) , etc. |
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.
|
void |
reverse()
Reverses the iteration order in linear time.
|
V |
setAt(V v,
int idx)
Changes the value at a specified
idx in the iteration order to v , without involving keys at all. |
protected void |
shiftKeys(int pos)
Shifts left entries with the specified hash code, starting at the
specified position, and empties the resulting free entry.
|
OrderedMap<K,V> |
shuffle(IRNG rng)
Randomly alters the iteration order for this OrderedMap using the given IRNG to shuffle.
|
int |
size() |
void |
sort(java.util.Comparator<? super K> comparator)
Sorts this whole OrderedMap on its keys using the supplied Comparator.
|
void |
sort(java.util.Comparator<? super K> comparator,
int start,
int end)
Sorts a sub-range of this OrderedMap on its keys from what is currently the index
start up to (but not
including) the index end , using the supplied Comparator. |
void |
sortByValue(java.util.Comparator<? super V> comparator)
Sorts this whole OrderedMap on its values using the supplied Comparator.
|
void |
sortByValue(java.util.Comparator<? super V> comparator,
int start,
int end)
Sorts a sub-range of this OrderedMap on its values from what is currently the index
start up to (but not
including) the index end , using the supplied Comparator. |
java.util.SortedMap<K,V> |
subMap(K from,
K to) |
boolean |
swap(K left,
K right)
Swaps the positions in the ordering for the given items, if they are both present.
|
boolean |
swapIndices(int left,
int right)
Swaps the given indices in the ordering, if they are both valid int indices.
|
java.util.SortedMap<K,V> |
tailMap(K from) |
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.
|
protected int |
unwrap(OrderedMap.ValueIterator i,
java.lang.Object[] array)
Unwraps an iterator into an array.
|
protected int |
unwrap(OrderedMap.ValueIterator i,
java.lang.Object[] array,
int offset,
int max)
Unwraps an iterator into an array starting at a given offset for a given number of elements.
|
java.util.Collection<V> |
values() |
java.util.ArrayList<V> |
valuesAsList() |
protected K[] key
protected V[] value
protected int mask
protected boolean containsNullKey
protected IntVLA order
reorder(int...)
and other methods.protected int n
protected int maxFill
f
.protected int size
public final float f
protected volatile OrderedMap.MapEntrySet entries
protected volatile OrderedMap.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 OrderedMap(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 OrderedMap(int expected)
expected
- the expected number of elements in the OrderedMap.public OrderedMap()
public OrderedMap(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 OrderedMap(java.util.Map<? extends K,? extends V> m)
m
- a Map
to be copied into the new OrderedMap.public OrderedMap(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 OrderedMap(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 OrderedMap(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 OrderedMap(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 OrderedMap(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 OrderedMap(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 OrderedMap(CrossHash.IHasher hasher)
public OrderedMap(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 OrderedMap(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 OrderedMap(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 OrderedMap(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 ensureCapacity(int capacity)
protected V removeEntry(int pos)
protected V removeNullEntry()
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)
public V setAt(V v, int idx)
idx
in the iteration order to v
, without involving keys at all.
If idx
isn't currently a valid index in the iteration order, this returns defaultReturnValue()
.v
- the new V value to assignidx
- the index in the iteration order to set v
atidx
in the iteration order, which may be null if the value was nullprotected final void shiftKeys(int pos)
pos
- a starting position.public V removeFirst()
java.util.NoSuchElementException
- is this map is empty.public V removeLast()
java.util.NoSuchElementException
- is this map is empty.public V getAndMoveToFirst(K k)
k
- the key.public V getAndMoveToLast(K k)
k
- the key.public V putAndMoveToFirst(K k, V v)
k
- the key.v
- the value.public V putAndMoveToLast(K k, V v)
k
- the key.v
- the value.protected int positionOf(java.lang.Object k)
public int indexOf(java.lang.Object k)
Arrangement
can access ordering position very quickly but doesn't store other values on its own).
Returns a value that is at least 0 if it found k, or -1 if k was not present.k
- a key or possible key that this should find the index ofpublic boolean swap(K left, K right)
left
- an item that should be present in this OrderedMapright
- an item that should be present in this OrderedMappublic boolean swapIndices(int left, int right)
public boolean containsKey(java.lang.Object k)
public boolean containsValue(java.lang.Object v)
protected int fixOrder(int i)
i
- the index of an entry.protected void fixOrder(int s, int d)
s
- the source position.d
- the destination position.public K firstKey()
public K lastKey()
public java.util.Comparator<? super K> comparator()
public java.util.SortedSet<K> keySet()
public OrderedSet<K> keysAsOrderedSet()
public java.util.Collection<V> values()
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 OrderedMap<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 )
.expected
- the expected number of elements in a hash table.f
- the load factor.java.lang.IllegalArgumentException
- if the necessary size is larger than 230.protected int unwrap(OrderedMap.ValueIterator i, java.lang.Object[] array, int offset, int max)
This method iterates over the given type-specific iterator and stores the elements returned, up to a maximum of length
, in the given array starting at offset
. The
number of actually unwrapped elements is returned (it may be less than max
if the iterator emits less than max
elements).
i
- a type-specific iterator.array
- an array to contain the output of the iterator.offset
- the first element of the array to be returned.max
- the maximum number of elements to unwrap.protected int unwrap(OrderedMap.ValueIterator i, java.lang.Object[] array)
This method iterates over the given type-specific iterator and stores the elements returned in the given array. The iteration will stop when the iterator has no more elements or when the end of the array has been reached.
i
- a type-specific iterator.array
- an array to contain the output of the iterator.protected static <K> int objectUnwrap(java.util.Iterator<? extends K> i, K[] array, int offset, int max)
This method iterates over the given type-specific iterator and stores the elements returned, up to a maximum of length
, in the given array starting at offset
. The
number of actually unwrapped elements is returned (it may be less than max
if the iterator emits less than max
elements).
i
- a type-specific iterator.array
- an array to contain the output of the iterator.offset
- the first element of the array to be returned.max
- the maximum number of elements to unwrap.protected static <K> int objectUnwrap(java.util.Iterator<? extends K> i, K[] array)
This method iterates over the given type-specific iterator and stores the elements returned in the given array. The iteration will stop when the iterator has no more elements or when the end of the array has been reached.
i
- a type-specific iterator.array
- an array to contain the output of the iterator.public java.lang.String toString()
toString
in class java.lang.Object
public boolean equals(java.lang.Object o)
public V getAt(int idx)
idx
- the index in the iteration order of the value to fetchpublic K keyAt(int idx)
idx
- the index in the iteration order of the key to fetchpublic java.util.Map.Entry<K,V> entryAt(int idx)
idx
- the index in the iteration order of the entry to fetchpublic V removeAt(int idx)
idx
- the index in the iteration order of the key and value to removepublic V randomValue(IRNG rng)
rng
- used to generate a random index for a valuepublic K randomKey(IRNG rng)
rng
- used to generate a random index for a keypublic java.util.Map.Entry<K,V> randomEntry(IRNG rng)
rng
- used to generate a random index for a entrypublic OrderedMap<K,V> shuffle(IRNG rng)
rng
- used to generate a random orderingpublic OrderedMap<K,V> reorder(int... ordering)
getAt(ordering[0])
(with some care taken for negative
or too-large indices), the second item in the returned version is the same as getAt(ordering[1])
, etc.
ordering[ordering.length - 1]
. If ordering is smaller than size()
, only the indices up to the
length of ordering will be modified. If ordering is larger than size()
, only as many indices will be
affected as size()
, and reversed distances are measured from the end of this Map's entries instead of
the end of ordering. Duplicate values in ordering will produce duplicate values in the returned Map.
ordering
- an array or varargs of int indices, where the nth item in ordering changes the nth item in this
Map to have the value currently in this Map at the index specified by the value in orderingpublic V alterCarefully(K original, K replacement)
alter(Object, Object)
, this will not change this OrderedMap if replacement is already
present. To contrast, alter() can reduce the size of the OrderedMap if both original and replacement are already
in the Map. If replacement is found, this returns the default return value, otherwise it switches out original
for replacement and returns whatever was associated with original.original
- the key to find and swap outreplacement
- the key to replace original withpublic V alter(K original, K replacement)
alterCarefully(Object, Object)
if you don't feel like checking by hand for whether
replacement is already present, but using this method is perfectly reasonable if you know overlaps won't happen.original
- the key to find and swap outreplacement
- the key to replace original withpublic V alterAt(int index, K replacement)
alterAtCarefully(int, Object)
if you don't feel like checking by hand for whether
replacement is already present, but using this method is perfectly reasonable if you know overlaps won't happen.index
- an index to replace the K key atreplacement
- another K key that will replace the original at the remembered indexpublic V alterAtCarefully(int index, K replacement)
alterAt(int, Object)
, this will not change this OrderedMap if replacement is
already present. To contrast, alterAt() can reduce the size of the OrderedMap if replacement is already
in the Map. If replacement is found, this returns the default return value, otherwise it switches out the index
for replacement and returns whatever value was at the index before.index
- an index to replace the K key atreplacement
- another K key that will replace the original at the remembered indexpublic 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 OrderedMap<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> OrderedMap<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> OrderedMap<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 specifiedpublic void sort(java.util.Comparator<? super K> comparator)
comparator
- a Comparator that can be used on the same type this uses for its keys (may need wildcards)public void sort(java.util.Comparator<? super K> comparator, int start, int end)
start
up to (but not
including) the index end
, using the supplied Comparator.comparator
- a Comparator that can be used on the same type this uses for its keys (may need wildcards)start
- the first index of a key to sort (the index can change after this)end
- the exclusive bound on the indices to sort; often this is just size()
public void sortByValue(java.util.Comparator<? super V> comparator)
comparator
- a Comparator that can be used on the same type this uses for its values (may need wildcards)public void sortByValue(java.util.Comparator<? super V> comparator, int start, int end)
start
up to (but not
including) the index end
, using the supplied Comparator.comparator
- a Comparator that can be used on the same type this uses for its values (may need wildcards)start
- the first index of a value to sort (the index can change after this)end
- the exclusive bound on the indices to sort; often this is just size()
public void reverse()
Copyright © Eben Howard 2012–2022. All rights reserved.