Class ObjectMap<K,​V>


  • public class ObjectMap<K,​V>
    extends java.lang.Object
    An unordered map. This implementation is a cuckoo hash map using 3 hashes (if table size is less than 2^16) or 4 hashes (if table size is greater than or equal to 2^16), random walking, and a small stash for problematic keys Null keys are not allowed. Null values are allowed. No allocation is done except when growing the table size.

    This map performs very fast get, containsKey, and remove (typically O(1), worst case O(log(n))). Put may be a bit slower, depending on hash collisions. Load factors greater than 0.91 greatly increase the chances the map will have to rehash to the next higher POT size.
    • Constructor Summary

      Constructors 
      Constructor Description
      ObjectMap()
      Creates a new map with an initial capacity of 32 and a load factor of 0.8.
      ObjectMap​(int initialCapacity)
      Creates a new map with a load factor of 0.8.
      ObjectMap​(int initialCapacity, float loadFactor)
      Creates a new map with the specified initial capacity and load factor.
      ObjectMap​(ObjectMap<? extends K,​? extends V> map)
      Creates a new map identical to the specified map.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void clear()  
      void clear​(int maximumCapacity)
      Clears the map and reduces the size of the backing arrays to be the specified capacity if they are larger.
      boolean containsKey​(K key)  
      private boolean containsKeyStash​(K key)  
      boolean containsValue​(java.lang.Object value, boolean identity)
      Returns true if the specified value is in the map.
      void ensureCapacity​(int additionalCapacity)
      Increases the size of the backing array to acommodate the specified number of additional items.
      ObjectMap.Entries<K,​V> entries()
      Returns an iterator for the entries in the map.
      K findKey​(java.lang.Object value, boolean identity)
      Returns the key for the specified value, or null if it is not in the map.
      V get​(K key)  
      V get​(K key, V defaultValue)
      Returns the value for the specified key, or the default value if the key is not in the map.
      private V getStash​(K key)  
      private V getStash​(K key, V defaultValue)  
      private int hash2​(int h)  
      private int hash3​(int h)  
      private int hash4​(int h)  
      ObjectMap.Keys<K> keys()
      Returns an iterator for the keys in the map.
      static int nextPowerOfTwo​(int value)  
      private void push​(K insertKey, V insertValue, int index1, K key1, int index2, K key2, int index3, K key3, int index4, K key4)  
      V put​(K key, V value)
      Returns the old value associated with the specified key, or null.
      private V put_internal​(K key, V value)  
      void putAll​(ObjectMap<K,​V> map)  
      private void putResize​(K key, V value)
      Skips checks for existing keys.
      private void putStash​(K key, V value)  
      V remove​(K key)  
      (package private) V removeStash​(K key)  
      (package private) void removeStashIndex​(int index)  
      private void resize​(int newSize)  
      void shrink​(int maximumCapacity)
      Reduces the size of the backing arrays to be the specified capacity or less.
      java.lang.String toString()  
      ObjectMap.Values<V> values()
      Returns an iterator for the values in the map.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Field Detail

      • random

        static java.util.Random random
      • size

        public int size
      • keyTable

        K[] keyTable
      • valueTable

        V[] valueTable
      • capacity

        int capacity
      • stashSize

        int stashSize
      • loadFactor

        private float loadFactor
      • hashShift

        private int hashShift
      • mask

        private int mask
      • threshold

        private int threshold
      • stashCapacity

        private int stashCapacity
      • pushIterations

        private int pushIterations
      • bigTable

        private boolean bigTable
    • Constructor Detail

      • ObjectMap

        public ObjectMap()
        Creates a new map with an initial capacity of 32 and a load factor of 0.8. This map will hold 25 items before growing the backing table.
      • ObjectMap

        public ObjectMap​(int initialCapacity)
        Creates a new map with a load factor of 0.8. This map will hold initialCapacity * 0.8 items before growing the backing table.
      • ObjectMap

        public ObjectMap​(int initialCapacity,
                         float loadFactor)
        Creates a new map with the specified initial capacity and load factor. This map will hold initialCapacity * loadFactor items before growing the backing table.
      • ObjectMap

        public ObjectMap​(ObjectMap<? extends K,​? extends V> map)
        Creates a new map identical to the specified map.
    • Method Detail

      • put

        public V put​(K key,
                     V value)
        Returns the old value associated with the specified key, or null.
      • put_internal

        private V put_internal​(K key,
                               V value)
      • putAll

        public void putAll​(ObjectMap<K,​V> map)
      • putResize

        private void putResize​(K key,
                               V value)
        Skips checks for existing keys.
      • push

        private void push​(K insertKey,
                          V insertValue,
                          int index1,
                          K key1,
                          int index2,
                          K key2,
                          int index3,
                          K key3,
                          int index4,
                          K key4)
      • putStash

        private void putStash​(K key,
                              V value)
      • get

        public V get​(K key)
      • getStash

        private V getStash​(K key)
      • get

        public V get​(K key,
                     V defaultValue)
        Returns the value for the specified key, or the default value if the key is not in the map.
      • getStash

        private V getStash​(K key,
                           V defaultValue)
      • remove

        public V remove​(K key)
      • removeStash

        V removeStash​(K key)
      • removeStashIndex

        void removeStashIndex​(int index)
      • shrink

        public void shrink​(int maximumCapacity)
        Reduces the size of the backing arrays to be the specified capacity or less. If the capacity is already less, nothing is done. If the map contains more items than the specified capacity, the next highest power of two capacity is used instead.
      • clear

        public void clear​(int maximumCapacity)
        Clears the map and reduces the size of the backing arrays to be the specified capacity if they are larger.
      • clear

        public void clear()
      • containsValue

        public boolean containsValue​(java.lang.Object value,
                                     boolean identity)
        Returns true if the specified value is in the map. Note this traverses the entire map and compares every value, which may be an expensive operation.
        Parameters:
        identity - If true, uses == to compare the specified value with values in the map. If false, uses Object.equals(Object).
      • containsKey

        public boolean containsKey​(K key)
      • containsKeyStash

        private boolean containsKeyStash​(K key)
      • findKey

        public K findKey​(java.lang.Object value,
                         boolean identity)
        Returns the key for the specified value, or null if it is not in the map. Note this traverses the entire map and compares every value, which may be an expensive operation.
        Parameters:
        identity - If true, uses == to compare the specified value with values in the map. If false, uses Object.equals(Object).
      • ensureCapacity

        public void ensureCapacity​(int additionalCapacity)
        Increases the size of the backing array to acommodate the specified number of additional items. Useful before adding many items to avoid multiple backing array resizes.
      • resize

        private void resize​(int newSize)
      • hash2

        private int hash2​(int h)
      • hash3

        private int hash3​(int h)
      • hash4

        private int hash4​(int h)
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • entries

        public ObjectMap.Entries<K,​V> entries()
        Returns an iterator for the entries in the map. Remove is supported.
      • values

        public ObjectMap.Values<V> values()
        Returns an iterator for the values in the map. Remove is supported.
      • keys

        public ObjectMap.Keys<K> keys()
        Returns an iterator for the keys in the map. Remove is supported.
      • nextPowerOfTwo

        public static int nextPowerOfTwo​(int value)