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