Class OrderedMap<K,V>
- java.lang.Object
-
- com.badlogic.gdx.utils.ObjectMap<K,V>
-
- com.badlogic.gdx.utils.OrderedMap<K,V>
-
- All Implemented Interfaces:
java.lang.Iterable<ObjectMap.Entry<K,V>>
public class OrderedMap<K,V> extends ObjectMap<K,V>
AnObjectMapthat also stores keys in anArrayusing the insertion order. Null keys are not allowed. No allocation is done except when growing the table size.Iteration over the
entries(),keys(), andvalues()is ordered and faster than an unordered map. Keys can also be accessed and the order changed usingorderedKeys(). There is some additional overhead for put and remove. When used for faster iteration versus ObjectMap and the order does not actually matter, copying during remove can be greatly reduced by settingArray.orderedto false fororderedKeys().This class performs fast contains (typically O(1), worst case O(n) but that is rare in practice). Remove is somewhat slower due to
orderedKeys(). Add may be slightly slower, depending on hash collisions. Hashcodes are rehashed to reduce collisions and the need to resize. Load factors greater than 0.91 greatly increase the chances to resize to the next higher POT size.Unordered sets and maps are not designed to provide especially fast iteration. Iteration is faster with OrderedSet and OrderedMap.
This implementation uses linear probing with the backward shift algorithm for removal. Hashcodes are rehashed using Fibonacci hashing, instead of the more common power-of-two mask, to better distribute poor hashCodes (see Malte Skarupke's blog post). Linear probing continues to work even when all hashCodes collide, just more slowly.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classOrderedMap.OrderedMapEntries<K,V>static classOrderedMap.OrderedMapKeys<K>static classOrderedMap.OrderedMapValues<V>-
Nested classes/interfaces inherited from class com.badlogic.gdx.utils.ObjectMap
ObjectMap.Entries<K,V>, ObjectMap.Entry<K,V>, ObjectMap.Keys<K>, ObjectMap.Values<V>
-
-
Constructor Summary
Constructors Constructor Description OrderedMap()Creates a new map with an initial capacity of 51 and a load factor of 0.8.OrderedMap(int initialCapacity)Creates a new map with a load factor of 0.8.OrderedMap(int initialCapacity, float loadFactor)Creates a new map with the specified initial capacity and load factor.OrderedMap(OrderedMap<? extends K,? extends V> map)Creates a new map containing the items in the specified map.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanalter(K before, K after)Changes the keybeforetoafterwithout changing its position in the order or its value.booleanalterIndex(int index, K after)Changes the key at the givenindexin the order toafter, without changing the ordering of other entries or any values.voidclear()voidclear(int maximumCapacity)Clears the map and reduces the size of the backing arrays to be the specified capacity / loadFactor, if they are larger.ObjectMap.Entries<K,V>entries()Returns an iterator for the entries in the map.ObjectMap.Entries<K,V>iterator()ObjectMap.Keys<K>keys()Returns an iterator for the keys in the map.Array<K>orderedKeys()Vput(K key, V value)Returns the old value associated with the specified key, or null.<T extends K>
voidputAll(OrderedMap<T,? extends V> map)Vremove(K key)Returns the value for the removed key, or null if the key is not in the map.VremoveIndex(int index)protected java.lang.StringtoString(java.lang.String separator, boolean braces)ObjectMap.Values<V>values()Returns an iterator for the values in the map.-
Methods inherited from class com.badlogic.gdx.utils.ObjectMap
containsKey, containsValue, ensureCapacity, equals, equalsIdentity, findKey, get, get, hashCode, isEmpty, notEmpty, place, putAll, shrink, toString, toString
-
-
-
-
Constructor Detail
-
OrderedMap
public OrderedMap()
Creates a new map with an initial capacity of 51 and a load factor of 0.8.
-
OrderedMap
public OrderedMap(int initialCapacity)
Creates a new map with a load factor of 0.8.- Parameters:
initialCapacity- The backing array size is initialCapacity / loadFactor, increased to the next power of two.
-
OrderedMap
public OrderedMap(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- The backing array size is initialCapacity / loadFactor, increased to the next power of two.
-
OrderedMap
public OrderedMap(OrderedMap<? extends K,? extends V> map)
Creates a new map containing the items in the specified map.
-
-
Method Detail
-
put
public V put(K key, V value)
Description copied from class:ObjectMapReturns the old value associated with the specified key, or null.
-
putAll
public <T extends K> void putAll(OrderedMap<T,? extends V> map)
-
remove
public V remove(K key)
Description copied from class:ObjectMapReturns the value for the removed key, or null if the key is not in the map.
-
removeIndex
public V removeIndex(int index)
-
alter
public boolean alter(K before, K after)
Changes the keybeforetoafterwithout changing its position in the order or its value. Returns true ifafterhas been added to the OrderedMap andbeforehas been removed; returns false ifafteris already present orbeforeis not present. If you are iterating over an OrderedMap and have an index, you should preferalterIndex(int, Object), which doesn't need to search for an index like this does and so can be faster.- Parameters:
before- a key that must be present for this to succeedafter- a key that must not be in this map for this to succeed- Returns:
- true if
beforewas removed andafterwas added, false otherwise
-
alterIndex
public boolean alterIndex(int index, K after)Changes the key at the givenindexin the order toafter, without changing the ordering of other entries or any values. Ifafteris already present, this returns false; it will also return false ifindexis invalid for the size of this map. Otherwise, it returns true. Unlikealter(Object, Object), this operates in constant time.- Parameters:
index- the index in the order of the key to change; must be non-negative and less thanObjectMap.sizeafter- the key that will replace the contents atindex; this key must not be present for this to succeed- Returns:
- true if
aftersuccessfully replaced the key atindex, false otherwise
-
clear
public void clear(int maximumCapacity)
Description copied from class:ObjectMapClears the map and reduces the size of the backing arrays to be the specified capacity / loadFactor, if they are larger.
-
iterator
public ObjectMap.Entries<K,V> iterator()
-
entries
public ObjectMap.Entries<K,V> entries()
Returns an iterator for the entries in the map. Remove is supported.If
Collections.allocateIteratorsis false, the same iterator instance is returned each time this method is called. Use theOrderedMap.OrderedMapEntriesconstructor for nested or multithreaded iteration.
-
values
public ObjectMap.Values<V> values()
Returns an iterator for the values in the map. Remove is supported.If
Collections.allocateIteratorsis false, the same iterator instance is returned each time this method is called. Use theOrderedMap.OrderedMapValuesconstructor for nested or multithreaded iteration.
-
keys
public ObjectMap.Keys<K> keys()
Returns an iterator for the keys in the map. Remove is supported.If
Collections.allocateIteratorsis false, the same iterator instance is returned each time this method is called. Use theOrderedMap.OrderedMapKeysconstructor for nested or multithreaded iteration.
-
-