Class IdentityObjectIntMap<K>

  • All Implemented Interfaces:
    Iterable<ObjectIntMap.Entry<K>>

    public class IdentityObjectIntMap<K>
    extends ObjectIntMap<K>
    An unordered map where identity comparison is used for the objects keys and the values are unboxed ints. 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. 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.

    This implementation uses linear probing with the backward shift algorithm for removal. Linear probing continues to work even when all hashCodes collide, just more slowly.

    Author:
    Nathan Sweet, Tommy Ettinger
    • Constructor Detail

      • IdentityObjectIntMap

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

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

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

        public IdentityObjectIntMap​(IdentityObjectIntMap<K> map)
        Creates a new map identical to the specified map.
    • Method Detail

      • place

        protected int place​(K item)
        Description copied from class: ObjectIntMap
        Returns an index >= 0 and <= ObjectIntMap.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;

        Overrides:
        place in class ObjectIntMap<K>
      • get

        public int get​(K key,
                       int defaultValue)
        Description copied from class: ObjectIntMap
        Returns the value for the specified key, or the default value if the key is not in the map.
        Overrides:
        get in class ObjectIntMap<K>