Class ConcurrentChainedInt2ObjectHashTable<V>

java.lang.Object
ca.spottedleaf.concurrentutil.map.concurrent.ints.ConcurrentChainedInt2ObjectHashTable<V>
Type Parameters:
V - The type for values in this map
All Implemented Interfaces:
Iterable<ConcurrentChainedInt2ObjectHashTable.TableEntry<V>>

public class ConcurrentChainedInt2ObjectHashTable<V> extends Object implements Iterable<ConcurrentChainedInt2ObjectHashTable.TableEntry<V>>
Concurrent hashtable implementation supporting mapping arbitrary non-null int values onto non-null V values with support for multiple writer and multiple reader threads.

Happens-before relationship

As with ConcurrentMap, there is a happens-before relationship between actions in one thread prior to writing to the map and access to the results of those actions in another thread.

Atomicity of functional methods

Functional methods are functions declared in this class which possibly perform a write (remove, replace, or modify) to an entry in this map as a result of invoking a function on an input parameter. Functional methods will be performed atomically, that is, the input parameter is guaranteed to only be invoked at most once per function call. However, the consequence of this behaviour is that a critical lock for a bin entry is held, which means that if the input parameter invocation makes additional calls to write into this hash table that the result is undefined and deadlock-prone.

See Also:
  • Field Details

  • Constructor Details

    • ConcurrentChainedInt2ObjectHashTable

      public ConcurrentChainedInt2ObjectHashTable()
    • ConcurrentChainedInt2ObjectHashTable

      protected ConcurrentChainedInt2ObjectHashTable(int capacity, float loadFactor)
  • Method Details

    • defaultCapacity

      public static int defaultCapacity()
    • defaultLoadFactor

      public static float defaultLoadFactor()
    • getThresholdAcquire

      protected final int getThresholdAcquire()
    • getThresholdVolatile

      protected final int getThresholdVolatile()
    • setThresholdPlain

      protected final void setThresholdPlain(int threshold)
    • setThresholdRelease

      protected final void setThresholdRelease(int threshold)
    • setThresholdVolatile

      protected final void setThresholdVolatile(int threshold)
    • compareExchangeThresholdVolatile

      protected final int compareExchangeThresholdVolatile(int expect, int update)
    • getTargetThreshold

      protected static int getTargetThreshold(int capacity, float loadFactor)
    • getCapacityFor

      protected static int getCapacityFor(int capacity)
    • createWithCapacity

      public static <V> ConcurrentChainedInt2ObjectHashTable<V> createWithCapacity(int capacity)
    • createWithCapacity

      public static <V> ConcurrentChainedInt2ObjectHashTable<V> createWithCapacity(int capacity, float loadFactor)
    • createWithExpected

      public static <V> ConcurrentChainedInt2ObjectHashTable<V> createWithExpected(int expected)
    • createWithExpected

      public static <V> ConcurrentChainedInt2ObjectHashTable<V> createWithExpected(int expected, float loadFactor)
    • getHash

      protected static int getHash(int key)
      must be deterministic given a key
    • getLoadFactor

      public final float getLoadFactor()
      Returns the load factor associated with this map.
    • getAtIndexAcquire

      protected static <V> ConcurrentChainedInt2ObjectHashTable.TableEntry<V> getAtIndexAcquire(ConcurrentChainedInt2ObjectHashTable.TableEntry<V>[] table, int index)
    • setAtIndexRelease

      protected static <V> void setAtIndexRelease(ConcurrentChainedInt2ObjectHashTable.TableEntry<V>[] table, int index, ConcurrentChainedInt2ObjectHashTable.TableEntry<V> value)
    • setAtIndexVolatile

      protected static <V> void setAtIndexVolatile(ConcurrentChainedInt2ObjectHashTable.TableEntry<V>[] table, int index, ConcurrentChainedInt2ObjectHashTable.TableEntry<V> value)
    • compareAndExchangeAtIndexVolatile

    • fetchNewTable

    • getNode

      protected final ConcurrentChainedInt2ObjectHashTable.TableEntry<V> getNode(int key)
      Returns the possible node associated with the key, or null if there is no such node. The node returned may have a null ConcurrentChainedInt2ObjectHashTable.TableEntry.value, in which case the node is a placeholder for a compute/computeIfAbsent call. The placeholder node should not be considered mapped in order to preserve happens-before relationships between writes and reads in the map.
    • get

      public V get(int key)
      Returns the currently mapped value associated with the specified key, or null if there is none.
      Parameters:
      key - Specified key
      Throws:
      NullPointerException - If key is null
    • getOrDefault

      public V getOrDefault(int key, V defaultValue)
      Returns the currently mapped value associated with the specified key, or the specified default value if there is none.
      Parameters:
      key - Specified key
      defaultValue - Specified default value
      Throws:
      NullPointerException - If key is null
    • containsKey

      public boolean containsKey(int key)
      Returns whether the specified key is mapped to some value.
      Parameters:
      key - Specified key
      Throws:
      NullPointerException - If key is null
    • containsValue

      public boolean containsValue(V value)
      Returns whether the specified value has a key mapped to it.
      Parameters:
      value - Specified value
      Throws:
      NullPointerException - If value is null
    • contains

      public boolean contains(int key, V value)
      Returns whether the specified key is mapped and is mapped to the specified value.
      Parameters:
      key - Specified key
      value - Specified value
      Throws:
      NullPointerException - If key is null
      NullPointerException - If value is null
    • size

      public int size()
      Returns the number of mappings in this map.
    • isEmpty

      public boolean isEmpty()
      Returns whether this map has no mappings.
    • addSize

      protected final void addSize(long count)
      Adds count to size and checks threshold for resizing
    • subSize

      protected final void subSize(long count)
      Subtracts count from size
    • put

      public V put(int key, V value)
      Atomically updates the value associated with key to value, or inserts a new mapping with key mapped to value.
      Parameters:
      key - Specified key
      value - Specified value
      Returns:
      Old value previously associated with key, or null if none.
      Throws:
      NullPointerException - If key is null
      NullPointerException - If value is null
    • putIfAbsent

      public V putIfAbsent(int key, V value)
      Atomically inserts a new mapping with key mapped to value if and only if key is not currently mapped to some value.
      Parameters:
      key - Specified key
      value - Specified value
      Returns:
      Value currently associated with key, or null if none and value was associated.
      Throws:
      NullPointerException - If key is null
      NullPointerException - If value is null
    • replace

      public V replace(int key, V value)
      Atomically updates the value associated with key to value, or does nothing if key is not associated with a value.
      Parameters:
      key - Specified key
      value - Specified value
      Returns:
      Old value previously associated with key, or null if none.
      Throws:
      NullPointerException - If key is null
      NullPointerException - If value is null
    • replace

      public V replace(int key, V expect, V update)
      Atomically updates the value associated with key to update if the currently associated value is equal to expect, otherwise does nothing.
      Parameters:
      key - Specified key
      expect - Expected value to check current mapped value with
      update - Update value to replace mapped value with
      Returns:
      If the currently mapped value is not equal to expect, then returns the currently mapped value. If the key is not mapped to any value, then returns null. If neither of the two cases are true, then returns expect.
      Throws:
      NullPointerException - If key is null
      NullPointerException - If expect is null
      NullPointerException - If update is null
    • remove

      public V remove(int key)
      Atomically removes the mapping for the specified key and returns the value it was associated with. If the key is not mapped to a value, then does nothing and returns null.
      Parameters:
      key - Specified key
      Returns:
      Old value previously associated with key, or null if none.
      Throws:
      NullPointerException - If key is null
    • remove

      public V remove(int key, V expect)
      Atomically removes the mapping for the specified key if it is mapped to expect and returns expect. If the key is not mapped to a value, then does nothing and returns null. If the key is mapped to a value that is not equal to expect, then returns that value.
      Parameters:
      key - Specified key
      expect - Specified expected value
      Returns:
      The specified expected value if the key was mapped to expect. If the key is not mapped to any value, then returns null. If neither of those cases are true, then returns the current mapped value for key.
      Throws:
      NullPointerException - If key is null
    • removeIf

      public V removeIf(int key, ConcurrentChainedInt2ObjectHashTable.RemoveIfPredicate<? super V> predicate)
      Atomically removes the mapping for the specified key the predicate returns true for its currently mapped value. If the key is not mapped to a value, then does nothing and returns null.

      This function is a "functional method" as defined by ConcurrentChainedInt2ObjectHashTable.

      Parameters:
      key - Specified key
      predicate - Specified predicate
      Returns:
      The specified expected value if the key was mapped to expect. If the key is not mapped to any value, then returns null. If neither of those cases are true, then returns the current (non-null) mapped value for key.
      Throws:
      NullPointerException - If key is null
      NullPointerException - If predicate is null
    • compute

      public V compute(int key, ConcurrentChainedInt2ObjectHashTable.BiIntObjectObjectFunction<? super V,? extends V> function)
      See ConcurrentMap.compute(Object, BiFunction)

      This function is a "functional method" as defined by ConcurrentChainedInt2ObjectHashTable.

    • computeIfAbsent

      public V computeIfAbsent(int key, ConcurrentChainedInt2ObjectHashTable.BiIntObjectFunction<? extends V> function)
      See ConcurrentMap.computeIfAbsent(Object, Function)

      This function is a "functional method" as defined by ConcurrentChainedInt2ObjectHashTable.

    • computeIfPresent

      public V computeIfPresent(int key, ConcurrentChainedInt2ObjectHashTable.BiIntObjectObjectFunction<? super V,? extends V> function)
      See ConcurrentMap.computeIfPresent(Object, BiFunction)

      This function is a "functional method" as defined by ConcurrentChainedInt2ObjectHashTable.

    • merge

      public V merge(int key, V def, BiFunction<? super V,? super V,? extends V> function)
      See ConcurrentMap.merge(Object, Object, BiFunction)

      This function is a "functional method" as defined by ConcurrentChainedInt2ObjectHashTable.

    • clear

      public void clear()
      Removes at least all entries currently mapped at the beginning of this call. May not remove entries added during this call. As a result, only if this map is not modified during the call, that all entries will be removed by the end of the call.

      This function is not atomic.

    • entryIterator

      Returns an iterator over the entries in this map. The iterator is only guaranteed to see entries that were added before the beginning of this call, but it may see entries added during.
    • iterator

      Specified by:
      iterator in interface Iterable<V>
    • keyIterator

      public BaseIntIterator keyIterator()
      Returns an iterator over the keys in this map. The iterator is only guaranteed to see keys that were added before the beginning of this call, but it may see keys added during.
    • valueIterator

      public BaseObjectIterator<V> valueIterator()
      Returns an iterator over the values in this map. The iterator is only guaranteed to see values that were added before the beginning of this call, but it may see values added during.
    • values

      public Collection<V> values()
    • entrySet