Class SWMRHashTable<K,V>
java.lang.Object
ca.spottedleaf.concurrentutil.map.SWMRHashTable<K,V>
- Type Parameters:
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.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected static final classprotected static final classprotected static final classprotected static final classprotected static final classprotected static classprotected static final classprotected static final classprotected static classprotected static class -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected static final intprotected static final floatprotected SWMRHashTable.EntrySet<K, V> protected SWMRHashTable.KeySet<K, V> protected final floatprotected static final intprotected intprotected static final VarHandleprotected SWMRHashTable.TableEntry<K,V>[] protected static final VarHandleprotected intprotected SWMRHashTable.ValueCollection<K, V> -
Constructor Summary
ConstructorsConstructorDescriptionConstructs this map with a capacity of16and load factor of0.75f.SWMRHashTable(int capacity) Constructs this map with the specified capacity and load factor of0.75f.SWMRHashTable(int capacity, float loadFactor) Constructs this map with the specified capacity and load factor.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.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 of0.75f.SWMRHashTable(Map<K, V> other) Constructs this map with a capacity of16or the specified map's size, whichever is larger, and with a load factor of0.75f. -
Method Summary
Modifier and TypeMethodDescriptionprotected final intaddToSize(int num) protected final voidcheckResize(int minCapacity) voidclear()clone()computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction) computeIfPresent(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) booleanReturnstrueif this map contains an entry with the specified key and value at some point during this call.booleancontainsKey(Object key) booleancontainsValue(Object value) entrySet()booleanvoidforEach(BiConsumer<? super K, ? super V> action) voidvoidforEachKey(Consumer<? super K> action) Provides the specified consumer with all keys contained within this map.voidforEachValue(Consumer<? super V> action) Provides the specified consumer with all values contained within this map.protected static <K,V> SWMRHashTable.TableEntry <K, V> getAtIndexOpaque(SWMRHashTable.TableEntry<K, V>[] table, int index) protected static intgetCapacityFor(int capacity) protected final SWMRHashTable.TableEntry<K, V> getEntryForOpaque(K key) Callers must still use acquire when reading the value of the entry.protected final SWMRHashTable.TableEntry<K, V> getEntryForPlain(K key) final floatgetOrDefault(Object key, V defaultValue) protected final intprotected final intprotected final intprotected final SWMRHashTable.TableEntry<K,V>[] protected final SWMRHashTable.TableEntry<K,V>[] protected static intgetTargetCapacity(int capacity, float loadFactor) inthashCode()booleanisEmpty()iterator()keySet()protected final VvoidputIfAbsent(K key, V value) protected final VbooleanintremoveEntryIf(Predicate<? super Map.Entry<K, V>> predicate) Removes a key-value pair from this map if the specified predicate returns true.protected final intremoveFromSize(int num) protected final intremoveFromSizePlain(int num) intremoveIf(BiPredicate<K, V> predicate) Removes a key-value pair from this map if the specified predicate returns true.booleanvoidreplaceAll(BiFunction<? super K, ? super V, ? extends V> function) protected static <K,V> void setAtIndexRelease(SWMRHashTable.TableEntry<K, V>[] table, int index, SWMRHashTable.TableEntry<K, V> value) protected final voidsetSizeOpaque(int value) protected final voidsetSizePlain(int value) protected final voidsetSizeRelease(int value) protected final voidsetTablePlain(SWMRHashTable.TableEntry<K, V>[] table) protected final voidsetTableRelease(SWMRHashTable.TableEntry<K, V>[] table) intsize()toString()values()Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.lang.Iterable
spliterator
-
Field Details
-
size
protected int size -
table
-
loadFactor
protected final float loadFactor -
SIZE_HANDLE
-
TABLE_HANDLE
-
DEFAULT_CAPACITY
protected static final int DEFAULT_CAPACITY- See Also:
-
DEFAULT_LOAD_FACTOR
protected static final float DEFAULT_LOAD_FACTOR- See Also:
-
MAXIMUM_CAPACITY
protected static final int MAXIMUM_CAPACITY- See Also:
-
keyset
-
values
-
entrySet
-
threshold
protected int threshold
-
-
Constructor Details
-
SWMRHashTable
public SWMRHashTable()Constructs this map with a capacity of16and load factor of0.75f. -
SWMRHashTable
public SWMRHashTable(int capacity) Constructs this map with the specified capacity and load factor of0.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, > 0loadFactor- specified load factor,> 0 && finite
-
SWMRHashTable
Constructs this map with a capacity of16or the specified map's size, whichever is larger, and with a load factor of0.75f. All of the specified map's entries are copied into this map.- Parameters:
other- The specified map.
-
SWMRHashTable
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 of0.75f. All of the specified map's entries are copied into this map.- Parameters:
capacity- specified capacity, > 0other- The specified map.
-
SWMRHashTable
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, > 0loadFactor- specified load factor,> 0 && finiteother- 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
-
getTableAcquire
-
setTablePlain
-
setTableRelease
-
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
Callers must still use acquire when reading the value of the entry. -
getEntryForPlain
-
getTargetCapacity
protected static int getTargetCapacity(int capacity, float loadFactor) -
equals
-
hashCode
public int hashCode() -
toString
-
clone
-
iterator
-
forEach
-
forEach
-
forEachKey
Provides the specified consumer with all keys contained within this map.- Parameters:
action- The specified consumer.
-
forEachValue
Provides the specified consumer with all values contained within this map. Equivalent tomap.values().forEach(Consumer).- Parameters:
action- The specified consumer.
-
get
-
containsKey
- Specified by:
containsKeyin interfaceMap<K,V>
-
contains
Returnstrueif 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:
trueif this map contains an entry with the specified key and value.
-
containsValue
- Specified by:
containsValuein interfaceMap<K,V>
-
getOrDefault
- Specified by:
getOrDefaultin interfaceMap<K,V>
-
size
public int size() -
isEmpty
public boolean isEmpty() -
keySet
-
values
-
entrySet
-
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
-
removeIf
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
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
-
putIfAbsent
- Specified by:
putIfAbsentin interfaceMap<K,V>
-
remove
-
remove
-
remove
-
replace
-
replace
-
replaceAll
- Specified by:
replaceAllin interfaceMap<K,V>
-
putAll
-
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.
-
compute
-
computeIfPresent
- Specified by:
computeIfPresentin interfaceMap<K,V>
-
computeIfAbsent
- Specified by:
computeIfAbsentin interfaceMap<K,V>
-
merge
-