1 |
dl |
1.1 |
/* |
2 |
|
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
|
* Expert Group and released to the public domain, as explained at |
4 |
jsr166 |
1.67 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
dl |
1.1 |
*/ |
6 |
|
|
|
7 |
|
|
package java.util.concurrent; |
8 |
jsr166 |
1.138 |
|
9 |
jsr166 |
1.130 |
import java.io.Serializable; |
10 |
dl |
1.118 |
import java.util.AbstractCollection; |
11 |
|
|
import java.util.AbstractMap; |
12 |
|
|
import java.util.AbstractSet; |
13 |
|
|
import java.util.ArrayList; |
14 |
|
|
import java.util.Collection; |
15 |
|
|
import java.util.Collections; |
16 |
|
|
import java.util.Comparator; |
17 |
|
|
import java.util.Iterator; |
18 |
|
|
import java.util.List; |
19 |
|
|
import java.util.Map; |
20 |
|
|
import java.util.NavigableMap; |
21 |
|
|
import java.util.NavigableSet; |
22 |
|
|
import java.util.NoSuchElementException; |
23 |
|
|
import java.util.Set; |
24 |
|
|
import java.util.SortedMap; |
25 |
dl |
1.83 |
import java.util.Spliterator; |
26 |
jsr166 |
1.138 |
import java.util.function.BiConsumer; |
27 |
dl |
1.118 |
import java.util.function.BiFunction; |
28 |
dl |
1.94 |
import java.util.function.Consumer; |
29 |
dl |
1.109 |
import java.util.function.Function; |
30 |
dl |
1.143 |
import java.util.function.Predicate; |
31 |
dl |
1.83 |
|
32 |
dl |
1.1 |
/** |
33 |
jsr166 |
1.22 |
* A scalable concurrent {@link ConcurrentNavigableMap} implementation. |
34 |
|
|
* The map is sorted according to the {@linkplain Comparable natural |
35 |
|
|
* ordering} of its keys, or by a {@link Comparator} provided at map |
36 |
|
|
* creation time, depending on which constructor is used. |
37 |
dl |
1.1 |
* |
38 |
|
|
* <p>This class implements a concurrent variant of <a |
39 |
dl |
1.66 |
* href="http://en.wikipedia.org/wiki/Skip_list" target="_top">SkipLists</a> |
40 |
|
|
* providing expected average <i>log(n)</i> time cost for the |
41 |
jsr166 |
1.82 |
* {@code containsKey}, {@code get}, {@code put} and |
42 |
|
|
* {@code remove} operations and their variants. Insertion, removal, |
43 |
dl |
1.1 |
* update, and access operations safely execute concurrently by |
44 |
jsr166 |
1.133 |
* multiple threads. |
45 |
|
|
* |
46 |
|
|
* <p>Iterators and spliterators are |
47 |
|
|
* <a href="package-summary.html#Weakly"><i>weakly consistent</i></a>. |
48 |
|
|
* |
49 |
|
|
* <p>Ascending key ordered views and their iterators are faster than |
50 |
|
|
* descending ones. |
51 |
dl |
1.1 |
* |
52 |
jsr166 |
1.82 |
* <p>All {@code Map.Entry} pairs returned by methods in this class |
53 |
dl |
1.1 |
* and its views represent snapshots of mappings at the time they were |
54 |
jsr166 |
1.82 |
* produced. They do <em>not</em> support the {@code Entry.setValue} |
55 |
dl |
1.1 |
* method. (Note however that it is possible to change mappings in the |
56 |
jsr166 |
1.82 |
* associated map using {@code put}, {@code putIfAbsent}, or |
57 |
|
|
* {@code replace}, depending on exactly which effect you need.) |
58 |
dl |
1.1 |
* |
59 |
jsr166 |
1.82 |
* <p>Beware that, unlike in most collections, the {@code size} |
60 |
dl |
1.1 |
* method is <em>not</em> a constant-time operation. Because of the |
61 |
|
|
* asynchronous nature of these maps, determining the current number |
62 |
dl |
1.69 |
* of elements requires a traversal of the elements, and so may report |
63 |
|
|
* inaccurate results if this collection is modified during traversal. |
64 |
jsr166 |
1.82 |
* Additionally, the bulk operations {@code putAll}, {@code equals}, |
65 |
|
|
* {@code toArray}, {@code containsValue}, and {@code clear} are |
66 |
dl |
1.69 |
* <em>not</em> guaranteed to be performed atomically. For example, an |
67 |
jsr166 |
1.82 |
* iterator operating concurrently with a {@code putAll} operation |
68 |
dl |
1.69 |
* might view only some of the added elements. |
69 |
dl |
1.1 |
* |
70 |
|
|
* <p>This class and its views and iterators implement all of the |
71 |
|
|
* <em>optional</em> methods of the {@link Map} and {@link Iterator} |
72 |
|
|
* interfaces. Like most other concurrent collections, this class does |
73 |
jsr166 |
1.82 |
* <em>not</em> permit the use of {@code null} keys or values because some |
74 |
jsr166 |
1.22 |
* null return values cannot be reliably distinguished from the absence of |
75 |
|
|
* elements. |
76 |
dl |
1.1 |
* |
77 |
jsr166 |
1.21 |
* <p>This class is a member of the |
78 |
jsr166 |
1.51 |
* <a href="{@docRoot}/../technotes/guides/collections/index.html"> |
79 |
jsr166 |
1.21 |
* Java Collections Framework</a>. |
80 |
|
|
* |
81 |
dl |
1.1 |
* @author Doug Lea |
82 |
|
|
* @param <K> the type of keys maintained by this map |
83 |
dl |
1.9 |
* @param <V> the type of mapped values |
84 |
jsr166 |
1.20 |
* @since 1.6 |
85 |
dl |
1.1 |
*/ |
86 |
dl |
1.9 |
public class ConcurrentSkipListMap<K,V> extends AbstractMap<K,V> |
87 |
jsr166 |
1.130 |
implements ConcurrentNavigableMap<K,V>, Cloneable, Serializable { |
88 |
dl |
1.1 |
/* |
89 |
|
|
* This class implements a tree-like two-dimensionally linked skip |
90 |
|
|
* list in which the index levels are represented in separate |
91 |
|
|
* nodes from the base nodes holding data. There are two reasons |
92 |
|
|
* for taking this approach instead of the usual array-based |
93 |
|
|
* structure: 1) Array based implementations seem to encounter |
94 |
|
|
* more complexity and overhead 2) We can use cheaper algorithms |
95 |
|
|
* for the heavily-traversed index lists than can be used for the |
96 |
|
|
* base lists. Here's a picture of some of the basics for a |
97 |
|
|
* possible list with 2 levels of index: |
98 |
|
|
* |
99 |
|
|
* Head nodes Index nodes |
100 |
dl |
1.9 |
* +-+ right +-+ +-+ |
101 |
dl |
1.1 |
* |2|---------------->| |--------------------->| |->null |
102 |
dl |
1.9 |
* +-+ +-+ +-+ |
103 |
dl |
1.1 |
* | down | | |
104 |
|
|
* v v v |
105 |
dl |
1.9 |
* +-+ +-+ +-+ +-+ +-+ +-+ |
106 |
dl |
1.1 |
* |1|----------->| |->| |------>| |----------->| |------>| |->null |
107 |
dl |
1.9 |
* +-+ +-+ +-+ +-+ +-+ +-+ |
108 |
dl |
1.1 |
* v | | | | | |
109 |
|
|
* Nodes next v v v v v |
110 |
dl |
1.9 |
* +-+ +-+ +-+ +-+ +-+ +-+ +-+ +-+ +-+ +-+ +-+ +-+ |
111 |
dl |
1.1 |
* | |->|A|->|B|->|C|->|D|->|E|->|F|->|G|->|H|->|I|->|J|->|K|->null |
112 |
dl |
1.9 |
* +-+ +-+ +-+ +-+ +-+ +-+ +-+ +-+ +-+ +-+ +-+ +-+ |
113 |
dl |
1.1 |
* |
114 |
|
|
* The base lists use a variant of the HM linked ordered set |
115 |
|
|
* algorithm. See Tim Harris, "A pragmatic implementation of |
116 |
|
|
* non-blocking linked lists" |
117 |
|
|
* http://www.cl.cam.ac.uk/~tlh20/publications.html and Maged |
118 |
|
|
* Michael "High Performance Dynamic Lock-Free Hash Tables and |
119 |
|
|
* List-Based Sets" |
120 |
|
|
* http://www.research.ibm.com/people/m/michael/pubs.htm. The |
121 |
|
|
* basic idea in these lists is to mark the "next" pointers of |
122 |
|
|
* deleted nodes when deleting to avoid conflicts with concurrent |
123 |
|
|
* insertions, and when traversing to keep track of triples |
124 |
|
|
* (predecessor, node, successor) in order to detect when and how |
125 |
|
|
* to unlink these deleted nodes. |
126 |
|
|
* |
127 |
|
|
* Rather than using mark-bits to mark list deletions (which can |
128 |
|
|
* be slow and space-intensive using AtomicMarkedReference), nodes |
129 |
|
|
* use direct CAS'able next pointers. On deletion, instead of |
130 |
|
|
* marking a pointer, they splice in another node that can be |
131 |
|
|
* thought of as standing for a marked pointer (indicating this by |
132 |
|
|
* using otherwise impossible field values). Using plain nodes |
133 |
|
|
* acts roughly like "boxed" implementations of marked pointers, |
134 |
|
|
* but uses new nodes only when nodes are deleted, not for every |
135 |
|
|
* link. This requires less space and supports faster |
136 |
|
|
* traversal. Even if marked references were better supported by |
137 |
|
|
* JVMs, traversal using this technique might still be faster |
138 |
|
|
* because any search need only read ahead one more node than |
139 |
|
|
* otherwise required (to check for trailing marker) rather than |
140 |
|
|
* unmasking mark bits or whatever on each read. |
141 |
|
|
* |
142 |
|
|
* This approach maintains the essential property needed in the HM |
143 |
|
|
* algorithm of changing the next-pointer of a deleted node so |
144 |
|
|
* that any other CAS of it will fail, but implements the idea by |
145 |
|
|
* changing the pointer to point to a different node, not by |
146 |
|
|
* marking it. While it would be possible to further squeeze |
147 |
|
|
* space by defining marker nodes not to have key/value fields, it |
148 |
|
|
* isn't worth the extra type-testing overhead. The deletion |
149 |
|
|
* markers are rarely encountered during traversal and are |
150 |
|
|
* normally quickly garbage collected. (Note that this technique |
151 |
|
|
* would not work well in systems without garbage collection.) |
152 |
|
|
* |
153 |
|
|
* In addition to using deletion markers, the lists also use |
154 |
|
|
* nullness of value fields to indicate deletion, in a style |
155 |
|
|
* similar to typical lazy-deletion schemes. If a node's value is |
156 |
|
|
* null, then it is considered logically deleted and ignored even |
157 |
|
|
* though it is still reachable. This maintains proper control of |
158 |
|
|
* concurrent replace vs delete operations -- an attempted replace |
159 |
|
|
* must fail if a delete beat it by nulling field, and a delete |
160 |
|
|
* must return the last non-null value held in the field. (Note: |
161 |
|
|
* Null, rather than some special marker, is used for value fields |
162 |
|
|
* here because it just so happens to mesh with the Map API |
163 |
|
|
* requirement that method get returns null if there is no |
164 |
|
|
* mapping, which allows nodes to remain concurrently readable |
165 |
|
|
* even when deleted. Using any other marker value here would be |
166 |
|
|
* messy at best.) |
167 |
|
|
* |
168 |
|
|
* Here's the sequence of events for a deletion of node n with |
169 |
|
|
* predecessor b and successor f, initially: |
170 |
|
|
* |
171 |
dl |
1.9 |
* +------+ +------+ +------+ |
172 |
dl |
1.1 |
* ... | b |------>| n |----->| f | ... |
173 |
dl |
1.9 |
* +------+ +------+ +------+ |
174 |
dl |
1.1 |
* |
175 |
|
|
* 1. CAS n's value field from non-null to null. |
176 |
|
|
* From this point on, no public operations encountering |
177 |
|
|
* the node consider this mapping to exist. However, other |
178 |
|
|
* ongoing insertions and deletions might still modify |
179 |
|
|
* n's next pointer. |
180 |
|
|
* |
181 |
|
|
* 2. CAS n's next pointer to point to a new marker node. |
182 |
|
|
* From this point on, no other nodes can be appended to n. |
183 |
|
|
* which avoids deletion errors in CAS-based linked lists. |
184 |
|
|
* |
185 |
|
|
* +------+ +------+ +------+ +------+ |
186 |
|
|
* ... | b |------>| n |----->|marker|------>| f | ... |
187 |
dl |
1.9 |
* +------+ +------+ +------+ +------+ |
188 |
dl |
1.1 |
* |
189 |
|
|
* 3. CAS b's next pointer over both n and its marker. |
190 |
|
|
* From this point on, no new traversals will encounter n, |
191 |
|
|
* and it can eventually be GCed. |
192 |
|
|
* +------+ +------+ |
193 |
|
|
* ... | b |----------------------------------->| f | ... |
194 |
dl |
1.9 |
* +------+ +------+ |
195 |
|
|
* |
196 |
dl |
1.1 |
* A failure at step 1 leads to simple retry due to a lost race |
197 |
|
|
* with another operation. Steps 2-3 can fail because some other |
198 |
|
|
* thread noticed during a traversal a node with null value and |
199 |
|
|
* helped out by marking and/or unlinking. This helping-out |
200 |
|
|
* ensures that no thread can become stuck waiting for progress of |
201 |
|
|
* the deleting thread. The use of marker nodes slightly |
202 |
|
|
* complicates helping-out code because traversals must track |
203 |
|
|
* consistent reads of up to four nodes (b, n, marker, f), not |
204 |
|
|
* just (b, n, f), although the next field of a marker is |
205 |
|
|
* immutable, and once a next field is CAS'ed to point to a |
206 |
|
|
* marker, it never again changes, so this requires less care. |
207 |
|
|
* |
208 |
|
|
* Skip lists add indexing to this scheme, so that the base-level |
209 |
|
|
* traversals start close to the locations being found, inserted |
210 |
|
|
* or deleted -- usually base level traversals only traverse a few |
211 |
|
|
* nodes. This doesn't change the basic algorithm except for the |
212 |
|
|
* need to make sure base traversals start at predecessors (here, |
213 |
|
|
* b) that are not (structurally) deleted, otherwise retrying |
214 |
dl |
1.9 |
* after processing the deletion. |
215 |
dl |
1.1 |
* |
216 |
|
|
* Index levels are maintained as lists with volatile next fields, |
217 |
|
|
* using CAS to link and unlink. Races are allowed in index-list |
218 |
|
|
* operations that can (rarely) fail to link in a new index node |
219 |
|
|
* or delete one. (We can't do this of course for data nodes.) |
220 |
|
|
* However, even when this happens, the index lists remain sorted, |
221 |
|
|
* so correctly serve as indices. This can impact performance, |
222 |
|
|
* but since skip lists are probabilistic anyway, the net result |
223 |
|
|
* is that under contention, the effective "p" value may be lower |
224 |
|
|
* than its nominal value. And race windows are kept small enough |
225 |
|
|
* that in practice these failures are rare, even under a lot of |
226 |
|
|
* contention. |
227 |
|
|
* |
228 |
|
|
* The fact that retries (for both base and index lists) are |
229 |
|
|
* relatively cheap due to indexing allows some minor |
230 |
|
|
* simplifications of retry logic. Traversal restarts are |
231 |
|
|
* performed after most "helping-out" CASes. This isn't always |
232 |
|
|
* strictly necessary, but the implicit backoffs tend to help |
233 |
|
|
* reduce other downstream failed CAS's enough to outweigh restart |
234 |
|
|
* cost. This worsens the worst case, but seems to improve even |
235 |
|
|
* highly contended cases. |
236 |
|
|
* |
237 |
|
|
* Unlike most skip-list implementations, index insertion and |
238 |
jsr166 |
1.135 |
* deletion here require a separate traversal pass occurring after |
239 |
dl |
1.1 |
* the base-level action, to add or remove index nodes. This adds |
240 |
|
|
* to single-threaded overhead, but improves contended |
241 |
|
|
* multithreaded performance by narrowing interference windows, |
242 |
|
|
* and allows deletion to ensure that all index nodes will be made |
243 |
|
|
* unreachable upon return from a public remove operation, thus |
244 |
|
|
* avoiding unwanted garbage retention. This is more important |
245 |
|
|
* here than in some other data structures because we cannot null |
246 |
|
|
* out node fields referencing user keys since they might still be |
247 |
|
|
* read by other ongoing traversals. |
248 |
|
|
* |
249 |
|
|
* Indexing uses skip list parameters that maintain good search |
250 |
|
|
* performance while using sparser-than-usual indices: The |
251 |
dl |
1.118 |
* hardwired parameters k=1, p=0.5 (see method doPut) mean |
252 |
dl |
1.1 |
* that about one-quarter of the nodes have indices. Of those that |
253 |
|
|
* do, half have one level, a quarter have two, and so on (see |
254 |
|
|
* Pugh's Skip List Cookbook, sec 3.4). The expected total space |
255 |
|
|
* requirement for a map is slightly less than for the current |
256 |
|
|
* implementation of java.util.TreeMap. |
257 |
|
|
* |
258 |
|
|
* Changing the level of the index (i.e, the height of the |
259 |
|
|
* tree-like structure) also uses CAS. The head index has initial |
260 |
|
|
* level/height of one. Creation of an index with height greater |
261 |
|
|
* than the current level adds a level to the head index by |
262 |
|
|
* CAS'ing on a new top-most head. To maintain good performance |
263 |
|
|
* after a lot of removals, deletion methods heuristically try to |
264 |
|
|
* reduce the height if the topmost levels appear to be empty. |
265 |
|
|
* This may encounter races in which it possible (but rare) to |
266 |
|
|
* reduce and "lose" a level just as it is about to contain an |
267 |
|
|
* index (that will then never be encountered). This does no |
268 |
|
|
* structural harm, and in practice appears to be a better option |
269 |
|
|
* than allowing unrestrained growth of levels. |
270 |
|
|
* |
271 |
|
|
* The code for all this is more verbose than you'd like. Most |
272 |
|
|
* operations entail locating an element (or position to insert an |
273 |
|
|
* element). The code to do this can't be nicely factored out |
274 |
|
|
* because subsequent uses require a snapshot of predecessor |
275 |
|
|
* and/or successor and/or value fields which can't be returned |
276 |
|
|
* all at once, at least not without creating yet another object |
277 |
|
|
* to hold them -- creating such little objects is an especially |
278 |
|
|
* bad idea for basic internal search operations because it adds |
279 |
|
|
* to GC overhead. (This is one of the few times I've wished Java |
280 |
|
|
* had macros.) Instead, some traversal code is interleaved within |
281 |
|
|
* insertion and removal operations. The control logic to handle |
282 |
|
|
* all the retry conditions is sometimes twisty. Most search is |
283 |
|
|
* broken into 2 parts. findPredecessor() searches index nodes |
284 |
|
|
* only, returning a base-level predecessor of the key. findNode() |
285 |
|
|
* finishes out the base-level search. Even with this factoring, |
286 |
|
|
* there is a fair amount of near-duplication of code to handle |
287 |
|
|
* variants. |
288 |
|
|
* |
289 |
dl |
1.92 |
* To produce random values without interference across threads, |
290 |
|
|
* we use within-JDK thread local random support (via the |
291 |
|
|
* "secondary seed", to avoid interference with user-level |
292 |
|
|
* ThreadLocalRandom.) |
293 |
|
|
* |
294 |
dl |
1.88 |
* A previous version of this class wrapped non-comparable keys |
295 |
|
|
* with their comparators to emulate Comparables when using |
296 |
dl |
1.118 |
* comparators vs Comparables. However, JVMs now appear to better |
297 |
|
|
* handle infusing comparator-vs-comparable choice into search |
298 |
|
|
* loops. Static method cpr(comparator, x, y) is used for all |
299 |
|
|
* comparisons, which works well as long as the comparator |
300 |
|
|
* argument is set up outside of loops (thus sometimes passed as |
301 |
|
|
* an argument to internal methods) to avoid field re-reads. |
302 |
dl |
1.88 |
* |
303 |
dl |
1.1 |
* For explanation of algorithms sharing at least a couple of |
304 |
|
|
* features with this one, see Mikhail Fomitchev's thesis |
305 |
|
|
* (http://www.cs.yorku.ca/~mikhail/), Keir Fraser's thesis |
306 |
dl |
1.4 |
* (http://www.cl.cam.ac.uk/users/kaf24/), and Hakan Sundell's |
307 |
dl |
1.1 |
* thesis (http://www.cs.chalmers.se/~phs/). |
308 |
|
|
* |
309 |
|
|
* Given the use of tree-like index nodes, you might wonder why |
310 |
|
|
* this doesn't use some kind of search tree instead, which would |
311 |
|
|
* support somewhat faster search operations. The reason is that |
312 |
|
|
* there are no known efficient lock-free insertion and deletion |
313 |
|
|
* algorithms for search trees. The immutability of the "down" |
314 |
|
|
* links of index nodes (as opposed to mutable "left" fields in |
315 |
|
|
* true trees) makes this tractable using only CAS operations. |
316 |
|
|
* |
317 |
|
|
* Notation guide for local variables |
318 |
|
|
* Node: b, n, f for predecessor, node, successor |
319 |
|
|
* Index: q, r, d for index node, right, down. |
320 |
|
|
* t for another index node |
321 |
|
|
* Head: h |
322 |
|
|
* Levels: j |
323 |
|
|
* Keys: k, key |
324 |
|
|
* Values: v, value |
325 |
|
|
* Comparisons: c |
326 |
|
|
*/ |
327 |
|
|
|
328 |
|
|
private static final long serialVersionUID = -8627078645895051609L; |
329 |
|
|
|
330 |
|
|
/** |
331 |
|
|
* Special value used to identify base-level header |
332 |
dl |
1.9 |
*/ |
333 |
dl |
1.1 |
private static final Object BASE_HEADER = new Object(); |
334 |
|
|
|
335 |
|
|
/** |
336 |
dl |
1.9 |
* The topmost head index of the skiplist. |
337 |
dl |
1.1 |
*/ |
338 |
|
|
private transient volatile HeadIndex<K,V> head; |
339 |
|
|
|
340 |
|
|
/** |
341 |
dl |
1.118 |
* The comparator used to maintain order in this map, or null if |
342 |
|
|
* using natural ordering. (Non-private to simplify access in |
343 |
jsr166 |
1.120 |
* nested classes.) |
344 |
dl |
1.1 |
* @serial |
345 |
|
|
*/ |
346 |
dl |
1.118 |
final Comparator<? super K> comparator; |
347 |
dl |
1.1 |
|
348 |
|
|
/** Lazily initialized key set */ |
349 |
dl |
1.118 |
private transient KeySet<K> keySet; |
350 |
dl |
1.1 |
/** Lazily initialized entry set */ |
351 |
jsr166 |
1.71 |
private transient EntrySet<K,V> entrySet; |
352 |
dl |
1.1 |
/** Lazily initialized values collection */ |
353 |
jsr166 |
1.71 |
private transient Values<V> values; |
354 |
dl |
1.1 |
/** Lazily initialized descending key set */ |
355 |
dl |
1.46 |
private transient ConcurrentNavigableMap<K,V> descendingMap; |
356 |
dl |
1.1 |
|
357 |
|
|
/** |
358 |
jsr166 |
1.13 |
* Initializes or resets state. Needed by constructors, clone, |
359 |
dl |
1.1 |
* clear, readObject. and ConcurrentSkipListSet.clone. |
360 |
|
|
* (Note that comparator must be separately initialized.) |
361 |
|
|
*/ |
362 |
dl |
1.118 |
private void initialize() { |
363 |
dl |
1.1 |
keySet = null; |
364 |
dl |
1.9 |
entrySet = null; |
365 |
dl |
1.1 |
values = null; |
366 |
dl |
1.46 |
descendingMap = null; |
367 |
dl |
1.1 |
head = new HeadIndex<K,V>(new Node<K,V>(null, BASE_HEADER, null), |
368 |
|
|
null, null, 1); |
369 |
|
|
} |
370 |
|
|
|
371 |
|
|
/** |
372 |
|
|
* compareAndSet head node |
373 |
|
|
*/ |
374 |
|
|
private boolean casHead(HeadIndex<K,V> cmp, HeadIndex<K,V> val) { |
375 |
jsr166 |
1.141 |
return U.compareAndSwapObject(this, HEAD, cmp, val); |
376 |
dl |
1.1 |
} |
377 |
|
|
|
378 |
|
|
/* ---------------- Nodes -------------- */ |
379 |
|
|
|
380 |
|
|
/** |
381 |
|
|
* Nodes hold keys and values, and are singly linked in sorted |
382 |
|
|
* order, possibly with some intervening marker nodes. The list is |
383 |
|
|
* headed by a dummy node accessible as head.node. The value field |
384 |
|
|
* is declared only as Object because it takes special non-V |
385 |
|
|
* values for marker and header nodes. |
386 |
|
|
*/ |
387 |
|
|
static final class Node<K,V> { |
388 |
|
|
final K key; |
389 |
|
|
volatile Object value; |
390 |
|
|
volatile Node<K,V> next; |
391 |
|
|
|
392 |
|
|
/** |
393 |
|
|
* Creates a new regular node. |
394 |
|
|
*/ |
395 |
|
|
Node(K key, Object value, Node<K,V> next) { |
396 |
|
|
this.key = key; |
397 |
|
|
this.value = value; |
398 |
|
|
this.next = next; |
399 |
|
|
} |
400 |
|
|
|
401 |
|
|
/** |
402 |
|
|
* Creates a new marker node. A marker is distinguished by |
403 |
|
|
* having its value field point to itself. Marker nodes also |
404 |
|
|
* have null keys, a fact that is exploited in a few places, |
405 |
|
|
* but this doesn't distinguish markers from the base-level |
406 |
|
|
* header node (head.node), which also has a null key. |
407 |
|
|
*/ |
408 |
|
|
Node(Node<K,V> next) { |
409 |
|
|
this.key = null; |
410 |
|
|
this.value = this; |
411 |
|
|
this.next = next; |
412 |
|
|
} |
413 |
|
|
|
414 |
|
|
/** |
415 |
|
|
* compareAndSet value field |
416 |
|
|
*/ |
417 |
|
|
boolean casValue(Object cmp, Object val) { |
418 |
jsr166 |
1.141 |
return U.compareAndSwapObject(this, VALUE, cmp, val); |
419 |
dl |
1.1 |
} |
420 |
jsr166 |
1.60 |
|
421 |
dl |
1.1 |
/** |
422 |
|
|
* compareAndSet next field |
423 |
|
|
*/ |
424 |
|
|
boolean casNext(Node<K,V> cmp, Node<K,V> val) { |
425 |
jsr166 |
1.141 |
return U.compareAndSwapObject(this, NEXT, cmp, val); |
426 |
dl |
1.1 |
} |
427 |
|
|
|
428 |
|
|
/** |
429 |
jsr166 |
1.10 |
* Returns true if this node is a marker. This method isn't |
430 |
|
|
* actually called in any current code checking for markers |
431 |
dl |
1.1 |
* because callers will have already read value field and need |
432 |
|
|
* to use that read (not another done here) and so directly |
433 |
|
|
* test if value points to node. |
434 |
jsr166 |
1.93 |
* |
435 |
dl |
1.1 |
* @return true if this node is a marker node |
436 |
|
|
*/ |
437 |
|
|
boolean isMarker() { |
438 |
|
|
return value == this; |
439 |
|
|
} |
440 |
|
|
|
441 |
|
|
/** |
442 |
jsr166 |
1.10 |
* Returns true if this node is the header of base-level list. |
443 |
dl |
1.1 |
* @return true if this node is header node |
444 |
|
|
*/ |
445 |
|
|
boolean isBaseHeader() { |
446 |
|
|
return value == BASE_HEADER; |
447 |
|
|
} |
448 |
|
|
|
449 |
|
|
/** |
450 |
|
|
* Tries to append a deletion marker to this node. |
451 |
|
|
* @param f the assumed current successor of this node |
452 |
|
|
* @return true if successful |
453 |
|
|
*/ |
454 |
|
|
boolean appendMarker(Node<K,V> f) { |
455 |
|
|
return casNext(f, new Node<K,V>(f)); |
456 |
|
|
} |
457 |
|
|
|
458 |
|
|
/** |
459 |
|
|
* Helps out a deletion by appending marker or unlinking from |
460 |
|
|
* predecessor. This is called during traversals when value |
461 |
|
|
* field seen to be null. |
462 |
|
|
* @param b predecessor |
463 |
|
|
* @param f successor |
464 |
|
|
*/ |
465 |
|
|
void helpDelete(Node<K,V> b, Node<K,V> f) { |
466 |
|
|
/* |
467 |
|
|
* Rechecking links and then doing only one of the |
468 |
|
|
* help-out stages per call tends to minimize CAS |
469 |
|
|
* interference among helping threads. |
470 |
|
|
*/ |
471 |
|
|
if (f == next && this == b.next) { |
472 |
|
|
if (f == null || f.value != f) // not already marked |
473 |
dl |
1.118 |
casNext(f, new Node<K,V>(f)); |
474 |
dl |
1.1 |
else |
475 |
|
|
b.casNext(this, f.next); |
476 |
|
|
} |
477 |
|
|
} |
478 |
|
|
|
479 |
|
|
/** |
480 |
jsr166 |
1.11 |
* Returns value if this node contains a valid key-value pair, |
481 |
dl |
1.9 |
* else null. |
482 |
dl |
1.1 |
* @return this node's value if it isn't a marker or header or |
483 |
jsr166 |
1.126 |
* is deleted, else null |
484 |
dl |
1.1 |
*/ |
485 |
|
|
V getValidValue() { |
486 |
|
|
Object v = value; |
487 |
|
|
if (v == this || v == BASE_HEADER) |
488 |
|
|
return null; |
489 |
dl |
1.118 |
@SuppressWarnings("unchecked") V vv = (V)v; |
490 |
|
|
return vv; |
491 |
dl |
1.1 |
} |
492 |
|
|
|
493 |
|
|
/** |
494 |
jsr166 |
1.10 |
* Creates and returns a new SimpleImmutableEntry holding current |
495 |
|
|
* mapping if this node holds a valid value, else null. |
496 |
dl |
1.1 |
* @return new entry or null |
497 |
|
|
*/ |
498 |
dl |
1.2 |
AbstractMap.SimpleImmutableEntry<K,V> createSnapshot() { |
499 |
dl |
1.118 |
Object v = value; |
500 |
|
|
if (v == null || v == this || v == BASE_HEADER) |
501 |
dl |
1.1 |
return null; |
502 |
dl |
1.118 |
@SuppressWarnings("unchecked") V vv = (V)v; |
503 |
|
|
return new AbstractMap.SimpleImmutableEntry<K,V>(key, vv); |
504 |
dl |
1.1 |
} |
505 |
dl |
1.59 |
|
506 |
jsr166 |
1.141 |
// Unsafe mechanics |
507 |
dl |
1.65 |
|
508 |
jsr166 |
1.141 |
private static final sun.misc.Unsafe U = sun.misc.Unsafe.getUnsafe(); |
509 |
|
|
private static final long VALUE; |
510 |
|
|
private static final long NEXT; |
511 |
dl |
1.59 |
|
512 |
dl |
1.65 |
static { |
513 |
|
|
try { |
514 |
jsr166 |
1.141 |
VALUE = U.objectFieldOffset |
515 |
|
|
(Node.class.getDeclaredField("value")); |
516 |
|
|
NEXT = U.objectFieldOffset |
517 |
|
|
(Node.class.getDeclaredField("next")); |
518 |
jsr166 |
1.140 |
} catch (ReflectiveOperationException e) { |
519 |
dl |
1.65 |
throw new Error(e); |
520 |
|
|
} |
521 |
|
|
} |
522 |
dl |
1.1 |
} |
523 |
|
|
|
524 |
|
|
/* ---------------- Indexing -------------- */ |
525 |
|
|
|
526 |
|
|
/** |
527 |
dl |
1.40 |
* Index nodes represent the levels of the skip list. Note that |
528 |
|
|
* even though both Nodes and Indexes have forward-pointing |
529 |
|
|
* fields, they have different types and are handled in different |
530 |
|
|
* ways, that can't nicely be captured by placing field in a |
531 |
|
|
* shared abstract class. |
532 |
dl |
1.1 |
*/ |
533 |
|
|
static class Index<K,V> { |
534 |
|
|
final Node<K,V> node; |
535 |
|
|
final Index<K,V> down; |
536 |
|
|
volatile Index<K,V> right; |
537 |
|
|
|
538 |
|
|
/** |
539 |
jsr166 |
1.10 |
* Creates index node with given values. |
540 |
dl |
1.9 |
*/ |
541 |
dl |
1.1 |
Index(Node<K,V> node, Index<K,V> down, Index<K,V> right) { |
542 |
|
|
this.node = node; |
543 |
|
|
this.down = down; |
544 |
|
|
this.right = right; |
545 |
|
|
} |
546 |
|
|
|
547 |
|
|
/** |
548 |
|
|
* compareAndSet right field |
549 |
|
|
*/ |
550 |
|
|
final boolean casRight(Index<K,V> cmp, Index<K,V> val) { |
551 |
jsr166 |
1.141 |
return U.compareAndSwapObject(this, RIGHT, cmp, val); |
552 |
dl |
1.1 |
} |
553 |
|
|
|
554 |
|
|
/** |
555 |
|
|
* Returns true if the node this indexes has been deleted. |
556 |
|
|
* @return true if indexed node is known to be deleted |
557 |
|
|
*/ |
558 |
|
|
final boolean indexesDeletedNode() { |
559 |
|
|
return node.value == null; |
560 |
|
|
} |
561 |
|
|
|
562 |
|
|
/** |
563 |
|
|
* Tries to CAS newSucc as successor. To minimize races with |
564 |
|
|
* unlink that may lose this index node, if the node being |
565 |
|
|
* indexed is known to be deleted, it doesn't try to link in. |
566 |
|
|
* @param succ the expected current successor |
567 |
|
|
* @param newSucc the new successor |
568 |
|
|
* @return true if successful |
569 |
|
|
*/ |
570 |
|
|
final boolean link(Index<K,V> succ, Index<K,V> newSucc) { |
571 |
|
|
Node<K,V> n = node; |
572 |
dl |
1.9 |
newSucc.right = succ; |
573 |
dl |
1.1 |
return n.value != null && casRight(succ, newSucc); |
574 |
|
|
} |
575 |
|
|
|
576 |
|
|
/** |
577 |
|
|
* Tries to CAS right field to skip over apparent successor |
578 |
|
|
* succ. Fails (forcing a retraversal by caller) if this node |
579 |
|
|
* is known to be deleted. |
580 |
|
|
* @param succ the expected current successor |
581 |
|
|
* @return true if successful |
582 |
|
|
*/ |
583 |
|
|
final boolean unlink(Index<K,V> succ) { |
584 |
dl |
1.118 |
return node.value != null && casRight(succ, succ.right); |
585 |
dl |
1.1 |
} |
586 |
dl |
1.59 |
|
587 |
|
|
// Unsafe mechanics |
588 |
jsr166 |
1.141 |
private static final sun.misc.Unsafe U = sun.misc.Unsafe.getUnsafe(); |
589 |
|
|
private static final long RIGHT; |
590 |
dl |
1.65 |
static { |
591 |
|
|
try { |
592 |
jsr166 |
1.141 |
RIGHT = U.objectFieldOffset |
593 |
|
|
(Index.class.getDeclaredField("right")); |
594 |
jsr166 |
1.140 |
} catch (ReflectiveOperationException e) { |
595 |
dl |
1.65 |
throw new Error(e); |
596 |
|
|
} |
597 |
|
|
} |
598 |
dl |
1.1 |
} |
599 |
|
|
|
600 |
|
|
/* ---------------- Head nodes -------------- */ |
601 |
|
|
|
602 |
|
|
/** |
603 |
|
|
* Nodes heading each level keep track of their level. |
604 |
|
|
*/ |
605 |
|
|
static final class HeadIndex<K,V> extends Index<K,V> { |
606 |
|
|
final int level; |
607 |
|
|
HeadIndex(Node<K,V> node, Index<K,V> down, Index<K,V> right, int level) { |
608 |
|
|
super(node, down, right); |
609 |
|
|
this.level = level; |
610 |
|
|
} |
611 |
dl |
1.9 |
} |
612 |
dl |
1.1 |
|
613 |
|
|
/* ---------------- Comparison utilities -------------- */ |
614 |
|
|
|
615 |
|
|
/** |
616 |
dl |
1.118 |
* Compares using comparator or natural ordering if null. |
617 |
|
|
* Called only by methods that have performed required type checks. |
618 |
dl |
1.1 |
*/ |
619 |
dl |
1.118 |
@SuppressWarnings({"unchecked", "rawtypes"}) |
620 |
|
|
static final int cpr(Comparator c, Object x, Object y) { |
621 |
|
|
return (c != null) ? c.compare(x, y) : ((Comparable)x).compareTo(y); |
622 |
dl |
1.1 |
} |
623 |
|
|
|
624 |
|
|
/* ---------------- Traversal -------------- */ |
625 |
|
|
|
626 |
|
|
/** |
627 |
jsr166 |
1.10 |
* Returns a base-level node with key strictly less than given key, |
628 |
dl |
1.1 |
* or the base-level header if there is no such node. Also |
629 |
|
|
* unlinks indexes to deleted nodes found along the way. Callers |
630 |
|
|
* rely on this side-effect of clearing indices to deleted nodes. |
631 |
|
|
* @param key the key |
632 |
dl |
1.9 |
* @return a predecessor of key |
633 |
dl |
1.1 |
*/ |
634 |
dl |
1.118 |
private Node<K,V> findPredecessor(Object key, Comparator<? super K> cmp) { |
635 |
jsr166 |
1.41 |
if (key == null) |
636 |
dl |
1.40 |
throw new NullPointerException(); // don't postpone errors |
637 |
dl |
1.1 |
for (;;) { |
638 |
dl |
1.118 |
for (Index<K,V> q = head, r = q.right, d;;) { |
639 |
dl |
1.40 |
if (r != null) { |
640 |
|
|
Node<K,V> n = r.node; |
641 |
|
|
K k = n.key; |
642 |
|
|
if (n.value == null) { |
643 |
|
|
if (!q.unlink(r)) |
644 |
|
|
break; // restart |
645 |
|
|
r = q.right; // reread r |
646 |
|
|
continue; |
647 |
dl |
1.1 |
} |
648 |
dl |
1.118 |
if (cpr(cmp, key, k) > 0) { |
649 |
dl |
1.1 |
q = r; |
650 |
dl |
1.40 |
r = r.right; |
651 |
dl |
1.1 |
continue; |
652 |
|
|
} |
653 |
|
|
} |
654 |
dl |
1.118 |
if ((d = q.down) == null) |
655 |
dl |
1.1 |
return q.node; |
656 |
dl |
1.118 |
q = d; |
657 |
|
|
r = d.right; |
658 |
dl |
1.1 |
} |
659 |
|
|
} |
660 |
|
|
} |
661 |
|
|
|
662 |
|
|
/** |
663 |
jsr166 |
1.10 |
* Returns node holding key or null if no such, clearing out any |
664 |
dl |
1.1 |
* deleted nodes seen along the way. Repeatedly traverses at |
665 |
|
|
* base-level looking for key starting at predecessor returned |
666 |
|
|
* from findPredecessor, processing base-level deletions as |
667 |
|
|
* encountered. Some callers rely on this side-effect of clearing |
668 |
|
|
* deleted nodes. |
669 |
|
|
* |
670 |
|
|
* Restarts occur, at traversal step centered on node n, if: |
671 |
|
|
* |
672 |
|
|
* (1) After reading n's next field, n is no longer assumed |
673 |
|
|
* predecessor b's current successor, which means that |
674 |
|
|
* we don't have a consistent 3-node snapshot and so cannot |
675 |
|
|
* unlink any subsequent deleted nodes encountered. |
676 |
|
|
* |
677 |
|
|
* (2) n's value field is null, indicating n is deleted, in |
678 |
|
|
* which case we help out an ongoing structural deletion |
679 |
|
|
* before retrying. Even though there are cases where such |
680 |
|
|
* unlinking doesn't require restart, they aren't sorted out |
681 |
|
|
* here because doing so would not usually outweigh cost of |
682 |
|
|
* restarting. |
683 |
|
|
* |
684 |
dl |
1.9 |
* (3) n is a marker or n's predecessor's value field is null, |
685 |
dl |
1.1 |
* indicating (among other possibilities) that |
686 |
|
|
* findPredecessor returned a deleted node. We can't unlink |
687 |
|
|
* the node because we don't know its predecessor, so rely |
688 |
|
|
* on another call to findPredecessor to notice and return |
689 |
|
|
* some earlier predecessor, which it will do. This check is |
690 |
|
|
* only strictly needed at beginning of loop, (and the |
691 |
|
|
* b.value check isn't strictly needed at all) but is done |
692 |
|
|
* each iteration to help avoid contention with other |
693 |
|
|
* threads by callers that will fail to be able to change |
694 |
|
|
* links, and so will retry anyway. |
695 |
|
|
* |
696 |
|
|
* The traversal loops in doPut, doRemove, and findNear all |
697 |
|
|
* include the same three kinds of checks. And specialized |
698 |
dl |
1.31 |
* versions appear in findFirst, and findLast and their |
699 |
|
|
* variants. They can't easily share code because each uses the |
700 |
dl |
1.1 |
* reads of fields held in locals occurring in the orders they |
701 |
|
|
* were performed. |
702 |
dl |
1.9 |
* |
703 |
dl |
1.1 |
* @param key the key |
704 |
jsr166 |
1.22 |
* @return node holding key, or null if no such |
705 |
dl |
1.1 |
*/ |
706 |
dl |
1.118 |
private Node<K,V> findNode(Object key) { |
707 |
dl |
1.88 |
if (key == null) |
708 |
|
|
throw new NullPointerException(); // don't postpone errors |
709 |
dl |
1.118 |
Comparator<? super K> cmp = comparator; |
710 |
|
|
outer: for (;;) { |
711 |
|
|
for (Node<K,V> b = findPredecessor(key, cmp), n = b.next;;) { |
712 |
|
|
Object v; int c; |
713 |
dl |
1.9 |
if (n == null) |
714 |
dl |
1.118 |
break outer; |
715 |
dl |
1.1 |
Node<K,V> f = n.next; |
716 |
|
|
if (n != b.next) // inconsistent read |
717 |
|
|
break; |
718 |
dl |
1.118 |
if ((v = n.value) == null) { // n is deleted |
719 |
dl |
1.1 |
n.helpDelete(b, f); |
720 |
|
|
break; |
721 |
|
|
} |
722 |
dl |
1.118 |
if (b.value == null || v == n) // b is deleted |
723 |
dl |
1.1 |
break; |
724 |
dl |
1.118 |
if ((c = cpr(cmp, key, n.key)) == 0) |
725 |
dl |
1.40 |
return n; |
726 |
dl |
1.1 |
if (c < 0) |
727 |
dl |
1.118 |
break outer; |
728 |
dl |
1.1 |
b = n; |
729 |
|
|
n = f; |
730 |
|
|
} |
731 |
|
|
} |
732 |
dl |
1.118 |
return null; |
733 |
dl |
1.1 |
} |
734 |
|
|
|
735 |
dl |
1.9 |
/** |
736 |
dl |
1.88 |
* Gets value for key. Almost the same as findNode, but returns |
737 |
|
|
* the found value (to avoid retries during re-reads) |
738 |
|
|
* |
739 |
dl |
1.118 |
* @param key the key |
740 |
dl |
1.1 |
* @return the value, or null if absent |
741 |
|
|
*/ |
742 |
dl |
1.118 |
private V doGet(Object key) { |
743 |
|
|
if (key == null) |
744 |
dl |
1.88 |
throw new NullPointerException(); |
745 |
dl |
1.118 |
Comparator<? super K> cmp = comparator; |
746 |
|
|
outer: for (;;) { |
747 |
|
|
for (Node<K,V> b = findPredecessor(key, cmp), n = b.next;;) { |
748 |
|
|
Object v; int c; |
749 |
dl |
1.88 |
if (n == null) |
750 |
dl |
1.118 |
break outer; |
751 |
dl |
1.88 |
Node<K,V> f = n.next; |
752 |
|
|
if (n != b.next) // inconsistent read |
753 |
|
|
break; |
754 |
dl |
1.118 |
if ((v = n.value) == null) { // n is deleted |
755 |
dl |
1.88 |
n.helpDelete(b, f); |
756 |
|
|
break; |
757 |
|
|
} |
758 |
dl |
1.118 |
if (b.value == null || v == n) // b is deleted |
759 |
dl |
1.88 |
break; |
760 |
dl |
1.118 |
if ((c = cpr(cmp, key, n.key)) == 0) { |
761 |
|
|
@SuppressWarnings("unchecked") V vv = (V)v; |
762 |
dl |
1.100 |
return vv; |
763 |
dl |
1.118 |
} |
764 |
dl |
1.88 |
if (c < 0) |
765 |
dl |
1.118 |
break outer; |
766 |
dl |
1.88 |
b = n; |
767 |
|
|
n = f; |
768 |
|
|
} |
769 |
dl |
1.1 |
} |
770 |
dl |
1.118 |
return null; |
771 |
dl |
1.1 |
} |
772 |
|
|
|
773 |
|
|
/* ---------------- Insertion -------------- */ |
774 |
|
|
|
775 |
|
|
/** |
776 |
|
|
* Main insertion method. Adds element if not present, or |
777 |
|
|
* replaces value if present and onlyIfAbsent is false. |
778 |
dl |
1.118 |
* @param key the key |
779 |
jsr166 |
1.103 |
* @param value the value that must be associated with key |
780 |
dl |
1.1 |
* @param onlyIfAbsent if should not insert if already present |
781 |
|
|
* @return the old value, or null if newly inserted |
782 |
|
|
*/ |
783 |
dl |
1.118 |
private V doPut(K key, V value, boolean onlyIfAbsent) { |
784 |
|
|
Node<K,V> z; // added node |
785 |
|
|
if (key == null) |
786 |
dl |
1.88 |
throw new NullPointerException(); |
787 |
dl |
1.118 |
Comparator<? super K> cmp = comparator; |
788 |
|
|
outer: for (;;) { |
789 |
|
|
for (Node<K,V> b = findPredecessor(key, cmp), n = b.next;;) { |
790 |
dl |
1.1 |
if (n != null) { |
791 |
dl |
1.118 |
Object v; int c; |
792 |
dl |
1.1 |
Node<K,V> f = n.next; |
793 |
|
|
if (n != b.next) // inconsistent read |
794 |
jsr166 |
1.57 |
break; |
795 |
dl |
1.118 |
if ((v = n.value) == null) { // n is deleted |
796 |
dl |
1.1 |
n.helpDelete(b, f); |
797 |
|
|
break; |
798 |
|
|
} |
799 |
dl |
1.118 |
if (b.value == null || v == n) // b is deleted |
800 |
dl |
1.1 |
break; |
801 |
dl |
1.118 |
if ((c = cpr(cmp, key, n.key)) > 0) { |
802 |
dl |
1.1 |
b = n; |
803 |
|
|
n = f; |
804 |
|
|
continue; |
805 |
|
|
} |
806 |
|
|
if (c == 0) { |
807 |
dl |
1.118 |
if (onlyIfAbsent || n.casValue(v, value)) { |
808 |
|
|
@SuppressWarnings("unchecked") V vv = (V)v; |
809 |
dl |
1.100 |
return vv; |
810 |
dl |
1.118 |
} |
811 |
|
|
break; // restart if lost race to replace value |
812 |
dl |
1.1 |
} |
813 |
|
|
// else c < 0; fall through |
814 |
|
|
} |
815 |
dl |
1.9 |
|
816 |
dl |
1.118 |
z = new Node<K,V>(key, value, n); |
817 |
dl |
1.9 |
if (!b.casNext(n, z)) |
818 |
dl |
1.1 |
break; // restart if lost race to append to b |
819 |
dl |
1.118 |
break outer; |
820 |
dl |
1.1 |
} |
821 |
|
|
} |
822 |
|
|
|
823 |
dl |
1.122 |
int rnd = ThreadLocalRandom.nextSecondarySeed(); |
824 |
dl |
1.92 |
if ((rnd & 0x80000001) == 0) { // test highest and lowest bits |
825 |
|
|
int level = 1, max; |
826 |
|
|
while (((rnd >>>= 1) & 1) != 0) |
827 |
|
|
++level; |
828 |
dl |
1.1 |
Index<K,V> idx = null; |
829 |
dl |
1.92 |
HeadIndex<K,V> h = head; |
830 |
|
|
if (level <= (max = h.level)) { |
831 |
|
|
for (int i = 1; i <= level; ++i) |
832 |
|
|
idx = new Index<K,V>(z, idx, null); |
833 |
dl |
1.1 |
} |
834 |
dl |
1.92 |
else { // try to grow by one level |
835 |
|
|
level = max + 1; // hold in array and later pick the one to use |
836 |
dl |
1.118 |
@SuppressWarnings("unchecked")Index<K,V>[] idxs = |
837 |
|
|
(Index<K,V>[])new Index<?,?>[level+1]; |
838 |
dl |
1.92 |
for (int i = 1; i <= level; ++i) |
839 |
|
|
idxs[i] = idx = new Index<K,V>(z, idx, null); |
840 |
|
|
for (;;) { |
841 |
|
|
h = head; |
842 |
|
|
int oldLevel = h.level; |
843 |
|
|
if (level <= oldLevel) // lost race to add level |
844 |
|
|
break; |
845 |
|
|
HeadIndex<K,V> newh = h; |
846 |
|
|
Node<K,V> oldbase = h.node; |
847 |
|
|
for (int j = oldLevel+1; j <= level; ++j) |
848 |
|
|
newh = new HeadIndex<K,V>(oldbase, newh, idxs[j], j); |
849 |
|
|
if (casHead(h, newh)) { |
850 |
|
|
h = newh; |
851 |
|
|
idx = idxs[level = oldLevel]; |
852 |
|
|
break; |
853 |
dl |
1.1 |
} |
854 |
|
|
} |
855 |
dl |
1.92 |
} |
856 |
dl |
1.118 |
// find insertion points and splice in |
857 |
|
|
splice: for (int insertionLevel = level;;) { |
858 |
dl |
1.92 |
int j = h.level; |
859 |
dl |
1.118 |
for (Index<K,V> q = h, r = q.right, t = idx;;) { |
860 |
dl |
1.92 |
if (q == null || t == null) |
861 |
dl |
1.118 |
break splice; |
862 |
dl |
1.92 |
if (r != null) { |
863 |
|
|
Node<K,V> n = r.node; |
864 |
|
|
// compare before deletion check avoids needing recheck |
865 |
dl |
1.118 |
int c = cpr(cmp, key, n.key); |
866 |
dl |
1.92 |
if (n.value == null) { |
867 |
|
|
if (!q.unlink(r)) |
868 |
|
|
break; |
869 |
|
|
r = q.right; |
870 |
|
|
continue; |
871 |
|
|
} |
872 |
|
|
if (c > 0) { |
873 |
|
|
q = r; |
874 |
|
|
r = r.right; |
875 |
|
|
continue; |
876 |
|
|
} |
877 |
dl |
1.1 |
} |
878 |
dl |
1.92 |
|
879 |
|
|
if (j == insertionLevel) { |
880 |
|
|
if (!q.link(r, t)) |
881 |
|
|
break; // restart |
882 |
|
|
if (t.node.value == null) { |
883 |
dl |
1.118 |
findNode(key); |
884 |
|
|
break splice; |
885 |
dl |
1.88 |
} |
886 |
dl |
1.92 |
if (--insertionLevel == 0) |
887 |
dl |
1.118 |
break splice; |
888 |
dl |
1.1 |
} |
889 |
dl |
1.92 |
|
890 |
|
|
if (--j >= insertionLevel && j < level) |
891 |
|
|
t = t.down; |
892 |
|
|
q = q.down; |
893 |
|
|
r = q.right; |
894 |
dl |
1.1 |
} |
895 |
|
|
} |
896 |
|
|
} |
897 |
dl |
1.118 |
return null; |
898 |
dl |
1.1 |
} |
899 |
|
|
|
900 |
|
|
/* ---------------- Deletion -------------- */ |
901 |
|
|
|
902 |
|
|
/** |
903 |
|
|
* Main deletion method. Locates node, nulls value, appends a |
904 |
|
|
* deletion marker, unlinks predecessor, removes associated index |
905 |
|
|
* nodes, and possibly reduces head index level. |
906 |
|
|
* |
907 |
|
|
* Index nodes are cleared out simply by calling findPredecessor. |
908 |
|
|
* which unlinks indexes to deleted nodes found along path to key, |
909 |
|
|
* which will include the indexes to this node. This is done |
910 |
|
|
* unconditionally. We can't check beforehand whether there are |
911 |
|
|
* index nodes because it might be the case that some or all |
912 |
|
|
* indexes hadn't been inserted yet for this node during initial |
913 |
|
|
* search for it, and we'd like to ensure lack of garbage |
914 |
dl |
1.9 |
* retention, so must call to be sure. |
915 |
dl |
1.1 |
* |
916 |
dl |
1.118 |
* @param key the key |
917 |
dl |
1.1 |
* @param value if non-null, the value that must be |
918 |
|
|
* associated with key |
919 |
|
|
* @return the node, or null if not found |
920 |
|
|
*/ |
921 |
dl |
1.118 |
final V doRemove(Object key, Object value) { |
922 |
|
|
if (key == null) |
923 |
dl |
1.88 |
throw new NullPointerException(); |
924 |
dl |
1.118 |
Comparator<? super K> cmp = comparator; |
925 |
|
|
outer: for (;;) { |
926 |
|
|
for (Node<K,V> b = findPredecessor(key, cmp), n = b.next;;) { |
927 |
|
|
Object v; int c; |
928 |
dl |
1.9 |
if (n == null) |
929 |
dl |
1.118 |
break outer; |
930 |
dl |
1.1 |
Node<K,V> f = n.next; |
931 |
|
|
if (n != b.next) // inconsistent read |
932 |
|
|
break; |
933 |
dl |
1.118 |
if ((v = n.value) == null) { // n is deleted |
934 |
dl |
1.1 |
n.helpDelete(b, f); |
935 |
|
|
break; |
936 |
|
|
} |
937 |
dl |
1.118 |
if (b.value == null || v == n) // b is deleted |
938 |
dl |
1.1 |
break; |
939 |
dl |
1.118 |
if ((c = cpr(cmp, key, n.key)) < 0) |
940 |
|
|
break outer; |
941 |
dl |
1.1 |
if (c > 0) { |
942 |
|
|
b = n; |
943 |
|
|
n = f; |
944 |
|
|
continue; |
945 |
|
|
} |
946 |
dl |
1.9 |
if (value != null && !value.equals(v)) |
947 |
dl |
1.118 |
break outer; |
948 |
dl |
1.9 |
if (!n.casValue(v, null)) |
949 |
dl |
1.1 |
break; |
950 |
dl |
1.9 |
if (!n.appendMarker(f) || !b.casNext(n, f)) |
951 |
dl |
1.118 |
findNode(key); // retry via findNode |
952 |
dl |
1.1 |
else { |
953 |
dl |
1.118 |
findPredecessor(key, cmp); // clean index |
954 |
dl |
1.9 |
if (head.right == null) |
955 |
dl |
1.1 |
tryReduceLevel(); |
956 |
|
|
} |
957 |
dl |
1.100 |
@SuppressWarnings("unchecked") V vv = (V)v; |
958 |
|
|
return vv; |
959 |
dl |
1.1 |
} |
960 |
|
|
} |
961 |
dl |
1.118 |
return null; |
962 |
dl |
1.1 |
} |
963 |
|
|
|
964 |
|
|
/** |
965 |
|
|
* Possibly reduce head level if it has no nodes. This method can |
966 |
|
|
* (rarely) make mistakes, in which case levels can disappear even |
967 |
|
|
* though they are about to contain index nodes. This impacts |
968 |
|
|
* performance, not correctness. To minimize mistakes as well as |
969 |
|
|
* to reduce hysteresis, the level is reduced by one only if the |
970 |
|
|
* topmost three levels look empty. Also, if the removed level |
971 |
|
|
* looks non-empty after CAS, we try to change it back quick |
972 |
|
|
* before anyone notices our mistake! (This trick works pretty |
973 |
|
|
* well because this method will practically never make mistakes |
974 |
|
|
* unless current thread stalls immediately before first CAS, in |
975 |
|
|
* which case it is very unlikely to stall again immediately |
976 |
|
|
* afterwards, so will recover.) |
977 |
|
|
* |
978 |
|
|
* We put up with all this rather than just let levels grow |
979 |
|
|
* because otherwise, even a small map that has undergone a large |
980 |
|
|
* number of insertions and removals will have a lot of levels, |
981 |
|
|
* slowing down access more than would an occasional unwanted |
982 |
|
|
* reduction. |
983 |
|
|
*/ |
984 |
|
|
private void tryReduceLevel() { |
985 |
|
|
HeadIndex<K,V> h = head; |
986 |
|
|
HeadIndex<K,V> d; |
987 |
|
|
HeadIndex<K,V> e; |
988 |
|
|
if (h.level > 3 && |
989 |
dl |
1.9 |
(d = (HeadIndex<K,V>)h.down) != null && |
990 |
|
|
(e = (HeadIndex<K,V>)d.down) != null && |
991 |
|
|
e.right == null && |
992 |
|
|
d.right == null && |
993 |
dl |
1.1 |
h.right == null && |
994 |
|
|
casHead(h, d) && // try to set |
995 |
|
|
h.right != null) // recheck |
996 |
|
|
casHead(d, h); // try to backout |
997 |
|
|
} |
998 |
|
|
|
999 |
|
|
/* ---------------- Finding and removing first element -------------- */ |
1000 |
|
|
|
1001 |
|
|
/** |
1002 |
jsr166 |
1.22 |
* Specialized variant of findNode to get first valid node. |
1003 |
dl |
1.1 |
* @return first node or null if empty |
1004 |
|
|
*/ |
1005 |
dl |
1.118 |
final Node<K,V> findFirst() { |
1006 |
|
|
for (Node<K,V> b, n;;) { |
1007 |
|
|
if ((n = (b = head.node).next) == null) |
1008 |
dl |
1.1 |
return null; |
1009 |
dl |
1.9 |
if (n.value != null) |
1010 |
dl |
1.1 |
return n; |
1011 |
|
|
n.helpDelete(b, n.next); |
1012 |
|
|
} |
1013 |
|
|
} |
1014 |
|
|
|
1015 |
|
|
/** |
1016 |
dl |
1.25 |
* Removes first entry; returns its snapshot. |
1017 |
jsr166 |
1.28 |
* @return null if empty, else snapshot of first entry |
1018 |
dl |
1.1 |
*/ |
1019 |
dl |
1.118 |
private Map.Entry<K,V> doRemoveFirstEntry() { |
1020 |
|
|
for (Node<K,V> b, n;;) { |
1021 |
|
|
if ((n = (b = head.node).next) == null) |
1022 |
dl |
1.1 |
return null; |
1023 |
|
|
Node<K,V> f = n.next; |
1024 |
|
|
if (n != b.next) |
1025 |
|
|
continue; |
1026 |
|
|
Object v = n.value; |
1027 |
|
|
if (v == null) { |
1028 |
|
|
n.helpDelete(b, f); |
1029 |
|
|
continue; |
1030 |
|
|
} |
1031 |
|
|
if (!n.casValue(v, null)) |
1032 |
|
|
continue; |
1033 |
|
|
if (!n.appendMarker(f) || !b.casNext(n, f)) |
1034 |
|
|
findFirst(); // retry |
1035 |
|
|
clearIndexToFirst(); |
1036 |
dl |
1.100 |
@SuppressWarnings("unchecked") V vv = (V)v; |
1037 |
|
|
return new AbstractMap.SimpleImmutableEntry<K,V>(n.key, vv); |
1038 |
jsr166 |
1.55 |
} |
1039 |
dl |
1.1 |
} |
1040 |
|
|
|
1041 |
|
|
/** |
1042 |
jsr166 |
1.10 |
* Clears out index nodes associated with deleted first entry. |
1043 |
dl |
1.1 |
*/ |
1044 |
|
|
private void clearIndexToFirst() { |
1045 |
|
|
for (;;) { |
1046 |
dl |
1.118 |
for (Index<K,V> q = head;;) { |
1047 |
dl |
1.1 |
Index<K,V> r = q.right; |
1048 |
|
|
if (r != null && r.indexesDeletedNode() && !q.unlink(r)) |
1049 |
dl |
1.9 |
break; |
1050 |
dl |
1.1 |
if ((q = q.down) == null) { |
1051 |
dl |
1.9 |
if (head.right == null) |
1052 |
dl |
1.1 |
tryReduceLevel(); |
1053 |
|
|
return; |
1054 |
|
|
} |
1055 |
|
|
} |
1056 |
|
|
} |
1057 |
|
|
} |
1058 |
|
|
|
1059 |
dl |
1.88 |
/** |
1060 |
|
|
* Removes last entry; returns its snapshot. |
1061 |
|
|
* Specialized variant of doRemove. |
1062 |
|
|
* @return null if empty, else snapshot of last entry |
1063 |
|
|
*/ |
1064 |
dl |
1.118 |
private Map.Entry<K,V> doRemoveLastEntry() { |
1065 |
dl |
1.88 |
for (;;) { |
1066 |
|
|
Node<K,V> b = findPredecessorOfLast(); |
1067 |
|
|
Node<K,V> n = b.next; |
1068 |
|
|
if (n == null) { |
1069 |
|
|
if (b.isBaseHeader()) // empty |
1070 |
|
|
return null; |
1071 |
|
|
else |
1072 |
|
|
continue; // all b's successors are deleted; retry |
1073 |
|
|
} |
1074 |
|
|
for (;;) { |
1075 |
|
|
Node<K,V> f = n.next; |
1076 |
|
|
if (n != b.next) // inconsistent read |
1077 |
|
|
break; |
1078 |
|
|
Object v = n.value; |
1079 |
|
|
if (v == null) { // n is deleted |
1080 |
|
|
n.helpDelete(b, f); |
1081 |
|
|
break; |
1082 |
|
|
} |
1083 |
dl |
1.118 |
if (b.value == null || v == n) // b is deleted |
1084 |
dl |
1.88 |
break; |
1085 |
|
|
if (f != null) { |
1086 |
|
|
b = n; |
1087 |
|
|
n = f; |
1088 |
|
|
continue; |
1089 |
|
|
} |
1090 |
|
|
if (!n.casValue(v, null)) |
1091 |
|
|
break; |
1092 |
|
|
K key = n.key; |
1093 |
dl |
1.118 |
if (!n.appendMarker(f) || !b.casNext(n, f)) |
1094 |
|
|
findNode(key); // retry via findNode |
1095 |
|
|
else { // clean index |
1096 |
|
|
findPredecessor(key, comparator); |
1097 |
dl |
1.88 |
if (head.right == null) |
1098 |
|
|
tryReduceLevel(); |
1099 |
|
|
} |
1100 |
dl |
1.118 |
@SuppressWarnings("unchecked") V vv = (V)v; |
1101 |
|
|
return new AbstractMap.SimpleImmutableEntry<K,V>(key, vv); |
1102 |
dl |
1.88 |
} |
1103 |
|
|
} |
1104 |
|
|
} |
1105 |
dl |
1.1 |
|
1106 |
|
|
/* ---------------- Finding and removing last element -------------- */ |
1107 |
|
|
|
1108 |
|
|
/** |
1109 |
jsr166 |
1.10 |
* Specialized version of find to get last valid node. |
1110 |
dl |
1.1 |
* @return last node or null if empty |
1111 |
|
|
*/ |
1112 |
dl |
1.118 |
final Node<K,V> findLast() { |
1113 |
dl |
1.1 |
/* |
1114 |
|
|
* findPredecessor can't be used to traverse index level |
1115 |
|
|
* because this doesn't use comparisons. So traversals of |
1116 |
|
|
* both levels are folded together. |
1117 |
|
|
*/ |
1118 |
|
|
Index<K,V> q = head; |
1119 |
|
|
for (;;) { |
1120 |
|
|
Index<K,V> d, r; |
1121 |
|
|
if ((r = q.right) != null) { |
1122 |
|
|
if (r.indexesDeletedNode()) { |
1123 |
|
|
q.unlink(r); |
1124 |
|
|
q = head; // restart |
1125 |
dl |
1.9 |
} |
1126 |
dl |
1.1 |
else |
1127 |
|
|
q = r; |
1128 |
|
|
} else if ((d = q.down) != null) { |
1129 |
|
|
q = d; |
1130 |
|
|
} else { |
1131 |
dl |
1.118 |
for (Node<K,V> b = q.node, n = b.next;;) { |
1132 |
dl |
1.9 |
if (n == null) |
1133 |
jsr166 |
1.61 |
return b.isBaseHeader() ? null : b; |
1134 |
dl |
1.1 |
Node<K,V> f = n.next; // inconsistent read |
1135 |
|
|
if (n != b.next) |
1136 |
|
|
break; |
1137 |
|
|
Object v = n.value; |
1138 |
|
|
if (v == null) { // n is deleted |
1139 |
|
|
n.helpDelete(b, f); |
1140 |
|
|
break; |
1141 |
|
|
} |
1142 |
dl |
1.118 |
if (b.value == null || v == n) // b is deleted |
1143 |
dl |
1.1 |
break; |
1144 |
|
|
b = n; |
1145 |
|
|
n = f; |
1146 |
|
|
} |
1147 |
|
|
q = head; // restart |
1148 |
|
|
} |
1149 |
|
|
} |
1150 |
|
|
} |
1151 |
|
|
|
1152 |
dl |
1.31 |
/** |
1153 |
jsr166 |
1.32 |
* Specialized variant of findPredecessor to get predecessor of last |
1154 |
|
|
* valid node. Needed when removing the last entry. It is possible |
1155 |
|
|
* that all successors of returned node will have been deleted upon |
1156 |
dl |
1.31 |
* return, in which case this method can be retried. |
1157 |
|
|
* @return likely predecessor of last node |
1158 |
|
|
*/ |
1159 |
|
|
private Node<K,V> findPredecessorOfLast() { |
1160 |
|
|
for (;;) { |
1161 |
dl |
1.118 |
for (Index<K,V> q = head;;) { |
1162 |
dl |
1.31 |
Index<K,V> d, r; |
1163 |
|
|
if ((r = q.right) != null) { |
1164 |
|
|
if (r.indexesDeletedNode()) { |
1165 |
|
|
q.unlink(r); |
1166 |
|
|
break; // must restart |
1167 |
|
|
} |
1168 |
|
|
// proceed as far across as possible without overshooting |
1169 |
|
|
if (r.node.next != null) { |
1170 |
|
|
q = r; |
1171 |
|
|
continue; |
1172 |
|
|
} |
1173 |
|
|
} |
1174 |
|
|
if ((d = q.down) != null) |
1175 |
|
|
q = d; |
1176 |
|
|
else |
1177 |
|
|
return q.node; |
1178 |
|
|
} |
1179 |
|
|
} |
1180 |
|
|
} |
1181 |
dl |
1.1 |
|
1182 |
dl |
1.88 |
/* ---------------- Relational operations -------------- */ |
1183 |
|
|
|
1184 |
|
|
// Control values OR'ed as arguments to findNear |
1185 |
|
|
|
1186 |
|
|
private static final int EQ = 1; |
1187 |
|
|
private static final int LT = 2; |
1188 |
|
|
private static final int GT = 0; // Actually checked as !LT |
1189 |
|
|
|
1190 |
dl |
1.1 |
/** |
1191 |
dl |
1.88 |
* Utility for ceiling, floor, lower, higher methods. |
1192 |
dl |
1.118 |
* @param key the key |
1193 |
dl |
1.88 |
* @param rel the relation -- OR'ed combination of EQ, LT, GT |
1194 |
|
|
* @return nearest node fitting relation, or null if no such |
1195 |
dl |
1.1 |
*/ |
1196 |
dl |
1.118 |
final Node<K,V> findNear(K key, int rel, Comparator<? super K> cmp) { |
1197 |
|
|
if (key == null) |
1198 |
|
|
throw new NullPointerException(); |
1199 |
dl |
1.88 |
for (;;) { |
1200 |
dl |
1.118 |
for (Node<K,V> b = findPredecessor(key, cmp), n = b.next;;) { |
1201 |
|
|
Object v; |
1202 |
dl |
1.88 |
if (n == null) |
1203 |
|
|
return ((rel & LT) == 0 || b.isBaseHeader()) ? null : b; |
1204 |
|
|
Node<K,V> f = n.next; |
1205 |
|
|
if (n != b.next) // inconsistent read |
1206 |
|
|
break; |
1207 |
dl |
1.118 |
if ((v = n.value) == null) { // n is deleted |
1208 |
dl |
1.88 |
n.helpDelete(b, f); |
1209 |
|
|
break; |
1210 |
|
|
} |
1211 |
dl |
1.118 |
if (b.value == null || v == n) // b is deleted |
1212 |
dl |
1.88 |
break; |
1213 |
dl |
1.118 |
int c = cpr(cmp, key, n.key); |
1214 |
dl |
1.88 |
if ((c == 0 && (rel & EQ) != 0) || |
1215 |
|
|
(c < 0 && (rel & LT) == 0)) |
1216 |
|
|
return n; |
1217 |
|
|
if ( c <= 0 && (rel & LT) != 0) |
1218 |
|
|
return b.isBaseHeader() ? null : b; |
1219 |
|
|
b = n; |
1220 |
|
|
n = f; |
1221 |
|
|
} |
1222 |
|
|
} |
1223 |
|
|
} |
1224 |
|
|
|
1225 |
dl |
1.1 |
/** |
1226 |
jsr166 |
1.10 |
* Returns SimpleImmutableEntry for results of findNear. |
1227 |
dl |
1.40 |
* @param key the key |
1228 |
dl |
1.1 |
* @param rel the relation -- OR'ed combination of EQ, LT, GT |
1229 |
|
|
* @return Entry fitting relation, or null if no such |
1230 |
|
|
*/ |
1231 |
dl |
1.118 |
final AbstractMap.SimpleImmutableEntry<K,V> getNear(K key, int rel) { |
1232 |
|
|
Comparator<? super K> cmp = comparator; |
1233 |
dl |
1.1 |
for (;;) { |
1234 |
dl |
1.118 |
Node<K,V> n = findNear(key, rel, cmp); |
1235 |
dl |
1.1 |
if (n == null) |
1236 |
|
|
return null; |
1237 |
dl |
1.2 |
AbstractMap.SimpleImmutableEntry<K,V> e = n.createSnapshot(); |
1238 |
dl |
1.1 |
if (e != null) |
1239 |
|
|
return e; |
1240 |
|
|
} |
1241 |
|
|
} |
1242 |
|
|
|
1243 |
|
|
/* ---------------- Constructors -------------- */ |
1244 |
|
|
|
1245 |
|
|
/** |
1246 |
jsr166 |
1.22 |
* Constructs a new, empty map, sorted according to the |
1247 |
|
|
* {@linkplain Comparable natural ordering} of the keys. |
1248 |
dl |
1.1 |
*/ |
1249 |
|
|
public ConcurrentSkipListMap() { |
1250 |
|
|
this.comparator = null; |
1251 |
|
|
initialize(); |
1252 |
|
|
} |
1253 |
|
|
|
1254 |
|
|
/** |
1255 |
jsr166 |
1.22 |
* Constructs a new, empty map, sorted according to the specified |
1256 |
|
|
* comparator. |
1257 |
dl |
1.1 |
* |
1258 |
jsr166 |
1.22 |
* @param comparator the comparator that will be used to order this map. |
1259 |
jsr166 |
1.82 |
* If {@code null}, the {@linkplain Comparable natural |
1260 |
jsr166 |
1.22 |
* ordering} of the keys will be used. |
1261 |
dl |
1.1 |
*/ |
1262 |
jsr166 |
1.22 |
public ConcurrentSkipListMap(Comparator<? super K> comparator) { |
1263 |
|
|
this.comparator = comparator; |
1264 |
dl |
1.1 |
initialize(); |
1265 |
|
|
} |
1266 |
|
|
|
1267 |
|
|
/** |
1268 |
|
|
* Constructs a new map containing the same mappings as the given map, |
1269 |
jsr166 |
1.22 |
* sorted according to the {@linkplain Comparable natural ordering} of |
1270 |
|
|
* the keys. |
1271 |
dl |
1.1 |
* |
1272 |
jsr166 |
1.22 |
* @param m the map whose mappings are to be placed in this map |
1273 |
jsr166 |
1.82 |
* @throws ClassCastException if the keys in {@code m} are not |
1274 |
jsr166 |
1.22 |
* {@link Comparable}, or are not mutually comparable |
1275 |
|
|
* @throws NullPointerException if the specified map or any of its keys |
1276 |
|
|
* or values are null |
1277 |
dl |
1.1 |
*/ |
1278 |
|
|
public ConcurrentSkipListMap(Map<? extends K, ? extends V> m) { |
1279 |
|
|
this.comparator = null; |
1280 |
|
|
initialize(); |
1281 |
|
|
putAll(m); |
1282 |
|
|
} |
1283 |
|
|
|
1284 |
|
|
/** |
1285 |
jsr166 |
1.22 |
* Constructs a new map containing the same mappings and using the |
1286 |
|
|
* same ordering as the specified sorted map. |
1287 |
|
|
* |
1288 |
dl |
1.1 |
* @param m the sorted map whose mappings are to be placed in this |
1289 |
jsr166 |
1.22 |
* map, and whose comparator is to be used to sort this map |
1290 |
|
|
* @throws NullPointerException if the specified sorted map or any of |
1291 |
|
|
* its keys or values are null |
1292 |
dl |
1.1 |
*/ |
1293 |
|
|
public ConcurrentSkipListMap(SortedMap<K, ? extends V> m) { |
1294 |
|
|
this.comparator = m.comparator(); |
1295 |
|
|
initialize(); |
1296 |
|
|
buildFromSorted(m); |
1297 |
|
|
} |
1298 |
|
|
|
1299 |
|
|
/** |
1300 |
jsr166 |
1.82 |
* Returns a shallow copy of this {@code ConcurrentSkipListMap} |
1301 |
jsr166 |
1.22 |
* instance. (The keys and values themselves are not cloned.) |
1302 |
dl |
1.1 |
* |
1303 |
jsr166 |
1.22 |
* @return a shallow copy of this map |
1304 |
dl |
1.1 |
*/ |
1305 |
jsr166 |
1.16 |
public ConcurrentSkipListMap<K,V> clone() { |
1306 |
dl |
1.1 |
try { |
1307 |
jsr166 |
1.76 |
@SuppressWarnings("unchecked") |
1308 |
|
|
ConcurrentSkipListMap<K,V> clone = |
1309 |
|
|
(ConcurrentSkipListMap<K,V>) super.clone(); |
1310 |
|
|
clone.initialize(); |
1311 |
|
|
clone.buildFromSorted(this); |
1312 |
|
|
return clone; |
1313 |
dl |
1.1 |
} catch (CloneNotSupportedException e) { |
1314 |
|
|
throw new InternalError(); |
1315 |
|
|
} |
1316 |
|
|
} |
1317 |
|
|
|
1318 |
|
|
/** |
1319 |
|
|
* Streamlined bulk insertion to initialize from elements of |
1320 |
|
|
* given sorted map. Call only from constructor or clone |
1321 |
|
|
* method. |
1322 |
|
|
*/ |
1323 |
|
|
private void buildFromSorted(SortedMap<K, ? extends V> map) { |
1324 |
|
|
if (map == null) |
1325 |
|
|
throw new NullPointerException(); |
1326 |
|
|
|
1327 |
|
|
HeadIndex<K,V> h = head; |
1328 |
|
|
Node<K,V> basepred = h.node; |
1329 |
|
|
|
1330 |
|
|
// Track the current rightmost node at each level. Uses an |
1331 |
|
|
// ArrayList to avoid committing to initial or maximum level. |
1332 |
jsr166 |
1.137 |
ArrayList<Index<K,V>> preds = new ArrayList<>(); |
1333 |
dl |
1.1 |
|
1334 |
|
|
// initialize |
1335 |
dl |
1.9 |
for (int i = 0; i <= h.level; ++i) |
1336 |
dl |
1.1 |
preds.add(null); |
1337 |
|
|
Index<K,V> q = h; |
1338 |
|
|
for (int i = h.level; i > 0; --i) { |
1339 |
|
|
preds.set(i, q); |
1340 |
|
|
q = q.down; |
1341 |
|
|
} |
1342 |
|
|
|
1343 |
dl |
1.9 |
Iterator<? extends Map.Entry<? extends K, ? extends V>> it = |
1344 |
dl |
1.1 |
map.entrySet().iterator(); |
1345 |
|
|
while (it.hasNext()) { |
1346 |
|
|
Map.Entry<? extends K, ? extends V> e = it.next(); |
1347 |
dl |
1.92 |
int rnd = ThreadLocalRandom.current().nextInt(); |
1348 |
|
|
int j = 0; |
1349 |
|
|
if ((rnd & 0x80000001) == 0) { |
1350 |
|
|
do { |
1351 |
|
|
++j; |
1352 |
|
|
} while (((rnd >>>= 1) & 1) != 0); |
1353 |
|
|
if (j > h.level) j = h.level + 1; |
1354 |
|
|
} |
1355 |
dl |
1.1 |
K k = e.getKey(); |
1356 |
|
|
V v = e.getValue(); |
1357 |
|
|
if (k == null || v == null) |
1358 |
|
|
throw new NullPointerException(); |
1359 |
|
|
Node<K,V> z = new Node<K,V>(k, v, null); |
1360 |
|
|
basepred.next = z; |
1361 |
|
|
basepred = z; |
1362 |
|
|
if (j > 0) { |
1363 |
|
|
Index<K,V> idx = null; |
1364 |
|
|
for (int i = 1; i <= j; ++i) { |
1365 |
|
|
idx = new Index<K,V>(z, idx, null); |
1366 |
dl |
1.9 |
if (i > h.level) |
1367 |
dl |
1.1 |
h = new HeadIndex<K,V>(h.node, h, idx, i); |
1368 |
|
|
|
1369 |
|
|
if (i < preds.size()) { |
1370 |
|
|
preds.get(i).right = idx; |
1371 |
|
|
preds.set(i, idx); |
1372 |
|
|
} else |
1373 |
|
|
preds.add(idx); |
1374 |
|
|
} |
1375 |
|
|
} |
1376 |
|
|
} |
1377 |
|
|
head = h; |
1378 |
|
|
} |
1379 |
|
|
|
1380 |
|
|
/* ---------------- Serialization -------------- */ |
1381 |
|
|
|
1382 |
|
|
/** |
1383 |
jsr166 |
1.80 |
* Saves this map to a stream (that is, serializes it). |
1384 |
dl |
1.1 |
* |
1385 |
jsr166 |
1.128 |
* @param s the stream |
1386 |
jsr166 |
1.129 |
* @throws java.io.IOException if an I/O error occurs |
1387 |
dl |
1.1 |
* @serialData The key (Object) and value (Object) for each |
1388 |
jsr166 |
1.10 |
* key-value mapping represented by the map, followed by |
1389 |
jsr166 |
1.82 |
* {@code null}. The key-value mappings are emitted in key-order |
1390 |
dl |
1.1 |
* (as determined by the Comparator, or by the keys' natural |
1391 |
|
|
* ordering if no Comparator). |
1392 |
|
|
*/ |
1393 |
|
|
private void writeObject(java.io.ObjectOutputStream s) |
1394 |
|
|
throws java.io.IOException { |
1395 |
|
|
// Write out the Comparator and any hidden stuff |
1396 |
|
|
s.defaultWriteObject(); |
1397 |
|
|
|
1398 |
|
|
// Write out keys and values (alternating) |
1399 |
|
|
for (Node<K,V> n = findFirst(); n != null; n = n.next) { |
1400 |
|
|
V v = n.getValidValue(); |
1401 |
|
|
if (v != null) { |
1402 |
|
|
s.writeObject(n.key); |
1403 |
|
|
s.writeObject(v); |
1404 |
|
|
} |
1405 |
|
|
} |
1406 |
|
|
s.writeObject(null); |
1407 |
|
|
} |
1408 |
|
|
|
1409 |
|
|
/** |
1410 |
jsr166 |
1.80 |
* Reconstitutes this map from a stream (that is, deserializes it). |
1411 |
jsr166 |
1.128 |
* @param s the stream |
1412 |
jsr166 |
1.129 |
* @throws ClassNotFoundException if the class of a serialized object |
1413 |
|
|
* could not be found |
1414 |
|
|
* @throws java.io.IOException if an I/O error occurs |
1415 |
dl |
1.1 |
*/ |
1416 |
dl |
1.100 |
@SuppressWarnings("unchecked") |
1417 |
dl |
1.1 |
private void readObject(final java.io.ObjectInputStream s) |
1418 |
|
|
throws java.io.IOException, ClassNotFoundException { |
1419 |
|
|
// Read in the Comparator and any hidden stuff |
1420 |
|
|
s.defaultReadObject(); |
1421 |
|
|
// Reset transients |
1422 |
|
|
initialize(); |
1423 |
|
|
|
1424 |
dl |
1.9 |
/* |
1425 |
dl |
1.1 |
* This is nearly identical to buildFromSorted, but is |
1426 |
|
|
* distinct because readObject calls can't be nicely adapted |
1427 |
|
|
* as the kind of iterator needed by buildFromSorted. (They |
1428 |
|
|
* can be, but doing so requires type cheats and/or creation |
1429 |
|
|
* of adaptor classes.) It is simpler to just adapt the code. |
1430 |
|
|
*/ |
1431 |
|
|
|
1432 |
|
|
HeadIndex<K,V> h = head; |
1433 |
|
|
Node<K,V> basepred = h.node; |
1434 |
jsr166 |
1.137 |
ArrayList<Index<K,V>> preds = new ArrayList<>(); |
1435 |
dl |
1.9 |
for (int i = 0; i <= h.level; ++i) |
1436 |
dl |
1.1 |
preds.add(null); |
1437 |
|
|
Index<K,V> q = h; |
1438 |
|
|
for (int i = h.level; i > 0; --i) { |
1439 |
|
|
preds.set(i, q); |
1440 |
|
|
q = q.down; |
1441 |
|
|
} |
1442 |
|
|
|
1443 |
|
|
for (;;) { |
1444 |
|
|
Object k = s.readObject(); |
1445 |
|
|
if (k == null) |
1446 |
|
|
break; |
1447 |
|
|
Object v = s.readObject(); |
1448 |
dl |
1.9 |
if (v == null) |
1449 |
dl |
1.1 |
throw new NullPointerException(); |
1450 |
|
|
K key = (K) k; |
1451 |
|
|
V val = (V) v; |
1452 |
dl |
1.92 |
int rnd = ThreadLocalRandom.current().nextInt(); |
1453 |
|
|
int j = 0; |
1454 |
|
|
if ((rnd & 0x80000001) == 0) { |
1455 |
|
|
do { |
1456 |
|
|
++j; |
1457 |
|
|
} while (((rnd >>>= 1) & 1) != 0); |
1458 |
|
|
if (j > h.level) j = h.level + 1; |
1459 |
|
|
} |
1460 |
dl |
1.1 |
Node<K,V> z = new Node<K,V>(key, val, null); |
1461 |
|
|
basepred.next = z; |
1462 |
|
|
basepred = z; |
1463 |
|
|
if (j > 0) { |
1464 |
|
|
Index<K,V> idx = null; |
1465 |
|
|
for (int i = 1; i <= j; ++i) { |
1466 |
|
|
idx = new Index<K,V>(z, idx, null); |
1467 |
dl |
1.9 |
if (i > h.level) |
1468 |
dl |
1.1 |
h = new HeadIndex<K,V>(h.node, h, idx, i); |
1469 |
|
|
|
1470 |
|
|
if (i < preds.size()) { |
1471 |
|
|
preds.get(i).right = idx; |
1472 |
|
|
preds.set(i, idx); |
1473 |
|
|
} else |
1474 |
|
|
preds.add(idx); |
1475 |
|
|
} |
1476 |
|
|
} |
1477 |
|
|
} |
1478 |
|
|
head = h; |
1479 |
|
|
} |
1480 |
|
|
|
1481 |
|
|
/* ------ Map API methods ------ */ |
1482 |
|
|
|
1483 |
|
|
/** |
1484 |
jsr166 |
1.82 |
* Returns {@code true} if this map contains a mapping for the specified |
1485 |
dl |
1.1 |
* key. |
1486 |
jsr166 |
1.22 |
* |
1487 |
|
|
* @param key key whose presence in this map is to be tested |
1488 |
jsr166 |
1.82 |
* @return {@code true} if this map contains a mapping for the specified key |
1489 |
jsr166 |
1.22 |
* @throws ClassCastException if the specified key cannot be compared |
1490 |
|
|
* with the keys currently in the map |
1491 |
|
|
* @throws NullPointerException if the specified key is null |
1492 |
dl |
1.1 |
*/ |
1493 |
|
|
public boolean containsKey(Object key) { |
1494 |
dl |
1.118 |
return doGet(key) != null; |
1495 |
dl |
1.1 |
} |
1496 |
|
|
|
1497 |
|
|
/** |
1498 |
jsr166 |
1.42 |
* Returns the value to which the specified key is mapped, |
1499 |
|
|
* or {@code null} if this map contains no mapping for the key. |
1500 |
|
|
* |
1501 |
|
|
* <p>More formally, if this map contains a mapping from a key |
1502 |
|
|
* {@code k} to a value {@code v} such that {@code key} compares |
1503 |
|
|
* equal to {@code k} according to the map's ordering, then this |
1504 |
|
|
* method returns {@code v}; otherwise it returns {@code null}. |
1505 |
|
|
* (There can be at most one such mapping.) |
1506 |
dl |
1.1 |
* |
1507 |
jsr166 |
1.22 |
* @throws ClassCastException if the specified key cannot be compared |
1508 |
|
|
* with the keys currently in the map |
1509 |
|
|
* @throws NullPointerException if the specified key is null |
1510 |
dl |
1.1 |
*/ |
1511 |
|
|
public V get(Object key) { |
1512 |
dl |
1.118 |
return doGet(key); |
1513 |
dl |
1.1 |
} |
1514 |
|
|
|
1515 |
|
|
/** |
1516 |
dl |
1.109 |
* Returns the value to which the specified key is mapped, |
1517 |
|
|
* or the given defaultValue if this map contains no mapping for the key. |
1518 |
|
|
* |
1519 |
|
|
* @param key the key |
1520 |
|
|
* @param defaultValue the value to return if this map contains |
1521 |
|
|
* no mapping for the given key |
1522 |
|
|
* @return the mapping for the key, if present; else the defaultValue |
1523 |
|
|
* @throws NullPointerException if the specified key is null |
1524 |
|
|
* @since 1.8 |
1525 |
|
|
*/ |
1526 |
|
|
public V getOrDefault(Object key, V defaultValue) { |
1527 |
|
|
V v; |
1528 |
dl |
1.118 |
return (v = doGet(key)) == null ? defaultValue : v; |
1529 |
dl |
1.109 |
} |
1530 |
|
|
|
1531 |
|
|
/** |
1532 |
dl |
1.1 |
* Associates the specified value with the specified key in this map. |
1533 |
jsr166 |
1.22 |
* If the map previously contained a mapping for the key, the old |
1534 |
dl |
1.1 |
* value is replaced. |
1535 |
|
|
* |
1536 |
jsr166 |
1.22 |
* @param key key with which the specified value is to be associated |
1537 |
|
|
* @param value value to be associated with the specified key |
1538 |
|
|
* @return the previous value associated with the specified key, or |
1539 |
jsr166 |
1.82 |
* {@code null} if there was no mapping for the key |
1540 |
jsr166 |
1.22 |
* @throws ClassCastException if the specified key cannot be compared |
1541 |
|
|
* with the keys currently in the map |
1542 |
|
|
* @throws NullPointerException if the specified key or value is null |
1543 |
dl |
1.1 |
*/ |
1544 |
|
|
public V put(K key, V value) { |
1545 |
dl |
1.9 |
if (value == null) |
1546 |
dl |
1.1 |
throw new NullPointerException(); |
1547 |
dl |
1.118 |
return doPut(key, value, false); |
1548 |
dl |
1.1 |
} |
1549 |
|
|
|
1550 |
|
|
/** |
1551 |
jsr166 |
1.36 |
* Removes the mapping for the specified key from this map if present. |
1552 |
dl |
1.1 |
* |
1553 |
|
|
* @param key key for which mapping should be removed |
1554 |
jsr166 |
1.22 |
* @return the previous value associated with the specified key, or |
1555 |
jsr166 |
1.82 |
* {@code null} if there was no mapping for the key |
1556 |
jsr166 |
1.22 |
* @throws ClassCastException if the specified key cannot be compared |
1557 |
|
|
* with the keys currently in the map |
1558 |
|
|
* @throws NullPointerException if the specified key is null |
1559 |
dl |
1.1 |
*/ |
1560 |
|
|
public V remove(Object key) { |
1561 |
dl |
1.118 |
return doRemove(key, null); |
1562 |
dl |
1.1 |
} |
1563 |
|
|
|
1564 |
|
|
/** |
1565 |
jsr166 |
1.82 |
* Returns {@code true} if this map maps one or more keys to the |
1566 |
dl |
1.1 |
* specified value. This operation requires time linear in the |
1567 |
dl |
1.69 |
* map size. Additionally, it is possible for the map to change |
1568 |
|
|
* during execution of this method, in which case the returned |
1569 |
|
|
* result may be inaccurate. |
1570 |
dl |
1.1 |
* |
1571 |
jsr166 |
1.22 |
* @param value value whose presence in this map is to be tested |
1572 |
jsr166 |
1.82 |
* @return {@code true} if a mapping to {@code value} exists; |
1573 |
|
|
* {@code false} otherwise |
1574 |
jsr166 |
1.22 |
* @throws NullPointerException if the specified value is null |
1575 |
dl |
1.9 |
*/ |
1576 |
dl |
1.1 |
public boolean containsValue(Object value) { |
1577 |
dl |
1.9 |
if (value == null) |
1578 |
dl |
1.1 |
throw new NullPointerException(); |
1579 |
|
|
for (Node<K,V> n = findFirst(); n != null; n = n.next) { |
1580 |
|
|
V v = n.getValidValue(); |
1581 |
|
|
if (v != null && value.equals(v)) |
1582 |
|
|
return true; |
1583 |
|
|
} |
1584 |
|
|
return false; |
1585 |
|
|
} |
1586 |
|
|
|
1587 |
|
|
/** |
1588 |
dl |
1.6 |
* Returns the number of key-value mappings in this map. If this map |
1589 |
jsr166 |
1.82 |
* contains more than {@code Integer.MAX_VALUE} elements, it |
1590 |
|
|
* returns {@code Integer.MAX_VALUE}. |
1591 |
dl |
1.1 |
* |
1592 |
|
|
* <p>Beware that, unlike in most collections, this method is |
1593 |
|
|
* <em>NOT</em> a constant-time operation. Because of the |
1594 |
|
|
* asynchronous nature of these maps, determining the current |
1595 |
|
|
* number of elements requires traversing them all to count them. |
1596 |
|
|
* Additionally, it is possible for the size to change during |
1597 |
|
|
* execution of this method, in which case the returned result |
1598 |
|
|
* will be inaccurate. Thus, this method is typically not very |
1599 |
|
|
* useful in concurrent applications. |
1600 |
|
|
* |
1601 |
jsr166 |
1.22 |
* @return the number of elements in this map |
1602 |
dl |
1.1 |
*/ |
1603 |
|
|
public int size() { |
1604 |
|
|
long count = 0; |
1605 |
|
|
for (Node<K,V> n = findFirst(); n != null; n = n.next) { |
1606 |
|
|
if (n.getValidValue() != null) |
1607 |
|
|
++count; |
1608 |
|
|
} |
1609 |
jsr166 |
1.61 |
return (count >= Integer.MAX_VALUE) ? Integer.MAX_VALUE : (int) count; |
1610 |
dl |
1.1 |
} |
1611 |
|
|
|
1612 |
|
|
/** |
1613 |
jsr166 |
1.82 |
* Returns {@code true} if this map contains no key-value mappings. |
1614 |
|
|
* @return {@code true} if this map contains no key-value mappings |
1615 |
dl |
1.1 |
*/ |
1616 |
|
|
public boolean isEmpty() { |
1617 |
|
|
return findFirst() == null; |
1618 |
|
|
} |
1619 |
|
|
|
1620 |
|
|
/** |
1621 |
jsr166 |
1.22 |
* Removes all of the mappings from this map. |
1622 |
dl |
1.1 |
*/ |
1623 |
|
|
public void clear() { |
1624 |
|
|
initialize(); |
1625 |
|
|
} |
1626 |
|
|
|
1627 |
dl |
1.109 |
/** |
1628 |
|
|
* If the specified key is not already associated with a value, |
1629 |
|
|
* attempts to compute its value using the given mapping function |
1630 |
|
|
* and enters it into this map unless {@code null}. The function |
1631 |
|
|
* is <em>NOT</em> guaranteed to be applied once atomically only |
1632 |
|
|
* if the value is not present. |
1633 |
|
|
* |
1634 |
|
|
* @param key key with which the specified value is to be associated |
1635 |
|
|
* @param mappingFunction the function to compute a value |
1636 |
|
|
* @return the current (existing or computed) value associated with |
1637 |
|
|
* the specified key, or null if the computed value is null |
1638 |
|
|
* @throws NullPointerException if the specified key is null |
1639 |
|
|
* or the mappingFunction is null |
1640 |
|
|
* @since 1.8 |
1641 |
|
|
*/ |
1642 |
jsr166 |
1.110 |
public V computeIfAbsent(K key, |
1643 |
dl |
1.109 |
Function<? super K, ? extends V> mappingFunction) { |
1644 |
jsr166 |
1.110 |
if (key == null || mappingFunction == null) |
1645 |
|
|
throw new NullPointerException(); |
1646 |
|
|
V v, p, r; |
1647 |
dl |
1.118 |
if ((v = doGet(key)) == null && |
1648 |
|
|
(r = mappingFunction.apply(key)) != null) |
1649 |
|
|
v = (p = doPut(key, r, true)) == null ? r : p; |
1650 |
dl |
1.109 |
return v; |
1651 |
|
|
} |
1652 |
|
|
|
1653 |
|
|
/** |
1654 |
|
|
* If the value for the specified key is present, attempts to |
1655 |
|
|
* compute a new mapping given the key and its current mapped |
1656 |
|
|
* value. The function is <em>NOT</em> guaranteed to be applied |
1657 |
|
|
* once atomically. |
1658 |
|
|
* |
1659 |
dl |
1.111 |
* @param key key with which a value may be associated |
1660 |
dl |
1.109 |
* @param remappingFunction the function to compute a value |
1661 |
|
|
* @return the new value associated with the specified key, or null if none |
1662 |
|
|
* @throws NullPointerException if the specified key is null |
1663 |
|
|
* or the remappingFunction is null |
1664 |
|
|
* @since 1.8 |
1665 |
|
|
*/ |
1666 |
jsr166 |
1.110 |
public V computeIfPresent(K key, |
1667 |
dl |
1.109 |
BiFunction<? super K, ? super V, ? extends V> remappingFunction) { |
1668 |
jsr166 |
1.110 |
if (key == null || remappingFunction == null) |
1669 |
|
|
throw new NullPointerException(); |
1670 |
dl |
1.118 |
Node<K,V> n; Object v; |
1671 |
|
|
while ((n = findNode(key)) != null) { |
1672 |
|
|
if ((v = n.value) != null) { |
1673 |
|
|
@SuppressWarnings("unchecked") V vv = (V) v; |
1674 |
|
|
V r = remappingFunction.apply(key, vv); |
1675 |
|
|
if (r != null) { |
1676 |
|
|
if (n.casValue(vv, r)) |
1677 |
|
|
return r; |
1678 |
dl |
1.109 |
} |
1679 |
dl |
1.118 |
else if (doRemove(key, vv) != null) |
1680 |
|
|
break; |
1681 |
dl |
1.109 |
} |
1682 |
jsr166 |
1.110 |
} |
1683 |
|
|
return null; |
1684 |
dl |
1.109 |
} |
1685 |
|
|
|
1686 |
|
|
/** |
1687 |
|
|
* Attempts to compute a mapping for the specified key and its |
1688 |
|
|
* current mapped value (or {@code null} if there is no current |
1689 |
|
|
* mapping). The function is <em>NOT</em> guaranteed to be applied |
1690 |
|
|
* once atomically. |
1691 |
|
|
* |
1692 |
|
|
* @param key key with which the specified value is to be associated |
1693 |
|
|
* @param remappingFunction the function to compute a value |
1694 |
|
|
* @return the new value associated with the specified key, or null if none |
1695 |
|
|
* @throws NullPointerException if the specified key is null |
1696 |
|
|
* or the remappingFunction is null |
1697 |
|
|
* @since 1.8 |
1698 |
|
|
*/ |
1699 |
jsr166 |
1.110 |
public V compute(K key, |
1700 |
dl |
1.109 |
BiFunction<? super K, ? super V, ? extends V> remappingFunction) { |
1701 |
jsr166 |
1.110 |
if (key == null || remappingFunction == null) |
1702 |
|
|
throw new NullPointerException(); |
1703 |
dl |
1.118 |
for (;;) { |
1704 |
|
|
Node<K,V> n; Object v; V r; |
1705 |
|
|
if ((n = findNode(key)) == null) { |
1706 |
|
|
if ((r = remappingFunction.apply(key, null)) == null) |
1707 |
|
|
break; |
1708 |
dl |
1.124 |
if (doPut(key, r, true) == null) |
1709 |
dl |
1.118 |
return r; |
1710 |
|
|
} |
1711 |
|
|
else if ((v = n.value) != null) { |
1712 |
|
|
@SuppressWarnings("unchecked") V vv = (V) v; |
1713 |
|
|
if ((r = remappingFunction.apply(key, vv)) != null) { |
1714 |
|
|
if (n.casValue(vv, r)) |
1715 |
dl |
1.109 |
return r; |
1716 |
|
|
} |
1717 |
dl |
1.118 |
else if (doRemove(key, vv) != null) |
1718 |
|
|
break; |
1719 |
dl |
1.109 |
} |
1720 |
|
|
} |
1721 |
jsr166 |
1.110 |
return null; |
1722 |
dl |
1.109 |
} |
1723 |
|
|
|
1724 |
|
|
/** |
1725 |
|
|
* If the specified key is not already associated with a value, |
1726 |
|
|
* associates it with the given value. Otherwise, replaces the |
1727 |
|
|
* value with the results of the given remapping function, or |
1728 |
|
|
* removes if {@code null}. The function is <em>NOT</em> |
1729 |
|
|
* guaranteed to be applied once atomically. |
1730 |
|
|
* |
1731 |
|
|
* @param key key with which the specified value is to be associated |
1732 |
|
|
* @param value the value to use if absent |
1733 |
|
|
* @param remappingFunction the function to recompute a value if present |
1734 |
|
|
* @return the new value associated with the specified key, or null if none |
1735 |
|
|
* @throws NullPointerException if the specified key or value is null |
1736 |
|
|
* or the remappingFunction is null |
1737 |
|
|
* @since 1.8 |
1738 |
|
|
*/ |
1739 |
|
|
public V merge(K key, V value, |
1740 |
|
|
BiFunction<? super V, ? super V, ? extends V> remappingFunction) { |
1741 |
jsr166 |
1.110 |
if (key == null || value == null || remappingFunction == null) |
1742 |
|
|
throw new NullPointerException(); |
1743 |
dl |
1.118 |
for (;;) { |
1744 |
|
|
Node<K,V> n; Object v; V r; |
1745 |
|
|
if ((n = findNode(key)) == null) { |
1746 |
dl |
1.124 |
if (doPut(key, value, true) == null) |
1747 |
dl |
1.118 |
return value; |
1748 |
|
|
} |
1749 |
|
|
else if ((v = n.value) != null) { |
1750 |
|
|
@SuppressWarnings("unchecked") V vv = (V) v; |
1751 |
|
|
if ((r = remappingFunction.apply(vv, value)) != null) { |
1752 |
|
|
if (n.casValue(vv, r)) |
1753 |
|
|
return r; |
1754 |
dl |
1.109 |
} |
1755 |
dl |
1.118 |
else if (doRemove(key, vv) != null) |
1756 |
|
|
return null; |
1757 |
dl |
1.109 |
} |
1758 |
jsr166 |
1.110 |
} |
1759 |
dl |
1.109 |
} |
1760 |
|
|
|
1761 |
dl |
1.46 |
/* ---------------- View methods -------------- */ |
1762 |
|
|
|
1763 |
|
|
/* |
1764 |
|
|
* Note: Lazy initialization works for views because view classes |
1765 |
|
|
* are stateless/immutable so it doesn't matter wrt correctness if |
1766 |
|
|
* more than one is created (which will only rarely happen). Even |
1767 |
|
|
* so, the following idiom conservatively ensures that the method |
1768 |
|
|
* returns the one it created if it does so, not one created by |
1769 |
|
|
* another racing thread. |
1770 |
|
|
*/ |
1771 |
|
|
|
1772 |
dl |
1.1 |
/** |
1773 |
jsr166 |
1.51 |
* Returns a {@link NavigableSet} view of the keys contained in this map. |
1774 |
jsr166 |
1.132 |
* |
1775 |
|
|
* <p>The set's iterator returns the keys in ascending order. |
1776 |
|
|
* The set's spliterator additionally reports {@link Spliterator#CONCURRENT}, |
1777 |
|
|
* {@link Spliterator#NONNULL}, {@link Spliterator#SORTED} and |
1778 |
|
|
* {@link Spliterator#ORDERED}, with an encounter order that is ascending |
1779 |
|
|
* key order. The spliterator's comparator (see |
1780 |
|
|
* {@link java.util.Spliterator#getComparator()}) is {@code null} if |
1781 |
|
|
* the map's comparator (see {@link #comparator()}) is {@code null}. |
1782 |
|
|
* Otherwise, the spliterator's comparator is the same as or imposes the |
1783 |
|
|
* same total ordering as the map's comparator. |
1784 |
|
|
* |
1785 |
|
|
* <p>The set is backed by the map, so changes to the map are |
1786 |
jsr166 |
1.22 |
* reflected in the set, and vice-versa. The set supports element |
1787 |
|
|
* removal, which removes the corresponding mapping from the map, |
1788 |
jsr166 |
1.51 |
* via the {@code Iterator.remove}, {@code Set.remove}, |
1789 |
|
|
* {@code removeAll}, {@code retainAll}, and {@code clear} |
1790 |
|
|
* operations. It does not support the {@code add} or {@code addAll} |
1791 |
jsr166 |
1.22 |
* operations. |
1792 |
|
|
* |
1793 |
jsr166 |
1.133 |
* <p>The view's iterators and spliterators are |
1794 |
|
|
* <a href="package-summary.html#Weakly"><i>weakly consistent</i></a>. |
1795 |
dl |
1.1 |
* |
1796 |
jsr166 |
1.51 |
* <p>This method is equivalent to method {@code navigableKeySet}. |
1797 |
|
|
* |
1798 |
|
|
* @return a navigable set view of the keys in this map |
1799 |
dl |
1.1 |
*/ |
1800 |
jsr166 |
1.68 |
public NavigableSet<K> keySet() { |
1801 |
dl |
1.118 |
KeySet<K> ks = keySet; |
1802 |
|
|
return (ks != null) ? ks : (keySet = new KeySet<K>(this)); |
1803 |
dl |
1.1 |
} |
1804 |
|
|
|
1805 |
dl |
1.46 |
public NavigableSet<K> navigableKeySet() { |
1806 |
dl |
1.118 |
KeySet<K> ks = keySet; |
1807 |
|
|
return (ks != null) ? ks : (keySet = new KeySet<K>(this)); |
1808 |
dl |
1.83 |
} |
1809 |
|
|
|
1810 |
|
|
/** |
1811 |
jsr166 |
1.22 |
* Returns a {@link Collection} view of the values contained in this map. |
1812 |
jsr166 |
1.132 |
* <p>The collection's iterator returns the values in ascending order |
1813 |
|
|
* of the corresponding keys. The collections's spliterator additionally |
1814 |
|
|
* reports {@link Spliterator#CONCURRENT}, {@link Spliterator#NONNULL} and |
1815 |
|
|
* {@link Spliterator#ORDERED}, with an encounter order that is ascending |
1816 |
|
|
* order of the corresponding keys. |
1817 |
|
|
* |
1818 |
|
|
* <p>The collection is backed by the map, so changes to the map are |
1819 |
dl |
1.1 |
* reflected in the collection, and vice-versa. The collection |
1820 |
|
|
* supports element removal, which removes the corresponding |
1821 |
jsr166 |
1.82 |
* mapping from the map, via the {@code Iterator.remove}, |
1822 |
|
|
* {@code Collection.remove}, {@code removeAll}, |
1823 |
|
|
* {@code retainAll} and {@code clear} operations. It does not |
1824 |
|
|
* support the {@code add} or {@code addAll} operations. |
1825 |
dl |
1.1 |
* |
1826 |
jsr166 |
1.133 |
* <p>The view's iterators and spliterators are |
1827 |
|
|
* <a href="package-summary.html#Weakly"><i>weakly consistent</i></a>. |
1828 |
dl |
1.1 |
*/ |
1829 |
|
|
public Collection<V> values() { |
1830 |
jsr166 |
1.71 |
Values<V> vs = values; |
1831 |
|
|
return (vs != null) ? vs : (values = new Values<V>(this)); |
1832 |
dl |
1.1 |
} |
1833 |
|
|
|
1834 |
|
|
/** |
1835 |
jsr166 |
1.22 |
* Returns a {@link Set} view of the mappings contained in this map. |
1836 |
jsr166 |
1.132 |
* |
1837 |
|
|
* <p>The set's iterator returns the entries in ascending key order. The |
1838 |
|
|
* set's spliterator additionally reports {@link Spliterator#CONCURRENT}, |
1839 |
|
|
* {@link Spliterator#NONNULL}, {@link Spliterator#SORTED} and |
1840 |
|
|
* {@link Spliterator#ORDERED}, with an encounter order that is ascending |
1841 |
|
|
* key order. |
1842 |
|
|
* |
1843 |
|
|
* <p>The set is backed by the map, so changes to the map are |
1844 |
jsr166 |
1.22 |
* reflected in the set, and vice-versa. The set supports element |
1845 |
|
|
* removal, which removes the corresponding mapping from the map, |
1846 |
jsr166 |
1.82 |
* via the {@code Iterator.remove}, {@code Set.remove}, |
1847 |
|
|
* {@code removeAll}, {@code retainAll} and {@code clear} |
1848 |
|
|
* operations. It does not support the {@code add} or |
1849 |
|
|
* {@code addAll} operations. |
1850 |
jsr166 |
1.22 |
* |
1851 |
jsr166 |
1.133 |
* <p>The view's iterators and spliterators are |
1852 |
|
|
* <a href="package-summary.html#Weakly"><i>weakly consistent</i></a>. |
1853 |
jsr166 |
1.132 |
* |
1854 |
|
|
* <p>The {@code Map.Entry} elements traversed by the {@code iterator} |
1855 |
|
|
* or {@code spliterator} do <em>not</em> support the {@code setValue} |
1856 |
|
|
* operation. |
1857 |
dl |
1.1 |
* |
1858 |
jsr166 |
1.22 |
* @return a set view of the mappings contained in this map, |
1859 |
|
|
* sorted in ascending key order |
1860 |
dl |
1.1 |
*/ |
1861 |
|
|
public Set<Map.Entry<K,V>> entrySet() { |
1862 |
jsr166 |
1.71 |
EntrySet<K,V> es = entrySet; |
1863 |
|
|
return (es != null) ? es : (entrySet = new EntrySet<K,V>(this)); |
1864 |
dl |
1.46 |
} |
1865 |
|
|
|
1866 |
|
|
public ConcurrentNavigableMap<K,V> descendingMap() { |
1867 |
|
|
ConcurrentNavigableMap<K,V> dm = descendingMap; |
1868 |
|
|
return (dm != null) ? dm : (descendingMap = new SubMap<K,V> |
1869 |
|
|
(this, null, false, null, false, true)); |
1870 |
dl |
1.1 |
} |
1871 |
|
|
|
1872 |
dl |
1.46 |
public NavigableSet<K> descendingKeySet() { |
1873 |
|
|
return descendingMap().navigableKeySet(); |
1874 |
dl |
1.1 |
} |
1875 |
|
|
|
1876 |
|
|
/* ---------------- AbstractMap Overrides -------------- */ |
1877 |
|
|
|
1878 |
|
|
/** |
1879 |
|
|
* Compares the specified object with this map for equality. |
1880 |
jsr166 |
1.82 |
* Returns {@code true} if the given object is also a map and the |
1881 |
dl |
1.1 |
* two maps represent the same mappings. More formally, two maps |
1882 |
jsr166 |
1.82 |
* {@code m1} and {@code m2} represent the same mappings if |
1883 |
|
|
* {@code m1.entrySet().equals(m2.entrySet())}. This |
1884 |
dl |
1.1 |
* operation may return misleading results if either map is |
1885 |
|
|
* concurrently modified during execution of this method. |
1886 |
|
|
* |
1887 |
jsr166 |
1.22 |
* @param o object to be compared for equality with this map |
1888 |
jsr166 |
1.82 |
* @return {@code true} if the specified object is equal to this map |
1889 |
dl |
1.1 |
*/ |
1890 |
|
|
public boolean equals(Object o) { |
1891 |
jsr166 |
1.55 |
if (o == this) |
1892 |
|
|
return true; |
1893 |
|
|
if (!(o instanceof Map)) |
1894 |
|
|
return false; |
1895 |
|
|
Map<?,?> m = (Map<?,?>) o; |
1896 |
dl |
1.1 |
try { |
1897 |
jsr166 |
1.55 |
for (Map.Entry<K,V> e : this.entrySet()) |
1898 |
|
|
if (! e.getValue().equals(m.get(e.getKey()))) |
1899 |
dl |
1.25 |
return false; |
1900 |
jsr166 |
1.55 |
for (Map.Entry<?,?> e : m.entrySet()) { |
1901 |
dl |
1.25 |
Object k = e.getKey(); |
1902 |
|
|
Object v = e.getValue(); |
1903 |
jsr166 |
1.55 |
if (k == null || v == null || !v.equals(get(k))) |
1904 |
dl |
1.25 |
return false; |
1905 |
|
|
} |
1906 |
|
|
return true; |
1907 |
jsr166 |
1.15 |
} catch (ClassCastException unused) { |
1908 |
dl |
1.1 |
return false; |
1909 |
jsr166 |
1.15 |
} catch (NullPointerException unused) { |
1910 |
dl |
1.1 |
return false; |
1911 |
|
|
} |
1912 |
|
|
} |
1913 |
|
|
|
1914 |
|
|
/* ------ ConcurrentMap API methods ------ */ |
1915 |
|
|
|
1916 |
|
|
/** |
1917 |
jsr166 |
1.22 |
* {@inheritDoc} |
1918 |
|
|
* |
1919 |
|
|
* @return the previous value associated with the specified key, |
1920 |
jsr166 |
1.82 |
* or {@code null} if there was no mapping for the key |
1921 |
jsr166 |
1.22 |
* @throws ClassCastException if the specified key cannot be compared |
1922 |
|
|
* with the keys currently in the map |
1923 |
|
|
* @throws NullPointerException if the specified key or value is null |
1924 |
dl |
1.1 |
*/ |
1925 |
|
|
public V putIfAbsent(K key, V value) { |
1926 |
dl |
1.9 |
if (value == null) |
1927 |
dl |
1.1 |
throw new NullPointerException(); |
1928 |
dl |
1.118 |
return doPut(key, value, true); |
1929 |
dl |
1.1 |
} |
1930 |
|
|
|
1931 |
|
|
/** |
1932 |
jsr166 |
1.22 |
* {@inheritDoc} |
1933 |
|
|
* |
1934 |
|
|
* @throws ClassCastException if the specified key cannot be compared |
1935 |
|
|
* with the keys currently in the map |
1936 |
dl |
1.23 |
* @throws NullPointerException if the specified key is null |
1937 |
dl |
1.1 |
*/ |
1938 |
|
|
public boolean remove(Object key, Object value) { |
1939 |
dl |
1.45 |
if (key == null) |
1940 |
|
|
throw new NullPointerException(); |
1941 |
dl |
1.118 |
return value != null && doRemove(key, value) != null; |
1942 |
dl |
1.1 |
} |
1943 |
|
|
|
1944 |
|
|
/** |
1945 |
jsr166 |
1.22 |
* {@inheritDoc} |
1946 |
|
|
* |
1947 |
|
|
* @throws ClassCastException if the specified key cannot be compared |
1948 |
|
|
* with the keys currently in the map |
1949 |
|
|
* @throws NullPointerException if any of the arguments are null |
1950 |
dl |
1.1 |
*/ |
1951 |
|
|
public boolean replace(K key, V oldValue, V newValue) { |
1952 |
dl |
1.118 |
if (key == null || oldValue == null || newValue == null) |
1953 |
dl |
1.1 |
throw new NullPointerException(); |
1954 |
|
|
for (;;) { |
1955 |
dl |
1.118 |
Node<K,V> n; Object v; |
1956 |
|
|
if ((n = findNode(key)) == null) |
1957 |
dl |
1.1 |
return false; |
1958 |
dl |
1.118 |
if ((v = n.value) != null) { |
1959 |
dl |
1.1 |
if (!oldValue.equals(v)) |
1960 |
|
|
return false; |
1961 |
|
|
if (n.casValue(v, newValue)) |
1962 |
|
|
return true; |
1963 |
|
|
} |
1964 |
|
|
} |
1965 |
|
|
} |
1966 |
|
|
|
1967 |
|
|
/** |
1968 |
jsr166 |
1.22 |
* {@inheritDoc} |
1969 |
|
|
* |
1970 |
|
|
* @return the previous value associated with the specified key, |
1971 |
jsr166 |
1.82 |
* or {@code null} if there was no mapping for the key |
1972 |
jsr166 |
1.22 |
* @throws ClassCastException if the specified key cannot be compared |
1973 |
|
|
* with the keys currently in the map |
1974 |
|
|
* @throws NullPointerException if the specified key or value is null |
1975 |
dl |
1.1 |
*/ |
1976 |
|
|
public V replace(K key, V value) { |
1977 |
dl |
1.118 |
if (key == null || value == null) |
1978 |
dl |
1.1 |
throw new NullPointerException(); |
1979 |
|
|
for (;;) { |
1980 |
dl |
1.118 |
Node<K,V> n; Object v; |
1981 |
|
|
if ((n = findNode(key)) == null) |
1982 |
dl |
1.1 |
return null; |
1983 |
dl |
1.118 |
if ((v = n.value) != null && n.casValue(v, value)) { |
1984 |
|
|
@SuppressWarnings("unchecked") V vv = (V)v; |
1985 |
|
|
return vv; |
1986 |
|
|
} |
1987 |
dl |
1.1 |
} |
1988 |
|
|
} |
1989 |
|
|
|
1990 |
|
|
/* ------ SortedMap API methods ------ */ |
1991 |
|
|
|
1992 |
|
|
public Comparator<? super K> comparator() { |
1993 |
|
|
return comparator; |
1994 |
|
|
} |
1995 |
|
|
|
1996 |
|
|
/** |
1997 |
jsr166 |
1.22 |
* @throws NoSuchElementException {@inheritDoc} |
1998 |
dl |
1.1 |
*/ |
1999 |
dl |
1.9 |
public K firstKey() { |
2000 |
dl |
1.1 |
Node<K,V> n = findFirst(); |
2001 |
|
|
if (n == null) |
2002 |
|
|
throw new NoSuchElementException(); |
2003 |
|
|
return n.key; |
2004 |
|
|
} |
2005 |
|
|
|
2006 |
|
|
/** |
2007 |
jsr166 |
1.22 |
* @throws NoSuchElementException {@inheritDoc} |
2008 |
dl |
1.1 |
*/ |
2009 |
|
|
public K lastKey() { |
2010 |
|
|
Node<K,V> n = findLast(); |
2011 |
|
|
if (n == null) |
2012 |
|
|
throw new NoSuchElementException(); |
2013 |
|
|
return n.key; |
2014 |
|
|
} |
2015 |
|
|
|
2016 |
|
|
/** |
2017 |
jsr166 |
1.49 |
* @throws ClassCastException {@inheritDoc} |
2018 |
|
|
* @throws NullPointerException if {@code fromKey} or {@code toKey} is null |
2019 |
jsr166 |
1.22 |
* @throws IllegalArgumentException {@inheritDoc} |
2020 |
dl |
1.1 |
*/ |
2021 |
dl |
1.47 |
public ConcurrentNavigableMap<K,V> subMap(K fromKey, |
2022 |
|
|
boolean fromInclusive, |
2023 |
|
|
K toKey, |
2024 |
|
|
boolean toInclusive) { |
2025 |
dl |
1.1 |
if (fromKey == null || toKey == null) |
2026 |
|
|
throw new NullPointerException(); |
2027 |
dl |
1.46 |
return new SubMap<K,V> |
2028 |
|
|
(this, fromKey, fromInclusive, toKey, toInclusive, false); |
2029 |
dl |
1.1 |
} |
2030 |
|
|
|
2031 |
|
|
/** |
2032 |
jsr166 |
1.49 |
* @throws ClassCastException {@inheritDoc} |
2033 |
|
|
* @throws NullPointerException if {@code toKey} is null |
2034 |
jsr166 |
1.22 |
* @throws IllegalArgumentException {@inheritDoc} |
2035 |
dl |
1.1 |
*/ |
2036 |
dl |
1.47 |
public ConcurrentNavigableMap<K,V> headMap(K toKey, |
2037 |
|
|
boolean inclusive) { |
2038 |
dl |
1.1 |
if (toKey == null) |
2039 |
|
|
throw new NullPointerException(); |
2040 |
dl |
1.46 |
return new SubMap<K,V> |
2041 |
|
|
(this, null, false, toKey, inclusive, false); |
2042 |
dl |
1.1 |
} |
2043 |
|
|
|
2044 |
|
|
/** |
2045 |
jsr166 |
1.49 |
* @throws ClassCastException {@inheritDoc} |
2046 |
|
|
* @throws NullPointerException if {@code fromKey} is null |
2047 |
jsr166 |
1.22 |
* @throws IllegalArgumentException {@inheritDoc} |
2048 |
dl |
1.1 |
*/ |
2049 |
dl |
1.47 |
public ConcurrentNavigableMap<K,V> tailMap(K fromKey, |
2050 |
|
|
boolean inclusive) { |
2051 |
dl |
1.6 |
if (fromKey == null) |
2052 |
|
|
throw new NullPointerException(); |
2053 |
dl |
1.46 |
return new SubMap<K,V> |
2054 |
|
|
(this, fromKey, inclusive, null, false, false); |
2055 |
dl |
1.6 |
} |
2056 |
|
|
|
2057 |
|
|
/** |
2058 |
jsr166 |
1.49 |
* @throws ClassCastException {@inheritDoc} |
2059 |
|
|
* @throws NullPointerException if {@code fromKey} or {@code toKey} is null |
2060 |
jsr166 |
1.22 |
* @throws IllegalArgumentException {@inheritDoc} |
2061 |
dl |
1.6 |
*/ |
2062 |
dl |
1.37 |
public ConcurrentNavigableMap<K,V> subMap(K fromKey, K toKey) { |
2063 |
dl |
1.47 |
return subMap(fromKey, true, toKey, false); |
2064 |
dl |
1.6 |
} |
2065 |
|
|
|
2066 |
|
|
/** |
2067 |
jsr166 |
1.49 |
* @throws ClassCastException {@inheritDoc} |
2068 |
|
|
* @throws NullPointerException if {@code toKey} is null |
2069 |
jsr166 |
1.22 |
* @throws IllegalArgumentException {@inheritDoc} |
2070 |
dl |
1.6 |
*/ |
2071 |
dl |
1.37 |
public ConcurrentNavigableMap<K,V> headMap(K toKey) { |
2072 |
dl |
1.47 |
return headMap(toKey, false); |
2073 |
dl |
1.6 |
} |
2074 |
|
|
|
2075 |
|
|
/** |
2076 |
jsr166 |
1.49 |
* @throws ClassCastException {@inheritDoc} |
2077 |
|
|
* @throws NullPointerException if {@code fromKey} is null |
2078 |
jsr166 |
1.22 |
* @throws IllegalArgumentException {@inheritDoc} |
2079 |
dl |
1.6 |
*/ |
2080 |
dl |
1.37 |
public ConcurrentNavigableMap<K,V> tailMap(K fromKey) { |
2081 |
dl |
1.47 |
return tailMap(fromKey, true); |
2082 |
dl |
1.1 |
} |
2083 |
|
|
|
2084 |
|
|
/* ---------------- Relational operations -------------- */ |
2085 |
|
|
|
2086 |
|
|
/** |
2087 |
jsr166 |
1.22 |
* Returns a key-value mapping associated with the greatest key |
2088 |
jsr166 |
1.82 |
* strictly less than the given key, or {@code null} if there is |
2089 |
jsr166 |
1.22 |
* no such key. The returned entry does <em>not</em> support the |
2090 |
jsr166 |
1.82 |
* {@code Entry.setValue} method. |
2091 |
dl |
1.9 |
* |
2092 |
jsr166 |
1.22 |
* @throws ClassCastException {@inheritDoc} |
2093 |
|
|
* @throws NullPointerException if the specified key is null |
2094 |
dl |
1.1 |
*/ |
2095 |
jsr166 |
1.22 |
public Map.Entry<K,V> lowerEntry(K key) { |
2096 |
|
|
return getNear(key, LT); |
2097 |
dl |
1.1 |
} |
2098 |
|
|
|
2099 |
|
|
/** |
2100 |
jsr166 |
1.22 |
* @throws ClassCastException {@inheritDoc} |
2101 |
|
|
* @throws NullPointerException if the specified key is null |
2102 |
dl |
1.1 |
*/ |
2103 |
jsr166 |
1.22 |
public K lowerKey(K key) { |
2104 |
dl |
1.118 |
Node<K,V> n = findNear(key, LT, comparator); |
2105 |
jsr166 |
1.61 |
return (n == null) ? null : n.key; |
2106 |
dl |
1.1 |
} |
2107 |
|
|
|
2108 |
|
|
/** |
2109 |
jsr166 |
1.22 |
* Returns a key-value mapping associated with the greatest key |
2110 |
jsr166 |
1.82 |
* less than or equal to the given key, or {@code null} if there |
2111 |
jsr166 |
1.22 |
* is no such key. The returned entry does <em>not</em> support |
2112 |
jsr166 |
1.82 |
* the {@code Entry.setValue} method. |
2113 |
dl |
1.9 |
* |
2114 |
jsr166 |
1.22 |
* @param key the key |
2115 |
|
|
* @throws ClassCastException {@inheritDoc} |
2116 |
|
|
* @throws NullPointerException if the specified key is null |
2117 |
dl |
1.1 |
*/ |
2118 |
jsr166 |
1.22 |
public Map.Entry<K,V> floorEntry(K key) { |
2119 |
|
|
return getNear(key, LT|EQ); |
2120 |
dl |
1.1 |
} |
2121 |
|
|
|
2122 |
|
|
/** |
2123 |
jsr166 |
1.22 |
* @param key the key |
2124 |
|
|
* @throws ClassCastException {@inheritDoc} |
2125 |
|
|
* @throws NullPointerException if the specified key is null |
2126 |
dl |
1.1 |
*/ |
2127 |
jsr166 |
1.22 |
public K floorKey(K key) { |
2128 |
dl |
1.118 |
Node<K,V> n = findNear(key, LT|EQ, comparator); |
2129 |
jsr166 |
1.61 |
return (n == null) ? null : n.key; |
2130 |
dl |
1.1 |
} |
2131 |
|
|
|
2132 |
|
|
/** |
2133 |
jsr166 |
1.22 |
* Returns a key-value mapping associated with the least key |
2134 |
jsr166 |
1.82 |
* greater than or equal to the given key, or {@code null} if |
2135 |
jsr166 |
1.22 |
* there is no such entry. The returned entry does <em>not</em> |
2136 |
jsr166 |
1.82 |
* support the {@code Entry.setValue} method. |
2137 |
dl |
1.9 |
* |
2138 |
jsr166 |
1.22 |
* @throws ClassCastException {@inheritDoc} |
2139 |
|
|
* @throws NullPointerException if the specified key is null |
2140 |
dl |
1.1 |
*/ |
2141 |
jsr166 |
1.22 |
public Map.Entry<K,V> ceilingEntry(K key) { |
2142 |
|
|
return getNear(key, GT|EQ); |
2143 |
dl |
1.1 |
} |
2144 |
|
|
|
2145 |
|
|
/** |
2146 |
jsr166 |
1.22 |
* @throws ClassCastException {@inheritDoc} |
2147 |
|
|
* @throws NullPointerException if the specified key is null |
2148 |
dl |
1.1 |
*/ |
2149 |
jsr166 |
1.22 |
public K ceilingKey(K key) { |
2150 |
dl |
1.118 |
Node<K,V> n = findNear(key, GT|EQ, comparator); |
2151 |
jsr166 |
1.61 |
return (n == null) ? null : n.key; |
2152 |
dl |
1.1 |
} |
2153 |
|
|
|
2154 |
|
|
/** |
2155 |
|
|
* Returns a key-value mapping associated with the least key |
2156 |
jsr166 |
1.82 |
* strictly greater than the given key, or {@code null} if there |
2157 |
jsr166 |
1.22 |
* is no such key. The returned entry does <em>not</em> support |
2158 |
jsr166 |
1.82 |
* the {@code Entry.setValue} method. |
2159 |
dl |
1.9 |
* |
2160 |
jsr166 |
1.22 |
* @param key the key |
2161 |
|
|
* @throws ClassCastException {@inheritDoc} |
2162 |
|
|
* @throws NullPointerException if the specified key is null |
2163 |
dl |
1.1 |
*/ |
2164 |
|
|
public Map.Entry<K,V> higherEntry(K key) { |
2165 |
|
|
return getNear(key, GT); |
2166 |
|
|
} |
2167 |
|
|
|
2168 |
|
|
/** |
2169 |
jsr166 |
1.22 |
* @param key the key |
2170 |
|
|
* @throws ClassCastException {@inheritDoc} |
2171 |
|
|
* @throws NullPointerException if the specified key is null |
2172 |
dl |
1.1 |
*/ |
2173 |
|
|
public K higherKey(K key) { |
2174 |
dl |
1.118 |
Node<K,V> n = findNear(key, GT, comparator); |
2175 |
jsr166 |
1.61 |
return (n == null) ? null : n.key; |
2176 |
dl |
1.1 |
} |
2177 |
|
|
|
2178 |
|
|
/** |
2179 |
|
|
* Returns a key-value mapping associated with the least |
2180 |
jsr166 |
1.82 |
* key in this map, or {@code null} if the map is empty. |
2181 |
dl |
1.1 |
* The returned entry does <em>not</em> support |
2182 |
jsr166 |
1.82 |
* the {@code Entry.setValue} method. |
2183 |
dl |
1.1 |
*/ |
2184 |
|
|
public Map.Entry<K,V> firstEntry() { |
2185 |
|
|
for (;;) { |
2186 |
|
|
Node<K,V> n = findFirst(); |
2187 |
dl |
1.9 |
if (n == null) |
2188 |
dl |
1.1 |
return null; |
2189 |
dl |
1.2 |
AbstractMap.SimpleImmutableEntry<K,V> e = n.createSnapshot(); |
2190 |
dl |
1.1 |
if (e != null) |
2191 |
|
|
return e; |
2192 |
|
|
} |
2193 |
|
|
} |
2194 |
|
|
|
2195 |
|
|
/** |
2196 |
|
|
* Returns a key-value mapping associated with the greatest |
2197 |
jsr166 |
1.82 |
* key in this map, or {@code null} if the map is empty. |
2198 |
dl |
1.1 |
* The returned entry does <em>not</em> support |
2199 |
jsr166 |
1.82 |
* the {@code Entry.setValue} method. |
2200 |
dl |
1.1 |
*/ |
2201 |
|
|
public Map.Entry<K,V> lastEntry() { |
2202 |
|
|
for (;;) { |
2203 |
|
|
Node<K,V> n = findLast(); |
2204 |
dl |
1.9 |
if (n == null) |
2205 |
dl |
1.1 |
return null; |
2206 |
dl |
1.2 |
AbstractMap.SimpleImmutableEntry<K,V> e = n.createSnapshot(); |
2207 |
dl |
1.1 |
if (e != null) |
2208 |
|
|
return e; |
2209 |
|
|
} |
2210 |
|
|
} |
2211 |
|
|
|
2212 |
|
|
/** |
2213 |
|
|
* Removes and returns a key-value mapping associated with |
2214 |
jsr166 |
1.82 |
* the least key in this map, or {@code null} if the map is empty. |
2215 |
dl |
1.1 |
* The returned entry does <em>not</em> support |
2216 |
jsr166 |
1.82 |
* the {@code Entry.setValue} method. |
2217 |
dl |
1.1 |
*/ |
2218 |
|
|
public Map.Entry<K,V> pollFirstEntry() { |
2219 |
dl |
1.25 |
return doRemoveFirstEntry(); |
2220 |
dl |
1.1 |
} |
2221 |
|
|
|
2222 |
|
|
/** |
2223 |
|
|
* Removes and returns a key-value mapping associated with |
2224 |
jsr166 |
1.82 |
* the greatest key in this map, or {@code null} if the map is empty. |
2225 |
dl |
1.1 |
* The returned entry does <em>not</em> support |
2226 |
jsr166 |
1.82 |
* the {@code Entry.setValue} method. |
2227 |
dl |
1.1 |
*/ |
2228 |
|
|
public Map.Entry<K,V> pollLastEntry() { |
2229 |
dl |
1.31 |
return doRemoveLastEntry(); |
2230 |
dl |
1.1 |
} |
2231 |
|
|
|
2232 |
|
|
|
2233 |
|
|
/* ---------------- Iterators -------------- */ |
2234 |
|
|
|
2235 |
|
|
/** |
2236 |
dl |
1.46 |
* Base of iterator classes: |
2237 |
dl |
1.1 |
*/ |
2238 |
dl |
1.46 |
abstract class Iter<T> implements Iterator<T> { |
2239 |
dl |
1.1 |
/** the last node returned by next() */ |
2240 |
jsr166 |
1.52 |
Node<K,V> lastReturned; |
2241 |
dl |
1.1 |
/** the next node to return from next(); */ |
2242 |
|
|
Node<K,V> next; |
2243 |
jsr166 |
1.55 |
/** Cache of next value field to maintain weak consistency */ |
2244 |
|
|
V nextValue; |
2245 |
dl |
1.1 |
|
2246 |
jsr166 |
1.13 |
/** Initializes ascending iterator for entire range. */ |
2247 |
dl |
1.46 |
Iter() { |
2248 |
dl |
1.118 |
while ((next = findFirst()) != null) { |
2249 |
jsr166 |
1.52 |
Object x = next.value; |
2250 |
|
|
if (x != null && x != next) { |
2251 |
dl |
1.100 |
@SuppressWarnings("unchecked") V vv = (V)x; |
2252 |
|
|
nextValue = vv; |
2253 |
dl |
1.1 |
break; |
2254 |
jsr166 |
1.55 |
} |
2255 |
dl |
1.1 |
} |
2256 |
|
|
} |
2257 |
|
|
|
2258 |
dl |
1.46 |
public final boolean hasNext() { |
2259 |
|
|
return next != null; |
2260 |
dl |
1.1 |
} |
2261 |
dl |
1.46 |
|
2262 |
jsr166 |
1.13 |
/** Advances next to higher entry. */ |
2263 |
dl |
1.46 |
final void advance() { |
2264 |
jsr166 |
1.54 |
if (next == null) |
2265 |
dl |
1.1 |
throw new NoSuchElementException(); |
2266 |
jsr166 |
1.55 |
lastReturned = next; |
2267 |
dl |
1.118 |
while ((next = next.next) != null) { |
2268 |
jsr166 |
1.52 |
Object x = next.value; |
2269 |
|
|
if (x != null && x != next) { |
2270 |
dl |
1.100 |
@SuppressWarnings("unchecked") V vv = (V)x; |
2271 |
|
|
nextValue = vv; |
2272 |
dl |
1.1 |
break; |
2273 |
jsr166 |
1.55 |
} |
2274 |
dl |
1.1 |
} |
2275 |
|
|
} |
2276 |
|
|
|
2277 |
|
|
public void remove() { |
2278 |
jsr166 |
1.52 |
Node<K,V> l = lastReturned; |
2279 |
dl |
1.1 |
if (l == null) |
2280 |
|
|
throw new IllegalStateException(); |
2281 |
|
|
// It would not be worth all of the overhead to directly |
2282 |
|
|
// unlink from here. Using remove is fast enough. |
2283 |
|
|
ConcurrentSkipListMap.this.remove(l.key); |
2284 |
jsr166 |
1.55 |
lastReturned = null; |
2285 |
dl |
1.1 |
} |
2286 |
|
|
|
2287 |
|
|
} |
2288 |
|
|
|
2289 |
dl |
1.46 |
final class ValueIterator extends Iter<V> { |
2290 |
dl |
1.9 |
public V next() { |
2291 |
jsr166 |
1.52 |
V v = nextValue; |
2292 |
dl |
1.46 |
advance(); |
2293 |
jsr166 |
1.52 |
return v; |
2294 |
dl |
1.1 |
} |
2295 |
|
|
} |
2296 |
|
|
|
2297 |
dl |
1.46 |
final class KeyIterator extends Iter<K> { |
2298 |
dl |
1.9 |
public K next() { |
2299 |
dl |
1.1 |
Node<K,V> n = next; |
2300 |
dl |
1.46 |
advance(); |
2301 |
dl |
1.1 |
return n.key; |
2302 |
|
|
} |
2303 |
|
|
} |
2304 |
|
|
|
2305 |
dl |
1.46 |
final class EntryIterator extends Iter<Map.Entry<K,V>> { |
2306 |
|
|
public Map.Entry<K,V> next() { |
2307 |
|
|
Node<K,V> n = next; |
2308 |
jsr166 |
1.52 |
V v = nextValue; |
2309 |
dl |
1.46 |
advance(); |
2310 |
|
|
return new AbstractMap.SimpleImmutableEntry<K,V>(n.key, v); |
2311 |
dl |
1.1 |
} |
2312 |
dl |
1.46 |
} |
2313 |
dl |
1.1 |
|
2314 |
dl |
1.46 |
// Factory methods for iterators needed by ConcurrentSkipListSet etc |
2315 |
|
|
|
2316 |
|
|
Iterator<K> keyIterator() { |
2317 |
dl |
1.1 |
return new KeyIterator(); |
2318 |
|
|
} |
2319 |
|
|
|
2320 |
dl |
1.46 |
Iterator<V> valueIterator() { |
2321 |
|
|
return new ValueIterator(); |
2322 |
dl |
1.1 |
} |
2323 |
|
|
|
2324 |
dl |
1.46 |
Iterator<Map.Entry<K,V>> entryIterator() { |
2325 |
|
|
return new EntryIterator(); |
2326 |
dl |
1.1 |
} |
2327 |
|
|
|
2328 |
dl |
1.46 |
/* ---------------- View Classes -------------- */ |
2329 |
|
|
|
2330 |
|
|
/* |
2331 |
|
|
* View classes are static, delegating to a ConcurrentNavigableMap |
2332 |
|
|
* to allow use by SubMaps, which outweighs the ugliness of |
2333 |
|
|
* needing type-tests for Iterator methods. |
2334 |
|
|
*/ |
2335 |
|
|
|
2336 |
jsr166 |
1.53 |
static final <E> List<E> toList(Collection<E> c) { |
2337 |
jsr166 |
1.55 |
// Using size() here would be a pessimization. |
2338 |
jsr166 |
1.90 |
ArrayList<E> list = new ArrayList<E>(); |
2339 |
jsr166 |
1.55 |
for (E e : c) |
2340 |
|
|
list.add(e); |
2341 |
|
|
return list; |
2342 |
jsr166 |
1.53 |
} |
2343 |
|
|
|
2344 |
jsr166 |
1.62 |
static final class KeySet<E> |
2345 |
|
|
extends AbstractSet<E> implements NavigableSet<E> { |
2346 |
dl |
1.118 |
final ConcurrentNavigableMap<E,?> m; |
2347 |
jsr166 |
1.71 |
KeySet(ConcurrentNavigableMap<E,?> map) { m = map; } |
2348 |
dl |
1.46 |
public int size() { return m.size(); } |
2349 |
|
|
public boolean isEmpty() { return m.isEmpty(); } |
2350 |
|
|
public boolean contains(Object o) { return m.containsKey(o); } |
2351 |
|
|
public boolean remove(Object o) { return m.remove(o) != null; } |
2352 |
|
|
public void clear() { m.clear(); } |
2353 |
|
|
public E lower(E e) { return m.lowerKey(e); } |
2354 |
|
|
public E floor(E e) { return m.floorKey(e); } |
2355 |
|
|
public E ceiling(E e) { return m.ceilingKey(e); } |
2356 |
|
|
public E higher(E e) { return m.higherKey(e); } |
2357 |
|
|
public Comparator<? super E> comparator() { return m.comparator(); } |
2358 |
|
|
public E first() { return m.firstKey(); } |
2359 |
|
|
public E last() { return m.lastKey(); } |
2360 |
|
|
public E pollFirst() { |
2361 |
jsr166 |
1.71 |
Map.Entry<E,?> e = m.pollFirstEntry(); |
2362 |
jsr166 |
1.61 |
return (e == null) ? null : e.getKey(); |
2363 |
dl |
1.46 |
} |
2364 |
|
|
public E pollLast() { |
2365 |
jsr166 |
1.71 |
Map.Entry<E,?> e = m.pollLastEntry(); |
2366 |
jsr166 |
1.61 |
return (e == null) ? null : e.getKey(); |
2367 |
dl |
1.46 |
} |
2368 |
dl |
1.100 |
@SuppressWarnings("unchecked") |
2369 |
dl |
1.46 |
public Iterator<E> iterator() { |
2370 |
|
|
if (m instanceof ConcurrentSkipListMap) |
2371 |
|
|
return ((ConcurrentSkipListMap<E,Object>)m).keyIterator(); |
2372 |
|
|
else |
2373 |
|
|
return ((ConcurrentSkipListMap.SubMap<E,Object>)m).keyIterator(); |
2374 |
dl |
1.1 |
} |
2375 |
dl |
1.45 |
public boolean equals(Object o) { |
2376 |
|
|
if (o == this) |
2377 |
|
|
return true; |
2378 |
|
|
if (!(o instanceof Set)) |
2379 |
|
|
return false; |
2380 |
|
|
Collection<?> c = (Collection<?>) o; |
2381 |
|
|
try { |
2382 |
|
|
return containsAll(c) && c.containsAll(this); |
2383 |
jsr166 |
1.81 |
} catch (ClassCastException unused) { |
2384 |
dl |
1.45 |
return false; |
2385 |
|
|
} catch (NullPointerException unused) { |
2386 |
|
|
return false; |
2387 |
|
|
} |
2388 |
|
|
} |
2389 |
jsr166 |
1.55 |
public Object[] toArray() { return toList(this).toArray(); } |
2390 |
|
|
public <T> T[] toArray(T[] a) { return toList(this).toArray(a); } |
2391 |
dl |
1.46 |
public Iterator<E> descendingIterator() { |
2392 |
|
|
return descendingSet().iterator(); |
2393 |
|
|
} |
2394 |
dl |
1.47 |
public NavigableSet<E> subSet(E fromElement, |
2395 |
|
|
boolean fromInclusive, |
2396 |
|
|
E toElement, |
2397 |
|
|
boolean toInclusive) { |
2398 |
jsr166 |
1.56 |
return new KeySet<E>(m.subMap(fromElement, fromInclusive, |
2399 |
|
|
toElement, toInclusive)); |
2400 |
dl |
1.46 |
} |
2401 |
dl |
1.47 |
public NavigableSet<E> headSet(E toElement, boolean inclusive) { |
2402 |
jsr166 |
1.56 |
return new KeySet<E>(m.headMap(toElement, inclusive)); |
2403 |
dl |
1.46 |
} |
2404 |
dl |
1.47 |
public NavigableSet<E> tailSet(E fromElement, boolean inclusive) { |
2405 |
jsr166 |
1.56 |
return new KeySet<E>(m.tailMap(fromElement, inclusive)); |
2406 |
dl |
1.46 |
} |
2407 |
jsr166 |
1.51 |
public NavigableSet<E> subSet(E fromElement, E toElement) { |
2408 |
dl |
1.47 |
return subSet(fromElement, true, toElement, false); |
2409 |
dl |
1.46 |
} |
2410 |
jsr166 |
1.51 |
public NavigableSet<E> headSet(E toElement) { |
2411 |
dl |
1.47 |
return headSet(toElement, false); |
2412 |
dl |
1.46 |
} |
2413 |
jsr166 |
1.51 |
public NavigableSet<E> tailSet(E fromElement) { |
2414 |
dl |
1.47 |
return tailSet(fromElement, true); |
2415 |
dl |
1.46 |
} |
2416 |
|
|
public NavigableSet<E> descendingSet() { |
2417 |
jsr166 |
1.71 |
return new KeySet<E>(m.descendingMap()); |
2418 |
dl |
1.46 |
} |
2419 |
dl |
1.100 |
@SuppressWarnings("unchecked") |
2420 |
dl |
1.106 |
public Spliterator<E> spliterator() { |
2421 |
dl |
1.100 |
if (m instanceof ConcurrentSkipListMap) |
2422 |
|
|
return ((ConcurrentSkipListMap<E,?>)m).keySpliterator(); |
2423 |
|
|
else |
2424 |
|
|
return (Spliterator<E>)((SubMap<E,?>)m).keyIterator(); |
2425 |
|
|
} |
2426 |
dl |
1.1 |
} |
2427 |
|
|
|
2428 |
dl |
1.46 |
static final class Values<E> extends AbstractCollection<E> { |
2429 |
jsr166 |
1.134 |
final ConcurrentNavigableMap<?,E> m; |
2430 |
|
|
Values(ConcurrentNavigableMap<?,E> map) { |
2431 |
dl |
1.46 |
m = map; |
2432 |
dl |
1.1 |
} |
2433 |
dl |
1.100 |
@SuppressWarnings("unchecked") |
2434 |
dl |
1.46 |
public Iterator<E> iterator() { |
2435 |
|
|
if (m instanceof ConcurrentSkipListMap) |
2436 |
jsr166 |
1.71 |
return ((ConcurrentSkipListMap<?,E>)m).valueIterator(); |
2437 |
dl |
1.46 |
else |
2438 |
jsr166 |
1.71 |
return ((SubMap<?,E>)m).valueIterator(); |
2439 |
dl |
1.1 |
} |
2440 |
|
|
public boolean isEmpty() { |
2441 |
dl |
1.46 |
return m.isEmpty(); |
2442 |
dl |
1.1 |
} |
2443 |
|
|
public int size() { |
2444 |
dl |
1.46 |
return m.size(); |
2445 |
dl |
1.1 |
} |
2446 |
|
|
public boolean contains(Object o) { |
2447 |
dl |
1.46 |
return m.containsValue(o); |
2448 |
dl |
1.1 |
} |
2449 |
|
|
public void clear() { |
2450 |
dl |
1.46 |
m.clear(); |
2451 |
dl |
1.1 |
} |
2452 |
jsr166 |
1.55 |
public Object[] toArray() { return toList(this).toArray(); } |
2453 |
|
|
public <T> T[] toArray(T[] a) { return toList(this).toArray(a); } |
2454 |
dl |
1.100 |
@SuppressWarnings("unchecked") |
2455 |
dl |
1.106 |
public Spliterator<E> spliterator() { |
2456 |
dl |
1.100 |
if (m instanceof ConcurrentSkipListMap) |
2457 |
|
|
return ((ConcurrentSkipListMap<?,E>)m).valueSpliterator(); |
2458 |
|
|
else |
2459 |
|
|
return (Spliterator<E>)((SubMap<?,E>)m).valueIterator(); |
2460 |
|
|
} |
2461 |
dl |
1.1 |
} |
2462 |
|
|
|
2463 |
dl |
1.46 |
static final class EntrySet<K1,V1> extends AbstractSet<Map.Entry<K1,V1>> { |
2464 |
dl |
1.118 |
final ConcurrentNavigableMap<K1, V1> m; |
2465 |
dl |
1.46 |
EntrySet(ConcurrentNavigableMap<K1, V1> map) { |
2466 |
|
|
m = map; |
2467 |
dl |
1.1 |
} |
2468 |
dl |
1.100 |
@SuppressWarnings("unchecked") |
2469 |
dl |
1.46 |
public Iterator<Map.Entry<K1,V1>> iterator() { |
2470 |
|
|
if (m instanceof ConcurrentSkipListMap) |
2471 |
|
|
return ((ConcurrentSkipListMap<K1,V1>)m).entryIterator(); |
2472 |
|
|
else |
2473 |
|
|
return ((SubMap<K1,V1>)m).entryIterator(); |
2474 |
|
|
} |
2475 |
dl |
1.47 |
|
2476 |
dl |
1.1 |
public boolean contains(Object o) { |
2477 |
|
|
if (!(o instanceof Map.Entry)) |
2478 |
|
|
return false; |
2479 |
jsr166 |
1.73 |
Map.Entry<?,?> e = (Map.Entry<?,?>)o; |
2480 |
dl |
1.46 |
V1 v = m.get(e.getKey()); |
2481 |
dl |
1.1 |
return v != null && v.equals(e.getValue()); |
2482 |
|
|
} |
2483 |
|
|
public boolean remove(Object o) { |
2484 |
|
|
if (!(o instanceof Map.Entry)) |
2485 |
|
|
return false; |
2486 |
jsr166 |
1.73 |
Map.Entry<?,?> e = (Map.Entry<?,?>)o; |
2487 |
dl |
1.46 |
return m.remove(e.getKey(), |
2488 |
dl |
1.47 |
e.getValue()); |
2489 |
dl |
1.1 |
} |
2490 |
|
|
public boolean isEmpty() { |
2491 |
dl |
1.46 |
return m.isEmpty(); |
2492 |
dl |
1.1 |
} |
2493 |
|
|
public int size() { |
2494 |
dl |
1.46 |
return m.size(); |
2495 |
dl |
1.1 |
} |
2496 |
|
|
public void clear() { |
2497 |
dl |
1.46 |
m.clear(); |
2498 |
dl |
1.1 |
} |
2499 |
dl |
1.45 |
public boolean equals(Object o) { |
2500 |
|
|
if (o == this) |
2501 |
|
|
return true; |
2502 |
|
|
if (!(o instanceof Set)) |
2503 |
|
|
return false; |
2504 |
|
|
Collection<?> c = (Collection<?>) o; |
2505 |
|
|
try { |
2506 |
|
|
return containsAll(c) && c.containsAll(this); |
2507 |
jsr166 |
1.81 |
} catch (ClassCastException unused) { |
2508 |
dl |
1.45 |
return false; |
2509 |
|
|
} catch (NullPointerException unused) { |
2510 |
|
|
return false; |
2511 |
|
|
} |
2512 |
|
|
} |
2513 |
jsr166 |
1.55 |
public Object[] toArray() { return toList(this).toArray(); } |
2514 |
|
|
public <T> T[] toArray(T[] a) { return toList(this).toArray(a); } |
2515 |
dl |
1.100 |
@SuppressWarnings("unchecked") |
2516 |
dl |
1.106 |
public Spliterator<Map.Entry<K1,V1>> spliterator() { |
2517 |
dl |
1.83 |
if (m instanceof ConcurrentSkipListMap) |
2518 |
dl |
1.100 |
return ((ConcurrentSkipListMap<K1,V1>)m).entrySpliterator(); |
2519 |
dl |
1.83 |
else |
2520 |
dl |
1.100 |
return (Spliterator<Map.Entry<K1,V1>>) |
2521 |
|
|
((SubMap<K1,V1>)m).entryIterator(); |
2522 |
|
|
} |
2523 |
dl |
1.143 |
public boolean removeIf(Predicate<? super Entry<K1, V1>> filter) { |
2524 |
|
|
return ((ConcurrentSkipListMap<K1,V1>)m).removeEntryIf(filter); |
2525 |
|
|
} |
2526 |
dl |
1.1 |
} |
2527 |
|
|
|
2528 |
|
|
/** |
2529 |
|
|
* Submaps returned by {@link ConcurrentSkipListMap} submap operations |
2530 |
|
|
* represent a subrange of mappings of their underlying |
2531 |
|
|
* maps. Instances of this class support all methods of their |
2532 |
|
|
* underlying maps, differing in that mappings outside their range are |
2533 |
|
|
* ignored, and attempts to add mappings outside their ranges result |
2534 |
|
|
* in {@link IllegalArgumentException}. Instances of this class are |
2535 |
jsr166 |
1.82 |
* constructed only using the {@code subMap}, {@code headMap}, and |
2536 |
|
|
* {@code tailMap} methods of their underlying maps. |
2537 |
jsr166 |
1.52 |
* |
2538 |
|
|
* @serial include |
2539 |
dl |
1.1 |
*/ |
2540 |
dl |
1.46 |
static final class SubMap<K,V> extends AbstractMap<K,V> |
2541 |
jsr166 |
1.130 |
implements ConcurrentNavigableMap<K,V>, Cloneable, Serializable { |
2542 |
dl |
1.1 |
private static final long serialVersionUID = -7647078645895051609L; |
2543 |
|
|
|
2544 |
|
|
/** Underlying map */ |
2545 |
|
|
private final ConcurrentSkipListMap<K,V> m; |
2546 |
|
|
/** lower bound key, or null if from start */ |
2547 |
dl |
1.46 |
private final K lo; |
2548 |
|
|
/** upper bound key, or null if to end */ |
2549 |
|
|
private final K hi; |
2550 |
|
|
/** inclusion flag for lo */ |
2551 |
|
|
private final boolean loInclusive; |
2552 |
|
|
/** inclusion flag for hi */ |
2553 |
|
|
private final boolean hiInclusive; |
2554 |
|
|
/** direction */ |
2555 |
|
|
private final boolean isDescending; |
2556 |
|
|
|
2557 |
dl |
1.1 |
// Lazily initialized view holders |
2558 |
dl |
1.46 |
private transient KeySet<K> keySetView; |
2559 |
dl |
1.1 |
private transient Set<Map.Entry<K,V>> entrySetView; |
2560 |
|
|
private transient Collection<V> valuesView; |
2561 |
|
|
|
2562 |
|
|
/** |
2563 |
jsr166 |
1.87 |
* Creates a new submap, initializing all fields. |
2564 |
dl |
1.46 |
*/ |
2565 |
|
|
SubMap(ConcurrentSkipListMap<K,V> map, |
2566 |
|
|
K fromKey, boolean fromInclusive, |
2567 |
|
|
K toKey, boolean toInclusive, |
2568 |
|
|
boolean isDescending) { |
2569 |
dl |
1.118 |
Comparator<? super K> cmp = map.comparator; |
2570 |
dl |
1.47 |
if (fromKey != null && toKey != null && |
2571 |
dl |
1.118 |
cpr(cmp, fromKey, toKey) > 0) |
2572 |
dl |
1.1 |
throw new IllegalArgumentException("inconsistent range"); |
2573 |
|
|
this.m = map; |
2574 |
dl |
1.46 |
this.lo = fromKey; |
2575 |
|
|
this.hi = toKey; |
2576 |
|
|
this.loInclusive = fromInclusive; |
2577 |
|
|
this.hiInclusive = toInclusive; |
2578 |
|
|
this.isDescending = isDescending; |
2579 |
dl |
1.1 |
} |
2580 |
|
|
|
2581 |
|
|
/* ---------------- Utilities -------------- */ |
2582 |
|
|
|
2583 |
dl |
1.118 |
boolean tooLow(Object key, Comparator<? super K> cmp) { |
2584 |
|
|
int c; |
2585 |
|
|
return (lo != null && ((c = cpr(cmp, key, lo)) < 0 || |
2586 |
|
|
(c == 0 && !loInclusive))); |
2587 |
dl |
1.1 |
} |
2588 |
|
|
|
2589 |
dl |
1.118 |
boolean tooHigh(Object key, Comparator<? super K> cmp) { |
2590 |
|
|
int c; |
2591 |
|
|
return (hi != null && ((c = cpr(cmp, key, hi)) > 0 || |
2592 |
|
|
(c == 0 && !hiInclusive))); |
2593 |
dl |
1.1 |
} |
2594 |
|
|
|
2595 |
dl |
1.118 |
boolean inBounds(Object key, Comparator<? super K> cmp) { |
2596 |
|
|
return !tooLow(key, cmp) && !tooHigh(key, cmp); |
2597 |
dl |
1.1 |
} |
2598 |
|
|
|
2599 |
dl |
1.118 |
void checkKeyBounds(K key, Comparator<? super K> cmp) { |
2600 |
dl |
1.46 |
if (key == null) |
2601 |
|
|
throw new NullPointerException(); |
2602 |
dl |
1.118 |
if (!inBounds(key, cmp)) |
2603 |
dl |
1.46 |
throw new IllegalArgumentException("key out of range"); |
2604 |
dl |
1.1 |
} |
2605 |
|
|
|
2606 |
dl |
1.46 |
/** |
2607 |
jsr166 |
1.87 |
* Returns true if node key is less than upper bound of range. |
2608 |
dl |
1.46 |
*/ |
2609 |
dl |
1.118 |
boolean isBeforeEnd(ConcurrentSkipListMap.Node<K,V> n, |
2610 |
|
|
Comparator<? super K> cmp) { |
2611 |
dl |
1.46 |
if (n == null) |
2612 |
|
|
return false; |
2613 |
|
|
if (hi == null) |
2614 |
|
|
return true; |
2615 |
|
|
K k = n.key; |
2616 |
|
|
if (k == null) // pass by markers and headers |
2617 |
|
|
return true; |
2618 |
dl |
1.118 |
int c = cpr(cmp, k, hi); |
2619 |
dl |
1.46 |
if (c > 0 || (c == 0 && !hiInclusive)) |
2620 |
|
|
return false; |
2621 |
|
|
return true; |
2622 |
dl |
1.1 |
} |
2623 |
|
|
|
2624 |
dl |
1.46 |
/** |
2625 |
|
|
* Returns lowest node. This node might not be in range, so |
2626 |
jsr166 |
1.87 |
* most usages need to check bounds. |
2627 |
dl |
1.46 |
*/ |
2628 |
dl |
1.118 |
ConcurrentSkipListMap.Node<K,V> loNode(Comparator<? super K> cmp) { |
2629 |
dl |
1.46 |
if (lo == null) |
2630 |
|
|
return m.findFirst(); |
2631 |
|
|
else if (loInclusive) |
2632 |
dl |
1.118 |
return m.findNear(lo, GT|EQ, cmp); |
2633 |
dl |
1.46 |
else |
2634 |
dl |
1.118 |
return m.findNear(lo, GT, cmp); |
2635 |
dl |
1.1 |
} |
2636 |
|
|
|
2637 |
|
|
/** |
2638 |
dl |
1.46 |
* Returns highest node. This node might not be in range, so |
2639 |
jsr166 |
1.87 |
* most usages need to check bounds. |
2640 |
dl |
1.1 |
*/ |
2641 |
dl |
1.118 |
ConcurrentSkipListMap.Node<K,V> hiNode(Comparator<? super K> cmp) { |
2642 |
dl |
1.46 |
if (hi == null) |
2643 |
|
|
return m.findLast(); |
2644 |
|
|
else if (hiInclusive) |
2645 |
dl |
1.118 |
return m.findNear(hi, LT|EQ, cmp); |
2646 |
dl |
1.46 |
else |
2647 |
dl |
1.118 |
return m.findNear(hi, LT, cmp); |
2648 |
dl |
1.1 |
} |
2649 |
|
|
|
2650 |
|
|
/** |
2651 |
jsr166 |
1.136 |
* Returns lowest absolute key (ignoring directionality). |
2652 |
dl |
1.1 |
*/ |
2653 |
dl |
1.118 |
K lowestKey() { |
2654 |
|
|
Comparator<? super K> cmp = m.comparator; |
2655 |
|
|
ConcurrentSkipListMap.Node<K,V> n = loNode(cmp); |
2656 |
|
|
if (isBeforeEnd(n, cmp)) |
2657 |
dl |
1.46 |
return n.key; |
2658 |
|
|
else |
2659 |
|
|
throw new NoSuchElementException(); |
2660 |
dl |
1.47 |
} |
2661 |
dl |
1.46 |
|
2662 |
|
|
/** |
2663 |
jsr166 |
1.136 |
* Returns highest absolute key (ignoring directionality). |
2664 |
dl |
1.46 |
*/ |
2665 |
dl |
1.118 |
K highestKey() { |
2666 |
|
|
Comparator<? super K> cmp = m.comparator; |
2667 |
|
|
ConcurrentSkipListMap.Node<K,V> n = hiNode(cmp); |
2668 |
dl |
1.46 |
if (n != null) { |
2669 |
|
|
K last = n.key; |
2670 |
dl |
1.118 |
if (inBounds(last, cmp)) |
2671 |
dl |
1.46 |
return last; |
2672 |
|
|
} |
2673 |
|
|
throw new NoSuchElementException(); |
2674 |
|
|
} |
2675 |
|
|
|
2676 |
dl |
1.118 |
Map.Entry<K,V> lowestEntry() { |
2677 |
|
|
Comparator<? super K> cmp = m.comparator; |
2678 |
dl |
1.46 |
for (;;) { |
2679 |
dl |
1.118 |
ConcurrentSkipListMap.Node<K,V> n = loNode(cmp); |
2680 |
|
|
if (!isBeforeEnd(n, cmp)) |
2681 |
dl |
1.46 |
return null; |
2682 |
|
|
Map.Entry<K,V> e = n.createSnapshot(); |
2683 |
|
|
if (e != null) |
2684 |
|
|
return e; |
2685 |
|
|
} |
2686 |
|
|
} |
2687 |
|
|
|
2688 |
dl |
1.118 |
Map.Entry<K,V> highestEntry() { |
2689 |
|
|
Comparator<? super K> cmp = m.comparator; |
2690 |
dl |
1.46 |
for (;;) { |
2691 |
dl |
1.118 |
ConcurrentSkipListMap.Node<K,V> n = hiNode(cmp); |
2692 |
|
|
if (n == null || !inBounds(n.key, cmp)) |
2693 |
dl |
1.46 |
return null; |
2694 |
|
|
Map.Entry<K,V> e = n.createSnapshot(); |
2695 |
|
|
if (e != null) |
2696 |
|
|
return e; |
2697 |
|
|
} |
2698 |
|
|
} |
2699 |
|
|
|
2700 |
dl |
1.118 |
Map.Entry<K,V> removeLowest() { |
2701 |
|
|
Comparator<? super K> cmp = m.comparator; |
2702 |
dl |
1.46 |
for (;;) { |
2703 |
dl |
1.118 |
Node<K,V> n = loNode(cmp); |
2704 |
dl |
1.46 |
if (n == null) |
2705 |
|
|
return null; |
2706 |
|
|
K k = n.key; |
2707 |
dl |
1.118 |
if (!inBounds(k, cmp)) |
2708 |
dl |
1.46 |
return null; |
2709 |
dl |
1.118 |
V v = m.doRemove(k, null); |
2710 |
dl |
1.46 |
if (v != null) |
2711 |
|
|
return new AbstractMap.SimpleImmutableEntry<K,V>(k, v); |
2712 |
|
|
} |
2713 |
|
|
} |
2714 |
|
|
|
2715 |
dl |
1.118 |
Map.Entry<K,V> removeHighest() { |
2716 |
|
|
Comparator<? super K> cmp = m.comparator; |
2717 |
dl |
1.46 |
for (;;) { |
2718 |
dl |
1.118 |
Node<K,V> n = hiNode(cmp); |
2719 |
dl |
1.46 |
if (n == null) |
2720 |
|
|
return null; |
2721 |
|
|
K k = n.key; |
2722 |
dl |
1.118 |
if (!inBounds(k, cmp)) |
2723 |
dl |
1.46 |
return null; |
2724 |
dl |
1.118 |
V v = m.doRemove(k, null); |
2725 |
dl |
1.46 |
if (v != null) |
2726 |
|
|
return new AbstractMap.SimpleImmutableEntry<K,V>(k, v); |
2727 |
|
|
} |
2728 |
dl |
1.1 |
} |
2729 |
|
|
|
2730 |
|
|
/** |
2731 |
dl |
1.46 |
* Submap version of ConcurrentSkipListMap.getNearEntry |
2732 |
dl |
1.1 |
*/ |
2733 |
dl |
1.118 |
Map.Entry<K,V> getNearEntry(K key, int rel) { |
2734 |
|
|
Comparator<? super K> cmp = m.comparator; |
2735 |
dl |
1.46 |
if (isDescending) { // adjust relation for direction |
2736 |
jsr166 |
1.70 |
if ((rel & LT) == 0) |
2737 |
|
|
rel |= LT; |
2738 |
dl |
1.46 |
else |
2739 |
jsr166 |
1.70 |
rel &= ~LT; |
2740 |
dl |
1.46 |
} |
2741 |
dl |
1.118 |
if (tooLow(key, cmp)) |
2742 |
jsr166 |
1.70 |
return ((rel & LT) != 0) ? null : lowestEntry(); |
2743 |
dl |
1.118 |
if (tooHigh(key, cmp)) |
2744 |
jsr166 |
1.70 |
return ((rel & LT) != 0) ? highestEntry() : null; |
2745 |
dl |
1.46 |
for (;;) { |
2746 |
dl |
1.118 |
Node<K,V> n = m.findNear(key, rel, cmp); |
2747 |
|
|
if (n == null || !inBounds(n.key, cmp)) |
2748 |
dl |
1.46 |
return null; |
2749 |
|
|
K k = n.key; |
2750 |
|
|
V v = n.getValidValue(); |
2751 |
|
|
if (v != null) |
2752 |
|
|
return new AbstractMap.SimpleImmutableEntry<K,V>(k, v); |
2753 |
|
|
} |
2754 |
dl |
1.1 |
} |
2755 |
|
|
|
2756 |
jsr166 |
1.48 |
// Almost the same as getNearEntry, except for keys |
2757 |
dl |
1.118 |
K getNearKey(K key, int rel) { |
2758 |
|
|
Comparator<? super K> cmp = m.comparator; |
2759 |
dl |
1.46 |
if (isDescending) { // adjust relation for direction |
2760 |
jsr166 |
1.70 |
if ((rel & LT) == 0) |
2761 |
|
|
rel |= LT; |
2762 |
dl |
1.46 |
else |
2763 |
jsr166 |
1.70 |
rel &= ~LT; |
2764 |
dl |
1.46 |
} |
2765 |
dl |
1.118 |
if (tooLow(key, cmp)) { |
2766 |
jsr166 |
1.70 |
if ((rel & LT) == 0) { |
2767 |
dl |
1.118 |
ConcurrentSkipListMap.Node<K,V> n = loNode(cmp); |
2768 |
|
|
if (isBeforeEnd(n, cmp)) |
2769 |
dl |
1.46 |
return n.key; |
2770 |
|
|
} |
2771 |
|
|
return null; |
2772 |
|
|
} |
2773 |
dl |
1.118 |
if (tooHigh(key, cmp)) { |
2774 |
jsr166 |
1.70 |
if ((rel & LT) != 0) { |
2775 |
dl |
1.118 |
ConcurrentSkipListMap.Node<K,V> n = hiNode(cmp); |
2776 |
dl |
1.46 |
if (n != null) { |
2777 |
|
|
K last = n.key; |
2778 |
dl |
1.118 |
if (inBounds(last, cmp)) |
2779 |
dl |
1.46 |
return last; |
2780 |
|
|
} |
2781 |
|
|
} |
2782 |
|
|
return null; |
2783 |
|
|
} |
2784 |
|
|
for (;;) { |
2785 |
dl |
1.118 |
Node<K,V> n = m.findNear(key, rel, cmp); |
2786 |
|
|
if (n == null || !inBounds(n.key, cmp)) |
2787 |
dl |
1.46 |
return null; |
2788 |
|
|
K k = n.key; |
2789 |
|
|
V v = n.getValidValue(); |
2790 |
|
|
if (v != null) |
2791 |
|
|
return k; |
2792 |
|
|
} |
2793 |
|
|
} |
2794 |
dl |
1.1 |
|
2795 |
|
|
/* ---------------- Map API methods -------------- */ |
2796 |
|
|
|
2797 |
|
|
public boolean containsKey(Object key) { |
2798 |
dl |
1.46 |
if (key == null) throw new NullPointerException(); |
2799 |
dl |
1.118 |
return inBounds(key, m.comparator) && m.containsKey(key); |
2800 |
dl |
1.1 |
} |
2801 |
|
|
|
2802 |
|
|
public V get(Object key) { |
2803 |
dl |
1.46 |
if (key == null) throw new NullPointerException(); |
2804 |
dl |
1.118 |
return (!inBounds(key, m.comparator)) ? null : m.get(key); |
2805 |
dl |
1.1 |
} |
2806 |
|
|
|
2807 |
|
|
public V put(K key, V value) { |
2808 |
dl |
1.118 |
checkKeyBounds(key, m.comparator); |
2809 |
dl |
1.1 |
return m.put(key, value); |
2810 |
|
|
} |
2811 |
|
|
|
2812 |
|
|
public V remove(Object key) { |
2813 |
dl |
1.118 |
return (!inBounds(key, m.comparator)) ? null : m.remove(key); |
2814 |
dl |
1.1 |
} |
2815 |
|
|
|
2816 |
|
|
public int size() { |
2817 |
dl |
1.118 |
Comparator<? super K> cmp = m.comparator; |
2818 |
dl |
1.1 |
long count = 0; |
2819 |
dl |
1.118 |
for (ConcurrentSkipListMap.Node<K,V> n = loNode(cmp); |
2820 |
|
|
isBeforeEnd(n, cmp); |
2821 |
dl |
1.1 |
n = n.next) { |
2822 |
|
|
if (n.getValidValue() != null) |
2823 |
|
|
++count; |
2824 |
|
|
} |
2825 |
jsr166 |
1.61 |
return count >= Integer.MAX_VALUE ? Integer.MAX_VALUE : (int)count; |
2826 |
dl |
1.1 |
} |
2827 |
|
|
|
2828 |
|
|
public boolean isEmpty() { |
2829 |
dl |
1.118 |
Comparator<? super K> cmp = m.comparator; |
2830 |
|
|
return !isBeforeEnd(loNode(cmp), cmp); |
2831 |
dl |
1.1 |
} |
2832 |
|
|
|
2833 |
|
|
public boolean containsValue(Object value) { |
2834 |
dl |
1.9 |
if (value == null) |
2835 |
dl |
1.1 |
throw new NullPointerException(); |
2836 |
dl |
1.118 |
Comparator<? super K> cmp = m.comparator; |
2837 |
|
|
for (ConcurrentSkipListMap.Node<K,V> n = loNode(cmp); |
2838 |
|
|
isBeforeEnd(n, cmp); |
2839 |
dl |
1.1 |
n = n.next) { |
2840 |
|
|
V v = n.getValidValue(); |
2841 |
|
|
if (v != null && value.equals(v)) |
2842 |
|
|
return true; |
2843 |
|
|
} |
2844 |
|
|
return false; |
2845 |
|
|
} |
2846 |
|
|
|
2847 |
|
|
public void clear() { |
2848 |
dl |
1.118 |
Comparator<? super K> cmp = m.comparator; |
2849 |
|
|
for (ConcurrentSkipListMap.Node<K,V> n = loNode(cmp); |
2850 |
|
|
isBeforeEnd(n, cmp); |
2851 |
dl |
1.1 |
n = n.next) { |
2852 |
|
|
if (n.getValidValue() != null) |
2853 |
|
|
m.remove(n.key); |
2854 |
|
|
} |
2855 |
|
|
} |
2856 |
|
|
|
2857 |
|
|
/* ---------------- ConcurrentMap API methods -------------- */ |
2858 |
|
|
|
2859 |
|
|
public V putIfAbsent(K key, V value) { |
2860 |
dl |
1.118 |
checkKeyBounds(key, m.comparator); |
2861 |
dl |
1.1 |
return m.putIfAbsent(key, value); |
2862 |
|
|
} |
2863 |
|
|
|
2864 |
|
|
public boolean remove(Object key, Object value) { |
2865 |
dl |
1.118 |
return inBounds(key, m.comparator) && m.remove(key, value); |
2866 |
dl |
1.1 |
} |
2867 |
|
|
|
2868 |
|
|
public boolean replace(K key, V oldValue, V newValue) { |
2869 |
dl |
1.118 |
checkKeyBounds(key, m.comparator); |
2870 |
dl |
1.1 |
return m.replace(key, oldValue, newValue); |
2871 |
|
|
} |
2872 |
|
|
|
2873 |
|
|
public V replace(K key, V value) { |
2874 |
dl |
1.118 |
checkKeyBounds(key, m.comparator); |
2875 |
dl |
1.1 |
return m.replace(key, value); |
2876 |
|
|
} |
2877 |
|
|
|
2878 |
|
|
/* ---------------- SortedMap API methods -------------- */ |
2879 |
|
|
|
2880 |
|
|
public Comparator<? super K> comparator() { |
2881 |
dl |
1.46 |
Comparator<? super K> cmp = m.comparator(); |
2882 |
jsr166 |
1.55 |
if (isDescending) |
2883 |
|
|
return Collections.reverseOrder(cmp); |
2884 |
|
|
else |
2885 |
|
|
return cmp; |
2886 |
dl |
1.1 |
} |
2887 |
dl |
1.47 |
|
2888 |
dl |
1.46 |
/** |
2889 |
|
|
* Utility to create submaps, where given bounds override |
2890 |
|
|
* unbounded(null) ones and/or are checked against bounded ones. |
2891 |
|
|
*/ |
2892 |
dl |
1.118 |
SubMap<K,V> newSubMap(K fromKey, boolean fromInclusive, |
2893 |
|
|
K toKey, boolean toInclusive) { |
2894 |
|
|
Comparator<? super K> cmp = m.comparator; |
2895 |
dl |
1.46 |
if (isDescending) { // flip senses |
2896 |
dl |
1.47 |
K tk = fromKey; |
2897 |
|
|
fromKey = toKey; |
2898 |
dl |
1.46 |
toKey = tk; |
2899 |
dl |
1.47 |
boolean ti = fromInclusive; |
2900 |
|
|
fromInclusive = toInclusive; |
2901 |
dl |
1.46 |
toInclusive = ti; |
2902 |
|
|
} |
2903 |
|
|
if (lo != null) { |
2904 |
|
|
if (fromKey == null) { |
2905 |
|
|
fromKey = lo; |
2906 |
|
|
fromInclusive = loInclusive; |
2907 |
|
|
} |
2908 |
|
|
else { |
2909 |
dl |
1.118 |
int c = cpr(cmp, fromKey, lo); |
2910 |
dl |
1.46 |
if (c < 0 || (c == 0 && !loInclusive && fromInclusive)) |
2911 |
|
|
throw new IllegalArgumentException("key out of range"); |
2912 |
|
|
} |
2913 |
|
|
} |
2914 |
|
|
if (hi != null) { |
2915 |
|
|
if (toKey == null) { |
2916 |
|
|
toKey = hi; |
2917 |
|
|
toInclusive = hiInclusive; |
2918 |
|
|
} |
2919 |
|
|
else { |
2920 |
dl |
1.118 |
int c = cpr(cmp, toKey, hi); |
2921 |
dl |
1.46 |
if (c > 0 || (c == 0 && !hiInclusive && toInclusive)) |
2922 |
|
|
throw new IllegalArgumentException("key out of range"); |
2923 |
|
|
} |
2924 |
dl |
1.1 |
} |
2925 |
dl |
1.47 |
return new SubMap<K,V>(m, fromKey, fromInclusive, |
2926 |
dl |
1.46 |
toKey, toInclusive, isDescending); |
2927 |
dl |
1.1 |
} |
2928 |
|
|
|
2929 |
dl |
1.118 |
public SubMap<K,V> subMap(K fromKey, boolean fromInclusive, |
2930 |
|
|
K toKey, boolean toInclusive) { |
2931 |
dl |
1.1 |
if (fromKey == null || toKey == null) |
2932 |
|
|
throw new NullPointerException(); |
2933 |
dl |
1.46 |
return newSubMap(fromKey, fromInclusive, toKey, toInclusive); |
2934 |
dl |
1.1 |
} |
2935 |
dl |
1.47 |
|
2936 |
dl |
1.118 |
public SubMap<K,V> headMap(K toKey, boolean inclusive) { |
2937 |
dl |
1.1 |
if (toKey == null) |
2938 |
|
|
throw new NullPointerException(); |
2939 |
dl |
1.46 |
return newSubMap(null, false, toKey, inclusive); |
2940 |
dl |
1.1 |
} |
2941 |
dl |
1.47 |
|
2942 |
dl |
1.118 |
public SubMap<K,V> tailMap(K fromKey, boolean inclusive) { |
2943 |
dl |
1.1 |
if (fromKey == null) |
2944 |
|
|
throw new NullPointerException(); |
2945 |
dl |
1.46 |
return newSubMap(fromKey, inclusive, null, false); |
2946 |
|
|
} |
2947 |
|
|
|
2948 |
|
|
public SubMap<K,V> subMap(K fromKey, K toKey) { |
2949 |
dl |
1.47 |
return subMap(fromKey, true, toKey, false); |
2950 |
dl |
1.1 |
} |
2951 |
|
|
|
2952 |
dl |
1.46 |
public SubMap<K,V> headMap(K toKey) { |
2953 |
dl |
1.47 |
return headMap(toKey, false); |
2954 |
dl |
1.6 |
} |
2955 |
|
|
|
2956 |
dl |
1.46 |
public SubMap<K,V> tailMap(K fromKey) { |
2957 |
dl |
1.47 |
return tailMap(fromKey, true); |
2958 |
dl |
1.6 |
} |
2959 |
|
|
|
2960 |
dl |
1.46 |
public SubMap<K,V> descendingMap() { |
2961 |
dl |
1.47 |
return new SubMap<K,V>(m, lo, loInclusive, |
2962 |
dl |
1.46 |
hi, hiInclusive, !isDescending); |
2963 |
dl |
1.6 |
} |
2964 |
|
|
|
2965 |
dl |
1.1 |
/* ---------------- Relational methods -------------- */ |
2966 |
|
|
|
2967 |
|
|
public Map.Entry<K,V> ceilingEntry(K key) { |
2968 |
jsr166 |
1.70 |
return getNearEntry(key, GT|EQ); |
2969 |
dl |
1.1 |
} |
2970 |
|
|
|
2971 |
|
|
public K ceilingKey(K key) { |
2972 |
jsr166 |
1.70 |
return getNearKey(key, GT|EQ); |
2973 |
dl |
1.1 |
} |
2974 |
|
|
|
2975 |
|
|
public Map.Entry<K,V> lowerEntry(K key) { |
2976 |
jsr166 |
1.70 |
return getNearEntry(key, LT); |
2977 |
dl |
1.1 |
} |
2978 |
|
|
|
2979 |
|
|
public K lowerKey(K key) { |
2980 |
jsr166 |
1.70 |
return getNearKey(key, LT); |
2981 |
dl |
1.1 |
} |
2982 |
|
|
|
2983 |
|
|
public Map.Entry<K,V> floorEntry(K key) { |
2984 |
jsr166 |
1.70 |
return getNearEntry(key, LT|EQ); |
2985 |
dl |
1.1 |
} |
2986 |
|
|
|
2987 |
|
|
public K floorKey(K key) { |
2988 |
jsr166 |
1.70 |
return getNearKey(key, LT|EQ); |
2989 |
dl |
1.1 |
} |
2990 |
|
|
|
2991 |
|
|
public Map.Entry<K,V> higherEntry(K key) { |
2992 |
jsr166 |
1.70 |
return getNearEntry(key, GT); |
2993 |
dl |
1.1 |
} |
2994 |
|
|
|
2995 |
|
|
public K higherKey(K key) { |
2996 |
jsr166 |
1.70 |
return getNearKey(key, GT); |
2997 |
dl |
1.46 |
} |
2998 |
|
|
|
2999 |
|
|
public K firstKey() { |
3000 |
jsr166 |
1.61 |
return isDescending ? highestKey() : lowestKey(); |
3001 |
dl |
1.46 |
} |
3002 |
|
|
|
3003 |
|
|
public K lastKey() { |
3004 |
jsr166 |
1.61 |
return isDescending ? lowestKey() : highestKey(); |
3005 |
dl |
1.1 |
} |
3006 |
|
|
|
3007 |
|
|
public Map.Entry<K,V> firstEntry() { |
3008 |
jsr166 |
1.61 |
return isDescending ? highestEntry() : lowestEntry(); |
3009 |
dl |
1.1 |
} |
3010 |
|
|
|
3011 |
|
|
public Map.Entry<K,V> lastEntry() { |
3012 |
jsr166 |
1.61 |
return isDescending ? lowestEntry() : highestEntry(); |
3013 |
dl |
1.1 |
} |
3014 |
|
|
|
3015 |
|
|
public Map.Entry<K,V> pollFirstEntry() { |
3016 |
jsr166 |
1.61 |
return isDescending ? removeHighest() : removeLowest(); |
3017 |
dl |
1.1 |
} |
3018 |
|
|
|
3019 |
|
|
public Map.Entry<K,V> pollLastEntry() { |
3020 |
jsr166 |
1.61 |
return isDescending ? removeLowest() : removeHighest(); |
3021 |
dl |
1.1 |
} |
3022 |
|
|
|
3023 |
|
|
/* ---------------- Submap Views -------------- */ |
3024 |
|
|
|
3025 |
jsr166 |
1.51 |
public NavigableSet<K> keySet() { |
3026 |
dl |
1.46 |
KeySet<K> ks = keySetView; |
3027 |
jsr166 |
1.71 |
return (ks != null) ? ks : (keySetView = new KeySet<K>(this)); |
3028 |
dl |
1.1 |
} |
3029 |
|
|
|
3030 |
dl |
1.46 |
public NavigableSet<K> navigableKeySet() { |
3031 |
|
|
KeySet<K> ks = keySetView; |
3032 |
jsr166 |
1.71 |
return (ks != null) ? ks : (keySetView = new KeySet<K>(this)); |
3033 |
dl |
1.46 |
} |
3034 |
dl |
1.45 |
|
3035 |
dl |
1.46 |
public Collection<V> values() { |
3036 |
|
|
Collection<V> vs = valuesView; |
3037 |
jsr166 |
1.71 |
return (vs != null) ? vs : (valuesView = new Values<V>(this)); |
3038 |
dl |
1.1 |
} |
3039 |
|
|
|
3040 |
dl |
1.46 |
public Set<Map.Entry<K,V>> entrySet() { |
3041 |
|
|
Set<Map.Entry<K,V>> es = entrySetView; |
3042 |
jsr166 |
1.71 |
return (es != null) ? es : (entrySetView = new EntrySet<K,V>(this)); |
3043 |
dl |
1.1 |
} |
3044 |
|
|
|
3045 |
dl |
1.46 |
public NavigableSet<K> descendingKeySet() { |
3046 |
|
|
return descendingMap().navigableKeySet(); |
3047 |
dl |
1.1 |
} |
3048 |
|
|
|
3049 |
dl |
1.46 |
Iterator<K> keyIterator() { |
3050 |
|
|
return new SubMapKeyIterator(); |
3051 |
dl |
1.1 |
} |
3052 |
|
|
|
3053 |
dl |
1.46 |
Iterator<V> valueIterator() { |
3054 |
|
|
return new SubMapValueIterator(); |
3055 |
dl |
1.1 |
} |
3056 |
|
|
|
3057 |
dl |
1.46 |
Iterator<Map.Entry<K,V>> entryIterator() { |
3058 |
|
|
return new SubMapEntryIterator(); |
3059 |
dl |
1.1 |
} |
3060 |
|
|
|
3061 |
dl |
1.46 |
/** |
3062 |
|
|
* Variant of main Iter class to traverse through submaps. |
3063 |
dl |
1.100 |
* Also serves as back-up Spliterator for views |
3064 |
dl |
1.46 |
*/ |
3065 |
dl |
1.100 |
abstract class SubMapIter<T> implements Iterator<T>, Spliterator<T> { |
3066 |
dl |
1.46 |
/** the last node returned by next() */ |
3067 |
jsr166 |
1.52 |
Node<K,V> lastReturned; |
3068 |
dl |
1.46 |
/** the next node to return from next(); */ |
3069 |
|
|
Node<K,V> next; |
3070 |
|
|
/** Cache of next value field to maintain weak consistency */ |
3071 |
jsr166 |
1.52 |
V nextValue; |
3072 |
dl |
1.46 |
|
3073 |
dl |
1.47 |
SubMapIter() { |
3074 |
dl |
1.118 |
Comparator<? super K> cmp = m.comparator; |
3075 |
dl |
1.46 |
for (;;) { |
3076 |
dl |
1.118 |
next = isDescending ? hiNode(cmp) : loNode(cmp); |
3077 |
dl |
1.46 |
if (next == null) |
3078 |
|
|
break; |
3079 |
jsr166 |
1.55 |
Object x = next.value; |
3080 |
jsr166 |
1.52 |
if (x != null && x != next) { |
3081 |
dl |
1.118 |
if (! inBounds(next.key, cmp)) |
3082 |
dl |
1.46 |
next = null; |
3083 |
dl |
1.118 |
else { |
3084 |
|
|
@SuppressWarnings("unchecked") V vv = (V)x; |
3085 |
dl |
1.100 |
nextValue = vv; |
3086 |
dl |
1.118 |
} |
3087 |
dl |
1.46 |
break; |
3088 |
|
|
} |
3089 |
|
|
} |
3090 |
dl |
1.1 |
} |
3091 |
dl |
1.46 |
|
3092 |
|
|
public final boolean hasNext() { |
3093 |
|
|
return next != null; |
3094 |
dl |
1.1 |
} |
3095 |
dl |
1.46 |
|
3096 |
|
|
final void advance() { |
3097 |
jsr166 |
1.54 |
if (next == null) |
3098 |
dl |
1.46 |
throw new NoSuchElementException(); |
3099 |
jsr166 |
1.55 |
lastReturned = next; |
3100 |
dl |
1.46 |
if (isDescending) |
3101 |
|
|
descend(); |
3102 |
|
|
else |
3103 |
|
|
ascend(); |
3104 |
dl |
1.1 |
} |
3105 |
dl |
1.46 |
|
3106 |
|
|
private void ascend() { |
3107 |
dl |
1.118 |
Comparator<? super K> cmp = m.comparator; |
3108 |
dl |
1.46 |
for (;;) { |
3109 |
|
|
next = next.next; |
3110 |
|
|
if (next == null) |
3111 |
|
|
break; |
3112 |
jsr166 |
1.55 |
Object x = next.value; |
3113 |
jsr166 |
1.52 |
if (x != null && x != next) { |
3114 |
dl |
1.118 |
if (tooHigh(next.key, cmp)) |
3115 |
dl |
1.46 |
next = null; |
3116 |
dl |
1.118 |
else { |
3117 |
|
|
@SuppressWarnings("unchecked") V vv = (V)x; |
3118 |
dl |
1.100 |
nextValue = vv; |
3119 |
dl |
1.118 |
} |
3120 |
dl |
1.46 |
break; |
3121 |
|
|
} |
3122 |
|
|
} |
3123 |
|
|
} |
3124 |
|
|
|
3125 |
|
|
private void descend() { |
3126 |
dl |
1.88 |
Comparator<? super K> cmp = m.comparator; |
3127 |
dl |
1.46 |
for (;;) { |
3128 |
jsr166 |
1.125 |
next = m.findNear(lastReturned.key, LT, cmp); |
3129 |
dl |
1.46 |
if (next == null) |
3130 |
|
|
break; |
3131 |
jsr166 |
1.55 |
Object x = next.value; |
3132 |
jsr166 |
1.52 |
if (x != null && x != next) { |
3133 |
dl |
1.118 |
if (tooLow(next.key, cmp)) |
3134 |
dl |
1.46 |
next = null; |
3135 |
dl |
1.118 |
else { |
3136 |
|
|
@SuppressWarnings("unchecked") V vv = (V)x; |
3137 |
dl |
1.100 |
nextValue = vv; |
3138 |
dl |
1.118 |
} |
3139 |
dl |
1.46 |
break; |
3140 |
|
|
} |
3141 |
|
|
} |
3142 |
dl |
1.1 |
} |
3143 |
dl |
1.46 |
|
3144 |
|
|
public void remove() { |
3145 |
jsr166 |
1.52 |
Node<K,V> l = lastReturned; |
3146 |
dl |
1.46 |
if (l == null) |
3147 |
|
|
throw new IllegalStateException(); |
3148 |
|
|
m.remove(l.key); |
3149 |
jsr166 |
1.55 |
lastReturned = null; |
3150 |
dl |
1.1 |
} |
3151 |
dl |
1.46 |
|
3152 |
dl |
1.107 |
public Spliterator<T> trySplit() { |
3153 |
|
|
return null; |
3154 |
jsr166 |
1.108 |
} |
3155 |
dl |
1.107 |
|
3156 |
dl |
1.100 |
public boolean tryAdvance(Consumer<? super T> action) { |
3157 |
|
|
if (hasNext()) { |
3158 |
|
|
action.accept(next()); |
3159 |
|
|
return true; |
3160 |
|
|
} |
3161 |
|
|
return false; |
3162 |
|
|
} |
3163 |
|
|
|
3164 |
dl |
1.113 |
public void forEachRemaining(Consumer<? super T> action) { |
3165 |
dl |
1.100 |
while (hasNext()) |
3166 |
|
|
action.accept(next()); |
3167 |
|
|
} |
3168 |
dl |
1.113 |
|
3169 |
jsr166 |
1.114 |
public long estimateSize() { |
3170 |
|
|
return Long.MAX_VALUE; |
3171 |
dl |
1.113 |
} |
3172 |
|
|
|
3173 |
dl |
1.46 |
} |
3174 |
|
|
|
3175 |
|
|
final class SubMapValueIterator extends SubMapIter<V> { |
3176 |
|
|
public V next() { |
3177 |
jsr166 |
1.52 |
V v = nextValue; |
3178 |
dl |
1.46 |
advance(); |
3179 |
jsr166 |
1.52 |
return v; |
3180 |
dl |
1.45 |
} |
3181 |
dl |
1.100 |
public int characteristics() { |
3182 |
|
|
return 0; |
3183 |
|
|
} |
3184 |
dl |
1.1 |
} |
3185 |
|
|
|
3186 |
dl |
1.46 |
final class SubMapKeyIterator extends SubMapIter<K> { |
3187 |
|
|
public K next() { |
3188 |
|
|
Node<K,V> n = next; |
3189 |
|
|
advance(); |
3190 |
|
|
return n.key; |
3191 |
|
|
} |
3192 |
dl |
1.100 |
public int characteristics() { |
3193 |
|
|
return Spliterator.DISTINCT | Spliterator.ORDERED | |
3194 |
|
|
Spliterator.SORTED; |
3195 |
|
|
} |
3196 |
jsr166 |
1.115 |
public final Comparator<? super K> getComparator() { |
3197 |
dl |
1.100 |
return SubMap.this.comparator(); |
3198 |
|
|
} |
3199 |
dl |
1.1 |
} |
3200 |
|
|
|
3201 |
dl |
1.46 |
final class SubMapEntryIterator extends SubMapIter<Map.Entry<K,V>> { |
3202 |
|
|
public Map.Entry<K,V> next() { |
3203 |
|
|
Node<K,V> n = next; |
3204 |
jsr166 |
1.52 |
V v = nextValue; |
3205 |
dl |
1.46 |
advance(); |
3206 |
|
|
return new AbstractMap.SimpleImmutableEntry<K,V>(n.key, v); |
3207 |
dl |
1.1 |
} |
3208 |
dl |
1.100 |
public int characteristics() { |
3209 |
|
|
return Spliterator.DISTINCT; |
3210 |
|
|
} |
3211 |
dl |
1.1 |
} |
3212 |
|
|
} |
3213 |
dl |
1.59 |
|
3214 |
dl |
1.123 |
// default Map method overrides |
3215 |
|
|
|
3216 |
|
|
public void forEach(BiConsumer<? super K, ? super V> action) { |
3217 |
|
|
if (action == null) throw new NullPointerException(); |
3218 |
|
|
V v; |
3219 |
|
|
for (Node<K,V> n = findFirst(); n != null; n = n.next) { |
3220 |
|
|
if ((v = n.getValidValue()) != null) |
3221 |
|
|
action.accept(n.key, v); |
3222 |
|
|
} |
3223 |
|
|
} |
3224 |
|
|
|
3225 |
|
|
public void replaceAll(BiFunction<? super K, ? super V, ? extends V> function) { |
3226 |
|
|
if (function == null) throw new NullPointerException(); |
3227 |
|
|
V v; |
3228 |
|
|
for (Node<K,V> n = findFirst(); n != null; n = n.next) { |
3229 |
|
|
while ((v = n.getValidValue()) != null) { |
3230 |
|
|
V r = function.apply(n.key, v); |
3231 |
|
|
if (r == null) throw new NullPointerException(); |
3232 |
|
|
if (n.casValue(v, r)) |
3233 |
|
|
break; |
3234 |
|
|
} |
3235 |
|
|
} |
3236 |
|
|
} |
3237 |
|
|
|
3238 |
dl |
1.83 |
/** |
3239 |
dl |
1.143 |
* Helper method for EntrySet.removeIf |
3240 |
|
|
*/ |
3241 |
|
|
boolean removeEntryIf(Predicate<? super Entry<K, V>> function) { |
3242 |
|
|
if (function == null) throw new NullPointerException(); |
3243 |
|
|
boolean removed = false; |
3244 |
|
|
for (Node<K,V> n = findFirst(); n != null; n = n.next) { |
3245 |
|
|
V v; |
3246 |
|
|
if ((v = n.getValidValue()) != null) { |
3247 |
|
|
K k = n.key; |
3248 |
|
|
Map.Entry<K,V> e = new AbstractMap.SimpleImmutableEntry<>(k, v); |
3249 |
|
|
if (function.test(e) && remove(k, v)) |
3250 |
|
|
removed = true; |
3251 |
|
|
} |
3252 |
|
|
} |
3253 |
|
|
return removed; |
3254 |
|
|
} |
3255 |
|
|
|
3256 |
|
|
/** |
3257 |
dl |
1.83 |
* Base class providing common structure for Spliterators. |
3258 |
|
|
* (Although not all that much common functionality; as usual for |
3259 |
|
|
* view classes, details annoyingly vary in key, value, and entry |
3260 |
|
|
* subclasses in ways that are not worth abstracting out for |
3261 |
|
|
* internal classes.) |
3262 |
|
|
* |
3263 |
|
|
* The basic split strategy is to recursively descend from top |
3264 |
|
|
* level, row by row, descending to next row when either split |
3265 |
|
|
* off, or the end of row is encountered. Control of the number of |
3266 |
|
|
* splits relies on some statistical estimation: The expected |
3267 |
|
|
* remaining number of elements of a skip list when advancing |
3268 |
|
|
* either across or down decreases by about 25%. To make this |
3269 |
|
|
* observation useful, we need to know initial size, which we |
3270 |
dl |
1.104 |
* don't. But we can just use Integer.MAX_VALUE so that we |
3271 |
|
|
* don't prematurely zero out while splitting. |
3272 |
dl |
1.83 |
*/ |
3273 |
jsr166 |
1.119 |
abstract static class CSLMSpliterator<K,V> { |
3274 |
dl |
1.83 |
final Comparator<? super K> comparator; |
3275 |
|
|
final K fence; // exclusive upper bound for keys, or null if to end |
3276 |
|
|
Index<K,V> row; // the level to split out |
3277 |
|
|
Node<K,V> current; // current traversal node; initialize at origin |
3278 |
|
|
int est; // pseudo-size estimate |
3279 |
|
|
CSLMSpliterator(Comparator<? super K> comparator, Index<K,V> row, |
3280 |
|
|
Node<K,V> origin, K fence, int est) { |
3281 |
|
|
this.comparator = comparator; this.row = row; |
3282 |
|
|
this.current = origin; this.fence = fence; this.est = est; |
3283 |
|
|
} |
3284 |
|
|
|
3285 |
|
|
public final long estimateSize() { return (long)est; } |
3286 |
|
|
} |
3287 |
|
|
|
3288 |
|
|
static final class KeySpliterator<K,V> extends CSLMSpliterator<K,V> |
3289 |
dl |
1.88 |
implements Spliterator<K> { |
3290 |
dl |
1.83 |
KeySpliterator(Comparator<? super K> comparator, Index<K,V> row, |
3291 |
|
|
Node<K,V> origin, K fence, int est) { |
3292 |
|
|
super(comparator, row, origin, fence, est); |
3293 |
|
|
} |
3294 |
|
|
|
3295 |
dl |
1.104 |
public Spliterator<K> trySplit() { |
3296 |
dl |
1.116 |
Node<K,V> e; K ek; |
3297 |
dl |
1.83 |
Comparator<? super K> cmp = comparator; |
3298 |
|
|
K f = fence; |
3299 |
dl |
1.116 |
if ((e = current) != null && (ek = e.key) != null) { |
3300 |
dl |
1.83 |
for (Index<K,V> q = row; q != null; q = row = q.down) { |
3301 |
dl |
1.118 |
Index<K,V> s; Node<K,V> b, n; K sk; |
3302 |
|
|
if ((s = q.right) != null && (b = s.node) != null && |
3303 |
|
|
(n = b.next) != null && n.value != null && |
3304 |
|
|
(sk = n.key) != null && cpr(cmp, sk, ek) > 0 && |
3305 |
|
|
(f == null || cpr(cmp, sk, f) < 0)) { |
3306 |
|
|
current = n; |
3307 |
|
|
Index<K,V> r = q.down; |
3308 |
|
|
row = (s.right != null) ? s : s.down; |
3309 |
|
|
est -= est >>> 2; |
3310 |
|
|
return new KeySpliterator<K,V>(cmp, r, e, sk, est); |
3311 |
dl |
1.83 |
} |
3312 |
|
|
} |
3313 |
|
|
} |
3314 |
|
|
return null; |
3315 |
|
|
} |
3316 |
|
|
|
3317 |
dl |
1.113 |
public void forEachRemaining(Consumer<? super K> action) { |
3318 |
dl |
1.100 |
if (action == null) throw new NullPointerException(); |
3319 |
dl |
1.118 |
Comparator<? super K> cmp = comparator; |
3320 |
dl |
1.83 |
K f = fence; |
3321 |
|
|
Node<K,V> e = current; |
3322 |
|
|
current = null; |
3323 |
jsr166 |
1.84 |
for (; e != null; e = e.next) { |
3324 |
dl |
1.83 |
K k; Object v; |
3325 |
dl |
1.118 |
if ((k = e.key) != null && f != null && cpr(cmp, f, k) <= 0) |
3326 |
dl |
1.83 |
break; |
3327 |
|
|
if ((v = e.value) != null && v != e) |
3328 |
dl |
1.100 |
action.accept(k); |
3329 |
dl |
1.83 |
} |
3330 |
|
|
} |
3331 |
|
|
|
3332 |
dl |
1.100 |
public boolean tryAdvance(Consumer<? super K> action) { |
3333 |
|
|
if (action == null) throw new NullPointerException(); |
3334 |
dl |
1.118 |
Comparator<? super K> cmp = comparator; |
3335 |
|
|
K f = fence; |
3336 |
|
|
Node<K,V> e = current; |
3337 |
|
|
for (; e != null; e = e.next) { |
3338 |
dl |
1.83 |
K k; Object v; |
3339 |
dl |
1.118 |
if ((k = e.key) != null && f != null && cpr(cmp, f, k) <= 0) { |
3340 |
dl |
1.83 |
e = null; |
3341 |
|
|
break; |
3342 |
|
|
} |
3343 |
|
|
if ((v = e.value) != null && v != e) { |
3344 |
|
|
current = e.next; |
3345 |
dl |
1.100 |
action.accept(k); |
3346 |
dl |
1.83 |
return true; |
3347 |
|
|
} |
3348 |
|
|
} |
3349 |
|
|
current = e; |
3350 |
|
|
return false; |
3351 |
|
|
} |
3352 |
dl |
1.100 |
|
3353 |
|
|
public int characteristics() { |
3354 |
jsr166 |
1.102 |
return Spliterator.DISTINCT | Spliterator.SORTED | |
3355 |
jsr166 |
1.101 |
Spliterator.ORDERED | Spliterator.CONCURRENT | |
3356 |
dl |
1.100 |
Spliterator.NONNULL; |
3357 |
|
|
} |
3358 |
|
|
|
3359 |
jsr166 |
1.115 |
public final Comparator<? super K> getComparator() { |
3360 |
dl |
1.100 |
return comparator; |
3361 |
|
|
} |
3362 |
dl |
1.83 |
} |
3363 |
jsr166 |
1.120 |
// factory method for KeySpliterator |
3364 |
dl |
1.118 |
final KeySpliterator<K,V> keySpliterator() { |
3365 |
|
|
Comparator<? super K> cmp = comparator; |
3366 |
|
|
for (;;) { // ensure h corresponds to origin p |
3367 |
|
|
HeadIndex<K,V> h; Node<K,V> p; |
3368 |
|
|
Node<K,V> b = (h = head).node; |
3369 |
|
|
if ((p = b.next) == null || p.value != null) |
3370 |
|
|
return new KeySpliterator<K,V>(cmp, h, p, null, (p == null) ? |
3371 |
|
|
0 : Integer.MAX_VALUE); |
3372 |
|
|
p.helpDelete(b, p.next); |
3373 |
|
|
} |
3374 |
|
|
} |
3375 |
dl |
1.83 |
|
3376 |
|
|
static final class ValueSpliterator<K,V> extends CSLMSpliterator<K,V> |
3377 |
dl |
1.88 |
implements Spliterator<V> { |
3378 |
dl |
1.83 |
ValueSpliterator(Comparator<? super K> comparator, Index<K,V> row, |
3379 |
|
|
Node<K,V> origin, K fence, int est) { |
3380 |
|
|
super(comparator, row, origin, fence, est); |
3381 |
|
|
} |
3382 |
|
|
|
3383 |
dl |
1.104 |
public Spliterator<V> trySplit() { |
3384 |
dl |
1.116 |
Node<K,V> e; K ek; |
3385 |
dl |
1.83 |
Comparator<? super K> cmp = comparator; |
3386 |
|
|
K f = fence; |
3387 |
dl |
1.116 |
if ((e = current) != null && (ek = e.key) != null) { |
3388 |
dl |
1.83 |
for (Index<K,V> q = row; q != null; q = row = q.down) { |
3389 |
dl |
1.118 |
Index<K,V> s; Node<K,V> b, n; K sk; |
3390 |
|
|
if ((s = q.right) != null && (b = s.node) != null && |
3391 |
|
|
(n = b.next) != null && n.value != null && |
3392 |
|
|
(sk = n.key) != null && cpr(cmp, sk, ek) > 0 && |
3393 |
|
|
(f == null || cpr(cmp, sk, f) < 0)) { |
3394 |
|
|
current = n; |
3395 |
|
|
Index<K,V> r = q.down; |
3396 |
|
|
row = (s.right != null) ? s : s.down; |
3397 |
|
|
est -= est >>> 2; |
3398 |
|
|
return new ValueSpliterator<K,V>(cmp, r, e, sk, est); |
3399 |
dl |
1.83 |
} |
3400 |
|
|
} |
3401 |
|
|
} |
3402 |
|
|
return null; |
3403 |
|
|
} |
3404 |
|
|
|
3405 |
dl |
1.113 |
public void forEachRemaining(Consumer<? super V> action) { |
3406 |
dl |
1.100 |
if (action == null) throw new NullPointerException(); |
3407 |
dl |
1.118 |
Comparator<? super K> cmp = comparator; |
3408 |
dl |
1.83 |
K f = fence; |
3409 |
|
|
Node<K,V> e = current; |
3410 |
|
|
current = null; |
3411 |
jsr166 |
1.84 |
for (; e != null; e = e.next) { |
3412 |
dl |
1.83 |
K k; Object v; |
3413 |
dl |
1.118 |
if ((k = e.key) != null && f != null && cpr(cmp, f, k) <= 0) |
3414 |
dl |
1.83 |
break; |
3415 |
dl |
1.100 |
if ((v = e.value) != null && v != e) { |
3416 |
|
|
@SuppressWarnings("unchecked") V vv = (V)v; |
3417 |
|
|
action.accept(vv); |
3418 |
|
|
} |
3419 |
dl |
1.83 |
} |
3420 |
|
|
} |
3421 |
|
|
|
3422 |
dl |
1.100 |
public boolean tryAdvance(Consumer<? super V> action) { |
3423 |
|
|
if (action == null) throw new NullPointerException(); |
3424 |
dl |
1.118 |
Comparator<? super K> cmp = comparator; |
3425 |
|
|
K f = fence; |
3426 |
|
|
Node<K,V> e = current; |
3427 |
|
|
for (; e != null; e = e.next) { |
3428 |
dl |
1.83 |
K k; Object v; |
3429 |
dl |
1.118 |
if ((k = e.key) != null && f != null && cpr(cmp, f, k) <= 0) { |
3430 |
dl |
1.83 |
e = null; |
3431 |
|
|
break; |
3432 |
|
|
} |
3433 |
|
|
if ((v = e.value) != null && v != e) { |
3434 |
|
|
current = e.next; |
3435 |
dl |
1.100 |
@SuppressWarnings("unchecked") V vv = (V)v; |
3436 |
|
|
action.accept(vv); |
3437 |
dl |
1.83 |
return true; |
3438 |
|
|
} |
3439 |
|
|
} |
3440 |
|
|
current = e; |
3441 |
|
|
return false; |
3442 |
|
|
} |
3443 |
dl |
1.100 |
|
3444 |
|
|
public int characteristics() { |
3445 |
jsr166 |
1.130 |
return Spliterator.CONCURRENT | Spliterator.ORDERED | |
3446 |
|
|
Spliterator.NONNULL; |
3447 |
dl |
1.100 |
} |
3448 |
dl |
1.83 |
} |
3449 |
|
|
|
3450 |
dl |
1.118 |
// Almost the same as keySpliterator() |
3451 |
|
|
final ValueSpliterator<K,V> valueSpliterator() { |
3452 |
|
|
Comparator<? super K> cmp = comparator; |
3453 |
|
|
for (;;) { |
3454 |
|
|
HeadIndex<K,V> h; Node<K,V> p; |
3455 |
|
|
Node<K,V> b = (h = head).node; |
3456 |
|
|
if ((p = b.next) == null || p.value != null) |
3457 |
|
|
return new ValueSpliterator<K,V>(cmp, h, p, null, (p == null) ? |
3458 |
|
|
0 : Integer.MAX_VALUE); |
3459 |
|
|
p.helpDelete(b, p.next); |
3460 |
|
|
} |
3461 |
|
|
} |
3462 |
|
|
|
3463 |
dl |
1.83 |
static final class EntrySpliterator<K,V> extends CSLMSpliterator<K,V> |
3464 |
dl |
1.88 |
implements Spliterator<Map.Entry<K,V>> { |
3465 |
dl |
1.83 |
EntrySpliterator(Comparator<? super K> comparator, Index<K,V> row, |
3466 |
|
|
Node<K,V> origin, K fence, int est) { |
3467 |
|
|
super(comparator, row, origin, fence, est); |
3468 |
|
|
} |
3469 |
|
|
|
3470 |
dl |
1.104 |
public Spliterator<Map.Entry<K,V>> trySplit() { |
3471 |
dl |
1.116 |
Node<K,V> e; K ek; |
3472 |
dl |
1.83 |
Comparator<? super K> cmp = comparator; |
3473 |
|
|
K f = fence; |
3474 |
dl |
1.116 |
if ((e = current) != null && (ek = e.key) != null) { |
3475 |
dl |
1.83 |
for (Index<K,V> q = row; q != null; q = row = q.down) { |
3476 |
dl |
1.118 |
Index<K,V> s; Node<K,V> b, n; K sk; |
3477 |
|
|
if ((s = q.right) != null && (b = s.node) != null && |
3478 |
|
|
(n = b.next) != null && n.value != null && |
3479 |
|
|
(sk = n.key) != null && cpr(cmp, sk, ek) > 0 && |
3480 |
|
|
(f == null || cpr(cmp, sk, f) < 0)) { |
3481 |
|
|
current = n; |
3482 |
|
|
Index<K,V> r = q.down; |
3483 |
|
|
row = (s.right != null) ? s : s.down; |
3484 |
|
|
est -= est >>> 2; |
3485 |
|
|
return new EntrySpliterator<K,V>(cmp, r, e, sk, est); |
3486 |
dl |
1.83 |
} |
3487 |
|
|
} |
3488 |
|
|
} |
3489 |
|
|
return null; |
3490 |
|
|
} |
3491 |
|
|
|
3492 |
dl |
1.113 |
public void forEachRemaining(Consumer<? super Map.Entry<K,V>> action) { |
3493 |
dl |
1.100 |
if (action == null) throw new NullPointerException(); |
3494 |
dl |
1.118 |
Comparator<? super K> cmp = comparator; |
3495 |
dl |
1.83 |
K f = fence; |
3496 |
|
|
Node<K,V> e = current; |
3497 |
|
|
current = null; |
3498 |
jsr166 |
1.84 |
for (; e != null; e = e.next) { |
3499 |
dl |
1.83 |
K k; Object v; |
3500 |
dl |
1.118 |
if ((k = e.key) != null && f != null && cpr(cmp, f, k) <= 0) |
3501 |
dl |
1.83 |
break; |
3502 |
dl |
1.100 |
if ((v = e.value) != null && v != e) { |
3503 |
|
|
@SuppressWarnings("unchecked") V vv = (V)v; |
3504 |
|
|
action.accept |
3505 |
|
|
(new AbstractMap.SimpleImmutableEntry<K,V>(k, vv)); |
3506 |
|
|
} |
3507 |
dl |
1.83 |
} |
3508 |
|
|
} |
3509 |
|
|
|
3510 |
dl |
1.100 |
public boolean tryAdvance(Consumer<? super Map.Entry<K,V>> action) { |
3511 |
|
|
if (action == null) throw new NullPointerException(); |
3512 |
dl |
1.118 |
Comparator<? super K> cmp = comparator; |
3513 |
|
|
K f = fence; |
3514 |
|
|
Node<K,V> e = current; |
3515 |
|
|
for (; e != null; e = e.next) { |
3516 |
dl |
1.83 |
K k; Object v; |
3517 |
dl |
1.118 |
if ((k = e.key) != null && f != null && cpr(cmp, f, k) <= 0) { |
3518 |
dl |
1.83 |
e = null; |
3519 |
|
|
break; |
3520 |
|
|
} |
3521 |
|
|
if ((v = e.value) != null && v != e) { |
3522 |
|
|
current = e.next; |
3523 |
dl |
1.100 |
@SuppressWarnings("unchecked") V vv = (V)v; |
3524 |
|
|
action.accept |
3525 |
|
|
(new AbstractMap.SimpleImmutableEntry<K,V>(k, vv)); |
3526 |
dl |
1.83 |
return true; |
3527 |
|
|
} |
3528 |
|
|
} |
3529 |
|
|
current = e; |
3530 |
|
|
return false; |
3531 |
|
|
} |
3532 |
dl |
1.100 |
|
3533 |
|
|
public int characteristics() { |
3534 |
jsr166 |
1.102 |
return Spliterator.DISTINCT | Spliterator.SORTED | |
3535 |
jsr166 |
1.101 |
Spliterator.ORDERED | Spliterator.CONCURRENT | |
3536 |
dl |
1.100 |
Spliterator.NONNULL; |
3537 |
|
|
} |
3538 |
dl |
1.113 |
|
3539 |
|
|
public final Comparator<Map.Entry<K,V>> getComparator() { |
3540 |
jsr166 |
1.130 |
// Adapt or create a key-based comparator |
3541 |
|
|
if (comparator != null) { |
3542 |
|
|
return Map.Entry.comparingByKey(comparator); |
3543 |
|
|
} |
3544 |
|
|
else { |
3545 |
jsr166 |
1.131 |
return (Comparator<Map.Entry<K,V>> & Serializable) (e1, e2) -> { |
3546 |
jsr166 |
1.130 |
@SuppressWarnings("unchecked") |
3547 |
|
|
Comparable<? super K> k1 = (Comparable<? super K>) e1.getKey(); |
3548 |
|
|
return k1.compareTo(e2.getKey()); |
3549 |
|
|
}; |
3550 |
|
|
} |
3551 |
dl |
1.113 |
} |
3552 |
dl |
1.118 |
} |
3553 |
dl |
1.113 |
|
3554 |
dl |
1.118 |
// Almost the same as keySpliterator() |
3555 |
|
|
final EntrySpliterator<K,V> entrySpliterator() { |
3556 |
|
|
Comparator<? super K> cmp = comparator; |
3557 |
|
|
for (;;) { // almost same as key version |
3558 |
|
|
HeadIndex<K,V> h; Node<K,V> p; |
3559 |
|
|
Node<K,V> b = (h = head).node; |
3560 |
|
|
if ((p = b.next) == null || p.value != null) |
3561 |
|
|
return new EntrySpliterator<K,V>(cmp, h, p, null, (p == null) ? |
3562 |
|
|
0 : Integer.MAX_VALUE); |
3563 |
|
|
p.helpDelete(b, p.next); |
3564 |
|
|
} |
3565 |
dl |
1.83 |
} |
3566 |
|
|
|
3567 |
dl |
1.59 |
// Unsafe mechanics |
3568 |
jsr166 |
1.141 |
private static final sun.misc.Unsafe U = sun.misc.Unsafe.getUnsafe(); |
3569 |
|
|
private static final long HEAD; |
3570 |
dl |
1.65 |
static { |
3571 |
dl |
1.59 |
try { |
3572 |
jsr166 |
1.141 |
HEAD = U.objectFieldOffset |
3573 |
|
|
(ConcurrentSkipListMap.class.getDeclaredField("head")); |
3574 |
jsr166 |
1.140 |
} catch (ReflectiveOperationException e) { |
3575 |
dl |
1.65 |
throw new Error(e); |
3576 |
dl |
1.59 |
} |
3577 |
|
|
} |
3578 |
dl |
1.1 |
} |