Package gaiasky.util

Class ObjectDoubleMap<K>

java.lang.Object
gaiasky.util.ObjectDoubleMap<K>
All Implemented Interfaces:
Iterable<ObjectDoubleMap.Entry<K>>

public class ObjectDoubleMap<K> extends Object implements Iterable<ObjectDoubleMap.Entry<K>>
An unordered map where the keys are objects and the values are unboxed doubles. Null keys are not allowed. No allocation is done except when growing the table size.

This class performs fast contains and remove (typically O(1), worst case O(n) but that is rare in practice). 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 class 
     
    static class 
     
    static class 
     
    static class 
     
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected int
    A bitmask used to confine hashcodes to the size of the table.
    protected int
    Used by place(Object) to bit shift the upper bits of a long into a usable range (>= 0 and <= mask).
    int
     
  • Constructor Summary

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

    Modifier and Type
    Method
    Description
    void
     
    void
    clear​(int maximumCapacity)
    Clears the map and reduces the size of the backing arrays to be the specified capacity / loadFactor, if they are larger.
    boolean
    containsKey​(K key)
     
    boolean
    containsValue​(double value)
    Returns true if the specified value is in the map.
    boolean
    containsValue​(double value, double epsilon)
    Returns true if the specified value is in the map.
    void
    ensureCapacity​(int additionalCapacity)
    Increases the size of the backing array to accommodate the specified number of additional items / loadFactor.
    Returns an iterator for the entries in the map.
    boolean
    equals​(Object obj)
     
    findKey​(double value)
    Returns the key for the specified value, or null if it is not in the map.
    findKey​(double value, double epsilon)
    Returns the key for the specified value, or null if it is not in the map.
    double
    get​(K key, double defaultValue)
    Returns the value for the specified key, or the default value if the key is not in the map.
    double
    getAndIncrement​(K key, double defaultValue, double increment)
    Returns the key's current value and increments the stored value.
    int
     
    boolean
    Returns true if the map is empty.
     
    Returns an iterator for the keys in the map.
    boolean
    Returns true if the map has one or more items.
    protected int
    place​(K item)
    Returns an index >= 0 and <= mask for the specified item.
    void
    put​(K key, double value)
     
    double
    put​(K key, double value, double defaultValue)
    Returns the old value associated with the specified key, or the specified default value.
    void
    putAll​(ObjectDoubleMap<? extends K> map)
     
    double
    remove​(K key, double defaultValue)
    Returns the value for the removed key, or the default value if the key is not in the map.
    void
    shrink​(int maximumCapacity)
    Reduces the size of the backing arrays to be the specified capacity / loadFactor, or less.
     
    toString​(String separator)
     
    Returns an iterator for the values in the map.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait

    Methods inherited from interface java.lang.Iterable

    forEach, spliterator
  • Field Details

    • size

      public int size
    • shift

      protected int shift
      Used by place(Object) to bit shift the upper bits of a long into a usable range (>= 0 and <= mask). The shift can be negative, which is convenient to match the number of bits in mask: if mask is a 7-bit number, a shift of -7 shifts the upper 7 bits into the lowest 7 positions. This class sets the shift > 32 and < 64, which if used with an int will still move the upper bits of an int to the lower bits due to Java's implicit modulus on shifts.

      mask can also be used to mask the low bits of a number, which may be faster for some hashcodes, if place(Object) is overridden.

    • mask

      protected int mask
      A bitmask used to confine hashcodes to the size of the table. Must be all 1 bits in its low positions, ie a power of two minus 1. If place(Object) is overriden, this can be used instead of shift to isolate usable bits of a hash.
  • Constructor Details

    • ObjectDoubleMap

      public ObjectDoubleMap()
      Creates a new map with an initial capacity of 51 and a load factor of 0.8.
    • ObjectDoubleMap

      public ObjectDoubleMap(int initialCapacity)
      Creates a new map with a load factor of 0.8.
      Parameters:
      initialCapacity - If not a power of two, it is increased to the next nearest power of two.
    • ObjectDoubleMap

      public ObjectDoubleMap(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.
    • ObjectDoubleMap

      public ObjectDoubleMap(ObjectDoubleMap<? extends K> map)
      Creates a new map identical to the specified map.
  • Method Details

    • place

      protected int place(K item)
      Returns an index >= 0 and <= mask for the specified item.

      The default implementation uses Fibonacci hashing on the item's Object.hashCode(): the hashcode is multiplied by a long constant (2 to the 64th, divided by the golden ratio) then the uppermost bits are shifted into the lowest positions to obtain an index in the desired range. Multiplication by a long may be slower than int (eg on GWT) but greatly improves rehashing, allowing even very poor hashcodes, such as those that only differ in their upper bits, to be used without high collision rates. Fibonacci hashing has increased collision rates when all or most hashcodes are multiples of larger Fibonacci numbers (see Malte Skarupke's blog post).

      This method can be overriden to customizing hashing. This may be useful eg in the unlikely event that most hashcodes are Fibonacci numbers, if keys provide poor or incorrect hashcodes, or to simplify hashing if keys provide high quality hashcodes and don't need Fibonacci hashing: return item.hashCode() & mask;

    • put

      public void put(K key, double value)
    • put

      public double put(K key, double value, double defaultValue)
      Returns the old value associated with the specified key, or the specified default value.
      Parameters:
      defaultValue - Double.NaN can be used for a value unlikely to be in the map.
    • putAll

      public void putAll(ObjectDoubleMap<? extends K> map)
    • get

      public double get(K key, double defaultValue)
      Returns the value for the specified key, or the default value if the key is not in the map.
      Parameters:
      defaultValue - Double.NaN can be used for a value unlikely to be in the map.
    • getAndIncrement

      public double getAndIncrement(K key, double defaultValue, double increment)
      Returns the key's current value and increments the stored value. If the key is not in the map, defaultValue + increment is put into the map and defaultValue is returned.
    • remove

      public double remove(K key, double defaultValue)
      Returns the value for the removed key, or the default value if the key is not in the map.
      Parameters:
      defaultValue - Double.NaN can be used for a value unlikely to be in the map.
    • notEmpty

      public boolean notEmpty()
      Returns true if the map has one or more items.
    • isEmpty

      public boolean isEmpty()
      Returns true if the map is empty.
    • shrink

      public void shrink(int maximumCapacity)
      Reduces the size of the backing arrays to be the specified capacity / loadFactor, 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 / loadFactor, if they are larger.
    • clear

      public void clear()
    • containsValue

      public boolean containsValue(double value)
      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.
    • containsValue

      public boolean containsValue(double value, double epsilon)
      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.
    • containsKey

      public boolean containsKey(K key)
    • findKey

      @Null public K findKey(double value)
      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.
    • findKey

      @Null public K findKey(double value, double epsilon)
      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.
    • ensureCapacity

      public void ensureCapacity(int additionalCapacity)
      Increases the size of the backing array to accommodate the specified number of additional items / loadFactor. Useful before adding many items to avoid multiple backing array resizes.
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • toString

      public String toString(String separator)
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • iterator

      public ObjectDoubleMap.Entries<K> iterator()
      Specified by:
      iterator in interface Iterable<K>
    • entries

      public ObjectDoubleMap.Entries<K> entries()
      Returns an iterator for the entries in the map. Remove is supported.

      If Collections.allocateIterators is false, the same iterator instance is returned each time this method is called. Use the ObjectDoubleMap.Entries constructor for nested or multithreaded iteration.

    • values

      public ObjectDoubleMap.Values values()
      Returns an iterator for the values in the map. Remove is supported.

      If Collections.allocateIterators is false, the same iterator instance is returned each time this method is called. Use the ObjectDoubleMap.Values constructor for nested or multithreaded iteration.

    • keys

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

      If Collections.allocateIterators is false, the same iterator instance is returned each time this method is called. Use the ObjectDoubleMap.Keys constructor for nested or multithreaded iteration.