Class IntIntOrderedMap
- All Implemented Interfaces:
Serializable
,Cloneable
public class IntIntOrderedMap extends Object implements Serializable, Cloneable
OrderedMap
) to support constant-time indexed access of keys,
values, and entries, and reordering. Deletion is slower with this algorithm (O(n) because the array used to keep
track of ordering and indexing only allows linear-time deletion, instead of a linked-list approach allowing
constant-time deletion but not helping at all with indexed access), and insertion sometimes takes longer than an
ordered map using a linked-list for order because rehashing is more expensive. The benefits from constant-time access
by index are numerous, especially for games, since fetching a random element becomes O(1) instead of O(n), and the
index can be used to store extra info for no cost.
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.
Iterators generated by this map will enumerate pairs in the same order in which they have been added to the map (addition of pairs whose key is already present in the set does not change the iteration order). Note that this order has nothing in common with the natural order of the keys. The order is kept by means of a int-specialized list,
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.
This class allows approximately constant-time lookup of keys or values by their index in the ordering, which can allow some novel usage of the data structure.
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(int, int, int)
, or alter
the key while keeping its value and index the same with alter(int, int)
. 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).
This doesn't allow custom IHashers because IHasher expects an Object to hash, and that would box the keys every time.
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
IntIntOrderedMap.FastEntryIterator
class
IntIntOrderedMap.KeyIterator
An iterator on keys.class
IntIntOrderedMap.KeySet
class
IntIntOrderedMap.MapEntry
The entry class for a OrderedMap does not record key and value, but rather the position in the hash table of the corresponding entry.class
IntIntOrderedMap.MapEntrySet
class
IntIntOrderedMap.ValueCollection
class
IntIntOrderedMap.ValueIterator
An iterator on values. -
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 int
defRetValue
Default return value.protected IntIntOrderedMap.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 int[]
key
The array of keys.protected IntIntOrderedMap.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 int[]
value
The array of values.protected IntIntOrderedMap.ValueCollection
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 IntIntOrderedMap()
Creates a new OrderedMap with initial expected 16 entries and 0.75f as load factor.IntIntOrderedMap(int expected)
Creates a new OrderedMap with 0.75f as load factor.IntIntOrderedMap(int[] keyArray, int[] valueArray)
Creates a new OrderedMap with 0.75f as load factor using the elements of two parallel arrays.IntIntOrderedMap(int[] keyArray, int[] valueArray, float f)
Creates a new OrderedMap using the elements of two parallel arrays.IntIntOrderedMap(int expected, float f)
Creates a new OrderedMap.IntIntOrderedMap(IntIntOrderedMap m)
Creates a new OrderedMap with 0.75f as load factor copying a given one.IntIntOrderedMap(IntIntOrderedMap m, float f)
Creates a new OrderedMap copying a given one.IntIntOrderedMap(IntVLA keyColl, IntVLA valueColl)
Creates a new OrderedMap using the elements of two parallel arrays.IntIntOrderedMap(IntVLA keyColl, IntVLA valueColl, float f)
Creates a new OrderedMap using the elements of two parallel arrays. -
Method Summary
Modifier and Type Method Description int
alter(int original, int 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).int
alterAt(int index, int replacement)
Changes the int at the given index to replacement while keeping replacement at the same point in the ordering.int
alterAtCarefully(int index, int replacement)
Changes the int at the given index to replacement while keeping replacement at the same point in the ordering.int
alterCarefully(int original, int 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 toMath.ceil( expected / f )
.void
clear()
IntIntOrderedMap
clone()
Returns a deep copy of this map.boolean
containsKey(int k)
boolean
containsValue(int v)
int
defaultReturnValue()
void
defaultReturnValue(int rv)
IntIntOrderedMap.MapEntry
entryAt(int idx)
Gets the key-value Map.Entry at the given index in the iteration order in constant time (random-access).SortedSet<IntIntOrderedMap.MapEntry>
entrySet()
boolean
equals(Object o)
int
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.int
get(int k)
int
getAndIncrement(int k, int increment)
Looks up the keyk
in this map, remembers the associated value (which will bedefaultReturnValue()
if k wasn't found), addsincrement
to the actual associated value in the map, and returns the remembered value.int
getAt(int idx)
Gets the value at the given index in the iteration order in constant time (random-access).IntVLA
getMany(int... keys)
int
getOrDefault(int k, int defaultValue)
long
hash64()
int
hashCode()
Returns a hash code for this map.int
indexOf(int 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()
int
keyAt(int idx)
Gets the key at the given index in the iteration order in constant time (random-access).int[]
keysAsArray()
OrderedSet<Integer>
keysAsOrderedSet()
IntIntOrderedMap.KeySet
keySet()
int
lastKey()
Returns the last key of this map in iteration order.static IntIntOrderedMap
makeMap()
Makes an empty IntIntOrderedMap.This method is provided for completeness relative to makeMap() with 2 or more parameters.static IntIntOrderedMap
makeMap(int k0, int v0, int... rest)
Makes an IntIntOrderedMap using the given int keys and values in alternating key-value-key-value order.static int
maxFill(int n, float f)
Returns the maximum number of entries that can be filled before rehashing.protected int
positionOf(int k)
int
put(int k, int v)
void
putAll(int[] keyArray, int[] valueArray)
Puts the first key in keyArray with the first value in valueArray, then the second in each and so on.void
putAll(IntIntOrderedMap m)
Puts all key-value pairs in the Map m into this OrderedMap.int
putAt(int k, int v, int idx)
int
putIfAbsent(int key, int value)
If the specified key is not already associated with a value, associates it with the given value and returns that value, else returns the current value without changing anything.IntIntOrderedMap
putPairs(int k0, int v0, int... rest)
Given alternating key and value arguments in pairs, puts each key-value pair into this OrderedMap as if by callingput(int, int)
repeatedly for each pair.IntIntOrderedMap.MapEntry
randomEntry(IRNG rng)
Gets a random entry from this OrderedMap in constant time, using the given IRNG to generate a random number.int
randomKey(IRNG rng)
Gets a random key from this OrderedMap in constant time, using the given IRNG to generate a random number.int
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.int
remove(int k)
boolean
remove(int key, int value)
Removes the entry for the specified key only if it is currently mapped to the specified value.int
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).int
removeFirst()
Removes the mapping associated with the first key in iteration order.int
removeLast()
Removes the mapping associated with the last key in iteration order.IntIntOrderedMap
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 asgetAt(ordering[0])
(with some care taken for negative or too-large indices), the second item in the returned version is the same asgetAt(ordering[1])
, etc.int
replace(int key, int value)
Replaces the entry for the specified key only if it is currently mapped to some value.boolean
replace(int key, int oldValue, int 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.protected void
shiftKeys(int pos)
Shifts left entries with the specified hash code, starting at the specified position, and empties the resulting free entry.IntIntOrderedMap
shuffle(IRNG rng)
Randomly alters the iteration order for this OrderedMap using the given IRNG to shuffle.int
size()
boolean
swap(int left, int 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.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<Integer>
values()
IntVLA
valuesAsList()
-
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. -
order
An IntVLA (variable-length int sequence) that stores the positions in the key array of specific keys, with the positions in insertion order. The order can be changed withreorder(int...)
and other methods. -
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
-
-
Constructor Details
-
IntIntOrderedMap
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.
-
IntIntOrderedMap
Creates a new OrderedMap with 0.75f as load factor.- Parameters:
expected
- the expected number of elements in the OrderedMap.
-
IntIntOrderedMap
public IntIntOrderedMap()Creates a new OrderedMap with initial expected 16 entries and 0.75f as load factor. -
IntIntOrderedMap
Creates a new OrderedMap copying a given one.- Parameters:
m
- aMap
to be copied into the new OrderedMap.f
- the load factor.
-
IntIntOrderedMap
Creates a new OrderedMap with 0.75f as load factor copying a given one.- Parameters:
m
- aMap
to be copied into the new OrderedMap.
-
IntIntOrderedMap
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.
-
IntIntOrderedMap
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.
-
IntIntOrderedMap
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.
-
IntIntOrderedMap
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.
-
-
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 int keys that should usually have the same length as valueArrayvalueArray
- an array of int 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.- Parameters:
m
- an IntIntOrderedMap to add into this
-
put
-
putAt
-
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
-
removeFirst
Removes the mapping associated with the first key in iteration order.- Returns:
- the value previously associated with the first key in iteration order.
- Throws:
NoSuchElementException
- is this map is empty.
-
removeLast
Removes the mapping associated with the last key in iteration order.- Returns:
- the value previously associated with the last key in iteration order.
- Throws:
NoSuchElementException
- is this map is empty.
-
get
-
getOrDefault
-
positionOf
-
indexOf
Gets the position in the ordering of the given key, though not as efficiently as some data structures can do it (e.g.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.- Parameters:
k
- a key or possible key that this should find the index of- Returns:
- the index of k, if present, or -1 if it is not present in this OrderedMap
-
swap
Swaps the positions in the ordering for the given items, if they are both present. Returns true if the ordering changed as a result of this call, or false if it stayed the same (which can be because left or right was not present, or because left and right are the same reference (so swapping would do nothing)).- Parameters:
left
- an item that should be present in this OrderedMapright
- an item that should be present in this OrderedMap- Returns:
- true if this OrderedMap changed in ordering as a result of this call, or false otherwise
-
swapIndices
Swaps the given indices in the ordering, if they are both valid int indices. Returns true if the ordering changed as a result of this call, or false if it stayed the same (which can be because left or right referred to an out-of-bounds index, or because left and right are equal (so swapping would do nothing)). -
containsKey
-
containsValue
-
clear
-
size
-
isEmpty
-
getAndIncrement
Looks up the keyk
in this map, remembers the associated value (which will bedefaultReturnValue()
if k wasn't found), addsincrement
to the actual associated value in the map, and returns the remembered value.- Parameters:
k
- a key to look upincrement
- an int to add to the value associated withk
- Returns:
- the previous value associated with k, or
defaultReturnValue()
if k wasn't found
-
fixOrder
Modifies the ordering so that the given entry is removed. This method will complete in logarithmic time.- Parameters:
i
- the index of an entry.- Returns:
- the iteration-order index of the removed entry
-
fixOrder
Modifies the ordering for a shift from s to d.
This method will complete in logarithmic time or better.- Parameters:
s
- the source position.d
- the destination position.
-
firstKey
Returns the first key of this map in iteration order.- Returns:
- the first key in iteration order.
-
lastKey
Returns the last key of this map in iteration order.- Returns:
- the last key in iteration order.
-
entrySet
-
keySet
-
keysAsOrderedSet
-
keysAsArray
-
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. -
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 )
.- 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
-
getAt
Gets the value at the given index in the iteration order in constant time (random-access).- Parameters:
idx
- the index in the iteration order of the value to fetch- Returns:
- the value at the index, if the index is valid, otherwise the default return value
-
keyAt
Gets the key at the given index in the iteration order in constant time (random-access).- Parameters:
idx
- the index in the iteration order of the key to fetch- Returns:
- the key at the index, if the index is valid, otherwise 0
-
entryAt
Gets the key-value Map.Entry at the given index in the iteration order in constant time (random-access).- Parameters:
idx
- the index in the iteration order of the entry to fetch- Returns:
- the key-value entry at the index, if the index is valid, otherwise null
-
removeAt
Removes the key and value at the given index in the iteration order in not-exactly constant time (though it still should be efficient).- Parameters:
idx
- the index in the iteration order of the key and value to remove- Returns:
- the value removed, if there was anything removed, or the default return value otherwise (often null)
-
randomValue
Gets a random value from this OrderedMap in constant time, using the given IRNG to generate a random number.- Parameters:
rng
- used to generate a random index for a value- Returns:
- a random value from this OrderedMap
-
randomKey
Gets a random key from this OrderedMap in constant time, using the given IRNG to generate a random number.- Parameters:
rng
- used to generate a random index for a key- Returns:
- a random key from this OrderedMap
-
randomEntry
Gets a random entry from this OrderedMap in constant time, using the given IRNG to generate a random number.- Parameters:
rng
- used to generate a random index for a entry- Returns:
- a random key-value entry from this OrderedMap
-
shuffle
Randomly alters the iteration order for this OrderedMap using the given IRNG to shuffle.- Parameters:
rng
- used to generate a random ordering- Returns:
- this for chaining
-
reorder
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 asgetAt(ordering[0])
(with some care taken for negative or too-large indices), the second item in the returned version is the same asgetAt(ordering[1])
, etc.
Negative indices are considered reversed distances from the end of ordering, so -1 refers to the same index asordering[ordering.length - 1]
. If ordering is smaller thansize()
, only the indices up to the length of ordering will be modified. If ordering is larger thansize()
, only as many indices will be affected assize()
, 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.
This method modifies this OrderedMap in-place and also returns it for chaining.- Parameters:
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 ordering- Returns:
- this for chaining, after modifying it in-place
-
alterCarefully
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). Unlike the similar methodalter(int, int)
, 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.- Parameters:
original
- the key to find and swap outreplacement
- the key to replace original with- Returns:
- the value associated with original before, and replacement now
-
alter
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). Be aware that if both original and replacement are present in the OrderedMap, this will still replace original with replacement but will also remove the other occurrence of replacement to avoid duplicate keys. This can throw off the expected order because the duplicate could be at any point in the ordering when it is removed. You may want to preferalterCarefully(int, int)
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.- Parameters:
original
- the key to find and swap outreplacement
- the key to replace original with- Returns:
- the value associated with original before, and replacement now
-
getMany
-
alterAt
Changes the int at the given index to replacement while keeping replacement at the same point in the ordering. Be aware that if replacement is present in the OrderedMap, this will still replace the given index with replacement but will also remove the other occurrence of replacement to avoid duplicate keys. This can throw off the expected order because the duplicate could be at any point in the ordering when it is removed. You may want to preferalterAtCarefully(int, int)
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.- Parameters:
index
- an index to replace the int key atreplacement
- another int key that will replace the original at the remembered index- Returns:
- the value associated with the possibly-altered key
-
alterAtCarefully
Changes the int at the given index to replacement while keeping replacement at the same point in the ordering. Unlike the similar methodalterAt(int, int)
, 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.- Parameters:
index
- an index to replace the int key atreplacement
- another int key that will replace the original at the remembered index- Returns:
- the value associated with the key at the altered index before, and replacement now
-
putIfAbsent
If the specified key is not already associated with a value, associates it with the given value and returns that value, else returns the current value without changing anything.- 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
value
if there was no mapping for the key.
-
remove
Removes the entry for the specified key only if it is currently mapped to the specified value.- Parameters:
key
- key with which the specified value is associatedvalue
- value expected to be associated with the specified key- Returns:
true
if the value was removed
-
replace
Replaces the entry for the specified key only if currently mapped to the specified value. The position in the iteration order is retained.- Parameters:
key
- key with which the specified value is associatedoldValue
- value expected to be associated with the specified keynewValue
- value to be associated with the specified key- Returns:
true
if the value was replaced
-
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.- 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(int, int)
repeatedly for each pair. This mimics the parameter syntax used formakeMap(int, int, int...)
, and can be used to retain that style of insertion after an IntIntOrderedMap 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 only int elements- Returns:
- this, after adding all viable key-value pairs given
-
makeMap
Makes an IntIntOrderedMap using the given int keys and values in alternating key-value-key-value order. If rest has an odd-number length, then it discards the last item. If any item in rest is not an int, this will not compile. This is similar to the makeOM method in the Maker class, but produces an IntIntOrderedMap.
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.- 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 only int elements- Returns:
- a freshly-made IntIntOrderedMap, using k0, v0, and the contents of rest to fill it
-
makeMap
Makes an empty IntIntOrderedMap.This method is provided for completeness relative to makeMap() with 2 or more parameters.- Returns:
- an empty IntIntOrderedMap
-
reverse
Reverses the iteration order in linear time.
-