Class ConcurrentSkipListIntObjMultimap<V>
- Type Parameters:
V- the type of mapped values
- All Implemented Interfaces:
Iterable<ConcurrentSkipListIntObjMultimap.IntEntry<V>>
int keys.
This class implements a concurrent variant of SkipLists
providing expected average log(n) time cost for the
containsKey, get, put and
remove operations and their variants. Insertion, removal,
update, and access operations safely execute concurrently by
multiple threads.
This class is a multimap, which means the same key can be associated with
multiple values. Each such instance will be represented by a separate
IntEntry. There is no defined ordering for the values mapped to
the same key.
As a multimap, certain atomic operations like putIfPresent,
compute, or computeIfPresent, cannot be supported.
Likewise, some get-like operations cannot be supported.
Iterators and spliterators are weakly consistent.
All IntEntry pairs returned by methods in this class
represent snapshots of mappings at the time they were
produced. They do not support the Entry.setValue
method. (Note however that it is possible to change mappings in the
associated map using put, putIfAbsent, or
replace, depending on exactly which effect you need.)
Beware that bulk operations putAll, equals,
toArray, containsValue, and clear are
not guaranteed to be performed atomically. For example, an
iterator operating concurrently with a putAll operation
might view only some of the added elements.
This class does not permit the use of null values
because some null return values cannot be reliably distinguished from
the absence of elements.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescription(package private) final class(package private) static final classIndex nodes represent the levels of the skip list.static final classThe multimap entry type with primitiveintkeys.(package private) classBase of iterator classes(package private) static final classNodes hold keys and values, and are singly linked in sorted order, possibly with some intervening marker nodes. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static intprivate final LongCounterElement countprivate static final intprivate static final intprivate ConcurrentSkipListIntObjMultimap.Index<V> Lazily initialized topmost index of the skiplist.private static final AtomicReferenceFieldUpdater<ConcurrentSkipListIntObjMultimap<?>, ConcurrentSkipListIntObjMultimap.Index<?>> private static final intprivate static final AtomicReferenceFieldUpdater<ConcurrentSkipListIntObjMultimap.Node<?>, ConcurrentSkipListIntObjMultimap.Node<?>> private final intNo-key sentinel valueprivate static final AtomicReferenceFieldUpdater<ConcurrentSkipListIntObjMultimap.Index<?>, ConcurrentSkipListIntObjMultimap.Index<?>> private static final AtomicReferenceFieldUpdater<ConcurrentSkipListIntObjMultimap.Node<?>, Object> -
Constructor Summary
ConstructorsConstructorDescriptionConcurrentSkipListIntObjMultimap(int noKey) Constructs a new, empty map, sorted according to the natural ordering of the keys. -
Method Summary
Modifier and TypeMethodDescriptionprivate static voidOrders LOADS before the fence, with LOADS and STORES after the fence.private voidaddCount(long c) Adds to element count, initializing adder if necessary(package private) static <V> booleanaddIndices(ConcurrentSkipListIntObjMultimap.Index<V> q, int skips, ConcurrentSkipListIntObjMultimap.Index<V> x, int noKey) Add indices after an insertion.(package private) final ConcurrentSkipListIntObjMultimap.Node<V> baseHead()Returns the header for base node list, or null if uninitializedceilingEntry(int key) Returns a key-value mapping associated with the least key greater than or equal to the given key, ornullif there is no such entry.intceilingKey(int key) voidclear()Removes all of the mappings from this map.private static <T> Class<T> booleancontainsKey(int key) Returnstrueif this map contains a mapping for the specified key.booleancontainsValue(Object value) Returnstrueif this map maps one or more keys to the specified value.(package private) static intcpr(int x, int y) Compares using comparator or natural ordering if null.private VdoGet(int key) Gets value for key.private VMain insertion method.(package private) final VMain deletion method.Removes first entry; returns its snapshot.Removes last entry; returns its snapshot.(package private) final ConcurrentSkipListIntObjMultimap.Node<V> Gets first valid node, unlinking deleted nodes if encountered.(package private) final ConcurrentSkipListIntObjMultimap.IntEntry<V> Entry snapshot version of findFirst(package private) final ConcurrentSkipListIntObjMultimap.Node<V> findLast()Specialized version of find to get last valid node.(package private) final ConcurrentSkipListIntObjMultimap.IntEntry<V> Entry version of findLast(package private) final ConcurrentSkipListIntObjMultimap.Node<V> findNear(int key, int rel) Utility for ceiling, floor, lower, higher methods.(package private) final ConcurrentSkipListIntObjMultimap.IntEntry<V> findNearEntry(int key, int rel) Variant of findNear returning IntEntryprivate ConcurrentSkipListIntObjMultimap.Node<V> findNode(int key) Returns node holding key or null if no such, clearing out any deleted nodes seen along the way.private ConcurrentSkipListIntObjMultimap.Node<V> findPredecessor(int key) Returns an index node with key strictly less than given key.Returns a key-value mapping associated with the least key in this map, ornullif the map is empty.intfirstKey()floorEntry(int key) Returns a key-value mapping associated with the greatest key less than or equal to the given key, ornullif there is no such key.intfloorKey(int key) get(int key) Returns the value to which the specified key is mapped, ornullif this map contains no mapping for the key.(package private) final longReturns element count, initializing adder if necessary.getOrDefault(int key, V defaultValue) Returns the value to which the specified key is mapped, or the given defaultValue if this map contains no mapping for the key.higherEntry(int key) Returns a key-value mapping associated with the least key strictly greater than the given key, ornullif there is no such key.inthigherKey(int key) booleanisEmpty()Check if the collection is empty.iterator()Returns a key-value mapping associated with the greatest key in this map, ornullif the map is empty.intlastKey()lowerEntry(int key) Returns a key-value mapping associated with the greatest key strictly less than the given key, ornullif there is no such key.intlowerKey(int key) pollCeilingEntry(int key) Removes and returns a key-value mapping associated with the least key in this map, ornullif the map is empty.Removes and returns a key-value mapping associated with the greatest key in this map, ornullif the map is empty.voidAssociates the specified value with the specified key in this map.remove(int key) Removes the mapping for the specified key from this map if present.booleanRemove the specific entry with the given key and value, if it exist.booleanReplace the specific entry with the given key and value, with the given replacement value, if such an entry exist.intsize()Get the approximate size of the collection.private voidPossibly reduce head level if it has no nodes.(package private) static <V> voidunlinkNode(ConcurrentSkipListIntObjMultimap.Node<V> b, ConcurrentSkipListIntObjMultimap.Node<V> n, int noKey) Tries to unlink deleted node n from predecessor b (if both exist), by first splicing in a marker if not already present.Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface Iterable
forEach, spliterator
-
Field Details
-
noKey
private final int noKeyNo-key sentinel value -
head
Lazily initialized topmost index of the skiplist. -
adder
Element count -
EQ
private static final int EQ- See Also:
-
LT
private static final int LT- See Also:
-
GT
private static final int GT- See Also:
-
HEAD
private static final AtomicReferenceFieldUpdater<ConcurrentSkipListIntObjMultimap<?>, ConcurrentSkipListIntObjMultimap.Index<?>> HEAD -
NEXT
private static final AtomicReferenceFieldUpdater<ConcurrentSkipListIntObjMultimap.Node<?>, ConcurrentSkipListIntObjMultimap.Node<?>> NEXT -
VAL
private static final AtomicReferenceFieldUpdater<ConcurrentSkipListIntObjMultimap.Node<?>, Object> VAL -
RIGHT
private static final AtomicReferenceFieldUpdater<ConcurrentSkipListIntObjMultimap.Index<?>, ConcurrentSkipListIntObjMultimap.Index<?>> RIGHT -
acquireFenceVariable
private static volatile int acquireFenceVariable
-
-
Constructor Details
-
ConcurrentSkipListIntObjMultimap
public ConcurrentSkipListIntObjMultimap(int noKey) Constructs a new, empty map, sorted according to the natural ordering of the keys.- Parameters:
noKey- The value to use as a sentinel for signaling the absence of a key.
-
-
Method Details
-
cpr
static int cpr(int x, int y) Compares using comparator or natural ordering if null. Called only by methods that have performed required type checks. -
baseHead
Returns the header for base node list, or null if uninitialized -
unlinkNode
static <V> void unlinkNode(ConcurrentSkipListIntObjMultimap.Node<V> b, ConcurrentSkipListIntObjMultimap.Node<V> n, int noKey) Tries to unlink deleted node n from predecessor b (if both exist), by first splicing in a marker if not already present. Upon return, node n is sure to be unlinked from b, possibly via the actions of some other thread.- Parameters:
b- if nonnull, predecessorn- if nonnull, node known to be deleted
-
addCount
private void addCount(long c) Adds to element count, initializing adder if necessary- Parameters:
c- count to add
-
getAdderCount
final long getAdderCount()Returns element count, initializing adder if necessary. -
findPredecessor
Returns an index node with key strictly less than given key. Also unlinks indexes to deleted nodes found along the way. Callers rely on this side-effect of clearing indices to deleted nodes.- Parameters:
key- if nonnull the key- Returns:
- a predecessor node of key, or null if uninitialized or null key
-
findNode
Returns node holding key or null if no such, clearing out any deleted nodes seen along the way. Repeatedly traverses at base-level looking for key starting at predecessor returned from findPredecessor, processing base-level deletions as encountered. Restarts occur, at traversal step encountering node n, if n's key field is null, indicating it is a marker, so its predecessor is deleted before continuing, which we help do by re-finding a valid predecessor. The traversal loops in doPut, doRemove, and findNear all include the same checks.- Parameters:
key- the key- Returns:
- node holding key, or null if no such
-
doGet
Gets value for key. Same idea as findNode, except skips over deletions and markers, and returns first encountered value to avoid possibly inconsistent rereads.- Parameters:
key- the key- Returns:
- the value, or null if absent
-
doPut
-
addIndices
static <V> boolean addIndices(ConcurrentSkipListIntObjMultimap.Index<V> q, int skips, ConcurrentSkipListIntObjMultimap.Index<V> x, int noKey) Add indices after an insertion. Descends iteratively to the highest level of insertion, then recursively, to chain index nodes to lower ones. Returns null on (staleness) failure, disabling higher-level insertions. Recursion depths are exponentially less probable.- Parameters:
q- starting index for current levelskips- levels to skip before insertingx- index for this insertion
-
doRemove
Main deletion method. Locates node, nulls value, appends a deletion marker, unlinks predecessor, removes associated index nodes, and possibly reduces head index level.- Parameters:
key- the keyvalue- if non-null, the value that must be associated with key- Returns:
- the node, or null if not found
-
tryReduceLevel
private void tryReduceLevel()Possibly reduce head level if it has no nodes. This method can (rarely) make mistakes, in which case levels can disappear even though they are about to contain index nodes. This impacts performance, not correctness. To minimize mistakes as well as to reduce hysteresis, the level is reduced by one only if the topmost three levels look empty. Also, if the removed level looks non-empty after CAS, we try to change it back quick before anyone notices our mistake! (This trick works pretty well because this method will practically never make mistakes unless current thread stalls immediately before first CAS, in which case it is very unlikely to stall again immediately afterwards, so will recover.)We put up with all this rather than just let levels grow because otherwise, even a small map that has undergone a large number of insertions and removals will have a lot of levels, slowing down access more than would an occasional unwanted reduction.
-
findFirst
Gets first valid node, unlinking deleted nodes if encountered.- Returns:
- first node or null if empty
-
findFirstEntry
Entry snapshot version of findFirst -
doRemoveFirstEntry
Removes first entry; returns its snapshot.- Returns:
- null if empty, else snapshot of first entry
-
findLast
Specialized version of find to get last valid node.- Returns:
- last node or null if empty
-
findLastEntry
Entry version of findLast- Returns:
- Entry for last node or null if empty
-
doRemoveLastEntry
Removes last entry; returns its snapshot. Specialized variant of doRemove.- Returns:
- null if empty, else snapshot of last entry
-
findNearEntry
Variant of findNear returning IntEntry- Parameters:
key- the keyrel- the relation -- OR'ed combination of EQ, LT, GT- Returns:
- Entry fitting relation, or null if no such
-
findNear
Utility for ceiling, floor, lower, higher methods.- Parameters:
key- the keyrel- the relation -- OR'ed combination of EQ, LT, GT- Returns:
- nearest node fitting relation, or null if no such
-
containsKey
public boolean containsKey(int key) Returnstrueif this map contains a mapping for the specified key.- Parameters:
key- key whose presence in this map is to be tested- Returns:
trueif this map contains a mapping for the specified key- Throws:
ClassCastException- if the specified key cannot be compared with the keys currently in the mapNullPointerException- if the specified key is null
-
get
Returns the value to which the specified key is mapped, ornullif this map contains no mapping for the key.More formally, if this map contains a mapping from a key
kto a valuevsuch thatkeycompares equal tokaccording to the map's ordering, then this method returnsv; otherwise it returnsnull. (There can be at most one such mapping.)- Throws:
ClassCastException- if the specified key cannot be compared with the keys currently in the mapNullPointerException- if the specified key is null
-
getOrDefault
Returns the value to which the specified key is mapped, or the given defaultValue if this map contains no mapping for the key.- Parameters:
key- the keydefaultValue- the value to return if this map contains no mapping for the given key- Returns:
- the mapping for the key, if present; else the defaultValue
- Throws:
NullPointerException- if the specified key is null- Since:
- 1.8
-
put
Associates the specified value with the specified key in this map. If the map previously contained a mapping for the key, the old value is replaced.- Parameters:
key- key with which the specified value is to be associatedvalue- value to be associated with the specified key- Throws:
ClassCastException- if the specified key cannot be compared with the keys currently in the mapNullPointerException- if the specified key or value is null
-
remove
Removes the mapping for the specified key from this map if present.- Parameters:
key- key for which mapping should be removed- Returns:
- the previous value associated with the specified key, or
nullif there was no mapping for the key - Throws:
ClassCastException- if the specified key cannot be compared with the keys currently in the mapNullPointerException- if the specified key is null
-
containsValue
Returnstrueif this map maps one or more keys to the specified value. This operation requires time linear in the map size. Additionally, it is possible for the map to change during execution of this method, in which case the returned result may be inaccurate.- Parameters:
value- value whose presence in this map is to be tested- Returns:
trueif a mapping tovalueexists;falseotherwise- Throws:
NullPointerException- if the specified value is null
-
size
public int size()Get the approximate size of the collection. -
isEmpty
public boolean isEmpty()Check if the collection is empty. -
clear
public void clear()Removes all of the mappings from this map. -
remove
Remove the specific entry with the given key and value, if it exist.- Throws:
ClassCastException- if the specified key cannot be compared with the keys currently in the mapNullPointerException- if the specified key is null
-
replace
Replace the specific entry with the given key and value, with the given replacement value, if such an entry exist.- Throws:
ClassCastException- if the specified key cannot be compared with the keys currently in the mapNullPointerException- if any of the arguments are null
-
firstKey
public int firstKey() -
lastKey
public int lastKey() -
lowerEntry
Returns a key-value mapping associated with the greatest key strictly less than the given key, ornullif there is no such key. The returned entry does not support theEntry.setValuemethod.- Throws:
NullPointerException- if the specified key is null
-
lowerKey
public int lowerKey(int key) - Throws:
NullPointerException- if the specified key is null
-
floorEntry
Returns a key-value mapping associated with the greatest key less than or equal to the given key, ornullif there is no such key. The returned entry does not support theEntry.setValuemethod.- Parameters:
key- the key- Throws:
NullPointerException- if the specified key is null
-
floorKey
public int floorKey(int key) - Parameters:
key- the key- Throws:
NullPointerException- if the specified key is null
-
ceilingEntry
Returns a key-value mapping associated with the least key greater than or equal to the given key, ornullif there is no such entry. The returned entry does not support theEntry.setValuemethod.- Throws:
NullPointerException- if the specified key is null
-
ceilingKey
public int ceilingKey(int key) - Throws:
NullPointerException- if the specified key is null
-
higherEntry
Returns a key-value mapping associated with the least key strictly greater than the given key, ornullif there is no such key. The returned entry does not support theEntry.setValuemethod.- Parameters:
key- the key- Throws:
NullPointerException- if the specified key is null
-
higherKey
public int higherKey(int key) - Parameters:
key- the key- Throws:
NullPointerException- if the specified key is null
-
firstEntry
Returns a key-value mapping associated with the least key in this map, ornullif the map is empty. The returned entry does not support theEntry.setValuemethod. -
lastEntry
Returns a key-value mapping associated with the greatest key in this map, ornullif the map is empty. The returned entry does not support theEntry.setValuemethod. -
pollFirstEntry
Removes and returns a key-value mapping associated with the least key in this map, ornullif the map is empty. The returned entry does not support theEntry.setValuemethod. -
pollLastEntry
Removes and returns a key-value mapping associated with the greatest key in this map, ornullif the map is empty. The returned entry does not support theEntry.setValuemethod. -
pollCeilingEntry
-
iterator
-
cls
-
acquireFence
private static void acquireFence()Orders LOADS before the fence, with LOADS and STORES after the fence.
-