Class IntHashMap<VType>

java.lang.Object
org.kingdoms.utils.internal.integer.IntHashMap<VType>
All Implemented Interfaces:
Iterable<VType>

public class IntHashMap<VType> extends Object implements Iterable<VType>
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected int
    The number of stored keys (assigned key slots), excluding the special "empty" key, if any (use size() instead).
    protected boolean
    7 Special treatment for the "empty slot" key marker.
    protected int
    Seed used to ensure the hash iteration order is different from an iteration to another.
    int[]
    The array holding keys.
    protected double
    The load factor for keys.
    protected int
    Mask for slot scans in keys.
    protected int
    Expand (rehash) keys when assigned hits this value.
    The array holding values.
  • Constructor Summary

    Constructors
    Constructor
    Description
    New instance with sane defaults.
    IntHashMap(int expectedElements)
    New instance with sane defaults.
    IntHashMap(int expectedElements, double loadFactor)
    New instance with the provided defaults.
  • Method Summary

    Modifier and Type
    Method
    Description
    protected void
    allocateBuffers(int arraySize)
    Allocate new internal buffers.
    protected void
    allocateThenInsertThenRehash(int slot, int 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.
    void
     
    boolean
    containsKey(int key)
     
    void
    ensureCapacity(int expectedElements)
    Ensure this container can hold at least the given number of keys (entries) without resizing its buffers.
    boolean
     
    get(int key)
     
    getOrDefault(int key, VType defaultValue)
     
    int
     
    protected int
    hashKey(int key)
    Returns a hash code for the given key.
    boolean
     
    @NonNull Iterator<VType>
     
    put(int key, VType value)
     
    protected void
    rehash(int[] fromKeys, VType[] fromValues)
    Rehash from old buffers to new buffers.
    void
     
    remove(int key)
     
    protected void
    shiftConflictingKeys(int gapSlot)
    Shift all the slot-conflicting keys and values allocated to (and including) slot.
    int
     
    protected double
    verifyLoadFactor(double loadFactor)
    Validate load factor range and return it.

    Methods inherited from class java.lang.Object

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

    Methods inherited from interface java.lang.Iterable

    forEach, spliterator
  • Field Details

    • keys

      public int[] 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:
    • 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
      7 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 Details

    • IntHashMap

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

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

      public IntHashMap(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).
  • Method Details

    • put

      public VType put(int key, VType value)
    • remove

      public VType remove(int key)
    • get

      public VType get(int key)
    • getOrDefault

      public VType getOrDefault(int key, VType defaultValue)
    • containsKey

      public boolean containsKey(int key)
    • 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
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • 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.
    • hashKey

      protected int hashKey(int 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(int[] 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, int 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.
    • iterator

      public @NonNull Iterator<VType> iterator()
      Specified by:
      iterator in interface Iterable<VType>