Class ShortIntHashMap

    • Constructor Detail

      • ShortIntHashMap

        public ShortIntHashMap()
      • ShortIntHashMap

        public ShortIntHashMap​(int initialCapacity)
      • ShortIntHashMap

        public ShortIntHashMap​(ShortIntMap map)
    • Method Detail

      • newWithKeysValues

        public static ShortIntHashMap newWithKeysValues​(short key1,
                                                        int value1)
      • newWithKeysValues

        public static ShortIntHashMap newWithKeysValues​(short key1,
                                                        int value1,
                                                        short key2,
                                                        int value2)
      • newWithKeysValues

        public static ShortIntHashMap newWithKeysValues​(short key1,
                                                        int value1,
                                                        short key2,
                                                        int value2,
                                                        short key3,
                                                        int value3)
      • newWithKeysValues

        public static ShortIntHashMap newWithKeysValues​(short key1,
                                                        int value1,
                                                        short key2,
                                                        int value2,
                                                        short key3,
                                                        int value3,
                                                        short key4,
                                                        int value4)
      • smallestPowerOfTwoGreaterThan

        private int smallestPowerOfTwoGreaterThan​(int n)
      • equals

        public boolean equals​(java.lang.Object obj)
        Description copied from interface: ShortIntMap
        Follows the same general contract as Map.equals(Object).
        Specified by:
        equals in interface ShortIntMap
        Overrides:
        equals in class java.lang.Object
      • hashCode

        public int hashCode()
        Description copied from interface: ShortIntMap
        Follows the same general contract as Map.hashCode().
        Specified by:
        hashCode in interface ShortIntMap
        Overrides:
        hashCode in class java.lang.Object
      • toString

        public java.lang.String toString()
        Description copied from interface: PrimitiveIterable
        Returns a string with the elements of this iterable separated by commas with spaces and enclosed in square brackets.
         Assert.assertEquals("[]", IntLists.mutable.empty().toString());
         Assert.assertEquals("[1]", IntLists.mutable.with(1).toString());
         Assert.assertEquals("[1, 2, 3]", IntLists.mutable.with(1, 2, 3).toString());
         
        Specified by:
        toString in interface PrimitiveIterable
        Specified by:
        toString in interface ShortIntMap
        Overrides:
        toString in class AbstractIntIterable
        Returns:
        a string representation of this PrimitiveIterable
        See Also:
        AbstractCollection.toString()
      • put

        public void put​(short key,
                        int value)
        Description copied from interface: MutableShortIntMap
        Associates a value with the specified key. If a value is already associated with the key in this map, it will be replaced with value.
        Specified by:
        put in interface MutableShortIntMap
        Parameters:
        key - the key
        value - the value to associate with value
      • putForRemovedSentinel

        private void putForRemovedSentinel​(int value)
      • putForEmptySentinel

        private void putForEmptySentinel​(int value)
      • putAll

        public void putAll​(ShortIntMap map)
        Description copied from interface: MutableShortIntMap
        Puts all of the key/value mappings from the specified map into this map. If this map already has a value associated with one of the keys in the map, it will be replaced with the value in map.
        Specified by:
        putAll in interface MutableShortIntMap
        Parameters:
        map - the map to copy into this map
      • removeKeyIfAbsent

        public int removeKeyIfAbsent​(short key,
                                     int value)
        Description copied from interface: MutableShortIntMap
        Removes the mapping associated with the key, if one exists, from the map, returning the previously associated value with the key. If no mapping existed for the key, the specified default value is returned.
        Specified by:
        removeKeyIfAbsent in interface MutableShortIntMap
        Parameters:
        key - the key to remove
        value - the default value to return if no mapping for the key exists
        Returns:
        the value previously associated with the key, if one existed, or value if not
      • getIfAbsentPut

        public int getIfAbsentPut​(short key,
                                  int value)
        Description copied from interface: MutableShortIntMap
        Retrieves the value associated with the key if one exists; if it does not, associates a value with the key.
        Specified by:
        getIfAbsentPut in interface MutableShortIntMap
        Parameters:
        key - the key
        value - the value to associate with key if no such mapping exists
        Returns:
        the value associated with key, if one exists, or value if not
      • getAndPut

        public int getAndPut​(short key,
                             int putValue,
                             int defaultValue)
        Description copied from interface: MutableShortIntMap
        Retrieves the value associated with the key if one exists; associates a putValue with the key.
        Specified by:
        getAndPut in interface MutableShortIntMap
        Parameters:
        key - the key
        putValue - the value to associate with key if no such mapping exists
        defaultValue - the value to return if no mapping associated with key exists
        Returns:
        the value associated with key, if one exists, or defaultValue if not
      • getIfAbsentPut

        public int getIfAbsentPut​(short key,
                                  IntFunction0 function)
        Description copied from interface: MutableShortIntMap
        Retrieves the value associated with the key if one exists; if it does not, invokes the supplier and associates the result with the key.
        Specified by:
        getIfAbsentPut in interface MutableShortIntMap
        Parameters:
        key - the key
        function - the supplier that provides the value if no mapping exists for key
        Returns:
        the value associated with the key, if one exists, or the result of invoking function if not
      • getIfAbsentPutWith

        public <P> int getIfAbsentPutWith​(short key,
                                          IntFunction<? super P> function,
                                          P parameter)
        Description copied from interface: MutableShortIntMap
        Retrieves the value associated with the key if one exists; if it does not, invokes the value function with the parameter and associates the result with the key.
        Specified by:
        getIfAbsentPutWith in interface MutableShortIntMap
        Type Parameters:
        P - the type of the value function's parameter
        Parameters:
        key - the key
        function - the function that provides the value if no mapping exists. The specified parameter will be passed as the argument to the function.
        parameter - the parameter to provide to function if no value exists for key
        Returns:
        the value associated with the key, if one exists, or the result of invoking function with parameter if not
      • getIfAbsentPutWithKey

        public int getIfAbsentPutWithKey​(short key,
                                         ShortToIntFunction function)
        Description copied from interface: MutableShortIntMap
        Retrieves the value associated with the key if one exists; if it does not, associates the result of invoking the value function with the key.
        Specified by:
        getIfAbsentPutWithKey in interface MutableShortIntMap
        Parameters:
        key - the key
        function - the function that provides the value if no mapping exists. The key will be passed as the argument to the function.
        Returns:
        the value associated with the key, if one exists, or the result of invoking function with key if not
      • addToValue

        public int addToValue​(short key,
                              int toBeAdded)
        Description copied from interface: MutableShortIntMap
        Increments and updates the value associated with the key, if a value exists, or sets the value to be the specified value if one does not.
        Specified by:
        addToValue in interface MutableShortIntMap
        Parameters:
        key - the key
        toBeAdded - the amount to increment the existing value, if one exists, or to use as the initial value if one does not
        Returns:
        the value after incrementing toBeAdded to the existing value associated with key or toBeAdded if one does not
      • addKeyValueAtIndex

        private void addKeyValueAtIndex​(short key,
                                        int value,
                                        int index)
      • removeKeyAtIndex

        private void removeKeyAtIndex​(int index)
      • copyKeys

        private void copyKeys()
      • updateValue

        public int updateValue​(short key,
                               int initialValueIfAbsent,
                               IntToIntFunction function)
        Description copied from interface: MutableShortIntMap
        Updates or sets the value associated with the key by applying the function to the existing value, if one exists, or to the specified initial value if one does not.
        Specified by:
        updateValue in interface MutableShortIntMap
        Parameters:
        key - the key
        initialValueIfAbsent - the initial value to supply to the function if no mapping exists for the key
        function - the function that returns the updated value based on the current value or the initial value, if no value exists
        Returns:
        the new value associated with the key, either as a result of applying function to the value already associated with the key or as a result of applying it to initialValueIfAbsent and associating the result with key
      • withKeysValues

        public ShortIntHashMap withKeysValues​(short key1,
                                              int value1,
                                              short key2,
                                              int value2)
      • withKeysValues

        public ShortIntHashMap withKeysValues​(short key1,
                                              int value1,
                                              short key2,
                                              int value2,
                                              short key3,
                                              int value3)
      • withKeysValues

        public ShortIntHashMap withKeysValues​(short key1,
                                              int value1,
                                              short key2,
                                              int value2,
                                              short key3,
                                              int value3,
                                              short key4,
                                              int value4)
      • asUnmodifiable

        public MutableShortIntMap asUnmodifiable()
        Description copied from interface: MutableShortIntMap
        Returns an unmodifiable view of this map, delegating all read-only operations to this map and throwing an UnsupportedOperationException for all mutating operations. This avoids the overhead of copying the map when calling ShortIntMap.toImmutable() while still providing immutability.
        Specified by:
        asUnmodifiable in interface MutableShortIntMap
        Returns:
        an unmodifiable view of this map
      • asSynchronized

        public MutableShortIntMap asSynchronized()
        Description copied from interface: MutableShortIntMap
        Returns a synchronized view of this map, delegating all operations to this map but ensuring only one caller has access to the map at a time.
        Specified by:
        asSynchronized in interface MutableShortIntMap
        Returns:
        a synchronized view of this map
      • toImmutable

        public ImmutableShortIntMap toImmutable()
        Description copied from interface: ShortIntMap
        Returns a copy of this map that is immutable (if this map is mutable) or itself if it is already immutable.
        Specified by:
        toImmutable in interface ShortIntMap
        Returns:
        an immutable map that is equivalent to this one
      • get

        public int get​(short key)
        Description copied from interface: ShortIntMap
        Retrieves the value associated with the key. If no mapping exists for the key, the default value (usually 0) is returned.
        Specified by:
        get in interface ShortIntMap
        Parameters:
        key - the key
        Returns:
        the value associated with the key, or the default value if no such mapping exists
      • getIfAbsent

        public int getIfAbsent​(short key,
                               int ifAbsent)
        Description copied from interface: ShortIntMap
        Retrieves the value associated with the key, returning the specified default value if no such mapping exists.
        Specified by:
        getIfAbsent in interface ShortIntMap
        Parameters:
        key - the key
        ifAbsent - the default value to return if no mapping exists for key
        Returns:
        the value associated with the key, or ifAbsent if no such mapping exists.
      • getForSentinel

        private int getForSentinel​(short key,
                                   int ifAbsent)
      • slowGetIfAbsent

        private int slowGetIfAbsent​(short key,
                                    int ifAbsent)
      • fastGetIfAbsent

        private int fastGetIfAbsent​(short key,
                                    int ifAbsent)
      • slowGetIfAbsentTwo

        private int slowGetIfAbsentTwo​(short key,
                                       int ifAbsent)
      • getOrThrow

        public int getOrThrow​(short key)
        Description copied from interface: ShortIntMap
        Retrieves the value associated with the key, throwing an IllegalStateException if no such mapping exists.
        Specified by:
        getOrThrow in interface ShortIntMap
        Parameters:
        key - the key
        Returns:
        the value associated with the key
      • containsKey

        public boolean containsKey​(short key)
        Description copied from interface: ShortIntMap
        Returns whether or not the key is present in the map.
        Specified by:
        containsKey in interface ShortIntMap
        Specified by:
        containsKey in interface ShortKeysMap
        Parameters:
        key - the key
        Returns:
        if a mapping exists in this map for the key
      • forEachKey

        public void forEachKey​(ShortProcedure procedure)
        Description copied from interface: ShortIntMap
        Iterates through each key in the map, invoking the procedure for each.
        Specified by:
        forEachKey in interface ShortIntMap
        Specified by:
        forEachKey in interface ShortKeysMap
        Parameters:
        procedure - the procedure to invoke for each key
      • forEachKeyValue

        public void forEachKeyValue​(ShortIntProcedure procedure)
        Description copied from interface: ShortIntMap
        Iterates through each key/value pair in the map, invoking the procedure for each.
        Specified by:
        forEachKeyValue in interface ShortIntMap
        Parameters:
        procedure - the procedure to invoke for each key/value pair
      • keysView

        public LazyShortIterable keysView()
        Description copied from interface: ShortIntMap
        Returns a view of the keys in this map. This iterable is backed by the map, so any modifications to the underlying map will be reflected in the keys returned by the iterable.
        Specified by:
        keysView in interface ShortIntMap
        Returns:
        a view of the keys in this map
      • keyValuesView

        public RichIterable<ShortIntPair> keyValuesView()
        Description copied from interface: ShortIntMap
        Returns a view of the key/value pairs in this map. This iterable is backed by the map, so any modifications to the underlying map will be reflected in the pairs returned by the iterable.
        Specified by:
        keyValuesView in interface ShortIntMap
        Returns:
        a view of the keys in this map
      • select

        public ShortIntHashMap select​(ShortIntPredicate predicate)
        Description copied from interface: ShortIntMap
        Return a copy of this map containing only the key/value pairs that match the predicate.
        Specified by:
        select in interface MutableShortIntMap
        Specified by:
        select in interface ShortIntMap
        Parameters:
        predicate - the predicate to determine which key/value pairs in this map should be included in the returned map
        Returns:
        a copy of this map with the matching key/value pairs
      • reject

        public ShortIntHashMap reject​(ShortIntPredicate predicate)
        Description copied from interface: ShortIntMap
        Return a copy of this map containing only the key/value pairs that do not match the predicate.
        Specified by:
        reject in interface MutableShortIntMap
        Specified by:
        reject in interface ShortIntMap
        Parameters:
        predicate - the predicate to determine which key/value pairs in this map should be excluded from the returned map
        Returns:
        a copy of this map without the matching key/value pairs
      • writeExternal

        public void writeExternal​(java.io.ObjectOutput out)
                           throws java.io.IOException
        Specified by:
        writeExternal in interface java.io.Externalizable
        Throws:
        java.io.IOException
      • readExternal

        public void readExternal​(java.io.ObjectInput in)
                          throws java.io.IOException,
                                 java.lang.ClassNotFoundException
        Specified by:
        readExternal in interface java.io.Externalizable
        Throws:
        java.io.IOException
        java.lang.ClassNotFoundException
      • compact

        public void compact()
        Rehashes every element in the set into a new backing table of the smallest possible size and eliminating removed sentinels.
      • rehashAndGrow

        private void rehashAndGrow()
      • rehash

        private void rehash​(int newCapacity)
      • probe

        int probe​(short element)
      • probeTwo

        int probeTwo​(short element,
                     int removedIndex)
      • probeThree

        int probeThree​(short element,
                       int removedIndex)
      • spreadAndMask

        int spreadAndMask​(short element)
      • spreadTwoAndMask

        int spreadTwoAndMask​(short element)
      • mask

        private int mask​(int spread)
      • allocateTable

        protected void allocateTable​(int sizeToAllocate)
      • isEmptyKey

        private static boolean isEmptyKey​(short key)
      • isRemovedKey

        private static boolean isRemovedKey​(short key)
      • isNonSentinel

        private static boolean isNonSentinel​(short key)
      • maxOccupiedWithData

        private int maxOccupiedWithData()
      • keySet

        public MutableShortSet keySet()
        Description copied from interface: ShortIntMap
        Returns a set containing all the keys in this map. The set is backed by the map, so any modifications to the returned set will affect this map.
        Specified by:
        keySet in interface ShortIntMap
        Returns:
        a mutable set containing the keys in this map
      • values

        public MutableIntCollection values()
        Description copied from interface: IntValuesMap
        Returns the values in this map as a separate collection. The returned collection is backed by the map, so any changes made to the returned collection will affect the state of this map.
        Specified by:
        values in interface IntValuesMap
        Returns:
        the values as a collection backed by this map