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