Class CharObjectHashMap<VType>

    • Field Detail

      • keys

        public char[] keys
        The array holding keys.
      • values

        public Object[] values
        The array holding values.
      • assigned

        protected int assigned
        The number of stored keys (assigned key slots), excluding the special "empty" key, if any (use size() instead).
        See Also:
        size()
      • mask

        protected int mask
        Mask for slot scans in keys.
      • resizeAt

        protected int resizeAt
        Expand (rehash) keys when assigned hits this value.
      • hasEmptyKey

        protected boolean hasEmptyKey
        Special treatment for the "empty slot" key marker.
      • loadFactor

        protected double loadFactor
        The load factor for keys.
      • iterationSeed

        protected int iterationSeed
        Seed used to ensure the hash iteration order is different from an iteration to another.
    • Constructor Detail

      • CharObjectHashMap

        public CharObjectHashMap()
        New instance with sane defaults.
      • CharObjectHashMap

        public CharObjectHashMap​(int expectedElements)
        New instance with sane defaults.
        Parameters:
        expectedElements - The expected number of elements guaranteed not to cause buffer expansion (inclusive).
      • CharObjectHashMap

        public CharObjectHashMap​(int expectedElements,
                                 double loadFactor)
        New instance with the provided defaults.
        Parameters:
        expectedElements - The expected number of elements guaranteed not to cause a rehash (inclusive).
        loadFactor - The load factor for internal buffers. Insane load factors (zero, full capacity) are rejected by verifyLoadFactor(double).
      • CharObjectHashMap

        public CharObjectHashMap​(CharObjectHashMap<VType> map)
        Create a hash map from all key-value pairs of another map.
    • Method Detail

      • put

        public VType put​(char key,
                         VType value)
      • putIfAbsent

        public boolean putIfAbsent​(char key,
                                   VType value)
        Trove-inspired API method. An equivalent of the following code:
         if (!map.containsKey(key)) map.put(value);
         
        Parameters:
        key - The key of the value to check.
        value - The value to put if key does not exist.
        Returns:
        true if key did not exist and value was placed in the map.
      • remove

        public VType remove​(char key)
      • get

        public VType get​(char key)
      • getOrDefault

        public VType getOrDefault​(char key,
                                  VType defaultValue)
      • containsKey

        public boolean containsKey​(char key)
      • indexOf

        public int indexOf​(char key)
      • indexExists

        public boolean indexExists​(int index)
      • indexGet

        public VType indexGet​(int index)
      • indexReplace

        public VType indexReplace​(int index,
                                  VType newValue)
      • indexInsert

        public void indexInsert​(int index,
                                char key,
                                VType value)
      • indexRemove

        public VType indexRemove​(int index)
      • clear

        public void clear()
      • release

        public void release()
      • size

        public int size()
      • isEmpty

        public boolean isEmpty()
      • hashCode

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

        protected boolean equalElements​(CharObjectHashMap<?> other)
        Return true if all keys of some other container exist in this container.
      • ensureCapacity

        public void ensureCapacity​(int expectedElements)
        Ensure this container can hold at least the given number of keys (entries) without resizing its buffers.
        Parameters:
        expectedElements - The total number of keys, inclusive.
      • nextIterationSeed

        protected int nextIterationSeed()
        Provides the next iteration seed used to build the iteration starting slot and offset increment. This method does not need to be synchronized, what matters is that each thread gets a sequence of varying seeds.
      • ramBytesUsed

        public long ramBytesUsed()
        Description copied from interface: Accountable
        Return the memory usage of this object in bytes. Negative values are illegal.
        Specified by:
        ramBytesUsed in interface Accountable
      • toString

        public String toString()
        Convert the contents of this map to a human-friendly string.
        Overrides:
        toString in class Object
      • from

        public static <VType> CharObjectHashMap<VType> from​(char[] keys,
                                                            VType[] values)
        Creates a hash map from two index-aligned arrays of key-value pairs.
      • hashKey

        protected int hashKey​(char key)
        Returns a hash code for the given key.

        The output from this function should evenly distribute keys across the entire integer range.

      • verifyLoadFactor

        protected double verifyLoadFactor​(double loadFactor)
        Validate load factor range and return it. Override and suppress if you need insane load factors.
      • rehash

        protected void rehash​(char[] fromKeys,
                              VType[] fromValues)
        Rehash from old buffers to new buffers.
      • allocateBuffers

        protected void allocateBuffers​(int arraySize)
        Allocate new internal buffers. This method attempts to allocate and assign internal buffers atomically (either allocations succeed or not).
      • allocateThenInsertThenRehash

        protected void allocateThenInsertThenRehash​(int slot,
                                                    char pendingKey,
                                                    VType pendingValue)
        This method is invoked when there is a new key/ value pair to be inserted into the buffers but there is not enough empty slots to do so.

        New buffers are allocated. If this succeeds, we know we can proceed with rehashing so we assign the pending element to the previous buffer (possibly violating the invariant of having at least one empty slot) and rehash all keys, substituting new buffers at the end.

      • shiftConflictingKeys

        protected void shiftConflictingKeys​(int gapSlot)
        Shift all the slot-conflicting keys and values allocated to (and including) slot.