Package com.github.tommyettinger.ds
Class IdentityObjectOrderedMap<K,V>
java.lang.Object
com.github.tommyettinger.ds.ObjectObjectMap<K,V>
com.github.tommyettinger.ds.ObjectObjectOrderedMap<K,V>
com.github.tommyettinger.ds.IdentityObjectOrderedMap<K,V>
- All Implemented Interfaces:
Arrangeable,Ordered<K>,Iterable<Map.Entry<K,,V>> Map<K,V>
A variant on
Note that the
ObjectObjectOrderedMap that compares keys by identity (using ==) instead of equality (using equals()}).
It also hashes with System.identityHashCode(Object) instead of calling the hashCode() of a key. This can be useful in
some cases where keys may have invalid Object.equals(Object) and/or Object.hashCode() implementations, or if keys could
be very large (making a hashCode() that uses all the items in the key slow). Oddly, System.identityHashCode(Object) tends to
be slower than the hashCode() for most small keys, because an explicitly-written hashCode() typically doesn't need to do anything
concurrently, but identityHashCode() needs to (concurrently) modify an internal JVM variable that ensures the results are unique, and
that requires the JVM to do lots of extra work whenever identityHashCode() is called. Despite that, identityHashCode() doesn't depend
on the quantity of variables in the key, so identityHashCode() gets relatively faster for larger keys. The equals() method used by
ObjectObjectOrderedMap also tends to slow down for large keys, relative to the constant-time == this uses.
Note that the
ObjectObjectOrderedMap.entrySet(), ObjectObjectOrderedMap.keySet() and individual Entry items this
produces are those of an ObjectObjectOrderedMap, and so do not compare by identity.-
Nested Class Summary
Nested classes/interfaces inherited from class com.github.tommyettinger.ds.ObjectObjectOrderedMap
ObjectObjectOrderedMap.OrderedMapEntries<K,V>, ObjectObjectOrderedMap.OrderedMapKeys<K, V>, ObjectObjectOrderedMap.OrderedMapValues<K, V> Nested classes/interfaces inherited from class com.github.tommyettinger.ds.ObjectObjectMap
ObjectObjectMap.Entries<K,V>, ObjectObjectMap.Entry<K, V>, ObjectObjectMap.Keys<K, V>, ObjectObjectMap.MapIterator<K, V, I>, ObjectObjectMap.Values<K, V> Nested classes/interfaces inherited from interface com.github.tommyettinger.ds.Arrangeable
Arrangeable.ArrangeableList<T>Nested classes/interfaces inherited from interface com.github.tommyettinger.ds.Ordered
Ordered.OfBoolean, Ordered.OfByte, Ordered.OfChar, Ordered.OfDouble, Ordered.OfFloat, Ordered.OfInt, Ordered.OfLong, Ordered.OfShort -
Field Summary
Fields inherited from class com.github.tommyettinger.ds.ObjectObjectOrderedMap
keysFields inherited from class com.github.tommyettinger.ds.ObjectObjectMap
defaultValue, entries1, entries2, hashMultiplier, keys1, keys2, keyTable, loadFactor, mask, shift, size, threshold, values1, values2, valueTable -
Constructor Summary
ConstructorsConstructorDescriptionCreates a new map with an initial capacity ofUtilities.getDefaultTableCapacity()and a load factor ofUtilities.getDefaultLoadFactor().IdentityObjectOrderedMap(int initialCapacity) Creates a new map with the given starting capacity and a load factor ofUtilities.getDefaultLoadFactor().IdentityObjectOrderedMap(int initialCapacity, float loadFactor) Creates a new map with the specified initial capacity and load factor.IdentityObjectOrderedMap(int initialCapacity, float loadFactor, OrderType ordering) Creates a new map with the specified initial capacity and load factor.IdentityObjectOrderedMap(int initialCapacity, OrderType ordering) Creates a new map with the given starting capacity and a load factor ofUtilities.getDefaultLoadFactor().IdentityObjectOrderedMap(ObjectObjectMap<? extends K, ? extends V> map) Creates a new map identical to the specified map.IdentityObjectOrderedMap(ObjectObjectMap<? extends K, ? extends V> map, OrderType ordering) Creates a new map identical to the specified map.IdentityObjectOrderedMap(ObjectObjectOrderedMap<? extends K, ? extends V> map) Creates a new map identical to the specified map.IdentityObjectOrderedMap(ObjectObjectOrderedMap<? extends K, ? extends V> other, int offset, int count) Creates a new set by copyingcountitems from the given ObjectObjectOrderedMap (or subclass, such as IdentityObjectOrderedMap), starting atoffsetin that Map, into this.IdentityObjectOrderedMap(ObjectObjectOrderedMap<? extends K, ? extends V> other, int offset, int count, OrderType ordering) Creates a new set by copyingcountitems from the given ObjectObjectOrderedMap, starting atoffsetin that Map, into this.IdentityObjectOrderedMap(OrderType ordering) Creates a new map with an initial capacity ofUtilities.getDefaultTableCapacity()and a load factor ofUtilities.getDefaultLoadFactor().IdentityObjectOrderedMap(Collection<? extends K> keys, Collection<? extends V> values) Given two side-by-side collections, one of keys, one of values, this constructs a map and inserts each pair of key and value into it.IdentityObjectOrderedMap(Collection<? extends K> keys, Collection<? extends V> values, OrderType ordering) Given two side-by-side collections, one of keys, one of values, this constructs a map and inserts each pair of key and value into it.IdentityObjectOrderedMap(Map<? extends K, ? extends V> map) Creates a new map identical to the specified map.IdentityObjectOrderedMap(Map<? extends K, ? extends V> map, OrderType ordering) Creates a new map identical to the specified map.IdentityObjectOrderedMap(K[] keys, V[] values) Given two side-by-side arrays, one of keys, one of values, this constructs a map and inserts each pair of key and value into it.IdentityObjectOrderedMap(K[] keys, V[] values, OrderType ordering) Given two side-by-side arrays, one of keys, one of values, this constructs a map and inserts each pair of key and value into it. -
Method Summary
Modifier and TypeMethodDescriptionprotected booleanCompares the objects left and right, which are usually keys, for equality, returning true if they are considered equal.intEffectively does nothing here because the hashMultiplier is not used by identity hashing.inthashCode()static <K,V> IdentityObjectOrderedMap<K, V> parse(String str, String entrySeparator, String keyValueSeparator, PartialParser<K> keyParser, PartialParser<V> valueParser) Creates a new map by parsing all ofstrwith the given PartialParser for keys and for values, with entries separated byentrySeparator, such as", "and the keys separated from values bykeyValueSeparator, such as"=".static <K,V> IdentityObjectOrderedMap<K, V> parse(String str, String entrySeparator, String keyValueSeparator, PartialParser<K> keyParser, PartialParser<V> valueParser, boolean brackets) Creates a new map by parsing all ofstr(or ifbracketsis true, all but the first and last chars) with the given PartialParser for keys and for values, with entries separated byentrySeparator, such as", "and the keys separated from values bykeyValueSeparator, such as"=".static <K,V> IdentityObjectOrderedMap<K, V> parse(String str, String entrySeparator, String keyValueSeparator, PartialParser<K> keyParser, PartialParser<V> valueParser, int offset, int length) Creates a new map by parsing the given subrange ofstrwith the given PartialParser for keys and for values, with entries separated byentrySeparator, such as", "and the keys separated from values bykeyValueSeparator, such as"=".protected intReturns an index >= 0 and <=ObjectObjectMap.maskfor the specifieditem.voidsetHashMultiplier(int hashMultiplier) Effectively does nothing here because the hashMultiplier is not used by identity hashing.static <K,V> IdentityObjectOrderedMap<K, V> with()Constructs an empty map given the types as generic type arguments.static <K,V> IdentityObjectOrderedMap<K, V> with(K key0, V value0) Constructs a single-entry map given one key and one value.static <K,V> IdentityObjectOrderedMap<K, V> Constructs a map given alternating keys and values.Methods inherited from class com.github.tommyettinger.ds.ObjectObjectOrderedMap
alter, alterAt, appendTo, clear, clear, ensureCapacity, entrySet, getAt, iterator, keyAt, keySet, order, put, put, putAll, putAll, putAll, putOrDefault, remove, removeAt, removeRange, setAt, sort, sort, sortByValue, truncate, values, with, with, withMethods inherited from class com.github.tommyettinger.ds.ObjectObjectMap
appendTo, combine, combine, containsKey, containsValue, containsValue, equals, equalsIdentity, findKey, findKey, get, getDefaultValue, getLoadFactor, getOrDefault, getTableSize, isEmpty, locateKey, notEmpty, putAll, putAll, putAll, putAll, putAll, putAll, putLegible, putLegible, putLegible, putLegible, putPairs, putResize, replace, resize, setDefaultValue, setLoadFactor, shrink, size, toString, toString, toString, toStringMethods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface com.github.tommyettinger.ds.Arrangeable
rearrange, shuffle, sizeMethods inherited from interface java.lang.Iterable
forEach, spliteratorMethods inherited from interface java.util.Map
compute, computeIfAbsent, computeIfPresent, forEach, merge, putIfAbsent, remove, replace, replaceAllMethods inherited from interface com.github.tommyettinger.ds.Ordered
getOrderType, random, random, reverse, selectRanked, selectRankedIndex, shuffle, swap
-
Constructor Details
-
IdentityObjectOrderedMap
Creates a new map with an initial capacity ofUtilities.getDefaultTableCapacity()and a load factor ofUtilities.getDefaultLoadFactor().- Parameters:
ordering- determines what implementationObjectObjectOrderedMap.order()will use
-
IdentityObjectOrderedMap
Creates a new map with the given starting capacity and a load factor ofUtilities.getDefaultLoadFactor().- Parameters:
initialCapacity- If not a power of two, it is increased to the next nearest power of two.ordering- determines what implementationObjectObjectOrderedMap.order()will use
-
IdentityObjectOrderedMap
Creates a new map with the specified initial capacity and load factor. This map will hold initialCapacity items before growing the backing table.- Parameters:
initialCapacity- If not a power of two, it is increased to the next nearest power of two.loadFactor- what fraction of the capacity can be filled before this has to resize; 0 < loadFactor <= 1ordering- determines what implementationObjectObjectOrderedMap.order()will use
-
IdentityObjectOrderedMap
Creates a new map identical to the specified map.- Parameters:
map- the map to copyordering- determines what implementationObjectObjectOrderedMap.order()will use
-
IdentityObjectOrderedMap
Creates a new map identical to the specified map.- Parameters:
map- the map to copyordering- determines what implementationObjectObjectOrderedMap.order()will use
-
IdentityObjectOrderedMap
public IdentityObjectOrderedMap(ObjectObjectOrderedMap<? extends K, ? extends V> other, int offset, int count, OrderType ordering) Creates a new set by copyingcountitems from the given ObjectObjectOrderedMap, starting atoffsetin that Map, into this.- Parameters:
other- another ObjectObjectOrderedMap of the same typeoffset- the first index in other's ordering to draw an item fromcount- how many items to copy from otherordering- determines what implementationObjectObjectOrderedMap.order()will use
-
IdentityObjectOrderedMap
Given two side-by-side arrays, one of keys, one of values, this constructs a map and inserts each pair of key and value into it. If keys and values have different lengths, this only uses the length of the smaller array.- Parameters:
keys- an array of keysvalues- an array of valuesordering- determines what implementationObjectObjectOrderedMap.order()will use
-
IdentityObjectOrderedMap
public IdentityObjectOrderedMap(Collection<? extends K> keys, Collection<? extends V> values, OrderType ordering) Given two side-by-side collections, one of keys, one of values, this constructs a map and inserts each pair of key and value into it. If keys and values have different lengths, this only uses the length of the smaller collection.- Parameters:
keys- a Collection of keysvalues- a Collection of valuesordering- determines what implementationObjectObjectOrderedMap.order()will use
-
IdentityObjectOrderedMap
public IdentityObjectOrderedMap()Creates a new map with an initial capacity ofUtilities.getDefaultTableCapacity()and a load factor ofUtilities.getDefaultLoadFactor(). -
IdentityObjectOrderedMap
public IdentityObjectOrderedMap(int initialCapacity) Creates a new map with the given starting capacity and a load factor ofUtilities.getDefaultLoadFactor().- Parameters:
initialCapacity- If not a power of two, it is increased to the next nearest power of two.
-
IdentityObjectOrderedMap
public IdentityObjectOrderedMap(int initialCapacity, float loadFactor) Creates a new map with the specified initial capacity and load factor. This map will hold initialCapacity items before growing the backing table.- Parameters:
initialCapacity- If not a power of two, it is increased to the next nearest power of two.loadFactor- what fraction of the capacity can be filled before this has to resize; 0 < loadFactor <= 1
-
IdentityObjectOrderedMap
Creates a new map identical to the specified map.- Parameters:
map- an ObjectObjectOrderedMap to copy, or a subclass such as this one
-
IdentityObjectOrderedMap
Creates a new map identical to the specified map.- Parameters:
map- an ObjectObjectMap to copy, or a subclass
-
IdentityObjectOrderedMap
Creates a new map identical to the specified map.- Parameters:
map- a Map to copy
-
IdentityObjectOrderedMap
Given two side-by-side arrays, one of keys, one of values, this constructs a map and inserts each pair of key and value into it. If keys and values have different lengths, this only uses the length of the smaller array.- Parameters:
keys- an array of keysvalues- an array of values
-
IdentityObjectOrderedMap
Given two side-by-side collections, one of keys, one of values, this constructs a map and inserts each pair of key and value into it. If keys and values have different lengths, this only uses the length of the smaller collection.- Parameters:
keys- a Collection of keysvalues- a Collection of values
-
IdentityObjectOrderedMap
public IdentityObjectOrderedMap(ObjectObjectOrderedMap<? extends K, ? extends V> other, int offset, int count) Creates a new set by copyingcountitems from the given ObjectObjectOrderedMap (or subclass, such as IdentityObjectOrderedMap), starting atoffsetin that Map, into this.- Parameters:
other- an ObjectObjectOrderedMap or other subclass, like IdentityObjectOrderedMap, of the same generic typesoffset- the first index in other's ordering to draw an item fromcount- how many items to copy from other
-
-
Method Details
-
place
Returns an index >= 0 and <=ObjectObjectMap.maskfor the specifieditem.This particular overload relies on the naturally-random nature of
System.identityHashCode(Object), and just masks the identity hash code so only the lower bits are used. This should be fine because the identity hash code defaults to a decent random number generator for its output, so it should collide over all bits very rarely, and collide only over the masked bits somewhat rarely.- Overrides:
placein classObjectObjectMap<K,V> - Parameters:
item- a non-null Object; its identityHashCode is used here- Returns:
- an index between 0 and
ObjectObjectMap.mask(both inclusive)
-
equate
Description copied from class:ObjectObjectMapCompares the objects left and right, which are usually keys, for equality, returning true if they are considered equal. This is used by the rest of this class to determine whether two keys are considered equal. Normally, this returnsleft.equals(right), but subclasses can override it to use reference equality, fuzzy equality, deep array equality, or any other custom definition of equality. Usually,ObjectObjectMap.place(Object)is also overridden if this method is.- Overrides:
equatein classObjectObjectMap<K,V> - Parameters:
left- must be non-null; typically a key being compared, but not necessarilyright- may be null; typically a key being compared, but can often be null for an empty key slot, or some other type- Returns:
- true if left and right are considered equal for the purposes of this class
-
hashCode
public int hashCode() -
getHashMultiplier
public int getHashMultiplier()Effectively does nothing here because the hashMultiplier is not used by identity hashing.- Overrides:
getHashMultiplierin classObjectObjectMap<K,V> - Returns:
- any int; the value isn't used internally, but may be used by subclasses to identify something
-
setHashMultiplier
public void setHashMultiplier(int hashMultiplier) Effectively does nothing here because the hashMultiplier is not used by identity hashing. Subclasses can use this to set some kind of identifier or user data, though. Unlike the superclass implementation, this does not alter the given int to make it negative or odd.- Overrides:
setHashMultiplierin classObjectObjectMap<K,V> - Parameters:
hashMultiplier- any int; will not be used
-
with
Constructs an empty map given the types as generic type arguments. This is usually less useful than just using the constructor, but can be handy in some code-generation scenarios when you don't know how many arguments you will have.- Type Parameters:
K- the type of keysV- the type of values- Returns:
- a new map containing nothing
-
with
Constructs a single-entry map given one key and one value. This is mostly useful as an optimization forwith(Object, Object, Object...)when there's no "rest" of the keys or values.- Type Parameters:
K- the type of key0V- the type of value0- Parameters:
key0- the first and only keyvalue0- the first and only value- Returns:
- a new map containing just the entry mapping key0 to value0
-
with
Constructs a map given alternating keys and values. This can be useful in some code-generation scenarios, or when you want to make a map conveniently by-hand and have it populated at the start. You can also useIdentityObjectOrderedMap(Object[], Object[]), which takes all keys and then all values. This needs all keys to have the same type and all values to have the same type, because it gets those types from the first key parameter and first value parameter. Any keys that don't have K as their type or values that don't have V as their type have that entry skipped.- Type Parameters:
K- the type of keys, inferred from key0V- the type of values, inferred from value0- Parameters:
key0- the first key; will be used to determine the type of all keysvalue0- the first value; will be used to determine the type of all valuesrest- an array or varargs of alternating K, V, K, V... elements- Returns:
- a new map containing the given keys and values
-
parse
public static <K,V> IdentityObjectOrderedMap<K,V> parse(String str, String entrySeparator, String keyValueSeparator, PartialParser<K> keyParser, PartialParser<V> valueParser) Creates a new map by parsing all ofstrwith the given PartialParser for keys and for values, with entries separated byentrySeparator, such as", "and the keys separated from values bykeyValueSeparator, such as"=".
VariousPartialParserinstances are defined as constants, such asPartialParser.DEFAULT_STRING, and others can be created by static methods in PartialParser, such asPartialParser.objectListParser(PartialParser, String, boolean).- Parameters:
str- a String containing parseable textentrySeparator- the String separating every key-value pairkeyValueSeparator- the String separating every key from its corresponding valuekeyParser- a PartialParser that returns aKkey from a section ofstrvalueParser- a PartialParser that returns aVvalue from a section ofstr
-
parse
public static <K,V> IdentityObjectOrderedMap<K,V> parse(String str, String entrySeparator, String keyValueSeparator, PartialParser<K> keyParser, PartialParser<V> valueParser, boolean brackets) Creates a new map by parsing all ofstr(or ifbracketsis true, all but the first and last chars) with the given PartialParser for keys and for values, with entries separated byentrySeparator, such as", "and the keys separated from values bykeyValueSeparator, such as"=".
VariousPartialParserinstances are defined as constants, such asPartialParser.DEFAULT_STRING, and others can be created by static methods in PartialParser, such asPartialParser.objectListParser(PartialParser, String, boolean).- Parameters:
str- a String containing parseable textentrySeparator- the String separating every key-value pairkeyValueSeparator- the String separating every key from its corresponding valuekeyParser- a PartialParser that returns aKkey from a section ofstrvalueParser- a PartialParser that returns aVvalue from a section ofstrbrackets- if true, the first and last chars instrwill be ignored
-
parse
public static <K,V> IdentityObjectOrderedMap<K,V> parse(String str, String entrySeparator, String keyValueSeparator, PartialParser<K> keyParser, PartialParser<V> valueParser, int offset, int length) Creates a new map by parsing the given subrange ofstrwith the given PartialParser for keys and for values, with entries separated byentrySeparator, such as", "and the keys separated from values bykeyValueSeparator, such as"=".
VariousPartialParserinstances are defined as constants, such asPartialParser.DEFAULT_STRING, and others can be created by static methods in PartialParser, such asPartialParser.objectListParser(PartialParser, String, boolean).- Parameters:
str- a String containing parseable textentrySeparator- the String separating every key-value pairkeyValueSeparator- the String separating every key from its corresponding valuekeyParser- a PartialParser that returns aKkey from a section ofstrvalueParser- a PartialParser that returns aVvalue from a section ofstroffset- the first position to read parseable text from instrlength- how many chars to read; -1 is treated as maximum length
-