Class SWMRHashTable<K,V>

java.lang.Object
ca.spottedleaf.concurrentutil.map.SWMRHashTable<K,V>
Type Parameters:
K -
V -
All Implemented Interfaces:
Iterable<Map.Entry<K,V>>, Map<K,V>

public class SWMRHashTable<K,V> extends Object implements Map<K,V>, Iterable<Map.Entry<K,V>>

Note: Not really tested, use at your own risk.

This map is safe for reading from multiple threads, however it is only safe to write from a single thread. null keys or values are not permitted. Writes to values in this map are guaranteed to be ordered by release semantics, however immediate visibility to other threads is not guaranteed. However, writes are guaranteed to be made visible eventually. Reads are ordered by acquire semantics.

Iterators cannot be modified concurrently, and its backing map cannot be modified concurrently. There is no fast-fail attempt made by iterators, thus modifying the iterator's backing map while iterating will have undefined behaviour.

Subclasses should override clone() to return correct instances of this class.

  • Field Details

  • Constructor Details

    • SWMRHashTable

      public SWMRHashTable()
      Constructs this map with a capacity of 16 and load factor of 0.75f.
    • SWMRHashTable

      public SWMRHashTable(int capacity)
      Constructs this map with the specified capacity and load factor of 0.75f.
      Parameters:
      capacity - specified initial capacity, > 0
    • SWMRHashTable

      public SWMRHashTable(int capacity, float loadFactor)
      Constructs this map with the specified capacity and load factor.
      Parameters:
      capacity - specified capacity, > 0
      loadFactor - specified load factor, > 0 && finite
    • SWMRHashTable

      public SWMRHashTable(Map<K,V> other)
      Constructs this map with a capacity of 16 or the specified map's size, whichever is larger, and with a load factor of 0.75f. All of the specified map's entries are copied into this map.
      Parameters:
      other - The specified map.
    • SWMRHashTable

      public SWMRHashTable(int capacity, Map<K,V> other)
      Constructs this map with a minimum capacity of the specified capacity or the specified map's size, whichever is larger, and with a load factor of 0.75f. All of the specified map's entries are copied into this map.
      Parameters:
      capacity - specified capacity, > 0
      other - The specified map.
    • SWMRHashTable

      public SWMRHashTable(int capacity, float loadFactor, Map<K,V> other)
      Constructs this map with a min capacity of the specified capacity or the specified map's size, whichever is larger, and with the specified load factor. All of the specified map's entries are copied into this map.
      Parameters:
      capacity - specified capacity, > 0
      loadFactor - specified load factor, > 0 && finite
      other - The specified map.
  • Method Details

    • getSizePlain

      protected final int getSizePlain()
    • getSizeOpaque

      protected final int getSizeOpaque()
    • getSizeAcquire

      protected final int getSizeAcquire()
    • setSizePlain

      protected final void setSizePlain(int value)
    • setSizeOpaque

      protected final void setSizeOpaque(int value)
    • setSizeRelease

      protected final void setSizeRelease(int value)
    • getTablePlain

      protected final SWMRHashTable.TableEntry<K,V>[] getTablePlain()
    • getTableAcquire

      protected final SWMRHashTable.TableEntry<K,V>[] getTableAcquire()
    • setTablePlain

      protected final void setTablePlain(SWMRHashTable.TableEntry<K,V>[] table)
    • setTableRelease

      protected final void setTableRelease(SWMRHashTable.TableEntry<K,V>[] table)
    • getAtIndexOpaque

      protected static <K, V> SWMRHashTable.TableEntry<K,V> getAtIndexOpaque(SWMRHashTable.TableEntry<K,V>[] table, int index)
    • setAtIndexRelease

      protected static <K, V> void setAtIndexRelease(SWMRHashTable.TableEntry<K,V>[] table, int index, SWMRHashTable.TableEntry<K,V> value)
    • getLoadFactor

      public final float getLoadFactor()
    • getCapacityFor

      protected static int getCapacityFor(int capacity)
    • getEntryForOpaque

      protected final SWMRHashTable.TableEntry<K,V> getEntryForOpaque(K key)
      Callers must still use acquire when reading the value of the entry.
    • getEntryForPlain

      protected final SWMRHashTable.TableEntry<K,V> getEntryForPlain(K key)
    • getTargetCapacity

      protected static int getTargetCapacity(int capacity, float loadFactor)
    • equals

      public boolean equals(Object obj)
      Specified by:
      equals in interface Map<K,V>
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Specified by:
      hashCode in interface Map<K,V>
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • clone

      public SWMRHashTable<K,V> clone()
      Overrides:
      clone in class Object
    • iterator

      public Iterator<Map.Entry<K,V>> iterator()
      Specified by:
      iterator in interface Iterable<K>
    • forEach

      public void forEach(Consumer<? super Map.Entry<K,V>> action)
      Specified by:
      forEach in interface Iterable<K>
    • forEach

      public void forEach(BiConsumer<? super K,? super V> action)
      Specified by:
      forEach in interface Map<K,V>
    • forEachKey

      public void forEachKey(Consumer<? super K> action)
      Provides the specified consumer with all keys contained within this map.
      Parameters:
      action - The specified consumer.
    • forEachValue

      public void forEachValue(Consumer<? super V> action)
      Provides the specified consumer with all values contained within this map. Equivalent to map.values().forEach(Consumer).
      Parameters:
      action - The specified consumer.
    • get

      public V get(Object key)
      Specified by:
      get in interface Map<K,V>
    • containsKey

      public boolean containsKey(Object key)
      Specified by:
      containsKey in interface Map<K,V>
    • contains

      public boolean contains(Object key, Object value)
      Returns true if this map contains an entry with the specified key and value at some point during this call.
      Parameters:
      key - The specified key.
      value - The specified value.
      Returns:
      true if this map contains an entry with the specified key and value.
    • containsValue

      public boolean containsValue(Object value)
      Specified by:
      containsValue in interface Map<K,V>
    • getOrDefault

      public V getOrDefault(Object key, V defaultValue)
      Specified by:
      getOrDefault in interface Map<K,V>
    • size

      public int size()
      Specified by:
      size in interface Map<K,V>
    • isEmpty

      public boolean isEmpty()
      Specified by:
      isEmpty in interface Map<K,V>
    • keySet

      public Set<K> keySet()
      Specified by:
      keySet in interface Map<K,V>
    • values

      public Collection<V> values()
      Specified by:
      values in interface Map<K,V>
    • entrySet

      public Set<Map.Entry<K,V>> entrySet()
      Specified by:
      entrySet in interface Map<K,V>
    • checkResize

      protected final void checkResize(int minCapacity)
    • addToSize

      protected final int addToSize(int num)
    • removeFromSize

      protected final int removeFromSize(int num)
    • removeFromSizePlain

      protected final int removeFromSizePlain(int num)
    • put

      protected final V put(K key, V value, boolean onlyIfAbsent)
    • removeIf

      public int removeIf(BiPredicate<K,V> predicate)
      Removes a key-value pair from this map if the specified predicate returns true. The specified predicate is tested with every entry in this map. Returns the number of key-value pairs removed.
      Parameters:
      predicate - The predicate to test key-value pairs against.
      Returns:
      The total number of key-value pairs removed from this map.
    • removeEntryIf

      public int removeEntryIf(Predicate<? super Map.Entry<K,V>> predicate)
      Removes a key-value pair from this map if the specified predicate returns true. The specified predicate is tested with every entry in this map. Returns the number of key-value pairs removed.
      Parameters:
      predicate - The predicate to test key-value pairs against.
      Returns:
      The total number of key-value pairs removed from this map.
    • put

      public V put(K key, V value)
      Specified by:
      put in interface Map<K,V>
    • putIfAbsent

      public V putIfAbsent(K key, V value)
      Specified by:
      putIfAbsent in interface Map<K,V>
    • remove

      public boolean remove(Object key, Object value)
      Specified by:
      remove in interface Map<K,V>
    • remove

      protected final V remove(Object key, int hash)
    • remove

      public V remove(Object key)
      Specified by:
      remove in interface Map<K,V>
    • replace

      public boolean replace(K key, V oldValue, V newValue)
      Specified by:
      replace in interface Map<K,V>
    • replace

      public V replace(K key, V value)
      Specified by:
      replace in interface Map<K,V>
    • replaceAll

      public void replaceAll(BiFunction<? super K,? super V,? extends V> function)
      Specified by:
      replaceAll in interface Map<K,V>
    • putAll

      public void putAll(Map<? extends K,? extends V> map)
      Specified by:
      putAll in interface Map<K,V>
    • clear

      public void clear()

      This call is non-atomic and the order that which entries are removed is undefined. The clear operation itself is release ordered, that is, after the clear operation is performed a release fence is performed.

      Specified by:
      clear in interface Map<K,V>
    • compute

      public V compute(K key, BiFunction<? super K,? super V,? extends V> remappingFunction)
      Specified by:
      compute in interface Map<K,V>
    • computeIfPresent

      public V computeIfPresent(K key, BiFunction<? super K,? super V,? extends V> remappingFunction)
      Specified by:
      computeIfPresent in interface Map<K,V>
    • computeIfAbsent

      public V computeIfAbsent(K key, Function<? super K,? extends V> mappingFunction)
      Specified by:
      computeIfAbsent in interface Map<K,V>
    • merge

      public V merge(K key, V value, BiFunction<? super V,? super V,? extends V> remappingFunction)
      Specified by:
      merge in interface Map<K,V>