5 |
|
*/ |
6 |
|
|
7 |
|
package java.util.concurrent; |
8 |
– |
import java.util.concurrent.atomic.LongAdder; |
9 |
– |
import java.util.concurrent.ForkJoinPool; |
10 |
– |
import java.util.concurrent.ForkJoinTask; |
8 |
|
|
9 |
< |
import java.util.Comparator; |
9 |
> |
import java.io.ObjectStreamField; |
10 |
> |
import java.io.Serializable; |
11 |
> |
import java.lang.reflect.ParameterizedType; |
12 |
> |
import java.lang.reflect.Type; |
13 |
> |
import java.util.AbstractMap; |
14 |
|
import java.util.Arrays; |
14 |
– |
import java.util.Map; |
15 |
– |
import java.util.Set; |
15 |
|
import java.util.Collection; |
16 |
< |
import java.util.AbstractMap; |
18 |
< |
import java.util.AbstractSet; |
19 |
< |
import java.util.AbstractCollection; |
20 |
< |
import java.util.Hashtable; |
16 |
> |
import java.util.Enumeration; |
17 |
|
import java.util.HashMap; |
18 |
+ |
import java.util.Hashtable; |
19 |
|
import java.util.Iterator; |
20 |
< |
import java.util.Enumeration; |
24 |
< |
import java.util.ConcurrentModificationException; |
20 |
> |
import java.util.Map; |
21 |
|
import java.util.NoSuchElementException; |
22 |
< |
import java.util.concurrent.ConcurrentMap; |
23 |
< |
import java.util.concurrent.ThreadLocalRandom; |
28 |
< |
import java.util.concurrent.locks.LockSupport; |
29 |
< |
import java.util.concurrent.locks.AbstractQueuedSynchronizer; |
22 |
> |
import java.util.Set; |
23 |
> |
import java.util.Spliterator; |
24 |
|
import java.util.concurrent.atomic.AtomicReference; |
25 |
< |
|
26 |
< |
import java.io.Serializable; |
25 |
> |
import java.util.concurrent.locks.LockSupport; |
26 |
> |
import java.util.concurrent.locks.ReentrantLock; |
27 |
> |
import java.util.function.BiConsumer; |
28 |
> |
import java.util.function.BiFunction; |
29 |
> |
import java.util.function.Consumer; |
30 |
> |
import java.util.function.DoubleBinaryOperator; |
31 |
> |
import java.util.function.Function; |
32 |
> |
import java.util.function.IntBinaryOperator; |
33 |
> |
import java.util.function.LongBinaryOperator; |
34 |
> |
import java.util.function.Predicate; |
35 |
> |
import java.util.function.ToDoubleBiFunction; |
36 |
> |
import java.util.function.ToDoubleFunction; |
37 |
> |
import java.util.function.ToIntBiFunction; |
38 |
> |
import java.util.function.ToIntFunction; |
39 |
> |
import java.util.function.ToLongBiFunction; |
40 |
> |
import java.util.function.ToLongFunction; |
41 |
> |
import java.util.stream.Stream; |
42 |
> |
import jdk.internal.misc.Unsafe; |
43 |
|
|
44 |
|
/** |
45 |
|
* A hash table supporting full concurrency of retrievals and |
53 |
|
* interoperable with {@code Hashtable} in programs that rely on its |
54 |
|
* thread safety but not on its synchronization details. |
55 |
|
* |
56 |
< |
* <p> Retrieval operations (including {@code get}) generally do not |
56 |
> |
* <p>Retrieval operations (including {@code get}) generally do not |
57 |
|
* block, so may overlap with update operations (including {@code put} |
58 |
|
* and {@code remove}). Retrievals reflect the results of the most |
59 |
|
* recently <em>completed</em> update operations holding upon their |
62 |
|
* that key reporting the updated value.) For aggregate operations |
63 |
|
* such as {@code putAll} and {@code clear}, concurrent retrievals may |
64 |
|
* reflect insertion or removal of only some entries. Similarly, |
65 |
< |
* Iterators and Enumerations return elements reflecting the state of |
66 |
< |
* the hash table at some point at or since the creation of the |
65 |
> |
* Iterators, Spliterators and Enumerations return elements reflecting the |
66 |
> |
* state of the hash table at some point at or since the creation of the |
67 |
|
* iterator/enumeration. They do <em>not</em> throw {@link |
68 |
< |
* ConcurrentModificationException}. However, iterators are designed |
69 |
< |
* to be used by only one thread at a time. Bear in mind that the |
70 |
< |
* results of aggregate status methods including {@code size}, {@code |
71 |
< |
* isEmpty}, and {@code containsValue} are typically useful only when |
72 |
< |
* a map is not undergoing concurrent updates in other threads. |
68 |
> |
* java.util.ConcurrentModificationException ConcurrentModificationException}. |
69 |
> |
* However, iterators are designed to be used by only one thread at a time. |
70 |
> |
* Bear in mind that the results of aggregate status methods including |
71 |
> |
* {@code size}, {@code isEmpty}, and {@code containsValue} are typically |
72 |
> |
* useful only when a map is not undergoing concurrent updates in other threads. |
73 |
|
* Otherwise the results of these methods reflect transient states |
74 |
|
* that may be adequate for monitoring or estimation purposes, but not |
75 |
|
* for program control. |
76 |
|
* |
77 |
< |
* <p> The table is dynamically expanded when there are too many |
77 |
> |
* <p>The table is dynamically expanded when there are too many |
78 |
|
* collisions (i.e., keys that have distinct hash codes but fall into |
79 |
|
* the same slot modulo the table size), with the expected average |
80 |
|
* effect of maintaining roughly two bins per mapping (corresponding |
93 |
|
* expected {@code concurrencyLevel} as an additional hint for |
94 |
|
* internal sizing. Note that using many keys with exactly the same |
95 |
|
* {@code hashCode()} is a sure way to slow down performance of any |
96 |
< |
* hash table. |
96 |
> |
* hash table. To ameliorate impact, when keys are {@link Comparable}, |
97 |
> |
* this class may use comparison order among keys to help break ties. |
98 |
|
* |
99 |
< |
* <p> A {@link Set} projection of a ConcurrentHashMap may be created |
99 |
> |
* <p>A {@link Set} projection of a ConcurrentHashMap may be created |
100 |
|
* (using {@link #newKeySet()} or {@link #newKeySet(int)}), or viewed |
101 |
|
* (using {@link #keySet(Object)} when only keys are of interest, and the |
102 |
|
* mapped values are (perhaps transiently) not used or all take the |
103 |
|
* same mapping value. |
104 |
|
* |
105 |
< |
* <p> A ConcurrentHashMap can be used as scalable frequency map (a |
106 |
< |
* form of histogram or multiset) by using {@link LongAdder} values |
107 |
< |
* and initializing via {@link #computeIfAbsent}. For example, to add |
108 |
< |
* a count to a {@code ConcurrentHashMap<String,LongAdder> freqs}, you |
109 |
< |
* can use {@code freqs.computeIfAbsent(k -> new |
110 |
< |
* LongAdder()).increment();} |
105 |
> |
* <p>A ConcurrentHashMap can be used as a scalable frequency map (a |
106 |
> |
* form of histogram or multiset) by using {@link |
107 |
> |
* java.util.concurrent.atomic.LongAdder} values and initializing via |
108 |
> |
* {@link #computeIfAbsent computeIfAbsent}. For example, to add a count |
109 |
> |
* to a {@code ConcurrentHashMap<String,LongAdder> freqs}, you can use |
110 |
> |
* {@code freqs.computeIfAbsent(key, k -> new LongAdder()).increment();} |
111 |
|
* |
112 |
|
* <p>This class and its views and iterators implement all of the |
113 |
|
* <em>optional</em> methods of the {@link Map} and {@link Iterator} |
114 |
|
* interfaces. |
115 |
|
* |
116 |
< |
* <p> Like {@link Hashtable} but unlike {@link HashMap}, this class |
116 |
> |
* <p>Like {@link Hashtable} but unlike {@link HashMap}, this class |
117 |
|
* does <em>not</em> allow {@code null} to be used as a key or value. |
118 |
|
* |
119 |
< |
* <p>ConcurrentHashMaps support parallel operations using the {@link |
120 |
< |
* ForkJoinPool#commonPool}. (Tasks that may be used in other contexts |
121 |
< |
* are available in class {@link ForkJoinTasks}). These operations are |
122 |
< |
* designed to be safely, and often sensibly, applied even with maps |
123 |
< |
* that are being concurrently updated by other threads; for example, |
124 |
< |
* when computing a snapshot summary of the values in a shared |
125 |
< |
* registry. There are three kinds of operation, each with four |
126 |
< |
* forms, accepting functions with Keys, Values, Entries, and (Key, |
116 |
< |
* Value) arguments and/or return values. (The first three forms are |
117 |
< |
* also available via the {@link #keySet()}, {@link #values()} and |
118 |
< |
* {@link #entrySet()} views). Because the elements of a |
119 |
> |
* <p>ConcurrentHashMaps support a set of sequential and parallel bulk |
120 |
> |
* operations that, unlike most {@link Stream} methods, are designed |
121 |
> |
* to be safely, and often sensibly, applied even with maps that are |
122 |
> |
* being concurrently updated by other threads; for example, when |
123 |
> |
* computing a snapshot summary of the values in a shared registry. |
124 |
> |
* There are three kinds of operation, each with four forms, accepting |
125 |
> |
* functions with keys, values, entries, and (key, value) pairs as |
126 |
> |
* arguments and/or return values. Because the elements of a |
127 |
|
* ConcurrentHashMap are not ordered in any particular way, and may be |
128 |
|
* processed in different orders in different parallel executions, the |
129 |
|
* correctness of supplied functions should not depend on any |
130 |
|
* ordering, or on any other objects or values that may transiently |
131 |
|
* change while computation is in progress; and except for forEach |
132 |
< |
* actions, should ideally be side-effect-free. |
132 |
> |
* actions, should ideally be side-effect-free. Bulk operations on |
133 |
> |
* {@link Map.Entry} objects do not support method {@code setValue}. |
134 |
|
* |
135 |
|
* <ul> |
136 |
< |
* <li> forEach: Perform a given action on each element. |
136 |
> |
* <li>forEach: Performs a given action on each element. |
137 |
|
* A variant form applies a given transformation on each element |
138 |
< |
* before performing the action.</li> |
138 |
> |
* before performing the action. |
139 |
|
* |
140 |
< |
* <li> search: Return the first available non-null result of |
140 |
> |
* <li>search: Returns the first available non-null result of |
141 |
|
* applying a given function on each element; skipping further |
142 |
< |
* search when a result is found.</li> |
142 |
> |
* search when a result is found. |
143 |
|
* |
144 |
< |
* <li> reduce: Accumulate each element. The supplied reduction |
144 |
> |
* <li>reduce: Accumulates each element. The supplied reduction |
145 |
|
* function cannot rely on ordering (more formally, it should be |
146 |
|
* both associative and commutative). There are five variants: |
147 |
|
* |
148 |
|
* <ul> |
149 |
|
* |
150 |
< |
* <li> Plain reductions. (There is not a form of this method for |
150 |
> |
* <li>Plain reductions. (There is not a form of this method for |
151 |
|
* (key, value) function arguments since there is no corresponding |
152 |
< |
* return type.)</li> |
152 |
> |
* return type.) |
153 |
|
* |
154 |
< |
* <li> Mapped reductions that accumulate the results of a given |
155 |
< |
* function applied to each element.</li> |
154 |
> |
* <li>Mapped reductions that accumulate the results of a given |
155 |
> |
* function applied to each element. |
156 |
|
* |
157 |
< |
* <li> Reductions to scalar doubles, longs, and ints, using a |
158 |
< |
* given basis value.</li> |
157 |
> |
* <li>Reductions to scalar doubles, longs, and ints, using a |
158 |
> |
* given basis value. |
159 |
|
* |
151 |
– |
* </li> |
160 |
|
* </ul> |
161 |
|
* </ul> |
162 |
|
* |
163 |
+ |
* <p>These bulk operations accept a {@code parallelismThreshold} |
164 |
+ |
* argument. Methods proceed sequentially if the current map size is |
165 |
+ |
* estimated to be less than the given threshold. Using a value of |
166 |
+ |
* {@code Long.MAX_VALUE} suppresses all parallelism. Using a value |
167 |
+ |
* of {@code 1} results in maximal parallelism by partitioning into |
168 |
+ |
* enough subtasks to fully utilize the {@link |
169 |
+ |
* ForkJoinPool#commonPool()} that is used for all parallel |
170 |
+ |
* computations. Normally, you would initially choose one of these |
171 |
+ |
* extreme values, and then measure performance of using in-between |
172 |
+ |
* values that trade off overhead versus throughput. |
173 |
+ |
* |
174 |
|
* <p>The concurrency properties of bulk operations follow |
175 |
|
* from those of ConcurrentHashMap: Any non-null result returned |
176 |
|
* from {@code get(key)} and related access methods bears a |
206 |
|
* arguments can be supplied using {@code new |
207 |
|
* AbstractMap.SimpleEntry(k,v)}. |
208 |
|
* |
209 |
< |
* <p> Bulk operations may complete abruptly, throwing an |
209 |
> |
* <p>Bulk operations may complete abruptly, throwing an |
210 |
|
* exception encountered in the application of a supplied |
211 |
|
* function. Bear in mind when handling such exceptions that other |
212 |
|
* concurrently executing functions could also have thrown |
213 |
|
* exceptions, or would have done so if the first exception had |
214 |
|
* not occurred. |
215 |
|
* |
216 |
< |
* <p>Parallel speedups for bulk operations compared to sequential |
217 |
< |
* processing are common but not guaranteed. Operations involving |
218 |
< |
* brief functions on small maps may execute more slowly than |
219 |
< |
* sequential loops if the underlying work to parallelize the |
220 |
< |
* computation is more expensive than the computation itself. |
221 |
< |
* Similarly, parallelization may not lead to much actual parallelism |
222 |
< |
* if all processors are busy performing unrelated tasks. |
216 |
> |
* <p>Speedups for parallel compared to sequential forms are common |
217 |
> |
* but not guaranteed. Parallel operations involving brief functions |
218 |
> |
* on small maps may execute more slowly than sequential forms if the |
219 |
> |
* underlying work to parallelize the computation is more expensive |
220 |
> |
* than the computation itself. Similarly, parallelization may not |
221 |
> |
* lead to much actual parallelism if all processors are busy |
222 |
> |
* performing unrelated tasks. |
223 |
|
* |
224 |
< |
* <p> All arguments to all task methods must be non-null. |
206 |
< |
* |
207 |
< |
* <p><em>jsr166e note: During transition, this class |
208 |
< |
* uses nested functional interfaces with different names but the |
209 |
< |
* same forms as those expected for JDK8.<em> |
224 |
> |
* <p>All arguments to all task methods must be non-null. |
225 |
|
* |
226 |
|
* <p>This class is a member of the |
227 |
< |
* <a href="{@docRoot}/../technotes/guides/collections/index.html"> |
227 |
> |
* <a href="{@docRoot}/java.base/java/util/package-summary.html#CollectionsFramework"> |
228 |
|
* Java Collections Framework</a>. |
229 |
|
* |
230 |
|
* @since 1.5 |
232 |
|
* @param <K> the type of keys maintained by this map |
233 |
|
* @param <V> the type of mapped values |
234 |
|
*/ |
235 |
< |
public class ConcurrentHashMap<K, V> |
236 |
< |
implements ConcurrentMap<K, V>, Serializable { |
235 |
> |
public class ConcurrentHashMap<K,V> extends AbstractMap<K,V> |
236 |
> |
implements ConcurrentMap<K,V>, Serializable { |
237 |
|
private static final long serialVersionUID = 7249069246763182397L; |
238 |
|
|
224 |
– |
/** |
225 |
– |
* A partitionable iterator. A Spliterator can be traversed |
226 |
– |
* directly, but can also be partitioned (before traversal) by |
227 |
– |
* creating another Spliterator that covers a non-overlapping |
228 |
– |
* portion of the elements, and so may be amenable to parallel |
229 |
– |
* execution. |
230 |
– |
* |
231 |
– |
* <p> This interface exports a subset of expected JDK8 |
232 |
– |
* functionality. |
233 |
– |
* |
234 |
– |
* <p>Sample usage: Here is one (of the several) ways to compute |
235 |
– |
* the sum of the values held in a map using the ForkJoin |
236 |
– |
* framework. As illustrated here, Spliterators are well suited to |
237 |
– |
* designs in which a task repeatedly splits off half its work |
238 |
– |
* into forked subtasks until small enough to process directly, |
239 |
– |
* and then joins these subtasks. Variants of this style can also |
240 |
– |
* be used in completion-based designs. |
241 |
– |
* |
242 |
– |
* <pre> |
243 |
– |
* {@code ConcurrentHashMap<String, Long> m = ... |
244 |
– |
* // split as if have 8 * parallelism, for load balance |
245 |
– |
* int n = m.size(); |
246 |
– |
* int p = aForkJoinPool.getParallelism() * 8; |
247 |
– |
* int split = (n < p)? n : p; |
248 |
– |
* long sum = aForkJoinPool.invoke(new SumValues(m.valueSpliterator(), split, null)); |
249 |
– |
* // ... |
250 |
– |
* static class SumValues extends RecursiveTask<Long> { |
251 |
– |
* final Spliterator<Long> s; |
252 |
– |
* final int split; // split while > 1 |
253 |
– |
* final SumValues nextJoin; // records forked subtasks to join |
254 |
– |
* SumValues(Spliterator<Long> s, int depth, SumValues nextJoin) { |
255 |
– |
* this.s = s; this.depth = depth; this.nextJoin = nextJoin; |
256 |
– |
* } |
257 |
– |
* public Long compute() { |
258 |
– |
* long sum = 0; |
259 |
– |
* SumValues subtasks = null; // fork subtasks |
260 |
– |
* for (int s = split >>> 1; s > 0; s >>>= 1) |
261 |
– |
* (subtasks = new SumValues(s.split(), s, subtasks)).fork(); |
262 |
– |
* while (s.hasNext()) // directly process remaining elements |
263 |
– |
* sum += s.next(); |
264 |
– |
* for (SumValues t = subtasks; t != null; t = t.nextJoin) |
265 |
– |
* sum += t.join(); // collect subtask results |
266 |
– |
* return sum; |
267 |
– |
* } |
268 |
– |
* } |
269 |
– |
* }</pre> |
270 |
– |
*/ |
271 |
– |
public static interface Spliterator<T> extends Iterator<T> { |
272 |
– |
/** |
273 |
– |
* Returns a Spliterator covering approximately half of the |
274 |
– |
* elements, guaranteed not to overlap with those subsequently |
275 |
– |
* returned by this Spliterator. After invoking this method, |
276 |
– |
* the current Spliterator will <em>not</em> produce any of |
277 |
– |
* the elements of the returned Spliterator, but the two |
278 |
– |
* Spliterators together will produce all of the elements that |
279 |
– |
* would have been produced by this Spliterator had this |
280 |
– |
* method not been called. The exact number of elements |
281 |
– |
* produced by the returned Spliterator is not guaranteed, and |
282 |
– |
* may be zero (i.e., with {@code hasNext()} reporting {@code |
283 |
– |
* false}) if this Spliterator cannot be further split. |
284 |
– |
* |
285 |
– |
* @return a Spliterator covering approximately half of the |
286 |
– |
* elements |
287 |
– |
* @throws IllegalStateException if this Spliterator has |
288 |
– |
* already commenced traversing elements |
289 |
– |
*/ |
290 |
– |
Spliterator<T> split(); |
291 |
– |
} |
292 |
– |
|
293 |
– |
|
239 |
|
/* |
240 |
|
* Overview: |
241 |
|
* |
246 |
|
* the same or better than java.util.HashMap, and to support high |
247 |
|
* initial insertion rates on an empty table by many threads. |
248 |
|
* |
249 |
< |
* Each key-value mapping is held in a Node. Because Node fields |
250 |
< |
* can contain special values, they are defined using plain Object |
251 |
< |
* types. Similarly in turn, all internal methods that use them |
252 |
< |
* work off Object types. And similarly, so do the internal |
253 |
< |
* methods of auxiliary iterator and view classes. All public |
254 |
< |
* generic typed methods relay in/out of these internal methods, |
255 |
< |
* supplying null-checks and casts as needed. This also allows |
256 |
< |
* many of the public methods to be factored into a smaller number |
257 |
< |
* of internal methods (although sadly not so for the five |
258 |
< |
* variants of put-related operations). The validation-based |
259 |
< |
* approach explained below leads to a lot of code sprawl because |
260 |
< |
* retry-control precludes factoring into smaller methods. |
249 |
> |
* This map usually acts as a binned (bucketed) hash table. Each |
250 |
> |
* key-value mapping is held in a Node. Most nodes are instances |
251 |
> |
* of the basic Node class with hash, key, value, and next |
252 |
> |
* fields. However, various subclasses exist: TreeNodes are |
253 |
> |
* arranged in balanced trees, not lists. TreeBins hold the roots |
254 |
> |
* of sets of TreeNodes. ForwardingNodes are placed at the heads |
255 |
> |
* of bins during resizing. ReservationNodes are used as |
256 |
> |
* placeholders while establishing values in computeIfAbsent and |
257 |
> |
* related methods. The types TreeBin, ForwardingNode, and |
258 |
> |
* ReservationNode do not hold normal user keys, values, or |
259 |
> |
* hashes, and are readily distinguishable during search etc |
260 |
> |
* because they have negative hash fields and null key and value |
261 |
> |
* fields. (These special nodes are either uncommon or transient, |
262 |
> |
* so the impact of carrying around some unused fields is |
263 |
> |
* insignificant.) |
264 |
|
* |
265 |
|
* The table is lazily initialized to a power-of-two size upon the |
266 |
|
* first insertion. Each bin in the table normally contains a |
268 |
|
* Table accesses require volatile/atomic reads, writes, and |
269 |
|
* CASes. Because there is no other way to arrange this without |
270 |
|
* adding further indirections, we use intrinsics |
271 |
< |
* (sun.misc.Unsafe) operations. The lists of nodes within bins |
272 |
< |
* are always accurately traversable under volatile reads, so long |
273 |
< |
* as lookups check hash code and non-nullness of value before |
274 |
< |
* checking key equality. |
275 |
< |
* |
276 |
< |
* We use the top two bits of Node hash fields for control |
329 |
< |
* purposes -- they are available anyway because of addressing |
330 |
< |
* constraints. As explained further below, these top bits are |
331 |
< |
* used as follows: |
332 |
< |
* 00 - Normal |
333 |
< |
* 01 - Locked |
334 |
< |
* 11 - Locked and may have a thread waiting for lock |
335 |
< |
* 10 - Node is a forwarding node |
336 |
< |
* |
337 |
< |
* The lower 30 bits of each Node's hash field contain a |
338 |
< |
* transformation of the key's hash code, except for forwarding |
339 |
< |
* nodes, for which the lower bits are zero (and so always have |
340 |
< |
* hash field == MOVED). |
271 |
> |
* (jdk.internal.misc.Unsafe) operations. |
272 |
> |
* |
273 |
> |
* We use the top (sign) bit of Node hash fields for control |
274 |
> |
* purposes -- it is available anyway because of addressing |
275 |
> |
* constraints. Nodes with negative hash fields are specially |
276 |
> |
* handled or ignored in map methods. |
277 |
|
* |
278 |
|
* Insertion (via put or its variants) of the first node in an |
279 |
|
* empty bin is performed by just CASing it to the bin. This is |
282 |
|
* delete, and replace) require locks. We do not want to waste |
283 |
|
* the space required to associate a distinct lock object with |
284 |
|
* each bin, so instead use the first node of a bin list itself as |
285 |
< |
* a lock. Blocking support for these locks relies on the builtin |
286 |
< |
* "synchronized" monitors. However, we also need a tryLock |
351 |
< |
* construction, so we overlay these by using bits of the Node |
352 |
< |
* hash field for lock control (see above), and so normally use |
353 |
< |
* builtin monitors only for blocking and signalling using |
354 |
< |
* wait/notifyAll constructions. See Node.tryAwaitLock. |
285 |
> |
* a lock. Locking support for these locks relies on builtin |
286 |
> |
* "synchronized" monitors. |
287 |
|
* |
288 |
|
* Using the first node of a list as a lock does not by itself |
289 |
|
* suffice though: When a node is locked, any update must first |
290 |
|
* validate that it is still the first node after locking it, and |
291 |
|
* retry if not. Because new nodes are always appended to lists, |
292 |
|
* once a node is first in a bin, it remains first until deleted |
293 |
< |
* or the bin becomes invalidated (upon resizing). However, |
362 |
< |
* operations that only conditionally update may inspect nodes |
363 |
< |
* until the point of update. This is a converse of sorts to the |
364 |
< |
* lazy locking technique described by Herlihy & Shavit. |
293 |
> |
* or the bin becomes invalidated (upon resizing). |
294 |
|
* |
295 |
|
* The main disadvantage of per-bin locks is that other update |
296 |
|
* operations on other nodes in a bin list protected by the same |
323 |
|
* sometimes deviate significantly from uniform randomness. This |
324 |
|
* includes the case when N > (1<<30), so some keys MUST collide. |
325 |
|
* Similarly for dumb or hostile usages in which multiple keys are |
326 |
< |
* designed to have identical hash codes. Also, although we guard |
327 |
< |
* against the worst effects of this (see method spread), sets of |
328 |
< |
* hashes may differ only in bits that do not impact their bin |
329 |
< |
* index for a given power-of-two mask. So we use a secondary |
330 |
< |
* strategy that applies when the number of nodes in a bin exceeds |
331 |
< |
* a threshold, and at least one of the keys implements |
403 |
< |
* Comparable. These TreeBins use a balanced tree to hold nodes |
404 |
< |
* (a specialized form of red-black trees), bounding search time |
405 |
< |
* to O(log N). Each search step in a TreeBin is around twice as |
326 |
> |
* designed to have identical hash codes or ones that differs only |
327 |
> |
* in masked-out high bits. So we use a secondary strategy that |
328 |
> |
* applies when the number of nodes in a bin exceeds a |
329 |
> |
* threshold. These TreeBins use a balanced tree to hold nodes (a |
330 |
> |
* specialized form of red-black trees), bounding search time to |
331 |
> |
* O(log N). Each search step in a TreeBin is at least twice as |
332 |
|
* slow as in a regular list, but given that N cannot exceed |
333 |
|
* (1<<64) (before running out of addresses) this bounds search |
334 |
|
* steps, lock hold times, etc, to reasonable constants (roughly |
339 |
|
* iterators in the same way. |
340 |
|
* |
341 |
|
* The table is resized when occupancy exceeds a percentage |
342 |
< |
* threshold (nominally, 0.75, but see below). Only a single |
343 |
< |
* thread performs the resize (using field "sizeCtl", to arrange |
344 |
< |
* exclusion), but the table otherwise remains usable for reads |
345 |
< |
* and updates. Resizing proceeds by transferring bins, one by |
346 |
< |
* one, from the table to the next table. Because we are using |
347 |
< |
* power-of-two expansion, the elements from each bin must either |
348 |
< |
* stay at same index, or move with a power of two offset. We |
349 |
< |
* eliminate unnecessary node creation by catching cases where old |
350 |
< |
* nodes can be reused because their next fields won't change. On |
351 |
< |
* average, only about one-sixth of them need cloning when a table |
352 |
< |
* doubles. The nodes they replace will be garbage collectable as |
353 |
< |
* soon as they are no longer referenced by any reader thread that |
354 |
< |
* may be in the midst of concurrently traversing table. Upon |
355 |
< |
* transfer, the old table bin contains only a special forwarding |
356 |
< |
* node (with hash field "MOVED") that contains the next table as |
357 |
< |
* its key. On encountering a forwarding node, access and update |
358 |
< |
* operations restart, using the new table. |
359 |
< |
* |
360 |
< |
* Each bin transfer requires its bin lock. However, unlike other |
361 |
< |
* cases, a transfer can skip a bin if it fails to acquire its |
362 |
< |
* lock, and revisit it later (unless it is a TreeBin). Method |
363 |
< |
* rebuild maintains a buffer of TRANSFER_BUFFER_SIZE bins that |
364 |
< |
* have been skipped because of failure to acquire a lock, and |
365 |
< |
* blocks only if none are available (i.e., only very rarely). |
366 |
< |
* The transfer operation must also ensure that all accessible |
367 |
< |
* bins in both the old and new table are usable by any traversal. |
368 |
< |
* When there are no lock acquisition failures, this is arranged |
369 |
< |
* simply by proceeding from the last bin (table.length - 1) up |
370 |
< |
* towards the first. Upon seeing a forwarding node, traversals |
371 |
< |
* (see class Iter) arrange to move to the new table |
372 |
< |
* without revisiting nodes. However, when any node is skipped |
373 |
< |
* during a transfer, all earlier table bins may have become |
374 |
< |
* visible, so are initialized with a reverse-forwarding node back |
375 |
< |
* to the old table until the new ones are established. (This |
376 |
< |
* sometimes requires transiently locking a forwarding node, which |
377 |
< |
* is possible under the above encoding.) These more expensive |
378 |
< |
* mechanics trigger only when necessary. |
342 |
> |
* threshold (nominally, 0.75, but see below). Any thread |
343 |
> |
* noticing an overfull bin may assist in resizing after the |
344 |
> |
* initiating thread allocates and sets up the replacement array. |
345 |
> |
* However, rather than stalling, these other threads may proceed |
346 |
> |
* with insertions etc. The use of TreeBins shields us from the |
347 |
> |
* worst case effects of overfilling while resizes are in |
348 |
> |
* progress. Resizing proceeds by transferring bins, one by one, |
349 |
> |
* from the table to the next table. However, threads claim small |
350 |
> |
* blocks of indices to transfer (via field transferIndex) before |
351 |
> |
* doing so, reducing contention. A generation stamp in field |
352 |
> |
* sizeCtl ensures that resizings do not overlap. Because we are |
353 |
> |
* using power-of-two expansion, the elements from each bin must |
354 |
> |
* either stay at same index, or move with a power of two |
355 |
> |
* offset. We eliminate unnecessary node creation by catching |
356 |
> |
* cases where old nodes can be reused because their next fields |
357 |
> |
* won't change. On average, only about one-sixth of them need |
358 |
> |
* cloning when a table doubles. The nodes they replace will be |
359 |
> |
* garbage collectable as soon as they are no longer referenced by |
360 |
> |
* any reader thread that may be in the midst of concurrently |
361 |
> |
* traversing table. Upon transfer, the old table bin contains |
362 |
> |
* only a special forwarding node (with hash field "MOVED") that |
363 |
> |
* contains the next table as its key. On encountering a |
364 |
> |
* forwarding node, access and update operations restart, using |
365 |
> |
* the new table. |
366 |
> |
* |
367 |
> |
* Each bin transfer requires its bin lock, which can stall |
368 |
> |
* waiting for locks while resizing. However, because other |
369 |
> |
* threads can join in and help resize rather than contend for |
370 |
> |
* locks, average aggregate waits become shorter as resizing |
371 |
> |
* progresses. The transfer operation must also ensure that all |
372 |
> |
* accessible bins in both the old and new table are usable by any |
373 |
> |
* traversal. This is arranged in part by proceeding from the |
374 |
> |
* last bin (table.length - 1) up towards the first. Upon seeing |
375 |
> |
* a forwarding node, traversals (see class Traverser) arrange to |
376 |
> |
* move to the new table without revisiting nodes. To ensure that |
377 |
> |
* no intervening nodes are skipped even when moved out of order, |
378 |
> |
* a stack (see class TableStack) is created on first encounter of |
379 |
> |
* a forwarding node during a traversal, to maintain its place if |
380 |
> |
* later processing the current table. The need for these |
381 |
> |
* save/restore mechanics is relatively rare, but when one |
382 |
> |
* forwarding node is encountered, typically many more will be. |
383 |
> |
* So Traversers use a simple caching scheme to avoid creating so |
384 |
> |
* many new TableStack nodes. (Thanks to Peter Levart for |
385 |
> |
* suggesting use of a stack here.) |
386 |
|
* |
387 |
|
* The traversal scheme also applies to partial traversals of |
388 |
|
* ranges of bins (via an alternate Traverser constructor) |
397 |
|
* These cases attempt to override the initial capacity settings, |
398 |
|
* but harmlessly fail to take effect in cases of races. |
399 |
|
* |
400 |
< |
* The element count is maintained using a LongAdder, which avoids |
401 |
< |
* contention on updates but can encounter cache thrashing if read |
402 |
< |
* too frequently during concurrent access. To avoid reading so |
403 |
< |
* often, resizing is attempted either when a bin lock is |
404 |
< |
* contended, or upon adding to a bin already holding two or more |
405 |
< |
* nodes (checked before adding in the xIfAbsent methods, after |
406 |
< |
* adding in others). Under uniform hash distributions, the |
407 |
< |
* probability of this occurring at threshold is around 13%, |
408 |
< |
* meaning that only about 1 in 8 puts check threshold (and after |
409 |
< |
* resizing, many fewer do so). But this approximation has high |
410 |
< |
* variance for small table sizes, so we check on any collision |
411 |
< |
* for sizes <= 64. The bulk putAll operation further reduces |
412 |
< |
* contention by only committing count updates upon these size |
413 |
< |
* checks. |
400 |
> |
* The element count is maintained using a specialization of |
401 |
> |
* LongAdder. We need to incorporate a specialization rather than |
402 |
> |
* just use a LongAdder in order to access implicit |
403 |
> |
* contention-sensing that leads to creation of multiple |
404 |
> |
* CounterCells. The counter mechanics avoid contention on |
405 |
> |
* updates but can encounter cache thrashing if read too |
406 |
> |
* frequently during concurrent access. To avoid reading so often, |
407 |
> |
* resizing under contention is attempted only upon adding to a |
408 |
> |
* bin already holding two or more nodes. Under uniform hash |
409 |
> |
* distributions, the probability of this occurring at threshold |
410 |
> |
* is around 13%, meaning that only about 1 in 8 puts check |
411 |
> |
* threshold (and after resizing, many fewer do so). |
412 |
> |
* |
413 |
> |
* TreeBins use a special form of comparison for search and |
414 |
> |
* related operations (which is the main reason we cannot use |
415 |
> |
* existing collections such as TreeMaps). TreeBins contain |
416 |
> |
* Comparable elements, but may contain others, as well as |
417 |
> |
* elements that are Comparable but not necessarily Comparable for |
418 |
> |
* the same T, so we cannot invoke compareTo among them. To handle |
419 |
> |
* this, the tree is ordered primarily by hash value, then by |
420 |
> |
* Comparable.compareTo order if applicable. On lookup at a node, |
421 |
> |
* if elements are not comparable or compare as 0 then both left |
422 |
> |
* and right children may need to be searched in the case of tied |
423 |
> |
* hash values. (This corresponds to the full list search that |
424 |
> |
* would be necessary if all elements were non-Comparable and had |
425 |
> |
* tied hashes.) On insertion, to keep a total ordering (or as |
426 |
> |
* close as is required here) across rebalancings, we compare |
427 |
> |
* classes and identityHashCodes as tie-breakers. The red-black |
428 |
> |
* balancing code is updated from pre-jdk-collections |
429 |
> |
* (http://gee.cs.oswego.edu/dl/classes/collections/RBCell.java) |
430 |
> |
* based in turn on Cormen, Leiserson, and Rivest "Introduction to |
431 |
> |
* Algorithms" (CLR). |
432 |
> |
* |
433 |
> |
* TreeBins also require an additional locking mechanism. While |
434 |
> |
* list traversal is always possible by readers even during |
435 |
> |
* updates, tree traversal is not, mainly because of tree-rotations |
436 |
> |
* that may change the root node and/or its linkages. TreeBins |
437 |
> |
* include a simple read-write lock mechanism parasitic on the |
438 |
> |
* main bin-synchronization strategy: Structural adjustments |
439 |
> |
* associated with an insertion or removal are already bin-locked |
440 |
> |
* (and so cannot conflict with other writers) but must wait for |
441 |
> |
* ongoing readers to finish. Since there can be only one such |
442 |
> |
* waiter, we use a simple scheme using a single "waiter" field to |
443 |
> |
* block writers. However, readers need never block. If the root |
444 |
> |
* lock is held, they proceed along the slow traversal path (via |
445 |
> |
* next-pointers) until the lock becomes available or the list is |
446 |
> |
* exhausted, whichever comes first. These cases are not fast, but |
447 |
> |
* maximize aggregate expected throughput. |
448 |
|
* |
449 |
|
* Maintaining API and serialization compatibility with previous |
450 |
|
* versions of this class introduces several oddities. Mainly: We |
451 |
< |
* leave untouched but unused constructor arguments refering to |
451 |
> |
* leave untouched but unused constructor arguments referring to |
452 |
|
* concurrencyLevel. We accept a loadFactor constructor argument, |
453 |
|
* but apply it only to initial table capacity (which is the only |
454 |
|
* time that we can guarantee to honor it.) We also declare an |
455 |
|
* unused "Segment" class that is instantiated in minimal form |
456 |
|
* only when serializing. |
457 |
+ |
* |
458 |
+ |
* Also, solely for compatibility with previous versions of this |
459 |
+ |
* class, it extends AbstractMap, even though all of its methods |
460 |
+ |
* are overridden, so it is just useless baggage. |
461 |
+ |
* |
462 |
+ |
* This file is organized to make things a little easier to follow |
463 |
+ |
* while reading than they might otherwise: First the main static |
464 |
+ |
* declarations and utilities, then fields, then main public |
465 |
+ |
* methods (with a few factorings of multiple public methods into |
466 |
+ |
* internal ones), then sizing methods, trees, traversers, and |
467 |
+ |
* bulk operations. |
468 |
|
*/ |
469 |
|
|
470 |
|
/* ---------------- Constants -------------- */ |
506 |
|
private static final float LOAD_FACTOR = 0.75f; |
507 |
|
|
508 |
|
/** |
509 |
< |
* The buffer size for skipped bins during transfers. The |
510 |
< |
* value is arbitrary but should be large enough to avoid |
511 |
< |
* most locking stalls during resizes. |
509 |
> |
* The bin count threshold for using a tree rather than list for a |
510 |
> |
* bin. Bins are converted to trees when adding an element to a |
511 |
> |
* bin with at least this many nodes. The value must be greater |
512 |
> |
* than 2, and should be at least 8 to mesh with assumptions in |
513 |
> |
* tree removal about conversion back to plain bins upon |
514 |
> |
* shrinkage. |
515 |
|
*/ |
516 |
< |
private static final int TRANSFER_BUFFER_SIZE = 32; |
516 |
> |
static final int TREEIFY_THRESHOLD = 8; |
517 |
|
|
518 |
|
/** |
519 |
< |
* The bin count threshold for using a tree rather than list for a |
520 |
< |
* bin. The value reflects the approximate break-even point for |
521 |
< |
* using tree-based operations. |
519 |
> |
* The bin count threshold for untreeifying a (split) bin during a |
520 |
> |
* resize operation. Should be less than TREEIFY_THRESHOLD, and at |
521 |
> |
* most 6 to mesh with shrinkage detection under removal. |
522 |
|
*/ |
523 |
< |
private static final int TREE_THRESHOLD = 8; |
523 |
> |
static final int UNTREEIFY_THRESHOLD = 6; |
524 |
|
|
525 |
< |
/* |
526 |
< |
* Encodings for special uses of Node hash fields. See above for |
527 |
< |
* explanation. |
525 |
> |
/** |
526 |
> |
* The smallest table capacity for which bins may be treeified. |
527 |
> |
* (Otherwise the table is resized if too many nodes in a bin.) |
528 |
> |
* The value should be at least 4 * TREEIFY_THRESHOLD to avoid |
529 |
> |
* conflicts between resizing and treeification thresholds. |
530 |
|
*/ |
531 |
< |
static final int MOVED = 0x80000000; // hash field for forwarding nodes |
549 |
< |
static final int LOCKED = 0x40000000; // set/tested only as a bit |
550 |
< |
static final int WAITING = 0xc0000000; // both bits set/tested together |
551 |
< |
static final int HASH_BITS = 0x3fffffff; // usable bits of normal node hash |
552 |
< |
|
553 |
< |
/* ---------------- Fields -------------- */ |
531 |
> |
static final int MIN_TREEIFY_CAPACITY = 64; |
532 |
|
|
533 |
|
/** |
534 |
< |
* The array of bins. Lazily initialized upon first insertion. |
535 |
< |
* Size is always a power of two. Accessed directly by iterators. |
534 |
> |
* Minimum number of rebinnings per transfer step. Ranges are |
535 |
> |
* subdivided to allow multiple resizer threads. This value |
536 |
> |
* serves as a lower bound to avoid resizers encountering |
537 |
> |
* excessive memory contention. The value should be at least |
538 |
> |
* DEFAULT_CAPACITY. |
539 |
|
*/ |
540 |
< |
transient volatile Node[] table; |
540 |
> |
private static final int MIN_TRANSFER_STRIDE = 16; |
541 |
|
|
542 |
|
/** |
543 |
< |
* The counter maintaining number of elements. |
543 |
> |
* The number of bits used for generation stamp in sizeCtl. |
544 |
> |
* Must be at least 6 for 32bit arrays. |
545 |
|
*/ |
546 |
< |
private transient final LongAdder counter; |
546 |
> |
private static final int RESIZE_STAMP_BITS = 16; |
547 |
|
|
548 |
|
/** |
549 |
< |
* Table initialization and resizing control. When negative, the |
550 |
< |
* table is being initialized or resized. Otherwise, when table is |
569 |
< |
* null, holds the initial table size to use upon creation, or 0 |
570 |
< |
* for default. After initialization, holds the next element count |
571 |
< |
* value upon which to resize the table. |
549 |
> |
* The maximum number of threads that can help resize. |
550 |
> |
* Must fit in 32 - RESIZE_STAMP_BITS bits. |
551 |
|
*/ |
552 |
< |
private transient volatile int sizeCtl; |
574 |
< |
|
575 |
< |
// views |
576 |
< |
private transient KeySetView<K,V> keySet; |
577 |
< |
private transient ValuesView<K,V> values; |
578 |
< |
private transient EntrySetView<K,V> entrySet; |
552 |
> |
private static final int MAX_RESIZERS = (1 << (32 - RESIZE_STAMP_BITS)) - 1; |
553 |
|
|
554 |
< |
/** For serialization compatibility. Null unless serialized; see below */ |
555 |
< |
private Segment<K,V>[] segments; |
556 |
< |
|
557 |
< |
/* ---------------- Table element access -------------- */ |
554 |
> |
/** |
555 |
> |
* The bit shift for recording size stamp in sizeCtl. |
556 |
> |
*/ |
557 |
> |
private static final int RESIZE_STAMP_SHIFT = 32 - RESIZE_STAMP_BITS; |
558 |
|
|
559 |
|
/* |
560 |
< |
* Volatile access methods are used for table elements as well as |
587 |
< |
* elements of in-progress next table while resizing. Uses are |
588 |
< |
* null checked by callers, and implicitly bounds-checked, relying |
589 |
< |
* on the invariants that tab arrays have non-zero size, and all |
590 |
< |
* indices are masked with (tab.length - 1) which is never |
591 |
< |
* negative and always less than length. Note that, to be correct |
592 |
< |
* wrt arbitrary concurrency errors by users, bounds checks must |
593 |
< |
* operate on local variables, which accounts for some odd-looking |
594 |
< |
* inline assignments below. |
560 |
> |
* Encodings for Node hash fields. See above for explanation. |
561 |
|
*/ |
562 |
< |
|
563 |
< |
static final Node tabAt(Node[] tab, int i) { // used by Iter |
564 |
< |
return (Node)UNSAFE.getObjectVolatile(tab, ((long)i<<ASHIFT)+ABASE); |
565 |
< |
} |
566 |
< |
|
567 |
< |
private static final boolean casTabAt(Node[] tab, int i, Node c, Node v) { |
568 |
< |
return UNSAFE.compareAndSwapObject(tab, ((long)i<<ASHIFT)+ABASE, c, v); |
569 |
< |
} |
570 |
< |
|
571 |
< |
private static final void setTabAt(Node[] tab, int i, Node v) { |
572 |
< |
UNSAFE.putObjectVolatile(tab, ((long)i<<ASHIFT)+ABASE, v); |
573 |
< |
} |
562 |
> |
static final int MOVED = -1; // hash for forwarding nodes |
563 |
> |
static final int TREEBIN = -2; // hash for roots of trees |
564 |
> |
static final int RESERVED = -3; // hash for transient reservations |
565 |
> |
static final int HASH_BITS = 0x7fffffff; // usable bits of normal node hash |
566 |
> |
|
567 |
> |
/** Number of CPUS, to place bounds on some sizings */ |
568 |
> |
static final int NCPU = Runtime.getRuntime().availableProcessors(); |
569 |
> |
|
570 |
> |
/** |
571 |
> |
* Serialized pseudo-fields, provided only for jdk7 compatibility. |
572 |
> |
* @serialField segments Segment[] |
573 |
> |
* The segments, each of which is a specialized hash table. |
574 |
> |
* @serialField segmentMask int |
575 |
> |
* Mask value for indexing into segments. The upper bits of a |
576 |
> |
* key's hash code are used to choose the segment. |
577 |
> |
* @serialField segmentShift int |
578 |
> |
* Shift value for indexing within segments. |
579 |
> |
*/ |
580 |
> |
private static final ObjectStreamField[] serialPersistentFields = { |
581 |
> |
new ObjectStreamField("segments", Segment[].class), |
582 |
> |
new ObjectStreamField("segmentMask", Integer.TYPE), |
583 |
> |
new ObjectStreamField("segmentShift", Integer.TYPE), |
584 |
> |
}; |
585 |
|
|
586 |
|
/* ---------------- Nodes -------------- */ |
587 |
|
|
588 |
|
/** |
589 |
< |
* Key-value entry. Note that this is never exported out as a |
590 |
< |
* user-visible Map.Entry (see MapEntry below). Nodes with a hash |
591 |
< |
* field of MOVED are special, and do not contain user keys or |
592 |
< |
* values. Otherwise, keys are never null, and null val fields |
593 |
< |
* indicate that a node is in the process of being deleted or |
594 |
< |
* created. For purposes of read-only access, a key may be read |
595 |
< |
* before a val, but can only be used after checking val to be |
596 |
< |
* non-null. |
597 |
< |
*/ |
598 |
< |
static class Node { |
599 |
< |
volatile int hash; |
600 |
< |
final Object key; |
624 |
< |
volatile Object val; |
625 |
< |
volatile Node next; |
589 |
> |
* Key-value entry. This class is never exported out as a |
590 |
> |
* user-mutable Map.Entry (i.e., one supporting setValue; see |
591 |
> |
* MapEntry below), but can be used for read-only traversals used |
592 |
> |
* in bulk tasks. Subclasses of Node with a negative hash field |
593 |
> |
* are special, and contain null keys and values (but are never |
594 |
> |
* exported). Otherwise, keys and vals are never null. |
595 |
> |
*/ |
596 |
> |
static class Node<K,V> implements Map.Entry<K,V> { |
597 |
> |
final int hash; |
598 |
> |
final K key; |
599 |
> |
volatile V val; |
600 |
> |
volatile Node<K,V> next; |
601 |
|
|
602 |
< |
Node(int hash, Object key, Object val, Node next) { |
602 |
> |
Node(int hash, K key, V val) { |
603 |
|
this.hash = hash; |
604 |
|
this.key = key; |
605 |
|
this.val = val; |
631 |
– |
this.next = next; |
632 |
– |
} |
633 |
– |
|
634 |
– |
/** CompareAndSet the hash field */ |
635 |
– |
final boolean casHash(int cmp, int val) { |
636 |
– |
return UNSAFE.compareAndSwapInt(this, hashOffset, cmp, val); |
637 |
– |
} |
638 |
– |
|
639 |
– |
/** The number of spins before blocking for a lock */ |
640 |
– |
static final int MAX_SPINS = |
641 |
– |
Runtime.getRuntime().availableProcessors() > 1 ? 64 : 1; |
642 |
– |
|
643 |
– |
/** |
644 |
– |
* Spins a while if LOCKED bit set and this node is the first |
645 |
– |
* of its bin, and then sets WAITING bits on hash field and |
646 |
– |
* blocks (once) if they are still set. It is OK for this |
647 |
– |
* method to return even if lock is not available upon exit, |
648 |
– |
* which enables these simple single-wait mechanics. |
649 |
– |
* |
650 |
– |
* The corresponding signalling operation is performed within |
651 |
– |
* callers: Upon detecting that WAITING has been set when |
652 |
– |
* unlocking lock (via a failed CAS from non-waiting LOCKED |
653 |
– |
* state), unlockers acquire the sync lock and perform a |
654 |
– |
* notifyAll. |
655 |
– |
* |
656 |
– |
* The initial sanity check on tab and bounds is not currently |
657 |
– |
* necessary in the only usages of this method, but enables |
658 |
– |
* use in other future contexts. |
659 |
– |
*/ |
660 |
– |
final void tryAwaitLock(Node[] tab, int i) { |
661 |
– |
if (tab != null && i >= 0 && i < tab.length) { // sanity check |
662 |
– |
int r = ThreadLocalRandom.current().nextInt(); // randomize spins |
663 |
– |
int spins = MAX_SPINS, h; |
664 |
– |
while (tabAt(tab, i) == this && ((h = hash) & LOCKED) != 0) { |
665 |
– |
if (spins >= 0) { |
666 |
– |
r ^= r << 1; r ^= r >>> 3; r ^= r << 10; // xorshift |
667 |
– |
if (r >= 0 && --spins == 0) |
668 |
– |
Thread.yield(); // yield before block |
669 |
– |
} |
670 |
– |
else if (casHash(h, h | WAITING)) { |
671 |
– |
synchronized (this) { |
672 |
– |
if (tabAt(tab, i) == this && |
673 |
– |
(hash & WAITING) == WAITING) { |
674 |
– |
try { |
675 |
– |
wait(); |
676 |
– |
} catch (InterruptedException ie) { |
677 |
– |
try { |
678 |
– |
Thread.currentThread().interrupt(); |
679 |
– |
} catch (SecurityException ignore) { |
680 |
– |
} |
681 |
– |
} |
682 |
– |
} |
683 |
– |
else |
684 |
– |
notifyAll(); // possibly won race vs signaller |
685 |
– |
} |
686 |
– |
break; |
687 |
– |
} |
688 |
– |
} |
689 |
– |
} |
690 |
– |
} |
691 |
– |
|
692 |
– |
// Unsafe mechanics for casHash |
693 |
– |
private static final sun.misc.Unsafe UNSAFE; |
694 |
– |
private static final long hashOffset; |
695 |
– |
|
696 |
– |
static { |
697 |
– |
try { |
698 |
– |
UNSAFE = sun.misc.Unsafe.getUnsafe(); |
699 |
– |
Class<?> k = Node.class; |
700 |
– |
hashOffset = UNSAFE.objectFieldOffset |
701 |
– |
(k.getDeclaredField("hash")); |
702 |
– |
} catch (Exception e) { |
703 |
– |
throw new Error(e); |
704 |
– |
} |
606 |
|
} |
706 |
– |
} |
607 |
|
|
608 |
< |
/* ---------------- TreeBins -------------- */ |
609 |
< |
|
610 |
< |
/** |
711 |
< |
* Nodes for use in TreeBins |
712 |
< |
*/ |
713 |
< |
static final class TreeNode extends Node { |
714 |
< |
TreeNode parent; // red-black tree links |
715 |
< |
TreeNode left; |
716 |
< |
TreeNode right; |
717 |
< |
TreeNode prev; // needed to unlink next upon deletion |
718 |
< |
boolean red; |
719 |
< |
|
720 |
< |
TreeNode(int hash, Object key, Object val, Node next, TreeNode parent) { |
721 |
< |
super(hash, key, val, next); |
722 |
< |
this.parent = parent; |
723 |
< |
} |
724 |
< |
} |
725 |
< |
|
726 |
< |
/** |
727 |
< |
* A specialized form of red-black tree for use in bins |
728 |
< |
* whose size exceeds a threshold. |
729 |
< |
* |
730 |
< |
* TreeBins use a special form of comparison for search and |
731 |
< |
* related operations (which is the main reason we cannot use |
732 |
< |
* existing collections such as TreeMaps). TreeBins contain |
733 |
< |
* Comparable elements, but may contain others, as well as |
734 |
< |
* elements that are Comparable but not necessarily Comparable<T> |
735 |
< |
* for the same T, so we cannot invoke compareTo among them. To |
736 |
< |
* handle this, the tree is ordered primarily by hash value, then |
737 |
< |
* by getClass().getName() order, and then by Comparator order |
738 |
< |
* among elements of the same class. On lookup at a node, if |
739 |
< |
* elements are not comparable or compare as 0, both left and |
740 |
< |
* right children may need to be searched in the case of tied hash |
741 |
< |
* values. (This corresponds to the full list search that would be |
742 |
< |
* necessary if all elements were non-Comparable and had tied |
743 |
< |
* hashes.) The red-black balancing code is updated from |
744 |
< |
* pre-jdk-collections |
745 |
< |
* (http://gee.cs.oswego.edu/dl/classes/collections/RBCell.java) |
746 |
< |
* based in turn on Cormen, Leiserson, and Rivest "Introduction to |
747 |
< |
* Algorithms" (CLR). |
748 |
< |
* |
749 |
< |
* TreeBins also maintain a separate locking discipline than |
750 |
< |
* regular bins. Because they are forwarded via special MOVED |
751 |
< |
* nodes at bin heads (which can never change once established), |
752 |
< |
* we cannot use those nodes as locks. Instead, TreeBin |
753 |
< |
* extends AbstractQueuedSynchronizer to support a simple form of |
754 |
< |
* read-write lock. For update operations and table validation, |
755 |
< |
* the exclusive form of lock behaves in the same way as bin-head |
756 |
< |
* locks. However, lookups use shared read-lock mechanics to allow |
757 |
< |
* multiple readers in the absence of writers. Additionally, |
758 |
< |
* these lookups do not ever block: While the lock is not |
759 |
< |
* available, they proceed along the slow traversal path (via |
760 |
< |
* next-pointers) until the lock becomes available or the list is |
761 |
< |
* exhausted, whichever comes first. (These cases are not fast, |
762 |
< |
* but maximize aggregate expected throughput.) The AQS mechanics |
763 |
< |
* for doing this are straightforward. The lock state is held as |
764 |
< |
* AQS getState(). Read counts are negative; the write count (1) |
765 |
< |
* is positive. There are no signalling preferences among readers |
766 |
< |
* and writers. Since we don't need to export full Lock API, we |
767 |
< |
* just override the minimal AQS methods and use them directly. |
768 |
< |
*/ |
769 |
< |
static final class TreeBin extends AbstractQueuedSynchronizer { |
770 |
< |
private static final long serialVersionUID = 2249069246763182397L; |
771 |
< |
transient TreeNode root; // root of tree |
772 |
< |
transient TreeNode first; // head of next-pointer list |
773 |
< |
|
774 |
< |
/* AQS overrides */ |
775 |
< |
public final boolean isHeldExclusively() { return getState() > 0; } |
776 |
< |
public final boolean tryAcquire(int ignore) { |
777 |
< |
if (compareAndSetState(0, 1)) { |
778 |
< |
setExclusiveOwnerThread(Thread.currentThread()); |
779 |
< |
return true; |
780 |
< |
} |
781 |
< |
return false; |
782 |
< |
} |
783 |
< |
public final boolean tryRelease(int ignore) { |
784 |
< |
setExclusiveOwnerThread(null); |
785 |
< |
setState(0); |
786 |
< |
return true; |
787 |
< |
} |
788 |
< |
public final int tryAcquireShared(int ignore) { |
789 |
< |
for (int c;;) { |
790 |
< |
if ((c = getState()) > 0) |
791 |
< |
return -1; |
792 |
< |
if (compareAndSetState(c, c -1)) |
793 |
< |
return 1; |
794 |
< |
} |
795 |
< |
} |
796 |
< |
public final boolean tryReleaseShared(int ignore) { |
797 |
< |
int c; |
798 |
< |
do {} while (!compareAndSetState(c = getState(), c + 1)); |
799 |
< |
return c == -1; |
800 |
< |
} |
801 |
< |
|
802 |
< |
/** From CLR */ |
803 |
< |
private void rotateLeft(TreeNode p) { |
804 |
< |
if (p != null) { |
805 |
< |
TreeNode r = p.right, pp, rl; |
806 |
< |
if ((rl = p.right = r.left) != null) |
807 |
< |
rl.parent = p; |
808 |
< |
if ((pp = r.parent = p.parent) == null) |
809 |
< |
root = r; |
810 |
< |
else if (pp.left == p) |
811 |
< |
pp.left = r; |
812 |
< |
else |
813 |
< |
pp.right = r; |
814 |
< |
r.left = p; |
815 |
< |
p.parent = r; |
816 |
< |
} |
608 |
> |
Node(int hash, K key, V val, Node<K,V> next) { |
609 |
> |
this(hash, key, val); |
610 |
> |
this.next = next; |
611 |
|
} |
612 |
|
|
613 |
< |
/** From CLR */ |
614 |
< |
private void rotateRight(TreeNode p) { |
615 |
< |
if (p != null) { |
616 |
< |
TreeNode l = p.left, pp, lr; |
617 |
< |
if ((lr = p.left = l.right) != null) |
824 |
< |
lr.parent = p; |
825 |
< |
if ((pp = l.parent = p.parent) == null) |
826 |
< |
root = l; |
827 |
< |
else if (pp.right == p) |
828 |
< |
pp.right = l; |
829 |
< |
else |
830 |
< |
pp.left = l; |
831 |
< |
l.right = p; |
832 |
< |
p.parent = l; |
833 |
< |
} |
613 |
> |
public final K getKey() { return key; } |
614 |
> |
public final V getValue() { return val; } |
615 |
> |
public final int hashCode() { return key.hashCode() ^ val.hashCode(); } |
616 |
> |
public final String toString() { |
617 |
> |
return Helpers.mapEntryToString(key, val); |
618 |
|
} |
619 |
< |
|
620 |
< |
/** |
837 |
< |
* Returns the TreeNode (or null if not found) for the given key |
838 |
< |
* starting at given root. |
839 |
< |
*/ |
840 |
< |
@SuppressWarnings("unchecked") final TreeNode getTreeNode |
841 |
< |
(int h, Object k, TreeNode p) { |
842 |
< |
Class<?> c = k.getClass(); |
843 |
< |
while (p != null) { |
844 |
< |
int dir, ph; Object pk; Class<?> pc; |
845 |
< |
if ((ph = p.hash) == h) { |
846 |
< |
if ((pk = p.key) == k || k.equals(pk)) |
847 |
< |
return p; |
848 |
< |
if (c != (pc = pk.getClass()) || |
849 |
< |
!(k instanceof Comparable) || |
850 |
< |
(dir = ((Comparable)k).compareTo((Comparable)pk)) == 0) { |
851 |
< |
dir = (c == pc) ? 0 : c.getName().compareTo(pc.getName()); |
852 |
< |
TreeNode r = null, s = null, pl, pr; |
853 |
< |
if (dir >= 0) { |
854 |
< |
if ((pl = p.left) != null && h <= pl.hash) |
855 |
< |
s = pl; |
856 |
< |
} |
857 |
< |
else if ((pr = p.right) != null && h >= pr.hash) |
858 |
< |
s = pr; |
859 |
< |
if (s != null && (r = getTreeNode(h, k, s)) != null) |
860 |
< |
return r; |
861 |
< |
} |
862 |
< |
} |
863 |
< |
else |
864 |
< |
dir = (h < ph) ? -1 : 1; |
865 |
< |
p = (dir > 0) ? p.right : p.left; |
866 |
< |
} |
867 |
< |
return null; |
619 |
> |
public final V setValue(V value) { |
620 |
> |
throw new UnsupportedOperationException(); |
621 |
|
} |
622 |
|
|
623 |
< |
/** |
624 |
< |
* Wrapper for getTreeNode used by CHM.get. Tries to obtain |
625 |
< |
* read-lock to call getTreeNode, but during failure to get |
626 |
< |
* lock, searches along next links. |
627 |
< |
*/ |
628 |
< |
final Object getValue(int h, Object k) { |
629 |
< |
Node r = null; |
877 |
< |
int c = getState(); // Must read lock state first |
878 |
< |
for (Node e = first; e != null; e = e.next) { |
879 |
< |
if (c <= 0 && compareAndSetState(c, c - 1)) { |
880 |
< |
try { |
881 |
< |
r = getTreeNode(h, k, root); |
882 |
< |
} finally { |
883 |
< |
releaseShared(0); |
884 |
< |
} |
885 |
< |
break; |
886 |
< |
} |
887 |
< |
else if ((e.hash & HASH_BITS) == h && k.equals(e.key)) { |
888 |
< |
r = e; |
889 |
< |
break; |
890 |
< |
} |
891 |
< |
else |
892 |
< |
c = getState(); |
893 |
< |
} |
894 |
< |
return r == null ? null : r.val; |
623 |
> |
public final boolean equals(Object o) { |
624 |
> |
Object k, v, u; Map.Entry<?,?> e; |
625 |
> |
return ((o instanceof Map.Entry) && |
626 |
> |
(k = (e = (Map.Entry<?,?>)o).getKey()) != null && |
627 |
> |
(v = e.getValue()) != null && |
628 |
> |
(k == key || k.equals(key)) && |
629 |
> |
(v == (u = val) || v.equals(u))); |
630 |
|
} |
631 |
|
|
632 |
|
/** |
633 |
< |
* Finds or adds a node. |
899 |
< |
* @return null if added |
633 |
> |
* Virtualized support for map.get(); overridden in subclasses. |
634 |
|
*/ |
635 |
< |
@SuppressWarnings("unchecked") final TreeNode putTreeNode |
636 |
< |
(int h, Object k, Object v) { |
637 |
< |
Class<?> c = k.getClass(); |
638 |
< |
TreeNode pp = root, p = null; |
639 |
< |
int dir = 0; |
640 |
< |
while (pp != null) { // find existing node or leaf to insert at |
641 |
< |
int ph; Object pk; Class<?> pc; |
642 |
< |
p = pp; |
643 |
< |
if ((ph = p.hash) == h) { |
910 |
< |
if ((pk = p.key) == k || k.equals(pk)) |
911 |
< |
return p; |
912 |
< |
if (c != (pc = pk.getClass()) || |
913 |
< |
!(k instanceof Comparable) || |
914 |
< |
(dir = ((Comparable)k).compareTo((Comparable)pk)) == 0) { |
915 |
< |
dir = (c == pc) ? 0 : c.getName().compareTo(pc.getName()); |
916 |
< |
TreeNode r = null, s = null, pl, pr; |
917 |
< |
if (dir >= 0) { |
918 |
< |
if ((pl = p.left) != null && h <= pl.hash) |
919 |
< |
s = pl; |
920 |
< |
} |
921 |
< |
else if ((pr = p.right) != null && h >= pr.hash) |
922 |
< |
s = pr; |
923 |
< |
if (s != null && (r = getTreeNode(h, k, s)) != null) |
924 |
< |
return r; |
925 |
< |
} |
926 |
< |
} |
927 |
< |
else |
928 |
< |
dir = (h < ph) ? -1 : 1; |
929 |
< |
pp = (dir > 0) ? p.right : p.left; |
930 |
< |
} |
931 |
< |
|
932 |
< |
TreeNode f = first; |
933 |
< |
TreeNode x = first = new TreeNode(h, k, v, f, p); |
934 |
< |
if (p == null) |
935 |
< |
root = x; |
936 |
< |
else { // attach and rebalance; adapted from CLR |
937 |
< |
TreeNode xp, xpp; |
938 |
< |
if (f != null) |
939 |
< |
f.prev = x; |
940 |
< |
if (dir <= 0) |
941 |
< |
p.left = x; |
942 |
< |
else |
943 |
< |
p.right = x; |
944 |
< |
x.red = true; |
945 |
< |
while (x != null && (xp = x.parent) != null && xp.red && |
946 |
< |
(xpp = xp.parent) != null) { |
947 |
< |
TreeNode xppl = xpp.left; |
948 |
< |
if (xp == xppl) { |
949 |
< |
TreeNode y = xpp.right; |
950 |
< |
if (y != null && y.red) { |
951 |
< |
y.red = false; |
952 |
< |
xp.red = false; |
953 |
< |
xpp.red = true; |
954 |
< |
x = xpp; |
955 |
< |
} |
956 |
< |
else { |
957 |
< |
if (x == xp.right) { |
958 |
< |
rotateLeft(x = xp); |
959 |
< |
xpp = (xp = x.parent) == null ? null : xp.parent; |
960 |
< |
} |
961 |
< |
if (xp != null) { |
962 |
< |
xp.red = false; |
963 |
< |
if (xpp != null) { |
964 |
< |
xpp.red = true; |
965 |
< |
rotateRight(xpp); |
966 |
< |
} |
967 |
< |
} |
968 |
< |
} |
969 |
< |
} |
970 |
< |
else { |
971 |
< |
TreeNode y = xppl; |
972 |
< |
if (y != null && y.red) { |
973 |
< |
y.red = false; |
974 |
< |
xp.red = false; |
975 |
< |
xpp.red = true; |
976 |
< |
x = xpp; |
977 |
< |
} |
978 |
< |
else { |
979 |
< |
if (x == xp.left) { |
980 |
< |
rotateRight(x = xp); |
981 |
< |
xpp = (xp = x.parent) == null ? null : xp.parent; |
982 |
< |
} |
983 |
< |
if (xp != null) { |
984 |
< |
xp.red = false; |
985 |
< |
if (xpp != null) { |
986 |
< |
xpp.red = true; |
987 |
< |
rotateLeft(xpp); |
988 |
< |
} |
989 |
< |
} |
990 |
< |
} |
991 |
< |
} |
992 |
< |
} |
993 |
< |
TreeNode r = root; |
994 |
< |
if (r != null && r.red) |
995 |
< |
r.red = false; |
635 |
> |
Node<K,V> find(int h, Object k) { |
636 |
> |
Node<K,V> e = this; |
637 |
> |
if (k != null) { |
638 |
> |
do { |
639 |
> |
K ek; |
640 |
> |
if (e.hash == h && |
641 |
> |
((ek = e.key) == k || (ek != null && k.equals(ek)))) |
642 |
> |
return e; |
643 |
> |
} while ((e = e.next) != null); |
644 |
|
} |
645 |
|
return null; |
646 |
|
} |
999 |
– |
|
1000 |
– |
/** |
1001 |
– |
* Removes the given node, that must be present before this |
1002 |
– |
* call. This is messier than typical red-black deletion code |
1003 |
– |
* because we cannot swap the contents of an interior node |
1004 |
– |
* with a leaf successor that is pinned by "next" pointers |
1005 |
– |
* that are accessible independently of lock. So instead we |
1006 |
– |
* swap the tree linkages. |
1007 |
– |
*/ |
1008 |
– |
final void deleteTreeNode(TreeNode p) { |
1009 |
– |
TreeNode next = (TreeNode)p.next; // unlink traversal pointers |
1010 |
– |
TreeNode pred = p.prev; |
1011 |
– |
if (pred == null) |
1012 |
– |
first = next; |
1013 |
– |
else |
1014 |
– |
pred.next = next; |
1015 |
– |
if (next != null) |
1016 |
– |
next.prev = pred; |
1017 |
– |
TreeNode replacement; |
1018 |
– |
TreeNode pl = p.left; |
1019 |
– |
TreeNode pr = p.right; |
1020 |
– |
if (pl != null && pr != null) { |
1021 |
– |
TreeNode s = pr, sl; |
1022 |
– |
while ((sl = s.left) != null) // find successor |
1023 |
– |
s = sl; |
1024 |
– |
boolean c = s.red; s.red = p.red; p.red = c; // swap colors |
1025 |
– |
TreeNode sr = s.right; |
1026 |
– |
TreeNode pp = p.parent; |
1027 |
– |
if (s == pr) { // p was s's direct parent |
1028 |
– |
p.parent = s; |
1029 |
– |
s.right = p; |
1030 |
– |
} |
1031 |
– |
else { |
1032 |
– |
TreeNode sp = s.parent; |
1033 |
– |
if ((p.parent = sp) != null) { |
1034 |
– |
if (s == sp.left) |
1035 |
– |
sp.left = p; |
1036 |
– |
else |
1037 |
– |
sp.right = p; |
1038 |
– |
} |
1039 |
– |
if ((s.right = pr) != null) |
1040 |
– |
pr.parent = s; |
1041 |
– |
} |
1042 |
– |
p.left = null; |
1043 |
– |
if ((p.right = sr) != null) |
1044 |
– |
sr.parent = p; |
1045 |
– |
if ((s.left = pl) != null) |
1046 |
– |
pl.parent = s; |
1047 |
– |
if ((s.parent = pp) == null) |
1048 |
– |
root = s; |
1049 |
– |
else if (p == pp.left) |
1050 |
– |
pp.left = s; |
1051 |
– |
else |
1052 |
– |
pp.right = s; |
1053 |
– |
replacement = sr; |
1054 |
– |
} |
1055 |
– |
else |
1056 |
– |
replacement = (pl != null) ? pl : pr; |
1057 |
– |
TreeNode pp = p.parent; |
1058 |
– |
if (replacement == null) { |
1059 |
– |
if (pp == null) { |
1060 |
– |
root = null; |
1061 |
– |
return; |
1062 |
– |
} |
1063 |
– |
replacement = p; |
1064 |
– |
} |
1065 |
– |
else { |
1066 |
– |
replacement.parent = pp; |
1067 |
– |
if (pp == null) |
1068 |
– |
root = replacement; |
1069 |
– |
else if (p == pp.left) |
1070 |
– |
pp.left = replacement; |
1071 |
– |
else |
1072 |
– |
pp.right = replacement; |
1073 |
– |
p.left = p.right = p.parent = null; |
1074 |
– |
} |
1075 |
– |
if (!p.red) { // rebalance, from CLR |
1076 |
– |
TreeNode x = replacement; |
1077 |
– |
while (x != null) { |
1078 |
– |
TreeNode xp, xpl; |
1079 |
– |
if (x.red || (xp = x.parent) == null) { |
1080 |
– |
x.red = false; |
1081 |
– |
break; |
1082 |
– |
} |
1083 |
– |
if (x == (xpl = xp.left)) { |
1084 |
– |
TreeNode sib = xp.right; |
1085 |
– |
if (sib != null && sib.red) { |
1086 |
– |
sib.red = false; |
1087 |
– |
xp.red = true; |
1088 |
– |
rotateLeft(xp); |
1089 |
– |
sib = (xp = x.parent) == null ? null : xp.right; |
1090 |
– |
} |
1091 |
– |
if (sib == null) |
1092 |
– |
x = xp; |
1093 |
– |
else { |
1094 |
– |
TreeNode sl = sib.left, sr = sib.right; |
1095 |
– |
if ((sr == null || !sr.red) && |
1096 |
– |
(sl == null || !sl.red)) { |
1097 |
– |
sib.red = true; |
1098 |
– |
x = xp; |
1099 |
– |
} |
1100 |
– |
else { |
1101 |
– |
if (sr == null || !sr.red) { |
1102 |
– |
if (sl != null) |
1103 |
– |
sl.red = false; |
1104 |
– |
sib.red = true; |
1105 |
– |
rotateRight(sib); |
1106 |
– |
sib = (xp = x.parent) == null ? null : xp.right; |
1107 |
– |
} |
1108 |
– |
if (sib != null) { |
1109 |
– |
sib.red = (xp == null) ? false : xp.red; |
1110 |
– |
if ((sr = sib.right) != null) |
1111 |
– |
sr.red = false; |
1112 |
– |
} |
1113 |
– |
if (xp != null) { |
1114 |
– |
xp.red = false; |
1115 |
– |
rotateLeft(xp); |
1116 |
– |
} |
1117 |
– |
x = root; |
1118 |
– |
} |
1119 |
– |
} |
1120 |
– |
} |
1121 |
– |
else { // symmetric |
1122 |
– |
TreeNode sib = xpl; |
1123 |
– |
if (sib != null && sib.red) { |
1124 |
– |
sib.red = false; |
1125 |
– |
xp.red = true; |
1126 |
– |
rotateRight(xp); |
1127 |
– |
sib = (xp = x.parent) == null ? null : xp.left; |
1128 |
– |
} |
1129 |
– |
if (sib == null) |
1130 |
– |
x = xp; |
1131 |
– |
else { |
1132 |
– |
TreeNode sl = sib.left, sr = sib.right; |
1133 |
– |
if ((sl == null || !sl.red) && |
1134 |
– |
(sr == null || !sr.red)) { |
1135 |
– |
sib.red = true; |
1136 |
– |
x = xp; |
1137 |
– |
} |
1138 |
– |
else { |
1139 |
– |
if (sl == null || !sl.red) { |
1140 |
– |
if (sr != null) |
1141 |
– |
sr.red = false; |
1142 |
– |
sib.red = true; |
1143 |
– |
rotateLeft(sib); |
1144 |
– |
sib = (xp = x.parent) == null ? null : xp.left; |
1145 |
– |
} |
1146 |
– |
if (sib != null) { |
1147 |
– |
sib.red = (xp == null) ? false : xp.red; |
1148 |
– |
if ((sl = sib.left) != null) |
1149 |
– |
sl.red = false; |
1150 |
– |
} |
1151 |
– |
if (xp != null) { |
1152 |
– |
xp.red = false; |
1153 |
– |
rotateRight(xp); |
1154 |
– |
} |
1155 |
– |
x = root; |
1156 |
– |
} |
1157 |
– |
} |
1158 |
– |
} |
1159 |
– |
} |
1160 |
– |
} |
1161 |
– |
if (p == replacement && (pp = p.parent) != null) { |
1162 |
– |
if (p == pp.left) // detach pointers |
1163 |
– |
pp.left = null; |
1164 |
– |
else if (p == pp.right) |
1165 |
– |
pp.right = null; |
1166 |
– |
p.parent = null; |
1167 |
– |
} |
1168 |
– |
} |
647 |
|
} |
648 |
|
|
649 |
< |
/* ---------------- Collision reduction methods -------------- */ |
649 |
> |
/* ---------------- Static utilities -------------- */ |
650 |
|
|
651 |
|
/** |
652 |
< |
* Spreads higher bits to lower, and also forces top 2 bits to 0. |
653 |
< |
* Because the table uses power-of-two masking, sets of hashes |
654 |
< |
* that vary only in bits above the current mask will always |
655 |
< |
* collide. (Among known examples are sets of Float keys holding |
656 |
< |
* consecutive whole numbers in small tables.) To counter this, |
657 |
< |
* we apply a transform that spreads the impact of higher bits |
652 |
> |
* Spreads (XORs) higher bits of hash to lower and also forces top |
653 |
> |
* bit to 0. Because the table uses power-of-two masking, sets of |
654 |
> |
* hashes that vary only in bits above the current mask will |
655 |
> |
* always collide. (Among known examples are sets of Float keys |
656 |
> |
* holding consecutive whole numbers in small tables.) So we |
657 |
> |
* apply a transform that spreads the impact of higher bits |
658 |
|
* downward. There is a tradeoff between speed, utility, and |
659 |
|
* quality of bit-spreading. Because many common sets of hashes |
660 |
< |
* are already reasonably distributed across bits (so don't benefit |
661 |
< |
* from spreading), and because we use trees to handle large sets |
662 |
< |
* of collisions in bins, we don't need excessively high quality. |
660 |
> |
* are already reasonably distributed (so don't benefit from |
661 |
> |
* spreading), and because we use trees to handle large sets of |
662 |
> |
* collisions in bins, we just XOR some shifted bits in the |
663 |
> |
* cheapest possible way to reduce systematic lossage, as well as |
664 |
> |
* to incorporate impact of the highest bits that would otherwise |
665 |
> |
* never be used in index calculations because of table bounds. |
666 |
|
*/ |
667 |
< |
private static final int spread(int h) { |
668 |
< |
h ^= (h >>> 18) ^ (h >>> 12); |
1188 |
< |
return (h ^ (h >>> 10)) & HASH_BITS; |
667 |
> |
static final int spread(int h) { |
668 |
> |
return (h ^ (h >>> 16)) & HASH_BITS; |
669 |
|
} |
670 |
|
|
671 |
|
/** |
672 |
< |
* Replaces a list bin with a tree bin. Call only when locked. |
673 |
< |
* Fails to replace if the given key is non-comparable or table |
1194 |
< |
* is, or needs, resizing. |
672 |
> |
* Returns a power of two table size for the given desired capacity. |
673 |
> |
* See Hackers Delight, sec 3.2 |
674 |
|
*/ |
675 |
< |
private final void replaceWithTreeBin(Node[] tab, int index, Object key) { |
676 |
< |
if ((key instanceof Comparable) && |
677 |
< |
(tab.length >= MAXIMUM_CAPACITY || counter.sum() < (long)sizeCtl)) { |
1199 |
< |
TreeBin t = new TreeBin(); |
1200 |
< |
for (Node e = tabAt(tab, index); e != null; e = e.next) |
1201 |
< |
t.putTreeNode(e.hash & HASH_BITS, e.key, e.val); |
1202 |
< |
setTabAt(tab, index, new Node(MOVED, t, null, null)); |
1203 |
< |
} |
1204 |
< |
} |
1205 |
< |
|
1206 |
< |
/* ---------------- Internal access and update methods -------------- */ |
1207 |
< |
|
1208 |
< |
/** Implementation for get and containsKey */ |
1209 |
< |
private final Object internalGet(Object k) { |
1210 |
< |
int h = spread(k.hashCode()); |
1211 |
< |
retry: for (Node[] tab = table; tab != null;) { |
1212 |
< |
Node e, p; Object ek, ev; int eh; // locals to read fields once |
1213 |
< |
for (e = tabAt(tab, (tab.length - 1) & h); e != null; e = e.next) { |
1214 |
< |
if ((eh = e.hash) == MOVED) { |
1215 |
< |
if ((ek = e.key) instanceof TreeBin) // search TreeBin |
1216 |
< |
return ((TreeBin)ek).getValue(h, k); |
1217 |
< |
else { // restart with new table |
1218 |
< |
tab = (Node[])ek; |
1219 |
< |
continue retry; |
1220 |
< |
} |
1221 |
< |
} |
1222 |
< |
else if ((eh & HASH_BITS) == h && (ev = e.val) != null && |
1223 |
< |
((ek = e.key) == k || k.equals(ek))) |
1224 |
< |
return ev; |
1225 |
< |
} |
1226 |
< |
break; |
1227 |
< |
} |
1228 |
< |
return null; |
675 |
> |
private static final int tableSizeFor(int c) { |
676 |
> |
int n = -1 >>> Integer.numberOfLeadingZeros(c - 1); |
677 |
> |
return (n < 0) ? 1 : (n >= MAXIMUM_CAPACITY) ? MAXIMUM_CAPACITY : n + 1; |
678 |
|
} |
679 |
|
|
680 |
|
/** |
681 |
< |
* Implementation for the four public remove/replace methods: |
682 |
< |
* Replaces node value with v, conditional upon match of cv if |
1234 |
< |
* non-null. If resulting value is null, delete. |
681 |
> |
* Returns x's Class if it is of the form "class C implements |
682 |
> |
* Comparable<C>", else null. |
683 |
|
*/ |
684 |
< |
private final Object internalReplace(Object k, Object v, Object cv) { |
685 |
< |
int h = spread(k.hashCode()); |
686 |
< |
Object oldVal = null; |
687 |
< |
for (Node[] tab = table;;) { |
688 |
< |
Node f; int i, fh; Object fk; |
689 |
< |
if (tab == null || |
690 |
< |
(f = tabAt(tab, i = (tab.length - 1) & h)) == null) |
691 |
< |
break; |
692 |
< |
else if ((fh = f.hash) == MOVED) { |
693 |
< |
if ((fk = f.key) instanceof TreeBin) { |
694 |
< |
TreeBin t = (TreeBin)fk; |
695 |
< |
boolean validated = false; |
696 |
< |
boolean deleted = false; |
1249 |
< |
t.acquire(0); |
1250 |
< |
try { |
1251 |
< |
if (tabAt(tab, i) == f) { |
1252 |
< |
validated = true; |
1253 |
< |
TreeNode p = t.getTreeNode(h, k, t.root); |
1254 |
< |
if (p != null) { |
1255 |
< |
Object pv = p.val; |
1256 |
< |
if (cv == null || cv == pv || cv.equals(pv)) { |
1257 |
< |
oldVal = pv; |
1258 |
< |
if ((p.val = v) == null) { |
1259 |
< |
deleted = true; |
1260 |
< |
t.deleteTreeNode(p); |
1261 |
< |
} |
1262 |
< |
} |
1263 |
< |
} |
1264 |
< |
} |
1265 |
< |
} finally { |
1266 |
< |
t.release(0); |
1267 |
< |
} |
1268 |
< |
if (validated) { |
1269 |
< |
if (deleted) |
1270 |
< |
counter.add(-1L); |
1271 |
< |
break; |
1272 |
< |
} |
1273 |
< |
} |
1274 |
< |
else |
1275 |
< |
tab = (Node[])fk; |
1276 |
< |
} |
1277 |
< |
else if ((fh & HASH_BITS) != h && f.next == null) // precheck |
1278 |
< |
break; // rules out possible existence |
1279 |
< |
else if ((fh & LOCKED) != 0) { |
1280 |
< |
checkForResize(); // try resizing if can't get lock |
1281 |
< |
f.tryAwaitLock(tab, i); |
1282 |
< |
} |
1283 |
< |
else if (f.casHash(fh, fh | LOCKED)) { |
1284 |
< |
boolean validated = false; |
1285 |
< |
boolean deleted = false; |
1286 |
< |
try { |
1287 |
< |
if (tabAt(tab, i) == f) { |
1288 |
< |
validated = true; |
1289 |
< |
for (Node e = f, pred = null;;) { |
1290 |
< |
Object ek, ev; |
1291 |
< |
if ((e.hash & HASH_BITS) == h && |
1292 |
< |
((ev = e.val) != null) && |
1293 |
< |
((ek = e.key) == k || k.equals(ek))) { |
1294 |
< |
if (cv == null || cv == ev || cv.equals(ev)) { |
1295 |
< |
oldVal = ev; |
1296 |
< |
if ((e.val = v) == null) { |
1297 |
< |
deleted = true; |
1298 |
< |
Node en = e.next; |
1299 |
< |
if (pred != null) |
1300 |
< |
pred.next = en; |
1301 |
< |
else |
1302 |
< |
setTabAt(tab, i, en); |
1303 |
< |
} |
1304 |
< |
} |
1305 |
< |
break; |
1306 |
< |
} |
1307 |
< |
pred = e; |
1308 |
< |
if ((e = e.next) == null) |
1309 |
< |
break; |
1310 |
< |
} |
1311 |
< |
} |
1312 |
< |
} finally { |
1313 |
< |
if (!f.casHash(fh | LOCKED, fh)) { |
1314 |
< |
f.hash = fh; |
1315 |
< |
synchronized (f) { f.notifyAll(); }; |
1316 |
< |
} |
1317 |
< |
} |
1318 |
< |
if (validated) { |
1319 |
< |
if (deleted) |
1320 |
< |
counter.add(-1L); |
1321 |
< |
break; |
684 |
> |
static Class<?> comparableClassFor(Object x) { |
685 |
> |
if (x instanceof Comparable) { |
686 |
> |
Class<?> c; Type[] ts, as; ParameterizedType p; |
687 |
> |
if ((c = x.getClass()) == String.class) // bypass checks |
688 |
> |
return c; |
689 |
> |
if ((ts = c.getGenericInterfaces()) != null) { |
690 |
> |
for (Type t : ts) { |
691 |
> |
if ((t instanceof ParameterizedType) && |
692 |
> |
((p = (ParameterizedType)t).getRawType() == |
693 |
> |
Comparable.class) && |
694 |
> |
(as = p.getActualTypeArguments()) != null && |
695 |
> |
as.length == 1 && as[0] == c) // type arg is c |
696 |
> |
return c; |
697 |
|
} |
698 |
|
} |
699 |
|
} |
1325 |
– |
return oldVal; |
1326 |
– |
} |
1327 |
– |
|
1328 |
– |
/* |
1329 |
– |
* Internal versions of the six insertion methods, each a |
1330 |
– |
* little more complicated than the last. All have |
1331 |
– |
* the same basic structure as the first (internalPut): |
1332 |
– |
* 1. If table uninitialized, create |
1333 |
– |
* 2. If bin empty, try to CAS new node |
1334 |
– |
* 3. If bin stale, use new table |
1335 |
– |
* 4. if bin converted to TreeBin, validate and relay to TreeBin methods |
1336 |
– |
* 5. Lock and validate; if valid, scan and add or update |
1337 |
– |
* |
1338 |
– |
* The others interweave other checks and/or alternative actions: |
1339 |
– |
* * Plain put checks for and performs resize after insertion. |
1340 |
– |
* * putIfAbsent prescans for mapping without lock (and fails to add |
1341 |
– |
* if present), which also makes pre-emptive resize checks worthwhile. |
1342 |
– |
* * computeIfAbsent extends form used in putIfAbsent with additional |
1343 |
– |
* mechanics to deal with, calls, potential exceptions and null |
1344 |
– |
* returns from function call. |
1345 |
– |
* * compute uses the same function-call mechanics, but without |
1346 |
– |
* the prescans |
1347 |
– |
* * merge acts as putIfAbsent in the absent case, but invokes the |
1348 |
– |
* update function if present |
1349 |
– |
* * putAll attempts to pre-allocate enough table space |
1350 |
– |
* and more lazily performs count updates and checks. |
1351 |
– |
* |
1352 |
– |
* Someday when details settle down a bit more, it might be worth |
1353 |
– |
* some factoring to reduce sprawl. |
1354 |
– |
*/ |
1355 |
– |
|
1356 |
– |
/** Implementation for put */ |
1357 |
– |
private final Object internalPut(Object k, Object v) { |
1358 |
– |
int h = spread(k.hashCode()); |
1359 |
– |
int count = 0; |
1360 |
– |
for (Node[] tab = table;;) { |
1361 |
– |
int i; Node f; int fh; Object fk; |
1362 |
– |
if (tab == null) |
1363 |
– |
tab = initTable(); |
1364 |
– |
else if ((f = tabAt(tab, i = (tab.length - 1) & h)) == null) { |
1365 |
– |
if (casTabAt(tab, i, null, new Node(h, k, v, null))) |
1366 |
– |
break; // no lock when adding to empty bin |
1367 |
– |
} |
1368 |
– |
else if ((fh = f.hash) == MOVED) { |
1369 |
– |
if ((fk = f.key) instanceof TreeBin) { |
1370 |
– |
TreeBin t = (TreeBin)fk; |
1371 |
– |
Object oldVal = null; |
1372 |
– |
t.acquire(0); |
1373 |
– |
try { |
1374 |
– |
if (tabAt(tab, i) == f) { |
1375 |
– |
count = 2; |
1376 |
– |
TreeNode p = t.putTreeNode(h, k, v); |
1377 |
– |
if (p != null) { |
1378 |
– |
oldVal = p.val; |
1379 |
– |
p.val = v; |
1380 |
– |
} |
1381 |
– |
} |
1382 |
– |
} finally { |
1383 |
– |
t.release(0); |
1384 |
– |
} |
1385 |
– |
if (count != 0) { |
1386 |
– |
if (oldVal != null) |
1387 |
– |
return oldVal; |
1388 |
– |
break; |
1389 |
– |
} |
1390 |
– |
} |
1391 |
– |
else |
1392 |
– |
tab = (Node[])fk; |
1393 |
– |
} |
1394 |
– |
else if ((fh & LOCKED) != 0) { |
1395 |
– |
checkForResize(); |
1396 |
– |
f.tryAwaitLock(tab, i); |
1397 |
– |
} |
1398 |
– |
else if (f.casHash(fh, fh | LOCKED)) { |
1399 |
– |
Object oldVal = null; |
1400 |
– |
try { // needed in case equals() throws |
1401 |
– |
if (tabAt(tab, i) == f) { |
1402 |
– |
count = 1; |
1403 |
– |
for (Node e = f;; ++count) { |
1404 |
– |
Object ek, ev; |
1405 |
– |
if ((e.hash & HASH_BITS) == h && |
1406 |
– |
(ev = e.val) != null && |
1407 |
– |
((ek = e.key) == k || k.equals(ek))) { |
1408 |
– |
oldVal = ev; |
1409 |
– |
e.val = v; |
1410 |
– |
break; |
1411 |
– |
} |
1412 |
– |
Node last = e; |
1413 |
– |
if ((e = e.next) == null) { |
1414 |
– |
last.next = new Node(h, k, v, null); |
1415 |
– |
if (count >= TREE_THRESHOLD) |
1416 |
– |
replaceWithTreeBin(tab, i, k); |
1417 |
– |
break; |
1418 |
– |
} |
1419 |
– |
} |
1420 |
– |
} |
1421 |
– |
} finally { // unlock and signal if needed |
1422 |
– |
if (!f.casHash(fh | LOCKED, fh)) { |
1423 |
– |
f.hash = fh; |
1424 |
– |
synchronized (f) { f.notifyAll(); }; |
1425 |
– |
} |
1426 |
– |
} |
1427 |
– |
if (count != 0) { |
1428 |
– |
if (oldVal != null) |
1429 |
– |
return oldVal; |
1430 |
– |
if (tab.length <= 64) |
1431 |
– |
count = 2; |
1432 |
– |
break; |
1433 |
– |
} |
1434 |
– |
} |
1435 |
– |
} |
1436 |
– |
counter.add(1L); |
1437 |
– |
if (count > 1) |
1438 |
– |
checkForResize(); |
1439 |
– |
return null; |
1440 |
– |
} |
1441 |
– |
|
1442 |
– |
/** Implementation for putIfAbsent */ |
1443 |
– |
private final Object internalPutIfAbsent(Object k, Object v) { |
1444 |
– |
int h = spread(k.hashCode()); |
1445 |
– |
int count = 0; |
1446 |
– |
for (Node[] tab = table;;) { |
1447 |
– |
int i; Node f; int fh; Object fk, fv; |
1448 |
– |
if (tab == null) |
1449 |
– |
tab = initTable(); |
1450 |
– |
else if ((f = tabAt(tab, i = (tab.length - 1) & h)) == null) { |
1451 |
– |
if (casTabAt(tab, i, null, new Node(h, k, v, null))) |
1452 |
– |
break; |
1453 |
– |
} |
1454 |
– |
else if ((fh = f.hash) == MOVED) { |
1455 |
– |
if ((fk = f.key) instanceof TreeBin) { |
1456 |
– |
TreeBin t = (TreeBin)fk; |
1457 |
– |
Object oldVal = null; |
1458 |
– |
t.acquire(0); |
1459 |
– |
try { |
1460 |
– |
if (tabAt(tab, i) == f) { |
1461 |
– |
count = 2; |
1462 |
– |
TreeNode p = t.putTreeNode(h, k, v); |
1463 |
– |
if (p != null) |
1464 |
– |
oldVal = p.val; |
1465 |
– |
} |
1466 |
– |
} finally { |
1467 |
– |
t.release(0); |
1468 |
– |
} |
1469 |
– |
if (count != 0) { |
1470 |
– |
if (oldVal != null) |
1471 |
– |
return oldVal; |
1472 |
– |
break; |
1473 |
– |
} |
1474 |
– |
} |
1475 |
– |
else |
1476 |
– |
tab = (Node[])fk; |
1477 |
– |
} |
1478 |
– |
else if ((fh & HASH_BITS) == h && (fv = f.val) != null && |
1479 |
– |
((fk = f.key) == k || k.equals(fk))) |
1480 |
– |
return fv; |
1481 |
– |
else { |
1482 |
– |
Node g = f.next; |
1483 |
– |
if (g != null) { // at least 2 nodes -- search and maybe resize |
1484 |
– |
for (Node e = g;;) { |
1485 |
– |
Object ek, ev; |
1486 |
– |
if ((e.hash & HASH_BITS) == h && (ev = e.val) != null && |
1487 |
– |
((ek = e.key) == k || k.equals(ek))) |
1488 |
– |
return ev; |
1489 |
– |
if ((e = e.next) == null) { |
1490 |
– |
checkForResize(); |
1491 |
– |
break; |
1492 |
– |
} |
1493 |
– |
} |
1494 |
– |
} |
1495 |
– |
if (((fh = f.hash) & LOCKED) != 0) { |
1496 |
– |
checkForResize(); |
1497 |
– |
f.tryAwaitLock(tab, i); |
1498 |
– |
} |
1499 |
– |
else if (tabAt(tab, i) == f && f.casHash(fh, fh | LOCKED)) { |
1500 |
– |
Object oldVal = null; |
1501 |
– |
try { |
1502 |
– |
if (tabAt(tab, i) == f) { |
1503 |
– |
count = 1; |
1504 |
– |
for (Node e = f;; ++count) { |
1505 |
– |
Object ek, ev; |
1506 |
– |
if ((e.hash & HASH_BITS) == h && |
1507 |
– |
(ev = e.val) != null && |
1508 |
– |
((ek = e.key) == k || k.equals(ek))) { |
1509 |
– |
oldVal = ev; |
1510 |
– |
break; |
1511 |
– |
} |
1512 |
– |
Node last = e; |
1513 |
– |
if ((e = e.next) == null) { |
1514 |
– |
last.next = new Node(h, k, v, null); |
1515 |
– |
if (count >= TREE_THRESHOLD) |
1516 |
– |
replaceWithTreeBin(tab, i, k); |
1517 |
– |
break; |
1518 |
– |
} |
1519 |
– |
} |
1520 |
– |
} |
1521 |
– |
} finally { |
1522 |
– |
if (!f.casHash(fh | LOCKED, fh)) { |
1523 |
– |
f.hash = fh; |
1524 |
– |
synchronized (f) { f.notifyAll(); }; |
1525 |
– |
} |
1526 |
– |
} |
1527 |
– |
if (count != 0) { |
1528 |
– |
if (oldVal != null) |
1529 |
– |
return oldVal; |
1530 |
– |
if (tab.length <= 64) |
1531 |
– |
count = 2; |
1532 |
– |
break; |
1533 |
– |
} |
1534 |
– |
} |
1535 |
– |
} |
1536 |
– |
} |
1537 |
– |
counter.add(1L); |
1538 |
– |
if (count > 1) |
1539 |
– |
checkForResize(); |
700 |
|
return null; |
701 |
|
} |
702 |
|
|
703 |
< |
/** Implementation for computeIfAbsent */ |
704 |
< |
private final Object internalComputeIfAbsent(K k, |
705 |
< |
Fun<? super K, ?> mf) { |
706 |
< |
int h = spread(k.hashCode()); |
707 |
< |
Object val = null; |
708 |
< |
int count = 0; |
709 |
< |
for (Node[] tab = table;;) { |
710 |
< |
Node f; int i, fh; Object fk, fv; |
1551 |
< |
if (tab == null) |
1552 |
< |
tab = initTable(); |
1553 |
< |
else if ((f = tabAt(tab, i = (tab.length - 1) & h)) == null) { |
1554 |
< |
Node node = new Node(fh = h | LOCKED, k, null, null); |
1555 |
< |
if (casTabAt(tab, i, null, node)) { |
1556 |
< |
count = 1; |
1557 |
< |
try { |
1558 |
< |
if ((val = mf.apply(k)) != null) |
1559 |
< |
node.val = val; |
1560 |
< |
} finally { |
1561 |
< |
if (val == null) |
1562 |
< |
setTabAt(tab, i, null); |
1563 |
< |
if (!node.casHash(fh, h)) { |
1564 |
< |
node.hash = h; |
1565 |
< |
synchronized (node) { node.notifyAll(); }; |
1566 |
< |
} |
1567 |
< |
} |
1568 |
< |
} |
1569 |
< |
if (count != 0) |
1570 |
< |
break; |
1571 |
< |
} |
1572 |
< |
else if ((fh = f.hash) == MOVED) { |
1573 |
< |
if ((fk = f.key) instanceof TreeBin) { |
1574 |
< |
TreeBin t = (TreeBin)fk; |
1575 |
< |
boolean added = false; |
1576 |
< |
t.acquire(0); |
1577 |
< |
try { |
1578 |
< |
if (tabAt(tab, i) == f) { |
1579 |
< |
count = 1; |
1580 |
< |
TreeNode p = t.getTreeNode(h, k, t.root); |
1581 |
< |
if (p != null) |
1582 |
< |
val = p.val; |
1583 |
< |
else if ((val = mf.apply(k)) != null) { |
1584 |
< |
added = true; |
1585 |
< |
count = 2; |
1586 |
< |
t.putTreeNode(h, k, val); |
1587 |
< |
} |
1588 |
< |
} |
1589 |
< |
} finally { |
1590 |
< |
t.release(0); |
1591 |
< |
} |
1592 |
< |
if (count != 0) { |
1593 |
< |
if (!added) |
1594 |
< |
return val; |
1595 |
< |
break; |
1596 |
< |
} |
1597 |
< |
} |
1598 |
< |
else |
1599 |
< |
tab = (Node[])fk; |
1600 |
< |
} |
1601 |
< |
else if ((fh & HASH_BITS) == h && (fv = f.val) != null && |
1602 |
< |
((fk = f.key) == k || k.equals(fk))) |
1603 |
< |
return fv; |
1604 |
< |
else { |
1605 |
< |
Node g = f.next; |
1606 |
< |
if (g != null) { |
1607 |
< |
for (Node e = g;;) { |
1608 |
< |
Object ek, ev; |
1609 |
< |
if ((e.hash & HASH_BITS) == h && (ev = e.val) != null && |
1610 |
< |
((ek = e.key) == k || k.equals(ek))) |
1611 |
< |
return ev; |
1612 |
< |
if ((e = e.next) == null) { |
1613 |
< |
checkForResize(); |
1614 |
< |
break; |
1615 |
< |
} |
1616 |
< |
} |
1617 |
< |
} |
1618 |
< |
if (((fh = f.hash) & LOCKED) != 0) { |
1619 |
< |
checkForResize(); |
1620 |
< |
f.tryAwaitLock(tab, i); |
1621 |
< |
} |
1622 |
< |
else if (tabAt(tab, i) == f && f.casHash(fh, fh | LOCKED)) { |
1623 |
< |
boolean added = false; |
1624 |
< |
try { |
1625 |
< |
if (tabAt(tab, i) == f) { |
1626 |
< |
count = 1; |
1627 |
< |
for (Node e = f;; ++count) { |
1628 |
< |
Object ek, ev; |
1629 |
< |
if ((e.hash & HASH_BITS) == h && |
1630 |
< |
(ev = e.val) != null && |
1631 |
< |
((ek = e.key) == k || k.equals(ek))) { |
1632 |
< |
val = ev; |
1633 |
< |
break; |
1634 |
< |
} |
1635 |
< |
Node last = e; |
1636 |
< |
if ((e = e.next) == null) { |
1637 |
< |
if ((val = mf.apply(k)) != null) { |
1638 |
< |
added = true; |
1639 |
< |
last.next = new Node(h, k, val, null); |
1640 |
< |
if (count >= TREE_THRESHOLD) |
1641 |
< |
replaceWithTreeBin(tab, i, k); |
1642 |
< |
} |
1643 |
< |
break; |
1644 |
< |
} |
1645 |
< |
} |
1646 |
< |
} |
1647 |
< |
} finally { |
1648 |
< |
if (!f.casHash(fh | LOCKED, fh)) { |
1649 |
< |
f.hash = fh; |
1650 |
< |
synchronized (f) { f.notifyAll(); }; |
1651 |
< |
} |
1652 |
< |
} |
1653 |
< |
if (count != 0) { |
1654 |
< |
if (!added) |
1655 |
< |
return val; |
1656 |
< |
if (tab.length <= 64) |
1657 |
< |
count = 2; |
1658 |
< |
break; |
1659 |
< |
} |
1660 |
< |
} |
1661 |
< |
} |
1662 |
< |
} |
1663 |
< |
if (val != null) { |
1664 |
< |
counter.add(1L); |
1665 |
< |
if (count > 1) |
1666 |
< |
checkForResize(); |
1667 |
< |
} |
1668 |
< |
return val; |
703 |
> |
/** |
704 |
> |
* Returns k.compareTo(x) if x matches kc (k's screened comparable |
705 |
> |
* class), else 0. |
706 |
> |
*/ |
707 |
> |
@SuppressWarnings({"rawtypes","unchecked"}) // for cast to Comparable |
708 |
> |
static int compareComparables(Class<?> kc, Object k, Object x) { |
709 |
> |
return (x == null || x.getClass() != kc ? 0 : |
710 |
> |
((Comparable)k).compareTo(x)); |
711 |
|
} |
712 |
|
|
713 |
< |
/** Implementation for compute */ |
1672 |
< |
@SuppressWarnings("unchecked") private final Object internalCompute |
1673 |
< |
(K k, boolean onlyIfPresent, BiFun<? super K, ? super V, ? extends V> mf) { |
1674 |
< |
int h = spread(k.hashCode()); |
1675 |
< |
Object val = null; |
1676 |
< |
int delta = 0; |
1677 |
< |
int count = 0; |
1678 |
< |
for (Node[] tab = table;;) { |
1679 |
< |
Node f; int i, fh; Object fk; |
1680 |
< |
if (tab == null) |
1681 |
< |
tab = initTable(); |
1682 |
< |
else if ((f = tabAt(tab, i = (tab.length - 1) & h)) == null) { |
1683 |
< |
if (onlyIfPresent) |
1684 |
< |
break; |
1685 |
< |
Node node = new Node(fh = h | LOCKED, k, null, null); |
1686 |
< |
if (casTabAt(tab, i, null, node)) { |
1687 |
< |
try { |
1688 |
< |
count = 1; |
1689 |
< |
if ((val = mf.apply(k, null)) != null) { |
1690 |
< |
node.val = val; |
1691 |
< |
delta = 1; |
1692 |
< |
} |
1693 |
< |
} finally { |
1694 |
< |
if (delta == 0) |
1695 |
< |
setTabAt(tab, i, null); |
1696 |
< |
if (!node.casHash(fh, h)) { |
1697 |
< |
node.hash = h; |
1698 |
< |
synchronized (node) { node.notifyAll(); }; |
1699 |
< |
} |
1700 |
< |
} |
1701 |
< |
} |
1702 |
< |
if (count != 0) |
1703 |
< |
break; |
1704 |
< |
} |
1705 |
< |
else if ((fh = f.hash) == MOVED) { |
1706 |
< |
if ((fk = f.key) instanceof TreeBin) { |
1707 |
< |
TreeBin t = (TreeBin)fk; |
1708 |
< |
t.acquire(0); |
1709 |
< |
try { |
1710 |
< |
if (tabAt(tab, i) == f) { |
1711 |
< |
count = 1; |
1712 |
< |
TreeNode p = t.getTreeNode(h, k, t.root); |
1713 |
< |
Object pv = (p == null) ? null : p.val; |
1714 |
< |
if ((val = mf.apply(k, (V)pv)) != null) { |
1715 |
< |
if (p != null) |
1716 |
< |
p.val = val; |
1717 |
< |
else { |
1718 |
< |
count = 2; |
1719 |
< |
delta = 1; |
1720 |
< |
t.putTreeNode(h, k, val); |
1721 |
< |
} |
1722 |
< |
} |
1723 |
< |
else if (p != null) { |
1724 |
< |
delta = -1; |
1725 |
< |
t.deleteTreeNode(p); |
1726 |
< |
} |
1727 |
< |
} |
1728 |
< |
} finally { |
1729 |
< |
t.release(0); |
1730 |
< |
} |
1731 |
< |
if (count != 0) |
1732 |
< |
break; |
1733 |
< |
} |
1734 |
< |
else |
1735 |
< |
tab = (Node[])fk; |
1736 |
< |
} |
1737 |
< |
else if ((fh & LOCKED) != 0) { |
1738 |
< |
checkForResize(); |
1739 |
< |
f.tryAwaitLock(tab, i); |
1740 |
< |
} |
1741 |
< |
else if (f.casHash(fh, fh | LOCKED)) { |
1742 |
< |
try { |
1743 |
< |
if (tabAt(tab, i) == f) { |
1744 |
< |
count = 1; |
1745 |
< |
for (Node e = f, pred = null;; ++count) { |
1746 |
< |
Object ek, ev; |
1747 |
< |
if ((e.hash & HASH_BITS) == h && |
1748 |
< |
(ev = e.val) != null && |
1749 |
< |
((ek = e.key) == k || k.equals(ek))) { |
1750 |
< |
val = mf.apply(k, (V)ev); |
1751 |
< |
if (val != null) |
1752 |
< |
e.val = val; |
1753 |
< |
else { |
1754 |
< |
delta = -1; |
1755 |
< |
Node en = e.next; |
1756 |
< |
if (pred != null) |
1757 |
< |
pred.next = en; |
1758 |
< |
else |
1759 |
< |
setTabAt(tab, i, en); |
1760 |
< |
} |
1761 |
< |
break; |
1762 |
< |
} |
1763 |
< |
pred = e; |
1764 |
< |
if ((e = e.next) == null) { |
1765 |
< |
if (!onlyIfPresent && (val = mf.apply(k, null)) != null) { |
1766 |
< |
pred.next = new Node(h, k, val, null); |
1767 |
< |
delta = 1; |
1768 |
< |
if (count >= TREE_THRESHOLD) |
1769 |
< |
replaceWithTreeBin(tab, i, k); |
1770 |
< |
} |
1771 |
< |
break; |
1772 |
< |
} |
1773 |
< |
} |
1774 |
< |
} |
1775 |
< |
} finally { |
1776 |
< |
if (!f.casHash(fh | LOCKED, fh)) { |
1777 |
< |
f.hash = fh; |
1778 |
< |
synchronized (f) { f.notifyAll(); }; |
1779 |
< |
} |
1780 |
< |
} |
1781 |
< |
if (count != 0) { |
1782 |
< |
if (tab.length <= 64) |
1783 |
< |
count = 2; |
1784 |
< |
break; |
1785 |
< |
} |
1786 |
< |
} |
1787 |
< |
} |
1788 |
< |
if (delta != 0) { |
1789 |
< |
counter.add((long)delta); |
1790 |
< |
if (count > 1) |
1791 |
< |
checkForResize(); |
1792 |
< |
} |
1793 |
< |
return val; |
1794 |
< |
} |
713 |
> |
/* ---------------- Table element access -------------- */ |
714 |
|
|
715 |
< |
/** Implementation for merge */ |
716 |
< |
@SuppressWarnings("unchecked") private final Object internalMerge |
717 |
< |
(K k, V v, BiFun<? super V, ? super V, ? extends V> mf) { |
718 |
< |
int h = spread(k.hashCode()); |
719 |
< |
Object val = null; |
720 |
< |
int delta = 0; |
721 |
< |
int count = 0; |
722 |
< |
for (Node[] tab = table;;) { |
723 |
< |
int i; Node f; int fh; Object fk, fv; |
724 |
< |
if (tab == null) |
725 |
< |
tab = initTable(); |
726 |
< |
else if ((f = tabAt(tab, i = (tab.length - 1) & h)) == null) { |
727 |
< |
if (casTabAt(tab, i, null, new Node(h, k, v, null))) { |
728 |
< |
delta = 1; |
729 |
< |
val = v; |
730 |
< |
break; |
731 |
< |
} |
732 |
< |
} |
733 |
< |
else if ((fh = f.hash) == MOVED) { |
734 |
< |
if ((fk = f.key) instanceof TreeBin) { |
735 |
< |
TreeBin t = (TreeBin)fk; |
736 |
< |
t.acquire(0); |
1818 |
< |
try { |
1819 |
< |
if (tabAt(tab, i) == f) { |
1820 |
< |
count = 1; |
1821 |
< |
TreeNode p = t.getTreeNode(h, k, t.root); |
1822 |
< |
val = (p == null) ? v : mf.apply((V)p.val, v); |
1823 |
< |
if (val != null) { |
1824 |
< |
if (p != null) |
1825 |
< |
p.val = val; |
1826 |
< |
else { |
1827 |
< |
count = 2; |
1828 |
< |
delta = 1; |
1829 |
< |
t.putTreeNode(h, k, val); |
1830 |
< |
} |
1831 |
< |
} |
1832 |
< |
else if (p != null) { |
1833 |
< |
delta = -1; |
1834 |
< |
t.deleteTreeNode(p); |
1835 |
< |
} |
1836 |
< |
} |
1837 |
< |
} finally { |
1838 |
< |
t.release(0); |
1839 |
< |
} |
1840 |
< |
if (count != 0) |
1841 |
< |
break; |
1842 |
< |
} |
1843 |
< |
else |
1844 |
< |
tab = (Node[])fk; |
1845 |
< |
} |
1846 |
< |
else if ((fh & LOCKED) != 0) { |
1847 |
< |
checkForResize(); |
1848 |
< |
f.tryAwaitLock(tab, i); |
1849 |
< |
} |
1850 |
< |
else if (f.casHash(fh, fh | LOCKED)) { |
1851 |
< |
try { |
1852 |
< |
if (tabAt(tab, i) == f) { |
1853 |
< |
count = 1; |
1854 |
< |
for (Node e = f, pred = null;; ++count) { |
1855 |
< |
Object ek, ev; |
1856 |
< |
if ((e.hash & HASH_BITS) == h && |
1857 |
< |
(ev = e.val) != null && |
1858 |
< |
((ek = e.key) == k || k.equals(ek))) { |
1859 |
< |
val = mf.apply(v, (V)ev); |
1860 |
< |
if (val != null) |
1861 |
< |
e.val = val; |
1862 |
< |
else { |
1863 |
< |
delta = -1; |
1864 |
< |
Node en = e.next; |
1865 |
< |
if (pred != null) |
1866 |
< |
pred.next = en; |
1867 |
< |
else |
1868 |
< |
setTabAt(tab, i, en); |
1869 |
< |
} |
1870 |
< |
break; |
1871 |
< |
} |
1872 |
< |
pred = e; |
1873 |
< |
if ((e = e.next) == null) { |
1874 |
< |
val = v; |
1875 |
< |
pred.next = new Node(h, k, val, null); |
1876 |
< |
delta = 1; |
1877 |
< |
if (count >= TREE_THRESHOLD) |
1878 |
< |
replaceWithTreeBin(tab, i, k); |
1879 |
< |
break; |
1880 |
< |
} |
1881 |
< |
} |
1882 |
< |
} |
1883 |
< |
} finally { |
1884 |
< |
if (!f.casHash(fh | LOCKED, fh)) { |
1885 |
< |
f.hash = fh; |
1886 |
< |
synchronized (f) { f.notifyAll(); }; |
1887 |
< |
} |
1888 |
< |
} |
1889 |
< |
if (count != 0) { |
1890 |
< |
if (tab.length <= 64) |
1891 |
< |
count = 2; |
1892 |
< |
break; |
1893 |
< |
} |
1894 |
< |
} |
1895 |
< |
} |
1896 |
< |
if (delta != 0) { |
1897 |
< |
counter.add((long)delta); |
1898 |
< |
if (count > 1) |
1899 |
< |
checkForResize(); |
1900 |
< |
} |
1901 |
< |
return val; |
715 |
> |
/* |
716 |
> |
* Atomic access methods are used for table elements as well as |
717 |
> |
* elements of in-progress next table while resizing. All uses of |
718 |
> |
* the tab arguments must be null checked by callers. All callers |
719 |
> |
* also paranoically precheck that tab's length is not zero (or an |
720 |
> |
* equivalent check), thus ensuring that any index argument taking |
721 |
> |
* the form of a hash value anded with (length - 1) is a valid |
722 |
> |
* index. Note that, to be correct wrt arbitrary concurrency |
723 |
> |
* errors by users, these checks must operate on local variables, |
724 |
> |
* which accounts for some odd-looking inline assignments below. |
725 |
> |
* Note that calls to setTabAt always occur within locked regions, |
726 |
> |
* and so require only release ordering. |
727 |
> |
*/ |
728 |
> |
|
729 |
> |
@SuppressWarnings("unchecked") |
730 |
> |
static final <K,V> Node<K,V> tabAt(Node<K,V>[] tab, int i) { |
731 |
> |
return (Node<K,V>)U.getObjectAcquire(tab, ((long)i << ASHIFT) + ABASE); |
732 |
> |
} |
733 |
> |
|
734 |
> |
static final <K,V> boolean casTabAt(Node<K,V>[] tab, int i, |
735 |
> |
Node<K,V> c, Node<K,V> v) { |
736 |
> |
return U.compareAndSetObject(tab, ((long)i << ASHIFT) + ABASE, c, v); |
737 |
|
} |
738 |
|
|
739 |
< |
/** Implementation for putAll */ |
740 |
< |
private final void internalPutAll(Map<?, ?> m) { |
1906 |
< |
tryPresize(m.size()); |
1907 |
< |
long delta = 0L; // number of uncommitted additions |
1908 |
< |
boolean npe = false; // to throw exception on exit for nulls |
1909 |
< |
try { // to clean up counts on other exceptions |
1910 |
< |
for (Map.Entry<?, ?> entry : m.entrySet()) { |
1911 |
< |
Object k, v; |
1912 |
< |
if (entry == null || (k = entry.getKey()) == null || |
1913 |
< |
(v = entry.getValue()) == null) { |
1914 |
< |
npe = true; |
1915 |
< |
break; |
1916 |
< |
} |
1917 |
< |
int h = spread(k.hashCode()); |
1918 |
< |
for (Node[] tab = table;;) { |
1919 |
< |
int i; Node f; int fh; Object fk; |
1920 |
< |
if (tab == null) |
1921 |
< |
tab = initTable(); |
1922 |
< |
else if ((f = tabAt(tab, i = (tab.length - 1) & h)) == null){ |
1923 |
< |
if (casTabAt(tab, i, null, new Node(h, k, v, null))) { |
1924 |
< |
++delta; |
1925 |
< |
break; |
1926 |
< |
} |
1927 |
< |
} |
1928 |
< |
else if ((fh = f.hash) == MOVED) { |
1929 |
< |
if ((fk = f.key) instanceof TreeBin) { |
1930 |
< |
TreeBin t = (TreeBin)fk; |
1931 |
< |
boolean validated = false; |
1932 |
< |
t.acquire(0); |
1933 |
< |
try { |
1934 |
< |
if (tabAt(tab, i) == f) { |
1935 |
< |
validated = true; |
1936 |
< |
TreeNode p = t.getTreeNode(h, k, t.root); |
1937 |
< |
if (p != null) |
1938 |
< |
p.val = v; |
1939 |
< |
else { |
1940 |
< |
t.putTreeNode(h, k, v); |
1941 |
< |
++delta; |
1942 |
< |
} |
1943 |
< |
} |
1944 |
< |
} finally { |
1945 |
< |
t.release(0); |
1946 |
< |
} |
1947 |
< |
if (validated) |
1948 |
< |
break; |
1949 |
< |
} |
1950 |
< |
else |
1951 |
< |
tab = (Node[])fk; |
1952 |
< |
} |
1953 |
< |
else if ((fh & LOCKED) != 0) { |
1954 |
< |
counter.add(delta); |
1955 |
< |
delta = 0L; |
1956 |
< |
checkForResize(); |
1957 |
< |
f.tryAwaitLock(tab, i); |
1958 |
< |
} |
1959 |
< |
else if (f.casHash(fh, fh | LOCKED)) { |
1960 |
< |
int count = 0; |
1961 |
< |
try { |
1962 |
< |
if (tabAt(tab, i) == f) { |
1963 |
< |
count = 1; |
1964 |
< |
for (Node e = f;; ++count) { |
1965 |
< |
Object ek, ev; |
1966 |
< |
if ((e.hash & HASH_BITS) == h && |
1967 |
< |
(ev = e.val) != null && |
1968 |
< |
((ek = e.key) == k || k.equals(ek))) { |
1969 |
< |
e.val = v; |
1970 |
< |
break; |
1971 |
< |
} |
1972 |
< |
Node last = e; |
1973 |
< |
if ((e = e.next) == null) { |
1974 |
< |
++delta; |
1975 |
< |
last.next = new Node(h, k, v, null); |
1976 |
< |
if (count >= TREE_THRESHOLD) |
1977 |
< |
replaceWithTreeBin(tab, i, k); |
1978 |
< |
break; |
1979 |
< |
} |
1980 |
< |
} |
1981 |
< |
} |
1982 |
< |
} finally { |
1983 |
< |
if (!f.casHash(fh | LOCKED, fh)) { |
1984 |
< |
f.hash = fh; |
1985 |
< |
synchronized (f) { f.notifyAll(); }; |
1986 |
< |
} |
1987 |
< |
} |
1988 |
< |
if (count != 0) { |
1989 |
< |
if (count > 1) { |
1990 |
< |
counter.add(delta); |
1991 |
< |
delta = 0L; |
1992 |
< |
checkForResize(); |
1993 |
< |
} |
1994 |
< |
break; |
1995 |
< |
} |
1996 |
< |
} |
1997 |
< |
} |
1998 |
< |
} |
1999 |
< |
} finally { |
2000 |
< |
if (delta != 0) |
2001 |
< |
counter.add(delta); |
2002 |
< |
} |
2003 |
< |
if (npe) |
2004 |
< |
throw new NullPointerException(); |
739 |
> |
static final <K,V> void setTabAt(Node<K,V>[] tab, int i, Node<K,V> v) { |
740 |
> |
U.putObjectRelease(tab, ((long)i << ASHIFT) + ABASE, v); |
741 |
|
} |
742 |
|
|
743 |
< |
/* ---------------- Table Initialization and Resizing -------------- */ |
743 |
> |
/* ---------------- Fields -------------- */ |
744 |
|
|
745 |
|
/** |
746 |
< |
* Returns a power of two table size for the given desired capacity. |
747 |
< |
* See Hackers Delight, sec 3.2 |
746 |
> |
* The array of bins. Lazily initialized upon first insertion. |
747 |
> |
* Size is always a power of two. Accessed directly by iterators. |
748 |
|
*/ |
749 |
< |
private static final int tableSizeFor(int c) { |
2014 |
< |
int n = c - 1; |
2015 |
< |
n |= n >>> 1; |
2016 |
< |
n |= n >>> 2; |
2017 |
< |
n |= n >>> 4; |
2018 |
< |
n |= n >>> 8; |
2019 |
< |
n |= n >>> 16; |
2020 |
< |
return (n < 0) ? 1 : (n >= MAXIMUM_CAPACITY) ? MAXIMUM_CAPACITY : n + 1; |
2021 |
< |
} |
749 |
> |
transient volatile Node<K,V>[] table; |
750 |
|
|
751 |
|
/** |
752 |
< |
* Initializes table, using the size recorded in sizeCtl. |
752 |
> |
* The next table to use; non-null only while resizing. |
753 |
|
*/ |
754 |
< |
private final Node[] initTable() { |
2027 |
< |
Node[] tab; int sc; |
2028 |
< |
while ((tab = table) == null) { |
2029 |
< |
if ((sc = sizeCtl) < 0) |
2030 |
< |
Thread.yield(); // lost initialization race; just spin |
2031 |
< |
else if (UNSAFE.compareAndSwapInt(this, sizeCtlOffset, sc, -1)) { |
2032 |
< |
try { |
2033 |
< |
if ((tab = table) == null) { |
2034 |
< |
int n = (sc > 0) ? sc : DEFAULT_CAPACITY; |
2035 |
< |
tab = table = new Node[n]; |
2036 |
< |
sc = n - (n >>> 2); |
2037 |
< |
} |
2038 |
< |
} finally { |
2039 |
< |
sizeCtl = sc; |
2040 |
< |
} |
2041 |
< |
break; |
2042 |
< |
} |
2043 |
< |
} |
2044 |
< |
return tab; |
2045 |
< |
} |
2046 |
< |
|
2047 |
< |
/** |
2048 |
< |
* If table is too small and not already resizing, creates next |
2049 |
< |
* table and transfers bins. Rechecks occupancy after a transfer |
2050 |
< |
* to see if another resize is already needed because resizings |
2051 |
< |
* are lagging additions. |
2052 |
< |
*/ |
2053 |
< |
private final void checkForResize() { |
2054 |
< |
Node[] tab; int n, sc; |
2055 |
< |
while ((tab = table) != null && |
2056 |
< |
(n = tab.length) < MAXIMUM_CAPACITY && |
2057 |
< |
(sc = sizeCtl) >= 0 && counter.sum() >= (long)sc && |
2058 |
< |
UNSAFE.compareAndSwapInt(this, sizeCtlOffset, sc, -1)) { |
2059 |
< |
try { |
2060 |
< |
if (tab == table) { |
2061 |
< |
table = rebuild(tab); |
2062 |
< |
sc = (n << 1) - (n >>> 1); |
2063 |
< |
} |
2064 |
< |
} finally { |
2065 |
< |
sizeCtl = sc; |
2066 |
< |
} |
2067 |
< |
} |
2068 |
< |
} |
754 |
> |
private transient volatile Node<K,V>[] nextTable; |
755 |
|
|
756 |
|
/** |
757 |
< |
* Tries to presize table to accommodate the given number of elements. |
758 |
< |
* |
759 |
< |
* @param size number of elements (doesn't need to be perfectly accurate) |
757 |
> |
* Base counter value, used mainly when there is no contention, |
758 |
> |
* but also as a fallback during table initialization |
759 |
> |
* races. Updated via CAS. |
760 |
|
*/ |
761 |
< |
private final void tryPresize(int size) { |
2076 |
< |
int c = (size >= (MAXIMUM_CAPACITY >>> 1)) ? MAXIMUM_CAPACITY : |
2077 |
< |
tableSizeFor(size + (size >>> 1) + 1); |
2078 |
< |
int sc; |
2079 |
< |
while ((sc = sizeCtl) >= 0) { |
2080 |
< |
Node[] tab = table; int n; |
2081 |
< |
if (tab == null || (n = tab.length) == 0) { |
2082 |
< |
n = (sc > c) ? sc : c; |
2083 |
< |
if (UNSAFE.compareAndSwapInt(this, sizeCtlOffset, sc, -1)) { |
2084 |
< |
try { |
2085 |
< |
if (table == tab) { |
2086 |
< |
table = new Node[n]; |
2087 |
< |
sc = n - (n >>> 2); |
2088 |
< |
} |
2089 |
< |
} finally { |
2090 |
< |
sizeCtl = sc; |
2091 |
< |
} |
2092 |
< |
} |
2093 |
< |
} |
2094 |
< |
else if (c <= sc || n >= MAXIMUM_CAPACITY) |
2095 |
< |
break; |
2096 |
< |
else if (UNSAFE.compareAndSwapInt(this, sizeCtlOffset, sc, -1)) { |
2097 |
< |
try { |
2098 |
< |
if (table == tab) { |
2099 |
< |
table = rebuild(tab); |
2100 |
< |
sc = (n << 1) - (n >>> 1); |
2101 |
< |
} |
2102 |
< |
} finally { |
2103 |
< |
sizeCtl = sc; |
2104 |
< |
} |
2105 |
< |
} |
2106 |
< |
} |
2107 |
< |
} |
2108 |
< |
|
2109 |
< |
/* |
2110 |
< |
* Moves and/or copies the nodes in each bin to new table. See |
2111 |
< |
* above for explanation. |
2112 |
< |
* |
2113 |
< |
* @return the new table |
2114 |
< |
*/ |
2115 |
< |
private static final Node[] rebuild(Node[] tab) { |
2116 |
< |
int n = tab.length; |
2117 |
< |
Node[] nextTab = new Node[n << 1]; |
2118 |
< |
Node fwd = new Node(MOVED, nextTab, null, null); |
2119 |
< |
int[] buffer = null; // holds bins to revisit; null until needed |
2120 |
< |
Node rev = null; // reverse forwarder; null until needed |
2121 |
< |
int nbuffered = 0; // the number of bins in buffer list |
2122 |
< |
int bufferIndex = 0; // buffer index of current buffered bin |
2123 |
< |
int bin = n - 1; // current non-buffered bin or -1 if none |
2124 |
< |
|
2125 |
< |
for (int i = bin;;) { // start upwards sweep |
2126 |
< |
int fh; Node f; |
2127 |
< |
if ((f = tabAt(tab, i)) == null) { |
2128 |
< |
if (bin >= 0) { // Unbuffered; no lock needed (or available) |
2129 |
< |
if (!casTabAt(tab, i, f, fwd)) |
2130 |
< |
continue; |
2131 |
< |
} |
2132 |
< |
else { // transiently use a locked forwarding node |
2133 |
< |
Node g = new Node(MOVED|LOCKED, nextTab, null, null); |
2134 |
< |
if (!casTabAt(tab, i, f, g)) |
2135 |
< |
continue; |
2136 |
< |
setTabAt(nextTab, i, null); |
2137 |
< |
setTabAt(nextTab, i + n, null); |
2138 |
< |
setTabAt(tab, i, fwd); |
2139 |
< |
if (!g.casHash(MOVED|LOCKED, MOVED)) { |
2140 |
< |
g.hash = MOVED; |
2141 |
< |
synchronized (g) { g.notifyAll(); } |
2142 |
< |
} |
2143 |
< |
} |
2144 |
< |
} |
2145 |
< |
else if ((fh = f.hash) == MOVED) { |
2146 |
< |
Object fk = f.key; |
2147 |
< |
if (fk instanceof TreeBin) { |
2148 |
< |
TreeBin t = (TreeBin)fk; |
2149 |
< |
boolean validated = false; |
2150 |
< |
t.acquire(0); |
2151 |
< |
try { |
2152 |
< |
if (tabAt(tab, i) == f) { |
2153 |
< |
validated = true; |
2154 |
< |
splitTreeBin(nextTab, i, t); |
2155 |
< |
setTabAt(tab, i, fwd); |
2156 |
< |
} |
2157 |
< |
} finally { |
2158 |
< |
t.release(0); |
2159 |
< |
} |
2160 |
< |
if (!validated) |
2161 |
< |
continue; |
2162 |
< |
} |
2163 |
< |
} |
2164 |
< |
else if ((fh & LOCKED) == 0 && f.casHash(fh, fh|LOCKED)) { |
2165 |
< |
boolean validated = false; |
2166 |
< |
try { // split to lo and hi lists; copying as needed |
2167 |
< |
if (tabAt(tab, i) == f) { |
2168 |
< |
validated = true; |
2169 |
< |
splitBin(nextTab, i, f); |
2170 |
< |
setTabAt(tab, i, fwd); |
2171 |
< |
} |
2172 |
< |
} finally { |
2173 |
< |
if (!f.casHash(fh | LOCKED, fh)) { |
2174 |
< |
f.hash = fh; |
2175 |
< |
synchronized (f) { f.notifyAll(); }; |
2176 |
< |
} |
2177 |
< |
} |
2178 |
< |
if (!validated) |
2179 |
< |
continue; |
2180 |
< |
} |
2181 |
< |
else { |
2182 |
< |
if (buffer == null) // initialize buffer for revisits |
2183 |
< |
buffer = new int[TRANSFER_BUFFER_SIZE]; |
2184 |
< |
if (bin < 0 && bufferIndex > 0) { |
2185 |
< |
int j = buffer[--bufferIndex]; |
2186 |
< |
buffer[bufferIndex] = i; |
2187 |
< |
i = j; // swap with another bin |
2188 |
< |
continue; |
2189 |
< |
} |
2190 |
< |
if (bin < 0 || nbuffered >= TRANSFER_BUFFER_SIZE) { |
2191 |
< |
f.tryAwaitLock(tab, i); |
2192 |
< |
continue; // no other options -- block |
2193 |
< |
} |
2194 |
< |
if (rev == null) // initialize reverse-forwarder |
2195 |
< |
rev = new Node(MOVED, tab, null, null); |
2196 |
< |
if (tabAt(tab, i) != f || (f.hash & LOCKED) == 0) |
2197 |
< |
continue; // recheck before adding to list |
2198 |
< |
buffer[nbuffered++] = i; |
2199 |
< |
setTabAt(nextTab, i, rev); // install place-holders |
2200 |
< |
setTabAt(nextTab, i + n, rev); |
2201 |
< |
} |
2202 |
< |
|
2203 |
< |
if (bin > 0) |
2204 |
< |
i = --bin; |
2205 |
< |
else if (buffer != null && nbuffered > 0) { |
2206 |
< |
bin = -1; |
2207 |
< |
i = buffer[bufferIndex = --nbuffered]; |
2208 |
< |
} |
2209 |
< |
else |
2210 |
< |
return nextTab; |
2211 |
< |
} |
2212 |
< |
} |
761 |
> |
private transient volatile long baseCount; |
762 |
|
|
763 |
|
/** |
764 |
< |
* Splits a normal bin with list headed by e into lo and hi parts; |
765 |
< |
* installs in given table. |
764 |
> |
* Table initialization and resizing control. When negative, the |
765 |
> |
* table is being initialized or resized: -1 for initialization, |
766 |
> |
* else -(1 + the number of active resizing threads). Otherwise, |
767 |
> |
* when table is null, holds the initial table size to use upon |
768 |
> |
* creation, or 0 for default. After initialization, holds the |
769 |
> |
* next element count value upon which to resize the table. |
770 |
|
*/ |
771 |
< |
private static void splitBin(Node[] nextTab, int i, Node e) { |
2219 |
< |
int bit = nextTab.length >>> 1; // bit to split on |
2220 |
< |
int runBit = e.hash & bit; |
2221 |
< |
Node lastRun = e, lo = null, hi = null; |
2222 |
< |
for (Node p = e.next; p != null; p = p.next) { |
2223 |
< |
int b = p.hash & bit; |
2224 |
< |
if (b != runBit) { |
2225 |
< |
runBit = b; |
2226 |
< |
lastRun = p; |
2227 |
< |
} |
2228 |
< |
} |
2229 |
< |
if (runBit == 0) |
2230 |
< |
lo = lastRun; |
2231 |
< |
else |
2232 |
< |
hi = lastRun; |
2233 |
< |
for (Node p = e; p != lastRun; p = p.next) { |
2234 |
< |
int ph = p.hash & HASH_BITS; |
2235 |
< |
Object pk = p.key, pv = p.val; |
2236 |
< |
if ((ph & bit) == 0) |
2237 |
< |
lo = new Node(ph, pk, pv, lo); |
2238 |
< |
else |
2239 |
< |
hi = new Node(ph, pk, pv, hi); |
2240 |
< |
} |
2241 |
< |
setTabAt(nextTab, i, lo); |
2242 |
< |
setTabAt(nextTab, i + bit, hi); |
2243 |
< |
} |
771 |
> |
private transient volatile int sizeCtl; |
772 |
|
|
773 |
|
/** |
774 |
< |
* Splits a tree bin into lo and hi parts; installs in given table. |
774 |
> |
* The next table index (plus one) to split while resizing. |
775 |
|
*/ |
776 |
< |
private static void splitTreeBin(Node[] nextTab, int i, TreeBin t) { |
2249 |
< |
int bit = nextTab.length >>> 1; |
2250 |
< |
TreeBin lt = new TreeBin(); |
2251 |
< |
TreeBin ht = new TreeBin(); |
2252 |
< |
int lc = 0, hc = 0; |
2253 |
< |
for (Node e = t.first; e != null; e = e.next) { |
2254 |
< |
int h = e.hash & HASH_BITS; |
2255 |
< |
Object k = e.key, v = e.val; |
2256 |
< |
if ((h & bit) == 0) { |
2257 |
< |
++lc; |
2258 |
< |
lt.putTreeNode(h, k, v); |
2259 |
< |
} |
2260 |
< |
else { |
2261 |
< |
++hc; |
2262 |
< |
ht.putTreeNode(h, k, v); |
2263 |
< |
} |
2264 |
< |
} |
2265 |
< |
Node ln, hn; // throw away trees if too small |
2266 |
< |
if (lc <= (TREE_THRESHOLD >>> 1)) { |
2267 |
< |
ln = null; |
2268 |
< |
for (Node p = lt.first; p != null; p = p.next) |
2269 |
< |
ln = new Node(p.hash, p.key, p.val, ln); |
2270 |
< |
} |
2271 |
< |
else |
2272 |
< |
ln = new Node(MOVED, lt, null, null); |
2273 |
< |
setTabAt(nextTab, i, ln); |
2274 |
< |
if (hc <= (TREE_THRESHOLD >>> 1)) { |
2275 |
< |
hn = null; |
2276 |
< |
for (Node p = ht.first; p != null; p = p.next) |
2277 |
< |
hn = new Node(p.hash, p.key, p.val, hn); |
2278 |
< |
} |
2279 |
< |
else |
2280 |
< |
hn = new Node(MOVED, ht, null, null); |
2281 |
< |
setTabAt(nextTab, i + bit, hn); |
2282 |
< |
} |
776 |
> |
private transient volatile int transferIndex; |
777 |
|
|
778 |
|
/** |
779 |
< |
* Implementation for clear. Steps through each bin, removing all |
2286 |
< |
* nodes. |
779 |
> |
* Spinlock (locked via CAS) used when resizing and/or creating CounterCells. |
780 |
|
*/ |
781 |
< |
private final void internalClear() { |
2289 |
< |
long delta = 0L; // negative number of deletions |
2290 |
< |
int i = 0; |
2291 |
< |
Node[] tab = table; |
2292 |
< |
while (tab != null && i < tab.length) { |
2293 |
< |
int fh; Object fk; |
2294 |
< |
Node f = tabAt(tab, i); |
2295 |
< |
if (f == null) |
2296 |
< |
++i; |
2297 |
< |
else if ((fh = f.hash) == MOVED) { |
2298 |
< |
if ((fk = f.key) instanceof TreeBin) { |
2299 |
< |
TreeBin t = (TreeBin)fk; |
2300 |
< |
t.acquire(0); |
2301 |
< |
try { |
2302 |
< |
if (tabAt(tab, i) == f) { |
2303 |
< |
for (Node p = t.first; p != null; p = p.next) { |
2304 |
< |
if (p.val != null) { // (currently always true) |
2305 |
< |
p.val = null; |
2306 |
< |
--delta; |
2307 |
< |
} |
2308 |
< |
} |
2309 |
< |
t.first = null; |
2310 |
< |
t.root = null; |
2311 |
< |
++i; |
2312 |
< |
} |
2313 |
< |
} finally { |
2314 |
< |
t.release(0); |
2315 |
< |
} |
2316 |
< |
} |
2317 |
< |
else |
2318 |
< |
tab = (Node[])fk; |
2319 |
< |
} |
2320 |
< |
else if ((fh & LOCKED) != 0) { |
2321 |
< |
counter.add(delta); // opportunistically update count |
2322 |
< |
delta = 0L; |
2323 |
< |
f.tryAwaitLock(tab, i); |
2324 |
< |
} |
2325 |
< |
else if (f.casHash(fh, fh | LOCKED)) { |
2326 |
< |
try { |
2327 |
< |
if (tabAt(tab, i) == f) { |
2328 |
< |
for (Node e = f; e != null; e = e.next) { |
2329 |
< |
if (e.val != null) { // (currently always true) |
2330 |
< |
e.val = null; |
2331 |
< |
--delta; |
2332 |
< |
} |
2333 |
< |
} |
2334 |
< |
setTabAt(tab, i, null); |
2335 |
< |
++i; |
2336 |
< |
} |
2337 |
< |
} finally { |
2338 |
< |
if (!f.casHash(fh | LOCKED, fh)) { |
2339 |
< |
f.hash = fh; |
2340 |
< |
synchronized (f) { f.notifyAll(); }; |
2341 |
< |
} |
2342 |
< |
} |
2343 |
< |
} |
2344 |
< |
} |
2345 |
< |
if (delta != 0) |
2346 |
< |
counter.add(delta); |
2347 |
< |
} |
2348 |
< |
|
2349 |
< |
/* ----------------Table Traversal -------------- */ |
781 |
> |
private transient volatile int cellsBusy; |
782 |
|
|
783 |
|
/** |
784 |
< |
* Encapsulates traversal for methods such as containsValue; also |
785 |
< |
* serves as a base class for other iterators and bulk tasks. |
786 |
< |
* |
2355 |
< |
* At each step, the iterator snapshots the key ("nextKey") and |
2356 |
< |
* value ("nextVal") of a valid node (i.e., one that, at point of |
2357 |
< |
* snapshot, has a non-null user value). Because val fields can |
2358 |
< |
* change (including to null, indicating deletion), field nextVal |
2359 |
< |
* might not be accurate at point of use, but still maintains the |
2360 |
< |
* weak consistency property of holding a value that was once |
2361 |
< |
* valid. To support iterator.remove, the nextKey field is not |
2362 |
< |
* updated (nulled out) when the iterator cannot advance. |
2363 |
< |
* |
2364 |
< |
* Internal traversals directly access these fields, as in: |
2365 |
< |
* {@code while (it.advance() != null) { process(it.nextKey); }} |
2366 |
< |
* |
2367 |
< |
* Exported iterators must track whether the iterator has advanced |
2368 |
< |
* (in hasNext vs next) (by setting/checking/nulling field |
2369 |
< |
* nextVal), and then extract key, value, or key-value pairs as |
2370 |
< |
* return values of next(). |
2371 |
< |
* |
2372 |
< |
* The iterator visits once each still-valid node that was |
2373 |
< |
* reachable upon iterator construction. It might miss some that |
2374 |
< |
* were added to a bin after the bin was visited, which is OK wrt |
2375 |
< |
* consistency guarantees. Maintaining this property in the face |
2376 |
< |
* of possible ongoing resizes requires a fair amount of |
2377 |
< |
* bookkeeping state that is difficult to optimize away amidst |
2378 |
< |
* volatile accesses. Even so, traversal maintains reasonable |
2379 |
< |
* throughput. |
2380 |
< |
* |
2381 |
< |
* Normally, iteration proceeds bin-by-bin traversing lists. |
2382 |
< |
* However, if the table has been resized, then all future steps |
2383 |
< |
* must traverse both the bin at the current index as well as at |
2384 |
< |
* (index + baseSize); and so on for further resizings. To |
2385 |
< |
* paranoically cope with potential sharing by users of iterators |
2386 |
< |
* across threads, iteration terminates if a bounds checks fails |
2387 |
< |
* for a table read. |
2388 |
< |
* |
2389 |
< |
* This class extends ForkJoinTask to streamline parallel |
2390 |
< |
* iteration in bulk operations (see BulkTask). This adds only an |
2391 |
< |
* int of space overhead, which is close enough to negligible in |
2392 |
< |
* cases where it is not needed to not worry about it. Because |
2393 |
< |
* ForkJoinTask is Serializable, but iterators need not be, we |
2394 |
< |
* need to add warning suppressions. |
2395 |
< |
*/ |
2396 |
< |
@SuppressWarnings("serial") static class Traverser<K,V,R> extends ForkJoinTask<R> { |
2397 |
< |
final ConcurrentHashMap<K, V> map; |
2398 |
< |
Node next; // the next entry to use |
2399 |
< |
Object nextKey; // cached key field of next |
2400 |
< |
Object nextVal; // cached val field of next |
2401 |
< |
Node[] tab; // current table; updated if resized |
2402 |
< |
int index; // index of bin to use next |
2403 |
< |
int baseIndex; // current index of initial table |
2404 |
< |
int baseLimit; // index bound for initial table |
2405 |
< |
int baseSize; // initial table size |
2406 |
< |
|
2407 |
< |
/** Creates iterator for all entries in the table. */ |
2408 |
< |
Traverser(ConcurrentHashMap<K, V> map) { |
2409 |
< |
this.map = map; |
2410 |
< |
} |
2411 |
< |
|
2412 |
< |
/** Creates iterator for split() methods */ |
2413 |
< |
Traverser(Traverser<K,V,?> it) { |
2414 |
< |
ConcurrentHashMap<K, V> m; Node[] t; |
2415 |
< |
if ((m = this.map = it.map) == null) |
2416 |
< |
t = null; |
2417 |
< |
else if ((t = it.tab) == null && // force parent tab initialization |
2418 |
< |
(t = it.tab = m.table) != null) |
2419 |
< |
it.baseLimit = it.baseSize = t.length; |
2420 |
< |
this.tab = t; |
2421 |
< |
this.baseSize = it.baseSize; |
2422 |
< |
it.baseLimit = this.index = this.baseIndex = |
2423 |
< |
((this.baseLimit = it.baseLimit) + it.baseIndex + 1) >>> 1; |
2424 |
< |
} |
2425 |
< |
|
2426 |
< |
/** |
2427 |
< |
* Advances next; returns nextVal or null if terminated. |
2428 |
< |
* See above for explanation. |
2429 |
< |
*/ |
2430 |
< |
final Object advance() { |
2431 |
< |
Node e = next; |
2432 |
< |
Object ev = null; |
2433 |
< |
outer: do { |
2434 |
< |
if (e != null) // advance past used/skipped node |
2435 |
< |
e = e.next; |
2436 |
< |
while (e == null) { // get to next non-null bin |
2437 |
< |
ConcurrentHashMap<K, V> m; |
2438 |
< |
Node[] t; int b, i, n; Object ek; // checks must use locals |
2439 |
< |
if ((t = tab) != null) |
2440 |
< |
n = t.length; |
2441 |
< |
else if ((m = map) != null && (t = tab = m.table) != null) |
2442 |
< |
n = baseLimit = baseSize = t.length; |
2443 |
< |
else |
2444 |
< |
break outer; |
2445 |
< |
if ((b = baseIndex) >= baseLimit || |
2446 |
< |
(i = index) < 0 || i >= n) |
2447 |
< |
break outer; |
2448 |
< |
if ((e = tabAt(t, i)) != null && e.hash == MOVED) { |
2449 |
< |
if ((ek = e.key) instanceof TreeBin) |
2450 |
< |
e = ((TreeBin)ek).first; |
2451 |
< |
else { |
2452 |
< |
tab = (Node[])ek; |
2453 |
< |
continue; // restarts due to null val |
2454 |
< |
} |
2455 |
< |
} // visit upper slots if present |
2456 |
< |
index = (i += baseSize) < n ? i : (baseIndex = b + 1); |
2457 |
< |
} |
2458 |
< |
nextKey = e.key; |
2459 |
< |
} while ((ev = e.val) == null); // skip deleted or special nodes |
2460 |
< |
next = e; |
2461 |
< |
return nextVal = ev; |
2462 |
< |
} |
2463 |
< |
|
2464 |
< |
public final void remove() { |
2465 |
< |
Object k = nextKey; |
2466 |
< |
if (k == null && (advance() == null || (k = nextKey) == null)) |
2467 |
< |
throw new IllegalStateException(); |
2468 |
< |
map.internalReplace(k, null, null); |
2469 |
< |
} |
784 |
> |
* Table of counter cells. When non-null, size is a power of 2. |
785 |
> |
*/ |
786 |
> |
private transient volatile CounterCell[] counterCells; |
787 |
|
|
788 |
< |
public final boolean hasNext() { |
789 |
< |
return nextVal != null || advance() != null; |
790 |
< |
} |
788 |
> |
// views |
789 |
> |
private transient KeySetView<K,V> keySet; |
790 |
> |
private transient ValuesView<K,V> values; |
791 |
> |
private transient EntrySetView<K,V> entrySet; |
792 |
|
|
2475 |
– |
public final boolean hasMoreElements() { return hasNext(); } |
2476 |
– |
public final void setRawResult(Object x) { } |
2477 |
– |
public R getRawResult() { return null; } |
2478 |
– |
public boolean exec() { return true; } |
2479 |
– |
} |
793 |
|
|
794 |
|
/* ---------------- Public operations -------------- */ |
795 |
|
|
797 |
|
* Creates a new, empty map with the default initial table size (16). |
798 |
|
*/ |
799 |
|
public ConcurrentHashMap() { |
2487 |
– |
this.counter = new LongAdder(); |
800 |
|
} |
801 |
|
|
802 |
|
/** |
810 |
|
* elements is negative |
811 |
|
*/ |
812 |
|
public ConcurrentHashMap(int initialCapacity) { |
813 |
< |
if (initialCapacity < 0) |
2502 |
< |
throw new IllegalArgumentException(); |
2503 |
< |
int cap = ((initialCapacity >= (MAXIMUM_CAPACITY >>> 1)) ? |
2504 |
< |
MAXIMUM_CAPACITY : |
2505 |
< |
tableSizeFor(initialCapacity + (initialCapacity >>> 1) + 1)); |
2506 |
< |
this.counter = new LongAdder(); |
2507 |
< |
this.sizeCtl = cap; |
813 |
> |
this(initialCapacity, LOAD_FACTOR, 1); |
814 |
|
} |
815 |
|
|
816 |
|
/** |
819 |
|
* @param m the map |
820 |
|
*/ |
821 |
|
public ConcurrentHashMap(Map<? extends K, ? extends V> m) { |
2516 |
– |
this.counter = new LongAdder(); |
822 |
|
this.sizeCtl = DEFAULT_CAPACITY; |
823 |
< |
internalPutAll(m); |
823 |
> |
putAll(m); |
824 |
|
} |
825 |
|
|
826 |
|
/** |
844 |
|
|
845 |
|
/** |
846 |
|
* Creates a new, empty map with an initial table size based on |
847 |
< |
* the given number of elements ({@code initialCapacity}), table |
848 |
< |
* density ({@code loadFactor}), and number of concurrently |
847 |
> |
* the given number of elements ({@code initialCapacity}), initial |
848 |
> |
* table density ({@code loadFactor}), and number of concurrently |
849 |
|
* updating threads ({@code concurrencyLevel}). |
850 |
|
* |
851 |
|
* @param initialCapacity the initial capacity. The implementation |
861 |
|
* nonpositive |
862 |
|
*/ |
863 |
|
public ConcurrentHashMap(int initialCapacity, |
864 |
< |
float loadFactor, int concurrencyLevel) { |
864 |
> |
float loadFactor, int concurrencyLevel) { |
865 |
|
if (!(loadFactor > 0.0f) || initialCapacity < 0 || concurrencyLevel <= 0) |
866 |
|
throw new IllegalArgumentException(); |
867 |
|
if (initialCapacity < concurrencyLevel) // Use at least as many bins |
869 |
|
long size = (long)(1.0 + (long)initialCapacity / loadFactor); |
870 |
|
int cap = (size >= (long)MAXIMUM_CAPACITY) ? |
871 |
|
MAXIMUM_CAPACITY : tableSizeFor((int)size); |
2567 |
– |
this.counter = new LongAdder(); |
872 |
|
this.sizeCtl = cap; |
873 |
|
} |
874 |
|
|
875 |
< |
/** |
2572 |
< |
* Creates a new {@link Set} backed by a ConcurrentHashMap |
2573 |
< |
* from the given type to {@code Boolean.TRUE}. |
2574 |
< |
* |
2575 |
< |
* @return the new set |
2576 |
< |
*/ |
2577 |
< |
public static <K> KeySetView<K,Boolean> newKeySet() { |
2578 |
< |
return new KeySetView<K,Boolean>(new ConcurrentHashMap<K,Boolean>(), |
2579 |
< |
Boolean.TRUE); |
2580 |
< |
} |
2581 |
< |
|
2582 |
< |
/** |
2583 |
< |
* Creates a new {@link Set} backed by a ConcurrentHashMap |
2584 |
< |
* from the given type to {@code Boolean.TRUE}. |
2585 |
< |
* |
2586 |
< |
* @param initialCapacity The implementation performs internal |
2587 |
< |
* sizing to accommodate this many elements. |
2588 |
< |
* @throws IllegalArgumentException if the initial capacity of |
2589 |
< |
* elements is negative |
2590 |
< |
* @return the new set |
2591 |
< |
*/ |
2592 |
< |
public static <K> KeySetView<K,Boolean> newKeySet(int initialCapacity) { |
2593 |
< |
return new KeySetView<K,Boolean>(new ConcurrentHashMap<K,Boolean>(initialCapacity), |
2594 |
< |
Boolean.TRUE); |
2595 |
< |
} |
2596 |
< |
|
2597 |
< |
/** |
2598 |
< |
* {@inheritDoc} |
2599 |
< |
*/ |
2600 |
< |
public boolean isEmpty() { |
2601 |
< |
return counter.sum() <= 0L; // ignore transient negative values |
2602 |
< |
} |
875 |
> |
// Original (since JDK1.2) Map methods |
876 |
|
|
877 |
|
/** |
878 |
|
* {@inheritDoc} |
879 |
|
*/ |
880 |
|
public int size() { |
881 |
< |
long n = counter.sum(); |
881 |
> |
long n = sumCount(); |
882 |
|
return ((n < 0L) ? 0 : |
883 |
|
(n > (long)Integer.MAX_VALUE) ? Integer.MAX_VALUE : |
884 |
|
(int)n); |
885 |
|
} |
886 |
|
|
887 |
|
/** |
888 |
< |
* Returns the number of mappings. This method should be used |
2616 |
< |
* instead of {@link #size} because a ConcurrentHashMap may |
2617 |
< |
* contain more mappings than can be represented as an int. The |
2618 |
< |
* value returned is a snapshot; the actual count may differ if |
2619 |
< |
* there are ongoing concurrent insertions or removals. |
2620 |
< |
* |
2621 |
< |
* @return the number of mappings |
888 |
> |
* {@inheritDoc} |
889 |
|
*/ |
890 |
< |
public long mappingCount() { |
891 |
< |
long n = counter.sum(); |
2625 |
< |
return (n < 0L) ? 0L : n; // ignore transient negative values |
890 |
> |
public boolean isEmpty() { |
891 |
> |
return sumCount() <= 0L; // ignore transient negative values |
892 |
|
} |
893 |
|
|
894 |
|
/** |
902 |
|
* |
903 |
|
* @throws NullPointerException if the specified key is null |
904 |
|
*/ |
905 |
< |
@SuppressWarnings("unchecked") public V get(Object key) { |
906 |
< |
if (key == null) |
907 |
< |
throw new NullPointerException(); |
908 |
< |
return (V)internalGet(key); |
909 |
< |
} |
910 |
< |
|
911 |
< |
/** |
912 |
< |
* Returns the value to which the specified key is mapped, |
913 |
< |
* or the given defaultValue if this map contains no mapping for the key. |
914 |
< |
* |
915 |
< |
* @param key the key |
916 |
< |
* @param defaultValue the value to return if this map contains |
917 |
< |
* no mapping for the given key |
918 |
< |
* @return the mapping for the key, if present; else the defaultValue |
919 |
< |
* @throws NullPointerException if the specified key is null |
920 |
< |
*/ |
921 |
< |
@SuppressWarnings("unchecked") public V getValueOrDefault(Object key, V defaultValue) { |
922 |
< |
if (key == null) |
2657 |
< |
throw new NullPointerException(); |
2658 |
< |
V v = (V) internalGet(key); |
2659 |
< |
return v == null ? defaultValue : v; |
905 |
> |
public V get(Object key) { |
906 |
> |
Node<K,V>[] tab; Node<K,V> e, p; int n, eh; K ek; |
907 |
> |
int h = spread(key.hashCode()); |
908 |
> |
if ((tab = table) != null && (n = tab.length) > 0 && |
909 |
> |
(e = tabAt(tab, (n - 1) & h)) != null) { |
910 |
> |
if ((eh = e.hash) == h) { |
911 |
> |
if ((ek = e.key) == key || (ek != null && key.equals(ek))) |
912 |
> |
return e.val; |
913 |
> |
} |
914 |
> |
else if (eh < 0) |
915 |
> |
return (p = e.find(h, key)) != null ? p.val : null; |
916 |
> |
while ((e = e.next) != null) { |
917 |
> |
if (e.hash == h && |
918 |
> |
((ek = e.key) == key || (ek != null && key.equals(ek)))) |
919 |
> |
return e.val; |
920 |
> |
} |
921 |
> |
} |
922 |
> |
return null; |
923 |
|
} |
924 |
|
|
925 |
|
/** |
926 |
|
* Tests if the specified object is a key in this table. |
927 |
|
* |
928 |
< |
* @param key possible key |
928 |
> |
* @param key possible key |
929 |
|
* @return {@code true} if and only if the specified object |
930 |
|
* is a key in this table, as determined by the |
931 |
|
* {@code equals} method; {@code false} otherwise |
932 |
|
* @throws NullPointerException if the specified key is null |
933 |
|
*/ |
934 |
|
public boolean containsKey(Object key) { |
935 |
< |
if (key == null) |
2673 |
< |
throw new NullPointerException(); |
2674 |
< |
return internalGet(key) != null; |
935 |
> |
return get(key) != null; |
936 |
|
} |
937 |
|
|
938 |
|
/** |
948 |
|
public boolean containsValue(Object value) { |
949 |
|
if (value == null) |
950 |
|
throw new NullPointerException(); |
951 |
< |
Object v; |
952 |
< |
Traverser<K,V,Object> it = new Traverser<K,V,Object>(this); |
953 |
< |
while ((v = it.advance()) != null) { |
954 |
< |
if (v == value || value.equals(v)) |
955 |
< |
return true; |
951 |
> |
Node<K,V>[] t; |
952 |
> |
if ((t = table) != null) { |
953 |
> |
Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length); |
954 |
> |
for (Node<K,V> p; (p = it.advance()) != null; ) { |
955 |
> |
V v; |
956 |
> |
if ((v = p.val) == value || (v != null && value.equals(v))) |
957 |
> |
return true; |
958 |
> |
} |
959 |
|
} |
960 |
|
return false; |
961 |
|
} |
962 |
|
|
963 |
|
/** |
2700 |
– |
* Legacy method testing if some key maps into the specified value |
2701 |
– |
* in this table. This method is identical in functionality to |
2702 |
– |
* {@link #containsValue}, and exists solely to ensure |
2703 |
– |
* full compatibility with class {@link java.util.Hashtable}, |
2704 |
– |
* which supported this method prior to introduction of the |
2705 |
– |
* Java Collections framework. |
2706 |
– |
* |
2707 |
– |
* @param value a value to search for |
2708 |
– |
* @return {@code true} if and only if some key maps to the |
2709 |
– |
* {@code value} argument in this table as |
2710 |
– |
* determined by the {@code equals} method; |
2711 |
– |
* {@code false} otherwise |
2712 |
– |
* @throws NullPointerException if the specified value is null |
2713 |
– |
*/ |
2714 |
– |
public boolean contains(Object value) { |
2715 |
– |
return containsValue(value); |
2716 |
– |
} |
2717 |
– |
|
2718 |
– |
/** |
964 |
|
* Maps the specified key to the specified value in this table. |
965 |
|
* Neither the key nor the value can be null. |
966 |
|
* |
967 |
< |
* <p> The value can be retrieved by calling the {@code get} method |
967 |
> |
* <p>The value can be retrieved by calling the {@code get} method |
968 |
|
* with a key that is equal to the original key. |
969 |
|
* |
970 |
|
* @param key key with which the specified value is to be associated |
973 |
|
* {@code null} if there was no mapping for {@code key} |
974 |
|
* @throws NullPointerException if the specified key or value is null |
975 |
|
*/ |
976 |
< |
@SuppressWarnings("unchecked") public V put(K key, V value) { |
977 |
< |
if (key == null || value == null) |
976 |
> |
public V put(K key, V value) { |
977 |
> |
return putVal(key, value, false); |
978 |
> |
} |
979 |
> |
|
980 |
> |
/** Implementation for put and putIfAbsent */ |
981 |
> |
final V putVal(K key, V value, boolean onlyIfAbsent) { |
982 |
> |
if (key == null || value == null) throw new NullPointerException(); |
983 |
> |
int hash = spread(key.hashCode()); |
984 |
> |
int binCount = 0; |
985 |
> |
for (Node<K,V>[] tab = table;;) { |
986 |
> |
Node<K,V> f; int n, i, fh; K fk; V fv; |
987 |
> |
if (tab == null || (n = tab.length) == 0) |
988 |
> |
tab = initTable(); |
989 |
> |
else if ((f = tabAt(tab, i = (n - 1) & hash)) == null) { |
990 |
> |
if (casTabAt(tab, i, null, new Node<K,V>(hash, key, value))) |
991 |
> |
break; // no lock when adding to empty bin |
992 |
> |
} |
993 |
> |
else if ((fh = f.hash) == MOVED) |
994 |
> |
tab = helpTransfer(tab, f); |
995 |
> |
else if (onlyIfAbsent // check first node without acquiring lock |
996 |
> |
&& fh == hash |
997 |
> |
&& ((fk = f.key) == key || (fk != null && key.equals(fk))) |
998 |
> |
&& (fv = f.val) != null) |
999 |
> |
return fv; |
1000 |
> |
else { |
1001 |
> |
V oldVal = null; |
1002 |
> |
synchronized (f) { |
1003 |
> |
if (tabAt(tab, i) == f) { |
1004 |
> |
if (fh >= 0) { |
1005 |
> |
binCount = 1; |
1006 |
> |
for (Node<K,V> e = f;; ++binCount) { |
1007 |
> |
K ek; |
1008 |
> |
if (e.hash == hash && |
1009 |
> |
((ek = e.key) == key || |
1010 |
> |
(ek != null && key.equals(ek)))) { |
1011 |
> |
oldVal = e.val; |
1012 |
> |
if (!onlyIfAbsent) |
1013 |
> |
e.val = value; |
1014 |
> |
break; |
1015 |
> |
} |
1016 |
> |
Node<K,V> pred = e; |
1017 |
> |
if ((e = e.next) == null) { |
1018 |
> |
pred.next = new Node<K,V>(hash, key, value); |
1019 |
> |
break; |
1020 |
> |
} |
1021 |
> |
} |
1022 |
> |
} |
1023 |
> |
else if (f instanceof TreeBin) { |
1024 |
> |
Node<K,V> p; |
1025 |
> |
binCount = 2; |
1026 |
> |
if ((p = ((TreeBin<K,V>)f).putTreeVal(hash, key, |
1027 |
> |
value)) != null) { |
1028 |
> |
oldVal = p.val; |
1029 |
> |
if (!onlyIfAbsent) |
1030 |
> |
p.val = value; |
1031 |
> |
} |
1032 |
> |
} |
1033 |
> |
else if (f instanceof ReservationNode) |
1034 |
> |
throw new IllegalStateException("Recursive update"); |
1035 |
> |
} |
1036 |
> |
} |
1037 |
> |
if (binCount != 0) { |
1038 |
> |
if (binCount >= TREEIFY_THRESHOLD) |
1039 |
> |
treeifyBin(tab, i); |
1040 |
> |
if (oldVal != null) |
1041 |
> |
return oldVal; |
1042 |
> |
break; |
1043 |
> |
} |
1044 |
> |
} |
1045 |
> |
} |
1046 |
> |
addCount(1L, binCount); |
1047 |
> |
return null; |
1048 |
> |
} |
1049 |
> |
|
1050 |
> |
/** |
1051 |
> |
* Copies all of the mappings from the specified map to this one. |
1052 |
> |
* These mappings replace any mappings that this map had for any of the |
1053 |
> |
* keys currently in the specified map. |
1054 |
> |
* |
1055 |
> |
* @param m mappings to be stored in this map |
1056 |
> |
*/ |
1057 |
> |
public void putAll(Map<? extends K, ? extends V> m) { |
1058 |
> |
tryPresize(m.size()); |
1059 |
> |
for (Map.Entry<? extends K, ? extends V> e : m.entrySet()) |
1060 |
> |
putVal(e.getKey(), e.getValue(), false); |
1061 |
> |
} |
1062 |
> |
|
1063 |
> |
/** |
1064 |
> |
* Removes the key (and its corresponding value) from this map. |
1065 |
> |
* This method does nothing if the key is not in the map. |
1066 |
> |
* |
1067 |
> |
* @param key the key that needs to be removed |
1068 |
> |
* @return the previous value associated with {@code key}, or |
1069 |
> |
* {@code null} if there was no mapping for {@code key} |
1070 |
> |
* @throws NullPointerException if the specified key is null |
1071 |
> |
*/ |
1072 |
> |
public V remove(Object key) { |
1073 |
> |
return replaceNode(key, null, null); |
1074 |
> |
} |
1075 |
> |
|
1076 |
> |
/** |
1077 |
> |
* Implementation for the four public remove/replace methods: |
1078 |
> |
* Replaces node value with v, conditional upon match of cv if |
1079 |
> |
* non-null. If resulting value is null, delete. |
1080 |
> |
*/ |
1081 |
> |
final V replaceNode(Object key, V value, Object cv) { |
1082 |
> |
int hash = spread(key.hashCode()); |
1083 |
> |
for (Node<K,V>[] tab = table;;) { |
1084 |
> |
Node<K,V> f; int n, i, fh; |
1085 |
> |
if (tab == null || (n = tab.length) == 0 || |
1086 |
> |
(f = tabAt(tab, i = (n - 1) & hash)) == null) |
1087 |
> |
break; |
1088 |
> |
else if ((fh = f.hash) == MOVED) |
1089 |
> |
tab = helpTransfer(tab, f); |
1090 |
> |
else { |
1091 |
> |
V oldVal = null; |
1092 |
> |
boolean validated = false; |
1093 |
> |
synchronized (f) { |
1094 |
> |
if (tabAt(tab, i) == f) { |
1095 |
> |
if (fh >= 0) { |
1096 |
> |
validated = true; |
1097 |
> |
for (Node<K,V> e = f, pred = null;;) { |
1098 |
> |
K ek; |
1099 |
> |
if (e.hash == hash && |
1100 |
> |
((ek = e.key) == key || |
1101 |
> |
(ek != null && key.equals(ek)))) { |
1102 |
> |
V ev = e.val; |
1103 |
> |
if (cv == null || cv == ev || |
1104 |
> |
(ev != null && cv.equals(ev))) { |
1105 |
> |
oldVal = ev; |
1106 |
> |
if (value != null) |
1107 |
> |
e.val = value; |
1108 |
> |
else if (pred != null) |
1109 |
> |
pred.next = e.next; |
1110 |
> |
else |
1111 |
> |
setTabAt(tab, i, e.next); |
1112 |
> |
} |
1113 |
> |
break; |
1114 |
> |
} |
1115 |
> |
pred = e; |
1116 |
> |
if ((e = e.next) == null) |
1117 |
> |
break; |
1118 |
> |
} |
1119 |
> |
} |
1120 |
> |
else if (f instanceof TreeBin) { |
1121 |
> |
validated = true; |
1122 |
> |
TreeBin<K,V> t = (TreeBin<K,V>)f; |
1123 |
> |
TreeNode<K,V> r, p; |
1124 |
> |
if ((r = t.root) != null && |
1125 |
> |
(p = r.findTreeNode(hash, key, null)) != null) { |
1126 |
> |
V pv = p.val; |
1127 |
> |
if (cv == null || cv == pv || |
1128 |
> |
(pv != null && cv.equals(pv))) { |
1129 |
> |
oldVal = pv; |
1130 |
> |
if (value != null) |
1131 |
> |
p.val = value; |
1132 |
> |
else if (t.removeTreeNode(p)) |
1133 |
> |
setTabAt(tab, i, untreeify(t.first)); |
1134 |
> |
} |
1135 |
> |
} |
1136 |
> |
} |
1137 |
> |
else if (f instanceof ReservationNode) |
1138 |
> |
throw new IllegalStateException("Recursive update"); |
1139 |
> |
} |
1140 |
> |
} |
1141 |
> |
if (validated) { |
1142 |
> |
if (oldVal != null) { |
1143 |
> |
if (value == null) |
1144 |
> |
addCount(-1L, -1); |
1145 |
> |
return oldVal; |
1146 |
> |
} |
1147 |
> |
break; |
1148 |
> |
} |
1149 |
> |
} |
1150 |
> |
} |
1151 |
> |
return null; |
1152 |
> |
} |
1153 |
> |
|
1154 |
> |
/** |
1155 |
> |
* Removes all of the mappings from this map. |
1156 |
> |
*/ |
1157 |
> |
public void clear() { |
1158 |
> |
long delta = 0L; // negative number of deletions |
1159 |
> |
int i = 0; |
1160 |
> |
Node<K,V>[] tab = table; |
1161 |
> |
while (tab != null && i < tab.length) { |
1162 |
> |
int fh; |
1163 |
> |
Node<K,V> f = tabAt(tab, i); |
1164 |
> |
if (f == null) |
1165 |
> |
++i; |
1166 |
> |
else if ((fh = f.hash) == MOVED) { |
1167 |
> |
tab = helpTransfer(tab, f); |
1168 |
> |
i = 0; // restart |
1169 |
> |
} |
1170 |
> |
else { |
1171 |
> |
synchronized (f) { |
1172 |
> |
if (tabAt(tab, i) == f) { |
1173 |
> |
Node<K,V> p = (fh >= 0 ? f : |
1174 |
> |
(f instanceof TreeBin) ? |
1175 |
> |
((TreeBin<K,V>)f).first : null); |
1176 |
> |
while (p != null) { |
1177 |
> |
--delta; |
1178 |
> |
p = p.next; |
1179 |
> |
} |
1180 |
> |
setTabAt(tab, i++, null); |
1181 |
> |
} |
1182 |
> |
} |
1183 |
> |
} |
1184 |
> |
} |
1185 |
> |
if (delta != 0L) |
1186 |
> |
addCount(delta, -1); |
1187 |
> |
} |
1188 |
> |
|
1189 |
> |
/** |
1190 |
> |
* Returns a {@link Set} view of the keys contained in this map. |
1191 |
> |
* The set is backed by the map, so changes to the map are |
1192 |
> |
* reflected in the set, and vice-versa. The set supports element |
1193 |
> |
* removal, which removes the corresponding mapping from this map, |
1194 |
> |
* via the {@code Iterator.remove}, {@code Set.remove}, |
1195 |
> |
* {@code removeAll}, {@code retainAll}, and {@code clear} |
1196 |
> |
* operations. It does not support the {@code add} or |
1197 |
> |
* {@code addAll} operations. |
1198 |
> |
* |
1199 |
> |
* <p>The view's iterators and spliterators are |
1200 |
> |
* <a href="package-summary.html#Weakly"><i>weakly consistent</i></a>. |
1201 |
> |
* |
1202 |
> |
* <p>The view's {@code spliterator} reports {@link Spliterator#CONCURRENT}, |
1203 |
> |
* {@link Spliterator#DISTINCT}, and {@link Spliterator#NONNULL}. |
1204 |
> |
* |
1205 |
> |
* @return the set view |
1206 |
> |
*/ |
1207 |
> |
public KeySetView<K,V> keySet() { |
1208 |
> |
KeySetView<K,V> ks; |
1209 |
> |
if ((ks = keySet) != null) return ks; |
1210 |
> |
return keySet = new KeySetView<K,V>(this, null); |
1211 |
> |
} |
1212 |
> |
|
1213 |
> |
/** |
1214 |
> |
* Returns a {@link Collection} view of the values contained in this map. |
1215 |
> |
* The collection is backed by the map, so changes to the map are |
1216 |
> |
* reflected in the collection, and vice-versa. The collection |
1217 |
> |
* supports element removal, which removes the corresponding |
1218 |
> |
* mapping from this map, via the {@code Iterator.remove}, |
1219 |
> |
* {@code Collection.remove}, {@code removeAll}, |
1220 |
> |
* {@code retainAll}, and {@code clear} operations. It does not |
1221 |
> |
* support the {@code add} or {@code addAll} operations. |
1222 |
> |
* |
1223 |
> |
* <p>The view's iterators and spliterators are |
1224 |
> |
* <a href="package-summary.html#Weakly"><i>weakly consistent</i></a>. |
1225 |
> |
* |
1226 |
> |
* <p>The view's {@code spliterator} reports {@link Spliterator#CONCURRENT} |
1227 |
> |
* and {@link Spliterator#NONNULL}. |
1228 |
> |
* |
1229 |
> |
* @return the collection view |
1230 |
> |
*/ |
1231 |
> |
public Collection<V> values() { |
1232 |
> |
ValuesView<K,V> vs; |
1233 |
> |
if ((vs = values) != null) return vs; |
1234 |
> |
return values = new ValuesView<K,V>(this); |
1235 |
> |
} |
1236 |
> |
|
1237 |
> |
/** |
1238 |
> |
* Returns a {@link Set} view of the mappings contained in this map. |
1239 |
> |
* The set is backed by the map, so changes to the map are |
1240 |
> |
* reflected in the set, and vice-versa. The set supports element |
1241 |
> |
* removal, which removes the corresponding mapping from the map, |
1242 |
> |
* via the {@code Iterator.remove}, {@code Set.remove}, |
1243 |
> |
* {@code removeAll}, {@code retainAll}, and {@code clear} |
1244 |
> |
* operations. |
1245 |
> |
* |
1246 |
> |
* <p>The view's iterators and spliterators are |
1247 |
> |
* <a href="package-summary.html#Weakly"><i>weakly consistent</i></a>. |
1248 |
> |
* |
1249 |
> |
* <p>The view's {@code spliterator} reports {@link Spliterator#CONCURRENT}, |
1250 |
> |
* {@link Spliterator#DISTINCT}, and {@link Spliterator#NONNULL}. |
1251 |
> |
* |
1252 |
> |
* @return the set view |
1253 |
> |
*/ |
1254 |
> |
public Set<Map.Entry<K,V>> entrySet() { |
1255 |
> |
EntrySetView<K,V> es; |
1256 |
> |
if ((es = entrySet) != null) return es; |
1257 |
> |
return entrySet = new EntrySetView<K,V>(this); |
1258 |
> |
} |
1259 |
> |
|
1260 |
> |
/** |
1261 |
> |
* Returns the hash code value for this {@link Map}, i.e., |
1262 |
> |
* the sum of, for each key-value pair in the map, |
1263 |
> |
* {@code key.hashCode() ^ value.hashCode()}. |
1264 |
> |
* |
1265 |
> |
* @return the hash code value for this map |
1266 |
> |
*/ |
1267 |
> |
public int hashCode() { |
1268 |
> |
int h = 0; |
1269 |
> |
Node<K,V>[] t; |
1270 |
> |
if ((t = table) != null) { |
1271 |
> |
Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length); |
1272 |
> |
for (Node<K,V> p; (p = it.advance()) != null; ) |
1273 |
> |
h += p.key.hashCode() ^ p.val.hashCode(); |
1274 |
> |
} |
1275 |
> |
return h; |
1276 |
> |
} |
1277 |
> |
|
1278 |
> |
/** |
1279 |
> |
* Returns a string representation of this map. The string |
1280 |
> |
* representation consists of a list of key-value mappings (in no |
1281 |
> |
* particular order) enclosed in braces ("{@code {}}"). Adjacent |
1282 |
> |
* mappings are separated by the characters {@code ", "} (comma |
1283 |
> |
* and space). Each key-value mapping is rendered as the key |
1284 |
> |
* followed by an equals sign ("{@code =}") followed by the |
1285 |
> |
* associated value. |
1286 |
> |
* |
1287 |
> |
* @return a string representation of this map |
1288 |
> |
*/ |
1289 |
> |
public String toString() { |
1290 |
> |
Node<K,V>[] t; |
1291 |
> |
int f = (t = table) == null ? 0 : t.length; |
1292 |
> |
Traverser<K,V> it = new Traverser<K,V>(t, f, 0, f); |
1293 |
> |
StringBuilder sb = new StringBuilder(); |
1294 |
> |
sb.append('{'); |
1295 |
> |
Node<K,V> p; |
1296 |
> |
if ((p = it.advance()) != null) { |
1297 |
> |
for (;;) { |
1298 |
> |
K k = p.key; |
1299 |
> |
V v = p.val; |
1300 |
> |
sb.append(k == this ? "(this Map)" : k); |
1301 |
> |
sb.append('='); |
1302 |
> |
sb.append(v == this ? "(this Map)" : v); |
1303 |
> |
if ((p = it.advance()) == null) |
1304 |
> |
break; |
1305 |
> |
sb.append(',').append(' '); |
1306 |
> |
} |
1307 |
> |
} |
1308 |
> |
return sb.append('}').toString(); |
1309 |
> |
} |
1310 |
> |
|
1311 |
> |
/** |
1312 |
> |
* Compares the specified object with this map for equality. |
1313 |
> |
* Returns {@code true} if the given object is a map with the same |
1314 |
> |
* mappings as this map. This operation may return misleading |
1315 |
> |
* results if either map is concurrently modified during execution |
1316 |
> |
* of this method. |
1317 |
> |
* |
1318 |
> |
* @param o object to be compared for equality with this map |
1319 |
> |
* @return {@code true} if the specified object is equal to this map |
1320 |
> |
*/ |
1321 |
> |
public boolean equals(Object o) { |
1322 |
> |
if (o != this) { |
1323 |
> |
if (!(o instanceof Map)) |
1324 |
> |
return false; |
1325 |
> |
Map<?,?> m = (Map<?,?>) o; |
1326 |
> |
Node<K,V>[] t; |
1327 |
> |
int f = (t = table) == null ? 0 : t.length; |
1328 |
> |
Traverser<K,V> it = new Traverser<K,V>(t, f, 0, f); |
1329 |
> |
for (Node<K,V> p; (p = it.advance()) != null; ) { |
1330 |
> |
V val = p.val; |
1331 |
> |
Object v = m.get(p.key); |
1332 |
> |
if (v == null || (v != val && !v.equals(val))) |
1333 |
> |
return false; |
1334 |
> |
} |
1335 |
> |
for (Map.Entry<?,?> e : m.entrySet()) { |
1336 |
> |
Object mk, mv, v; |
1337 |
> |
if ((mk = e.getKey()) == null || |
1338 |
> |
(mv = e.getValue()) == null || |
1339 |
> |
(v = get(mk)) == null || |
1340 |
> |
(mv != v && !mv.equals(v))) |
1341 |
> |
return false; |
1342 |
> |
} |
1343 |
> |
} |
1344 |
> |
return true; |
1345 |
> |
} |
1346 |
> |
|
1347 |
> |
/** |
1348 |
> |
* Stripped-down version of helper class used in previous version, |
1349 |
> |
* declared for the sake of serialization compatibility. |
1350 |
> |
*/ |
1351 |
> |
static class Segment<K,V> extends ReentrantLock implements Serializable { |
1352 |
> |
private static final long serialVersionUID = 2249069246763182397L; |
1353 |
> |
final float loadFactor; |
1354 |
> |
Segment(float lf) { this.loadFactor = lf; } |
1355 |
> |
} |
1356 |
> |
|
1357 |
> |
/** |
1358 |
> |
* Saves this map to a stream (that is, serializes it). |
1359 |
> |
* |
1360 |
> |
* @param s the stream |
1361 |
> |
* @throws java.io.IOException if an I/O error occurs |
1362 |
> |
* @serialData |
1363 |
> |
* the serialized fields, followed by the key (Object) and value |
1364 |
> |
* (Object) for each key-value mapping, followed by a null pair. |
1365 |
> |
* The key-value mappings are emitted in no particular order. |
1366 |
> |
*/ |
1367 |
> |
private void writeObject(java.io.ObjectOutputStream s) |
1368 |
> |
throws java.io.IOException { |
1369 |
> |
// For serialization compatibility |
1370 |
> |
// Emulate segment calculation from previous version of this class |
1371 |
> |
int sshift = 0; |
1372 |
> |
int ssize = 1; |
1373 |
> |
while (ssize < DEFAULT_CONCURRENCY_LEVEL) { |
1374 |
> |
++sshift; |
1375 |
> |
ssize <<= 1; |
1376 |
> |
} |
1377 |
> |
int segmentShift = 32 - sshift; |
1378 |
> |
int segmentMask = ssize - 1; |
1379 |
> |
@SuppressWarnings("unchecked") |
1380 |
> |
Segment<K,V>[] segments = (Segment<K,V>[]) |
1381 |
> |
new Segment<?,?>[DEFAULT_CONCURRENCY_LEVEL]; |
1382 |
> |
for (int i = 0; i < segments.length; ++i) |
1383 |
> |
segments[i] = new Segment<K,V>(LOAD_FACTOR); |
1384 |
> |
java.io.ObjectOutputStream.PutField streamFields = s.putFields(); |
1385 |
> |
streamFields.put("segments", segments); |
1386 |
> |
streamFields.put("segmentShift", segmentShift); |
1387 |
> |
streamFields.put("segmentMask", segmentMask); |
1388 |
> |
s.writeFields(); |
1389 |
> |
|
1390 |
> |
Node<K,V>[] t; |
1391 |
> |
if ((t = table) != null) { |
1392 |
> |
Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length); |
1393 |
> |
for (Node<K,V> p; (p = it.advance()) != null; ) { |
1394 |
> |
s.writeObject(p.key); |
1395 |
> |
s.writeObject(p.val); |
1396 |
> |
} |
1397 |
> |
} |
1398 |
> |
s.writeObject(null); |
1399 |
> |
s.writeObject(null); |
1400 |
> |
} |
1401 |
> |
|
1402 |
> |
/** |
1403 |
> |
* Reconstitutes this map from a stream (that is, deserializes it). |
1404 |
> |
* @param s the stream |
1405 |
> |
* @throws ClassNotFoundException if the class of a serialized object |
1406 |
> |
* could not be found |
1407 |
> |
* @throws java.io.IOException if an I/O error occurs |
1408 |
> |
*/ |
1409 |
> |
private void readObject(java.io.ObjectInputStream s) |
1410 |
> |
throws java.io.IOException, ClassNotFoundException { |
1411 |
> |
/* |
1412 |
> |
* To improve performance in typical cases, we create nodes |
1413 |
> |
* while reading, then place in table once size is known. |
1414 |
> |
* However, we must also validate uniqueness and deal with |
1415 |
> |
* overpopulated bins while doing so, which requires |
1416 |
> |
* specialized versions of putVal mechanics. |
1417 |
> |
*/ |
1418 |
> |
sizeCtl = -1; // force exclusion for table construction |
1419 |
> |
s.defaultReadObject(); |
1420 |
> |
long size = 0L; |
1421 |
> |
Node<K,V> p = null; |
1422 |
> |
for (;;) { |
1423 |
> |
@SuppressWarnings("unchecked") |
1424 |
> |
K k = (K) s.readObject(); |
1425 |
> |
@SuppressWarnings("unchecked") |
1426 |
> |
V v = (V) s.readObject(); |
1427 |
> |
if (k != null && v != null) { |
1428 |
> |
p = new Node<K,V>(spread(k.hashCode()), k, v, p); |
1429 |
> |
++size; |
1430 |
> |
} |
1431 |
> |
else |
1432 |
> |
break; |
1433 |
> |
} |
1434 |
> |
if (size == 0L) |
1435 |
> |
sizeCtl = 0; |
1436 |
> |
else { |
1437 |
> |
long ts = (long)(1.0 + size / LOAD_FACTOR); |
1438 |
> |
int n = (ts >= (long)MAXIMUM_CAPACITY) ? |
1439 |
> |
MAXIMUM_CAPACITY : tableSizeFor((int)ts); |
1440 |
> |
@SuppressWarnings("unchecked") |
1441 |
> |
Node<K,V>[] tab = (Node<K,V>[])new Node<?,?>[n]; |
1442 |
> |
int mask = n - 1; |
1443 |
> |
long added = 0L; |
1444 |
> |
while (p != null) { |
1445 |
> |
boolean insertAtFront; |
1446 |
> |
Node<K,V> next = p.next, first; |
1447 |
> |
int h = p.hash, j = h & mask; |
1448 |
> |
if ((first = tabAt(tab, j)) == null) |
1449 |
> |
insertAtFront = true; |
1450 |
> |
else { |
1451 |
> |
K k = p.key; |
1452 |
> |
if (first.hash < 0) { |
1453 |
> |
TreeBin<K,V> t = (TreeBin<K,V>)first; |
1454 |
> |
if (t.putTreeVal(h, k, p.val) == null) |
1455 |
> |
++added; |
1456 |
> |
insertAtFront = false; |
1457 |
> |
} |
1458 |
> |
else { |
1459 |
> |
int binCount = 0; |
1460 |
> |
insertAtFront = true; |
1461 |
> |
Node<K,V> q; K qk; |
1462 |
> |
for (q = first; q != null; q = q.next) { |
1463 |
> |
if (q.hash == h && |
1464 |
> |
((qk = q.key) == k || |
1465 |
> |
(qk != null && k.equals(qk)))) { |
1466 |
> |
insertAtFront = false; |
1467 |
> |
break; |
1468 |
> |
} |
1469 |
> |
++binCount; |
1470 |
> |
} |
1471 |
> |
if (insertAtFront && binCount >= TREEIFY_THRESHOLD) { |
1472 |
> |
insertAtFront = false; |
1473 |
> |
++added; |
1474 |
> |
p.next = first; |
1475 |
> |
TreeNode<K,V> hd = null, tl = null; |
1476 |
> |
for (q = p; q != null; q = q.next) { |
1477 |
> |
TreeNode<K,V> t = new TreeNode<K,V> |
1478 |
> |
(q.hash, q.key, q.val, null, null); |
1479 |
> |
if ((t.prev = tl) == null) |
1480 |
> |
hd = t; |
1481 |
> |
else |
1482 |
> |
tl.next = t; |
1483 |
> |
tl = t; |
1484 |
> |
} |
1485 |
> |
setTabAt(tab, j, new TreeBin<K,V>(hd)); |
1486 |
> |
} |
1487 |
> |
} |
1488 |
> |
} |
1489 |
> |
if (insertAtFront) { |
1490 |
> |
++added; |
1491 |
> |
p.next = first; |
1492 |
> |
setTabAt(tab, j, p); |
1493 |
> |
} |
1494 |
> |
p = next; |
1495 |
> |
} |
1496 |
> |
table = tab; |
1497 |
> |
sizeCtl = n - (n >>> 2); |
1498 |
> |
baseCount = added; |
1499 |
> |
} |
1500 |
> |
} |
1501 |
> |
|
1502 |
> |
// ConcurrentMap methods |
1503 |
> |
|
1504 |
> |
/** |
1505 |
> |
* {@inheritDoc} |
1506 |
> |
* |
1507 |
> |
* @return the previous value associated with the specified key, |
1508 |
> |
* or {@code null} if there was no mapping for the key |
1509 |
> |
* @throws NullPointerException if the specified key or value is null |
1510 |
> |
*/ |
1511 |
> |
public V putIfAbsent(K key, V value) { |
1512 |
> |
return putVal(key, value, true); |
1513 |
> |
} |
1514 |
> |
|
1515 |
> |
/** |
1516 |
> |
* {@inheritDoc} |
1517 |
> |
* |
1518 |
> |
* @throws NullPointerException if the specified key is null |
1519 |
> |
*/ |
1520 |
> |
public boolean remove(Object key, Object value) { |
1521 |
> |
if (key == null) |
1522 |
|
throw new NullPointerException(); |
1523 |
< |
return (V)internalPut(key, value); |
1523 |
> |
return value != null && replaceNode(key, null, value) != null; |
1524 |
> |
} |
1525 |
> |
|
1526 |
> |
/** |
1527 |
> |
* {@inheritDoc} |
1528 |
> |
* |
1529 |
> |
* @throws NullPointerException if any of the arguments are null |
1530 |
> |
*/ |
1531 |
> |
public boolean replace(K key, V oldValue, V newValue) { |
1532 |
> |
if (key == null || oldValue == null || newValue == null) |
1533 |
> |
throw new NullPointerException(); |
1534 |
> |
return replaceNode(key, newValue, oldValue) != null; |
1535 |
|
} |
1536 |
|
|
1537 |
|
/** |
1541 |
|
* or {@code null} if there was no mapping for the key |
1542 |
|
* @throws NullPointerException if the specified key or value is null |
1543 |
|
*/ |
1544 |
< |
@SuppressWarnings("unchecked") public V putIfAbsent(K key, V value) { |
1544 |
> |
public V replace(K key, V value) { |
1545 |
|
if (key == null || value == null) |
1546 |
|
throw new NullPointerException(); |
1547 |
< |
return (V)internalPutIfAbsent(key, value); |
1547 |
> |
return replaceNode(key, value, null); |
1548 |
|
} |
1549 |
|
|
1550 |
+ |
// Overrides of JDK8+ Map extension method defaults |
1551 |
+ |
|
1552 |
|
/** |
1553 |
< |
* Copies all of the mappings from the specified map to this one. |
1554 |
< |
* These mappings replace any mappings that this map had for any of the |
1555 |
< |
* keys currently in the specified map. |
1553 |
> |
* Returns the value to which the specified key is mapped, or the |
1554 |
> |
* given default value if this map contains no mapping for the |
1555 |
> |
* key. |
1556 |
|
* |
1557 |
< |
* @param m mappings to be stored in this map |
1557 |
> |
* @param key the key whose associated value is to be returned |
1558 |
> |
* @param defaultValue the value to return if this map contains |
1559 |
> |
* no mapping for the given key |
1560 |
> |
* @return the mapping for the key, if present; else the default value |
1561 |
> |
* @throws NullPointerException if the specified key is null |
1562 |
|
*/ |
1563 |
< |
public void putAll(Map<? extends K, ? extends V> m) { |
1564 |
< |
internalPutAll(m); |
1563 |
> |
public V getOrDefault(Object key, V defaultValue) { |
1564 |
> |
V v; |
1565 |
> |
return (v = get(key)) == null ? defaultValue : v; |
1566 |
> |
} |
1567 |
> |
|
1568 |
> |
public void forEach(BiConsumer<? super K, ? super V> action) { |
1569 |
> |
if (action == null) throw new NullPointerException(); |
1570 |
> |
Node<K,V>[] t; |
1571 |
> |
if ((t = table) != null) { |
1572 |
> |
Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length); |
1573 |
> |
for (Node<K,V> p; (p = it.advance()) != null; ) { |
1574 |
> |
action.accept(p.key, p.val); |
1575 |
> |
} |
1576 |
> |
} |
1577 |
> |
} |
1578 |
> |
|
1579 |
> |
public void replaceAll(BiFunction<? super K, ? super V, ? extends V> function) { |
1580 |
> |
if (function == null) throw new NullPointerException(); |
1581 |
> |
Node<K,V>[] t; |
1582 |
> |
if ((t = table) != null) { |
1583 |
> |
Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length); |
1584 |
> |
for (Node<K,V> p; (p = it.advance()) != null; ) { |
1585 |
> |
V oldValue = p.val; |
1586 |
> |
for (K key = p.key;;) { |
1587 |
> |
V newValue = function.apply(key, oldValue); |
1588 |
> |
if (newValue == null) |
1589 |
> |
throw new NullPointerException(); |
1590 |
> |
if (replaceNode(key, newValue, oldValue) != null || |
1591 |
> |
(oldValue = get(key)) == null) |
1592 |
> |
break; |
1593 |
> |
} |
1594 |
> |
} |
1595 |
> |
} |
1596 |
> |
} |
1597 |
> |
|
1598 |
> |
/** |
1599 |
> |
* Helper method for EntrySetView.removeIf. |
1600 |
> |
*/ |
1601 |
> |
boolean removeEntryIf(Predicate<? super Entry<K,V>> function) { |
1602 |
> |
if (function == null) throw new NullPointerException(); |
1603 |
> |
Node<K,V>[] t; |
1604 |
> |
boolean removed = false; |
1605 |
> |
if ((t = table) != null) { |
1606 |
> |
Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length); |
1607 |
> |
for (Node<K,V> p; (p = it.advance()) != null; ) { |
1608 |
> |
K k = p.key; |
1609 |
> |
V v = p.val; |
1610 |
> |
Map.Entry<K,V> e = new AbstractMap.SimpleImmutableEntry<>(k, v); |
1611 |
> |
if (function.test(e) && replaceNode(k, null, v) != null) |
1612 |
> |
removed = true; |
1613 |
> |
} |
1614 |
> |
} |
1615 |
> |
return removed; |
1616 |
> |
} |
1617 |
> |
|
1618 |
> |
/** |
1619 |
> |
* Helper method for ValuesView.removeIf. |
1620 |
> |
*/ |
1621 |
> |
boolean removeValueIf(Predicate<? super V> function) { |
1622 |
> |
if (function == null) throw new NullPointerException(); |
1623 |
> |
Node<K,V>[] t; |
1624 |
> |
boolean removed = false; |
1625 |
> |
if ((t = table) != null) { |
1626 |
> |
Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length); |
1627 |
> |
for (Node<K,V> p; (p = it.advance()) != null; ) { |
1628 |
> |
K k = p.key; |
1629 |
> |
V v = p.val; |
1630 |
> |
if (function.test(v) && replaceNode(k, null, v) != null) |
1631 |
> |
removed = true; |
1632 |
> |
} |
1633 |
> |
} |
1634 |
> |
return removed; |
1635 |
|
} |
1636 |
|
|
1637 |
|
/** |
1638 |
|
* If the specified key is not already associated with a value, |
1639 |
< |
* computes its value using the given mappingFunction and enters |
1640 |
< |
* it into the map unless null. This is equivalent to |
1641 |
< |
* <pre> {@code |
1642 |
< |
* if (map.containsKey(key)) |
1643 |
< |
* return map.get(key); |
1644 |
< |
* value = mappingFunction.apply(key); |
1645 |
< |
* if (value != null) |
2770 |
< |
* map.put(key, value); |
2771 |
< |
* return value;}</pre> |
2772 |
< |
* |
2773 |
< |
* except that the action is performed atomically. If the |
2774 |
< |
* function returns {@code null} no mapping is recorded. If the |
2775 |
< |
* function itself throws an (unchecked) exception, the exception |
2776 |
< |
* is rethrown to its caller, and no mapping is recorded. Some |
2777 |
< |
* attempted update operations on this map by other threads may be |
2778 |
< |
* blocked while computation is in progress, so the computation |
2779 |
< |
* should be short and simple, and must not attempt to update any |
2780 |
< |
* other mappings of this Map. The most appropriate usage is to |
2781 |
< |
* construct a new object serving as an initial mapped value, or |
2782 |
< |
* memoized result, as in: |
2783 |
< |
* |
2784 |
< |
* <pre> {@code |
2785 |
< |
* map.computeIfAbsent(key, new Fun<K, V>() { |
2786 |
< |
* public V map(K k) { return new Value(f(k)); }});}</pre> |
1639 |
> |
* attempts to compute its value using the given mapping function |
1640 |
> |
* and enters it into this map unless {@code null}. The entire |
1641 |
> |
* method invocation is performed atomically, so the function is |
1642 |
> |
* applied at most once per key. Some attempted update operations |
1643 |
> |
* on this map by other threads may be blocked while computation |
1644 |
> |
* is in progress, so the computation should be short and simple, |
1645 |
> |
* and must not attempt to update any other mappings of this map. |
1646 |
|
* |
1647 |
|
* @param key key with which the specified value is to be associated |
1648 |
|
* @param mappingFunction the function to compute a value |
1656 |
|
* @throws RuntimeException or Error if the mappingFunction does so, |
1657 |
|
* in which case the mapping is left unestablished |
1658 |
|
*/ |
1659 |
< |
@SuppressWarnings("unchecked") public V computeIfAbsent |
2801 |
< |
(K key, Fun<? super K, ? extends V> mappingFunction) { |
1659 |
> |
public V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction) { |
1660 |
|
if (key == null || mappingFunction == null) |
1661 |
|
throw new NullPointerException(); |
1662 |
< |
return (V)internalComputeIfAbsent(key, mappingFunction); |
1662 |
> |
int h = spread(key.hashCode()); |
1663 |
> |
V val = null; |
1664 |
> |
int binCount = 0; |
1665 |
> |
for (Node<K,V>[] tab = table;;) { |
1666 |
> |
Node<K,V> f; int n, i, fh; K fk; V fv; |
1667 |
> |
if (tab == null || (n = tab.length) == 0) |
1668 |
> |
tab = initTable(); |
1669 |
> |
else if ((f = tabAt(tab, i = (n - 1) & h)) == null) { |
1670 |
> |
Node<K,V> r = new ReservationNode<K,V>(); |
1671 |
> |
synchronized (r) { |
1672 |
> |
if (casTabAt(tab, i, null, r)) { |
1673 |
> |
binCount = 1; |
1674 |
> |
Node<K,V> node = null; |
1675 |
> |
try { |
1676 |
> |
if ((val = mappingFunction.apply(key)) != null) |
1677 |
> |
node = new Node<K,V>(h, key, val); |
1678 |
> |
} finally { |
1679 |
> |
setTabAt(tab, i, node); |
1680 |
> |
} |
1681 |
> |
} |
1682 |
> |
} |
1683 |
> |
if (binCount != 0) |
1684 |
> |
break; |
1685 |
> |
} |
1686 |
> |
else if ((fh = f.hash) == MOVED) |
1687 |
> |
tab = helpTransfer(tab, f); |
1688 |
> |
else if (fh == h // check first node without acquiring lock |
1689 |
> |
&& ((fk = f.key) == key || (fk != null && key.equals(fk))) |
1690 |
> |
&& (fv = f.val) != null) |
1691 |
> |
return fv; |
1692 |
> |
else { |
1693 |
> |
boolean added = false; |
1694 |
> |
synchronized (f) { |
1695 |
> |
if (tabAt(tab, i) == f) { |
1696 |
> |
if (fh >= 0) { |
1697 |
> |
binCount = 1; |
1698 |
> |
for (Node<K,V> e = f;; ++binCount) { |
1699 |
> |
K ek; |
1700 |
> |
if (e.hash == h && |
1701 |
> |
((ek = e.key) == key || |
1702 |
> |
(ek != null && key.equals(ek)))) { |
1703 |
> |
val = e.val; |
1704 |
> |
break; |
1705 |
> |
} |
1706 |
> |
Node<K,V> pred = e; |
1707 |
> |
if ((e = e.next) == null) { |
1708 |
> |
if ((val = mappingFunction.apply(key)) != null) { |
1709 |
> |
if (pred.next != null) |
1710 |
> |
throw new IllegalStateException("Recursive update"); |
1711 |
> |
added = true; |
1712 |
> |
pred.next = new Node<K,V>(h, key, val); |
1713 |
> |
} |
1714 |
> |
break; |
1715 |
> |
} |
1716 |
> |
} |
1717 |
> |
} |
1718 |
> |
else if (f instanceof TreeBin) { |
1719 |
> |
binCount = 2; |
1720 |
> |
TreeBin<K,V> t = (TreeBin<K,V>)f; |
1721 |
> |
TreeNode<K,V> r, p; |
1722 |
> |
if ((r = t.root) != null && |
1723 |
> |
(p = r.findTreeNode(h, key, null)) != null) |
1724 |
> |
val = p.val; |
1725 |
> |
else if ((val = mappingFunction.apply(key)) != null) { |
1726 |
> |
added = true; |
1727 |
> |
t.putTreeVal(h, key, val); |
1728 |
> |
} |
1729 |
> |
} |
1730 |
> |
else if (f instanceof ReservationNode) |
1731 |
> |
throw new IllegalStateException("Recursive update"); |
1732 |
> |
} |
1733 |
> |
} |
1734 |
> |
if (binCount != 0) { |
1735 |
> |
if (binCount >= TREEIFY_THRESHOLD) |
1736 |
> |
treeifyBin(tab, i); |
1737 |
> |
if (!added) |
1738 |
> |
return val; |
1739 |
> |
break; |
1740 |
> |
} |
1741 |
> |
} |
1742 |
> |
} |
1743 |
> |
if (val != null) |
1744 |
> |
addCount(1L, binCount); |
1745 |
> |
return val; |
1746 |
|
} |
1747 |
|
|
1748 |
|
/** |
1749 |
< |
* If the given key is present, computes a new mapping value given a key and |
1750 |
< |
* its current mapped value. This is equivalent to |
1751 |
< |
* <pre> {@code |
1752 |
< |
* if (map.containsKey(key)) { |
1753 |
< |
* value = remappingFunction.apply(key, map.get(key)); |
1754 |
< |
* if (value != null) |
1755 |
< |
* map.put(key, value); |
2815 |
< |
* else |
2816 |
< |
* map.remove(key); |
2817 |
< |
* } |
2818 |
< |
* }</pre> |
2819 |
< |
* |
2820 |
< |
* except that the action is performed atomically. If the |
2821 |
< |
* function returns {@code null}, the mapping is removed. If the |
2822 |
< |
* function itself throws an (unchecked) exception, the exception |
2823 |
< |
* is rethrown to its caller, and the current mapping is left |
2824 |
< |
* unchanged. Some attempted update operations on this map by |
2825 |
< |
* other threads may be blocked while computation is in progress, |
2826 |
< |
* so the computation should be short and simple, and must not |
2827 |
< |
* attempt to update any other mappings of this Map. For example, |
2828 |
< |
* to either create or append new messages to a value mapping: |
1749 |
> |
* If the value for the specified key is present, attempts to |
1750 |
> |
* compute a new mapping given the key and its current mapped |
1751 |
> |
* value. The entire method invocation is performed atomically. |
1752 |
> |
* Some attempted update operations on this map by other threads |
1753 |
> |
* may be blocked while computation is in progress, so the |
1754 |
> |
* computation should be short and simple, and must not attempt to |
1755 |
> |
* update any other mappings of this map. |
1756 |
|
* |
1757 |
< |
* @param key key with which the specified value is to be associated |
1757 |
> |
* @param key key with which a value may be associated |
1758 |
|
* @param remappingFunction the function to compute a value |
1759 |
|
* @return the new value associated with the specified key, or null if none |
1760 |
|
* @throws NullPointerException if the specified key or remappingFunction |
1765 |
|
* @throws RuntimeException or Error if the remappingFunction does so, |
1766 |
|
* in which case the mapping is unchanged |
1767 |
|
*/ |
1768 |
< |
@SuppressWarnings("unchecked") public V computeIfPresent |
2842 |
< |
(K key, BiFun<? super K, ? super V, ? extends V> remappingFunction) { |
1768 |
> |
public V computeIfPresent(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) { |
1769 |
|
if (key == null || remappingFunction == null) |
1770 |
|
throw new NullPointerException(); |
1771 |
< |
return (V)internalCompute(key, true, remappingFunction); |
1771 |
> |
int h = spread(key.hashCode()); |
1772 |
> |
V val = null; |
1773 |
> |
int delta = 0; |
1774 |
> |
int binCount = 0; |
1775 |
> |
for (Node<K,V>[] tab = table;;) { |
1776 |
> |
Node<K,V> f; int n, i, fh; |
1777 |
> |
if (tab == null || (n = tab.length) == 0) |
1778 |
> |
tab = initTable(); |
1779 |
> |
else if ((f = tabAt(tab, i = (n - 1) & h)) == null) |
1780 |
> |
break; |
1781 |
> |
else if ((fh = f.hash) == MOVED) |
1782 |
> |
tab = helpTransfer(tab, f); |
1783 |
> |
else { |
1784 |
> |
synchronized (f) { |
1785 |
> |
if (tabAt(tab, i) == f) { |
1786 |
> |
if (fh >= 0) { |
1787 |
> |
binCount = 1; |
1788 |
> |
for (Node<K,V> e = f, pred = null;; ++binCount) { |
1789 |
> |
K ek; |
1790 |
> |
if (e.hash == h && |
1791 |
> |
((ek = e.key) == key || |
1792 |
> |
(ek != null && key.equals(ek)))) { |
1793 |
> |
val = remappingFunction.apply(key, e.val); |
1794 |
> |
if (val != null) |
1795 |
> |
e.val = val; |
1796 |
> |
else { |
1797 |
> |
delta = -1; |
1798 |
> |
Node<K,V> en = e.next; |
1799 |
> |
if (pred != null) |
1800 |
> |
pred.next = en; |
1801 |
> |
else |
1802 |
> |
setTabAt(tab, i, en); |
1803 |
> |
} |
1804 |
> |
break; |
1805 |
> |
} |
1806 |
> |
pred = e; |
1807 |
> |
if ((e = e.next) == null) |
1808 |
> |
break; |
1809 |
> |
} |
1810 |
> |
} |
1811 |
> |
else if (f instanceof TreeBin) { |
1812 |
> |
binCount = 2; |
1813 |
> |
TreeBin<K,V> t = (TreeBin<K,V>)f; |
1814 |
> |
TreeNode<K,V> r, p; |
1815 |
> |
if ((r = t.root) != null && |
1816 |
> |
(p = r.findTreeNode(h, key, null)) != null) { |
1817 |
> |
val = remappingFunction.apply(key, p.val); |
1818 |
> |
if (val != null) |
1819 |
> |
p.val = val; |
1820 |
> |
else { |
1821 |
> |
delta = -1; |
1822 |
> |
if (t.removeTreeNode(p)) |
1823 |
> |
setTabAt(tab, i, untreeify(t.first)); |
1824 |
> |
} |
1825 |
> |
} |
1826 |
> |
} |
1827 |
> |
else if (f instanceof ReservationNode) |
1828 |
> |
throw new IllegalStateException("Recursive update"); |
1829 |
> |
} |
1830 |
> |
} |
1831 |
> |
if (binCount != 0) |
1832 |
> |
break; |
1833 |
> |
} |
1834 |
> |
} |
1835 |
> |
if (delta != 0) |
1836 |
> |
addCount((long)delta, binCount); |
1837 |
> |
return val; |
1838 |
|
} |
1839 |
|
|
1840 |
|
/** |
1841 |
< |
* Computes a new mapping value given a key and |
1842 |
< |
* its current mapped value (or {@code null} if there is no current |
1843 |
< |
* mapping). This is equivalent to |
1844 |
< |
* <pre> {@code |
1845 |
< |
* value = remappingFunction.apply(key, map.get(key)); |
1846 |
< |
* if (value != null) |
1847 |
< |
* map.put(key, value); |
2856 |
< |
* else |
2857 |
< |
* map.remove(key); |
2858 |
< |
* }</pre> |
2859 |
< |
* |
2860 |
< |
* except that the action is performed atomically. If the |
2861 |
< |
* function returns {@code null}, the mapping is removed. If the |
2862 |
< |
* function itself throws an (unchecked) exception, the exception |
2863 |
< |
* is rethrown to its caller, and the current mapping is left |
2864 |
< |
* unchanged. Some attempted update operations on this map by |
2865 |
< |
* other threads may be blocked while computation is in progress, |
2866 |
< |
* so the computation should be short and simple, and must not |
2867 |
< |
* attempt to update any other mappings of this Map. For example, |
2868 |
< |
* to either create or append new messages to a value mapping: |
2869 |
< |
* |
2870 |
< |
* <pre> {@code |
2871 |
< |
* Map<Key, String> map = ...; |
2872 |
< |
* final String msg = ...; |
2873 |
< |
* map.compute(key, new BiFun<Key, String, String>() { |
2874 |
< |
* public String apply(Key k, String v) { |
2875 |
< |
* return (v == null) ? msg : v + msg;});}}</pre> |
1841 |
> |
* Attempts to compute a mapping for the specified key and its |
1842 |
> |
* current mapped value (or {@code null} if there is no current |
1843 |
> |
* mapping). The entire method invocation is performed atomically. |
1844 |
> |
* Some attempted update operations on this map by other threads |
1845 |
> |
* may be blocked while computation is in progress, so the |
1846 |
> |
* computation should be short and simple, and must not attempt to |
1847 |
> |
* update any other mappings of this Map. |
1848 |
|
* |
1849 |
|
* @param key key with which the specified value is to be associated |
1850 |
|
* @param remappingFunction the function to compute a value |
1857 |
|
* @throws RuntimeException or Error if the remappingFunction does so, |
1858 |
|
* in which case the mapping is unchanged |
1859 |
|
*/ |
1860 |
< |
@SuppressWarnings("unchecked") public V compute |
1861 |
< |
(K key, BiFun<? super K, ? super V, ? extends V> remappingFunction) { |
1860 |
> |
public V compute(K key, |
1861 |
> |
BiFunction<? super K, ? super V, ? extends V> remappingFunction) { |
1862 |
|
if (key == null || remappingFunction == null) |
1863 |
|
throw new NullPointerException(); |
1864 |
< |
return (V)internalCompute(key, false, remappingFunction); |
1864 |
> |
int h = spread(key.hashCode()); |
1865 |
> |
V val = null; |
1866 |
> |
int delta = 0; |
1867 |
> |
int binCount = 0; |
1868 |
> |
for (Node<K,V>[] tab = table;;) { |
1869 |
> |
Node<K,V> f; int n, i, fh; |
1870 |
> |
if (tab == null || (n = tab.length) == 0) |
1871 |
> |
tab = initTable(); |
1872 |
> |
else if ((f = tabAt(tab, i = (n - 1) & h)) == null) { |
1873 |
> |
Node<K,V> r = new ReservationNode<K,V>(); |
1874 |
> |
synchronized (r) { |
1875 |
> |
if (casTabAt(tab, i, null, r)) { |
1876 |
> |
binCount = 1; |
1877 |
> |
Node<K,V> node = null; |
1878 |
> |
try { |
1879 |
> |
if ((val = remappingFunction.apply(key, null)) != null) { |
1880 |
> |
delta = 1; |
1881 |
> |
node = new Node<K,V>(h, key, val); |
1882 |
> |
} |
1883 |
> |
} finally { |
1884 |
> |
setTabAt(tab, i, node); |
1885 |
> |
} |
1886 |
> |
} |
1887 |
> |
} |
1888 |
> |
if (binCount != 0) |
1889 |
> |
break; |
1890 |
> |
} |
1891 |
> |
else if ((fh = f.hash) == MOVED) |
1892 |
> |
tab = helpTransfer(tab, f); |
1893 |
> |
else { |
1894 |
> |
synchronized (f) { |
1895 |
> |
if (tabAt(tab, i) == f) { |
1896 |
> |
if (fh >= 0) { |
1897 |
> |
binCount = 1; |
1898 |
> |
for (Node<K,V> e = f, pred = null;; ++binCount) { |
1899 |
> |
K ek; |
1900 |
> |
if (e.hash == h && |
1901 |
> |
((ek = e.key) == key || |
1902 |
> |
(ek != null && key.equals(ek)))) { |
1903 |
> |
val = remappingFunction.apply(key, e.val); |
1904 |
> |
if (val != null) |
1905 |
> |
e.val = val; |
1906 |
> |
else { |
1907 |
> |
delta = -1; |
1908 |
> |
Node<K,V> en = e.next; |
1909 |
> |
if (pred != null) |
1910 |
> |
pred.next = en; |
1911 |
> |
else |
1912 |
> |
setTabAt(tab, i, en); |
1913 |
> |
} |
1914 |
> |
break; |
1915 |
> |
} |
1916 |
> |
pred = e; |
1917 |
> |
if ((e = e.next) == null) { |
1918 |
> |
val = remappingFunction.apply(key, null); |
1919 |
> |
if (val != null) { |
1920 |
> |
if (pred.next != null) |
1921 |
> |
throw new IllegalStateException("Recursive update"); |
1922 |
> |
delta = 1; |
1923 |
> |
pred.next = new Node<K,V>(h, key, val); |
1924 |
> |
} |
1925 |
> |
break; |
1926 |
> |
} |
1927 |
> |
} |
1928 |
> |
} |
1929 |
> |
else if (f instanceof TreeBin) { |
1930 |
> |
binCount = 1; |
1931 |
> |
TreeBin<K,V> t = (TreeBin<K,V>)f; |
1932 |
> |
TreeNode<K,V> r, p; |
1933 |
> |
if ((r = t.root) != null) |
1934 |
> |
p = r.findTreeNode(h, key, null); |
1935 |
> |
else |
1936 |
> |
p = null; |
1937 |
> |
V pv = (p == null) ? null : p.val; |
1938 |
> |
val = remappingFunction.apply(key, pv); |
1939 |
> |
if (val != null) { |
1940 |
> |
if (p != null) |
1941 |
> |
p.val = val; |
1942 |
> |
else { |
1943 |
> |
delta = 1; |
1944 |
> |
t.putTreeVal(h, key, val); |
1945 |
> |
} |
1946 |
> |
} |
1947 |
> |
else if (p != null) { |
1948 |
> |
delta = -1; |
1949 |
> |
if (t.removeTreeNode(p)) |
1950 |
> |
setTabAt(tab, i, untreeify(t.first)); |
1951 |
> |
} |
1952 |
> |
} |
1953 |
> |
else if (f instanceof ReservationNode) |
1954 |
> |
throw new IllegalStateException("Recursive update"); |
1955 |
> |
} |
1956 |
> |
} |
1957 |
> |
if (binCount != 0) { |
1958 |
> |
if (binCount >= TREEIFY_THRESHOLD) |
1959 |
> |
treeifyBin(tab, i); |
1960 |
> |
break; |
1961 |
> |
} |
1962 |
> |
} |
1963 |
> |
} |
1964 |
> |
if (delta != 0) |
1965 |
> |
addCount((long)delta, binCount); |
1966 |
> |
return val; |
1967 |
|
} |
1968 |
|
|
1969 |
|
/** |
1970 |
< |
* If the specified key is not already associated |
1971 |
< |
* with a value, associate it with the given value. |
1972 |
< |
* Otherwise, replace the value with the results of |
1973 |
< |
* the given remapping function. This is equivalent to: |
1974 |
< |
* <pre> {@code |
1975 |
< |
* if (!map.containsKey(key)) |
1976 |
< |
* map.put(value); |
1977 |
< |
* else { |
1978 |
< |
* newValue = remappingFunction.apply(map.get(key), value); |
1979 |
< |
* if (value != null) |
1980 |
< |
* map.put(key, value); |
1981 |
< |
* else |
1982 |
< |
* map.remove(key); |
1983 |
< |
* } |
1984 |
< |
* }</pre> |
1985 |
< |
* except that the action is performed atomically. If the |
1986 |
< |
* function returns {@code null}, the mapping is removed. If the |
1987 |
< |
* function itself throws an (unchecked) exception, the exception |
2914 |
< |
* is rethrown to its caller, and the current mapping is left |
2915 |
< |
* unchanged. Some attempted update operations on this map by |
2916 |
< |
* other threads may be blocked while computation is in progress, |
2917 |
< |
* so the computation should be short and simple, and must not |
2918 |
< |
* attempt to update any other mappings of this Map. |
1970 |
> |
* If the specified key is not already associated with a |
1971 |
> |
* (non-null) value, associates it with the given value. |
1972 |
> |
* Otherwise, replaces the value with the results of the given |
1973 |
> |
* remapping function, or removes if {@code null}. The entire |
1974 |
> |
* method invocation is performed atomically. Some attempted |
1975 |
> |
* update operations on this map by other threads may be blocked |
1976 |
> |
* while computation is in progress, so the computation should be |
1977 |
> |
* short and simple, and must not attempt to update any other |
1978 |
> |
* mappings of this Map. |
1979 |
> |
* |
1980 |
> |
* @param key key with which the specified value is to be associated |
1981 |
> |
* @param value the value to use if absent |
1982 |
> |
* @param remappingFunction the function to recompute a value if present |
1983 |
> |
* @return the new value associated with the specified key, or null if none |
1984 |
> |
* @throws NullPointerException if the specified key or the |
1985 |
> |
* remappingFunction is null |
1986 |
> |
* @throws RuntimeException or Error if the remappingFunction does so, |
1987 |
> |
* in which case the mapping is unchanged |
1988 |
|
*/ |
1989 |
< |
@SuppressWarnings("unchecked") public V merge |
2921 |
< |
(K key, V value, BiFun<? super V, ? super V, ? extends V> remappingFunction) { |
1989 |
> |
public V merge(K key, V value, BiFunction<? super V, ? super V, ? extends V> remappingFunction) { |
1990 |
|
if (key == null || value == null || remappingFunction == null) |
1991 |
|
throw new NullPointerException(); |
1992 |
< |
return (V)internalMerge(key, value, remappingFunction); |
1992 |
> |
int h = spread(key.hashCode()); |
1993 |
> |
V val = null; |
1994 |
> |
int delta = 0; |
1995 |
> |
int binCount = 0; |
1996 |
> |
for (Node<K,V>[] tab = table;;) { |
1997 |
> |
Node<K,V> f; int n, i, fh; |
1998 |
> |
if (tab == null || (n = tab.length) == 0) |
1999 |
> |
tab = initTable(); |
2000 |
> |
else if ((f = tabAt(tab, i = (n - 1) & h)) == null) { |
2001 |
> |
if (casTabAt(tab, i, null, new Node<K,V>(h, key, value))) { |
2002 |
> |
delta = 1; |
2003 |
> |
val = value; |
2004 |
> |
break; |
2005 |
> |
} |
2006 |
> |
} |
2007 |
> |
else if ((fh = f.hash) == MOVED) |
2008 |
> |
tab = helpTransfer(tab, f); |
2009 |
> |
else { |
2010 |
> |
synchronized (f) { |
2011 |
> |
if (tabAt(tab, i) == f) { |
2012 |
> |
if (fh >= 0) { |
2013 |
> |
binCount = 1; |
2014 |
> |
for (Node<K,V> e = f, pred = null;; ++binCount) { |
2015 |
> |
K ek; |
2016 |
> |
if (e.hash == h && |
2017 |
> |
((ek = e.key) == key || |
2018 |
> |
(ek != null && key.equals(ek)))) { |
2019 |
> |
val = remappingFunction.apply(e.val, value); |
2020 |
> |
if (val != null) |
2021 |
> |
e.val = val; |
2022 |
> |
else { |
2023 |
> |
delta = -1; |
2024 |
> |
Node<K,V> en = e.next; |
2025 |
> |
if (pred != null) |
2026 |
> |
pred.next = en; |
2027 |
> |
else |
2028 |
> |
setTabAt(tab, i, en); |
2029 |
> |
} |
2030 |
> |
break; |
2031 |
> |
} |
2032 |
> |
pred = e; |
2033 |
> |
if ((e = e.next) == null) { |
2034 |
> |
delta = 1; |
2035 |
> |
val = value; |
2036 |
> |
pred.next = new Node<K,V>(h, key, val); |
2037 |
> |
break; |
2038 |
> |
} |
2039 |
> |
} |
2040 |
> |
} |
2041 |
> |
else if (f instanceof TreeBin) { |
2042 |
> |
binCount = 2; |
2043 |
> |
TreeBin<K,V> t = (TreeBin<K,V>)f; |
2044 |
> |
TreeNode<K,V> r = t.root; |
2045 |
> |
TreeNode<K,V> p = (r == null) ? null : |
2046 |
> |
r.findTreeNode(h, key, null); |
2047 |
> |
val = (p == null) ? value : |
2048 |
> |
remappingFunction.apply(p.val, value); |
2049 |
> |
if (val != null) { |
2050 |
> |
if (p != null) |
2051 |
> |
p.val = val; |
2052 |
> |
else { |
2053 |
> |
delta = 1; |
2054 |
> |
t.putTreeVal(h, key, val); |
2055 |
> |
} |
2056 |
> |
} |
2057 |
> |
else if (p != null) { |
2058 |
> |
delta = -1; |
2059 |
> |
if (t.removeTreeNode(p)) |
2060 |
> |
setTabAt(tab, i, untreeify(t.first)); |
2061 |
> |
} |
2062 |
> |
} |
2063 |
> |
else if (f instanceof ReservationNode) |
2064 |
> |
throw new IllegalStateException("Recursive update"); |
2065 |
> |
} |
2066 |
> |
} |
2067 |
> |
if (binCount != 0) { |
2068 |
> |
if (binCount >= TREEIFY_THRESHOLD) |
2069 |
> |
treeifyBin(tab, i); |
2070 |
> |
break; |
2071 |
> |
} |
2072 |
> |
} |
2073 |
> |
} |
2074 |
> |
if (delta != 0) |
2075 |
> |
addCount((long)delta, binCount); |
2076 |
> |
return val; |
2077 |
|
} |
2078 |
|
|
2079 |
+ |
// Hashtable legacy methods |
2080 |
+ |
|
2081 |
|
/** |
2082 |
< |
* Removes the key (and its corresponding value) from this map. |
2929 |
< |
* This method does nothing if the key is not in the map. |
2082 |
> |
* Tests if some key maps into the specified value in this table. |
2083 |
|
* |
2084 |
< |
* @param key the key that needs to be removed |
2085 |
< |
* @return the previous value associated with {@code key}, or |
2086 |
< |
* {@code null} if there was no mapping for {@code key} |
2087 |
< |
* @throws NullPointerException if the specified key is null |
2084 |
> |
* <p>Note that this method is identical in functionality to |
2085 |
> |
* {@link #containsValue(Object)}, and exists solely to ensure |
2086 |
> |
* full compatibility with class {@link java.util.Hashtable}, |
2087 |
> |
* which supported this method prior to introduction of the |
2088 |
> |
* Java Collections Framework. |
2089 |
> |
* |
2090 |
> |
* @param value a value to search for |
2091 |
> |
* @return {@code true} if and only if some key maps to the |
2092 |
> |
* {@code value} argument in this table as |
2093 |
> |
* determined by the {@code equals} method; |
2094 |
> |
* {@code false} otherwise |
2095 |
> |
* @throws NullPointerException if the specified value is null |
2096 |
|
*/ |
2097 |
< |
@SuppressWarnings("unchecked") public V remove(Object key) { |
2098 |
< |
if (key == null) |
2938 |
< |
throw new NullPointerException(); |
2939 |
< |
return (V)internalReplace(key, null, null); |
2097 |
> |
public boolean contains(Object value) { |
2098 |
> |
return containsValue(value); |
2099 |
|
} |
2100 |
|
|
2101 |
|
/** |
2102 |
< |
* {@inheritDoc} |
2102 |
> |
* Returns an enumeration of the keys in this table. |
2103 |
|
* |
2104 |
< |
* @throws NullPointerException if the specified key is null |
2104 |
> |
* @return an enumeration of the keys in this table |
2105 |
> |
* @see #keySet() |
2106 |
|
*/ |
2107 |
< |
public boolean remove(Object key, Object value) { |
2108 |
< |
if (key == null) |
2109 |
< |
throw new NullPointerException(); |
2110 |
< |
if (value == null) |
2951 |
< |
return false; |
2952 |
< |
return internalReplace(key, null, value) != null; |
2107 |
> |
public Enumeration<K> keys() { |
2108 |
> |
Node<K,V>[] t; |
2109 |
> |
int f = (t = table) == null ? 0 : t.length; |
2110 |
> |
return new KeyIterator<K,V>(t, f, 0, f, this); |
2111 |
|
} |
2112 |
|
|
2113 |
|
/** |
2114 |
< |
* {@inheritDoc} |
2114 |
> |
* Returns an enumeration of the values in this table. |
2115 |
|
* |
2116 |
< |
* @throws NullPointerException if any of the arguments are null |
2116 |
> |
* @return an enumeration of the values in this table |
2117 |
> |
* @see #values() |
2118 |
|
*/ |
2119 |
< |
public boolean replace(K key, V oldValue, V newValue) { |
2120 |
< |
if (key == null || oldValue == null || newValue == null) |
2121 |
< |
throw new NullPointerException(); |
2122 |
< |
return internalReplace(key, newValue, oldValue) != null; |
2119 |
> |
public Enumeration<V> elements() { |
2120 |
> |
Node<K,V>[] t; |
2121 |
> |
int f = (t = table) == null ? 0 : t.length; |
2122 |
> |
return new ValueIterator<K,V>(t, f, 0, f, this); |
2123 |
|
} |
2124 |
|
|
2125 |
+ |
// ConcurrentHashMap-only methods |
2126 |
+ |
|
2127 |
|
/** |
2128 |
< |
* {@inheritDoc} |
2128 |
> |
* Returns the number of mappings. This method should be used |
2129 |
> |
* instead of {@link #size} because a ConcurrentHashMap may |
2130 |
> |
* contain more mappings than can be represented as an int. The |
2131 |
> |
* value returned is an estimate; the actual count may differ if |
2132 |
> |
* there are concurrent insertions or removals. |
2133 |
|
* |
2134 |
< |
* @return the previous value associated with the specified key, |
2135 |
< |
* or {@code null} if there was no mapping for the key |
2971 |
< |
* @throws NullPointerException if the specified key or value is null |
2134 |
> |
* @return the number of mappings |
2135 |
> |
* @since 1.8 |
2136 |
|
*/ |
2137 |
< |
@SuppressWarnings("unchecked") public V replace(K key, V value) { |
2138 |
< |
if (key == null || value == null) |
2139 |
< |
throw new NullPointerException(); |
2976 |
< |
return (V)internalReplace(key, value, null); |
2137 |
> |
public long mappingCount() { |
2138 |
> |
long n = sumCount(); |
2139 |
> |
return (n < 0L) ? 0L : n; // ignore transient negative values |
2140 |
|
} |
2141 |
|
|
2142 |
|
/** |
2143 |
< |
* Removes all of the mappings from this map. |
2143 |
> |
* Creates a new {@link Set} backed by a ConcurrentHashMap |
2144 |
> |
* from the given type to {@code Boolean.TRUE}. |
2145 |
> |
* |
2146 |
> |
* @param <K> the element type of the returned set |
2147 |
> |
* @return the new set |
2148 |
> |
* @since 1.8 |
2149 |
|
*/ |
2150 |
< |
public void clear() { |
2151 |
< |
internalClear(); |
2150 |
> |
public static <K> KeySetView<K,Boolean> newKeySet() { |
2151 |
> |
return new KeySetView<K,Boolean> |
2152 |
> |
(new ConcurrentHashMap<K,Boolean>(), Boolean.TRUE); |
2153 |
|
} |
2154 |
|
|
2155 |
|
/** |
2156 |
< |
* Returns a {@link Set} view of the keys contained in this map. |
2157 |
< |
* The set is backed by the map, so changes to the map are |
2989 |
< |
* reflected in the set, and vice-versa. |
2156 |
> |
* Creates a new {@link Set} backed by a ConcurrentHashMap |
2157 |
> |
* from the given type to {@code Boolean.TRUE}. |
2158 |
|
* |
2159 |
< |
* @return the set view |
2159 |
> |
* @param initialCapacity The implementation performs internal |
2160 |
> |
* sizing to accommodate this many elements. |
2161 |
> |
* @param <K> the element type of the returned set |
2162 |
> |
* @return the new set |
2163 |
> |
* @throws IllegalArgumentException if the initial capacity of |
2164 |
> |
* elements is negative |
2165 |
> |
* @since 1.8 |
2166 |
|
*/ |
2167 |
< |
public KeySetView<K,V> keySet() { |
2168 |
< |
KeySetView<K,V> ks = keySet; |
2169 |
< |
return (ks != null) ? ks : (keySet = new KeySetView<K,V>(this, null)); |
2167 |
> |
public static <K> KeySetView<K,Boolean> newKeySet(int initialCapacity) { |
2168 |
> |
return new KeySetView<K,Boolean> |
2169 |
> |
(new ConcurrentHashMap<K,Boolean>(initialCapacity), Boolean.TRUE); |
2170 |
|
} |
2171 |
|
|
2172 |
|
/** |
2173 |
|
* Returns a {@link Set} view of the keys in this map, using the |
2174 |
|
* given common mapped value for any additions (i.e., {@link |
2175 |
< |
* Collection#add} and {@link Collection#addAll}). This is of |
2176 |
< |
* course only appropriate if it is acceptable to use the same |
2177 |
< |
* value for all additions from this view. |
2175 |
> |
* Collection#add} and {@link Collection#addAll(Collection)}). |
2176 |
> |
* This is of course only appropriate if it is acceptable to use |
2177 |
> |
* the same value for all additions from this view. |
2178 |
|
* |
2179 |
< |
* @param mappedValue the mapped value to use for any |
3006 |
< |
* additions. |
2179 |
> |
* @param mappedValue the mapped value to use for any additions |
2180 |
|
* @return the set view |
2181 |
|
* @throws NullPointerException if the mappedValue is null |
2182 |
|
*/ |
2186 |
|
return new KeySetView<K,V>(this, mappedValue); |
2187 |
|
} |
2188 |
|
|
2189 |
+ |
/* ---------------- Special Nodes -------------- */ |
2190 |
+ |
|
2191 |
|
/** |
2192 |
< |
* Returns a {@link Collection} view of the values contained in this map. |
3018 |
< |
* The collection is backed by the map, so changes to the map are |
3019 |
< |
* reflected in the collection, and vice-versa. |
2192 |
> |
* A node inserted at head of bins during transfer operations. |
2193 |
|
*/ |
2194 |
< |
public ValuesView<K,V> values() { |
2195 |
< |
ValuesView<K,V> vs = values; |
2196 |
< |
return (vs != null) ? vs : (values = new ValuesView<K,V>(this)); |
2194 |
> |
static final class ForwardingNode<K,V> extends Node<K,V> { |
2195 |
> |
final Node<K,V>[] nextTable; |
2196 |
> |
ForwardingNode(Node<K,V>[] tab) { |
2197 |
> |
super(MOVED, null, null); |
2198 |
> |
this.nextTable = tab; |
2199 |
> |
} |
2200 |
> |
|
2201 |
> |
Node<K,V> find(int h, Object k) { |
2202 |
> |
// loop to avoid arbitrarily deep recursion on forwarding nodes |
2203 |
> |
outer: for (Node<K,V>[] tab = nextTable;;) { |
2204 |
> |
Node<K,V> e; int n; |
2205 |
> |
if (k == null || tab == null || (n = tab.length) == 0 || |
2206 |
> |
(e = tabAt(tab, (n - 1) & h)) == null) |
2207 |
> |
return null; |
2208 |
> |
for (;;) { |
2209 |
> |
int eh; K ek; |
2210 |
> |
if ((eh = e.hash) == h && |
2211 |
> |
((ek = e.key) == k || (ek != null && k.equals(ek)))) |
2212 |
> |
return e; |
2213 |
> |
if (eh < 0) { |
2214 |
> |
if (e instanceof ForwardingNode) { |
2215 |
> |
tab = ((ForwardingNode<K,V>)e).nextTable; |
2216 |
> |
continue outer; |
2217 |
> |
} |
2218 |
> |
else |
2219 |
> |
return e.find(h, k); |
2220 |
> |
} |
2221 |
> |
if ((e = e.next) == null) |
2222 |
> |
return null; |
2223 |
> |
} |
2224 |
> |
} |
2225 |
> |
} |
2226 |
|
} |
2227 |
|
|
2228 |
|
/** |
2229 |
< |
* Returns a {@link Set} view of the mappings contained in this map. |
3028 |
< |
* The set is backed by the map, so changes to the map are |
3029 |
< |
* reflected in the set, and vice-versa. The set supports element |
3030 |
< |
* removal, which removes the corresponding mapping from the map, |
3031 |
< |
* via the {@code Iterator.remove}, {@code Set.remove}, |
3032 |
< |
* {@code removeAll}, {@code retainAll}, and {@code clear} |
3033 |
< |
* operations. It does not support the {@code add} or |
3034 |
< |
* {@code addAll} operations. |
3035 |
< |
* |
3036 |
< |
* <p>The view's {@code iterator} is a "weakly consistent" iterator |
3037 |
< |
* that will never throw {@link ConcurrentModificationException}, |
3038 |
< |
* and guarantees to traverse elements as they existed upon |
3039 |
< |
* construction of the iterator, and may (but is not guaranteed to) |
3040 |
< |
* reflect any modifications subsequent to construction. |
2229 |
> |
* A place-holder node used in computeIfAbsent and compute. |
2230 |
|
*/ |
2231 |
< |
public Set<Map.Entry<K,V>> entrySet() { |
2232 |
< |
EntrySetView<K,V> es = entrySet; |
2233 |
< |
return (es != null) ? es : (entrySet = new EntrySetView<K,V>(this)); |
2231 |
> |
static final class ReservationNode<K,V> extends Node<K,V> { |
2232 |
> |
ReservationNode() { |
2233 |
> |
super(RESERVED, null, null); |
2234 |
> |
} |
2235 |
> |
|
2236 |
> |
Node<K,V> find(int h, Object k) { |
2237 |
> |
return null; |
2238 |
> |
} |
2239 |
|
} |
2240 |
|
|
2241 |
+ |
/* ---------------- Table Initialization and Resizing -------------- */ |
2242 |
+ |
|
2243 |
|
/** |
2244 |
< |
* Returns an enumeration of the keys in this table. |
2245 |
< |
* |
3050 |
< |
* @return an enumeration of the keys in this table |
3051 |
< |
* @see #keySet() |
2244 |
> |
* Returns the stamp bits for resizing a table of size n. |
2245 |
> |
* Must be negative when shifted left by RESIZE_STAMP_SHIFT. |
2246 |
|
*/ |
2247 |
< |
public Enumeration<K> keys() { |
2248 |
< |
return new KeyIterator<K,V>(this); |
2247 |
> |
static final int resizeStamp(int n) { |
2248 |
> |
return Integer.numberOfLeadingZeros(n) | (1 << (RESIZE_STAMP_BITS - 1)); |
2249 |
|
} |
2250 |
|
|
2251 |
|
/** |
2252 |
< |
* Returns an enumeration of the values in this table. |
3059 |
< |
* |
3060 |
< |
* @return an enumeration of the values in this table |
3061 |
< |
* @see #values() |
2252 |
> |
* Initializes table, using the size recorded in sizeCtl. |
2253 |
|
*/ |
2254 |
< |
public Enumeration<V> elements() { |
2255 |
< |
return new ValueIterator<K,V>(this); |
2254 |
> |
private final Node<K,V>[] initTable() { |
2255 |
> |
Node<K,V>[] tab; int sc; |
2256 |
> |
while ((tab = table) == null || tab.length == 0) { |
2257 |
> |
if ((sc = sizeCtl) < 0) |
2258 |
> |
Thread.yield(); // lost initialization race; just spin |
2259 |
> |
else if (U.compareAndSetInt(this, SIZECTL, sc, -1)) { |
2260 |
> |
try { |
2261 |
> |
if ((tab = table) == null || tab.length == 0) { |
2262 |
> |
int n = (sc > 0) ? sc : DEFAULT_CAPACITY; |
2263 |
> |
@SuppressWarnings("unchecked") |
2264 |
> |
Node<K,V>[] nt = (Node<K,V>[])new Node<?,?>[n]; |
2265 |
> |
table = tab = nt; |
2266 |
> |
sc = n - (n >>> 2); |
2267 |
> |
} |
2268 |
> |
} finally { |
2269 |
> |
sizeCtl = sc; |
2270 |
> |
} |
2271 |
> |
break; |
2272 |
> |
} |
2273 |
> |
} |
2274 |
> |
return tab; |
2275 |
|
} |
2276 |
|
|
2277 |
|
/** |
2278 |
< |
* Returns a partitionable iterator of the keys in this map. |
2279 |
< |
* |
2280 |
< |
* @return a partitionable iterator of the keys in this map |
2278 |
> |
* Adds to count, and if table is too small and not already |
2279 |
> |
* resizing, initiates transfer. If already resizing, helps |
2280 |
> |
* perform transfer if work is available. Rechecks occupancy |
2281 |
> |
* after a transfer to see if another resize is already needed |
2282 |
> |
* because resizings are lagging additions. |
2283 |
> |
* |
2284 |
> |
* @param x the count to add |
2285 |
> |
* @param check if <0, don't check resize, if <= 1 only check if uncontended |
2286 |
> |
*/ |
2287 |
> |
private final void addCount(long x, int check) { |
2288 |
> |
CounterCell[] cs; long b, s; |
2289 |
> |
if ((cs = counterCells) != null || |
2290 |
> |
!U.compareAndSetLong(this, BASECOUNT, b = baseCount, s = b + x)) { |
2291 |
> |
CounterCell c; long v; int m; |
2292 |
> |
boolean uncontended = true; |
2293 |
> |
if (cs == null || (m = cs.length - 1) < 0 || |
2294 |
> |
(c = cs[ThreadLocalRandom.getProbe() & m]) == null || |
2295 |
> |
!(uncontended = |
2296 |
> |
U.compareAndSetLong(c, CELLVALUE, v = c.value, v + x))) { |
2297 |
> |
fullAddCount(x, uncontended); |
2298 |
> |
return; |
2299 |
> |
} |
2300 |
> |
if (check <= 1) |
2301 |
> |
return; |
2302 |
> |
s = sumCount(); |
2303 |
> |
} |
2304 |
> |
if (check >= 0) { |
2305 |
> |
Node<K,V>[] tab, nt; int n, sc; |
2306 |
> |
while (s >= (long)(sc = sizeCtl) && (tab = table) != null && |
2307 |
> |
(n = tab.length) < MAXIMUM_CAPACITY) { |
2308 |
> |
int rs = resizeStamp(n); |
2309 |
> |
if (sc < 0) { |
2310 |
> |
if ((sc >>> RESIZE_STAMP_SHIFT) != rs || sc == rs + 1 || |
2311 |
> |
sc == rs + MAX_RESIZERS || (nt = nextTable) == null || |
2312 |
> |
transferIndex <= 0) |
2313 |
> |
break; |
2314 |
> |
if (U.compareAndSetInt(this, SIZECTL, sc, sc + 1)) |
2315 |
> |
transfer(tab, nt); |
2316 |
> |
} |
2317 |
> |
else if (U.compareAndSetInt(this, SIZECTL, sc, |
2318 |
> |
(rs << RESIZE_STAMP_SHIFT) + 2)) |
2319 |
> |
transfer(tab, null); |
2320 |
> |
s = sumCount(); |
2321 |
> |
} |
2322 |
> |
} |
2323 |
> |
} |
2324 |
> |
|
2325 |
> |
/** |
2326 |
> |
* Helps transfer if a resize is in progress. |
2327 |
|
*/ |
2328 |
< |
public Spliterator<K> keySpliterator() { |
2329 |
< |
return new KeyIterator<K,V>(this); |
2328 |
> |
final Node<K,V>[] helpTransfer(Node<K,V>[] tab, Node<K,V> f) { |
2329 |
> |
Node<K,V>[] nextTab; int sc; |
2330 |
> |
if (tab != null && (f instanceof ForwardingNode) && |
2331 |
> |
(nextTab = ((ForwardingNode<K,V>)f).nextTable) != null) { |
2332 |
> |
int rs = resizeStamp(tab.length); |
2333 |
> |
while (nextTab == nextTable && table == tab && |
2334 |
> |
(sc = sizeCtl) < 0) { |
2335 |
> |
if ((sc >>> RESIZE_STAMP_SHIFT) != rs || sc == rs + 1 || |
2336 |
> |
sc == rs + MAX_RESIZERS || transferIndex <= 0) |
2337 |
> |
break; |
2338 |
> |
if (U.compareAndSetInt(this, SIZECTL, sc, sc + 1)) { |
2339 |
> |
transfer(tab, nextTab); |
2340 |
> |
break; |
2341 |
> |
} |
2342 |
> |
} |
2343 |
> |
return nextTab; |
2344 |
> |
} |
2345 |
> |
return table; |
2346 |
|
} |
2347 |
|
|
2348 |
|
/** |
2349 |
< |
* Returns a partitionable iterator of the values in this map. |
2349 |
> |
* Tries to presize table to accommodate the given number of elements. |
2350 |
|
* |
2351 |
< |
* @return a partitionable iterator of the values in this map |
2351 |
> |
* @param size number of elements (doesn't need to be perfectly accurate) |
2352 |
|
*/ |
2353 |
< |
public Spliterator<V> valueSpliterator() { |
2354 |
< |
return new ValueIterator<K,V>(this); |
2353 |
> |
private final void tryPresize(int size) { |
2354 |
> |
int c = (size >= (MAXIMUM_CAPACITY >>> 1)) ? MAXIMUM_CAPACITY : |
2355 |
> |
tableSizeFor(size + (size >>> 1) + 1); |
2356 |
> |
int sc; |
2357 |
> |
while ((sc = sizeCtl) >= 0) { |
2358 |
> |
Node<K,V>[] tab = table; int n; |
2359 |
> |
if (tab == null || (n = tab.length) == 0) { |
2360 |
> |
n = (sc > c) ? sc : c; |
2361 |
> |
if (U.compareAndSetInt(this, SIZECTL, sc, -1)) { |
2362 |
> |
try { |
2363 |
> |
if (table == tab) { |
2364 |
> |
@SuppressWarnings("unchecked") |
2365 |
> |
Node<K,V>[] nt = (Node<K,V>[])new Node<?,?>[n]; |
2366 |
> |
table = nt; |
2367 |
> |
sc = n - (n >>> 2); |
2368 |
> |
} |
2369 |
> |
} finally { |
2370 |
> |
sizeCtl = sc; |
2371 |
> |
} |
2372 |
> |
} |
2373 |
> |
} |
2374 |
> |
else if (c <= sc || n >= MAXIMUM_CAPACITY) |
2375 |
> |
break; |
2376 |
> |
else if (tab == table) { |
2377 |
> |
int rs = resizeStamp(n); |
2378 |
> |
if (U.compareAndSetInt(this, SIZECTL, sc, |
2379 |
> |
(rs << RESIZE_STAMP_SHIFT) + 2)) |
2380 |
> |
transfer(tab, null); |
2381 |
> |
} |
2382 |
> |
} |
2383 |
|
} |
2384 |
|
|
2385 |
|
/** |
2386 |
< |
* Returns a partitionable iterator of the entries in this map. |
2387 |
< |
* |
2388 |
< |
* @return a partitionable iterator of the entries in this map |
2386 |
> |
* Moves and/or copies the nodes in each bin to new table. See |
2387 |
> |
* above for explanation. |
2388 |
> |
*/ |
2389 |
> |
private final void transfer(Node<K,V>[] tab, Node<K,V>[] nextTab) { |
2390 |
> |
int n = tab.length, stride; |
2391 |
> |
if ((stride = (NCPU > 1) ? (n >>> 3) / NCPU : n) < MIN_TRANSFER_STRIDE) |
2392 |
> |
stride = MIN_TRANSFER_STRIDE; // subdivide range |
2393 |
> |
if (nextTab == null) { // initiating |
2394 |
> |
try { |
2395 |
> |
@SuppressWarnings("unchecked") |
2396 |
> |
Node<K,V>[] nt = (Node<K,V>[])new Node<?,?>[n << 1]; |
2397 |
> |
nextTab = nt; |
2398 |
> |
} catch (Throwable ex) { // try to cope with OOME |
2399 |
> |
sizeCtl = Integer.MAX_VALUE; |
2400 |
> |
return; |
2401 |
> |
} |
2402 |
> |
nextTable = nextTab; |
2403 |
> |
transferIndex = n; |
2404 |
> |
} |
2405 |
> |
int nextn = nextTab.length; |
2406 |
> |
ForwardingNode<K,V> fwd = new ForwardingNode<K,V>(nextTab); |
2407 |
> |
boolean advance = true; |
2408 |
> |
boolean finishing = false; // to ensure sweep before committing nextTab |
2409 |
> |
for (int i = 0, bound = 0;;) { |
2410 |
> |
Node<K,V> f; int fh; |
2411 |
> |
while (advance) { |
2412 |
> |
int nextIndex, nextBound; |
2413 |
> |
if (--i >= bound || finishing) |
2414 |
> |
advance = false; |
2415 |
> |
else if ((nextIndex = transferIndex) <= 0) { |
2416 |
> |
i = -1; |
2417 |
> |
advance = false; |
2418 |
> |
} |
2419 |
> |
else if (U.compareAndSetInt |
2420 |
> |
(this, TRANSFERINDEX, nextIndex, |
2421 |
> |
nextBound = (nextIndex > stride ? |
2422 |
> |
nextIndex - stride : 0))) { |
2423 |
> |
bound = nextBound; |
2424 |
> |
i = nextIndex - 1; |
2425 |
> |
advance = false; |
2426 |
> |
} |
2427 |
> |
} |
2428 |
> |
if (i < 0 || i >= n || i + n >= nextn) { |
2429 |
> |
int sc; |
2430 |
> |
if (finishing) { |
2431 |
> |
nextTable = null; |
2432 |
> |
table = nextTab; |
2433 |
> |
sizeCtl = (n << 1) - (n >>> 1); |
2434 |
> |
return; |
2435 |
> |
} |
2436 |
> |
if (U.compareAndSetInt(this, SIZECTL, sc = sizeCtl, sc - 1)) { |
2437 |
> |
if ((sc - 2) != resizeStamp(n) << RESIZE_STAMP_SHIFT) |
2438 |
> |
return; |
2439 |
> |
finishing = advance = true; |
2440 |
> |
i = n; // recheck before commit |
2441 |
> |
} |
2442 |
> |
} |
2443 |
> |
else if ((f = tabAt(tab, i)) == null) |
2444 |
> |
advance = casTabAt(tab, i, null, fwd); |
2445 |
> |
else if ((fh = f.hash) == MOVED) |
2446 |
> |
advance = true; // already processed |
2447 |
> |
else { |
2448 |
> |
synchronized (f) { |
2449 |
> |
if (tabAt(tab, i) == f) { |
2450 |
> |
Node<K,V> ln, hn; |
2451 |
> |
if (fh >= 0) { |
2452 |
> |
int runBit = fh & n; |
2453 |
> |
Node<K,V> lastRun = f; |
2454 |
> |
for (Node<K,V> p = f.next; p != null; p = p.next) { |
2455 |
> |
int b = p.hash & n; |
2456 |
> |
if (b != runBit) { |
2457 |
> |
runBit = b; |
2458 |
> |
lastRun = p; |
2459 |
> |
} |
2460 |
> |
} |
2461 |
> |
if (runBit == 0) { |
2462 |
> |
ln = lastRun; |
2463 |
> |
hn = null; |
2464 |
> |
} |
2465 |
> |
else { |
2466 |
> |
hn = lastRun; |
2467 |
> |
ln = null; |
2468 |
> |
} |
2469 |
> |
for (Node<K,V> p = f; p != lastRun; p = p.next) { |
2470 |
> |
int ph = p.hash; K pk = p.key; V pv = p.val; |
2471 |
> |
if ((ph & n) == 0) |
2472 |
> |
ln = new Node<K,V>(ph, pk, pv, ln); |
2473 |
> |
else |
2474 |
> |
hn = new Node<K,V>(ph, pk, pv, hn); |
2475 |
> |
} |
2476 |
> |
setTabAt(nextTab, i, ln); |
2477 |
> |
setTabAt(nextTab, i + n, hn); |
2478 |
> |
setTabAt(tab, i, fwd); |
2479 |
> |
advance = true; |
2480 |
> |
} |
2481 |
> |
else if (f instanceof TreeBin) { |
2482 |
> |
TreeBin<K,V> t = (TreeBin<K,V>)f; |
2483 |
> |
TreeNode<K,V> lo = null, loTail = null; |
2484 |
> |
TreeNode<K,V> hi = null, hiTail = null; |
2485 |
> |
int lc = 0, hc = 0; |
2486 |
> |
for (Node<K,V> e = t.first; e != null; e = e.next) { |
2487 |
> |
int h = e.hash; |
2488 |
> |
TreeNode<K,V> p = new TreeNode<K,V> |
2489 |
> |
(h, e.key, e.val, null, null); |
2490 |
> |
if ((h & n) == 0) { |
2491 |
> |
if ((p.prev = loTail) == null) |
2492 |
> |
lo = p; |
2493 |
> |
else |
2494 |
> |
loTail.next = p; |
2495 |
> |
loTail = p; |
2496 |
> |
++lc; |
2497 |
> |
} |
2498 |
> |
else { |
2499 |
> |
if ((p.prev = hiTail) == null) |
2500 |
> |
hi = p; |
2501 |
> |
else |
2502 |
> |
hiTail.next = p; |
2503 |
> |
hiTail = p; |
2504 |
> |
++hc; |
2505 |
> |
} |
2506 |
> |
} |
2507 |
> |
ln = (lc <= UNTREEIFY_THRESHOLD) ? untreeify(lo) : |
2508 |
> |
(hc != 0) ? new TreeBin<K,V>(lo) : t; |
2509 |
> |
hn = (hc <= UNTREEIFY_THRESHOLD) ? untreeify(hi) : |
2510 |
> |
(lc != 0) ? new TreeBin<K,V>(hi) : t; |
2511 |
> |
setTabAt(nextTab, i, ln); |
2512 |
> |
setTabAt(nextTab, i + n, hn); |
2513 |
> |
setTabAt(tab, i, fwd); |
2514 |
> |
advance = true; |
2515 |
> |
} |
2516 |
> |
else if (f instanceof ReservationNode) |
2517 |
> |
throw new IllegalStateException("Recursive update"); |
2518 |
> |
} |
2519 |
> |
} |
2520 |
> |
} |
2521 |
> |
} |
2522 |
> |
} |
2523 |
> |
|
2524 |
> |
/* ---------------- Counter support -------------- */ |
2525 |
> |
|
2526 |
> |
/** |
2527 |
> |
* A padded cell for distributing counts. Adapted from LongAdder |
2528 |
> |
* and Striped64. See their internal docs for explanation. |
2529 |
|
*/ |
2530 |
< |
public Spliterator<Map.Entry<K,V>> entrySpliterator() { |
2531 |
< |
return new EntryIterator<K,V>(this); |
2530 |
> |
@jdk.internal.vm.annotation.Contended static final class CounterCell { |
2531 |
> |
volatile long value; |
2532 |
> |
CounterCell(long x) { value = x; } |
2533 |
> |
} |
2534 |
> |
|
2535 |
> |
final long sumCount() { |
2536 |
> |
CounterCell[] cs = counterCells; |
2537 |
> |
long sum = baseCount; |
2538 |
> |
if (cs != null) { |
2539 |
> |
for (CounterCell c : cs) |
2540 |
> |
if (c != null) |
2541 |
> |
sum += c.value; |
2542 |
> |
} |
2543 |
> |
return sum; |
2544 |
> |
} |
2545 |
> |
|
2546 |
> |
// See LongAdder version for explanation |
2547 |
> |
private final void fullAddCount(long x, boolean wasUncontended) { |
2548 |
> |
int h; |
2549 |
> |
if ((h = ThreadLocalRandom.getProbe()) == 0) { |
2550 |
> |
ThreadLocalRandom.localInit(); // force initialization |
2551 |
> |
h = ThreadLocalRandom.getProbe(); |
2552 |
> |
wasUncontended = true; |
2553 |
> |
} |
2554 |
> |
boolean collide = false; // True if last slot nonempty |
2555 |
> |
for (;;) { |
2556 |
> |
CounterCell[] cs; CounterCell c; int n; long v; |
2557 |
> |
if ((cs = counterCells) != null && (n = cs.length) > 0) { |
2558 |
> |
if ((c = cs[(n - 1) & h]) == null) { |
2559 |
> |
if (cellsBusy == 0) { // Try to attach new Cell |
2560 |
> |
CounterCell r = new CounterCell(x); // Optimistic create |
2561 |
> |
if (cellsBusy == 0 && |
2562 |
> |
U.compareAndSetInt(this, CELLSBUSY, 0, 1)) { |
2563 |
> |
boolean created = false; |
2564 |
> |
try { // Recheck under lock |
2565 |
> |
CounterCell[] rs; int m, j; |
2566 |
> |
if ((rs = counterCells) != null && |
2567 |
> |
(m = rs.length) > 0 && |
2568 |
> |
rs[j = (m - 1) & h] == null) { |
2569 |
> |
rs[j] = r; |
2570 |
> |
created = true; |
2571 |
> |
} |
2572 |
> |
} finally { |
2573 |
> |
cellsBusy = 0; |
2574 |
> |
} |
2575 |
> |
if (created) |
2576 |
> |
break; |
2577 |
> |
continue; // Slot is now non-empty |
2578 |
> |
} |
2579 |
> |
} |
2580 |
> |
collide = false; |
2581 |
> |
} |
2582 |
> |
else if (!wasUncontended) // CAS already known to fail |
2583 |
> |
wasUncontended = true; // Continue after rehash |
2584 |
> |
else if (U.compareAndSetLong(c, CELLVALUE, v = c.value, v + x)) |
2585 |
> |
break; |
2586 |
> |
else if (counterCells != cs || n >= NCPU) |
2587 |
> |
collide = false; // At max size or stale |
2588 |
> |
else if (!collide) |
2589 |
> |
collide = true; |
2590 |
> |
else if (cellsBusy == 0 && |
2591 |
> |
U.compareAndSetInt(this, CELLSBUSY, 0, 1)) { |
2592 |
> |
try { |
2593 |
> |
if (counterCells == cs) // Expand table unless stale |
2594 |
> |
counterCells = Arrays.copyOf(cs, n << 1); |
2595 |
> |
} finally { |
2596 |
> |
cellsBusy = 0; |
2597 |
> |
} |
2598 |
> |
collide = false; |
2599 |
> |
continue; // Retry with expanded table |
2600 |
> |
} |
2601 |
> |
h = ThreadLocalRandom.advanceProbe(h); |
2602 |
> |
} |
2603 |
> |
else if (cellsBusy == 0 && counterCells == cs && |
2604 |
> |
U.compareAndSetInt(this, CELLSBUSY, 0, 1)) { |
2605 |
> |
boolean init = false; |
2606 |
> |
try { // Initialize table |
2607 |
> |
if (counterCells == cs) { |
2608 |
> |
CounterCell[] rs = new CounterCell[2]; |
2609 |
> |
rs[h & 1] = new CounterCell(x); |
2610 |
> |
counterCells = rs; |
2611 |
> |
init = true; |
2612 |
> |
} |
2613 |
> |
} finally { |
2614 |
> |
cellsBusy = 0; |
2615 |
> |
} |
2616 |
> |
if (init) |
2617 |
> |
break; |
2618 |
> |
} |
2619 |
> |
else if (U.compareAndSetLong(this, BASECOUNT, v = baseCount, v + x)) |
2620 |
> |
break; // Fall back on using base |
2621 |
> |
} |
2622 |
|
} |
2623 |
|
|
2624 |
+ |
/* ---------------- Conversion from/to TreeBins -------------- */ |
2625 |
+ |
|
2626 |
|
/** |
2627 |
< |
* Returns the hash code value for this {@link Map}, i.e., |
2628 |
< |
* the sum of, for each key-value pair in the map, |
2629 |
< |
* {@code key.hashCode() ^ value.hashCode()}. |
2630 |
< |
* |
2631 |
< |
* @return the hash code value for this map |
2627 |
> |
* Replaces all linked nodes in bin at given index unless table is |
2628 |
> |
* too small, in which case resizes instead. |
2629 |
> |
*/ |
2630 |
> |
private final void treeifyBin(Node<K,V>[] tab, int index) { |
2631 |
> |
Node<K,V> b; int n; |
2632 |
> |
if (tab != null) { |
2633 |
> |
if ((n = tab.length) < MIN_TREEIFY_CAPACITY) |
2634 |
> |
tryPresize(n << 1); |
2635 |
> |
else if ((b = tabAt(tab, index)) != null && b.hash >= 0) { |
2636 |
> |
synchronized (b) { |
2637 |
> |
if (tabAt(tab, index) == b) { |
2638 |
> |
TreeNode<K,V> hd = null, tl = null; |
2639 |
> |
for (Node<K,V> e = b; e != null; e = e.next) { |
2640 |
> |
TreeNode<K,V> p = |
2641 |
> |
new TreeNode<K,V>(e.hash, e.key, e.val, |
2642 |
> |
null, null); |
2643 |
> |
if ((p.prev = tl) == null) |
2644 |
> |
hd = p; |
2645 |
> |
else |
2646 |
> |
tl.next = p; |
2647 |
> |
tl = p; |
2648 |
> |
} |
2649 |
> |
setTabAt(tab, index, new TreeBin<K,V>(hd)); |
2650 |
> |
} |
2651 |
> |
} |
2652 |
> |
} |
2653 |
> |
} |
2654 |
> |
} |
2655 |
> |
|
2656 |
> |
/** |
2657 |
> |
* Returns a list of non-TreeNodes replacing those in given list. |
2658 |
|
*/ |
2659 |
< |
public int hashCode() { |
2660 |
< |
int h = 0; |
2661 |
< |
Traverser<K,V,Object> it = new Traverser<K,V,Object>(this); |
2662 |
< |
Object v; |
2663 |
< |
while ((v = it.advance()) != null) { |
2664 |
< |
h += it.nextKey.hashCode() ^ v.hashCode(); |
2659 |
> |
static <K,V> Node<K,V> untreeify(Node<K,V> b) { |
2660 |
> |
Node<K,V> hd = null, tl = null; |
2661 |
> |
for (Node<K,V> q = b; q != null; q = q.next) { |
2662 |
> |
Node<K,V> p = new Node<K,V>(q.hash, q.key, q.val); |
2663 |
> |
if (tl == null) |
2664 |
> |
hd = p; |
2665 |
> |
else |
2666 |
> |
tl.next = p; |
2667 |
> |
tl = p; |
2668 |
|
} |
2669 |
< |
return h; |
2669 |
> |
return hd; |
2670 |
|
} |
2671 |
|
|
2672 |
+ |
/* ---------------- TreeNodes -------------- */ |
2673 |
+ |
|
2674 |
|
/** |
2675 |
< |
* Returns a string representation of this map. The string |
3113 |
< |
* representation consists of a list of key-value mappings (in no |
3114 |
< |
* particular order) enclosed in braces ("{@code {}}"). Adjacent |
3115 |
< |
* mappings are separated by the characters {@code ", "} (comma |
3116 |
< |
* and space). Each key-value mapping is rendered as the key |
3117 |
< |
* followed by an equals sign ("{@code =}") followed by the |
3118 |
< |
* associated value. |
3119 |
< |
* |
3120 |
< |
* @return a string representation of this map |
2675 |
> |
* Nodes for use in TreeBins. |
2676 |
|
*/ |
2677 |
< |
public String toString() { |
2678 |
< |
Traverser<K,V,Object> it = new Traverser<K,V,Object>(this); |
2679 |
< |
StringBuilder sb = new StringBuilder(); |
2680 |
< |
sb.append('{'); |
2681 |
< |
Object v; |
2682 |
< |
if ((v = it.advance()) != null) { |
2683 |
< |
for (;;) { |
2684 |
< |
Object k = it.nextKey; |
2685 |
< |
sb.append(k == this ? "(this Map)" : k); |
2686 |
< |
sb.append('='); |
2687 |
< |
sb.append(v == this ? "(this Map)" : v); |
2688 |
< |
if ((v = it.advance()) == null) |
2677 |
> |
static final class TreeNode<K,V> extends Node<K,V> { |
2678 |
> |
TreeNode<K,V> parent; // red-black tree links |
2679 |
> |
TreeNode<K,V> left; |
2680 |
> |
TreeNode<K,V> right; |
2681 |
> |
TreeNode<K,V> prev; // needed to unlink next upon deletion |
2682 |
> |
boolean red; |
2683 |
> |
|
2684 |
> |
TreeNode(int hash, K key, V val, Node<K,V> next, |
2685 |
> |
TreeNode<K,V> parent) { |
2686 |
> |
super(hash, key, val, next); |
2687 |
> |
this.parent = parent; |
2688 |
> |
} |
2689 |
> |
|
2690 |
> |
Node<K,V> find(int h, Object k) { |
2691 |
> |
return findTreeNode(h, k, null); |
2692 |
> |
} |
2693 |
> |
|
2694 |
> |
/** |
2695 |
> |
* Returns the TreeNode (or null if not found) for the given key |
2696 |
> |
* starting at given root. |
2697 |
> |
*/ |
2698 |
> |
final TreeNode<K,V> findTreeNode(int h, Object k, Class<?> kc) { |
2699 |
> |
if (k != null) { |
2700 |
> |
TreeNode<K,V> p = this; |
2701 |
> |
do { |
2702 |
> |
int ph, dir; K pk; TreeNode<K,V> q; |
2703 |
> |
TreeNode<K,V> pl = p.left, pr = p.right; |
2704 |
> |
if ((ph = p.hash) > h) |
2705 |
> |
p = pl; |
2706 |
> |
else if (ph < h) |
2707 |
> |
p = pr; |
2708 |
> |
else if ((pk = p.key) == k || (pk != null && k.equals(pk))) |
2709 |
> |
return p; |
2710 |
> |
else if (pl == null) |
2711 |
> |
p = pr; |
2712 |
> |
else if (pr == null) |
2713 |
> |
p = pl; |
2714 |
> |
else if ((kc != null || |
2715 |
> |
(kc = comparableClassFor(k)) != null) && |
2716 |
> |
(dir = compareComparables(kc, k, pk)) != 0) |
2717 |
> |
p = (dir < 0) ? pl : pr; |
2718 |
> |
else if ((q = pr.findTreeNode(h, k, kc)) != null) |
2719 |
> |
return q; |
2720 |
> |
else |
2721 |
> |
p = pl; |
2722 |
> |
} while (p != null); |
2723 |
> |
} |
2724 |
> |
return null; |
2725 |
> |
} |
2726 |
> |
} |
2727 |
> |
|
2728 |
> |
/* ---------------- TreeBins -------------- */ |
2729 |
> |
|
2730 |
> |
/** |
2731 |
> |
* TreeNodes used at the heads of bins. TreeBins do not hold user |
2732 |
> |
* keys or values, but instead point to list of TreeNodes and |
2733 |
> |
* their root. They also maintain a parasitic read-write lock |
2734 |
> |
* forcing writers (who hold bin lock) to wait for readers (who do |
2735 |
> |
* not) to complete before tree restructuring operations. |
2736 |
> |
*/ |
2737 |
> |
static final class TreeBin<K,V> extends Node<K,V> { |
2738 |
> |
TreeNode<K,V> root; |
2739 |
> |
volatile TreeNode<K,V> first; |
2740 |
> |
volatile Thread waiter; |
2741 |
> |
volatile int lockState; |
2742 |
> |
// values for lockState |
2743 |
> |
static final int WRITER = 1; // set while holding write lock |
2744 |
> |
static final int WAITER = 2; // set when waiting for write lock |
2745 |
> |
static final int READER = 4; // increment value for setting read lock |
2746 |
> |
|
2747 |
> |
/** |
2748 |
> |
* Tie-breaking utility for ordering insertions when equal |
2749 |
> |
* hashCodes and non-comparable. We don't require a total |
2750 |
> |
* order, just a consistent insertion rule to maintain |
2751 |
> |
* equivalence across rebalancings. Tie-breaking further than |
2752 |
> |
* necessary simplifies testing a bit. |
2753 |
> |
*/ |
2754 |
> |
static int tieBreakOrder(Object a, Object b) { |
2755 |
> |
int d; |
2756 |
> |
if (a == null || b == null || |
2757 |
> |
(d = a.getClass().getName(). |
2758 |
> |
compareTo(b.getClass().getName())) == 0) |
2759 |
> |
d = (System.identityHashCode(a) <= System.identityHashCode(b) ? |
2760 |
> |
-1 : 1); |
2761 |
> |
return d; |
2762 |
> |
} |
2763 |
> |
|
2764 |
> |
/** |
2765 |
> |
* Creates bin with initial set of nodes headed by b. |
2766 |
> |
*/ |
2767 |
> |
TreeBin(TreeNode<K,V> b) { |
2768 |
> |
super(TREEBIN, null, null); |
2769 |
> |
this.first = b; |
2770 |
> |
TreeNode<K,V> r = null; |
2771 |
> |
for (TreeNode<K,V> x = b, next; x != null; x = next) { |
2772 |
> |
next = (TreeNode<K,V>)x.next; |
2773 |
> |
x.left = x.right = null; |
2774 |
> |
if (r == null) { |
2775 |
> |
x.parent = null; |
2776 |
> |
x.red = false; |
2777 |
> |
r = x; |
2778 |
> |
} |
2779 |
> |
else { |
2780 |
> |
K k = x.key; |
2781 |
> |
int h = x.hash; |
2782 |
> |
Class<?> kc = null; |
2783 |
> |
for (TreeNode<K,V> p = r;;) { |
2784 |
> |
int dir, ph; |
2785 |
> |
K pk = p.key; |
2786 |
> |
if ((ph = p.hash) > h) |
2787 |
> |
dir = -1; |
2788 |
> |
else if (ph < h) |
2789 |
> |
dir = 1; |
2790 |
> |
else if ((kc == null && |
2791 |
> |
(kc = comparableClassFor(k)) == null) || |
2792 |
> |
(dir = compareComparables(kc, k, pk)) == 0) |
2793 |
> |
dir = tieBreakOrder(k, pk); |
2794 |
> |
TreeNode<K,V> xp = p; |
2795 |
> |
if ((p = (dir <= 0) ? p.left : p.right) == null) { |
2796 |
> |
x.parent = xp; |
2797 |
> |
if (dir <= 0) |
2798 |
> |
xp.left = x; |
2799 |
> |
else |
2800 |
> |
xp.right = x; |
2801 |
> |
r = balanceInsertion(r, x); |
2802 |
> |
break; |
2803 |
> |
} |
2804 |
> |
} |
2805 |
> |
} |
2806 |
> |
} |
2807 |
> |
this.root = r; |
2808 |
> |
assert checkInvariants(root); |
2809 |
> |
} |
2810 |
> |
|
2811 |
> |
/** |
2812 |
> |
* Acquires write lock for tree restructuring. |
2813 |
> |
*/ |
2814 |
> |
private final void lockRoot() { |
2815 |
> |
if (!U.compareAndSetInt(this, LOCKSTATE, 0, WRITER)) |
2816 |
> |
contendedLock(); // offload to separate method |
2817 |
> |
} |
2818 |
> |
|
2819 |
> |
/** |
2820 |
> |
* Releases write lock for tree restructuring. |
2821 |
> |
*/ |
2822 |
> |
private final void unlockRoot() { |
2823 |
> |
lockState = 0; |
2824 |
> |
} |
2825 |
> |
|
2826 |
> |
/** |
2827 |
> |
* Possibly blocks awaiting root lock. |
2828 |
> |
*/ |
2829 |
> |
private final void contendedLock() { |
2830 |
> |
boolean waiting = false; |
2831 |
> |
for (int s;;) { |
2832 |
> |
if (((s = lockState) & ~WAITER) == 0) { |
2833 |
> |
if (U.compareAndSetInt(this, LOCKSTATE, s, WRITER)) { |
2834 |
> |
if (waiting) |
2835 |
> |
waiter = null; |
2836 |
> |
return; |
2837 |
> |
} |
2838 |
> |
} |
2839 |
> |
else if ((s & WAITER) == 0) { |
2840 |
> |
if (U.compareAndSetInt(this, LOCKSTATE, s, s | WAITER)) { |
2841 |
> |
waiting = true; |
2842 |
> |
waiter = Thread.currentThread(); |
2843 |
> |
} |
2844 |
> |
} |
2845 |
> |
else if (waiting) |
2846 |
> |
LockSupport.park(this); |
2847 |
> |
} |
2848 |
> |
} |
2849 |
> |
|
2850 |
> |
/** |
2851 |
> |
* Returns matching node or null if none. Tries to search |
2852 |
> |
* using tree comparisons from root, but continues linear |
2853 |
> |
* search when lock not available. |
2854 |
> |
*/ |
2855 |
> |
final Node<K,V> find(int h, Object k) { |
2856 |
> |
if (k != null) { |
2857 |
> |
for (Node<K,V> e = first; e != null; ) { |
2858 |
> |
int s; K ek; |
2859 |
> |
if (((s = lockState) & (WAITER|WRITER)) != 0) { |
2860 |
> |
if (e.hash == h && |
2861 |
> |
((ek = e.key) == k || (ek != null && k.equals(ek)))) |
2862 |
> |
return e; |
2863 |
> |
e = e.next; |
2864 |
> |
} |
2865 |
> |
else if (U.compareAndSetInt(this, LOCKSTATE, s, |
2866 |
> |
s + READER)) { |
2867 |
> |
TreeNode<K,V> r, p; |
2868 |
> |
try { |
2869 |
> |
p = ((r = root) == null ? null : |
2870 |
> |
r.findTreeNode(h, k, null)); |
2871 |
> |
} finally { |
2872 |
> |
Thread w; |
2873 |
> |
if (U.getAndAddInt(this, LOCKSTATE, -READER) == |
2874 |
> |
(READER|WAITER) && (w = waiter) != null) |
2875 |
> |
LockSupport.unpark(w); |
2876 |
> |
} |
2877 |
> |
return p; |
2878 |
> |
} |
2879 |
> |
} |
2880 |
> |
} |
2881 |
> |
return null; |
2882 |
> |
} |
2883 |
> |
|
2884 |
> |
/** |
2885 |
> |
* Finds or adds a node. |
2886 |
> |
* @return null if added |
2887 |
> |
*/ |
2888 |
> |
final TreeNode<K,V> putTreeVal(int h, K k, V v) { |
2889 |
> |
Class<?> kc = null; |
2890 |
> |
boolean searched = false; |
2891 |
> |
for (TreeNode<K,V> p = root;;) { |
2892 |
> |
int dir, ph; K pk; |
2893 |
> |
if (p == null) { |
2894 |
> |
first = root = new TreeNode<K,V>(h, k, v, null, null); |
2895 |
|
break; |
2896 |
< |
sb.append(',').append(' '); |
2896 |
> |
} |
2897 |
> |
else if ((ph = p.hash) > h) |
2898 |
> |
dir = -1; |
2899 |
> |
else if (ph < h) |
2900 |
> |
dir = 1; |
2901 |
> |
else if ((pk = p.key) == k || (pk != null && k.equals(pk))) |
2902 |
> |
return p; |
2903 |
> |
else if ((kc == null && |
2904 |
> |
(kc = comparableClassFor(k)) == null) || |
2905 |
> |
(dir = compareComparables(kc, k, pk)) == 0) { |
2906 |
> |
if (!searched) { |
2907 |
> |
TreeNode<K,V> q, ch; |
2908 |
> |
searched = true; |
2909 |
> |
if (((ch = p.left) != null && |
2910 |
> |
(q = ch.findTreeNode(h, k, kc)) != null) || |
2911 |
> |
((ch = p.right) != null && |
2912 |
> |
(q = ch.findTreeNode(h, k, kc)) != null)) |
2913 |
> |
return q; |
2914 |
> |
} |
2915 |
> |
dir = tieBreakOrder(k, pk); |
2916 |
> |
} |
2917 |
> |
|
2918 |
> |
TreeNode<K,V> xp = p; |
2919 |
> |
if ((p = (dir <= 0) ? p.left : p.right) == null) { |
2920 |
> |
TreeNode<K,V> x, f = first; |
2921 |
> |
first = x = new TreeNode<K,V>(h, k, v, f, xp); |
2922 |
> |
if (f != null) |
2923 |
> |
f.prev = x; |
2924 |
> |
if (dir <= 0) |
2925 |
> |
xp.left = x; |
2926 |
> |
else |
2927 |
> |
xp.right = x; |
2928 |
> |
if (!xp.red) |
2929 |
> |
x.red = true; |
2930 |
> |
else { |
2931 |
> |
lockRoot(); |
2932 |
> |
try { |
2933 |
> |
root = balanceInsertion(root, x); |
2934 |
> |
} finally { |
2935 |
> |
unlockRoot(); |
2936 |
> |
} |
2937 |
> |
} |
2938 |
> |
break; |
2939 |
> |
} |
2940 |
> |
} |
2941 |
> |
assert checkInvariants(root); |
2942 |
> |
return null; |
2943 |
> |
} |
2944 |
> |
|
2945 |
> |
/** |
2946 |
> |
* Removes the given node, that must be present before this |
2947 |
> |
* call. This is messier than typical red-black deletion code |
2948 |
> |
* because we cannot swap the contents of an interior node |
2949 |
> |
* with a leaf successor that is pinned by "next" pointers |
2950 |
> |
* that are accessible independently of lock. So instead we |
2951 |
> |
* swap the tree linkages. |
2952 |
> |
* |
2953 |
> |
* @return true if now too small, so should be untreeified |
2954 |
> |
*/ |
2955 |
> |
final boolean removeTreeNode(TreeNode<K,V> p) { |
2956 |
> |
TreeNode<K,V> next = (TreeNode<K,V>)p.next; |
2957 |
> |
TreeNode<K,V> pred = p.prev; // unlink traversal pointers |
2958 |
> |
TreeNode<K,V> r, rl; |
2959 |
> |
if (pred == null) |
2960 |
> |
first = next; |
2961 |
> |
else |
2962 |
> |
pred.next = next; |
2963 |
> |
if (next != null) |
2964 |
> |
next.prev = pred; |
2965 |
> |
if (first == null) { |
2966 |
> |
root = null; |
2967 |
> |
return true; |
2968 |
> |
} |
2969 |
> |
if ((r = root) == null || r.right == null || // too small |
2970 |
> |
(rl = r.left) == null || rl.left == null) |
2971 |
> |
return true; |
2972 |
> |
lockRoot(); |
2973 |
> |
try { |
2974 |
> |
TreeNode<K,V> replacement; |
2975 |
> |
TreeNode<K,V> pl = p.left; |
2976 |
> |
TreeNode<K,V> pr = p.right; |
2977 |
> |
if (pl != null && pr != null) { |
2978 |
> |
TreeNode<K,V> s = pr, sl; |
2979 |
> |
while ((sl = s.left) != null) // find successor |
2980 |
> |
s = sl; |
2981 |
> |
boolean c = s.red; s.red = p.red; p.red = c; // swap colors |
2982 |
> |
TreeNode<K,V> sr = s.right; |
2983 |
> |
TreeNode<K,V> pp = p.parent; |
2984 |
> |
if (s == pr) { // p was s's direct parent |
2985 |
> |
p.parent = s; |
2986 |
> |
s.right = p; |
2987 |
> |
} |
2988 |
> |
else { |
2989 |
> |
TreeNode<K,V> sp = s.parent; |
2990 |
> |
if ((p.parent = sp) != null) { |
2991 |
> |
if (s == sp.left) |
2992 |
> |
sp.left = p; |
2993 |
> |
else |
2994 |
> |
sp.right = p; |
2995 |
> |
} |
2996 |
> |
if ((s.right = pr) != null) |
2997 |
> |
pr.parent = s; |
2998 |
> |
} |
2999 |
> |
p.left = null; |
3000 |
> |
if ((p.right = sr) != null) |
3001 |
> |
sr.parent = p; |
3002 |
> |
if ((s.left = pl) != null) |
3003 |
> |
pl.parent = s; |
3004 |
> |
if ((s.parent = pp) == null) |
3005 |
> |
r = s; |
3006 |
> |
else if (p == pp.left) |
3007 |
> |
pp.left = s; |
3008 |
> |
else |
3009 |
> |
pp.right = s; |
3010 |
> |
if (sr != null) |
3011 |
> |
replacement = sr; |
3012 |
> |
else |
3013 |
> |
replacement = p; |
3014 |
> |
} |
3015 |
> |
else if (pl != null) |
3016 |
> |
replacement = pl; |
3017 |
> |
else if (pr != null) |
3018 |
> |
replacement = pr; |
3019 |
> |
else |
3020 |
> |
replacement = p; |
3021 |
> |
if (replacement != p) { |
3022 |
> |
TreeNode<K,V> pp = replacement.parent = p.parent; |
3023 |
> |
if (pp == null) |
3024 |
> |
r = replacement; |
3025 |
> |
else if (p == pp.left) |
3026 |
> |
pp.left = replacement; |
3027 |
> |
else |
3028 |
> |
pp.right = replacement; |
3029 |
> |
p.left = p.right = p.parent = null; |
3030 |
> |
} |
3031 |
> |
|
3032 |
> |
root = (p.red) ? r : balanceDeletion(r, replacement); |
3033 |
> |
|
3034 |
> |
if (p == replacement) { // detach pointers |
3035 |
> |
TreeNode<K,V> pp; |
3036 |
> |
if ((pp = p.parent) != null) { |
3037 |
> |
if (p == pp.left) |
3038 |
> |
pp.left = null; |
3039 |
> |
else if (p == pp.right) |
3040 |
> |
pp.right = null; |
3041 |
> |
p.parent = null; |
3042 |
> |
} |
3043 |
> |
} |
3044 |
> |
} finally { |
3045 |
> |
unlockRoot(); |
3046 |
> |
} |
3047 |
> |
assert checkInvariants(root); |
3048 |
> |
return false; |
3049 |
> |
} |
3050 |
> |
|
3051 |
> |
/* ------------------------------------------------------------ */ |
3052 |
> |
// Red-black tree methods, all adapted from CLR |
3053 |
> |
|
3054 |
> |
static <K,V> TreeNode<K,V> rotateLeft(TreeNode<K,V> root, |
3055 |
> |
TreeNode<K,V> p) { |
3056 |
> |
TreeNode<K,V> r, pp, rl; |
3057 |
> |
if (p != null && (r = p.right) != null) { |
3058 |
> |
if ((rl = p.right = r.left) != null) |
3059 |
> |
rl.parent = p; |
3060 |
> |
if ((pp = r.parent = p.parent) == null) |
3061 |
> |
(root = r).red = false; |
3062 |
> |
else if (pp.left == p) |
3063 |
> |
pp.left = r; |
3064 |
> |
else |
3065 |
> |
pp.right = r; |
3066 |
> |
r.left = p; |
3067 |
> |
p.parent = r; |
3068 |
> |
} |
3069 |
> |
return root; |
3070 |
> |
} |
3071 |
> |
|
3072 |
> |
static <K,V> TreeNode<K,V> rotateRight(TreeNode<K,V> root, |
3073 |
> |
TreeNode<K,V> p) { |
3074 |
> |
TreeNode<K,V> l, pp, lr; |
3075 |
> |
if (p != null && (l = p.left) != null) { |
3076 |
> |
if ((lr = p.left = l.right) != null) |
3077 |
> |
lr.parent = p; |
3078 |
> |
if ((pp = l.parent = p.parent) == null) |
3079 |
> |
(root = l).red = false; |
3080 |
> |
else if (pp.right == p) |
3081 |
> |
pp.right = l; |
3082 |
> |
else |
3083 |
> |
pp.left = l; |
3084 |
> |
l.right = p; |
3085 |
> |
p.parent = l; |
3086 |
> |
} |
3087 |
> |
return root; |
3088 |
> |
} |
3089 |
> |
|
3090 |
> |
static <K,V> TreeNode<K,V> balanceInsertion(TreeNode<K,V> root, |
3091 |
> |
TreeNode<K,V> x) { |
3092 |
> |
x.red = true; |
3093 |
> |
for (TreeNode<K,V> xp, xpp, xppl, xppr;;) { |
3094 |
> |
if ((xp = x.parent) == null) { |
3095 |
> |
x.red = false; |
3096 |
> |
return x; |
3097 |
> |
} |
3098 |
> |
else if (!xp.red || (xpp = xp.parent) == null) |
3099 |
> |
return root; |
3100 |
> |
if (xp == (xppl = xpp.left)) { |
3101 |
> |
if ((xppr = xpp.right) != null && xppr.red) { |
3102 |
> |
xppr.red = false; |
3103 |
> |
xp.red = false; |
3104 |
> |
xpp.red = true; |
3105 |
> |
x = xpp; |
3106 |
> |
} |
3107 |
> |
else { |
3108 |
> |
if (x == xp.right) { |
3109 |
> |
root = rotateLeft(root, x = xp); |
3110 |
> |
xpp = (xp = x.parent) == null ? null : xp.parent; |
3111 |
> |
} |
3112 |
> |
if (xp != null) { |
3113 |
> |
xp.red = false; |
3114 |
> |
if (xpp != null) { |
3115 |
> |
xpp.red = true; |
3116 |
> |
root = rotateRight(root, xpp); |
3117 |
> |
} |
3118 |
> |
} |
3119 |
> |
} |
3120 |
> |
} |
3121 |
> |
else { |
3122 |
> |
if (xppl != null && xppl.red) { |
3123 |
> |
xppl.red = false; |
3124 |
> |
xp.red = false; |
3125 |
> |
xpp.red = true; |
3126 |
> |
x = xpp; |
3127 |
> |
} |
3128 |
> |
else { |
3129 |
> |
if (x == xp.left) { |
3130 |
> |
root = rotateRight(root, x = xp); |
3131 |
> |
xpp = (xp = x.parent) == null ? null : xp.parent; |
3132 |
> |
} |
3133 |
> |
if (xp != null) { |
3134 |
> |
xp.red = false; |
3135 |
> |
if (xpp != null) { |
3136 |
> |
xpp.red = true; |
3137 |
> |
root = rotateLeft(root, xpp); |
3138 |
> |
} |
3139 |
> |
} |
3140 |
> |
} |
3141 |
> |
} |
3142 |
> |
} |
3143 |
> |
} |
3144 |
> |
|
3145 |
> |
static <K,V> TreeNode<K,V> balanceDeletion(TreeNode<K,V> root, |
3146 |
> |
TreeNode<K,V> x) { |
3147 |
> |
for (TreeNode<K,V> xp, xpl, xpr;;) { |
3148 |
> |
if (x == null || x == root) |
3149 |
> |
return root; |
3150 |
> |
else if ((xp = x.parent) == null) { |
3151 |
> |
x.red = false; |
3152 |
> |
return x; |
3153 |
> |
} |
3154 |
> |
else if (x.red) { |
3155 |
> |
x.red = false; |
3156 |
> |
return root; |
3157 |
> |
} |
3158 |
> |
else if ((xpl = xp.left) == x) { |
3159 |
> |
if ((xpr = xp.right) != null && xpr.red) { |
3160 |
> |
xpr.red = false; |
3161 |
> |
xp.red = true; |
3162 |
> |
root = rotateLeft(root, xp); |
3163 |
> |
xpr = (xp = x.parent) == null ? null : xp.right; |
3164 |
> |
} |
3165 |
> |
if (xpr == null) |
3166 |
> |
x = xp; |
3167 |
> |
else { |
3168 |
> |
TreeNode<K,V> sl = xpr.left, sr = xpr.right; |
3169 |
> |
if ((sr == null || !sr.red) && |
3170 |
> |
(sl == null || !sl.red)) { |
3171 |
> |
xpr.red = true; |
3172 |
> |
x = xp; |
3173 |
> |
} |
3174 |
> |
else { |
3175 |
> |
if (sr == null || !sr.red) { |
3176 |
> |
if (sl != null) |
3177 |
> |
sl.red = false; |
3178 |
> |
xpr.red = true; |
3179 |
> |
root = rotateRight(root, xpr); |
3180 |
> |
xpr = (xp = x.parent) == null ? |
3181 |
> |
null : xp.right; |
3182 |
> |
} |
3183 |
> |
if (xpr != null) { |
3184 |
> |
xpr.red = (xp == null) ? false : xp.red; |
3185 |
> |
if ((sr = xpr.right) != null) |
3186 |
> |
sr.red = false; |
3187 |
> |
} |
3188 |
> |
if (xp != null) { |
3189 |
> |
xp.red = false; |
3190 |
> |
root = rotateLeft(root, xp); |
3191 |
> |
} |
3192 |
> |
x = root; |
3193 |
> |
} |
3194 |
> |
} |
3195 |
> |
} |
3196 |
> |
else { // symmetric |
3197 |
> |
if (xpl != null && xpl.red) { |
3198 |
> |
xpl.red = false; |
3199 |
> |
xp.red = true; |
3200 |
> |
root = rotateRight(root, xp); |
3201 |
> |
xpl = (xp = x.parent) == null ? null : xp.left; |
3202 |
> |
} |
3203 |
> |
if (xpl == null) |
3204 |
> |
x = xp; |
3205 |
> |
else { |
3206 |
> |
TreeNode<K,V> sl = xpl.left, sr = xpl.right; |
3207 |
> |
if ((sl == null || !sl.red) && |
3208 |
> |
(sr == null || !sr.red)) { |
3209 |
> |
xpl.red = true; |
3210 |
> |
x = xp; |
3211 |
> |
} |
3212 |
> |
else { |
3213 |
> |
if (sl == null || !sl.red) { |
3214 |
> |
if (sr != null) |
3215 |
> |
sr.red = false; |
3216 |
> |
xpl.red = true; |
3217 |
> |
root = rotateLeft(root, xpl); |
3218 |
> |
xpl = (xp = x.parent) == null ? |
3219 |
> |
null : xp.left; |
3220 |
> |
} |
3221 |
> |
if (xpl != null) { |
3222 |
> |
xpl.red = (xp == null) ? false : xp.red; |
3223 |
> |
if ((sl = xpl.left) != null) |
3224 |
> |
sl.red = false; |
3225 |
> |
} |
3226 |
> |
if (xp != null) { |
3227 |
> |
xp.red = false; |
3228 |
> |
root = rotateRight(root, xp); |
3229 |
> |
} |
3230 |
> |
x = root; |
3231 |
> |
} |
3232 |
> |
} |
3233 |
> |
} |
3234 |
> |
} |
3235 |
> |
} |
3236 |
> |
|
3237 |
> |
/** |
3238 |
> |
* Checks invariants recursively for the tree of Nodes rooted at t. |
3239 |
> |
*/ |
3240 |
> |
static <K,V> boolean checkInvariants(TreeNode<K,V> t) { |
3241 |
> |
TreeNode<K,V> tp = t.parent, tl = t.left, tr = t.right, |
3242 |
> |
tb = t.prev, tn = (TreeNode<K,V>)t.next; |
3243 |
> |
if (tb != null && tb.next != t) |
3244 |
> |
return false; |
3245 |
> |
if (tn != null && tn.prev != t) |
3246 |
> |
return false; |
3247 |
> |
if (tp != null && t != tp.left && t != tp.right) |
3248 |
> |
return false; |
3249 |
> |
if (tl != null && (tl.parent != t || tl.hash > t.hash)) |
3250 |
> |
return false; |
3251 |
> |
if (tr != null && (tr.parent != t || tr.hash < t.hash)) |
3252 |
> |
return false; |
3253 |
> |
if (t.red && tl != null && tl.red && tr != null && tr.red) |
3254 |
> |
return false; |
3255 |
> |
if (tl != null && !checkInvariants(tl)) |
3256 |
> |
return false; |
3257 |
> |
if (tr != null && !checkInvariants(tr)) |
3258 |
> |
return false; |
3259 |
> |
return true; |
3260 |
> |
} |
3261 |
> |
|
3262 |
> |
private static final Unsafe U = Unsafe.getUnsafe(); |
3263 |
> |
private static final long LOCKSTATE; |
3264 |
> |
static { |
3265 |
> |
try { |
3266 |
> |
LOCKSTATE = U.objectFieldOffset |
3267 |
> |
(TreeBin.class.getDeclaredField("lockState")); |
3268 |
> |
} catch (ReflectiveOperationException e) { |
3269 |
> |
throw new ExceptionInInitializerError(e); |
3270 |
|
} |
3271 |
|
} |
3138 |
– |
return sb.append('}').toString(); |
3272 |
|
} |
3273 |
|
|
3274 |
+ |
/* ----------------Table Traversal -------------- */ |
3275 |
+ |
|
3276 |
|
/** |
3277 |
< |
* Compares the specified object with this map for equality. |
3278 |
< |
* Returns {@code true} if the given object is a map with the same |
3279 |
< |
* mappings as this map. This operation may return misleading |
3280 |
< |
* results if either map is concurrently modified during execution |
3281 |
< |
* of this method. |
3277 |
> |
* Records the table, its length, and current traversal index for a |
3278 |
> |
* traverser that must process a region of a forwarded table before |
3279 |
> |
* proceeding with current table. |
3280 |
> |
*/ |
3281 |
> |
static final class TableStack<K,V> { |
3282 |
> |
int length; |
3283 |
> |
int index; |
3284 |
> |
Node<K,V>[] tab; |
3285 |
> |
TableStack<K,V> next; |
3286 |
> |
} |
3287 |
> |
|
3288 |
> |
/** |
3289 |
> |
* Encapsulates traversal for methods such as containsValue; also |
3290 |
> |
* serves as a base class for other iterators and spliterators. |
3291 |
|
* |
3292 |
< |
* @param o object to be compared for equality with this map |
3293 |
< |
* @return {@code true} if the specified object is equal to this map |
3292 |
> |
* Method advance visits once each still-valid node that was |
3293 |
> |
* reachable upon iterator construction. It might miss some that |
3294 |
> |
* were added to a bin after the bin was visited, which is OK wrt |
3295 |
> |
* consistency guarantees. Maintaining this property in the face |
3296 |
> |
* of possible ongoing resizes requires a fair amount of |
3297 |
> |
* bookkeeping state that is difficult to optimize away amidst |
3298 |
> |
* volatile accesses. Even so, traversal maintains reasonable |
3299 |
> |
* throughput. |
3300 |
> |
* |
3301 |
> |
* Normally, iteration proceeds bin-by-bin traversing lists. |
3302 |
> |
* However, if the table has been resized, then all future steps |
3303 |
> |
* must traverse both the bin at the current index as well as at |
3304 |
> |
* (index + baseSize); and so on for further resizings. To |
3305 |
> |
* paranoically cope with potential sharing by users of iterators |
3306 |
> |
* across threads, iteration terminates if a bounds checks fails |
3307 |
> |
* for a table read. |
3308 |
|
*/ |
3309 |
< |
public boolean equals(Object o) { |
3310 |
< |
if (o != this) { |
3311 |
< |
if (!(o instanceof Map)) |
3312 |
< |
return false; |
3313 |
< |
Map<?,?> m = (Map<?,?>) o; |
3314 |
< |
Traverser<K,V,Object> it = new Traverser<K,V,Object>(this); |
3315 |
< |
Object val; |
3316 |
< |
while ((val = it.advance()) != null) { |
3317 |
< |
Object v = m.get(it.nextKey); |
3318 |
< |
if (v == null || (v != val && !v.equals(val))) |
3319 |
< |
return false; |
3309 |
> |
static class Traverser<K,V> { |
3310 |
> |
Node<K,V>[] tab; // current table; updated if resized |
3311 |
> |
Node<K,V> next; // the next entry to use |
3312 |
> |
TableStack<K,V> stack, spare; // to save/restore on ForwardingNodes |
3313 |
> |
int index; // index of bin to use next |
3314 |
> |
int baseIndex; // current index of initial table |
3315 |
> |
int baseLimit; // index bound for initial table |
3316 |
> |
final int baseSize; // initial table size |
3317 |
> |
|
3318 |
> |
Traverser(Node<K,V>[] tab, int size, int index, int limit) { |
3319 |
> |
this.tab = tab; |
3320 |
> |
this.baseSize = size; |
3321 |
> |
this.baseIndex = this.index = index; |
3322 |
> |
this.baseLimit = limit; |
3323 |
> |
this.next = null; |
3324 |
> |
} |
3325 |
> |
|
3326 |
> |
/** |
3327 |
> |
* Advances if possible, returning next valid node, or null if none. |
3328 |
> |
*/ |
3329 |
> |
final Node<K,V> advance() { |
3330 |
> |
Node<K,V> e; |
3331 |
> |
if ((e = next) != null) |
3332 |
> |
e = e.next; |
3333 |
> |
for (;;) { |
3334 |
> |
Node<K,V>[] t; int i, n; // must use locals in checks |
3335 |
> |
if (e != null) |
3336 |
> |
return next = e; |
3337 |
> |
if (baseIndex >= baseLimit || (t = tab) == null || |
3338 |
> |
(n = t.length) <= (i = index) || i < 0) |
3339 |
> |
return next = null; |
3340 |
> |
if ((e = tabAt(t, i)) != null && e.hash < 0) { |
3341 |
> |
if (e instanceof ForwardingNode) { |
3342 |
> |
tab = ((ForwardingNode<K,V>)e).nextTable; |
3343 |
> |
e = null; |
3344 |
> |
pushState(t, i, n); |
3345 |
> |
continue; |
3346 |
> |
} |
3347 |
> |
else if (e instanceof TreeBin) |
3348 |
> |
e = ((TreeBin<K,V>)e).first; |
3349 |
> |
else |
3350 |
> |
e = null; |
3351 |
> |
} |
3352 |
> |
if (stack != null) |
3353 |
> |
recoverState(n); |
3354 |
> |
else if ((index = i + baseSize) >= n) |
3355 |
> |
index = ++baseIndex; // visit upper slots if present |
3356 |
|
} |
3357 |
< |
for (Map.Entry<?,?> e : m.entrySet()) { |
3358 |
< |
Object mk, mv, v; |
3359 |
< |
if ((mk = e.getKey()) == null || |
3360 |
< |
(mv = e.getValue()) == null || |
3361 |
< |
(v = internalGet(mk)) == null || |
3362 |
< |
(mv != v && !mv.equals(v))) |
3363 |
< |
return false; |
3357 |
> |
} |
3358 |
> |
|
3359 |
> |
/** |
3360 |
> |
* Saves traversal state upon encountering a forwarding node. |
3361 |
> |
*/ |
3362 |
> |
private void pushState(Node<K,V>[] t, int i, int n) { |
3363 |
> |
TableStack<K,V> s = spare; // reuse if possible |
3364 |
> |
if (s != null) |
3365 |
> |
spare = s.next; |
3366 |
> |
else |
3367 |
> |
s = new TableStack<K,V>(); |
3368 |
> |
s.tab = t; |
3369 |
> |
s.length = n; |
3370 |
> |
s.index = i; |
3371 |
> |
s.next = stack; |
3372 |
> |
stack = s; |
3373 |
> |
} |
3374 |
> |
|
3375 |
> |
/** |
3376 |
> |
* Possibly pops traversal state. |
3377 |
> |
* |
3378 |
> |
* @param n length of current table |
3379 |
> |
*/ |
3380 |
> |
private void recoverState(int n) { |
3381 |
> |
TableStack<K,V> s; int len; |
3382 |
> |
while ((s = stack) != null && (index += (len = s.length)) >= n) { |
3383 |
> |
n = len; |
3384 |
> |
index = s.index; |
3385 |
> |
tab = s.tab; |
3386 |
> |
s.tab = null; |
3387 |
> |
TableStack<K,V> next = s.next; |
3388 |
> |
s.next = spare; // save for reuse |
3389 |
> |
stack = next; |
3390 |
> |
spare = s; |
3391 |
|
} |
3392 |
+ |
if (s == null && (index += baseSize) >= n) |
3393 |
+ |
index = ++baseIndex; |
3394 |
|
} |
3172 |
– |
return true; |
3395 |
|
} |
3396 |
|
|
3397 |
< |
/* ----------------Iterators -------------- */ |
3398 |
< |
|
3399 |
< |
@SuppressWarnings("serial") static final class KeyIterator<K,V> extends Traverser<K,V,Object> |
3400 |
< |
implements Spliterator<K>, Enumeration<K> { |
3401 |
< |
KeyIterator(ConcurrentHashMap<K, V> map) { super(map); } |
3402 |
< |
KeyIterator(Traverser<K,V,Object> it) { |
3403 |
< |
super(it); |
3397 |
> |
/** |
3398 |
> |
* Base of key, value, and entry Iterators. Adds fields to |
3399 |
> |
* Traverser to support iterator.remove. |
3400 |
> |
*/ |
3401 |
> |
static class BaseIterator<K,V> extends Traverser<K,V> { |
3402 |
> |
final ConcurrentHashMap<K,V> map; |
3403 |
> |
Node<K,V> lastReturned; |
3404 |
> |
BaseIterator(Node<K,V>[] tab, int size, int index, int limit, |
3405 |
> |
ConcurrentHashMap<K,V> map) { |
3406 |
> |
super(tab, size, index, limit); |
3407 |
> |
this.map = map; |
3408 |
> |
advance(); |
3409 |
|
} |
3410 |
< |
public KeyIterator<K,V> split() { |
3411 |
< |
if (nextKey != null) |
3410 |
> |
|
3411 |
> |
public final boolean hasNext() { return next != null; } |
3412 |
> |
public final boolean hasMoreElements() { return next != null; } |
3413 |
> |
|
3414 |
> |
public final void remove() { |
3415 |
> |
Node<K,V> p; |
3416 |
> |
if ((p = lastReturned) == null) |
3417 |
|
throw new IllegalStateException(); |
3418 |
< |
return new KeyIterator<K,V>(this); |
3418 |
> |
lastReturned = null; |
3419 |
> |
map.replaceNode(p.key, null, null); |
3420 |
|
} |
3421 |
< |
@SuppressWarnings("unchecked") public final K next() { |
3422 |
< |
if (nextVal == null && advance() == null) |
3421 |
> |
} |
3422 |
> |
|
3423 |
> |
static final class KeyIterator<K,V> extends BaseIterator<K,V> |
3424 |
> |
implements Iterator<K>, Enumeration<K> { |
3425 |
> |
KeyIterator(Node<K,V>[] tab, int size, int index, int limit, |
3426 |
> |
ConcurrentHashMap<K,V> map) { |
3427 |
> |
super(tab, size, index, limit, map); |
3428 |
> |
} |
3429 |
> |
|
3430 |
> |
public final K next() { |
3431 |
> |
Node<K,V> p; |
3432 |
> |
if ((p = next) == null) |
3433 |
|
throw new NoSuchElementException(); |
3434 |
< |
Object k = nextKey; |
3435 |
< |
nextVal = null; |
3436 |
< |
return (K) k; |
3434 |
> |
K k = p.key; |
3435 |
> |
lastReturned = p; |
3436 |
> |
advance(); |
3437 |
> |
return k; |
3438 |
|
} |
3439 |
|
|
3440 |
|
public final K nextElement() { return next(); } |
3441 |
|
} |
3442 |
|
|
3443 |
< |
@SuppressWarnings("serial") static final class ValueIterator<K,V> extends Traverser<K,V,Object> |
3444 |
< |
implements Spliterator<V>, Enumeration<V> { |
3445 |
< |
ValueIterator(ConcurrentHashMap<K, V> map) { super(map); } |
3446 |
< |
ValueIterator(Traverser<K,V,Object> it) { |
3447 |
< |
super(it); |
3204 |
< |
} |
3205 |
< |
public ValueIterator<K,V> split() { |
3206 |
< |
if (nextKey != null) |
3207 |
< |
throw new IllegalStateException(); |
3208 |
< |
return new ValueIterator<K,V>(this); |
3443 |
> |
static final class ValueIterator<K,V> extends BaseIterator<K,V> |
3444 |
> |
implements Iterator<V>, Enumeration<V> { |
3445 |
> |
ValueIterator(Node<K,V>[] tab, int size, int index, int limit, |
3446 |
> |
ConcurrentHashMap<K,V> map) { |
3447 |
> |
super(tab, size, index, limit, map); |
3448 |
|
} |
3449 |
|
|
3450 |
< |
@SuppressWarnings("unchecked") public final V next() { |
3451 |
< |
Object v; |
3452 |
< |
if ((v = nextVal) == null && (v = advance()) == null) |
3450 |
> |
public final V next() { |
3451 |
> |
Node<K,V> p; |
3452 |
> |
if ((p = next) == null) |
3453 |
|
throw new NoSuchElementException(); |
3454 |
< |
nextVal = null; |
3455 |
< |
return (V) v; |
3454 |
> |
V v = p.val; |
3455 |
> |
lastReturned = p; |
3456 |
> |
advance(); |
3457 |
> |
return v; |
3458 |
|
} |
3459 |
|
|
3460 |
|
public final V nextElement() { return next(); } |
3461 |
|
} |
3462 |
|
|
3463 |
< |
@SuppressWarnings("serial") static final class EntryIterator<K,V> extends Traverser<K,V,Object> |
3464 |
< |
implements Spliterator<Map.Entry<K,V>> { |
3465 |
< |
EntryIterator(ConcurrentHashMap<K, V> map) { super(map); } |
3466 |
< |
EntryIterator(Traverser<K,V,Object> it) { |
3467 |
< |
super(it); |
3227 |
< |
} |
3228 |
< |
public EntryIterator<K,V> split() { |
3229 |
< |
if (nextKey != null) |
3230 |
< |
throw new IllegalStateException(); |
3231 |
< |
return new EntryIterator<K,V>(this); |
3463 |
> |
static final class EntryIterator<K,V> extends BaseIterator<K,V> |
3464 |
> |
implements Iterator<Map.Entry<K,V>> { |
3465 |
> |
EntryIterator(Node<K,V>[] tab, int size, int index, int limit, |
3466 |
> |
ConcurrentHashMap<K,V> map) { |
3467 |
> |
super(tab, size, index, limit, map); |
3468 |
|
} |
3469 |
|
|
3470 |
< |
@SuppressWarnings("unchecked") public final Map.Entry<K,V> next() { |
3471 |
< |
Object v; |
3472 |
< |
if ((v = nextVal) == null && (v = advance()) == null) |
3470 |
> |
public final Map.Entry<K,V> next() { |
3471 |
> |
Node<K,V> p; |
3472 |
> |
if ((p = next) == null) |
3473 |
|
throw new NoSuchElementException(); |
3474 |
< |
Object k = nextKey; |
3475 |
< |
nextVal = null; |
3476 |
< |
return new MapEntry<K,V>((K)k, (V)v, map); |
3474 |
> |
K k = p.key; |
3475 |
> |
V v = p.val; |
3476 |
> |
lastReturned = p; |
3477 |
> |
advance(); |
3478 |
> |
return new MapEntry<K,V>(k, v, map); |
3479 |
|
} |
3480 |
|
} |
3481 |
|
|
3482 |
|
/** |
3483 |
< |
* Exported Entry for iterators |
3483 |
> |
* Exported Entry for EntryIterator. |
3484 |
|
*/ |
3485 |
< |
static final class MapEntry<K,V> implements Map.Entry<K, V> { |
3485 |
> |
static final class MapEntry<K,V> implements Map.Entry<K,V> { |
3486 |
|
final K key; // non-null |
3487 |
|
V val; // non-null |
3488 |
< |
final ConcurrentHashMap<K, V> map; |
3489 |
< |
MapEntry(K key, V val, ConcurrentHashMap<K, V> map) { |
3488 |
> |
final ConcurrentHashMap<K,V> map; |
3489 |
> |
MapEntry(K key, V val, ConcurrentHashMap<K,V> map) { |
3490 |
|
this.key = key; |
3491 |
|
this.val = val; |
3492 |
|
this.map = map; |
3493 |
|
} |
3494 |
< |
public final K getKey() { return key; } |
3495 |
< |
public final V getValue() { return val; } |
3496 |
< |
public final int hashCode() { return key.hashCode() ^ val.hashCode(); } |
3497 |
< |
public final String toString(){ return key + "=" + val; } |
3494 |
> |
public K getKey() { return key; } |
3495 |
> |
public V getValue() { return val; } |
3496 |
> |
public int hashCode() { return key.hashCode() ^ val.hashCode(); } |
3497 |
> |
public String toString() { |
3498 |
> |
return Helpers.mapEntryToString(key, val); |
3499 |
> |
} |
3500 |
|
|
3501 |
< |
public final boolean equals(Object o) { |
3501 |
> |
public boolean equals(Object o) { |
3502 |
|
Object k, v; Map.Entry<?,?> e; |
3503 |
|
return ((o instanceof Map.Entry) && |
3504 |
|
(k = (e = (Map.Entry<?,?>)o).getKey()) != null && |
3512 |
|
* value to return is somewhat arbitrary here. Since we do not |
3513 |
|
* necessarily track asynchronous changes, the most recent |
3514 |
|
* "previous" value could be different from what we return (or |
3515 |
< |
* could even have been removed in which case the put will |
3515 |
> |
* could even have been removed, in which case the put will |
3516 |
|
* re-establish). We do not and cannot guarantee more. |
3517 |
|
*/ |
3518 |
< |
public final V setValue(V value) { |
3518 |
> |
public V setValue(V value) { |
3519 |
|
if (value == null) throw new NullPointerException(); |
3520 |
|
V v = val; |
3521 |
|
val = value; |
3524 |
|
} |
3525 |
|
} |
3526 |
|
|
3527 |
< |
/* ---------------- Serialization Support -------------- */ |
3527 |
> |
static final class KeySpliterator<K,V> extends Traverser<K,V> |
3528 |
> |
implements Spliterator<K> { |
3529 |
> |
long est; // size estimate |
3530 |
> |
KeySpliterator(Node<K,V>[] tab, int size, int index, int limit, |
3531 |
> |
long est) { |
3532 |
> |
super(tab, size, index, limit); |
3533 |
> |
this.est = est; |
3534 |
> |
} |
3535 |
> |
|
3536 |
> |
public KeySpliterator<K,V> trySplit() { |
3537 |
> |
int i, f, h; |
3538 |
> |
return (h = ((i = baseIndex) + (f = baseLimit)) >>> 1) <= i ? null : |
3539 |
> |
new KeySpliterator<K,V>(tab, baseSize, baseLimit = h, |
3540 |
> |
f, est >>>= 1); |
3541 |
> |
} |
3542 |
|
|
3543 |
< |
/** |
3544 |
< |
* Stripped-down version of helper class used in previous version, |
3545 |
< |
* declared for the sake of serialization compatibility |
3546 |
< |
*/ |
3547 |
< |
static class Segment<K,V> implements Serializable { |
3294 |
< |
private static final long serialVersionUID = 2249069246763182397L; |
3295 |
< |
final float loadFactor; |
3296 |
< |
Segment(float lf) { this.loadFactor = lf; } |
3297 |
< |
} |
3543 |
> |
public void forEachRemaining(Consumer<? super K> action) { |
3544 |
> |
if (action == null) throw new NullPointerException(); |
3545 |
> |
for (Node<K,V> p; (p = advance()) != null;) |
3546 |
> |
action.accept(p.key); |
3547 |
> |
} |
3548 |
|
|
3549 |
< |
/** |
3550 |
< |
* Saves the state of the {@code ConcurrentHashMap} instance to a |
3551 |
< |
* stream (i.e., serializes it). |
3552 |
< |
* @param s the stream |
3553 |
< |
* @serialData |
3554 |
< |
* the key (Object) and value (Object) |
3555 |
< |
* for each key-value mapping, followed by a null pair. |
3556 |
< |
* The key-value mappings are emitted in no particular order. |
3557 |
< |
*/ |
3558 |
< |
@SuppressWarnings("unchecked") private void writeObject(java.io.ObjectOutputStream s) |
3559 |
< |
throws java.io.IOException { |
3560 |
< |
if (segments == null) { // for serialization compatibility |
3561 |
< |
segments = (Segment<K,V>[]) |
3562 |
< |
new Segment<?,?>[DEFAULT_CONCURRENCY_LEVEL]; |
3313 |
< |
for (int i = 0; i < segments.length; ++i) |
3314 |
< |
segments[i] = new Segment<K,V>(LOAD_FACTOR); |
3315 |
< |
} |
3316 |
< |
s.defaultWriteObject(); |
3317 |
< |
Traverser<K,V,Object> it = new Traverser<K,V,Object>(this); |
3318 |
< |
Object v; |
3319 |
< |
while ((v = it.advance()) != null) { |
3320 |
< |
s.writeObject(it.nextKey); |
3321 |
< |
s.writeObject(v); |
3549 |
> |
public boolean tryAdvance(Consumer<? super K> action) { |
3550 |
> |
if (action == null) throw new NullPointerException(); |
3551 |
> |
Node<K,V> p; |
3552 |
> |
if ((p = advance()) == null) |
3553 |
> |
return false; |
3554 |
> |
action.accept(p.key); |
3555 |
> |
return true; |
3556 |
> |
} |
3557 |
> |
|
3558 |
> |
public long estimateSize() { return est; } |
3559 |
> |
|
3560 |
> |
public int characteristics() { |
3561 |
> |
return Spliterator.DISTINCT | Spliterator.CONCURRENT | |
3562 |
> |
Spliterator.NONNULL; |
3563 |
|
} |
3323 |
– |
s.writeObject(null); |
3324 |
– |
s.writeObject(null); |
3325 |
– |
segments = null; // throw away |
3564 |
|
} |
3565 |
|
|
3566 |
< |
/** |
3567 |
< |
* Reconstitutes the instance from a stream (that is, deserializes it). |
3568 |
< |
* @param s the stream |
3569 |
< |
*/ |
3570 |
< |
@SuppressWarnings("unchecked") private void readObject(java.io.ObjectInputStream s) |
3571 |
< |
throws java.io.IOException, ClassNotFoundException { |
3572 |
< |
s.defaultReadObject(); |
3573 |
< |
this.segments = null; // unneeded |
3336 |
< |
// initialize transient final field |
3337 |
< |
UNSAFE.putObjectVolatile(this, counterOffset, new LongAdder()); |
3566 |
> |
static final class ValueSpliterator<K,V> extends Traverser<K,V> |
3567 |
> |
implements Spliterator<V> { |
3568 |
> |
long est; // size estimate |
3569 |
> |
ValueSpliterator(Node<K,V>[] tab, int size, int index, int limit, |
3570 |
> |
long est) { |
3571 |
> |
super(tab, size, index, limit); |
3572 |
> |
this.est = est; |
3573 |
> |
} |
3574 |
|
|
3575 |
< |
// Create all nodes, then place in table once size is known |
3576 |
< |
long size = 0L; |
3577 |
< |
Node p = null; |
3578 |
< |
for (;;) { |
3579 |
< |
K k = (K) s.readObject(); |
3344 |
< |
V v = (V) s.readObject(); |
3345 |
< |
if (k != null && v != null) { |
3346 |
< |
int h = spread(k.hashCode()); |
3347 |
< |
p = new Node(h, k, v, p); |
3348 |
< |
++size; |
3349 |
< |
} |
3350 |
< |
else |
3351 |
< |
break; |
3575 |
> |
public ValueSpliterator<K,V> trySplit() { |
3576 |
> |
int i, f, h; |
3577 |
> |
return (h = ((i = baseIndex) + (f = baseLimit)) >>> 1) <= i ? null : |
3578 |
> |
new ValueSpliterator<K,V>(tab, baseSize, baseLimit = h, |
3579 |
> |
f, est >>>= 1); |
3580 |
|
} |
3581 |
< |
if (p != null) { |
3582 |
< |
boolean init = false; |
3583 |
< |
int n; |
3584 |
< |
if (size >= (long)(MAXIMUM_CAPACITY >>> 1)) |
3585 |
< |
n = MAXIMUM_CAPACITY; |
3586 |
< |
else { |
3587 |
< |
int sz = (int)size; |
3588 |
< |
n = tableSizeFor(sz + (sz >>> 1) + 1); |
3589 |
< |
} |
3590 |
< |
int sc = sizeCtl; |
3591 |
< |
boolean collide = false; |
3592 |
< |
if (n > sc && |
3593 |
< |
UNSAFE.compareAndSwapInt(this, sizeCtlOffset, sc, -1)) { |
3594 |
< |
try { |
3595 |
< |
if (table == null) { |
3596 |
< |
init = true; |
3597 |
< |
Node[] tab = new Node[n]; |
3598 |
< |
int mask = n - 1; |
3599 |
< |
while (p != null) { |
3600 |
< |
int j = p.hash & mask; |
3373 |
< |
Node next = p.next; |
3374 |
< |
Node q = p.next = tabAt(tab, j); |
3375 |
< |
setTabAt(tab, j, p); |
3376 |
< |
if (!collide && q != null && q.hash == p.hash) |
3377 |
< |
collide = true; |
3378 |
< |
p = next; |
3379 |
< |
} |
3380 |
< |
table = tab; |
3381 |
< |
counter.add(size); |
3382 |
< |
sc = n - (n >>> 2); |
3383 |
< |
} |
3384 |
< |
} finally { |
3385 |
< |
sizeCtl = sc; |
3386 |
< |
} |
3387 |
< |
if (collide) { // rescan and convert to TreeBins |
3388 |
< |
Node[] tab = table; |
3389 |
< |
for (int i = 0; i < tab.length; ++i) { |
3390 |
< |
int c = 0; |
3391 |
< |
for (Node e = tabAt(tab, i); e != null; e = e.next) { |
3392 |
< |
if (++c > TREE_THRESHOLD && |
3393 |
< |
(e.key instanceof Comparable)) { |
3394 |
< |
replaceWithTreeBin(tab, i, e.key); |
3395 |
< |
break; |
3396 |
< |
} |
3397 |
< |
} |
3398 |
< |
} |
3399 |
< |
} |
3400 |
< |
} |
3401 |
< |
if (!init) { // Can only happen if unsafely published. |
3402 |
< |
while (p != null) { |
3403 |
< |
internalPut(p.key, p.val); |
3404 |
< |
p = p.next; |
3405 |
< |
} |
3406 |
< |
} |
3581 |
> |
|
3582 |
> |
public void forEachRemaining(Consumer<? super V> action) { |
3583 |
> |
if (action == null) throw new NullPointerException(); |
3584 |
> |
for (Node<K,V> p; (p = advance()) != null;) |
3585 |
> |
action.accept(p.val); |
3586 |
> |
} |
3587 |
> |
|
3588 |
> |
public boolean tryAdvance(Consumer<? super V> action) { |
3589 |
> |
if (action == null) throw new NullPointerException(); |
3590 |
> |
Node<K,V> p; |
3591 |
> |
if ((p = advance()) == null) |
3592 |
> |
return false; |
3593 |
> |
action.accept(p.val); |
3594 |
> |
return true; |
3595 |
> |
} |
3596 |
> |
|
3597 |
> |
public long estimateSize() { return est; } |
3598 |
> |
|
3599 |
> |
public int characteristics() { |
3600 |
> |
return Spliterator.CONCURRENT | Spliterator.NONNULL; |
3601 |
|
} |
3602 |
|
} |
3603 |
|
|
3604 |
+ |
static final class EntrySpliterator<K,V> extends Traverser<K,V> |
3605 |
+ |
implements Spliterator<Map.Entry<K,V>> { |
3606 |
+ |
final ConcurrentHashMap<K,V> map; // To export MapEntry |
3607 |
+ |
long est; // size estimate |
3608 |
+ |
EntrySpliterator(Node<K,V>[] tab, int size, int index, int limit, |
3609 |
+ |
long est, ConcurrentHashMap<K,V> map) { |
3610 |
+ |
super(tab, size, index, limit); |
3611 |
+ |
this.map = map; |
3612 |
+ |
this.est = est; |
3613 |
+ |
} |
3614 |
|
|
3615 |
< |
// ------------------------------------------------------- |
3615 |
> |
public EntrySpliterator<K,V> trySplit() { |
3616 |
> |
int i, f, h; |
3617 |
> |
return (h = ((i = baseIndex) + (f = baseLimit)) >>> 1) <= i ? null : |
3618 |
> |
new EntrySpliterator<K,V>(tab, baseSize, baseLimit = h, |
3619 |
> |
f, est >>>= 1, map); |
3620 |
> |
} |
3621 |
|
|
3622 |
< |
// Sams |
3623 |
< |
/** Interface describing a void action of one argument */ |
3624 |
< |
public interface Action<A> { void apply(A a); } |
3625 |
< |
/** Interface describing a void action of two arguments */ |
3626 |
< |
public interface BiAction<A,B> { void apply(A a, B b); } |
3418 |
< |
/** Interface describing a function of one argument */ |
3419 |
< |
public interface Fun<A,T> { T apply(A a); } |
3420 |
< |
/** Interface describing a function of two arguments */ |
3421 |
< |
public interface BiFun<A,B,T> { T apply(A a, B b); } |
3422 |
< |
/** Interface describing a function of no arguments */ |
3423 |
< |
public interface Generator<T> { T apply(); } |
3424 |
< |
/** Interface describing a function mapping its argument to a double */ |
3425 |
< |
public interface ObjectToDouble<A> { double apply(A a); } |
3426 |
< |
/** Interface describing a function mapping its argument to a long */ |
3427 |
< |
public interface ObjectToLong<A> { long apply(A a); } |
3428 |
< |
/** Interface describing a function mapping its argument to an int */ |
3429 |
< |
public interface ObjectToInt<A> {int apply(A a); } |
3430 |
< |
/** Interface describing a function mapping two arguments to a double */ |
3431 |
< |
public interface ObjectByObjectToDouble<A,B> { double apply(A a, B b); } |
3432 |
< |
/** Interface describing a function mapping two arguments to a long */ |
3433 |
< |
public interface ObjectByObjectToLong<A,B> { long apply(A a, B b); } |
3434 |
< |
/** Interface describing a function mapping two arguments to an int */ |
3435 |
< |
public interface ObjectByObjectToInt<A,B> {int apply(A a, B b); } |
3436 |
< |
/** Interface describing a function mapping a double to a double */ |
3437 |
< |
public interface DoubleToDouble { double apply(double a); } |
3438 |
< |
/** Interface describing a function mapping a long to a long */ |
3439 |
< |
public interface LongToLong { long apply(long a); } |
3440 |
< |
/** Interface describing a function mapping an int to an int */ |
3441 |
< |
public interface IntToInt { int apply(int a); } |
3442 |
< |
/** Interface describing a function mapping two doubles to a double */ |
3443 |
< |
public interface DoubleByDoubleToDouble { double apply(double a, double b); } |
3444 |
< |
/** Interface describing a function mapping two longs to a long */ |
3445 |
< |
public interface LongByLongToLong { long apply(long a, long b); } |
3446 |
< |
/** Interface describing a function mapping two ints to an int */ |
3447 |
< |
public interface IntByIntToInt { int apply(int a, int b); } |
3622 |
> |
public void forEachRemaining(Consumer<? super Map.Entry<K,V>> action) { |
3623 |
> |
if (action == null) throw new NullPointerException(); |
3624 |
> |
for (Node<K,V> p; (p = advance()) != null; ) |
3625 |
> |
action.accept(new MapEntry<K,V>(p.key, p.val, map)); |
3626 |
> |
} |
3627 |
|
|
3628 |
+ |
public boolean tryAdvance(Consumer<? super Map.Entry<K,V>> action) { |
3629 |
+ |
if (action == null) throw new NullPointerException(); |
3630 |
+ |
Node<K,V> p; |
3631 |
+ |
if ((p = advance()) == null) |
3632 |
+ |
return false; |
3633 |
+ |
action.accept(new MapEntry<K,V>(p.key, p.val, map)); |
3634 |
+ |
return true; |
3635 |
+ |
} |
3636 |
|
|
3637 |
< |
// ------------------------------------------------------- |
3637 |
> |
public long estimateSize() { return est; } |
3638 |
> |
|
3639 |
> |
public int characteristics() { |
3640 |
> |
return Spliterator.DISTINCT | Spliterator.CONCURRENT | |
3641 |
> |
Spliterator.NONNULL; |
3642 |
> |
} |
3643 |
> |
} |
3644 |
> |
|
3645 |
> |
// Parallel bulk operations |
3646 |
> |
|
3647 |
> |
/** |
3648 |
> |
* Computes initial batch value for bulk tasks. The returned value |
3649 |
> |
* is approximately exp2 of the number of times (minus one) to |
3650 |
> |
* split task by two before executing leaf action. This value is |
3651 |
> |
* faster to compute and more convenient to use as a guide to |
3652 |
> |
* splitting than is the depth, since it is used while dividing by |
3653 |
> |
* two anyway. |
3654 |
> |
*/ |
3655 |
> |
final int batchFor(long b) { |
3656 |
> |
long n; |
3657 |
> |
if (b == Long.MAX_VALUE || (n = sumCount()) <= 1L || n < b) |
3658 |
> |
return 0; |
3659 |
> |
int sp = ForkJoinPool.getCommonPoolParallelism() << 2; // slack of 4 |
3660 |
> |
return (b <= 0L || (n /= b) >= sp) ? sp : (int)n; |
3661 |
> |
} |
3662 |
|
|
3663 |
|
/** |
3664 |
|
* Performs the given action for each (key, value). |
3665 |
|
* |
3666 |
+ |
* @param parallelismThreshold the (estimated) number of elements |
3667 |
+ |
* needed for this operation to be executed in parallel |
3668 |
|
* @param action the action |
3669 |
+ |
* @since 1.8 |
3670 |
|
*/ |
3671 |
< |
public void forEach(BiAction<K,V> action) { |
3672 |
< |
ForkJoinTasks.forEach |
3673 |
< |
(this, action).invoke(); |
3671 |
> |
public void forEach(long parallelismThreshold, |
3672 |
> |
BiConsumer<? super K,? super V> action) { |
3673 |
> |
if (action == null) throw new NullPointerException(); |
3674 |
> |
new ForEachMappingTask<K,V> |
3675 |
> |
(null, batchFor(parallelismThreshold), 0, 0, table, |
3676 |
> |
action).invoke(); |
3677 |
|
} |
3678 |
|
|
3679 |
|
/** |
3680 |
|
* Performs the given action for each non-null transformation |
3681 |
|
* of each (key, value). |
3682 |
|
* |
3683 |
+ |
* @param parallelismThreshold the (estimated) number of elements |
3684 |
+ |
* needed for this operation to be executed in parallel |
3685 |
|
* @param transformer a function returning the transformation |
3686 |
< |
* for an element, or null of there is no transformation (in |
3687 |
< |
* which case the action is not applied). |
3686 |
> |
* for an element, or null if there is no transformation (in |
3687 |
> |
* which case the action is not applied) |
3688 |
|
* @param action the action |
3689 |
+ |
* @param <U> the return type of the transformer |
3690 |
+ |
* @since 1.8 |
3691 |
|
*/ |
3692 |
< |
public <U> void forEach(BiFun<? super K, ? super V, ? extends U> transformer, |
3693 |
< |
Action<U> action) { |
3694 |
< |
ForkJoinTasks.forEach |
3695 |
< |
(this, transformer, action).invoke(); |
3692 |
> |
public <U> void forEach(long parallelismThreshold, |
3693 |
> |
BiFunction<? super K, ? super V, ? extends U> transformer, |
3694 |
> |
Consumer<? super U> action) { |
3695 |
> |
if (transformer == null || action == null) |
3696 |
> |
throw new NullPointerException(); |
3697 |
> |
new ForEachTransformedMappingTask<K,V,U> |
3698 |
> |
(null, batchFor(parallelismThreshold), 0, 0, table, |
3699 |
> |
transformer, action).invoke(); |
3700 |
|
} |
3701 |
|
|
3702 |
|
/** |
3706 |
|
* results of any other parallel invocations of the search |
3707 |
|
* function are ignored. |
3708 |
|
* |
3709 |
+ |
* @param parallelismThreshold the (estimated) number of elements |
3710 |
+ |
* needed for this operation to be executed in parallel |
3711 |
|
* @param searchFunction a function returning a non-null |
3712 |
|
* result on success, else null |
3713 |
+ |
* @param <U> the return type of the search function |
3714 |
|
* @return a non-null result from applying the given search |
3715 |
|
* function on each (key, value), or null if none |
3716 |
+ |
* @since 1.8 |
3717 |
|
*/ |
3718 |
< |
public <U> U search(BiFun<? super K, ? super V, ? extends U> searchFunction) { |
3719 |
< |
return ForkJoinTasks.search |
3720 |
< |
(this, searchFunction).invoke(); |
3718 |
> |
public <U> U search(long parallelismThreshold, |
3719 |
> |
BiFunction<? super K, ? super V, ? extends U> searchFunction) { |
3720 |
> |
if (searchFunction == null) throw new NullPointerException(); |
3721 |
> |
return new SearchMappingsTask<K,V,U> |
3722 |
> |
(null, batchFor(parallelismThreshold), 0, 0, table, |
3723 |
> |
searchFunction, new AtomicReference<U>()).invoke(); |
3724 |
|
} |
3725 |
|
|
3726 |
|
/** |
3728 |
|
* of all (key, value) pairs using the given reducer to |
3729 |
|
* combine values, or null if none. |
3730 |
|
* |
3731 |
+ |
* @param parallelismThreshold the (estimated) number of elements |
3732 |
+ |
* needed for this operation to be executed in parallel |
3733 |
|
* @param transformer a function returning the transformation |
3734 |
< |
* for an element, or null of there is no transformation (in |
3735 |
< |
* which case it is not combined). |
3734 |
> |
* for an element, or null if there is no transformation (in |
3735 |
> |
* which case it is not combined) |
3736 |
|
* @param reducer a commutative associative combining function |
3737 |
+ |
* @param <U> the return type of the transformer |
3738 |
|
* @return the result of accumulating the given transformation |
3739 |
|
* of all (key, value) pairs |
3740 |
+ |
* @since 1.8 |
3741 |
|
*/ |
3742 |
< |
public <U> U reduce(BiFun<? super K, ? super V, ? extends U> transformer, |
3743 |
< |
BiFun<? super U, ? super U, ? extends U> reducer) { |
3744 |
< |
return ForkJoinTasks.reduce |
3745 |
< |
(this, transformer, reducer).invoke(); |
3742 |
> |
public <U> U reduce(long parallelismThreshold, |
3743 |
> |
BiFunction<? super K, ? super V, ? extends U> transformer, |
3744 |
> |
BiFunction<? super U, ? super U, ? extends U> reducer) { |
3745 |
> |
if (transformer == null || reducer == null) |
3746 |
> |
throw new NullPointerException(); |
3747 |
> |
return new MapReduceMappingsTask<K,V,U> |
3748 |
> |
(null, batchFor(parallelismThreshold), 0, 0, table, |
3749 |
> |
null, transformer, reducer).invoke(); |
3750 |
|
} |
3751 |
|
|
3752 |
|
/** |
3754 |
|
* of all (key, value) pairs using the given reducer to |
3755 |
|
* combine values, and the given basis as an identity value. |
3756 |
|
* |
3757 |
+ |
* @param parallelismThreshold the (estimated) number of elements |
3758 |
+ |
* needed for this operation to be executed in parallel |
3759 |
|
* @param transformer a function returning the transformation |
3760 |
|
* for an element |
3761 |
|
* @param basis the identity (initial default value) for the reduction |
3762 |
|
* @param reducer a commutative associative combining function |
3763 |
|
* @return the result of accumulating the given transformation |
3764 |
|
* of all (key, value) pairs |
3765 |
+ |
* @since 1.8 |
3766 |
|
*/ |
3767 |
< |
public double reduceToDouble(ObjectByObjectToDouble<? super K, ? super V> transformer, |
3767 |
> |
public double reduceToDouble(long parallelismThreshold, |
3768 |
> |
ToDoubleBiFunction<? super K, ? super V> transformer, |
3769 |
|
double basis, |
3770 |
< |
DoubleByDoubleToDouble reducer) { |
3771 |
< |
return ForkJoinTasks.reduceToDouble |
3772 |
< |
(this, transformer, basis, reducer).invoke(); |
3770 |
> |
DoubleBinaryOperator reducer) { |
3771 |
> |
if (transformer == null || reducer == null) |
3772 |
> |
throw new NullPointerException(); |
3773 |
> |
return new MapReduceMappingsToDoubleTask<K,V> |
3774 |
> |
(null, batchFor(parallelismThreshold), 0, 0, table, |
3775 |
> |
null, transformer, basis, reducer).invoke(); |
3776 |
|
} |
3777 |
|
|
3778 |
|
/** |
3780 |
|
* of all (key, value) pairs using the given reducer to |
3781 |
|
* combine values, and the given basis as an identity value. |
3782 |
|
* |
3783 |
+ |
* @param parallelismThreshold the (estimated) number of elements |
3784 |
+ |
* needed for this operation to be executed in parallel |
3785 |
|
* @param transformer a function returning the transformation |
3786 |
|
* for an element |
3787 |
|
* @param basis the identity (initial default value) for the reduction |
3788 |
|
* @param reducer a commutative associative combining function |
3789 |
|
* @return the result of accumulating the given transformation |
3790 |
|
* of all (key, value) pairs |
3791 |
+ |
* @since 1.8 |
3792 |
|
*/ |
3793 |
< |
public long reduceToLong(ObjectByObjectToLong<? super K, ? super V> transformer, |
3793 |
> |
public long reduceToLong(long parallelismThreshold, |
3794 |
> |
ToLongBiFunction<? super K, ? super V> transformer, |
3795 |
|
long basis, |
3796 |
< |
LongByLongToLong reducer) { |
3797 |
< |
return ForkJoinTasks.reduceToLong |
3798 |
< |
(this, transformer, basis, reducer).invoke(); |
3796 |
> |
LongBinaryOperator reducer) { |
3797 |
> |
if (transformer == null || reducer == null) |
3798 |
> |
throw new NullPointerException(); |
3799 |
> |
return new MapReduceMappingsToLongTask<K,V> |
3800 |
> |
(null, batchFor(parallelismThreshold), 0, 0, table, |
3801 |
> |
null, transformer, basis, reducer).invoke(); |
3802 |
|
} |
3803 |
|
|
3804 |
|
/** |
3806 |
|
* of all (key, value) pairs using the given reducer to |
3807 |
|
* combine values, and the given basis as an identity value. |
3808 |
|
* |
3809 |
+ |
* @param parallelismThreshold the (estimated) number of elements |
3810 |
+ |
* needed for this operation to be executed in parallel |
3811 |
|
* @param transformer a function returning the transformation |
3812 |
|
* for an element |
3813 |
|
* @param basis the identity (initial default value) for the reduction |
3814 |
|
* @param reducer a commutative associative combining function |
3815 |
|
* @return the result of accumulating the given transformation |
3816 |
|
* of all (key, value) pairs |
3817 |
+ |
* @since 1.8 |
3818 |
|
*/ |
3819 |
< |
public int reduceToInt(ObjectByObjectToInt<? super K, ? super V> transformer, |
3819 |
> |
public int reduceToInt(long parallelismThreshold, |
3820 |
> |
ToIntBiFunction<? super K, ? super V> transformer, |
3821 |
|
int basis, |
3822 |
< |
IntByIntToInt reducer) { |
3823 |
< |
return ForkJoinTasks.reduceToInt |
3824 |
< |
(this, transformer, basis, reducer).invoke(); |
3822 |
> |
IntBinaryOperator reducer) { |
3823 |
> |
if (transformer == null || reducer == null) |
3824 |
> |
throw new NullPointerException(); |
3825 |
> |
return new MapReduceMappingsToIntTask<K,V> |
3826 |
> |
(null, batchFor(parallelismThreshold), 0, 0, table, |
3827 |
> |
null, transformer, basis, reducer).invoke(); |
3828 |
|
} |
3829 |
|
|
3830 |
|
/** |
3831 |
|
* Performs the given action for each key. |
3832 |
|
* |
3833 |
+ |
* @param parallelismThreshold the (estimated) number of elements |
3834 |
+ |
* needed for this operation to be executed in parallel |
3835 |
|
* @param action the action |
3836 |
+ |
* @since 1.8 |
3837 |
|
*/ |
3838 |
< |
public void forEachKey(Action<K> action) { |
3839 |
< |
ForkJoinTasks.forEachKey |
3840 |
< |
(this, action).invoke(); |
3838 |
> |
public void forEachKey(long parallelismThreshold, |
3839 |
> |
Consumer<? super K> action) { |
3840 |
> |
if (action == null) throw new NullPointerException(); |
3841 |
> |
new ForEachKeyTask<K,V> |
3842 |
> |
(null, batchFor(parallelismThreshold), 0, 0, table, |
3843 |
> |
action).invoke(); |
3844 |
|
} |
3845 |
|
|
3846 |
|
/** |
3847 |
|
* Performs the given action for each non-null transformation |
3848 |
|
* of each key. |
3849 |
|
* |
3850 |
+ |
* @param parallelismThreshold the (estimated) number of elements |
3851 |
+ |
* needed for this operation to be executed in parallel |
3852 |
|
* @param transformer a function returning the transformation |
3853 |
< |
* for an element, or null of there is no transformation (in |
3854 |
< |
* which case the action is not applied). |
3853 |
> |
* for an element, or null if there is no transformation (in |
3854 |
> |
* which case the action is not applied) |
3855 |
|
* @param action the action |
3856 |
+ |
* @param <U> the return type of the transformer |
3857 |
+ |
* @since 1.8 |
3858 |
|
*/ |
3859 |
< |
public <U> void forEachKey(Fun<? super K, ? extends U> transformer, |
3860 |
< |
Action<U> action) { |
3861 |
< |
ForkJoinTasks.forEachKey |
3862 |
< |
(this, transformer, action).invoke(); |
3859 |
> |
public <U> void forEachKey(long parallelismThreshold, |
3860 |
> |
Function<? super K, ? extends U> transformer, |
3861 |
> |
Consumer<? super U> action) { |
3862 |
> |
if (transformer == null || action == null) |
3863 |
> |
throw new NullPointerException(); |
3864 |
> |
new ForEachTransformedKeyTask<K,V,U> |
3865 |
> |
(null, batchFor(parallelismThreshold), 0, 0, table, |
3866 |
> |
transformer, action).invoke(); |
3867 |
|
} |
3868 |
|
|
3869 |
|
/** |
3873 |
|
* any other parallel invocations of the search function are |
3874 |
|
* ignored. |
3875 |
|
* |
3876 |
+ |
* @param parallelismThreshold the (estimated) number of elements |
3877 |
+ |
* needed for this operation to be executed in parallel |
3878 |
|
* @param searchFunction a function returning a non-null |
3879 |
|
* result on success, else null |
3880 |
+ |
* @param <U> the return type of the search function |
3881 |
|
* @return a non-null result from applying the given search |
3882 |
|
* function on each key, or null if none |
3883 |
+ |
* @since 1.8 |
3884 |
|
*/ |
3885 |
< |
public <U> U searchKeys(Fun<? super K, ? extends U> searchFunction) { |
3886 |
< |
return ForkJoinTasks.searchKeys |
3887 |
< |
(this, searchFunction).invoke(); |
3885 |
> |
public <U> U searchKeys(long parallelismThreshold, |
3886 |
> |
Function<? super K, ? extends U> searchFunction) { |
3887 |
> |
if (searchFunction == null) throw new NullPointerException(); |
3888 |
> |
return new SearchKeysTask<K,V,U> |
3889 |
> |
(null, batchFor(parallelismThreshold), 0, 0, table, |
3890 |
> |
searchFunction, new AtomicReference<U>()).invoke(); |
3891 |
|
} |
3892 |
|
|
3893 |
|
/** |
3894 |
|
* Returns the result of accumulating all keys using the given |
3895 |
|
* reducer to combine values, or null if none. |
3896 |
|
* |
3897 |
+ |
* @param parallelismThreshold the (estimated) number of elements |
3898 |
+ |
* needed for this operation to be executed in parallel |
3899 |
|
* @param reducer a commutative associative combining function |
3900 |
|
* @return the result of accumulating all keys using the given |
3901 |
|
* reducer to combine values, or null if none |
3902 |
+ |
* @since 1.8 |
3903 |
|
*/ |
3904 |
< |
public K reduceKeys(BiFun<? super K, ? super K, ? extends K> reducer) { |
3905 |
< |
return ForkJoinTasks.reduceKeys |
3906 |
< |
(this, reducer).invoke(); |
3904 |
> |
public K reduceKeys(long parallelismThreshold, |
3905 |
> |
BiFunction<? super K, ? super K, ? extends K> reducer) { |
3906 |
> |
if (reducer == null) throw new NullPointerException(); |
3907 |
> |
return new ReduceKeysTask<K,V> |
3908 |
> |
(null, batchFor(parallelismThreshold), 0, 0, table, |
3909 |
> |
null, reducer).invoke(); |
3910 |
|
} |
3911 |
|
|
3912 |
|
/** |
3914 |
|
* of all keys using the given reducer to combine values, or |
3915 |
|
* null if none. |
3916 |
|
* |
3917 |
+ |
* @param parallelismThreshold the (estimated) number of elements |
3918 |
+ |
* needed for this operation to be executed in parallel |
3919 |
|
* @param transformer a function returning the transformation |
3920 |
< |
* for an element, or null of there is no transformation (in |
3921 |
< |
* which case it is not combined). |
3920 |
> |
* for an element, or null if there is no transformation (in |
3921 |
> |
* which case it is not combined) |
3922 |
|
* @param reducer a commutative associative combining function |
3923 |
+ |
* @param <U> the return type of the transformer |
3924 |
|
* @return the result of accumulating the given transformation |
3925 |
|
* of all keys |
3926 |
+ |
* @since 1.8 |
3927 |
|
*/ |
3928 |
< |
public <U> U reduceKeys(Fun<? super K, ? extends U> transformer, |
3929 |
< |
BiFun<? super U, ? super U, ? extends U> reducer) { |
3930 |
< |
return ForkJoinTasks.reduceKeys |
3931 |
< |
(this, transformer, reducer).invoke(); |
3928 |
> |
public <U> U reduceKeys(long parallelismThreshold, |
3929 |
> |
Function<? super K, ? extends U> transformer, |
3930 |
> |
BiFunction<? super U, ? super U, ? extends U> reducer) { |
3931 |
> |
if (transformer == null || reducer == null) |
3932 |
> |
throw new NullPointerException(); |
3933 |
> |
return new MapReduceKeysTask<K,V,U> |
3934 |
> |
(null, batchFor(parallelismThreshold), 0, 0, table, |
3935 |
> |
null, transformer, reducer).invoke(); |
3936 |
|
} |
3937 |
|
|
3938 |
|
/** |
3940 |
|
* of all keys using the given reducer to combine values, and |
3941 |
|
* the given basis as an identity value. |
3942 |
|
* |
3943 |
+ |
* @param parallelismThreshold the (estimated) number of elements |
3944 |
+ |
* needed for this operation to be executed in parallel |
3945 |
|
* @param transformer a function returning the transformation |
3946 |
|
* for an element |
3947 |
|
* @param basis the identity (initial default value) for the reduction |
3948 |
|
* @param reducer a commutative associative combining function |
3949 |
< |
* @return the result of accumulating the given transformation |
3949 |
> |
* @return the result of accumulating the given transformation |
3950 |
|
* of all keys |
3951 |
+ |
* @since 1.8 |
3952 |
|
*/ |
3953 |
< |
public double reduceKeysToDouble(ObjectToDouble<? super K> transformer, |
3953 |
> |
public double reduceKeysToDouble(long parallelismThreshold, |
3954 |
> |
ToDoubleFunction<? super K> transformer, |
3955 |
|
double basis, |
3956 |
< |
DoubleByDoubleToDouble reducer) { |
3957 |
< |
return ForkJoinTasks.reduceKeysToDouble |
3958 |
< |
(this, transformer, basis, reducer).invoke(); |
3956 |
> |
DoubleBinaryOperator reducer) { |
3957 |
> |
if (transformer == null || reducer == null) |
3958 |
> |
throw new NullPointerException(); |
3959 |
> |
return new MapReduceKeysToDoubleTask<K,V> |
3960 |
> |
(null, batchFor(parallelismThreshold), 0, 0, table, |
3961 |
> |
null, transformer, basis, reducer).invoke(); |
3962 |
|
} |
3963 |
|
|
3964 |
|
/** |
3966 |
|
* of all keys using the given reducer to combine values, and |
3967 |
|
* the given basis as an identity value. |
3968 |
|
* |
3969 |
+ |
* @param parallelismThreshold the (estimated) number of elements |
3970 |
+ |
* needed for this operation to be executed in parallel |
3971 |
|
* @param transformer a function returning the transformation |
3972 |
|
* for an element |
3973 |
|
* @param basis the identity (initial default value) for the reduction |
3974 |
|
* @param reducer a commutative associative combining function |
3975 |
|
* @return the result of accumulating the given transformation |
3976 |
|
* of all keys |
3977 |
+ |
* @since 1.8 |
3978 |
|
*/ |
3979 |
< |
public long reduceKeysToLong(ObjectToLong<? super K> transformer, |
3979 |
> |
public long reduceKeysToLong(long parallelismThreshold, |
3980 |
> |
ToLongFunction<? super K> transformer, |
3981 |
|
long basis, |
3982 |
< |
LongByLongToLong reducer) { |
3983 |
< |
return ForkJoinTasks.reduceKeysToLong |
3984 |
< |
(this, transformer, basis, reducer).invoke(); |
3982 |
> |
LongBinaryOperator reducer) { |
3983 |
> |
if (transformer == null || reducer == null) |
3984 |
> |
throw new NullPointerException(); |
3985 |
> |
return new MapReduceKeysToLongTask<K,V> |
3986 |
> |
(null, batchFor(parallelismThreshold), 0, 0, table, |
3987 |
> |
null, transformer, basis, reducer).invoke(); |
3988 |
|
} |
3989 |
|
|
3990 |
|
/** |
3992 |
|
* of all keys using the given reducer to combine values, and |
3993 |
|
* the given basis as an identity value. |
3994 |
|
* |
3995 |
+ |
* @param parallelismThreshold the (estimated) number of elements |
3996 |
+ |
* needed for this operation to be executed in parallel |
3997 |
|
* @param transformer a function returning the transformation |
3998 |
|
* for an element |
3999 |
|
* @param basis the identity (initial default value) for the reduction |
4000 |
|
* @param reducer a commutative associative combining function |
4001 |
|
* @return the result of accumulating the given transformation |
4002 |
|
* of all keys |
4003 |
+ |
* @since 1.8 |
4004 |
|
*/ |
4005 |
< |
public int reduceKeysToInt(ObjectToInt<? super K> transformer, |
4005 |
> |
public int reduceKeysToInt(long parallelismThreshold, |
4006 |
> |
ToIntFunction<? super K> transformer, |
4007 |
|
int basis, |
4008 |
< |
IntByIntToInt reducer) { |
4009 |
< |
return ForkJoinTasks.reduceKeysToInt |
4010 |
< |
(this, transformer, basis, reducer).invoke(); |
4008 |
> |
IntBinaryOperator reducer) { |
4009 |
> |
if (transformer == null || reducer == null) |
4010 |
> |
throw new NullPointerException(); |
4011 |
> |
return new MapReduceKeysToIntTask<K,V> |
4012 |
> |
(null, batchFor(parallelismThreshold), 0, 0, table, |
4013 |
> |
null, transformer, basis, reducer).invoke(); |
4014 |
|
} |
4015 |
|
|
4016 |
|
/** |
4017 |
|
* Performs the given action for each value. |
4018 |
|
* |
4019 |
+ |
* @param parallelismThreshold the (estimated) number of elements |
4020 |
+ |
* needed for this operation to be executed in parallel |
4021 |
|
* @param action the action |
4022 |
+ |
* @since 1.8 |
4023 |
|
*/ |
4024 |
< |
public void forEachValue(Action<V> action) { |
4025 |
< |
ForkJoinTasks.forEachValue |
4026 |
< |
(this, action).invoke(); |
4024 |
> |
public void forEachValue(long parallelismThreshold, |
4025 |
> |
Consumer<? super V> action) { |
4026 |
> |
if (action == null) |
4027 |
> |
throw new NullPointerException(); |
4028 |
> |
new ForEachValueTask<K,V> |
4029 |
> |
(null, batchFor(parallelismThreshold), 0, 0, table, |
4030 |
> |
action).invoke(); |
4031 |
|
} |
4032 |
|
|
4033 |
|
/** |
4034 |
|
* Performs the given action for each non-null transformation |
4035 |
|
* of each value. |
4036 |
|
* |
4037 |
+ |
* @param parallelismThreshold the (estimated) number of elements |
4038 |
+ |
* needed for this operation to be executed in parallel |
4039 |
|
* @param transformer a function returning the transformation |
4040 |
< |
* for an element, or null of there is no transformation (in |
4041 |
< |
* which case the action is not applied). |
4040 |
> |
* for an element, or null if there is no transformation (in |
4041 |
> |
* which case the action is not applied) |
4042 |
> |
* @param action the action |
4043 |
> |
* @param <U> the return type of the transformer |
4044 |
> |
* @since 1.8 |
4045 |
|
*/ |
4046 |
< |
public <U> void forEachValue(Fun<? super V, ? extends U> transformer, |
4047 |
< |
Action<U> action) { |
4048 |
< |
ForkJoinTasks.forEachValue |
4049 |
< |
(this, transformer, action).invoke(); |
4046 |
> |
public <U> void forEachValue(long parallelismThreshold, |
4047 |
> |
Function<? super V, ? extends U> transformer, |
4048 |
> |
Consumer<? super U> action) { |
4049 |
> |
if (transformer == null || action == null) |
4050 |
> |
throw new NullPointerException(); |
4051 |
> |
new ForEachTransformedValueTask<K,V,U> |
4052 |
> |
(null, batchFor(parallelismThreshold), 0, 0, table, |
4053 |
> |
transformer, action).invoke(); |
4054 |
|
} |
4055 |
|
|
4056 |
|
/** |
4060 |
|
* any other parallel invocations of the search function are |
4061 |
|
* ignored. |
4062 |
|
* |
4063 |
+ |
* @param parallelismThreshold the (estimated) number of elements |
4064 |
+ |
* needed for this operation to be executed in parallel |
4065 |
|
* @param searchFunction a function returning a non-null |
4066 |
|
* result on success, else null |
4067 |
+ |
* @param <U> the return type of the search function |
4068 |
|
* @return a non-null result from applying the given search |
4069 |
|
* function on each value, or null if none |
4070 |
< |
* |
4070 |
> |
* @since 1.8 |
4071 |
|
*/ |
4072 |
< |
public <U> U searchValues(Fun<? super V, ? extends U> searchFunction) { |
4073 |
< |
return ForkJoinTasks.searchValues |
4074 |
< |
(this, searchFunction).invoke(); |
4072 |
> |
public <U> U searchValues(long parallelismThreshold, |
4073 |
> |
Function<? super V, ? extends U> searchFunction) { |
4074 |
> |
if (searchFunction == null) throw new NullPointerException(); |
4075 |
> |
return new SearchValuesTask<K,V,U> |
4076 |
> |
(null, batchFor(parallelismThreshold), 0, 0, table, |
4077 |
> |
searchFunction, new AtomicReference<U>()).invoke(); |
4078 |
|
} |
4079 |
|
|
4080 |
|
/** |
4081 |
|
* Returns the result of accumulating all values using the |
4082 |
|
* given reducer to combine values, or null if none. |
4083 |
|
* |
4084 |
+ |
* @param parallelismThreshold the (estimated) number of elements |
4085 |
+ |
* needed for this operation to be executed in parallel |
4086 |
|
* @param reducer a commutative associative combining function |
4087 |
< |
* @return the result of accumulating all values |
4087 |
> |
* @return the result of accumulating all values |
4088 |
> |
* @since 1.8 |
4089 |
|
*/ |
4090 |
< |
public V reduceValues(BiFun<? super V, ? super V, ? extends V> reducer) { |
4091 |
< |
return ForkJoinTasks.reduceValues |
4092 |
< |
(this, reducer).invoke(); |
4090 |
> |
public V reduceValues(long parallelismThreshold, |
4091 |
> |
BiFunction<? super V, ? super V, ? extends V> reducer) { |
4092 |
> |
if (reducer == null) throw new NullPointerException(); |
4093 |
> |
return new ReduceValuesTask<K,V> |
4094 |
> |
(null, batchFor(parallelismThreshold), 0, 0, table, |
4095 |
> |
null, reducer).invoke(); |
4096 |
|
} |
4097 |
|
|
4098 |
|
/** |
4100 |
|
* of all values using the given reducer to combine values, or |
4101 |
|
* null if none. |
4102 |
|
* |
4103 |
+ |
* @param parallelismThreshold the (estimated) number of elements |
4104 |
+ |
* needed for this operation to be executed in parallel |
4105 |
|
* @param transformer a function returning the transformation |
4106 |
< |
* for an element, or null of there is no transformation (in |
4107 |
< |
* which case it is not combined). |
4106 |
> |
* for an element, or null if there is no transformation (in |
4107 |
> |
* which case it is not combined) |
4108 |
|
* @param reducer a commutative associative combining function |
4109 |
+ |
* @param <U> the return type of the transformer |
4110 |
|
* @return the result of accumulating the given transformation |
4111 |
|
* of all values |
4112 |
+ |
* @since 1.8 |
4113 |
|
*/ |
4114 |
< |
public <U> U reduceValues(Fun<? super V, ? extends U> transformer, |
4115 |
< |
BiFun<? super U, ? super U, ? extends U> reducer) { |
4116 |
< |
return ForkJoinTasks.reduceValues |
4117 |
< |
(this, transformer, reducer).invoke(); |
4114 |
> |
public <U> U reduceValues(long parallelismThreshold, |
4115 |
> |
Function<? super V, ? extends U> transformer, |
4116 |
> |
BiFunction<? super U, ? super U, ? extends U> reducer) { |
4117 |
> |
if (transformer == null || reducer == null) |
4118 |
> |
throw new NullPointerException(); |
4119 |
> |
return new MapReduceValuesTask<K,V,U> |
4120 |
> |
(null, batchFor(parallelismThreshold), 0, 0, table, |
4121 |
> |
null, transformer, reducer).invoke(); |
4122 |
|
} |
4123 |
|
|
4124 |
|
/** |
4126 |
|
* of all values using the given reducer to combine values, |
4127 |
|
* and the given basis as an identity value. |
4128 |
|
* |
4129 |
+ |
* @param parallelismThreshold the (estimated) number of elements |
4130 |
+ |
* needed for this operation to be executed in parallel |
4131 |
|
* @param transformer a function returning the transformation |
4132 |
|
* for an element |
4133 |
|
* @param basis the identity (initial default value) for the reduction |
4134 |
|
* @param reducer a commutative associative combining function |
4135 |
|
* @return the result of accumulating the given transformation |
4136 |
|
* of all values |
4137 |
+ |
* @since 1.8 |
4138 |
|
*/ |
4139 |
< |
public double reduceValuesToDouble(ObjectToDouble<? super V> transformer, |
4139 |
> |
public double reduceValuesToDouble(long parallelismThreshold, |
4140 |
> |
ToDoubleFunction<? super V> transformer, |
4141 |
|
double basis, |
4142 |
< |
DoubleByDoubleToDouble reducer) { |
4143 |
< |
return ForkJoinTasks.reduceValuesToDouble |
4144 |
< |
(this, transformer, basis, reducer).invoke(); |
4142 |
> |
DoubleBinaryOperator reducer) { |
4143 |
> |
if (transformer == null || reducer == null) |
4144 |
> |
throw new NullPointerException(); |
4145 |
> |
return new MapReduceValuesToDoubleTask<K,V> |
4146 |
> |
(null, batchFor(parallelismThreshold), 0, 0, table, |
4147 |
> |
null, transformer, basis, reducer).invoke(); |
4148 |
|
} |
4149 |
|
|
4150 |
|
/** |
4152 |
|
* of all values using the given reducer to combine values, |
4153 |
|
* and the given basis as an identity value. |
4154 |
|
* |
4155 |
+ |
* @param parallelismThreshold the (estimated) number of elements |
4156 |
+ |
* needed for this operation to be executed in parallel |
4157 |
|
* @param transformer a function returning the transformation |
4158 |
|
* for an element |
4159 |
|
* @param basis the identity (initial default value) for the reduction |
4160 |
|
* @param reducer a commutative associative combining function |
4161 |
|
* @return the result of accumulating the given transformation |
4162 |
|
* of all values |
4163 |
+ |
* @since 1.8 |
4164 |
|
*/ |
4165 |
< |
public long reduceValuesToLong(ObjectToLong<? super V> transformer, |
4165 |
> |
public long reduceValuesToLong(long parallelismThreshold, |
4166 |
> |
ToLongFunction<? super V> transformer, |
4167 |
|
long basis, |
4168 |
< |
LongByLongToLong reducer) { |
4169 |
< |
return ForkJoinTasks.reduceValuesToLong |
4170 |
< |
(this, transformer, basis, reducer).invoke(); |
4168 |
> |
LongBinaryOperator reducer) { |
4169 |
> |
if (transformer == null || reducer == null) |
4170 |
> |
throw new NullPointerException(); |
4171 |
> |
return new MapReduceValuesToLongTask<K,V> |
4172 |
> |
(null, batchFor(parallelismThreshold), 0, 0, table, |
4173 |
> |
null, transformer, basis, reducer).invoke(); |
4174 |
|
} |
4175 |
|
|
4176 |
|
/** |
4178 |
|
* of all values using the given reducer to combine values, |
4179 |
|
* and the given basis as an identity value. |
4180 |
|
* |
4181 |
+ |
* @param parallelismThreshold the (estimated) number of elements |
4182 |
+ |
* needed for this operation to be executed in parallel |
4183 |
|
* @param transformer a function returning the transformation |
4184 |
|
* for an element |
4185 |
|
* @param basis the identity (initial default value) for the reduction |
4186 |
|
* @param reducer a commutative associative combining function |
4187 |
|
* @return the result of accumulating the given transformation |
4188 |
|
* of all values |
4189 |
+ |
* @since 1.8 |
4190 |
|
*/ |
4191 |
< |
public int reduceValuesToInt(ObjectToInt<? super V> transformer, |
4191 |
> |
public int reduceValuesToInt(long parallelismThreshold, |
4192 |
> |
ToIntFunction<? super V> transformer, |
4193 |
|
int basis, |
4194 |
< |
IntByIntToInt reducer) { |
4195 |
< |
return ForkJoinTasks.reduceValuesToInt |
4196 |
< |
(this, transformer, basis, reducer).invoke(); |
4194 |
> |
IntBinaryOperator reducer) { |
4195 |
> |
if (transformer == null || reducer == null) |
4196 |
> |
throw new NullPointerException(); |
4197 |
> |
return new MapReduceValuesToIntTask<K,V> |
4198 |
> |
(null, batchFor(parallelismThreshold), 0, 0, table, |
4199 |
> |
null, transformer, basis, reducer).invoke(); |
4200 |
|
} |
4201 |
|
|
4202 |
|
/** |
4203 |
|
* Performs the given action for each entry. |
4204 |
|
* |
4205 |
+ |
* @param parallelismThreshold the (estimated) number of elements |
4206 |
+ |
* needed for this operation to be executed in parallel |
4207 |
|
* @param action the action |
4208 |
+ |
* @since 1.8 |
4209 |
|
*/ |
4210 |
< |
public void forEachEntry(Action<Map.Entry<K,V>> action) { |
4211 |
< |
ForkJoinTasks.forEachEntry |
4212 |
< |
(this, action).invoke(); |
4210 |
> |
public void forEachEntry(long parallelismThreshold, |
4211 |
> |
Consumer<? super Map.Entry<K,V>> action) { |
4212 |
> |
if (action == null) throw new NullPointerException(); |
4213 |
> |
new ForEachEntryTask<K,V>(null, batchFor(parallelismThreshold), 0, 0, table, |
4214 |
> |
action).invoke(); |
4215 |
|
} |
4216 |
|
|
4217 |
|
/** |
4218 |
|
* Performs the given action for each non-null transformation |
4219 |
|
* of each entry. |
4220 |
|
* |
4221 |
+ |
* @param parallelismThreshold the (estimated) number of elements |
4222 |
+ |
* needed for this operation to be executed in parallel |
4223 |
|
* @param transformer a function returning the transformation |
4224 |
< |
* for an element, or null of there is no transformation (in |
4225 |
< |
* which case the action is not applied). |
4224 |
> |
* for an element, or null if there is no transformation (in |
4225 |
> |
* which case the action is not applied) |
4226 |
|
* @param action the action |
4227 |
+ |
* @param <U> the return type of the transformer |
4228 |
+ |
* @since 1.8 |
4229 |
|
*/ |
4230 |
< |
public <U> void forEachEntry(Fun<Map.Entry<K,V>, ? extends U> transformer, |
4231 |
< |
Action<U> action) { |
4232 |
< |
ForkJoinTasks.forEachEntry |
4233 |
< |
(this, transformer, action).invoke(); |
4230 |
> |
public <U> void forEachEntry(long parallelismThreshold, |
4231 |
> |
Function<Map.Entry<K,V>, ? extends U> transformer, |
4232 |
> |
Consumer<? super U> action) { |
4233 |
> |
if (transformer == null || action == null) |
4234 |
> |
throw new NullPointerException(); |
4235 |
> |
new ForEachTransformedEntryTask<K,V,U> |
4236 |
> |
(null, batchFor(parallelismThreshold), 0, 0, table, |
4237 |
> |
transformer, action).invoke(); |
4238 |
|
} |
4239 |
|
|
4240 |
|
/** |
4244 |
|
* any other parallel invocations of the search function are |
4245 |
|
* ignored. |
4246 |
|
* |
4247 |
+ |
* @param parallelismThreshold the (estimated) number of elements |
4248 |
+ |
* needed for this operation to be executed in parallel |
4249 |
|
* @param searchFunction a function returning a non-null |
4250 |
|
* result on success, else null |
4251 |
+ |
* @param <U> the return type of the search function |
4252 |
|
* @return a non-null result from applying the given search |
4253 |
|
* function on each entry, or null if none |
4254 |
+ |
* @since 1.8 |
4255 |
|
*/ |
4256 |
< |
public <U> U searchEntries(Fun<Map.Entry<K,V>, ? extends U> searchFunction) { |
4257 |
< |
return ForkJoinTasks.searchEntries |
4258 |
< |
(this, searchFunction).invoke(); |
4256 |
> |
public <U> U searchEntries(long parallelismThreshold, |
4257 |
> |
Function<Map.Entry<K,V>, ? extends U> searchFunction) { |
4258 |
> |
if (searchFunction == null) throw new NullPointerException(); |
4259 |
> |
return new SearchEntriesTask<K,V,U> |
4260 |
> |
(null, batchFor(parallelismThreshold), 0, 0, table, |
4261 |
> |
searchFunction, new AtomicReference<U>()).invoke(); |
4262 |
|
} |
4263 |
|
|
4264 |
|
/** |
4265 |
|
* Returns the result of accumulating all entries using the |
4266 |
|
* given reducer to combine values, or null if none. |
4267 |
|
* |
4268 |
+ |
* @param parallelismThreshold the (estimated) number of elements |
4269 |
+ |
* needed for this operation to be executed in parallel |
4270 |
|
* @param reducer a commutative associative combining function |
4271 |
|
* @return the result of accumulating all entries |
4272 |
+ |
* @since 1.8 |
4273 |
|
*/ |
4274 |
< |
public Map.Entry<K,V> reduceEntries(BiFun<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer) { |
4275 |
< |
return ForkJoinTasks.reduceEntries |
4276 |
< |
(this, reducer).invoke(); |
4274 |
> |
public Map.Entry<K,V> reduceEntries(long parallelismThreshold, |
4275 |
> |
BiFunction<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer) { |
4276 |
> |
if (reducer == null) throw new NullPointerException(); |
4277 |
> |
return new ReduceEntriesTask<K,V> |
4278 |
> |
(null, batchFor(parallelismThreshold), 0, 0, table, |
4279 |
> |
null, reducer).invoke(); |
4280 |
|
} |
4281 |
|
|
4282 |
|
/** |
4284 |
|
* of all entries using the given reducer to combine values, |
4285 |
|
* or null if none. |
4286 |
|
* |
4287 |
+ |
* @param parallelismThreshold the (estimated) number of elements |
4288 |
+ |
* needed for this operation to be executed in parallel |
4289 |
|
* @param transformer a function returning the transformation |
4290 |
< |
* for an element, or null of there is no transformation (in |
4291 |
< |
* which case it is not combined). |
4290 |
> |
* for an element, or null if there is no transformation (in |
4291 |
> |
* which case it is not combined) |
4292 |
|
* @param reducer a commutative associative combining function |
4293 |
+ |
* @param <U> the return type of the transformer |
4294 |
|
* @return the result of accumulating the given transformation |
4295 |
|
* of all entries |
4296 |
+ |
* @since 1.8 |
4297 |
|
*/ |
4298 |
< |
public <U> U reduceEntries(Fun<Map.Entry<K,V>, ? extends U> transformer, |
4299 |
< |
BiFun<? super U, ? super U, ? extends U> reducer) { |
4300 |
< |
return ForkJoinTasks.reduceEntries |
4301 |
< |
(this, transformer, reducer).invoke(); |
4298 |
> |
public <U> U reduceEntries(long parallelismThreshold, |
4299 |
> |
Function<Map.Entry<K,V>, ? extends U> transformer, |
4300 |
> |
BiFunction<? super U, ? super U, ? extends U> reducer) { |
4301 |
> |
if (transformer == null || reducer == null) |
4302 |
> |
throw new NullPointerException(); |
4303 |
> |
return new MapReduceEntriesTask<K,V,U> |
4304 |
> |
(null, batchFor(parallelismThreshold), 0, 0, table, |
4305 |
> |
null, transformer, reducer).invoke(); |
4306 |
|
} |
4307 |
|
|
4308 |
|
/** |
4310 |
|
* of all entries using the given reducer to combine values, |
4311 |
|
* and the given basis as an identity value. |
4312 |
|
* |
4313 |
+ |
* @param parallelismThreshold the (estimated) number of elements |
4314 |
+ |
* needed for this operation to be executed in parallel |
4315 |
|
* @param transformer a function returning the transformation |
4316 |
|
* for an element |
4317 |
|
* @param basis the identity (initial default value) for the reduction |
4318 |
|
* @param reducer a commutative associative combining function |
4319 |
|
* @return the result of accumulating the given transformation |
4320 |
|
* of all entries |
4321 |
+ |
* @since 1.8 |
4322 |
|
*/ |
4323 |
< |
public double reduceEntriesToDouble(ObjectToDouble<Map.Entry<K,V>> transformer, |
4323 |
> |
public double reduceEntriesToDouble(long parallelismThreshold, |
4324 |
> |
ToDoubleFunction<Map.Entry<K,V>> transformer, |
4325 |
|
double basis, |
4326 |
< |
DoubleByDoubleToDouble reducer) { |
4327 |
< |
return ForkJoinTasks.reduceEntriesToDouble |
4328 |
< |
(this, transformer, basis, reducer).invoke(); |
4326 |
> |
DoubleBinaryOperator reducer) { |
4327 |
> |
if (transformer == null || reducer == null) |
4328 |
> |
throw new NullPointerException(); |
4329 |
> |
return new MapReduceEntriesToDoubleTask<K,V> |
4330 |
> |
(null, batchFor(parallelismThreshold), 0, 0, table, |
4331 |
> |
null, transformer, basis, reducer).invoke(); |
4332 |
|
} |
4333 |
|
|
4334 |
|
/** |
4336 |
|
* of all entries using the given reducer to combine values, |
4337 |
|
* and the given basis as an identity value. |
4338 |
|
* |
4339 |
+ |
* @param parallelismThreshold the (estimated) number of elements |
4340 |
+ |
* needed for this operation to be executed in parallel |
4341 |
|
* @param transformer a function returning the transformation |
4342 |
|
* for an element |
4343 |
|
* @param basis the identity (initial default value) for the reduction |
4344 |
|
* @param reducer a commutative associative combining function |
4345 |
< |
* @return the result of accumulating the given transformation |
4345 |
> |
* @return the result of accumulating the given transformation |
4346 |
|
* of all entries |
4347 |
+ |
* @since 1.8 |
4348 |
|
*/ |
4349 |
< |
public long reduceEntriesToLong(ObjectToLong<Map.Entry<K,V>> transformer, |
4349 |
> |
public long reduceEntriesToLong(long parallelismThreshold, |
4350 |
> |
ToLongFunction<Map.Entry<K,V>> transformer, |
4351 |
|
long basis, |
4352 |
< |
LongByLongToLong reducer) { |
4353 |
< |
return ForkJoinTasks.reduceEntriesToLong |
4354 |
< |
(this, transformer, basis, reducer).invoke(); |
4352 |
> |
LongBinaryOperator reducer) { |
4353 |
> |
if (transformer == null || reducer == null) |
4354 |
> |
throw new NullPointerException(); |
4355 |
> |
return new MapReduceEntriesToLongTask<K,V> |
4356 |
> |
(null, batchFor(parallelismThreshold), 0, 0, table, |
4357 |
> |
null, transformer, basis, reducer).invoke(); |
4358 |
|
} |
4359 |
|
|
4360 |
|
/** |
4362 |
|
* of all entries using the given reducer to combine values, |
4363 |
|
* and the given basis as an identity value. |
4364 |
|
* |
4365 |
+ |
* @param parallelismThreshold the (estimated) number of elements |
4366 |
+ |
* needed for this operation to be executed in parallel |
4367 |
|
* @param transformer a function returning the transformation |
4368 |
|
* for an element |
4369 |
|
* @param basis the identity (initial default value) for the reduction |
4370 |
|
* @param reducer a commutative associative combining function |
4371 |
|
* @return the result of accumulating the given transformation |
4372 |
|
* of all entries |
4373 |
+ |
* @since 1.8 |
4374 |
|
*/ |
4375 |
< |
public int reduceEntriesToInt(ObjectToInt<Map.Entry<K,V>> transformer, |
4375 |
> |
public int reduceEntriesToInt(long parallelismThreshold, |
4376 |
> |
ToIntFunction<Map.Entry<K,V>> transformer, |
4377 |
|
int basis, |
4378 |
< |
IntByIntToInt reducer) { |
4379 |
< |
return ForkJoinTasks.reduceEntriesToInt |
4380 |
< |
(this, transformer, basis, reducer).invoke(); |
4378 |
> |
IntBinaryOperator reducer) { |
4379 |
> |
if (transformer == null || reducer == null) |
4380 |
> |
throw new NullPointerException(); |
4381 |
> |
return new MapReduceEntriesToIntTask<K,V> |
4382 |
> |
(null, batchFor(parallelismThreshold), 0, 0, table, |
4383 |
> |
null, transformer, basis, reducer).invoke(); |
4384 |
|
} |
4385 |
|
|
4386 |
+ |
|
4387 |
|
/* ----------------Views -------------- */ |
4388 |
|
|
4389 |
|
/** |
4390 |
|
* Base class for views. |
4391 |
|
*/ |
4392 |
< |
static abstract class CHMView<K, V> { |
4393 |
< |
final ConcurrentHashMap<K, V> map; |
4394 |
< |
CHMView(ConcurrentHashMap<K, V> map) { this.map = map; } |
4392 |
> |
abstract static class CollectionView<K,V,E> |
4393 |
> |
implements Collection<E>, java.io.Serializable { |
4394 |
> |
private static final long serialVersionUID = 7249069246763182397L; |
4395 |
> |
final ConcurrentHashMap<K,V> map; |
4396 |
> |
CollectionView(ConcurrentHashMap<K,V> map) { this.map = map; } |
4397 |
|
|
4398 |
|
/** |
4399 |
|
* Returns the map backing this view. |
4402 |
|
*/ |
4403 |
|
public ConcurrentHashMap<K,V> getMap() { return map; } |
4404 |
|
|
4405 |
< |
public final int size() { return map.size(); } |
4406 |
< |
public final boolean isEmpty() { return map.isEmpty(); } |
4407 |
< |
public final void clear() { map.clear(); } |
4405 |
> |
/** |
4406 |
> |
* Removes all of the elements from this view, by removing all |
4407 |
> |
* the mappings from the map backing this view. |
4408 |
> |
*/ |
4409 |
> |
public final void clear() { map.clear(); } |
4410 |
> |
public final int size() { return map.size(); } |
4411 |
> |
public final boolean isEmpty() { return map.isEmpty(); } |
4412 |
|
|
4413 |
|
// implementations below rely on concrete classes supplying these |
4414 |
< |
abstract public Iterator<?> iterator(); |
4415 |
< |
abstract public boolean contains(Object o); |
4416 |
< |
abstract public boolean remove(Object o); |
4414 |
> |
// abstract methods |
4415 |
> |
/** |
4416 |
> |
* Returns an iterator over the elements in this collection. |
4417 |
> |
* |
4418 |
> |
* <p>The returned iterator is |
4419 |
> |
* <a href="package-summary.html#Weakly"><i>weakly consistent</i></a>. |
4420 |
> |
* |
4421 |
> |
* @return an iterator over the elements in this collection |
4422 |
> |
*/ |
4423 |
> |
public abstract Iterator<E> iterator(); |
4424 |
> |
public abstract boolean contains(Object o); |
4425 |
> |
public abstract boolean remove(Object o); |
4426 |
|
|
4427 |
< |
private static final String oomeMsg = "Required array size too large"; |
4427 |
> |
private static final String OOME_MSG = "Required array size too large"; |
4428 |
|
|
4429 |
|
public final Object[] toArray() { |
4430 |
|
long sz = map.mappingCount(); |
4431 |
< |
if (sz > (long)(MAX_ARRAY_SIZE)) |
4432 |
< |
throw new OutOfMemoryError(oomeMsg); |
4431 |
> |
if (sz > MAX_ARRAY_SIZE) |
4432 |
> |
throw new OutOfMemoryError(OOME_MSG); |
4433 |
|
int n = (int)sz; |
4434 |
|
Object[] r = new Object[n]; |
4435 |
|
int i = 0; |
4436 |
< |
Iterator<?> it = iterator(); |
3992 |
< |
while (it.hasNext()) { |
4436 |
> |
for (E e : this) { |
4437 |
|
if (i == n) { |
4438 |
|
if (n >= MAX_ARRAY_SIZE) |
4439 |
< |
throw new OutOfMemoryError(oomeMsg); |
4439 |
> |
throw new OutOfMemoryError(OOME_MSG); |
4440 |
|
if (n >= MAX_ARRAY_SIZE - (MAX_ARRAY_SIZE >>> 1) - 1) |
4441 |
|
n = MAX_ARRAY_SIZE; |
4442 |
|
else |
4443 |
|
n += (n >>> 1) + 1; |
4444 |
|
r = Arrays.copyOf(r, n); |
4445 |
|
} |
4446 |
< |
r[i++] = it.next(); |
4446 |
> |
r[i++] = e; |
4447 |
|
} |
4448 |
|
return (i == n) ? r : Arrays.copyOf(r, i); |
4449 |
|
} |
4450 |
|
|
4451 |
< |
@SuppressWarnings("unchecked") public final <T> T[] toArray(T[] a) { |
4451 |
> |
@SuppressWarnings("unchecked") |
4452 |
> |
public final <T> T[] toArray(T[] a) { |
4453 |
|
long sz = map.mappingCount(); |
4454 |
< |
if (sz > (long)(MAX_ARRAY_SIZE)) |
4455 |
< |
throw new OutOfMemoryError(oomeMsg); |
4454 |
> |
if (sz > MAX_ARRAY_SIZE) |
4455 |
> |
throw new OutOfMemoryError(OOME_MSG); |
4456 |
|
int m = (int)sz; |
4457 |
|
T[] r = (a.length >= m) ? a : |
4458 |
|
(T[])java.lang.reflect.Array |
4459 |
|
.newInstance(a.getClass().getComponentType(), m); |
4460 |
|
int n = r.length; |
4461 |
|
int i = 0; |
4462 |
< |
Iterator<?> it = iterator(); |
4018 |
< |
while (it.hasNext()) { |
4462 |
> |
for (E e : this) { |
4463 |
|
if (i == n) { |
4464 |
|
if (n >= MAX_ARRAY_SIZE) |
4465 |
< |
throw new OutOfMemoryError(oomeMsg); |
4465 |
> |
throw new OutOfMemoryError(OOME_MSG); |
4466 |
|
if (n >= MAX_ARRAY_SIZE - (MAX_ARRAY_SIZE >>> 1) - 1) |
4467 |
|
n = MAX_ARRAY_SIZE; |
4468 |
|
else |
4469 |
|
n += (n >>> 1) + 1; |
4470 |
|
r = Arrays.copyOf(r, n); |
4471 |
|
} |
4472 |
< |
r[i++] = (T)it.next(); |
4472 |
> |
r[i++] = (T)e; |
4473 |
|
} |
4474 |
|
if (a == r && i < n) { |
4475 |
|
r[i] = null; // null-terminate |
4478 |
|
return (i == n) ? r : Arrays.copyOf(r, i); |
4479 |
|
} |
4480 |
|
|
4481 |
< |
public final int hashCode() { |
4482 |
< |
int h = 0; |
4483 |
< |
for (Iterator<?> it = iterator(); it.hasNext();) |
4484 |
< |
h += it.next().hashCode(); |
4485 |
< |
return h; |
4486 |
< |
} |
4487 |
< |
|
4481 |
> |
/** |
4482 |
> |
* Returns a string representation of this collection. |
4483 |
> |
* The string representation consists of the string representations |
4484 |
> |
* of the collection's elements in the order they are returned by |
4485 |
> |
* its iterator, enclosed in square brackets ({@code "[]"}). |
4486 |
> |
* Adjacent elements are separated by the characters {@code ", "} |
4487 |
> |
* (comma and space). Elements are converted to strings as by |
4488 |
> |
* {@link String#valueOf(Object)}. |
4489 |
> |
* |
4490 |
> |
* @return a string representation of this collection |
4491 |
> |
*/ |
4492 |
|
public final String toString() { |
4493 |
|
StringBuilder sb = new StringBuilder(); |
4494 |
|
sb.append('['); |
4495 |
< |
Iterator<?> it = iterator(); |
4495 |
> |
Iterator<E> it = iterator(); |
4496 |
|
if (it.hasNext()) { |
4497 |
|
for (;;) { |
4498 |
|
Object e = it.next(); |
4507 |
|
|
4508 |
|
public final boolean containsAll(Collection<?> c) { |
4509 |
|
if (c != this) { |
4510 |
< |
for (Iterator<?> it = c.iterator(); it.hasNext();) { |
4063 |
< |
Object e = it.next(); |
4510 |
> |
for (Object e : c) { |
4511 |
|
if (e == null || !contains(e)) |
4512 |
|
return false; |
4513 |
|
} |
4515 |
|
return true; |
4516 |
|
} |
4517 |
|
|
4518 |
< |
public final boolean removeAll(Collection<?> c) { |
4518 |
> |
public boolean removeAll(Collection<?> c) { |
4519 |
> |
if (c == null) throw new NullPointerException(); |
4520 |
|
boolean modified = false; |
4521 |
< |
for (Iterator<?> it = iterator(); it.hasNext();) { |
4522 |
< |
if (c.contains(it.next())) { |
4523 |
< |
it.remove(); |
4524 |
< |
modified = true; |
4521 |
> |
// Use (c instanceof Set) as a hint that lookup in c is as |
4522 |
> |
// efficient as this view |
4523 |
> |
Node<K,V>[] t; |
4524 |
> |
if ((t = map.table) == null) { |
4525 |
> |
return false; |
4526 |
> |
} else if (c instanceof Set<?> && c.size() > t.length) { |
4527 |
> |
for (Iterator<?> it = iterator(); it.hasNext(); ) { |
4528 |
> |
if (c.contains(it.next())) { |
4529 |
> |
it.remove(); |
4530 |
> |
modified = true; |
4531 |
> |
} |
4532 |
|
} |
4533 |
+ |
} else { |
4534 |
+ |
for (Object e : c) |
4535 |
+ |
modified |= remove(e); |
4536 |
|
} |
4537 |
|
return modified; |
4538 |
|
} |
4539 |
|
|
4540 |
|
public final boolean retainAll(Collection<?> c) { |
4541 |
+ |
if (c == null) throw new NullPointerException(); |
4542 |
|
boolean modified = false; |
4543 |
< |
for (Iterator<?> it = iterator(); it.hasNext();) { |
4543 |
> |
for (Iterator<E> it = iterator(); it.hasNext();) { |
4544 |
|
if (!c.contains(it.next())) { |
4545 |
|
it.remove(); |
4546 |
|
modified = true; |
4554 |
|
/** |
4555 |
|
* A view of a ConcurrentHashMap as a {@link Set} of keys, in |
4556 |
|
* which additions may optionally be enabled by mapping to a |
4557 |
< |
* common value. This class cannot be directly instantiated. See |
4558 |
< |
* {@link #keySet}, {@link #keySet(Object)}, {@link #newKeySet()}, |
4559 |
< |
* {@link #newKeySet(int)}. |
4557 |
> |
* common value. This class cannot be directly instantiated. |
4558 |
> |
* See {@link #keySet() keySet()}, |
4559 |
> |
* {@link #keySet(Object) keySet(V)}, |
4560 |
> |
* {@link #newKeySet() newKeySet()}, |
4561 |
> |
* {@link #newKeySet(int) newKeySet(int)}. |
4562 |
> |
* |
4563 |
> |
* @since 1.8 |
4564 |
|
*/ |
4565 |
< |
public static class KeySetView<K,V> extends CHMView<K,V> implements Set<K>, java.io.Serializable { |
4565 |
> |
public static class KeySetView<K,V> extends CollectionView<K,V,K> |
4566 |
> |
implements Set<K>, java.io.Serializable { |
4567 |
|
private static final long serialVersionUID = 7249069246763182397L; |
4568 |
|
private final V value; |
4569 |
< |
KeySetView(ConcurrentHashMap<K, V> map, V value) { // non-public |
4569 |
> |
KeySetView(ConcurrentHashMap<K,V> map, V value) { // non-public |
4570 |
|
super(map); |
4571 |
|
this.value = value; |
4572 |
|
} |
4576 |
|
* or {@code null} if additions are not supported. |
4577 |
|
* |
4578 |
|
* @return the default mapped value for additions, or {@code null} |
4579 |
< |
* if not supported. |
4579 |
> |
* if not supported |
4580 |
|
*/ |
4581 |
|
public V getMappedValue() { return value; } |
4582 |
|
|
4583 |
< |
// implement Set API |
4584 |
< |
|
4583 |
> |
/** |
4584 |
> |
* {@inheritDoc} |
4585 |
> |
* @throws NullPointerException if the specified key is null |
4586 |
> |
*/ |
4587 |
|
public boolean contains(Object o) { return map.containsKey(o); } |
4122 |
– |
public boolean remove(Object o) { return map.remove(o) != null; } |
4588 |
|
|
4589 |
|
/** |
4590 |
< |
* Returns a "weakly consistent" iterator that will never |
4591 |
< |
* throw {@link ConcurrentModificationException}, and |
4592 |
< |
* guarantees to traverse elements as they existed upon |
4593 |
< |
* construction of the iterator, and may (but is not |
4594 |
< |
* guaranteed to) reflect any modifications subsequent to |
4595 |
< |
* construction. |
4590 |
> |
* Removes the key from this map view, by removing the key (and its |
4591 |
> |
* corresponding value) from the backing map. This method does |
4592 |
> |
* nothing if the key is not in the map. |
4593 |
> |
* |
4594 |
> |
* @param o the key to be removed from the backing map |
4595 |
> |
* @return {@code true} if the backing map contained the specified key |
4596 |
> |
* @throws NullPointerException if the specified key is null |
4597 |
> |
*/ |
4598 |
> |
public boolean remove(Object o) { return map.remove(o) != null; } |
4599 |
> |
|
4600 |
> |
/** |
4601 |
> |
* @return an iterator over the keys of the backing map |
4602 |
> |
*/ |
4603 |
> |
public Iterator<K> iterator() { |
4604 |
> |
Node<K,V>[] t; |
4605 |
> |
ConcurrentHashMap<K,V> m = map; |
4606 |
> |
int f = (t = m.table) == null ? 0 : t.length; |
4607 |
> |
return new KeyIterator<K,V>(t, f, 0, f, m); |
4608 |
> |
} |
4609 |
> |
|
4610 |
> |
/** |
4611 |
> |
* Adds the specified key to this set view by mapping the key to |
4612 |
> |
* the default mapped value in the backing map, if defined. |
4613 |
|
* |
4614 |
< |
* @return an iterator over the keys of this map |
4614 |
> |
* @param e key to be added |
4615 |
> |
* @return {@code true} if this set changed as a result of the call |
4616 |
> |
* @throws NullPointerException if the specified key is null |
4617 |
> |
* @throws UnsupportedOperationException if no default mapped value |
4618 |
> |
* for additions was provided |
4619 |
|
*/ |
4134 |
– |
public Iterator<K> iterator() { return new KeyIterator<K,V>(map); } |
4620 |
|
public boolean add(K e) { |
4621 |
|
V v; |
4622 |
|
if ((v = value) == null) |
4623 |
|
throw new UnsupportedOperationException(); |
4624 |
< |
if (e == null) |
4140 |
< |
throw new NullPointerException(); |
4141 |
< |
return map.internalPutIfAbsent(e, v) == null; |
4624 |
> |
return map.putVal(e, v, true) == null; |
4625 |
|
} |
4626 |
+ |
|
4627 |
+ |
/** |
4628 |
+ |
* Adds all of the elements in the specified collection to this set, |
4629 |
+ |
* as if by calling {@link #add} on each one. |
4630 |
+ |
* |
4631 |
+ |
* @param c the elements to be inserted into this set |
4632 |
+ |
* @return {@code true} if this set changed as a result of the call |
4633 |
+ |
* @throws NullPointerException if the collection or any of its |
4634 |
+ |
* elements are {@code null} |
4635 |
+ |
* @throws UnsupportedOperationException if no default mapped value |
4636 |
+ |
* for additions was provided |
4637 |
+ |
*/ |
4638 |
|
public boolean addAll(Collection<? extends K> c) { |
4639 |
|
boolean added = false; |
4640 |
|
V v; |
4641 |
|
if ((v = value) == null) |
4642 |
|
throw new UnsupportedOperationException(); |
4643 |
|
for (K e : c) { |
4644 |
< |
if (e == null) |
4150 |
< |
throw new NullPointerException(); |
4151 |
< |
if (map.internalPutIfAbsent(e, v) == null) |
4644 |
> |
if (map.putVal(e, v, true) == null) |
4645 |
|
added = true; |
4646 |
|
} |
4647 |
|
return added; |
4648 |
|
} |
4649 |
+ |
|
4650 |
+ |
public int hashCode() { |
4651 |
+ |
int h = 0; |
4652 |
+ |
for (K e : this) |
4653 |
+ |
h += e.hashCode(); |
4654 |
+ |
return h; |
4655 |
+ |
} |
4656 |
+ |
|
4657 |
|
public boolean equals(Object o) { |
4658 |
|
Set<?> c; |
4659 |
|
return ((o instanceof Set) && |
4661 |
|
(containsAll(c) && c.containsAll(this)))); |
4662 |
|
} |
4663 |
|
|
4664 |
< |
/** |
4665 |
< |
* Performs the given action for each key. |
4666 |
< |
* |
4667 |
< |
* @param action the action |
4668 |
< |
*/ |
4669 |
< |
public void forEach(Action<K> action) { |
4169 |
< |
ForkJoinTasks.forEachKey |
4170 |
< |
(map, action).invoke(); |
4171 |
< |
} |
4172 |
< |
|
4173 |
< |
/** |
4174 |
< |
* Performs the given action for each non-null transformation |
4175 |
< |
* of each key. |
4176 |
< |
* |
4177 |
< |
* @param transformer a function returning the transformation |
4178 |
< |
* for an element, or null of there is no transformation (in |
4179 |
< |
* which case the action is not applied). |
4180 |
< |
* @param action the action |
4181 |
< |
*/ |
4182 |
< |
public <U> void forEach(Fun<? super K, ? extends U> transformer, |
4183 |
< |
Action<U> action) { |
4184 |
< |
ForkJoinTasks.forEachKey |
4185 |
< |
(map, transformer, action).invoke(); |
4664 |
> |
public Spliterator<K> spliterator() { |
4665 |
> |
Node<K,V>[] t; |
4666 |
> |
ConcurrentHashMap<K,V> m = map; |
4667 |
> |
long n = m.sumCount(); |
4668 |
> |
int f = (t = m.table) == null ? 0 : t.length; |
4669 |
> |
return new KeySpliterator<K,V>(t, f, 0, f, n < 0L ? 0L : n); |
4670 |
|
} |
4671 |
|
|
4672 |
< |
/** |
4673 |
< |
* Returns a non-null result from applying the given search |
4674 |
< |
* function on each key, or null if none. Upon success, |
4675 |
< |
* further element processing is suppressed and the results of |
4676 |
< |
* any other parallel invocations of the search function are |
4677 |
< |
* ignored. |
4678 |
< |
* |
4679 |
< |
* @param searchFunction a function returning a non-null |
4196 |
< |
* result on success, else null |
4197 |
< |
* @return a non-null result from applying the given search |
4198 |
< |
* function on each key, or null if none |
4199 |
< |
*/ |
4200 |
< |
public <U> U search(Fun<? super K, ? extends U> searchFunction) { |
4201 |
< |
return ForkJoinTasks.searchKeys |
4202 |
< |
(map, searchFunction).invoke(); |
4203 |
< |
} |
4204 |
< |
|
4205 |
< |
/** |
4206 |
< |
* Returns the result of accumulating all keys using the given |
4207 |
< |
* reducer to combine values, or null if none. |
4208 |
< |
* |
4209 |
< |
* @param reducer a commutative associative combining function |
4210 |
< |
* @return the result of accumulating all keys using the given |
4211 |
< |
* reducer to combine values, or null if none |
4212 |
< |
*/ |
4213 |
< |
public K reduce(BiFun<? super K, ? super K, ? extends K> reducer) { |
4214 |
< |
return ForkJoinTasks.reduceKeys |
4215 |
< |
(map, reducer).invoke(); |
4216 |
< |
} |
4217 |
< |
|
4218 |
< |
/** |
4219 |
< |
* Returns the result of accumulating the given transformation |
4220 |
< |
* of all keys using the given reducer to combine values, and |
4221 |
< |
* the given basis as an identity value. |
4222 |
< |
* |
4223 |
< |
* @param transformer a function returning the transformation |
4224 |
< |
* for an element |
4225 |
< |
* @param basis the identity (initial default value) for the reduction |
4226 |
< |
* @param reducer a commutative associative combining function |
4227 |
< |
* @return the result of accumulating the given transformation |
4228 |
< |
* of all keys |
4229 |
< |
*/ |
4230 |
< |
public double reduceToDouble(ObjectToDouble<? super K> transformer, |
4231 |
< |
double basis, |
4232 |
< |
DoubleByDoubleToDouble reducer) { |
4233 |
< |
return ForkJoinTasks.reduceKeysToDouble |
4234 |
< |
(map, transformer, basis, reducer).invoke(); |
4235 |
< |
} |
4236 |
< |
|
4237 |
< |
|
4238 |
< |
/** |
4239 |
< |
* Returns the result of accumulating the given transformation |
4240 |
< |
* of all keys using the given reducer to combine values, and |
4241 |
< |
* the given basis as an identity value. |
4242 |
< |
* |
4243 |
< |
* @param transformer a function returning the transformation |
4244 |
< |
* for an element |
4245 |
< |
* @param basis the identity (initial default value) for the reduction |
4246 |
< |
* @param reducer a commutative associative combining function |
4247 |
< |
* @return the result of accumulating the given transformation |
4248 |
< |
* of all keys |
4249 |
< |
*/ |
4250 |
< |
public long reduceToLong(ObjectToLong<? super K> transformer, |
4251 |
< |
long basis, |
4252 |
< |
LongByLongToLong reducer) { |
4253 |
< |
return ForkJoinTasks.reduceKeysToLong |
4254 |
< |
(map, transformer, basis, reducer).invoke(); |
4255 |
< |
} |
4256 |
< |
|
4257 |
< |
/** |
4258 |
< |
* Returns the result of accumulating the given transformation |
4259 |
< |
* of all keys using the given reducer to combine values, and |
4260 |
< |
* the given basis as an identity value. |
4261 |
< |
* |
4262 |
< |
* @param transformer a function returning the transformation |
4263 |
< |
* for an element |
4264 |
< |
* @param basis the identity (initial default value) for the reduction |
4265 |
< |
* @param reducer a commutative associative combining function |
4266 |
< |
* @return the result of accumulating the given transformation |
4267 |
< |
* of all keys |
4268 |
< |
*/ |
4269 |
< |
public int reduceToInt(ObjectToInt<? super K> transformer, |
4270 |
< |
int basis, |
4271 |
< |
IntByIntToInt reducer) { |
4272 |
< |
return ForkJoinTasks.reduceKeysToInt |
4273 |
< |
(map, transformer, basis, reducer).invoke(); |
4672 |
> |
public void forEach(Consumer<? super K> action) { |
4673 |
> |
if (action == null) throw new NullPointerException(); |
4674 |
> |
Node<K,V>[] t; |
4675 |
> |
if ((t = map.table) != null) { |
4676 |
> |
Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length); |
4677 |
> |
for (Node<K,V> p; (p = it.advance()) != null; ) |
4678 |
> |
action.accept(p.key); |
4679 |
> |
} |
4680 |
|
} |
4275 |
– |
|
4681 |
|
} |
4682 |
|
|
4683 |
|
/** |
4684 |
|
* A view of a ConcurrentHashMap as a {@link Collection} of |
4685 |
|
* values, in which additions are disabled. This class cannot be |
4686 |
< |
* directly instantiated. See {@link #values}, |
4687 |
< |
* |
4688 |
< |
* <p>The view's {@code iterator} is a "weakly consistent" iterator |
4689 |
< |
* that will never throw {@link ConcurrentModificationException}, |
4690 |
< |
* and guarantees to traverse elements as they existed upon |
4691 |
< |
* construction of the iterator, and may (but is not guaranteed to) |
4692 |
< |
* reflect any modifications subsequent to construction. |
4693 |
< |
*/ |
4694 |
< |
public static final class ValuesView<K,V> extends CHMView<K,V> |
4695 |
< |
implements Collection<V> { |
4291 |
< |
ValuesView(ConcurrentHashMap<K, V> map) { super(map); } |
4292 |
< |
public final boolean contains(Object o) { return map.containsValue(o); } |
4686 |
> |
* directly instantiated. See {@link #values()}. |
4687 |
> |
*/ |
4688 |
> |
static final class ValuesView<K,V> extends CollectionView<K,V,V> |
4689 |
> |
implements Collection<V>, java.io.Serializable { |
4690 |
> |
private static final long serialVersionUID = 2249069246763182397L; |
4691 |
> |
ValuesView(ConcurrentHashMap<K,V> map) { super(map); } |
4692 |
> |
public final boolean contains(Object o) { |
4693 |
> |
return map.containsValue(o); |
4694 |
> |
} |
4695 |
> |
|
4696 |
|
public final boolean remove(Object o) { |
4697 |
|
if (o != null) { |
4698 |
< |
Iterator<V> it = new ValueIterator<K,V>(map); |
4296 |
< |
while (it.hasNext()) { |
4698 |
> |
for (Iterator<V> it = iterator(); it.hasNext();) { |
4699 |
|
if (o.equals(it.next())) { |
4700 |
|
it.remove(); |
4701 |
|
return true; |
4705 |
|
return false; |
4706 |
|
} |
4707 |
|
|
4306 |
– |
/** |
4307 |
– |
* Returns a "weakly consistent" iterator that will never |
4308 |
– |
* throw {@link ConcurrentModificationException}, and |
4309 |
– |
* guarantees to traverse elements as they existed upon |
4310 |
– |
* construction of the iterator, and may (but is not |
4311 |
– |
* guaranteed to) reflect any modifications subsequent to |
4312 |
– |
* construction. |
4313 |
– |
* |
4314 |
– |
* @return an iterator over the values of this map |
4315 |
– |
*/ |
4708 |
|
public final Iterator<V> iterator() { |
4709 |
< |
return new ValueIterator<K,V>(map); |
4709 |
> |
ConcurrentHashMap<K,V> m = map; |
4710 |
> |
Node<K,V>[] t; |
4711 |
> |
int f = (t = m.table) == null ? 0 : t.length; |
4712 |
> |
return new ValueIterator<K,V>(t, f, 0, f, m); |
4713 |
|
} |
4714 |
+ |
|
4715 |
|
public final boolean add(V e) { |
4716 |
|
throw new UnsupportedOperationException(); |
4717 |
|
} |
4719 |
|
throw new UnsupportedOperationException(); |
4720 |
|
} |
4721 |
|
|
4722 |
< |
/** |
4723 |
< |
* Performs the given action for each value. |
4724 |
< |
* |
4725 |
< |
* @param action the action |
4726 |
< |
*/ |
4727 |
< |
public void forEach(Action<V> action) { |
4728 |
< |
ForkJoinTasks.forEachValue |
4729 |
< |
(map, action).invoke(); |
4730 |
< |
} |
4731 |
< |
|
4336 |
< |
/** |
4337 |
< |
* Performs the given action for each non-null transformation |
4338 |
< |
* of each value. |
4339 |
< |
* |
4340 |
< |
* @param transformer a function returning the transformation |
4341 |
< |
* for an element, or null of there is no transformation (in |
4342 |
< |
* which case the action is not applied). |
4343 |
< |
*/ |
4344 |
< |
public <U> void forEach(Fun<? super V, ? extends U> transformer, |
4345 |
< |
Action<U> action) { |
4346 |
< |
ForkJoinTasks.forEachValue |
4347 |
< |
(map, transformer, action).invoke(); |
4348 |
< |
} |
4349 |
< |
|
4350 |
< |
/** |
4351 |
< |
* Returns a non-null result from applying the given search |
4352 |
< |
* function on each value, or null if none. Upon success, |
4353 |
< |
* further element processing is suppressed and the results of |
4354 |
< |
* any other parallel invocations of the search function are |
4355 |
< |
* ignored. |
4356 |
< |
* |
4357 |
< |
* @param searchFunction a function returning a non-null |
4358 |
< |
* result on success, else null |
4359 |
< |
* @return a non-null result from applying the given search |
4360 |
< |
* function on each value, or null if none |
4361 |
< |
* |
4362 |
< |
*/ |
4363 |
< |
public <U> U search(Fun<? super V, ? extends U> searchFunction) { |
4364 |
< |
return ForkJoinTasks.searchValues |
4365 |
< |
(map, searchFunction).invoke(); |
4366 |
< |
} |
4367 |
< |
|
4368 |
< |
/** |
4369 |
< |
* Returns the result of accumulating all values using the |
4370 |
< |
* given reducer to combine values, or null if none. |
4371 |
< |
* |
4372 |
< |
* @param reducer a commutative associative combining function |
4373 |
< |
* @return the result of accumulating all values |
4374 |
< |
*/ |
4375 |
< |
public V reduce(BiFun<? super V, ? super V, ? extends V> reducer) { |
4376 |
< |
return ForkJoinTasks.reduceValues |
4377 |
< |
(map, reducer).invoke(); |
4378 |
< |
} |
4379 |
< |
|
4380 |
< |
/** |
4381 |
< |
* Returns the result of accumulating the given transformation |
4382 |
< |
* of all values using the given reducer to combine values, or |
4383 |
< |
* null if none. |
4384 |
< |
* |
4385 |
< |
* @param transformer a function returning the transformation |
4386 |
< |
* for an element, or null of there is no transformation (in |
4387 |
< |
* which case it is not combined). |
4388 |
< |
* @param reducer a commutative associative combining function |
4389 |
< |
* @return the result of accumulating the given transformation |
4390 |
< |
* of all values |
4391 |
< |
*/ |
4392 |
< |
public <U> U reduce(Fun<? super V, ? extends U> transformer, |
4393 |
< |
BiFun<? super U, ? super U, ? extends U> reducer) { |
4394 |
< |
return ForkJoinTasks.reduceValues |
4395 |
< |
(map, transformer, reducer).invoke(); |
4722 |
> |
@Override public boolean removeAll(Collection<?> c) { |
4723 |
> |
if (c == null) throw new NullPointerException(); |
4724 |
> |
boolean modified = false; |
4725 |
> |
for (Iterator<V> it = iterator(); it.hasNext();) { |
4726 |
> |
if (c.contains(it.next())) { |
4727 |
> |
it.remove(); |
4728 |
> |
modified = true; |
4729 |
> |
} |
4730 |
> |
} |
4731 |
> |
return modified; |
4732 |
|
} |
4733 |
|
|
4734 |
< |
/** |
4735 |
< |
* Returns the result of accumulating the given transformation |
4400 |
< |
* of all values using the given reducer to combine values, |
4401 |
< |
* and the given basis as an identity value. |
4402 |
< |
* |
4403 |
< |
* @param transformer a function returning the transformation |
4404 |
< |
* for an element |
4405 |
< |
* @param basis the identity (initial default value) for the reduction |
4406 |
< |
* @param reducer a commutative associative combining function |
4407 |
< |
* @return the result of accumulating the given transformation |
4408 |
< |
* of all values |
4409 |
< |
*/ |
4410 |
< |
public double reduceToDouble(ObjectToDouble<? super V> transformer, |
4411 |
< |
double basis, |
4412 |
< |
DoubleByDoubleToDouble reducer) { |
4413 |
< |
return ForkJoinTasks.reduceValuesToDouble |
4414 |
< |
(map, transformer, basis, reducer).invoke(); |
4734 |
> |
public boolean removeIf(Predicate<? super V> filter) { |
4735 |
> |
return map.removeValueIf(filter); |
4736 |
|
} |
4737 |
|
|
4738 |
< |
/** |
4739 |
< |
* Returns the result of accumulating the given transformation |
4740 |
< |
* of all values using the given reducer to combine values, |
4741 |
< |
* and the given basis as an identity value. |
4742 |
< |
* |
4743 |
< |
* @param transformer a function returning the transformation |
4423 |
< |
* for an element |
4424 |
< |
* @param basis the identity (initial default value) for the reduction |
4425 |
< |
* @param reducer a commutative associative combining function |
4426 |
< |
* @return the result of accumulating the given transformation |
4427 |
< |
* of all values |
4428 |
< |
*/ |
4429 |
< |
public long reduceToLong(ObjectToLong<? super V> transformer, |
4430 |
< |
long basis, |
4431 |
< |
LongByLongToLong reducer) { |
4432 |
< |
return ForkJoinTasks.reduceValuesToLong |
4433 |
< |
(map, transformer, basis, reducer).invoke(); |
4738 |
> |
public Spliterator<V> spliterator() { |
4739 |
> |
Node<K,V>[] t; |
4740 |
> |
ConcurrentHashMap<K,V> m = map; |
4741 |
> |
long n = m.sumCount(); |
4742 |
> |
int f = (t = m.table) == null ? 0 : t.length; |
4743 |
> |
return new ValueSpliterator<K,V>(t, f, 0, f, n < 0L ? 0L : n); |
4744 |
|
} |
4745 |
|
|
4746 |
< |
/** |
4747 |
< |
* Returns the result of accumulating the given transformation |
4748 |
< |
* of all values using the given reducer to combine values, |
4749 |
< |
* and the given basis as an identity value. |
4750 |
< |
* |
4751 |
< |
* @param transformer a function returning the transformation |
4752 |
< |
* for an element |
4753 |
< |
* @param basis the identity (initial default value) for the reduction |
4444 |
< |
* @param reducer a commutative associative combining function |
4445 |
< |
* @return the result of accumulating the given transformation |
4446 |
< |
* of all values |
4447 |
< |
*/ |
4448 |
< |
public int reduceToInt(ObjectToInt<? super V> transformer, |
4449 |
< |
int basis, |
4450 |
< |
IntByIntToInt reducer) { |
4451 |
< |
return ForkJoinTasks.reduceValuesToInt |
4452 |
< |
(map, transformer, basis, reducer).invoke(); |
4746 |
> |
public void forEach(Consumer<? super V> action) { |
4747 |
> |
if (action == null) throw new NullPointerException(); |
4748 |
> |
Node<K,V>[] t; |
4749 |
> |
if ((t = map.table) != null) { |
4750 |
> |
Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length); |
4751 |
> |
for (Node<K,V> p; (p = it.advance()) != null; ) |
4752 |
> |
action.accept(p.val); |
4753 |
> |
} |
4754 |
|
} |
4454 |
– |
|
4755 |
|
} |
4756 |
|
|
4757 |
|
/** |
4758 |
|
* A view of a ConcurrentHashMap as a {@link Set} of (key, value) |
4759 |
|
* entries. This class cannot be directly instantiated. See |
4760 |
< |
* {@link #entrySet}. |
4760 |
> |
* {@link #entrySet()}. |
4761 |
|
*/ |
4762 |
< |
public static final class EntrySetView<K,V> extends CHMView<K,V> |
4763 |
< |
implements Set<Map.Entry<K,V>> { |
4764 |
< |
EntrySetView(ConcurrentHashMap<K, V> map) { super(map); } |
4765 |
< |
public final boolean contains(Object o) { |
4762 |
> |
static final class EntrySetView<K,V> extends CollectionView<K,V,Map.Entry<K,V>> |
4763 |
> |
implements Set<Map.Entry<K,V>>, java.io.Serializable { |
4764 |
> |
private static final long serialVersionUID = 2249069246763182397L; |
4765 |
> |
EntrySetView(ConcurrentHashMap<K,V> map) { super(map); } |
4766 |
> |
|
4767 |
> |
public boolean contains(Object o) { |
4768 |
|
Object k, v, r; Map.Entry<?,?> e; |
4769 |
|
return ((o instanceof Map.Entry) && |
4770 |
|
(k = (e = (Map.Entry<?,?>)o).getKey()) != null && |
4772 |
|
(v = e.getValue()) != null && |
4773 |
|
(v == r || v.equals(r))); |
4774 |
|
} |
4775 |
< |
public final boolean remove(Object o) { |
4775 |
> |
|
4776 |
> |
public boolean remove(Object o) { |
4777 |
|
Object k, v; Map.Entry<?,?> e; |
4778 |
|
return ((o instanceof Map.Entry) && |
4779 |
|
(k = (e = (Map.Entry<?,?>)o).getKey()) != null && |
4782 |
|
} |
4783 |
|
|
4784 |
|
/** |
4785 |
< |
* Returns a "weakly consistent" iterator that will never |
4483 |
< |
* throw {@link ConcurrentModificationException}, and |
4484 |
< |
* guarantees to traverse elements as they existed upon |
4485 |
< |
* construction of the iterator, and may (but is not |
4486 |
< |
* guaranteed to) reflect any modifications subsequent to |
4487 |
< |
* construction. |
4488 |
< |
* |
4489 |
< |
* @return an iterator over the entries of this map |
4785 |
> |
* @return an iterator over the entries of the backing map |
4786 |
|
*/ |
4787 |
< |
public final Iterator<Map.Entry<K,V>> iterator() { |
4788 |
< |
return new EntryIterator<K,V>(map); |
4787 |
> |
public Iterator<Map.Entry<K,V>> iterator() { |
4788 |
> |
ConcurrentHashMap<K,V> m = map; |
4789 |
> |
Node<K,V>[] t; |
4790 |
> |
int f = (t = m.table) == null ? 0 : t.length; |
4791 |
> |
return new EntryIterator<K,V>(t, f, 0, f, m); |
4792 |
|
} |
4793 |
|
|
4794 |
< |
public final boolean add(Entry<K,V> e) { |
4795 |
< |
K key = e.getKey(); |
4497 |
< |
V value = e.getValue(); |
4498 |
< |
if (key == null || value == null) |
4499 |
< |
throw new NullPointerException(); |
4500 |
< |
return map.internalPut(key, value) == null; |
4794 |
> |
public boolean add(Entry<K,V> e) { |
4795 |
> |
return map.putVal(e.getKey(), e.getValue(), false) == null; |
4796 |
|
} |
4797 |
< |
public final boolean addAll(Collection<? extends Entry<K,V>> c) { |
4797 |
> |
|
4798 |
> |
public boolean addAll(Collection<? extends Entry<K,V>> c) { |
4799 |
|
boolean added = false; |
4800 |
|
for (Entry<K,V> e : c) { |
4801 |
|
if (add(e)) |
4803 |
|
} |
4804 |
|
return added; |
4805 |
|
} |
4510 |
– |
public boolean equals(Object o) { |
4511 |
– |
Set<?> c; |
4512 |
– |
return ((o instanceof Set) && |
4513 |
– |
((c = (Set<?>)o) == this || |
4514 |
– |
(containsAll(c) && c.containsAll(this)))); |
4515 |
– |
} |
4516 |
– |
|
4517 |
– |
/** |
4518 |
– |
* Performs the given action for each entry. |
4519 |
– |
* |
4520 |
– |
* @param action the action |
4521 |
– |
*/ |
4522 |
– |
public void forEach(Action<Map.Entry<K,V>> action) { |
4523 |
– |
ForkJoinTasks.forEachEntry |
4524 |
– |
(map, action).invoke(); |
4525 |
– |
} |
4526 |
– |
|
4527 |
– |
/** |
4528 |
– |
* Performs the given action for each non-null transformation |
4529 |
– |
* of each entry. |
4530 |
– |
* |
4531 |
– |
* @param transformer a function returning the transformation |
4532 |
– |
* for an element, or null of there is no transformation (in |
4533 |
– |
* which case the action is not applied). |
4534 |
– |
* @param action the action |
4535 |
– |
*/ |
4536 |
– |
public <U> void forEach(Fun<Map.Entry<K,V>, ? extends U> transformer, |
4537 |
– |
Action<U> action) { |
4538 |
– |
ForkJoinTasks.forEachEntry |
4539 |
– |
(map, transformer, action).invoke(); |
4540 |
– |
} |
4806 |
|
|
4807 |
< |
/** |
4808 |
< |
* Returns a non-null result from applying the given search |
4544 |
< |
* function on each entry, or null if none. Upon success, |
4545 |
< |
* further element processing is suppressed and the results of |
4546 |
< |
* any other parallel invocations of the search function are |
4547 |
< |
* ignored. |
4548 |
< |
* |
4549 |
< |
* @param searchFunction a function returning a non-null |
4550 |
< |
* result on success, else null |
4551 |
< |
* @return a non-null result from applying the given search |
4552 |
< |
* function on each entry, or null if none |
4553 |
< |
*/ |
4554 |
< |
public <U> U search(Fun<Map.Entry<K,V>, ? extends U> searchFunction) { |
4555 |
< |
return ForkJoinTasks.searchEntries |
4556 |
< |
(map, searchFunction).invoke(); |
4807 |
> |
public boolean removeIf(Predicate<? super Entry<K,V>> filter) { |
4808 |
> |
return map.removeEntryIf(filter); |
4809 |
|
} |
4810 |
|
|
4811 |
< |
/** |
4812 |
< |
* Returns the result of accumulating all entries using the |
4813 |
< |
* given reducer to combine values, or null if none. |
4814 |
< |
* |
4815 |
< |
* @param reducer a commutative associative combining function |
4816 |
< |
* @return the result of accumulating all entries |
4817 |
< |
*/ |
4818 |
< |
public Map.Entry<K,V> reduce(BiFun<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer) { |
4819 |
< |
return ForkJoinTasks.reduceEntries |
4820 |
< |
(map, reducer).invoke(); |
4569 |
< |
} |
4570 |
< |
|
4571 |
< |
/** |
4572 |
< |
* Returns the result of accumulating the given transformation |
4573 |
< |
* of all entries using the given reducer to combine values, |
4574 |
< |
* or null if none. |
4575 |
< |
* |
4576 |
< |
* @param transformer a function returning the transformation |
4577 |
< |
* for an element, or null of there is no transformation (in |
4578 |
< |
* which case it is not combined). |
4579 |
< |
* @param reducer a commutative associative combining function |
4580 |
< |
* @return the result of accumulating the given transformation |
4581 |
< |
* of all entries |
4582 |
< |
*/ |
4583 |
< |
public <U> U reduce(Fun<Map.Entry<K,V>, ? extends U> transformer, |
4584 |
< |
BiFun<? super U, ? super U, ? extends U> reducer) { |
4585 |
< |
return ForkJoinTasks.reduceEntries |
4586 |
< |
(map, transformer, reducer).invoke(); |
4587 |
< |
} |
4588 |
< |
|
4589 |
< |
/** |
4590 |
< |
* Returns the result of accumulating the given transformation |
4591 |
< |
* of all entries using the given reducer to combine values, |
4592 |
< |
* and the given basis as an identity value. |
4593 |
< |
* |
4594 |
< |
* @param transformer a function returning the transformation |
4595 |
< |
* for an element |
4596 |
< |
* @param basis the identity (initial default value) for the reduction |
4597 |
< |
* @param reducer a commutative associative combining function |
4598 |
< |
* @return the result of accumulating the given transformation |
4599 |
< |
* of all entries |
4600 |
< |
*/ |
4601 |
< |
public double reduceToDouble(ObjectToDouble<Map.Entry<K,V>> transformer, |
4602 |
< |
double basis, |
4603 |
< |
DoubleByDoubleToDouble reducer) { |
4604 |
< |
return ForkJoinTasks.reduceEntriesToDouble |
4605 |
< |
(map, transformer, basis, reducer).invoke(); |
4606 |
< |
} |
4607 |
< |
|
4608 |
< |
/** |
4609 |
< |
* Returns the result of accumulating the given transformation |
4610 |
< |
* of all entries using the given reducer to combine values, |
4611 |
< |
* and the given basis as an identity value. |
4612 |
< |
* |
4613 |
< |
* @param transformer a function returning the transformation |
4614 |
< |
* for an element |
4615 |
< |
* @param basis the identity (initial default value) for the reduction |
4616 |
< |
* @param reducer a commutative associative combining function |
4617 |
< |
* @return the result of accumulating the given transformation |
4618 |
< |
* of all entries |
4619 |
< |
*/ |
4620 |
< |
public long reduceToLong(ObjectToLong<Map.Entry<K,V>> transformer, |
4621 |
< |
long basis, |
4622 |
< |
LongByLongToLong reducer) { |
4623 |
< |
return ForkJoinTasks.reduceEntriesToLong |
4624 |
< |
(map, transformer, basis, reducer).invoke(); |
4625 |
< |
} |
4626 |
< |
|
4627 |
< |
/** |
4628 |
< |
* Returns the result of accumulating the given transformation |
4629 |
< |
* of all entries using the given reducer to combine values, |
4630 |
< |
* and the given basis as an identity value. |
4631 |
< |
* |
4632 |
< |
* @param transformer a function returning the transformation |
4633 |
< |
* for an element |
4634 |
< |
* @param basis the identity (initial default value) for the reduction |
4635 |
< |
* @param reducer a commutative associative combining function |
4636 |
< |
* @return the result of accumulating the given transformation |
4637 |
< |
* of all entries |
4638 |
< |
*/ |
4639 |
< |
public int reduceToInt(ObjectToInt<Map.Entry<K,V>> transformer, |
4640 |
< |
int basis, |
4641 |
< |
IntByIntToInt reducer) { |
4642 |
< |
return ForkJoinTasks.reduceEntriesToInt |
4643 |
< |
(map, transformer, basis, reducer).invoke(); |
4644 |
< |
} |
4645 |
< |
|
4646 |
< |
} |
4647 |
< |
|
4648 |
< |
// --------------------------------------------------------------------- |
4649 |
< |
|
4650 |
< |
/** |
4651 |
< |
* Predefined tasks for performing bulk parallel operations on |
4652 |
< |
* ConcurrentHashMaps. These tasks follow the forms and rules used |
4653 |
< |
* for bulk operations. Each method has the same name, but returns |
4654 |
< |
* a task rather than invoking it. These methods may be useful in |
4655 |
< |
* custom applications such as submitting a task without waiting |
4656 |
< |
* for completion, using a custom pool, or combining with other |
4657 |
< |
* tasks. |
4658 |
< |
*/ |
4659 |
< |
public static class ForkJoinTasks { |
4660 |
< |
private ForkJoinTasks() {} |
4661 |
< |
|
4662 |
< |
/** |
4663 |
< |
* Returns a task that when invoked, performs the given |
4664 |
< |
* action for each (key, value) |
4665 |
< |
* |
4666 |
< |
* @param map the map |
4667 |
< |
* @param action the action |
4668 |
< |
* @return the task |
4669 |
< |
*/ |
4670 |
< |
public static <K,V> ForkJoinTask<Void> forEach |
4671 |
< |
(ConcurrentHashMap<K,V> map, |
4672 |
< |
BiAction<K,V> action) { |
4673 |
< |
if (action == null) throw new NullPointerException(); |
4674 |
< |
return new ForEachMappingTask<K,V>(map, null, -1, null, action); |
4675 |
< |
} |
4676 |
< |
|
4677 |
< |
/** |
4678 |
< |
* Returns a task that when invoked, performs the given |
4679 |
< |
* action for each non-null transformation of each (key, value) |
4680 |
< |
* |
4681 |
< |
* @param map the map |
4682 |
< |
* @param transformer a function returning the transformation |
4683 |
< |
* for an element, or null if there is no transformation (in |
4684 |
< |
* which case the action is not applied) |
4685 |
< |
* @param action the action |
4686 |
< |
* @return the task |
4687 |
< |
*/ |
4688 |
< |
public static <K,V,U> ForkJoinTask<Void> forEach |
4689 |
< |
(ConcurrentHashMap<K,V> map, |
4690 |
< |
BiFun<? super K, ? super V, ? extends U> transformer, |
4691 |
< |
Action<U> action) { |
4692 |
< |
if (transformer == null || action == null) |
4693 |
< |
throw new NullPointerException(); |
4694 |
< |
return new ForEachTransformedMappingTask<K,V,U> |
4695 |
< |
(map, null, -1, null, transformer, action); |
4696 |
< |
} |
4697 |
< |
|
4698 |
< |
/** |
4699 |
< |
* Returns a task that when invoked, returns a non-null result |
4700 |
< |
* from applying the given search function on each (key, |
4701 |
< |
* value), or null if none. Upon success, further element |
4702 |
< |
* processing is suppressed and the results of any other |
4703 |
< |
* parallel invocations of the search function are ignored. |
4704 |
< |
* |
4705 |
< |
* @param map the map |
4706 |
< |
* @param searchFunction a function returning a non-null |
4707 |
< |
* result on success, else null |
4708 |
< |
* @return the task |
4709 |
< |
*/ |
4710 |
< |
public static <K,V,U> ForkJoinTask<U> search |
4711 |
< |
(ConcurrentHashMap<K,V> map, |
4712 |
< |
BiFun<? super K, ? super V, ? extends U> searchFunction) { |
4713 |
< |
if (searchFunction == null) throw new NullPointerException(); |
4714 |
< |
return new SearchMappingsTask<K,V,U> |
4715 |
< |
(map, null, -1, null, searchFunction, |
4716 |
< |
new AtomicReference<U>()); |
4717 |
< |
} |
4718 |
< |
|
4719 |
< |
/** |
4720 |
< |
* Returns a task that when invoked, returns the result of |
4721 |
< |
* accumulating the given transformation of all (key, value) pairs |
4722 |
< |
* using the given reducer to combine values, or null if none. |
4723 |
< |
* |
4724 |
< |
* @param map the map |
4725 |
< |
* @param transformer a function returning the transformation |
4726 |
< |
* for an element, or null if there is no transformation (in |
4727 |
< |
* which case it is not combined). |
4728 |
< |
* @param reducer a commutative associative combining function |
4729 |
< |
* @return the task |
4730 |
< |
*/ |
4731 |
< |
public static <K,V,U> ForkJoinTask<U> reduce |
4732 |
< |
(ConcurrentHashMap<K,V> map, |
4733 |
< |
BiFun<? super K, ? super V, ? extends U> transformer, |
4734 |
< |
BiFun<? super U, ? super U, ? extends U> reducer) { |
4735 |
< |
if (transformer == null || reducer == null) |
4736 |
< |
throw new NullPointerException(); |
4737 |
< |
return new MapReduceMappingsTask<K,V,U> |
4738 |
< |
(map, null, -1, null, transformer, reducer); |
4739 |
< |
} |
4740 |
< |
|
4741 |
< |
/** |
4742 |
< |
* Returns a task that when invoked, returns the result of |
4743 |
< |
* accumulating the given transformation of all (key, value) pairs |
4744 |
< |
* using the given reducer to combine values, and the given |
4745 |
< |
* basis as an identity value. |
4746 |
< |
* |
4747 |
< |
* @param map the map |
4748 |
< |
* @param transformer a function returning the transformation |
4749 |
< |
* for an element |
4750 |
< |
* @param basis the identity (initial default value) for the reduction |
4751 |
< |
* @param reducer a commutative associative combining function |
4752 |
< |
* @return the task |
4753 |
< |
*/ |
4754 |
< |
public static <K,V> ForkJoinTask<Double> reduceToDouble |
4755 |
< |
(ConcurrentHashMap<K,V> map, |
4756 |
< |
ObjectByObjectToDouble<? super K, ? super V> transformer, |
4757 |
< |
double basis, |
4758 |
< |
DoubleByDoubleToDouble reducer) { |
4759 |
< |
if (transformer == null || reducer == null) |
4760 |
< |
throw new NullPointerException(); |
4761 |
< |
return new MapReduceMappingsToDoubleTask<K,V> |
4762 |
< |
(map, null, -1, null, transformer, basis, reducer); |
4763 |
< |
} |
4764 |
< |
|
4765 |
< |
/** |
4766 |
< |
* Returns a task that when invoked, returns the result of |
4767 |
< |
* accumulating the given transformation of all (key, value) pairs |
4768 |
< |
* using the given reducer to combine values, and the given |
4769 |
< |
* basis as an identity value. |
4770 |
< |
* |
4771 |
< |
* @param map the map |
4772 |
< |
* @param transformer a function returning the transformation |
4773 |
< |
* for an element |
4774 |
< |
* @param basis the identity (initial default value) for the reduction |
4775 |
< |
* @param reducer a commutative associative combining function |
4776 |
< |
* @return the task |
4777 |
< |
*/ |
4778 |
< |
public static <K,V> ForkJoinTask<Long> reduceToLong |
4779 |
< |
(ConcurrentHashMap<K,V> map, |
4780 |
< |
ObjectByObjectToLong<? super K, ? super V> transformer, |
4781 |
< |
long basis, |
4782 |
< |
LongByLongToLong reducer) { |
4783 |
< |
if (transformer == null || reducer == null) |
4784 |
< |
throw new NullPointerException(); |
4785 |
< |
return new MapReduceMappingsToLongTask<K,V> |
4786 |
< |
(map, null, -1, null, transformer, basis, reducer); |
4787 |
< |
} |
4788 |
< |
|
4789 |
< |
/** |
4790 |
< |
* Returns a task that when invoked, returns the result of |
4791 |
< |
* accumulating the given transformation of all (key, value) pairs |
4792 |
< |
* using the given reducer to combine values, and the given |
4793 |
< |
* basis as an identity value. |
4794 |
< |
* |
4795 |
< |
* @param transformer a function returning the transformation |
4796 |
< |
* for an element |
4797 |
< |
* @param basis the identity (initial default value) for the reduction |
4798 |
< |
* @param reducer a commutative associative combining function |
4799 |
< |
* @return the task |
4800 |
< |
*/ |
4801 |
< |
public static <K,V> ForkJoinTask<Integer> reduceToInt |
4802 |
< |
(ConcurrentHashMap<K,V> map, |
4803 |
< |
ObjectByObjectToInt<? super K, ? super V> transformer, |
4804 |
< |
int basis, |
4805 |
< |
IntByIntToInt reducer) { |
4806 |
< |
if (transformer == null || reducer == null) |
4807 |
< |
throw new NullPointerException(); |
4808 |
< |
return new MapReduceMappingsToIntTask<K,V> |
4809 |
< |
(map, null, -1, null, transformer, basis, reducer); |
4810 |
< |
} |
4811 |
< |
|
4812 |
< |
/** |
4813 |
< |
* Returns a task that when invoked, performs the given action |
4814 |
< |
* for each key. |
4815 |
< |
* |
4816 |
< |
* @param map the map |
4817 |
< |
* @param action the action |
4818 |
< |
* @return the task |
4819 |
< |
*/ |
4820 |
< |
public static <K,V> ForkJoinTask<Void> forEachKey |
4821 |
< |
(ConcurrentHashMap<K,V> map, |
4822 |
< |
Action<K> action) { |
4823 |
< |
if (action == null) throw new NullPointerException(); |
4824 |
< |
return new ForEachKeyTask<K,V>(map, null, -1, null, action); |
4825 |
< |
} |
4826 |
< |
|
4827 |
< |
/** |
4828 |
< |
* Returns a task that when invoked, performs the given action |
4829 |
< |
* for each non-null transformation of each key. |
4830 |
< |
* |
4831 |
< |
* @param map the map |
4832 |
< |
* @param transformer a function returning the transformation |
4833 |
< |
* for an element, or null if there is no transformation (in |
4834 |
< |
* which case the action is not applied) |
4835 |
< |
* @param action the action |
4836 |
< |
* @return the task |
4837 |
< |
*/ |
4838 |
< |
public static <K,V,U> ForkJoinTask<Void> forEachKey |
4839 |
< |
(ConcurrentHashMap<K,V> map, |
4840 |
< |
Fun<? super K, ? extends U> transformer, |
4841 |
< |
Action<U> action) { |
4842 |
< |
if (transformer == null || action == null) |
4843 |
< |
throw new NullPointerException(); |
4844 |
< |
return new ForEachTransformedKeyTask<K,V,U> |
4845 |
< |
(map, null, -1, null, transformer, action); |
4846 |
< |
} |
4847 |
< |
|
4848 |
< |
/** |
4849 |
< |
* Returns a task that when invoked, returns a non-null result |
4850 |
< |
* from applying the given search function on each key, or |
4851 |
< |
* null if none. Upon success, further element processing is |
4852 |
< |
* suppressed and the results of any other parallel |
4853 |
< |
* invocations of the search function are ignored. |
4854 |
< |
* |
4855 |
< |
* @param map the map |
4856 |
< |
* @param searchFunction a function returning a non-null |
4857 |
< |
* result on success, else null |
4858 |
< |
* @return the task |
4859 |
< |
*/ |
4860 |
< |
public static <K,V,U> ForkJoinTask<U> searchKeys |
4861 |
< |
(ConcurrentHashMap<K,V> map, |
4862 |
< |
Fun<? super K, ? extends U> searchFunction) { |
4863 |
< |
if (searchFunction == null) throw new NullPointerException(); |
4864 |
< |
return new SearchKeysTask<K,V,U> |
4865 |
< |
(map, null, -1, null, searchFunction, |
4866 |
< |
new AtomicReference<U>()); |
4867 |
< |
} |
4868 |
< |
|
4869 |
< |
/** |
4870 |
< |
* Returns a task that when invoked, returns the result of |
4871 |
< |
* accumulating all keys using the given reducer to combine |
4872 |
< |
* values, or null if none. |
4873 |
< |
* |
4874 |
< |
* @param map the map |
4875 |
< |
* @param reducer a commutative associative combining function |
4876 |
< |
* @return the task |
4877 |
< |
*/ |
4878 |
< |
public static <K,V> ForkJoinTask<K> reduceKeys |
4879 |
< |
(ConcurrentHashMap<K,V> map, |
4880 |
< |
BiFun<? super K, ? super K, ? extends K> reducer) { |
4881 |
< |
if (reducer == null) throw new NullPointerException(); |
4882 |
< |
return new ReduceKeysTask<K,V> |
4883 |
< |
(map, null, -1, null, reducer); |
4884 |
< |
} |
4885 |
< |
|
4886 |
< |
/** |
4887 |
< |
* Returns a task that when invoked, returns the result of |
4888 |
< |
* accumulating the given transformation of all keys using the given |
4889 |
< |
* reducer to combine values, or null if none. |
4890 |
< |
* |
4891 |
< |
* @param map the map |
4892 |
< |
* @param transformer a function returning the transformation |
4893 |
< |
* for an element, or null if there is no transformation (in |
4894 |
< |
* which case it is not combined). |
4895 |
< |
* @param reducer a commutative associative combining function |
4896 |
< |
* @return the task |
4897 |
< |
*/ |
4898 |
< |
public static <K,V,U> ForkJoinTask<U> reduceKeys |
4899 |
< |
(ConcurrentHashMap<K,V> map, |
4900 |
< |
Fun<? super K, ? extends U> transformer, |
4901 |
< |
BiFun<? super U, ? super U, ? extends U> reducer) { |
4902 |
< |
if (transformer == null || reducer == null) |
4903 |
< |
throw new NullPointerException(); |
4904 |
< |
return new MapReduceKeysTask<K,V,U> |
4905 |
< |
(map, null, -1, null, transformer, reducer); |
4906 |
< |
} |
4907 |
< |
|
4908 |
< |
/** |
4909 |
< |
* Returns a task that when invoked, returns the result of |
4910 |
< |
* accumulating the given transformation of all keys using the given |
4911 |
< |
* reducer to combine values, and the given basis as an |
4912 |
< |
* identity value. |
4913 |
< |
* |
4914 |
< |
* @param map the map |
4915 |
< |
* @param transformer a function returning the transformation |
4916 |
< |
* for an element |
4917 |
< |
* @param basis the identity (initial default value) for the reduction |
4918 |
< |
* @param reducer a commutative associative combining function |
4919 |
< |
* @return the task |
4920 |
< |
*/ |
4921 |
< |
public static <K,V> ForkJoinTask<Double> reduceKeysToDouble |
4922 |
< |
(ConcurrentHashMap<K,V> map, |
4923 |
< |
ObjectToDouble<? super K> transformer, |
4924 |
< |
double basis, |
4925 |
< |
DoubleByDoubleToDouble reducer) { |
4926 |
< |
if (transformer == null || reducer == null) |
4927 |
< |
throw new NullPointerException(); |
4928 |
< |
return new MapReduceKeysToDoubleTask<K,V> |
4929 |
< |
(map, null, -1, null, transformer, basis, reducer); |
4930 |
< |
} |
4931 |
< |
|
4932 |
< |
/** |
4933 |
< |
* Returns a task that when invoked, returns the result of |
4934 |
< |
* accumulating the given transformation of all keys using the given |
4935 |
< |
* reducer to combine values, and the given basis as an |
4936 |
< |
* identity value. |
4937 |
< |
* |
4938 |
< |
* @param map the map |
4939 |
< |
* @param transformer a function returning the transformation |
4940 |
< |
* for an element |
4941 |
< |
* @param basis the identity (initial default value) for the reduction |
4942 |
< |
* @param reducer a commutative associative combining function |
4943 |
< |
* @return the task |
4944 |
< |
*/ |
4945 |
< |
public static <K,V> ForkJoinTask<Long> reduceKeysToLong |
4946 |
< |
(ConcurrentHashMap<K,V> map, |
4947 |
< |
ObjectToLong<? super K> transformer, |
4948 |
< |
long basis, |
4949 |
< |
LongByLongToLong reducer) { |
4950 |
< |
if (transformer == null || reducer == null) |
4951 |
< |
throw new NullPointerException(); |
4952 |
< |
return new MapReduceKeysToLongTask<K,V> |
4953 |
< |
(map, null, -1, null, transformer, basis, reducer); |
4954 |
< |
} |
4955 |
< |
|
4956 |
< |
/** |
4957 |
< |
* Returns a task that when invoked, returns the result of |
4958 |
< |
* accumulating the given transformation of all keys using the given |
4959 |
< |
* reducer to combine values, and the given basis as an |
4960 |
< |
* identity value. |
4961 |
< |
* |
4962 |
< |
* @param map the map |
4963 |
< |
* @param transformer a function returning the transformation |
4964 |
< |
* for an element |
4965 |
< |
* @param basis the identity (initial default value) for the reduction |
4966 |
< |
* @param reducer a commutative associative combining function |
4967 |
< |
* @return the task |
4968 |
< |
*/ |
4969 |
< |
public static <K,V> ForkJoinTask<Integer> reduceKeysToInt |
4970 |
< |
(ConcurrentHashMap<K,V> map, |
4971 |
< |
ObjectToInt<? super K> transformer, |
4972 |
< |
int basis, |
4973 |
< |
IntByIntToInt reducer) { |
4974 |
< |
if (transformer == null || reducer == null) |
4975 |
< |
throw new NullPointerException(); |
4976 |
< |
return new MapReduceKeysToIntTask<K,V> |
4977 |
< |
(map, null, -1, null, transformer, basis, reducer); |
4978 |
< |
} |
4979 |
< |
|
4980 |
< |
/** |
4981 |
< |
* Returns a task that when invoked, performs the given action |
4982 |
< |
* for each value. |
4983 |
< |
* |
4984 |
< |
* @param map the map |
4985 |
< |
* @param action the action |
4986 |
< |
*/ |
4987 |
< |
public static <K,V> ForkJoinTask<Void> forEachValue |
4988 |
< |
(ConcurrentHashMap<K,V> map, |
4989 |
< |
Action<V> action) { |
4990 |
< |
if (action == null) throw new NullPointerException(); |
4991 |
< |
return new ForEachValueTask<K,V>(map, null, -1, null, action); |
4992 |
< |
} |
4993 |
< |
|
4994 |
< |
/** |
4995 |
< |
* Returns a task that when invoked, performs the given action |
4996 |
< |
* for each non-null transformation of each value. |
4997 |
< |
* |
4998 |
< |
* @param map the map |
4999 |
< |
* @param transformer a function returning the transformation |
5000 |
< |
* for an element, or null if there is no transformation (in |
5001 |
< |
* which case the action is not applied) |
5002 |
< |
* @param action the action |
5003 |
< |
*/ |
5004 |
< |
public static <K,V,U> ForkJoinTask<Void> forEachValue |
5005 |
< |
(ConcurrentHashMap<K,V> map, |
5006 |
< |
Fun<? super V, ? extends U> transformer, |
5007 |
< |
Action<U> action) { |
5008 |
< |
if (transformer == null || action == null) |
5009 |
< |
throw new NullPointerException(); |
5010 |
< |
return new ForEachTransformedValueTask<K,V,U> |
5011 |
< |
(map, null, -1, null, transformer, action); |
5012 |
< |
} |
5013 |
< |
|
5014 |
< |
/** |
5015 |
< |
* Returns a task that when invoked, returns a non-null result |
5016 |
< |
* from applying the given search function on each value, or |
5017 |
< |
* null if none. Upon success, further element processing is |
5018 |
< |
* suppressed and the results of any other parallel |
5019 |
< |
* invocations of the search function are ignored. |
5020 |
< |
* |
5021 |
< |
* @param map the map |
5022 |
< |
* @param searchFunction a function returning a non-null |
5023 |
< |
* result on success, else null |
5024 |
< |
* @return the task |
5025 |
< |
*/ |
5026 |
< |
public static <K,V,U> ForkJoinTask<U> searchValues |
5027 |
< |
(ConcurrentHashMap<K,V> map, |
5028 |
< |
Fun<? super V, ? extends U> searchFunction) { |
5029 |
< |
if (searchFunction == null) throw new NullPointerException(); |
5030 |
< |
return new SearchValuesTask<K,V,U> |
5031 |
< |
(map, null, -1, null, searchFunction, |
5032 |
< |
new AtomicReference<U>()); |
5033 |
< |
} |
5034 |
< |
|
5035 |
< |
/** |
5036 |
< |
* Returns a task that when invoked, returns the result of |
5037 |
< |
* accumulating all values using the given reducer to combine |
5038 |
< |
* values, or null if none. |
5039 |
< |
* |
5040 |
< |
* @param map the map |
5041 |
< |
* @param reducer a commutative associative combining function |
5042 |
< |
* @return the task |
5043 |
< |
*/ |
5044 |
< |
public static <K,V> ForkJoinTask<V> reduceValues |
5045 |
< |
(ConcurrentHashMap<K,V> map, |
5046 |
< |
BiFun<? super V, ? super V, ? extends V> reducer) { |
5047 |
< |
if (reducer == null) throw new NullPointerException(); |
5048 |
< |
return new ReduceValuesTask<K,V> |
5049 |
< |
(map, null, -1, null, reducer); |
5050 |
< |
} |
5051 |
< |
|
5052 |
< |
/** |
5053 |
< |
* Returns a task that when invoked, returns the result of |
5054 |
< |
* accumulating the given transformation of all values using the |
5055 |
< |
* given reducer to combine values, or null if none. |
5056 |
< |
* |
5057 |
< |
* @param map the map |
5058 |
< |
* @param transformer a function returning the transformation |
5059 |
< |
* for an element, or null if there is no transformation (in |
5060 |
< |
* which case it is not combined). |
5061 |
< |
* @param reducer a commutative associative combining function |
5062 |
< |
* @return the task |
5063 |
< |
*/ |
5064 |
< |
public static <K,V,U> ForkJoinTask<U> reduceValues |
5065 |
< |
(ConcurrentHashMap<K,V> map, |
5066 |
< |
Fun<? super V, ? extends U> transformer, |
5067 |
< |
BiFun<? super U, ? super U, ? extends U> reducer) { |
5068 |
< |
if (transformer == null || reducer == null) |
5069 |
< |
throw new NullPointerException(); |
5070 |
< |
return new MapReduceValuesTask<K,V,U> |
5071 |
< |
(map, null, -1, null, transformer, reducer); |
5072 |
< |
} |
5073 |
< |
|
5074 |
< |
/** |
5075 |
< |
* Returns a task that when invoked, returns the result of |
5076 |
< |
* accumulating the given transformation of all values using the |
5077 |
< |
* given reducer to combine values, and the given basis as an |
5078 |
< |
* identity value. |
5079 |
< |
* |
5080 |
< |
* @param map the map |
5081 |
< |
* @param transformer a function returning the transformation |
5082 |
< |
* for an element |
5083 |
< |
* @param basis the identity (initial default value) for the reduction |
5084 |
< |
* @param reducer a commutative associative combining function |
5085 |
< |
* @return the task |
5086 |
< |
*/ |
5087 |
< |
public static <K,V> ForkJoinTask<Double> reduceValuesToDouble |
5088 |
< |
(ConcurrentHashMap<K,V> map, |
5089 |
< |
ObjectToDouble<? super V> transformer, |
5090 |
< |
double basis, |
5091 |
< |
DoubleByDoubleToDouble reducer) { |
5092 |
< |
if (transformer == null || reducer == null) |
5093 |
< |
throw new NullPointerException(); |
5094 |
< |
return new MapReduceValuesToDoubleTask<K,V> |
5095 |
< |
(map, null, -1, null, transformer, basis, reducer); |
4811 |
> |
public final int hashCode() { |
4812 |
> |
int h = 0; |
4813 |
> |
Node<K,V>[] t; |
4814 |
> |
if ((t = map.table) != null) { |
4815 |
> |
Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length); |
4816 |
> |
for (Node<K,V> p; (p = it.advance()) != null; ) { |
4817 |
> |
h += p.hashCode(); |
4818 |
> |
} |
4819 |
> |
} |
4820 |
> |
return h; |
4821 |
|
} |
4822 |
|
|
4823 |
< |
/** |
4824 |
< |
* Returns a task that when invoked, returns the result of |
4825 |
< |
* accumulating the given transformation of all values using the |
4826 |
< |
* given reducer to combine values, and the given basis as an |
4827 |
< |
* identity value. |
5103 |
< |
* |
5104 |
< |
* @param map the map |
5105 |
< |
* @param transformer a function returning the transformation |
5106 |
< |
* for an element |
5107 |
< |
* @param basis the identity (initial default value) for the reduction |
5108 |
< |
* @param reducer a commutative associative combining function |
5109 |
< |
* @return the task |
5110 |
< |
*/ |
5111 |
< |
public static <K,V> ForkJoinTask<Long> reduceValuesToLong |
5112 |
< |
(ConcurrentHashMap<K,V> map, |
5113 |
< |
ObjectToLong<? super V> transformer, |
5114 |
< |
long basis, |
5115 |
< |
LongByLongToLong reducer) { |
5116 |
< |
if (transformer == null || reducer == null) |
5117 |
< |
throw new NullPointerException(); |
5118 |
< |
return new MapReduceValuesToLongTask<K,V> |
5119 |
< |
(map, null, -1, null, transformer, basis, reducer); |
4823 |
> |
public final boolean equals(Object o) { |
4824 |
> |
Set<?> c; |
4825 |
> |
return ((o instanceof Set) && |
4826 |
> |
((c = (Set<?>)o) == this || |
4827 |
> |
(containsAll(c) && c.containsAll(this)))); |
4828 |
|
} |
4829 |
|
|
4830 |
< |
/** |
4831 |
< |
* Returns a task that when invoked, returns the result of |
4832 |
< |
* accumulating the given transformation of all values using the |
4833 |
< |
* given reducer to combine values, and the given basis as an |
4834 |
< |
* identity value. |
4835 |
< |
* |
5128 |
< |
* @param map the map |
5129 |
< |
* @param transformer a function returning the transformation |
5130 |
< |
* for an element |
5131 |
< |
* @param basis the identity (initial default value) for the reduction |
5132 |
< |
* @param reducer a commutative associative combining function |
5133 |
< |
* @return the task |
5134 |
< |
*/ |
5135 |
< |
public static <K,V> ForkJoinTask<Integer> reduceValuesToInt |
5136 |
< |
(ConcurrentHashMap<K,V> map, |
5137 |
< |
ObjectToInt<? super V> transformer, |
5138 |
< |
int basis, |
5139 |
< |
IntByIntToInt reducer) { |
5140 |
< |
if (transformer == null || reducer == null) |
5141 |
< |
throw new NullPointerException(); |
5142 |
< |
return new MapReduceValuesToIntTask<K,V> |
5143 |
< |
(map, null, -1, null, transformer, basis, reducer); |
4830 |
> |
public Spliterator<Map.Entry<K,V>> spliterator() { |
4831 |
> |
Node<K,V>[] t; |
4832 |
> |
ConcurrentHashMap<K,V> m = map; |
4833 |
> |
long n = m.sumCount(); |
4834 |
> |
int f = (t = m.table) == null ? 0 : t.length; |
4835 |
> |
return new EntrySpliterator<K,V>(t, f, 0, f, n < 0L ? 0L : n, m); |
4836 |
|
} |
4837 |
|
|
4838 |
< |
/** |
5147 |
< |
* Returns a task that when invoked, perform the given action |
5148 |
< |
* for each entry. |
5149 |
< |
* |
5150 |
< |
* @param map the map |
5151 |
< |
* @param action the action |
5152 |
< |
*/ |
5153 |
< |
public static <K,V> ForkJoinTask<Void> forEachEntry |
5154 |
< |
(ConcurrentHashMap<K,V> map, |
5155 |
< |
Action<Map.Entry<K,V>> action) { |
4838 |
> |
public void forEach(Consumer<? super Map.Entry<K,V>> action) { |
4839 |
|
if (action == null) throw new NullPointerException(); |
4840 |
< |
return new ForEachEntryTask<K,V>(map, null, -1, null, action); |
4841 |
< |
} |
4842 |
< |
|
4843 |
< |
/** |
4844 |
< |
* Returns a task that when invoked, perform the given action |
4845 |
< |
* for each non-null transformation of each entry. |
5163 |
< |
* |
5164 |
< |
* @param map the map |
5165 |
< |
* @param transformer a function returning the transformation |
5166 |
< |
* for an element, or null if there is no transformation (in |
5167 |
< |
* which case the action is not applied) |
5168 |
< |
* @param action the action |
5169 |
< |
*/ |
5170 |
< |
public static <K,V,U> ForkJoinTask<Void> forEachEntry |
5171 |
< |
(ConcurrentHashMap<K,V> map, |
5172 |
< |
Fun<Map.Entry<K,V>, ? extends U> transformer, |
5173 |
< |
Action<U> action) { |
5174 |
< |
if (transformer == null || action == null) |
5175 |
< |
throw new NullPointerException(); |
5176 |
< |
return new ForEachTransformedEntryTask<K,V,U> |
5177 |
< |
(map, null, -1, null, transformer, action); |
5178 |
< |
} |
5179 |
< |
|
5180 |
< |
/** |
5181 |
< |
* Returns a task that when invoked, returns a non-null result |
5182 |
< |
* from applying the given search function on each entry, or |
5183 |
< |
* null if none. Upon success, further element processing is |
5184 |
< |
* suppressed and the results of any other parallel |
5185 |
< |
* invocations of the search function are ignored. |
5186 |
< |
* |
5187 |
< |
* @param map the map |
5188 |
< |
* @param searchFunction a function returning a non-null |
5189 |
< |
* result on success, else null |
5190 |
< |
* @return the task |
5191 |
< |
*/ |
5192 |
< |
public static <K,V,U> ForkJoinTask<U> searchEntries |
5193 |
< |
(ConcurrentHashMap<K,V> map, |
5194 |
< |
Fun<Map.Entry<K,V>, ? extends U> searchFunction) { |
5195 |
< |
if (searchFunction == null) throw new NullPointerException(); |
5196 |
< |
return new SearchEntriesTask<K,V,U> |
5197 |
< |
(map, null, -1, null, searchFunction, |
5198 |
< |
new AtomicReference<U>()); |
5199 |
< |
} |
5200 |
< |
|
5201 |
< |
/** |
5202 |
< |
* Returns a task that when invoked, returns the result of |
5203 |
< |
* accumulating all entries using the given reducer to combine |
5204 |
< |
* values, or null if none. |
5205 |
< |
* |
5206 |
< |
* @param map the map |
5207 |
< |
* @param reducer a commutative associative combining function |
5208 |
< |
* @return the task |
5209 |
< |
*/ |
5210 |
< |
public static <K,V> ForkJoinTask<Map.Entry<K,V>> reduceEntries |
5211 |
< |
(ConcurrentHashMap<K,V> map, |
5212 |
< |
BiFun<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer) { |
5213 |
< |
if (reducer == null) throw new NullPointerException(); |
5214 |
< |
return new ReduceEntriesTask<K,V> |
5215 |
< |
(map, null, -1, null, reducer); |
5216 |
< |
} |
5217 |
< |
|
5218 |
< |
/** |
5219 |
< |
* Returns a task that when invoked, returns the result of |
5220 |
< |
* accumulating the given transformation of all entries using the |
5221 |
< |
* given reducer to combine values, or null if none. |
5222 |
< |
* |
5223 |
< |
* @param map the map |
5224 |
< |
* @param transformer a function returning the transformation |
5225 |
< |
* for an element, or null if there is no transformation (in |
5226 |
< |
* which case it is not combined). |
5227 |
< |
* @param reducer a commutative associative combining function |
5228 |
< |
* @return the task |
5229 |
< |
*/ |
5230 |
< |
public static <K,V,U> ForkJoinTask<U> reduceEntries |
5231 |
< |
(ConcurrentHashMap<K,V> map, |
5232 |
< |
Fun<Map.Entry<K,V>, ? extends U> transformer, |
5233 |
< |
BiFun<? super U, ? super U, ? extends U> reducer) { |
5234 |
< |
if (transformer == null || reducer == null) |
5235 |
< |
throw new NullPointerException(); |
5236 |
< |
return new MapReduceEntriesTask<K,V,U> |
5237 |
< |
(map, null, -1, null, transformer, reducer); |
5238 |
< |
} |
5239 |
< |
|
5240 |
< |
/** |
5241 |
< |
* Returns a task that when invoked, returns the result of |
5242 |
< |
* accumulating the given transformation of all entries using the |
5243 |
< |
* given reducer to combine values, and the given basis as an |
5244 |
< |
* identity value. |
5245 |
< |
* |
5246 |
< |
* @param map the map |
5247 |
< |
* @param transformer a function returning the transformation |
5248 |
< |
* for an element |
5249 |
< |
* @param basis the identity (initial default value) for the reduction |
5250 |
< |
* @param reducer a commutative associative combining function |
5251 |
< |
* @return the task |
5252 |
< |
*/ |
5253 |
< |
public static <K,V> ForkJoinTask<Double> reduceEntriesToDouble |
5254 |
< |
(ConcurrentHashMap<K,V> map, |
5255 |
< |
ObjectToDouble<Map.Entry<K,V>> transformer, |
5256 |
< |
double basis, |
5257 |
< |
DoubleByDoubleToDouble reducer) { |
5258 |
< |
if (transformer == null || reducer == null) |
5259 |
< |
throw new NullPointerException(); |
5260 |
< |
return new MapReduceEntriesToDoubleTask<K,V> |
5261 |
< |
(map, null, -1, null, transformer, basis, reducer); |
5262 |
< |
} |
5263 |
< |
|
5264 |
< |
/** |
5265 |
< |
* Returns a task that when invoked, returns the result of |
5266 |
< |
* accumulating the given transformation of all entries using the |
5267 |
< |
* given reducer to combine values, and the given basis as an |
5268 |
< |
* identity value. |
5269 |
< |
* |
5270 |
< |
* @param map the map |
5271 |
< |
* @param transformer a function returning the transformation |
5272 |
< |
* for an element |
5273 |
< |
* @param basis the identity (initial default value) for the reduction |
5274 |
< |
* @param reducer a commutative associative combining function |
5275 |
< |
* @return the task |
5276 |
< |
*/ |
5277 |
< |
public static <K,V> ForkJoinTask<Long> reduceEntriesToLong |
5278 |
< |
(ConcurrentHashMap<K,V> map, |
5279 |
< |
ObjectToLong<Map.Entry<K,V>> transformer, |
5280 |
< |
long basis, |
5281 |
< |
LongByLongToLong reducer) { |
5282 |
< |
if (transformer == null || reducer == null) |
5283 |
< |
throw new NullPointerException(); |
5284 |
< |
return new MapReduceEntriesToLongTask<K,V> |
5285 |
< |
(map, null, -1, null, transformer, basis, reducer); |
4840 |
> |
Node<K,V>[] t; |
4841 |
> |
if ((t = map.table) != null) { |
4842 |
> |
Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length); |
4843 |
> |
for (Node<K,V> p; (p = it.advance()) != null; ) |
4844 |
> |
action.accept(new MapEntry<K,V>(p.key, p.val, map)); |
4845 |
> |
} |
4846 |
|
} |
4847 |
|
|
5288 |
– |
/** |
5289 |
– |
* Returns a task that when invoked, returns the result of |
5290 |
– |
* accumulating the given transformation of all entries using the |
5291 |
– |
* given reducer to combine values, and the given basis as an |
5292 |
– |
* identity value. |
5293 |
– |
* |
5294 |
– |
* @param map the map |
5295 |
– |
* @param transformer a function returning the transformation |
5296 |
– |
* for an element |
5297 |
– |
* @param basis the identity (initial default value) for the reduction |
5298 |
– |
* @param reducer a commutative associative combining function |
5299 |
– |
* @return the task |
5300 |
– |
*/ |
5301 |
– |
public static <K,V> ForkJoinTask<Integer> reduceEntriesToInt |
5302 |
– |
(ConcurrentHashMap<K,V> map, |
5303 |
– |
ObjectToInt<Map.Entry<K,V>> transformer, |
5304 |
– |
int basis, |
5305 |
– |
IntByIntToInt reducer) { |
5306 |
– |
if (transformer == null || reducer == null) |
5307 |
– |
throw new NullPointerException(); |
5308 |
– |
return new MapReduceEntriesToIntTask<K,V> |
5309 |
– |
(map, null, -1, null, transformer, basis, reducer); |
5310 |
– |
} |
4848 |
|
} |
4849 |
|
|
4850 |
|
// ------------------------------------------------------- |
4851 |
|
|
4852 |
|
/** |
4853 |
< |
* Base for FJ tasks for bulk operations. This adds a variant of |
4854 |
< |
* CountedCompleters and some split and merge bookkeeping to |
4855 |
< |
* iterator functionality. The forEach and reduce methods are |
4856 |
< |
* similar to those illustrated in CountedCompleter documentation, |
4857 |
< |
* except that bottom-up reduction completions perform them within |
4858 |
< |
* their compute methods. The search methods are like forEach |
4859 |
< |
* except they continually poll for success and exit early. Also, |
4860 |
< |
* exceptions are handled in a simpler manner, by just trying to |
4861 |
< |
* complete root task exceptionally. |
4862 |
< |
*/ |
4863 |
< |
@SuppressWarnings("serial") static abstract class BulkTask<K,V,R> extends Traverser<K,V,R> { |
4864 |
< |
final BulkTask<K,V,?> parent; // completion target |
4865 |
< |
int batch; // split control; -1 for unknown |
4866 |
< |
int pending; // completion control |
4867 |
< |
|
4868 |
< |
BulkTask(ConcurrentHashMap<K,V> map, BulkTask<K,V,?> parent, |
4869 |
< |
int batch) { |
4870 |
< |
super(map); |
4871 |
< |
this.parent = parent; |
4872 |
< |
this.batch = batch; |
4873 |
< |
if (parent != null && map != null) { // split parent |
4874 |
< |
Node[] t; |
4875 |
< |
if ((t = parent.tab) == null && |
4876 |
< |
(t = parent.tab = map.table) != null) |
4877 |
< |
parent.baseLimit = parent.baseSize = t.length; |
5341 |
< |
this.tab = t; |
5342 |
< |
this.baseSize = parent.baseSize; |
5343 |
< |
int hi = this.baseLimit = parent.baseLimit; |
5344 |
< |
parent.baseLimit = this.index = this.baseIndex = |
5345 |
< |
(hi + parent.baseIndex + 1) >>> 1; |
4853 |
> |
* Base class for bulk tasks. Repeats some fields and code from |
4854 |
> |
* class Traverser, because we need to subclass CountedCompleter. |
4855 |
> |
*/ |
4856 |
> |
@SuppressWarnings("serial") |
4857 |
> |
abstract static class BulkTask<K,V,R> extends CountedCompleter<R> { |
4858 |
> |
Node<K,V>[] tab; // same as Traverser |
4859 |
> |
Node<K,V> next; |
4860 |
> |
TableStack<K,V> stack, spare; |
4861 |
> |
int index; |
4862 |
> |
int baseIndex; |
4863 |
> |
int baseLimit; |
4864 |
> |
final int baseSize; |
4865 |
> |
int batch; // split control |
4866 |
> |
|
4867 |
> |
BulkTask(BulkTask<K,V,?> par, int b, int i, int f, Node<K,V>[] t) { |
4868 |
> |
super(par); |
4869 |
> |
this.batch = b; |
4870 |
> |
this.index = this.baseIndex = i; |
4871 |
> |
if ((this.tab = t) == null) |
4872 |
> |
this.baseSize = this.baseLimit = 0; |
4873 |
> |
else if (par == null) |
4874 |
> |
this.baseSize = this.baseLimit = t.length; |
4875 |
> |
else { |
4876 |
> |
this.baseLimit = f; |
4877 |
> |
this.baseSize = par.baseSize; |
4878 |
|
} |
4879 |
|
} |
4880 |
|
|
4881 |
|
/** |
4882 |
< |
* Forces root task to complete. |
5351 |
< |
* @param ex if null, complete normally, else exceptionally |
5352 |
< |
* @return false to simplify use |
4882 |
> |
* Same as Traverser version. |
4883 |
|
*/ |
4884 |
< |
final boolean tryCompleteComputation(Throwable ex) { |
4885 |
< |
for (BulkTask<K,V,?> a = this;;) { |
4886 |
< |
BulkTask<K,V,?> p = a.parent; |
4887 |
< |
if (p == null) { |
4888 |
< |
if (ex != null) |
4889 |
< |
a.completeExceptionally(ex); |
4884 |
> |
final Node<K,V> advance() { |
4885 |
> |
Node<K,V> e; |
4886 |
> |
if ((e = next) != null) |
4887 |
> |
e = e.next; |
4888 |
> |
for (;;) { |
4889 |
> |
Node<K,V>[] t; int i, n; |
4890 |
> |
if (e != null) |
4891 |
> |
return next = e; |
4892 |
> |
if (baseIndex >= baseLimit || (t = tab) == null || |
4893 |
> |
(n = t.length) <= (i = index) || i < 0) |
4894 |
> |
return next = null; |
4895 |
> |
if ((e = tabAt(t, i)) != null && e.hash < 0) { |
4896 |
> |
if (e instanceof ForwardingNode) { |
4897 |
> |
tab = ((ForwardingNode<K,V>)e).nextTable; |
4898 |
> |
e = null; |
4899 |
> |
pushState(t, i, n); |
4900 |
> |
continue; |
4901 |
> |
} |
4902 |
> |
else if (e instanceof TreeBin) |
4903 |
> |
e = ((TreeBin<K,V>)e).first; |
4904 |
|
else |
4905 |
< |
a.quietlyComplete(); |
5362 |
< |
return false; |
5363 |
< |
} |
5364 |
< |
a = p; |
5365 |
< |
} |
5366 |
< |
} |
5367 |
< |
|
5368 |
< |
/** |
5369 |
< |
* Version of tryCompleteComputation for function screening checks |
5370 |
< |
*/ |
5371 |
< |
final boolean abortOnNullFunction() { |
5372 |
< |
return tryCompleteComputation(new Error("Unexpected null function")); |
5373 |
< |
} |
5374 |
< |
|
5375 |
< |
// utilities |
5376 |
< |
|
5377 |
< |
/** CompareAndSet pending count */ |
5378 |
< |
final boolean casPending(int cmp, int val) { |
5379 |
< |
return U.compareAndSwapInt(this, PENDING, cmp, val); |
5380 |
< |
} |
5381 |
< |
|
5382 |
< |
/** |
5383 |
< |
* Returns approx exp2 of the number of times (minus one) to |
5384 |
< |
* split task by two before executing leaf action. This value |
5385 |
< |
* is faster to compute and more convenient to use as a guide |
5386 |
< |
* to splitting than is the depth, since it is used while |
5387 |
< |
* dividing by two anyway. |
5388 |
< |
*/ |
5389 |
< |
final int batch() { |
5390 |
< |
ConcurrentHashMap<K, V> m; int b; Node[] t; ForkJoinPool pool; |
5391 |
< |
if ((b = batch) < 0 && (m = map) != null) { // force initialization |
5392 |
< |
if ((t = tab) == null && (t = tab = m.table) != null) |
5393 |
< |
baseLimit = baseSize = t.length; |
5394 |
< |
if (t != null) { |
5395 |
< |
long n = m.counter.sum(); |
5396 |
< |
int par = ((pool = getPool()) == null) ? |
5397 |
< |
ForkJoinPool.getCommonPoolParallelism() : |
5398 |
< |
pool.getParallelism(); |
5399 |
< |
int sp = par << 3; // slack of 8 |
5400 |
< |
b = batch = (n <= 0L) ? 0 : (n < (long)sp) ? (int)n : sp; |
4905 |
> |
e = null; |
4906 |
|
} |
4907 |
+ |
if (stack != null) |
4908 |
+ |
recoverState(n); |
4909 |
+ |
else if ((index = i + baseSize) >= n) |
4910 |
+ |
index = ++baseIndex; |
4911 |
|
} |
5403 |
– |
return b; |
4912 |
|
} |
4913 |
|
|
4914 |
< |
/** |
4915 |
< |
* Returns exportable snapshot entry. |
4916 |
< |
*/ |
4917 |
< |
static <K,V> AbstractMap.SimpleEntry<K,V> entryFor(K k, V v) { |
4918 |
< |
return new AbstractMap.SimpleEntry<K,V>(k, v); |
4919 |
< |
} |
4920 |
< |
|
4921 |
< |
// Unsafe mechanics |
4922 |
< |
private static final sun.misc.Unsafe U; |
4923 |
< |
private static final long PENDING; |
4924 |
< |
static { |
4925 |
< |
try { |
4926 |
< |
U = sun.misc.Unsafe.getUnsafe(); |
4927 |
< |
PENDING = U.objectFieldOffset |
4928 |
< |
(BulkTask.class.getDeclaredField("pending")); |
4929 |
< |
} catch (Exception e) { |
4930 |
< |
throw new Error(e); |
4931 |
< |
} |
4932 |
< |
} |
4933 |
< |
} |
4934 |
< |
|
4935 |
< |
/** |
4936 |
< |
* Base class for non-reductive actions |
4937 |
< |
*/ |
5430 |
< |
@SuppressWarnings("serial") static abstract class BulkAction<K,V,R> extends BulkTask<K,V,R> { |
5431 |
< |
BulkAction<K,V,?> nextTask; |
5432 |
< |
BulkAction(ConcurrentHashMap<K,V> map, BulkTask<K,V,?> parent, |
5433 |
< |
int batch, BulkAction<K,V,?> nextTask) { |
5434 |
< |
super(map, parent, batch); |
5435 |
< |
this.nextTask = nextTask; |
5436 |
< |
} |
5437 |
< |
|
5438 |
< |
/** |
5439 |
< |
* Try to complete task and upward parents. Upon hitting |
5440 |
< |
* non-completed parent, if a non-FJ task, try to help out the |
5441 |
< |
* computation. |
5442 |
< |
*/ |
5443 |
< |
final void tryComplete(BulkAction<K,V,?> subtasks) { |
5444 |
< |
BulkTask<K,V,?> a = this, s = a; |
5445 |
< |
for (int c;;) { |
5446 |
< |
if ((c = a.pending) == 0) { |
5447 |
< |
if ((a = (s = a).parent) == null) { |
5448 |
< |
s.quietlyComplete(); |
5449 |
< |
break; |
5450 |
< |
} |
5451 |
< |
} |
5452 |
< |
else if (a.casPending(c, c - 1)) { |
5453 |
< |
if (subtasks != null && !inForkJoinPool()) { |
5454 |
< |
while ((s = a.parent) != null) |
5455 |
< |
a = s; |
5456 |
< |
while (!a.isDone()) { |
5457 |
< |
BulkAction<K,V,?> next = subtasks.nextTask; |
5458 |
< |
if (subtasks.tryUnfork()) |
5459 |
< |
subtasks.exec(); |
5460 |
< |
if ((subtasks = next) == null) |
5461 |
< |
break; |
5462 |
< |
} |
5463 |
< |
} |
5464 |
< |
break; |
5465 |
< |
} |
4914 |
> |
private void pushState(Node<K,V>[] t, int i, int n) { |
4915 |
> |
TableStack<K,V> s = spare; |
4916 |
> |
if (s != null) |
4917 |
> |
spare = s.next; |
4918 |
> |
else |
4919 |
> |
s = new TableStack<K,V>(); |
4920 |
> |
s.tab = t; |
4921 |
> |
s.length = n; |
4922 |
> |
s.index = i; |
4923 |
> |
s.next = stack; |
4924 |
> |
stack = s; |
4925 |
> |
} |
4926 |
> |
|
4927 |
> |
private void recoverState(int n) { |
4928 |
> |
TableStack<K,V> s; int len; |
4929 |
> |
while ((s = stack) != null && (index += (len = s.length)) >= n) { |
4930 |
> |
n = len; |
4931 |
> |
index = s.index; |
4932 |
> |
tab = s.tab; |
4933 |
> |
s.tab = null; |
4934 |
> |
TableStack<K,V> next = s.next; |
4935 |
> |
s.next = spare; // save for reuse |
4936 |
> |
stack = next; |
4937 |
> |
spare = s; |
4938 |
|
} |
4939 |
+ |
if (s == null && (index += baseSize) >= n) |
4940 |
+ |
index = ++baseIndex; |
4941 |
|
} |
5468 |
– |
|
4942 |
|
} |
4943 |
|
|
4944 |
|
/* |
4945 |
|
* Task classes. Coded in a regular but ugly format/style to |
4946 |
|
* simplify checks that each variant differs in the right way from |
4947 |
< |
* others. |
4948 |
< |
*/ |
4949 |
< |
|
4950 |
< |
@SuppressWarnings("serial") static final class ForEachKeyTask<K,V> |
4951 |
< |
extends BulkAction<K,V,Void> { |
4952 |
< |
final Action<K> action; |
4947 |
> |
* others. The null screenings exist because compilers cannot tell |
4948 |
> |
* that we've already null-checked task arguments, so we force |
4949 |
> |
* simplest hoisted bypass to help avoid convoluted traps. |
4950 |
> |
*/ |
4951 |
> |
@SuppressWarnings("serial") |
4952 |
> |
static final class ForEachKeyTask<K,V> |
4953 |
> |
extends BulkTask<K,V,Void> { |
4954 |
> |
final Consumer<? super K> action; |
4955 |
|
ForEachKeyTask |
4956 |
< |
(ConcurrentHashMap<K,V> m, BulkTask<K,V,?> p, int b, |
4957 |
< |
ForEachKeyTask<K,V> nextTask, |
4958 |
< |
Action<K> action) { |
5484 |
< |
super(m, p, b, nextTask); |
4956 |
> |
(BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t, |
4957 |
> |
Consumer<? super K> action) { |
4958 |
> |
super(p, b, i, f, t); |
4959 |
|
this.action = action; |
4960 |
|
} |
4961 |
< |
@SuppressWarnings("unchecked") public final boolean exec() { |
4962 |
< |
final Action<K> action = this.action; |
4963 |
< |
if (action == null) |
4964 |
< |
return abortOnNullFunction(); |
4965 |
< |
ForEachKeyTask<K,V> subtasks = null; |
4966 |
< |
try { |
4967 |
< |
int b = batch(), c; |
4968 |
< |
while (b > 1 && baseIndex != baseLimit) { |
4969 |
< |
do {} while (!casPending(c = pending, c+1)); |
4970 |
< |
(subtasks = new ForEachKeyTask<K,V> |
4971 |
< |
(map, this, b >>>= 1, subtasks, action)).fork(); |
4972 |
< |
} |
4973 |
< |
while (advance() != null) |
5500 |
< |
action.apply((K)nextKey); |
5501 |
< |
} catch (Throwable ex) { |
5502 |
< |
return tryCompleteComputation(ex); |
4961 |
> |
public final void compute() { |
4962 |
> |
final Consumer<? super K> action; |
4963 |
> |
if ((action = this.action) != null) { |
4964 |
> |
for (int i = baseIndex, f, h; batch > 0 && |
4965 |
> |
(h = ((f = baseLimit) + i) >>> 1) > i;) { |
4966 |
> |
addToPendingCount(1); |
4967 |
> |
new ForEachKeyTask<K,V> |
4968 |
> |
(this, batch >>>= 1, baseLimit = h, f, tab, |
4969 |
> |
action).fork(); |
4970 |
> |
} |
4971 |
> |
for (Node<K,V> p; (p = advance()) != null;) |
4972 |
> |
action.accept(p.key); |
4973 |
> |
propagateCompletion(); |
4974 |
|
} |
5504 |
– |
tryComplete(subtasks); |
5505 |
– |
return false; |
4975 |
|
} |
4976 |
|
} |
4977 |
|
|
4978 |
< |
@SuppressWarnings("serial") static final class ForEachValueTask<K,V> |
4979 |
< |
extends BulkAction<K,V,Void> { |
4980 |
< |
final Action<V> action; |
4978 |
> |
@SuppressWarnings("serial") |
4979 |
> |
static final class ForEachValueTask<K,V> |
4980 |
> |
extends BulkTask<K,V,Void> { |
4981 |
> |
final Consumer<? super V> action; |
4982 |
|
ForEachValueTask |
4983 |
< |
(ConcurrentHashMap<K,V> m, BulkTask<K,V,?> p, int b, |
4984 |
< |
ForEachValueTask<K,V> nextTask, |
4985 |
< |
Action<V> action) { |
5516 |
< |
super(m, p, b, nextTask); |
4983 |
> |
(BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t, |
4984 |
> |
Consumer<? super V> action) { |
4985 |
> |
super(p, b, i, f, t); |
4986 |
|
this.action = action; |
4987 |
|
} |
4988 |
< |
@SuppressWarnings("unchecked") public final boolean exec() { |
4989 |
< |
final Action<V> action = this.action; |
4990 |
< |
if (action == null) |
4991 |
< |
return abortOnNullFunction(); |
4992 |
< |
ForEachValueTask<K,V> subtasks = null; |
4993 |
< |
try { |
4994 |
< |
int b = batch(), c; |
4995 |
< |
while (b > 1 && baseIndex != baseLimit) { |
4996 |
< |
do {} while (!casPending(c = pending, c+1)); |
4997 |
< |
(subtasks = new ForEachValueTask<K,V> |
4998 |
< |
(map, this, b >>>= 1, subtasks, action)).fork(); |
4999 |
< |
} |
5000 |
< |
Object v; |
5532 |
< |
while ((v = advance()) != null) |
5533 |
< |
action.apply((V)v); |
5534 |
< |
} catch (Throwable ex) { |
5535 |
< |
return tryCompleteComputation(ex); |
4988 |
> |
public final void compute() { |
4989 |
> |
final Consumer<? super V> action; |
4990 |
> |
if ((action = this.action) != null) { |
4991 |
> |
for (int i = baseIndex, f, h; batch > 0 && |
4992 |
> |
(h = ((f = baseLimit) + i) >>> 1) > i;) { |
4993 |
> |
addToPendingCount(1); |
4994 |
> |
new ForEachValueTask<K,V> |
4995 |
> |
(this, batch >>>= 1, baseLimit = h, f, tab, |
4996 |
> |
action).fork(); |
4997 |
> |
} |
4998 |
> |
for (Node<K,V> p; (p = advance()) != null;) |
4999 |
> |
action.accept(p.val); |
5000 |
> |
propagateCompletion(); |
5001 |
|
} |
5537 |
– |
tryComplete(subtasks); |
5538 |
– |
return false; |
5002 |
|
} |
5003 |
|
} |
5004 |
|
|
5005 |
< |
@SuppressWarnings("serial") static final class ForEachEntryTask<K,V> |
5006 |
< |
extends BulkAction<K,V,Void> { |
5007 |
< |
final Action<Entry<K,V>> action; |
5005 |
> |
@SuppressWarnings("serial") |
5006 |
> |
static final class ForEachEntryTask<K,V> |
5007 |
> |
extends BulkTask<K,V,Void> { |
5008 |
> |
final Consumer<? super Entry<K,V>> action; |
5009 |
|
ForEachEntryTask |
5010 |
< |
(ConcurrentHashMap<K,V> m, BulkTask<K,V,?> p, int b, |
5011 |
< |
ForEachEntryTask<K,V> nextTask, |
5012 |
< |
Action<Entry<K,V>> action) { |
5549 |
< |
super(m, p, b, nextTask); |
5010 |
> |
(BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t, |
5011 |
> |
Consumer<? super Entry<K,V>> action) { |
5012 |
> |
super(p, b, i, f, t); |
5013 |
|
this.action = action; |
5014 |
|
} |
5015 |
< |
@SuppressWarnings("unchecked") public final boolean exec() { |
5016 |
< |
final Action<Entry<K,V>> action = this.action; |
5017 |
< |
if (action == null) |
5018 |
< |
return abortOnNullFunction(); |
5019 |
< |
ForEachEntryTask<K,V> subtasks = null; |
5020 |
< |
try { |
5021 |
< |
int b = batch(), c; |
5022 |
< |
while (b > 1 && baseIndex != baseLimit) { |
5023 |
< |
do {} while (!casPending(c = pending, c+1)); |
5024 |
< |
(subtasks = new ForEachEntryTask<K,V> |
5025 |
< |
(map, this, b >>>= 1, subtasks, action)).fork(); |
5026 |
< |
} |
5027 |
< |
Object v; |
5565 |
< |
while ((v = advance()) != null) |
5566 |
< |
action.apply(entryFor((K)nextKey, (V)v)); |
5567 |
< |
} catch (Throwable ex) { |
5568 |
< |
return tryCompleteComputation(ex); |
5015 |
> |
public final void compute() { |
5016 |
> |
final Consumer<? super Entry<K,V>> action; |
5017 |
> |
if ((action = this.action) != null) { |
5018 |
> |
for (int i = baseIndex, f, h; batch > 0 && |
5019 |
> |
(h = ((f = baseLimit) + i) >>> 1) > i;) { |
5020 |
> |
addToPendingCount(1); |
5021 |
> |
new ForEachEntryTask<K,V> |
5022 |
> |
(this, batch >>>= 1, baseLimit = h, f, tab, |
5023 |
> |
action).fork(); |
5024 |
> |
} |
5025 |
> |
for (Node<K,V> p; (p = advance()) != null; ) |
5026 |
> |
action.accept(p); |
5027 |
> |
propagateCompletion(); |
5028 |
|
} |
5570 |
– |
tryComplete(subtasks); |
5571 |
– |
return false; |
5029 |
|
} |
5030 |
|
} |
5031 |
|
|
5032 |
< |
@SuppressWarnings("serial") static final class ForEachMappingTask<K,V> |
5033 |
< |
extends BulkAction<K,V,Void> { |
5034 |
< |
final BiAction<K,V> action; |
5032 |
> |
@SuppressWarnings("serial") |
5033 |
> |
static final class ForEachMappingTask<K,V> |
5034 |
> |
extends BulkTask<K,V,Void> { |
5035 |
> |
final BiConsumer<? super K, ? super V> action; |
5036 |
|
ForEachMappingTask |
5037 |
< |
(ConcurrentHashMap<K,V> m, BulkTask<K,V,?> p, int b, |
5038 |
< |
ForEachMappingTask<K,V> nextTask, |
5039 |
< |
BiAction<K,V> action) { |
5582 |
< |
super(m, p, b, nextTask); |
5037 |
> |
(BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t, |
5038 |
> |
BiConsumer<? super K,? super V> action) { |
5039 |
> |
super(p, b, i, f, t); |
5040 |
|
this.action = action; |
5041 |
|
} |
5042 |
< |
@SuppressWarnings("unchecked") public final boolean exec() { |
5043 |
< |
final BiAction<K,V> action = this.action; |
5044 |
< |
if (action == null) |
5045 |
< |
return abortOnNullFunction(); |
5046 |
< |
ForEachMappingTask<K,V> subtasks = null; |
5047 |
< |
try { |
5048 |
< |
int b = batch(), c; |
5049 |
< |
while (b > 1 && baseIndex != baseLimit) { |
5050 |
< |
do {} while (!casPending(c = pending, c+1)); |
5051 |
< |
(subtasks = new ForEachMappingTask<K,V> |
5052 |
< |
(map, this, b >>>= 1, subtasks, action)).fork(); |
5053 |
< |
} |
5054 |
< |
Object v; |
5598 |
< |
while ((v = advance()) != null) |
5599 |
< |
action.apply((K)nextKey, (V)v); |
5600 |
< |
} catch (Throwable ex) { |
5601 |
< |
return tryCompleteComputation(ex); |
5042 |
> |
public final void compute() { |
5043 |
> |
final BiConsumer<? super K, ? super V> action; |
5044 |
> |
if ((action = this.action) != null) { |
5045 |
> |
for (int i = baseIndex, f, h; batch > 0 && |
5046 |
> |
(h = ((f = baseLimit) + i) >>> 1) > i;) { |
5047 |
> |
addToPendingCount(1); |
5048 |
> |
new ForEachMappingTask<K,V> |
5049 |
> |
(this, batch >>>= 1, baseLimit = h, f, tab, |
5050 |
> |
action).fork(); |
5051 |
> |
} |
5052 |
> |
for (Node<K,V> p; (p = advance()) != null; ) |
5053 |
> |
action.accept(p.key, p.val); |
5054 |
> |
propagateCompletion(); |
5055 |
|
} |
5603 |
– |
tryComplete(subtasks); |
5604 |
– |
return false; |
5056 |
|
} |
5057 |
|
} |
5058 |
|
|
5059 |
< |
@SuppressWarnings("serial") static final class ForEachTransformedKeyTask<K,V,U> |
5060 |
< |
extends BulkAction<K,V,Void> { |
5061 |
< |
final Fun<? super K, ? extends U> transformer; |
5062 |
< |
final Action<U> action; |
5059 |
> |
@SuppressWarnings("serial") |
5060 |
> |
static final class ForEachTransformedKeyTask<K,V,U> |
5061 |
> |
extends BulkTask<K,V,Void> { |
5062 |
> |
final Function<? super K, ? extends U> transformer; |
5063 |
> |
final Consumer<? super U> action; |
5064 |
|
ForEachTransformedKeyTask |
5065 |
< |
(ConcurrentHashMap<K,V> m, BulkTask<K,V,?> p, int b, |
5066 |
< |
ForEachTransformedKeyTask<K,V,U> nextTask, |
5067 |
< |
Fun<? super K, ? extends U> transformer, |
5068 |
< |
Action<U> action) { |
5069 |
< |
super(m, p, b, nextTask); |
5070 |
< |
this.transformer = transformer; |
5071 |
< |
this.action = action; |
5072 |
< |
|
5073 |
< |
} |
5074 |
< |
@SuppressWarnings("unchecked") public final boolean exec() { |
5075 |
< |
final Fun<? super K, ? extends U> transformer = |
5076 |
< |
this.transformer; |
5077 |
< |
final Action<U> action = this.action; |
5078 |
< |
if (transformer == null || action == null) |
5079 |
< |
return abortOnNullFunction(); |
5080 |
< |
ForEachTransformedKeyTask<K,V,U> subtasks = null; |
5081 |
< |
try { |
5082 |
< |
int b = batch(), c; |
5083 |
< |
while (b > 1 && baseIndex != baseLimit) { |
5084 |
< |
do {} while (!casPending(c = pending, c+1)); |
5085 |
< |
(subtasks = new ForEachTransformedKeyTask<K,V,U> |
5086 |
< |
(map, this, b >>>= 1, subtasks, transformer, action)).fork(); |
5087 |
< |
} |
5636 |
< |
U u; |
5637 |
< |
while (advance() != null) { |
5638 |
< |
if ((u = transformer.apply((K)nextKey)) != null) |
5639 |
< |
action.apply(u); |
5640 |
< |
} |
5641 |
< |
} catch (Throwable ex) { |
5642 |
< |
return tryCompleteComputation(ex); |
5065 |
> |
(BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t, |
5066 |
> |
Function<? super K, ? extends U> transformer, Consumer<? super U> action) { |
5067 |
> |
super(p, b, i, f, t); |
5068 |
> |
this.transformer = transformer; this.action = action; |
5069 |
> |
} |
5070 |
> |
public final void compute() { |
5071 |
> |
final Function<? super K, ? extends U> transformer; |
5072 |
> |
final Consumer<? super U> action; |
5073 |
> |
if ((transformer = this.transformer) != null && |
5074 |
> |
(action = this.action) != null) { |
5075 |
> |
for (int i = baseIndex, f, h; batch > 0 && |
5076 |
> |
(h = ((f = baseLimit) + i) >>> 1) > i;) { |
5077 |
> |
addToPendingCount(1); |
5078 |
> |
new ForEachTransformedKeyTask<K,V,U> |
5079 |
> |
(this, batch >>>= 1, baseLimit = h, f, tab, |
5080 |
> |
transformer, action).fork(); |
5081 |
> |
} |
5082 |
> |
for (Node<K,V> p; (p = advance()) != null; ) { |
5083 |
> |
U u; |
5084 |
> |
if ((u = transformer.apply(p.key)) != null) |
5085 |
> |
action.accept(u); |
5086 |
> |
} |
5087 |
> |
propagateCompletion(); |
5088 |
|
} |
5644 |
– |
tryComplete(subtasks); |
5645 |
– |
return false; |
5089 |
|
} |
5090 |
|
} |
5091 |
|
|
5092 |
< |
@SuppressWarnings("serial") static final class ForEachTransformedValueTask<K,V,U> |
5093 |
< |
extends BulkAction<K,V,Void> { |
5094 |
< |
final Fun<? super V, ? extends U> transformer; |
5095 |
< |
final Action<U> action; |
5092 |
> |
@SuppressWarnings("serial") |
5093 |
> |
static final class ForEachTransformedValueTask<K,V,U> |
5094 |
> |
extends BulkTask<K,V,Void> { |
5095 |
> |
final Function<? super V, ? extends U> transformer; |
5096 |
> |
final Consumer<? super U> action; |
5097 |
|
ForEachTransformedValueTask |
5098 |
< |
(ConcurrentHashMap<K,V> m, BulkTask<K,V,?> p, int b, |
5099 |
< |
ForEachTransformedValueTask<K,V,U> nextTask, |
5100 |
< |
Fun<? super V, ? extends U> transformer, |
5101 |
< |
Action<U> action) { |
5102 |
< |
super(m, p, b, nextTask); |
5103 |
< |
this.transformer = transformer; |
5104 |
< |
this.action = action; |
5105 |
< |
|
5106 |
< |
} |
5107 |
< |
@SuppressWarnings("unchecked") public final boolean exec() { |
5108 |
< |
final Fun<? super V, ? extends U> transformer = |
5109 |
< |
this.transformer; |
5110 |
< |
final Action<U> action = this.action; |
5111 |
< |
if (transformer == null || action == null) |
5112 |
< |
return abortOnNullFunction(); |
5113 |
< |
ForEachTransformedValueTask<K,V,U> subtasks = null; |
5114 |
< |
try { |
5115 |
< |
int b = batch(), c; |
5116 |
< |
while (b > 1 && baseIndex != baseLimit) { |
5117 |
< |
do {} while (!casPending(c = pending, c+1)); |
5118 |
< |
(subtasks = new ForEachTransformedValueTask<K,V,U> |
5119 |
< |
(map, this, b >>>= 1, subtasks, transformer, action)).fork(); |
5120 |
< |
} |
5677 |
< |
Object v; U u; |
5678 |
< |
while ((v = advance()) != null) { |
5679 |
< |
if ((u = transformer.apply((V)v)) != null) |
5680 |
< |
action.apply(u); |
5681 |
< |
} |
5682 |
< |
} catch (Throwable ex) { |
5683 |
< |
return tryCompleteComputation(ex); |
5098 |
> |
(BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t, |
5099 |
> |
Function<? super V, ? extends U> transformer, Consumer<? super U> action) { |
5100 |
> |
super(p, b, i, f, t); |
5101 |
> |
this.transformer = transformer; this.action = action; |
5102 |
> |
} |
5103 |
> |
public final void compute() { |
5104 |
> |
final Function<? super V, ? extends U> transformer; |
5105 |
> |
final Consumer<? super U> action; |
5106 |
> |
if ((transformer = this.transformer) != null && |
5107 |
> |
(action = this.action) != null) { |
5108 |
> |
for (int i = baseIndex, f, h; batch > 0 && |
5109 |
> |
(h = ((f = baseLimit) + i) >>> 1) > i;) { |
5110 |
> |
addToPendingCount(1); |
5111 |
> |
new ForEachTransformedValueTask<K,V,U> |
5112 |
> |
(this, batch >>>= 1, baseLimit = h, f, tab, |
5113 |
> |
transformer, action).fork(); |
5114 |
> |
} |
5115 |
> |
for (Node<K,V> p; (p = advance()) != null; ) { |
5116 |
> |
U u; |
5117 |
> |
if ((u = transformer.apply(p.val)) != null) |
5118 |
> |
action.accept(u); |
5119 |
> |
} |
5120 |
> |
propagateCompletion(); |
5121 |
|
} |
5685 |
– |
tryComplete(subtasks); |
5686 |
– |
return false; |
5122 |
|
} |
5123 |
|
} |
5124 |
|
|
5125 |
< |
@SuppressWarnings("serial") static final class ForEachTransformedEntryTask<K,V,U> |
5126 |
< |
extends BulkAction<K,V,Void> { |
5127 |
< |
final Fun<Map.Entry<K,V>, ? extends U> transformer; |
5128 |
< |
final Action<U> action; |
5125 |
> |
@SuppressWarnings("serial") |
5126 |
> |
static final class ForEachTransformedEntryTask<K,V,U> |
5127 |
> |
extends BulkTask<K,V,Void> { |
5128 |
> |
final Function<Map.Entry<K,V>, ? extends U> transformer; |
5129 |
> |
final Consumer<? super U> action; |
5130 |
|
ForEachTransformedEntryTask |
5131 |
< |
(ConcurrentHashMap<K,V> m, BulkTask<K,V,?> p, int b, |
5132 |
< |
ForEachTransformedEntryTask<K,V,U> nextTask, |
5133 |
< |
Fun<Map.Entry<K,V>, ? extends U> transformer, |
5134 |
< |
Action<U> action) { |
5135 |
< |
super(m, p, b, nextTask); |
5136 |
< |
this.transformer = transformer; |
5137 |
< |
this.action = action; |
5138 |
< |
|
5139 |
< |
} |
5140 |
< |
@SuppressWarnings("unchecked") public final boolean exec() { |
5141 |
< |
final Fun<Map.Entry<K,V>, ? extends U> transformer = |
5142 |
< |
this.transformer; |
5143 |
< |
final Action<U> action = this.action; |
5144 |
< |
if (transformer == null || action == null) |
5145 |
< |
return abortOnNullFunction(); |
5146 |
< |
ForEachTransformedEntryTask<K,V,U> subtasks = null; |
5147 |
< |
try { |
5148 |
< |
int b = batch(), c; |
5149 |
< |
while (b > 1 && baseIndex != baseLimit) { |
5150 |
< |
do {} while (!casPending(c = pending, c+1)); |
5151 |
< |
(subtasks = new ForEachTransformedEntryTask<K,V,U> |
5152 |
< |
(map, this, b >>>= 1, subtasks, transformer, action)).fork(); |
5153 |
< |
} |
5718 |
< |
Object v; U u; |
5719 |
< |
while ((v = advance()) != null) { |
5720 |
< |
if ((u = transformer.apply(entryFor((K)nextKey, (V)v))) != null) |
5721 |
< |
action.apply(u); |
5722 |
< |
} |
5723 |
< |
} catch (Throwable ex) { |
5724 |
< |
return tryCompleteComputation(ex); |
5131 |
> |
(BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t, |
5132 |
> |
Function<Map.Entry<K,V>, ? extends U> transformer, Consumer<? super U> action) { |
5133 |
> |
super(p, b, i, f, t); |
5134 |
> |
this.transformer = transformer; this.action = action; |
5135 |
> |
} |
5136 |
> |
public final void compute() { |
5137 |
> |
final Function<Map.Entry<K,V>, ? extends U> transformer; |
5138 |
> |
final Consumer<? super U> action; |
5139 |
> |
if ((transformer = this.transformer) != null && |
5140 |
> |
(action = this.action) != null) { |
5141 |
> |
for (int i = baseIndex, f, h; batch > 0 && |
5142 |
> |
(h = ((f = baseLimit) + i) >>> 1) > i;) { |
5143 |
> |
addToPendingCount(1); |
5144 |
> |
new ForEachTransformedEntryTask<K,V,U> |
5145 |
> |
(this, batch >>>= 1, baseLimit = h, f, tab, |
5146 |
> |
transformer, action).fork(); |
5147 |
> |
} |
5148 |
> |
for (Node<K,V> p; (p = advance()) != null; ) { |
5149 |
> |
U u; |
5150 |
> |
if ((u = transformer.apply(p)) != null) |
5151 |
> |
action.accept(u); |
5152 |
> |
} |
5153 |
> |
propagateCompletion(); |
5154 |
|
} |
5726 |
– |
tryComplete(subtasks); |
5727 |
– |
return false; |
5155 |
|
} |
5156 |
|
} |
5157 |
|
|
5158 |
< |
@SuppressWarnings("serial") static final class ForEachTransformedMappingTask<K,V,U> |
5159 |
< |
extends BulkAction<K,V,Void> { |
5160 |
< |
final BiFun<? super K, ? super V, ? extends U> transformer; |
5161 |
< |
final Action<U> action; |
5158 |
> |
@SuppressWarnings("serial") |
5159 |
> |
static final class ForEachTransformedMappingTask<K,V,U> |
5160 |
> |
extends BulkTask<K,V,Void> { |
5161 |
> |
final BiFunction<? super K, ? super V, ? extends U> transformer; |
5162 |
> |
final Consumer<? super U> action; |
5163 |
|
ForEachTransformedMappingTask |
5164 |
< |
(ConcurrentHashMap<K,V> m, BulkTask<K,V,?> p, int b, |
5165 |
< |
ForEachTransformedMappingTask<K,V,U> nextTask, |
5166 |
< |
BiFun<? super K, ? super V, ? extends U> transformer, |
5167 |
< |
Action<U> action) { |
5168 |
< |
super(m, p, b, nextTask); |
5169 |
< |
this.transformer = transformer; |
5170 |
< |
this.action = action; |
5171 |
< |
|
5172 |
< |
} |
5173 |
< |
@SuppressWarnings("unchecked") public final boolean exec() { |
5174 |
< |
final BiFun<? super K, ? super V, ? extends U> transformer = |
5175 |
< |
this.transformer; |
5176 |
< |
final Action<U> action = this.action; |
5177 |
< |
if (transformer == null || action == null) |
5178 |
< |
return abortOnNullFunction(); |
5179 |
< |
ForEachTransformedMappingTask<K,V,U> subtasks = null; |
5180 |
< |
try { |
5181 |
< |
int b = batch(), c; |
5182 |
< |
while (b > 1 && baseIndex != baseLimit) { |
5183 |
< |
do {} while (!casPending(c = pending, c+1)); |
5184 |
< |
(subtasks = new ForEachTransformedMappingTask<K,V,U> |
5185 |
< |
(map, this, b >>>= 1, subtasks, transformer, action)).fork(); |
5758 |
< |
} |
5759 |
< |
Object v; U u; |
5760 |
< |
while ((v = advance()) != null) { |
5761 |
< |
if ((u = transformer.apply((K)nextKey, (V)v)) != null) |
5762 |
< |
action.apply(u); |
5164 |
> |
(BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t, |
5165 |
> |
BiFunction<? super K, ? super V, ? extends U> transformer, |
5166 |
> |
Consumer<? super U> action) { |
5167 |
> |
super(p, b, i, f, t); |
5168 |
> |
this.transformer = transformer; this.action = action; |
5169 |
> |
} |
5170 |
> |
public final void compute() { |
5171 |
> |
final BiFunction<? super K, ? super V, ? extends U> transformer; |
5172 |
> |
final Consumer<? super U> action; |
5173 |
> |
if ((transformer = this.transformer) != null && |
5174 |
> |
(action = this.action) != null) { |
5175 |
> |
for (int i = baseIndex, f, h; batch > 0 && |
5176 |
> |
(h = ((f = baseLimit) + i) >>> 1) > i;) { |
5177 |
> |
addToPendingCount(1); |
5178 |
> |
new ForEachTransformedMappingTask<K,V,U> |
5179 |
> |
(this, batch >>>= 1, baseLimit = h, f, tab, |
5180 |
> |
transformer, action).fork(); |
5181 |
> |
} |
5182 |
> |
for (Node<K,V> p; (p = advance()) != null; ) { |
5183 |
> |
U u; |
5184 |
> |
if ((u = transformer.apply(p.key, p.val)) != null) |
5185 |
> |
action.accept(u); |
5186 |
|
} |
5187 |
< |
} catch (Throwable ex) { |
5765 |
< |
return tryCompleteComputation(ex); |
5187 |
> |
propagateCompletion(); |
5188 |
|
} |
5767 |
– |
tryComplete(subtasks); |
5768 |
– |
return false; |
5189 |
|
} |
5190 |
|
} |
5191 |
|
|
5192 |
< |
@SuppressWarnings("serial") static final class SearchKeysTask<K,V,U> |
5193 |
< |
extends BulkAction<K,V,U> { |
5194 |
< |
final Fun<? super K, ? extends U> searchFunction; |
5192 |
> |
@SuppressWarnings("serial") |
5193 |
> |
static final class SearchKeysTask<K,V,U> |
5194 |
> |
extends BulkTask<K,V,U> { |
5195 |
> |
final Function<? super K, ? extends U> searchFunction; |
5196 |
|
final AtomicReference<U> result; |
5197 |
|
SearchKeysTask |
5198 |
< |
(ConcurrentHashMap<K,V> m, BulkTask<K,V,?> p, int b, |
5199 |
< |
SearchKeysTask<K,V,U> nextTask, |
5779 |
< |
Fun<? super K, ? extends U> searchFunction, |
5198 |
> |
(BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t, |
5199 |
> |
Function<? super K, ? extends U> searchFunction, |
5200 |
|
AtomicReference<U> result) { |
5201 |
< |
super(m, p, b, nextTask); |
5201 |
> |
super(p, b, i, f, t); |
5202 |
|
this.searchFunction = searchFunction; this.result = result; |
5203 |
|
} |
5204 |
< |
@SuppressWarnings("unchecked") public final boolean exec() { |
5205 |
< |
AtomicReference<U> result = this.result; |
5206 |
< |
final Fun<? super K, ? extends U> searchFunction = |
5207 |
< |
this.searchFunction; |
5208 |
< |
if (searchFunction == null || result == null) |
5209 |
< |
return abortOnNullFunction(); |
5210 |
< |
SearchKeysTask<K,V,U> subtasks = null; |
5211 |
< |
try { |
5212 |
< |
int b = batch(), c; |
5213 |
< |
while (b > 1 && baseIndex != baseLimit && result.get() == null) { |
5214 |
< |
do {} while (!casPending(c = pending, c+1)); |
5215 |
< |
(subtasks = new SearchKeysTask<K,V,U> |
5216 |
< |
(map, this, b >>>= 1, subtasks, searchFunction, result)).fork(); |
5217 |
< |
} |
5218 |
< |
U u; |
5219 |
< |
while (result.get() == null && advance() != null) { |
5220 |
< |
if ((u = searchFunction.apply((K)nextKey)) != null) { |
5204 |
> |
public final U getRawResult() { return result.get(); } |
5205 |
> |
public final void compute() { |
5206 |
> |
final Function<? super K, ? extends U> searchFunction; |
5207 |
> |
final AtomicReference<U> result; |
5208 |
> |
if ((searchFunction = this.searchFunction) != null && |
5209 |
> |
(result = this.result) != null) { |
5210 |
> |
for (int i = baseIndex, f, h; batch > 0 && |
5211 |
> |
(h = ((f = baseLimit) + i) >>> 1) > i;) { |
5212 |
> |
if (result.get() != null) |
5213 |
> |
return; |
5214 |
> |
addToPendingCount(1); |
5215 |
> |
new SearchKeysTask<K,V,U> |
5216 |
> |
(this, batch >>>= 1, baseLimit = h, f, tab, |
5217 |
> |
searchFunction, result).fork(); |
5218 |
> |
} |
5219 |
> |
while (result.get() == null) { |
5220 |
> |
U u; |
5221 |
> |
Node<K,V> p; |
5222 |
> |
if ((p = advance()) == null) { |
5223 |
> |
propagateCompletion(); |
5224 |
> |
break; |
5225 |
> |
} |
5226 |
> |
if ((u = searchFunction.apply(p.key)) != null) { |
5227 |
|
if (result.compareAndSet(null, u)) |
5228 |
< |
tryCompleteComputation(null); |
5228 |
> |
quietlyCompleteRoot(); |
5229 |
|
break; |
5230 |
|
} |
5231 |
|
} |
5806 |
– |
} catch (Throwable ex) { |
5807 |
– |
return tryCompleteComputation(ex); |
5232 |
|
} |
5809 |
– |
tryComplete(subtasks); |
5810 |
– |
return false; |
5233 |
|
} |
5812 |
– |
public final U getRawResult() { return result.get(); } |
5234 |
|
} |
5235 |
|
|
5236 |
< |
@SuppressWarnings("serial") static final class SearchValuesTask<K,V,U> |
5237 |
< |
extends BulkAction<K,V,U> { |
5238 |
< |
final Fun<? super V, ? extends U> searchFunction; |
5236 |
> |
@SuppressWarnings("serial") |
5237 |
> |
static final class SearchValuesTask<K,V,U> |
5238 |
> |
extends BulkTask<K,V,U> { |
5239 |
> |
final Function<? super V, ? extends U> searchFunction; |
5240 |
|
final AtomicReference<U> result; |
5241 |
|
SearchValuesTask |
5242 |
< |
(ConcurrentHashMap<K,V> m, BulkTask<K,V,?> p, int b, |
5243 |
< |
SearchValuesTask<K,V,U> nextTask, |
5822 |
< |
Fun<? super V, ? extends U> searchFunction, |
5242 |
> |
(BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t, |
5243 |
> |
Function<? super V, ? extends U> searchFunction, |
5244 |
|
AtomicReference<U> result) { |
5245 |
< |
super(m, p, b, nextTask); |
5245 |
> |
super(p, b, i, f, t); |
5246 |
|
this.searchFunction = searchFunction; this.result = result; |
5247 |
|
} |
5248 |
< |
@SuppressWarnings("unchecked") public final boolean exec() { |
5249 |
< |
AtomicReference<U> result = this.result; |
5250 |
< |
final Fun<? super V, ? extends U> searchFunction = |
5251 |
< |
this.searchFunction; |
5252 |
< |
if (searchFunction == null || result == null) |
5253 |
< |
return abortOnNullFunction(); |
5254 |
< |
SearchValuesTask<K,V,U> subtasks = null; |
5255 |
< |
try { |
5256 |
< |
int b = batch(), c; |
5257 |
< |
while (b > 1 && baseIndex != baseLimit && result.get() == null) { |
5258 |
< |
do {} while (!casPending(c = pending, c+1)); |
5259 |
< |
(subtasks = new SearchValuesTask<K,V,U> |
5260 |
< |
(map, this, b >>>= 1, subtasks, searchFunction, result)).fork(); |
5261 |
< |
} |
5262 |
< |
Object v; U u; |
5263 |
< |
while (result.get() == null && (v = advance()) != null) { |
5264 |
< |
if ((u = searchFunction.apply((V)v)) != null) { |
5248 |
> |
public final U getRawResult() { return result.get(); } |
5249 |
> |
public final void compute() { |
5250 |
> |
final Function<? super V, ? extends U> searchFunction; |
5251 |
> |
final AtomicReference<U> result; |
5252 |
> |
if ((searchFunction = this.searchFunction) != null && |
5253 |
> |
(result = this.result) != null) { |
5254 |
> |
for (int i = baseIndex, f, h; batch > 0 && |
5255 |
> |
(h = ((f = baseLimit) + i) >>> 1) > i;) { |
5256 |
> |
if (result.get() != null) |
5257 |
> |
return; |
5258 |
> |
addToPendingCount(1); |
5259 |
> |
new SearchValuesTask<K,V,U> |
5260 |
> |
(this, batch >>>= 1, baseLimit = h, f, tab, |
5261 |
> |
searchFunction, result).fork(); |
5262 |
> |
} |
5263 |
> |
while (result.get() == null) { |
5264 |
> |
U u; |
5265 |
> |
Node<K,V> p; |
5266 |
> |
if ((p = advance()) == null) { |
5267 |
> |
propagateCompletion(); |
5268 |
> |
break; |
5269 |
> |
} |
5270 |
> |
if ((u = searchFunction.apply(p.val)) != null) { |
5271 |
|
if (result.compareAndSet(null, u)) |
5272 |
< |
tryCompleteComputation(null); |
5272 |
> |
quietlyCompleteRoot(); |
5273 |
|
break; |
5274 |
|
} |
5275 |
|
} |
5849 |
– |
} catch (Throwable ex) { |
5850 |
– |
return tryCompleteComputation(ex); |
5276 |
|
} |
5852 |
– |
tryComplete(subtasks); |
5853 |
– |
return false; |
5277 |
|
} |
5855 |
– |
public final U getRawResult() { return result.get(); } |
5278 |
|
} |
5279 |
|
|
5280 |
< |
@SuppressWarnings("serial") static final class SearchEntriesTask<K,V,U> |
5281 |
< |
extends BulkAction<K,V,U> { |
5282 |
< |
final Fun<Entry<K,V>, ? extends U> searchFunction; |
5280 |
> |
@SuppressWarnings("serial") |
5281 |
> |
static final class SearchEntriesTask<K,V,U> |
5282 |
> |
extends BulkTask<K,V,U> { |
5283 |
> |
final Function<Entry<K,V>, ? extends U> searchFunction; |
5284 |
|
final AtomicReference<U> result; |
5285 |
|
SearchEntriesTask |
5286 |
< |
(ConcurrentHashMap<K,V> m, BulkTask<K,V,?> p, int b, |
5287 |
< |
SearchEntriesTask<K,V,U> nextTask, |
5865 |
< |
Fun<Entry<K,V>, ? extends U> searchFunction, |
5286 |
> |
(BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t, |
5287 |
> |
Function<Entry<K,V>, ? extends U> searchFunction, |
5288 |
|
AtomicReference<U> result) { |
5289 |
< |
super(m, p, b, nextTask); |
5289 |
> |
super(p, b, i, f, t); |
5290 |
|
this.searchFunction = searchFunction; this.result = result; |
5291 |
|
} |
5292 |
< |
@SuppressWarnings("unchecked") public final boolean exec() { |
5293 |
< |
AtomicReference<U> result = this.result; |
5294 |
< |
final Fun<Entry<K,V>, ? extends U> searchFunction = |
5295 |
< |
this.searchFunction; |
5296 |
< |
if (searchFunction == null || result == null) |
5297 |
< |
return abortOnNullFunction(); |
5298 |
< |
SearchEntriesTask<K,V,U> subtasks = null; |
5299 |
< |
try { |
5300 |
< |
int b = batch(), c; |
5301 |
< |
while (b > 1 && baseIndex != baseLimit && result.get() == null) { |
5302 |
< |
do {} while (!casPending(c = pending, c+1)); |
5303 |
< |
(subtasks = new SearchEntriesTask<K,V,U> |
5304 |
< |
(map, this, b >>>= 1, subtasks, searchFunction, result)).fork(); |
5305 |
< |
} |
5306 |
< |
Object v; U u; |
5307 |
< |
while (result.get() == null && (v = advance()) != null) { |
5308 |
< |
if ((u = searchFunction.apply(entryFor((K)nextKey, (V)v))) != null) { |
5309 |
< |
if (result.compareAndSet(null, u)) |
5310 |
< |
tryCompleteComputation(null); |
5292 |
> |
public final U getRawResult() { return result.get(); } |
5293 |
> |
public final void compute() { |
5294 |
> |
final Function<Entry<K,V>, ? extends U> searchFunction; |
5295 |
> |
final AtomicReference<U> result; |
5296 |
> |
if ((searchFunction = this.searchFunction) != null && |
5297 |
> |
(result = this.result) != null) { |
5298 |
> |
for (int i = baseIndex, f, h; batch > 0 && |
5299 |
> |
(h = ((f = baseLimit) + i) >>> 1) > i;) { |
5300 |
> |
if (result.get() != null) |
5301 |
> |
return; |
5302 |
> |
addToPendingCount(1); |
5303 |
> |
new SearchEntriesTask<K,V,U> |
5304 |
> |
(this, batch >>>= 1, baseLimit = h, f, tab, |
5305 |
> |
searchFunction, result).fork(); |
5306 |
> |
} |
5307 |
> |
while (result.get() == null) { |
5308 |
> |
U u; |
5309 |
> |
Node<K,V> p; |
5310 |
> |
if ((p = advance()) == null) { |
5311 |
> |
propagateCompletion(); |
5312 |
|
break; |
5313 |
|
} |
5314 |
+ |
if ((u = searchFunction.apply(p)) != null) { |
5315 |
+ |
if (result.compareAndSet(null, u)) |
5316 |
+ |
quietlyCompleteRoot(); |
5317 |
+ |
return; |
5318 |
+ |
} |
5319 |
|
} |
5892 |
– |
} catch (Throwable ex) { |
5893 |
– |
return tryCompleteComputation(ex); |
5320 |
|
} |
5895 |
– |
tryComplete(subtasks); |
5896 |
– |
return false; |
5321 |
|
} |
5898 |
– |
public final U getRawResult() { return result.get(); } |
5322 |
|
} |
5323 |
|
|
5324 |
< |
@SuppressWarnings("serial") static final class SearchMappingsTask<K,V,U> |
5325 |
< |
extends BulkAction<K,V,U> { |
5326 |
< |
final BiFun<? super K, ? super V, ? extends U> searchFunction; |
5324 |
> |
@SuppressWarnings("serial") |
5325 |
> |
static final class SearchMappingsTask<K,V,U> |
5326 |
> |
extends BulkTask<K,V,U> { |
5327 |
> |
final BiFunction<? super K, ? super V, ? extends U> searchFunction; |
5328 |
|
final AtomicReference<U> result; |
5329 |
|
SearchMappingsTask |
5330 |
< |
(ConcurrentHashMap<K,V> m, BulkTask<K,V,?> p, int b, |
5331 |
< |
SearchMappingsTask<K,V,U> nextTask, |
5908 |
< |
BiFun<? super K, ? super V, ? extends U> searchFunction, |
5330 |
> |
(BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t, |
5331 |
> |
BiFunction<? super K, ? super V, ? extends U> searchFunction, |
5332 |
|
AtomicReference<U> result) { |
5333 |
< |
super(m, p, b, nextTask); |
5333 |
> |
super(p, b, i, f, t); |
5334 |
|
this.searchFunction = searchFunction; this.result = result; |
5335 |
|
} |
5336 |
< |
@SuppressWarnings("unchecked") public final boolean exec() { |
5337 |
< |
AtomicReference<U> result = this.result; |
5338 |
< |
final BiFun<? super K, ? super V, ? extends U> searchFunction = |
5339 |
< |
this.searchFunction; |
5340 |
< |
if (searchFunction == null || result == null) |
5341 |
< |
return abortOnNullFunction(); |
5342 |
< |
SearchMappingsTask<K,V,U> subtasks = null; |
5343 |
< |
try { |
5344 |
< |
int b = batch(), c; |
5345 |
< |
while (b > 1 && baseIndex != baseLimit && result.get() == null) { |
5346 |
< |
do {} while (!casPending(c = pending, c+1)); |
5347 |
< |
(subtasks = new SearchMappingsTask<K,V,U> |
5348 |
< |
(map, this, b >>>= 1, subtasks, searchFunction, result)).fork(); |
5349 |
< |
} |
5350 |
< |
Object v; U u; |
5351 |
< |
while (result.get() == null && (v = advance()) != null) { |
5352 |
< |
if ((u = searchFunction.apply((K)nextKey, (V)v)) != null) { |
5336 |
> |
public final U getRawResult() { return result.get(); } |
5337 |
> |
public final void compute() { |
5338 |
> |
final BiFunction<? super K, ? super V, ? extends U> searchFunction; |
5339 |
> |
final AtomicReference<U> result; |
5340 |
> |
if ((searchFunction = this.searchFunction) != null && |
5341 |
> |
(result = this.result) != null) { |
5342 |
> |
for (int i = baseIndex, f, h; batch > 0 && |
5343 |
> |
(h = ((f = baseLimit) + i) >>> 1) > i;) { |
5344 |
> |
if (result.get() != null) |
5345 |
> |
return; |
5346 |
> |
addToPendingCount(1); |
5347 |
> |
new SearchMappingsTask<K,V,U> |
5348 |
> |
(this, batch >>>= 1, baseLimit = h, f, tab, |
5349 |
> |
searchFunction, result).fork(); |
5350 |
> |
} |
5351 |
> |
while (result.get() == null) { |
5352 |
> |
U u; |
5353 |
> |
Node<K,V> p; |
5354 |
> |
if ((p = advance()) == null) { |
5355 |
> |
propagateCompletion(); |
5356 |
> |
break; |
5357 |
> |
} |
5358 |
> |
if ((u = searchFunction.apply(p.key, p.val)) != null) { |
5359 |
|
if (result.compareAndSet(null, u)) |
5360 |
< |
tryCompleteComputation(null); |
5360 |
> |
quietlyCompleteRoot(); |
5361 |
|
break; |
5362 |
|
} |
5363 |
|
} |
5935 |
– |
} catch (Throwable ex) { |
5936 |
– |
return tryCompleteComputation(ex); |
5364 |
|
} |
5938 |
– |
tryComplete(subtasks); |
5939 |
– |
return false; |
5365 |
|
} |
5941 |
– |
public final U getRawResult() { return result.get(); } |
5366 |
|
} |
5367 |
|
|
5368 |
< |
@SuppressWarnings("serial") static final class ReduceKeysTask<K,V> |
5368 |
> |
@SuppressWarnings("serial") |
5369 |
> |
static final class ReduceKeysTask<K,V> |
5370 |
|
extends BulkTask<K,V,K> { |
5371 |
< |
final BiFun<? super K, ? super K, ? extends K> reducer; |
5371 |
> |
final BiFunction<? super K, ? super K, ? extends K> reducer; |
5372 |
|
K result; |
5373 |
|
ReduceKeysTask<K,V> rights, nextRight; |
5374 |
|
ReduceKeysTask |
5375 |
< |
(ConcurrentHashMap<K,V> m, BulkTask<K,V,?> p, int b, |
5375 |
> |
(BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t, |
5376 |
|
ReduceKeysTask<K,V> nextRight, |
5377 |
< |
BiFun<? super K, ? super K, ? extends K> reducer) { |
5378 |
< |
super(m, p, b); this.nextRight = nextRight; |
5377 |
> |
BiFunction<? super K, ? super K, ? extends K> reducer) { |
5378 |
> |
super(p, b, i, f, t); this.nextRight = nextRight; |
5379 |
|
this.reducer = reducer; |
5380 |
|
} |
5381 |
< |
@SuppressWarnings("unchecked") public final boolean exec() { |
5382 |
< |
final BiFun<? super K, ? super K, ? extends K> reducer = |
5383 |
< |
this.reducer; |
5384 |
< |
if (reducer == null) |
5385 |
< |
return abortOnNullFunction(); |
5386 |
< |
try { |
5387 |
< |
for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) { |
5963 |
< |
do {} while (!casPending(c = pending, c+1)); |
5381 |
> |
public final K getRawResult() { return result; } |
5382 |
> |
public final void compute() { |
5383 |
> |
final BiFunction<? super K, ? super K, ? extends K> reducer; |
5384 |
> |
if ((reducer = this.reducer) != null) { |
5385 |
> |
for (int i = baseIndex, f, h; batch > 0 && |
5386 |
> |
(h = ((f = baseLimit) + i) >>> 1) > i;) { |
5387 |
> |
addToPendingCount(1); |
5388 |
|
(rights = new ReduceKeysTask<K,V> |
5389 |
< |
(map, this, b >>>= 1, rights, reducer)).fork(); |
5389 |
> |
(this, batch >>>= 1, baseLimit = h, f, tab, |
5390 |
> |
rights, reducer)).fork(); |
5391 |
|
} |
5392 |
|
K r = null; |
5393 |
< |
while (advance() != null) { |
5394 |
< |
K u = (K)nextKey; |
5395 |
< |
r = (r == null) ? u : reducer.apply(r, u); |
5393 |
> |
for (Node<K,V> p; (p = advance()) != null; ) { |
5394 |
> |
K u = p.key; |
5395 |
> |
r = (r == null) ? u : u == null ? r : reducer.apply(r, u); |
5396 |
|
} |
5397 |
|
result = r; |
5398 |
< |
for (ReduceKeysTask<K,V> t = this, s;;) { |
5399 |
< |
int c; BulkTask<K,V,?> par; K tr, sr; |
5400 |
< |
if ((c = t.pending) == 0) { |
5401 |
< |
for (s = t.rights; s != null; s = t.rights = s.nextRight) { |
5402 |
< |
if ((sr = s.result) != null) |
5403 |
< |
t.result = ((tr = t.result) == null) ? sr : reducer.apply(tr, sr); |
5404 |
< |
} |
5405 |
< |
if ((par = t.parent) == null || |
5406 |
< |
!(par instanceof ReduceKeysTask)) { |
5407 |
< |
t.quietlyComplete(); |
5408 |
< |
break; |
5409 |
< |
} |
5985 |
< |
t = (ReduceKeysTask<K,V>)par; |
5398 |
> |
CountedCompleter<?> c; |
5399 |
> |
for (c = firstComplete(); c != null; c = c.nextComplete()) { |
5400 |
> |
@SuppressWarnings("unchecked") |
5401 |
> |
ReduceKeysTask<K,V> |
5402 |
> |
t = (ReduceKeysTask<K,V>)c, |
5403 |
> |
s = t.rights; |
5404 |
> |
while (s != null) { |
5405 |
> |
K tr, sr; |
5406 |
> |
if ((sr = s.result) != null) |
5407 |
> |
t.result = (((tr = t.result) == null) ? sr : |
5408 |
> |
reducer.apply(tr, sr)); |
5409 |
> |
s = t.rights = s.nextRight; |
5410 |
|
} |
5987 |
– |
else if (t.casPending(c, c - 1)) |
5988 |
– |
break; |
5411 |
|
} |
5990 |
– |
} catch (Throwable ex) { |
5991 |
– |
return tryCompleteComputation(ex); |
5992 |
– |
} |
5993 |
– |
ReduceKeysTask<K,V> s = rights; |
5994 |
– |
if (s != null && !inForkJoinPool()) { |
5995 |
– |
do { |
5996 |
– |
if (s.tryUnfork()) |
5997 |
– |
s.exec(); |
5998 |
– |
} while ((s = s.nextRight) != null); |
5412 |
|
} |
6000 |
– |
return false; |
5413 |
|
} |
6002 |
– |
public final K getRawResult() { return result; } |
5414 |
|
} |
5415 |
|
|
5416 |
< |
@SuppressWarnings("serial") static final class ReduceValuesTask<K,V> |
5416 |
> |
@SuppressWarnings("serial") |
5417 |
> |
static final class ReduceValuesTask<K,V> |
5418 |
|
extends BulkTask<K,V,V> { |
5419 |
< |
final BiFun<? super V, ? super V, ? extends V> reducer; |
5419 |
> |
final BiFunction<? super V, ? super V, ? extends V> reducer; |
5420 |
|
V result; |
5421 |
|
ReduceValuesTask<K,V> rights, nextRight; |
5422 |
|
ReduceValuesTask |
5423 |
< |
(ConcurrentHashMap<K,V> m, BulkTask<K,V,?> p, int b, |
5423 |
> |
(BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t, |
5424 |
|
ReduceValuesTask<K,V> nextRight, |
5425 |
< |
BiFun<? super V, ? super V, ? extends V> reducer) { |
5426 |
< |
super(m, p, b); this.nextRight = nextRight; |
5425 |
> |
BiFunction<? super V, ? super V, ? extends V> reducer) { |
5426 |
> |
super(p, b, i, f, t); this.nextRight = nextRight; |
5427 |
|
this.reducer = reducer; |
5428 |
|
} |
5429 |
< |
@SuppressWarnings("unchecked") public final boolean exec() { |
5430 |
< |
final BiFun<? super V, ? super V, ? extends V> reducer = |
5431 |
< |
this.reducer; |
5432 |
< |
if (reducer == null) |
5433 |
< |
return abortOnNullFunction(); |
5434 |
< |
try { |
5435 |
< |
for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) { |
6024 |
< |
do {} while (!casPending(c = pending, c+1)); |
5429 |
> |
public final V getRawResult() { return result; } |
5430 |
> |
public final void compute() { |
5431 |
> |
final BiFunction<? super V, ? super V, ? extends V> reducer; |
5432 |
> |
if ((reducer = this.reducer) != null) { |
5433 |
> |
for (int i = baseIndex, f, h; batch > 0 && |
5434 |
> |
(h = ((f = baseLimit) + i) >>> 1) > i;) { |
5435 |
> |
addToPendingCount(1); |
5436 |
|
(rights = new ReduceValuesTask<K,V> |
5437 |
< |
(map, this, b >>>= 1, rights, reducer)).fork(); |
5437 |
> |
(this, batch >>>= 1, baseLimit = h, f, tab, |
5438 |
> |
rights, reducer)).fork(); |
5439 |
|
} |
5440 |
|
V r = null; |
5441 |
< |
Object v; |
5442 |
< |
while ((v = advance()) != null) { |
5443 |
< |
V u = (V)v; |
6032 |
< |
r = (r == null) ? u : reducer.apply(r, u); |
5441 |
> |
for (Node<K,V> p; (p = advance()) != null; ) { |
5442 |
> |
V v = p.val; |
5443 |
> |
r = (r == null) ? v : reducer.apply(r, v); |
5444 |
|
} |
5445 |
|
result = r; |
5446 |
< |
for (ReduceValuesTask<K,V> t = this, s;;) { |
5447 |
< |
int c; BulkTask<K,V,?> par; V tr, sr; |
5448 |
< |
if ((c = t.pending) == 0) { |
5449 |
< |
for (s = t.rights; s != null; s = t.rights = s.nextRight) { |
5450 |
< |
if ((sr = s.result) != null) |
5451 |
< |
t.result = ((tr = t.result) == null) ? sr : reducer.apply(tr, sr); |
5452 |
< |
} |
5453 |
< |
if ((par = t.parent) == null || |
5454 |
< |
!(par instanceof ReduceValuesTask)) { |
5455 |
< |
t.quietlyComplete(); |
5456 |
< |
break; |
5457 |
< |
} |
6047 |
< |
t = (ReduceValuesTask<K,V>)par; |
5446 |
> |
CountedCompleter<?> c; |
5447 |
> |
for (c = firstComplete(); c != null; c = c.nextComplete()) { |
5448 |
> |
@SuppressWarnings("unchecked") |
5449 |
> |
ReduceValuesTask<K,V> |
5450 |
> |
t = (ReduceValuesTask<K,V>)c, |
5451 |
> |
s = t.rights; |
5452 |
> |
while (s != null) { |
5453 |
> |
V tr, sr; |
5454 |
> |
if ((sr = s.result) != null) |
5455 |
> |
t.result = (((tr = t.result) == null) ? sr : |
5456 |
> |
reducer.apply(tr, sr)); |
5457 |
> |
s = t.rights = s.nextRight; |
5458 |
|
} |
6049 |
– |
else if (t.casPending(c, c - 1)) |
6050 |
– |
break; |
5459 |
|
} |
6052 |
– |
} catch (Throwable ex) { |
6053 |
– |
return tryCompleteComputation(ex); |
6054 |
– |
} |
6055 |
– |
ReduceValuesTask<K,V> s = rights; |
6056 |
– |
if (s != null && !inForkJoinPool()) { |
6057 |
– |
do { |
6058 |
– |
if (s.tryUnfork()) |
6059 |
– |
s.exec(); |
6060 |
– |
} while ((s = s.nextRight) != null); |
5460 |
|
} |
6062 |
– |
return false; |
5461 |
|
} |
6064 |
– |
public final V getRawResult() { return result; } |
5462 |
|
} |
5463 |
|
|
5464 |
< |
@SuppressWarnings("serial") static final class ReduceEntriesTask<K,V> |
5464 |
> |
@SuppressWarnings("serial") |
5465 |
> |
static final class ReduceEntriesTask<K,V> |
5466 |
|
extends BulkTask<K,V,Map.Entry<K,V>> { |
5467 |
< |
final BiFun<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer; |
5467 |
> |
final BiFunction<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer; |
5468 |
|
Map.Entry<K,V> result; |
5469 |
|
ReduceEntriesTask<K,V> rights, nextRight; |
5470 |
|
ReduceEntriesTask |
5471 |
< |
(ConcurrentHashMap<K,V> m, BulkTask<K,V,?> p, int b, |
5471 |
> |
(BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t, |
5472 |
|
ReduceEntriesTask<K,V> nextRight, |
5473 |
< |
BiFun<Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer) { |
5474 |
< |
super(m, p, b); this.nextRight = nextRight; |
5473 |
> |
BiFunction<Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer) { |
5474 |
> |
super(p, b, i, f, t); this.nextRight = nextRight; |
5475 |
|
this.reducer = reducer; |
5476 |
|
} |
5477 |
< |
@SuppressWarnings("unchecked") public final boolean exec() { |
5478 |
< |
final BiFun<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer = |
5479 |
< |
this.reducer; |
5480 |
< |
if (reducer == null) |
5481 |
< |
return abortOnNullFunction(); |
5482 |
< |
try { |
5483 |
< |
for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) { |
6086 |
< |
do {} while (!casPending(c = pending, c+1)); |
5477 |
> |
public final Map.Entry<K,V> getRawResult() { return result; } |
5478 |
> |
public final void compute() { |
5479 |
> |
final BiFunction<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer; |
5480 |
> |
if ((reducer = this.reducer) != null) { |
5481 |
> |
for (int i = baseIndex, f, h; batch > 0 && |
5482 |
> |
(h = ((f = baseLimit) + i) >>> 1) > i;) { |
5483 |
> |
addToPendingCount(1); |
5484 |
|
(rights = new ReduceEntriesTask<K,V> |
5485 |
< |
(map, this, b >>>= 1, rights, reducer)).fork(); |
5485 |
> |
(this, batch >>>= 1, baseLimit = h, f, tab, |
5486 |
> |
rights, reducer)).fork(); |
5487 |
|
} |
5488 |
|
Map.Entry<K,V> r = null; |
5489 |
< |
Object v; |
5490 |
< |
while ((v = advance()) != null) { |
6093 |
< |
Map.Entry<K,V> u = entryFor((K)nextKey, (V)v); |
6094 |
< |
r = (r == null) ? u : reducer.apply(r, u); |
6095 |
< |
} |
5489 |
> |
for (Node<K,V> p; (p = advance()) != null; ) |
5490 |
> |
r = (r == null) ? p : reducer.apply(r, p); |
5491 |
|
result = r; |
5492 |
< |
for (ReduceEntriesTask<K,V> t = this, s;;) { |
5493 |
< |
int c; BulkTask<K,V,?> par; Map.Entry<K,V> tr, sr; |
5494 |
< |
if ((c = t.pending) == 0) { |
5495 |
< |
for (s = t.rights; s != null; s = t.rights = s.nextRight) { |
5496 |
< |
if ((sr = s.result) != null) |
5497 |
< |
t.result = ((tr = t.result) == null) ? sr : reducer.apply(tr, sr); |
5498 |
< |
} |
5499 |
< |
if ((par = t.parent) == null || |
5500 |
< |
!(par instanceof ReduceEntriesTask)) { |
5501 |
< |
t.quietlyComplete(); |
5502 |
< |
break; |
5503 |
< |
} |
6109 |
< |
t = (ReduceEntriesTask<K,V>)par; |
5492 |
> |
CountedCompleter<?> c; |
5493 |
> |
for (c = firstComplete(); c != null; c = c.nextComplete()) { |
5494 |
> |
@SuppressWarnings("unchecked") |
5495 |
> |
ReduceEntriesTask<K,V> |
5496 |
> |
t = (ReduceEntriesTask<K,V>)c, |
5497 |
> |
s = t.rights; |
5498 |
> |
while (s != null) { |
5499 |
> |
Map.Entry<K,V> tr, sr; |
5500 |
> |
if ((sr = s.result) != null) |
5501 |
> |
t.result = (((tr = t.result) == null) ? sr : |
5502 |
> |
reducer.apply(tr, sr)); |
5503 |
> |
s = t.rights = s.nextRight; |
5504 |
|
} |
6111 |
– |
else if (t.casPending(c, c - 1)) |
6112 |
– |
break; |
5505 |
|
} |
6114 |
– |
} catch (Throwable ex) { |
6115 |
– |
return tryCompleteComputation(ex); |
6116 |
– |
} |
6117 |
– |
ReduceEntriesTask<K,V> s = rights; |
6118 |
– |
if (s != null && !inForkJoinPool()) { |
6119 |
– |
do { |
6120 |
– |
if (s.tryUnfork()) |
6121 |
– |
s.exec(); |
6122 |
– |
} while ((s = s.nextRight) != null); |
5506 |
|
} |
6124 |
– |
return false; |
5507 |
|
} |
6126 |
– |
public final Map.Entry<K,V> getRawResult() { return result; } |
5508 |
|
} |
5509 |
|
|
5510 |
< |
@SuppressWarnings("serial") static final class MapReduceKeysTask<K,V,U> |
5510 |
> |
@SuppressWarnings("serial") |
5511 |
> |
static final class MapReduceKeysTask<K,V,U> |
5512 |
|
extends BulkTask<K,V,U> { |
5513 |
< |
final Fun<? super K, ? extends U> transformer; |
5514 |
< |
final BiFun<? super U, ? super U, ? extends U> reducer; |
5513 |
> |
final Function<? super K, ? extends U> transformer; |
5514 |
> |
final BiFunction<? super U, ? super U, ? extends U> reducer; |
5515 |
|
U result; |
5516 |
|
MapReduceKeysTask<K,V,U> rights, nextRight; |
5517 |
|
MapReduceKeysTask |
5518 |
< |
(ConcurrentHashMap<K,V> m, BulkTask<K,V,?> p, int b, |
5518 |
> |
(BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t, |
5519 |
|
MapReduceKeysTask<K,V,U> nextRight, |
5520 |
< |
Fun<? super K, ? extends U> transformer, |
5521 |
< |
BiFun<? super U, ? super U, ? extends U> reducer) { |
5522 |
< |
super(m, p, b); this.nextRight = nextRight; |
5520 |
> |
Function<? super K, ? extends U> transformer, |
5521 |
> |
BiFunction<? super U, ? super U, ? extends U> reducer) { |
5522 |
> |
super(p, b, i, f, t); this.nextRight = nextRight; |
5523 |
|
this.transformer = transformer; |
5524 |
|
this.reducer = reducer; |
5525 |
|
} |
5526 |
< |
@SuppressWarnings("unchecked") public final boolean exec() { |
5527 |
< |
final Fun<? super K, ? extends U> transformer = |
5528 |
< |
this.transformer; |
5529 |
< |
final BiFun<? super U, ? super U, ? extends U> reducer = |
5530 |
< |
this.reducer; |
5531 |
< |
if (transformer == null || reducer == null) |
5532 |
< |
return abortOnNullFunction(); |
5533 |
< |
try { |
5534 |
< |
for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) { |
6153 |
< |
do {} while (!casPending(c = pending, c+1)); |
5526 |
> |
public final U getRawResult() { return result; } |
5527 |
> |
public final void compute() { |
5528 |
> |
final Function<? super K, ? extends U> transformer; |
5529 |
> |
final BiFunction<? super U, ? super U, ? extends U> reducer; |
5530 |
> |
if ((transformer = this.transformer) != null && |
5531 |
> |
(reducer = this.reducer) != null) { |
5532 |
> |
for (int i = baseIndex, f, h; batch > 0 && |
5533 |
> |
(h = ((f = baseLimit) + i) >>> 1) > i;) { |
5534 |
> |
addToPendingCount(1); |
5535 |
|
(rights = new MapReduceKeysTask<K,V,U> |
5536 |
< |
(map, this, b >>>= 1, rights, transformer, reducer)).fork(); |
5536 |
> |
(this, batch >>>= 1, baseLimit = h, f, tab, |
5537 |
> |
rights, transformer, reducer)).fork(); |
5538 |
|
} |
5539 |
< |
U r = null, u; |
5540 |
< |
while (advance() != null) { |
5541 |
< |
if ((u = transformer.apply((K)nextKey)) != null) |
5539 |
> |
U r = null; |
5540 |
> |
for (Node<K,V> p; (p = advance()) != null; ) { |
5541 |
> |
U u; |
5542 |
> |
if ((u = transformer.apply(p.key)) != null) |
5543 |
|
r = (r == null) ? u : reducer.apply(r, u); |
5544 |
|
} |
5545 |
|
result = r; |
5546 |
< |
for (MapReduceKeysTask<K,V,U> t = this, s;;) { |
5547 |
< |
int c; BulkTask<K,V,?> par; U tr, sr; |
5548 |
< |
if ((c = t.pending) == 0) { |
5549 |
< |
for (s = t.rights; s != null; s = t.rights = s.nextRight) { |
5550 |
< |
if ((sr = s.result) != null) |
5551 |
< |
t.result = ((tr = t.result) == null) ? sr : reducer.apply(tr, sr); |
5552 |
< |
} |
5553 |
< |
if ((par = t.parent) == null || |
5554 |
< |
!(par instanceof MapReduceKeysTask)) { |
5555 |
< |
t.quietlyComplete(); |
5556 |
< |
break; |
5557 |
< |
} |
6175 |
< |
t = (MapReduceKeysTask<K,V,U>)par; |
5546 |
> |
CountedCompleter<?> c; |
5547 |
> |
for (c = firstComplete(); c != null; c = c.nextComplete()) { |
5548 |
> |
@SuppressWarnings("unchecked") |
5549 |
> |
MapReduceKeysTask<K,V,U> |
5550 |
> |
t = (MapReduceKeysTask<K,V,U>)c, |
5551 |
> |
s = t.rights; |
5552 |
> |
while (s != null) { |
5553 |
> |
U tr, sr; |
5554 |
> |
if ((sr = s.result) != null) |
5555 |
> |
t.result = (((tr = t.result) == null) ? sr : |
5556 |
> |
reducer.apply(tr, sr)); |
5557 |
> |
s = t.rights = s.nextRight; |
5558 |
|
} |
6177 |
– |
else if (t.casPending(c, c - 1)) |
6178 |
– |
break; |
5559 |
|
} |
6180 |
– |
} catch (Throwable ex) { |
6181 |
– |
return tryCompleteComputation(ex); |
5560 |
|
} |
6183 |
– |
MapReduceKeysTask<K,V,U> s = rights; |
6184 |
– |
if (s != null && !inForkJoinPool()) { |
6185 |
– |
do { |
6186 |
– |
if (s.tryUnfork()) |
6187 |
– |
s.exec(); |
6188 |
– |
} while ((s = s.nextRight) != null); |
6189 |
– |
} |
6190 |
– |
return false; |
5561 |
|
} |
6192 |
– |
public final U getRawResult() { return result; } |
5562 |
|
} |
5563 |
|
|
5564 |
< |
@SuppressWarnings("serial") static final class MapReduceValuesTask<K,V,U> |
5564 |
> |
@SuppressWarnings("serial") |
5565 |
> |
static final class MapReduceValuesTask<K,V,U> |
5566 |
|
extends BulkTask<K,V,U> { |
5567 |
< |
final Fun<? super V, ? extends U> transformer; |
5568 |
< |
final BiFun<? super U, ? super U, ? extends U> reducer; |
5567 |
> |
final Function<? super V, ? extends U> transformer; |
5568 |
> |
final BiFunction<? super U, ? super U, ? extends U> reducer; |
5569 |
|
U result; |
5570 |
|
MapReduceValuesTask<K,V,U> rights, nextRight; |
5571 |
|
MapReduceValuesTask |
5572 |
< |
(ConcurrentHashMap<K,V> m, BulkTask<K,V,?> p, int b, |
5572 |
> |
(BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t, |
5573 |
|
MapReduceValuesTask<K,V,U> nextRight, |
5574 |
< |
Fun<? super V, ? extends U> transformer, |
5575 |
< |
BiFun<? super U, ? super U, ? extends U> reducer) { |
5576 |
< |
super(m, p, b); this.nextRight = nextRight; |
5574 |
> |
Function<? super V, ? extends U> transformer, |
5575 |
> |
BiFunction<? super U, ? super U, ? extends U> reducer) { |
5576 |
> |
super(p, b, i, f, t); this.nextRight = nextRight; |
5577 |
|
this.transformer = transformer; |
5578 |
|
this.reducer = reducer; |
5579 |
|
} |
5580 |
< |
@SuppressWarnings("unchecked") public final boolean exec() { |
5581 |
< |
final Fun<? super V, ? extends U> transformer = |
5582 |
< |
this.transformer; |
5583 |
< |
final BiFun<? super U, ? super U, ? extends U> reducer = |
5584 |
< |
this.reducer; |
5585 |
< |
if (transformer == null || reducer == null) |
5586 |
< |
return abortOnNullFunction(); |
5587 |
< |
try { |
5588 |
< |
for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) { |
6219 |
< |
do {} while (!casPending(c = pending, c+1)); |
5580 |
> |
public final U getRawResult() { return result; } |
5581 |
> |
public final void compute() { |
5582 |
> |
final Function<? super V, ? extends U> transformer; |
5583 |
> |
final BiFunction<? super U, ? super U, ? extends U> reducer; |
5584 |
> |
if ((transformer = this.transformer) != null && |
5585 |
> |
(reducer = this.reducer) != null) { |
5586 |
> |
for (int i = baseIndex, f, h; batch > 0 && |
5587 |
> |
(h = ((f = baseLimit) + i) >>> 1) > i;) { |
5588 |
> |
addToPendingCount(1); |
5589 |
|
(rights = new MapReduceValuesTask<K,V,U> |
5590 |
< |
(map, this, b >>>= 1, rights, transformer, reducer)).fork(); |
5590 |
> |
(this, batch >>>= 1, baseLimit = h, f, tab, |
5591 |
> |
rights, transformer, reducer)).fork(); |
5592 |
|
} |
5593 |
< |
U r = null, u; |
5594 |
< |
Object v; |
5595 |
< |
while ((v = advance()) != null) { |
5596 |
< |
if ((u = transformer.apply((V)v)) != null) |
5593 |
> |
U r = null; |
5594 |
> |
for (Node<K,V> p; (p = advance()) != null; ) { |
5595 |
> |
U u; |
5596 |
> |
if ((u = transformer.apply(p.val)) != null) |
5597 |
|
r = (r == null) ? u : reducer.apply(r, u); |
5598 |
|
} |
5599 |
|
result = r; |
5600 |
< |
for (MapReduceValuesTask<K,V,U> t = this, s;;) { |
5601 |
< |
int c; BulkTask<K,V,?> par; U tr, sr; |
5602 |
< |
if ((c = t.pending) == 0) { |
5603 |
< |
for (s = t.rights; s != null; s = t.rights = s.nextRight) { |
5604 |
< |
if ((sr = s.result) != null) |
5605 |
< |
t.result = ((tr = t.result) == null) ? sr : reducer.apply(tr, sr); |
5606 |
< |
} |
5607 |
< |
if ((par = t.parent) == null || |
5608 |
< |
!(par instanceof MapReduceValuesTask)) { |
5609 |
< |
t.quietlyComplete(); |
5610 |
< |
break; |
5611 |
< |
} |
6242 |
< |
t = (MapReduceValuesTask<K,V,U>)par; |
5600 |
> |
CountedCompleter<?> c; |
5601 |
> |
for (c = firstComplete(); c != null; c = c.nextComplete()) { |
5602 |
> |
@SuppressWarnings("unchecked") |
5603 |
> |
MapReduceValuesTask<K,V,U> |
5604 |
> |
t = (MapReduceValuesTask<K,V,U>)c, |
5605 |
> |
s = t.rights; |
5606 |
> |
while (s != null) { |
5607 |
> |
U tr, sr; |
5608 |
> |
if ((sr = s.result) != null) |
5609 |
> |
t.result = (((tr = t.result) == null) ? sr : |
5610 |
> |
reducer.apply(tr, sr)); |
5611 |
> |
s = t.rights = s.nextRight; |
5612 |
|
} |
6244 |
– |
else if (t.casPending(c, c - 1)) |
6245 |
– |
break; |
5613 |
|
} |
6247 |
– |
} catch (Throwable ex) { |
6248 |
– |
return tryCompleteComputation(ex); |
6249 |
– |
} |
6250 |
– |
MapReduceValuesTask<K,V,U> s = rights; |
6251 |
– |
if (s != null && !inForkJoinPool()) { |
6252 |
– |
do { |
6253 |
– |
if (s.tryUnfork()) |
6254 |
– |
s.exec(); |
6255 |
– |
} while ((s = s.nextRight) != null); |
5614 |
|
} |
6257 |
– |
return false; |
5615 |
|
} |
6259 |
– |
public final U getRawResult() { return result; } |
5616 |
|
} |
5617 |
|
|
5618 |
< |
@SuppressWarnings("serial") static final class MapReduceEntriesTask<K,V,U> |
5618 |
> |
@SuppressWarnings("serial") |
5619 |
> |
static final class MapReduceEntriesTask<K,V,U> |
5620 |
|
extends BulkTask<K,V,U> { |
5621 |
< |
final Fun<Map.Entry<K,V>, ? extends U> transformer; |
5622 |
< |
final BiFun<? super U, ? super U, ? extends U> reducer; |
5621 |
> |
final Function<Map.Entry<K,V>, ? extends U> transformer; |
5622 |
> |
final BiFunction<? super U, ? super U, ? extends U> reducer; |
5623 |
|
U result; |
5624 |
|
MapReduceEntriesTask<K,V,U> rights, nextRight; |
5625 |
|
MapReduceEntriesTask |
5626 |
< |
(ConcurrentHashMap<K,V> m, BulkTask<K,V,?> p, int b, |
5626 |
> |
(BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t, |
5627 |
|
MapReduceEntriesTask<K,V,U> nextRight, |
5628 |
< |
Fun<Map.Entry<K,V>, ? extends U> transformer, |
5629 |
< |
BiFun<? super U, ? super U, ? extends U> reducer) { |
5630 |
< |
super(m, p, b); this.nextRight = nextRight; |
5628 |
> |
Function<Map.Entry<K,V>, ? extends U> transformer, |
5629 |
> |
BiFunction<? super U, ? super U, ? extends U> reducer) { |
5630 |
> |
super(p, b, i, f, t); this.nextRight = nextRight; |
5631 |
|
this.transformer = transformer; |
5632 |
|
this.reducer = reducer; |
5633 |
|
} |
5634 |
< |
@SuppressWarnings("unchecked") public final boolean exec() { |
5635 |
< |
final Fun<Map.Entry<K,V>, ? extends U> transformer = |
5636 |
< |
this.transformer; |
5637 |
< |
final BiFun<? super U, ? super U, ? extends U> reducer = |
5638 |
< |
this.reducer; |
5639 |
< |
if (transformer == null || reducer == null) |
5640 |
< |
return abortOnNullFunction(); |
5641 |
< |
try { |
5642 |
< |
for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) { |
6286 |
< |
do {} while (!casPending(c = pending, c+1)); |
5634 |
> |
public final U getRawResult() { return result; } |
5635 |
> |
public final void compute() { |
5636 |
> |
final Function<Map.Entry<K,V>, ? extends U> transformer; |
5637 |
> |
final BiFunction<? super U, ? super U, ? extends U> reducer; |
5638 |
> |
if ((transformer = this.transformer) != null && |
5639 |
> |
(reducer = this.reducer) != null) { |
5640 |
> |
for (int i = baseIndex, f, h; batch > 0 && |
5641 |
> |
(h = ((f = baseLimit) + i) >>> 1) > i;) { |
5642 |
> |
addToPendingCount(1); |
5643 |
|
(rights = new MapReduceEntriesTask<K,V,U> |
5644 |
< |
(map, this, b >>>= 1, rights, transformer, reducer)).fork(); |
5644 |
> |
(this, batch >>>= 1, baseLimit = h, f, tab, |
5645 |
> |
rights, transformer, reducer)).fork(); |
5646 |
|
} |
5647 |
< |
U r = null, u; |
5648 |
< |
Object v; |
5649 |
< |
while ((v = advance()) != null) { |
5650 |
< |
if ((u = transformer.apply(entryFor((K)nextKey, (V)v))) != null) |
5647 |
> |
U r = null; |
5648 |
> |
for (Node<K,V> p; (p = advance()) != null; ) { |
5649 |
> |
U u; |
5650 |
> |
if ((u = transformer.apply(p)) != null) |
5651 |
|
r = (r == null) ? u : reducer.apply(r, u); |
5652 |
|
} |
5653 |
|
result = r; |
5654 |
< |
for (MapReduceEntriesTask<K,V,U> t = this, s;;) { |
5655 |
< |
int c; BulkTask<K,V,?> par; U tr, sr; |
5656 |
< |
if ((c = t.pending) == 0) { |
5657 |
< |
for (s = t.rights; s != null; s = t.rights = s.nextRight) { |
5658 |
< |
if ((sr = s.result) != null) |
5659 |
< |
t.result = ((tr = t.result) == null) ? sr : reducer.apply(tr, sr); |
5660 |
< |
} |
5661 |
< |
if ((par = t.parent) == null || |
5662 |
< |
!(par instanceof MapReduceEntriesTask)) { |
5663 |
< |
t.quietlyComplete(); |
5664 |
< |
break; |
5665 |
< |
} |
6309 |
< |
t = (MapReduceEntriesTask<K,V,U>)par; |
5654 |
> |
CountedCompleter<?> c; |
5655 |
> |
for (c = firstComplete(); c != null; c = c.nextComplete()) { |
5656 |
> |
@SuppressWarnings("unchecked") |
5657 |
> |
MapReduceEntriesTask<K,V,U> |
5658 |
> |
t = (MapReduceEntriesTask<K,V,U>)c, |
5659 |
> |
s = t.rights; |
5660 |
> |
while (s != null) { |
5661 |
> |
U tr, sr; |
5662 |
> |
if ((sr = s.result) != null) |
5663 |
> |
t.result = (((tr = t.result) == null) ? sr : |
5664 |
> |
reducer.apply(tr, sr)); |
5665 |
> |
s = t.rights = s.nextRight; |
5666 |
|
} |
6311 |
– |
else if (t.casPending(c, c - 1)) |
6312 |
– |
break; |
5667 |
|
} |
6314 |
– |
} catch (Throwable ex) { |
6315 |
– |
return tryCompleteComputation(ex); |
6316 |
– |
} |
6317 |
– |
MapReduceEntriesTask<K,V,U> s = rights; |
6318 |
– |
if (s != null && !inForkJoinPool()) { |
6319 |
– |
do { |
6320 |
– |
if (s.tryUnfork()) |
6321 |
– |
s.exec(); |
6322 |
– |
} while ((s = s.nextRight) != null); |
5668 |
|
} |
6324 |
– |
return false; |
5669 |
|
} |
6326 |
– |
public final U getRawResult() { return result; } |
5670 |
|
} |
5671 |
|
|
5672 |
< |
@SuppressWarnings("serial") static final class MapReduceMappingsTask<K,V,U> |
5672 |
> |
@SuppressWarnings("serial") |
5673 |
> |
static final class MapReduceMappingsTask<K,V,U> |
5674 |
|
extends BulkTask<K,V,U> { |
5675 |
< |
final BiFun<? super K, ? super V, ? extends U> transformer; |
5676 |
< |
final BiFun<? super U, ? super U, ? extends U> reducer; |
5675 |
> |
final BiFunction<? super K, ? super V, ? extends U> transformer; |
5676 |
> |
final BiFunction<? super U, ? super U, ? extends U> reducer; |
5677 |
|
U result; |
5678 |
|
MapReduceMappingsTask<K,V,U> rights, nextRight; |
5679 |
|
MapReduceMappingsTask |
5680 |
< |
(ConcurrentHashMap<K,V> m, BulkTask<K,V,?> p, int b, |
5680 |
> |
(BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t, |
5681 |
|
MapReduceMappingsTask<K,V,U> nextRight, |
5682 |
< |
BiFun<? super K, ? super V, ? extends U> transformer, |
5683 |
< |
BiFun<? super U, ? super U, ? extends U> reducer) { |
5684 |
< |
super(m, p, b); this.nextRight = nextRight; |
5682 |
> |
BiFunction<? super K, ? super V, ? extends U> transformer, |
5683 |
> |
BiFunction<? super U, ? super U, ? extends U> reducer) { |
5684 |
> |
super(p, b, i, f, t); this.nextRight = nextRight; |
5685 |
|
this.transformer = transformer; |
5686 |
|
this.reducer = reducer; |
5687 |
|
} |
5688 |
< |
@SuppressWarnings("unchecked") public final boolean exec() { |
5689 |
< |
final BiFun<? super K, ? super V, ? extends U> transformer = |
5690 |
< |
this.transformer; |
5691 |
< |
final BiFun<? super U, ? super U, ? extends U> reducer = |
5692 |
< |
this.reducer; |
5693 |
< |
if (transformer == null || reducer == null) |
5694 |
< |
return abortOnNullFunction(); |
5695 |
< |
try { |
5696 |
< |
for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) { |
6353 |
< |
do {} while (!casPending(c = pending, c+1)); |
5688 |
> |
public final U getRawResult() { return result; } |
5689 |
> |
public final void compute() { |
5690 |
> |
final BiFunction<? super K, ? super V, ? extends U> transformer; |
5691 |
> |
final BiFunction<? super U, ? super U, ? extends U> reducer; |
5692 |
> |
if ((transformer = this.transformer) != null && |
5693 |
> |
(reducer = this.reducer) != null) { |
5694 |
> |
for (int i = baseIndex, f, h; batch > 0 && |
5695 |
> |
(h = ((f = baseLimit) + i) >>> 1) > i;) { |
5696 |
> |
addToPendingCount(1); |
5697 |
|
(rights = new MapReduceMappingsTask<K,V,U> |
5698 |
< |
(map, this, b >>>= 1, rights, transformer, reducer)).fork(); |
5698 |
> |
(this, batch >>>= 1, baseLimit = h, f, tab, |
5699 |
> |
rights, transformer, reducer)).fork(); |
5700 |
|
} |
5701 |
< |
U r = null, u; |
5702 |
< |
Object v; |
5703 |
< |
while ((v = advance()) != null) { |
5704 |
< |
if ((u = transformer.apply((K)nextKey, (V)v)) != null) |
5701 |
> |
U r = null; |
5702 |
> |
for (Node<K,V> p; (p = advance()) != null; ) { |
5703 |
> |
U u; |
5704 |
> |
if ((u = transformer.apply(p.key, p.val)) != null) |
5705 |
|
r = (r == null) ? u : reducer.apply(r, u); |
5706 |
|
} |
5707 |
|
result = r; |
5708 |
< |
for (MapReduceMappingsTask<K,V,U> t = this, s;;) { |
5709 |
< |
int c; BulkTask<K,V,?> par; U tr, sr; |
5710 |
< |
if ((c = t.pending) == 0) { |
5711 |
< |
for (s = t.rights; s != null; s = t.rights = s.nextRight) { |
5712 |
< |
if ((sr = s.result) != null) |
5713 |
< |
t.result = ((tr = t.result) == null) ? sr : reducer.apply(tr, sr); |
5714 |
< |
} |
5715 |
< |
if ((par = t.parent) == null || |
5716 |
< |
!(par instanceof MapReduceMappingsTask)) { |
5717 |
< |
t.quietlyComplete(); |
5718 |
< |
break; |
5719 |
< |
} |
6376 |
< |
t = (MapReduceMappingsTask<K,V,U>)par; |
5708 |
> |
CountedCompleter<?> c; |
5709 |
> |
for (c = firstComplete(); c != null; c = c.nextComplete()) { |
5710 |
> |
@SuppressWarnings("unchecked") |
5711 |
> |
MapReduceMappingsTask<K,V,U> |
5712 |
> |
t = (MapReduceMappingsTask<K,V,U>)c, |
5713 |
> |
s = t.rights; |
5714 |
> |
while (s != null) { |
5715 |
> |
U tr, sr; |
5716 |
> |
if ((sr = s.result) != null) |
5717 |
> |
t.result = (((tr = t.result) == null) ? sr : |
5718 |
> |
reducer.apply(tr, sr)); |
5719 |
> |
s = t.rights = s.nextRight; |
5720 |
|
} |
6378 |
– |
else if (t.casPending(c, c - 1)) |
6379 |
– |
break; |
5721 |
|
} |
6381 |
– |
} catch (Throwable ex) { |
6382 |
– |
return tryCompleteComputation(ex); |
6383 |
– |
} |
6384 |
– |
MapReduceMappingsTask<K,V,U> s = rights; |
6385 |
– |
if (s != null && !inForkJoinPool()) { |
6386 |
– |
do { |
6387 |
– |
if (s.tryUnfork()) |
6388 |
– |
s.exec(); |
6389 |
– |
} while ((s = s.nextRight) != null); |
5722 |
|
} |
6391 |
– |
return false; |
5723 |
|
} |
6393 |
– |
public final U getRawResult() { return result; } |
5724 |
|
} |
5725 |
|
|
5726 |
< |
@SuppressWarnings("serial") static final class MapReduceKeysToDoubleTask<K,V> |
5726 |
> |
@SuppressWarnings("serial") |
5727 |
> |
static final class MapReduceKeysToDoubleTask<K,V> |
5728 |
|
extends BulkTask<K,V,Double> { |
5729 |
< |
final ObjectToDouble<? super K> transformer; |
5730 |
< |
final DoubleByDoubleToDouble reducer; |
5729 |
> |
final ToDoubleFunction<? super K> transformer; |
5730 |
> |
final DoubleBinaryOperator reducer; |
5731 |
|
final double basis; |
5732 |
|
double result; |
5733 |
|
MapReduceKeysToDoubleTask<K,V> rights, nextRight; |
5734 |
|
MapReduceKeysToDoubleTask |
5735 |
< |
(ConcurrentHashMap<K,V> m, BulkTask<K,V,?> p, int b, |
5735 |
> |
(BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t, |
5736 |
|
MapReduceKeysToDoubleTask<K,V> nextRight, |
5737 |
< |
ObjectToDouble<? super K> transformer, |
5737 |
> |
ToDoubleFunction<? super K> transformer, |
5738 |
|
double basis, |
5739 |
< |
DoubleByDoubleToDouble reducer) { |
5740 |
< |
super(m, p, b); this.nextRight = nextRight; |
5739 |
> |
DoubleBinaryOperator reducer) { |
5740 |
> |
super(p, b, i, f, t); this.nextRight = nextRight; |
5741 |
|
this.transformer = transformer; |
5742 |
|
this.basis = basis; this.reducer = reducer; |
5743 |
|
} |
5744 |
< |
@SuppressWarnings("unchecked") public final boolean exec() { |
5745 |
< |
final ObjectToDouble<? super K> transformer = |
5746 |
< |
this.transformer; |
5747 |
< |
final DoubleByDoubleToDouble reducer = this.reducer; |
5748 |
< |
if (transformer == null || reducer == null) |
5749 |
< |
return abortOnNullFunction(); |
5750 |
< |
try { |
5751 |
< |
final double id = this.basis; |
5752 |
< |
for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) { |
5753 |
< |
do {} while (!casPending(c = pending, c+1)); |
5744 |
> |
public final Double getRawResult() { return result; } |
5745 |
> |
public final void compute() { |
5746 |
> |
final ToDoubleFunction<? super K> transformer; |
5747 |
> |
final DoubleBinaryOperator reducer; |
5748 |
> |
if ((transformer = this.transformer) != null && |
5749 |
> |
(reducer = this.reducer) != null) { |
5750 |
> |
double r = this.basis; |
5751 |
> |
for (int i = baseIndex, f, h; batch > 0 && |
5752 |
> |
(h = ((f = baseLimit) + i) >>> 1) > i;) { |
5753 |
> |
addToPendingCount(1); |
5754 |
|
(rights = new MapReduceKeysToDoubleTask<K,V> |
5755 |
< |
(map, this, b >>>= 1, rights, transformer, id, reducer)).fork(); |
5755 |
> |
(this, batch >>>= 1, baseLimit = h, f, tab, |
5756 |
> |
rights, transformer, r, reducer)).fork(); |
5757 |
|
} |
5758 |
< |
double r = id; |
5759 |
< |
while (advance() != null) |
6428 |
< |
r = reducer.apply(r, transformer.apply((K)nextKey)); |
5758 |
> |
for (Node<K,V> p; (p = advance()) != null; ) |
5759 |
> |
r = reducer.applyAsDouble(r, transformer.applyAsDouble(p.key)); |
5760 |
|
result = r; |
5761 |
< |
for (MapReduceKeysToDoubleTask<K,V> t = this, s;;) { |
5762 |
< |
int c; BulkTask<K,V,?> par; |
5763 |
< |
if ((c = t.pending) == 0) { |
5764 |
< |
for (s = t.rights; s != null; s = t.rights = s.nextRight) { |
5765 |
< |
t.result = reducer.apply(t.result, s.result); |
5766 |
< |
} |
5767 |
< |
if ((par = t.parent) == null || |
5768 |
< |
!(par instanceof MapReduceKeysToDoubleTask)) { |
5769 |
< |
t.quietlyComplete(); |
6439 |
< |
break; |
6440 |
< |
} |
6441 |
< |
t = (MapReduceKeysToDoubleTask<K,V>)par; |
5761 |
> |
CountedCompleter<?> c; |
5762 |
> |
for (c = firstComplete(); c != null; c = c.nextComplete()) { |
5763 |
> |
@SuppressWarnings("unchecked") |
5764 |
> |
MapReduceKeysToDoubleTask<K,V> |
5765 |
> |
t = (MapReduceKeysToDoubleTask<K,V>)c, |
5766 |
> |
s = t.rights; |
5767 |
> |
while (s != null) { |
5768 |
> |
t.result = reducer.applyAsDouble(t.result, s.result); |
5769 |
> |
s = t.rights = s.nextRight; |
5770 |
|
} |
6443 |
– |
else if (t.casPending(c, c - 1)) |
6444 |
– |
break; |
5771 |
|
} |
6446 |
– |
} catch (Throwable ex) { |
6447 |
– |
return tryCompleteComputation(ex); |
5772 |
|
} |
6449 |
– |
MapReduceKeysToDoubleTask<K,V> s = rights; |
6450 |
– |
if (s != null && !inForkJoinPool()) { |
6451 |
– |
do { |
6452 |
– |
if (s.tryUnfork()) |
6453 |
– |
s.exec(); |
6454 |
– |
} while ((s = s.nextRight) != null); |
6455 |
– |
} |
6456 |
– |
return false; |
5773 |
|
} |
6458 |
– |
public final Double getRawResult() { return result; } |
5774 |
|
} |
5775 |
|
|
5776 |
< |
@SuppressWarnings("serial") static final class MapReduceValuesToDoubleTask<K,V> |
5776 |
> |
@SuppressWarnings("serial") |
5777 |
> |
static final class MapReduceValuesToDoubleTask<K,V> |
5778 |
|
extends BulkTask<K,V,Double> { |
5779 |
< |
final ObjectToDouble<? super V> transformer; |
5780 |
< |
final DoubleByDoubleToDouble reducer; |
5779 |
> |
final ToDoubleFunction<? super V> transformer; |
5780 |
> |
final DoubleBinaryOperator reducer; |
5781 |
|
final double basis; |
5782 |
|
double result; |
5783 |
|
MapReduceValuesToDoubleTask<K,V> rights, nextRight; |
5784 |
|
MapReduceValuesToDoubleTask |
5785 |
< |
(ConcurrentHashMap<K,V> m, BulkTask<K,V,?> p, int b, |
5785 |
> |
(BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t, |
5786 |
|
MapReduceValuesToDoubleTask<K,V> nextRight, |
5787 |
< |
ObjectToDouble<? super V> transformer, |
5787 |
> |
ToDoubleFunction<? super V> transformer, |
5788 |
|
double basis, |
5789 |
< |
DoubleByDoubleToDouble reducer) { |
5790 |
< |
super(m, p, b); this.nextRight = nextRight; |
5789 |
> |
DoubleBinaryOperator reducer) { |
5790 |
> |
super(p, b, i, f, t); this.nextRight = nextRight; |
5791 |
|
this.transformer = transformer; |
5792 |
|
this.basis = basis; this.reducer = reducer; |
5793 |
|
} |
5794 |
< |
@SuppressWarnings("unchecked") public final boolean exec() { |
5795 |
< |
final ObjectToDouble<? super V> transformer = |
5796 |
< |
this.transformer; |
5797 |
< |
final DoubleByDoubleToDouble reducer = this.reducer; |
5798 |
< |
if (transformer == null || reducer == null) |
5799 |
< |
return abortOnNullFunction(); |
5800 |
< |
try { |
5801 |
< |
final double id = this.basis; |
5802 |
< |
for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) { |
5803 |
< |
do {} while (!casPending(c = pending, c+1)); |
5794 |
> |
public final Double getRawResult() { return result; } |
5795 |
> |
public final void compute() { |
5796 |
> |
final ToDoubleFunction<? super V> transformer; |
5797 |
> |
final DoubleBinaryOperator reducer; |
5798 |
> |
if ((transformer = this.transformer) != null && |
5799 |
> |
(reducer = this.reducer) != null) { |
5800 |
> |
double r = this.basis; |
5801 |
> |
for (int i = baseIndex, f, h; batch > 0 && |
5802 |
> |
(h = ((f = baseLimit) + i) >>> 1) > i;) { |
5803 |
> |
addToPendingCount(1); |
5804 |
|
(rights = new MapReduceValuesToDoubleTask<K,V> |
5805 |
< |
(map, this, b >>>= 1, rights, transformer, id, reducer)).fork(); |
5805 |
> |
(this, batch >>>= 1, baseLimit = h, f, tab, |
5806 |
> |
rights, transformer, r, reducer)).fork(); |
5807 |
|
} |
5808 |
< |
double r = id; |
5809 |
< |
Object v; |
6493 |
< |
while ((v = advance()) != null) |
6494 |
< |
r = reducer.apply(r, transformer.apply((V)v)); |
5808 |
> |
for (Node<K,V> p; (p = advance()) != null; ) |
5809 |
> |
r = reducer.applyAsDouble(r, transformer.applyAsDouble(p.val)); |
5810 |
|
result = r; |
5811 |
< |
for (MapReduceValuesToDoubleTask<K,V> t = this, s;;) { |
5812 |
< |
int c; BulkTask<K,V,?> par; |
5813 |
< |
if ((c = t.pending) == 0) { |
5814 |
< |
for (s = t.rights; s != null; s = t.rights = s.nextRight) { |
5815 |
< |
t.result = reducer.apply(t.result, s.result); |
5816 |
< |
} |
5817 |
< |
if ((par = t.parent) == null || |
5818 |
< |
!(par instanceof MapReduceValuesToDoubleTask)) { |
5819 |
< |
t.quietlyComplete(); |
6505 |
< |
break; |
6506 |
< |
} |
6507 |
< |
t = (MapReduceValuesToDoubleTask<K,V>)par; |
5811 |
> |
CountedCompleter<?> c; |
5812 |
> |
for (c = firstComplete(); c != null; c = c.nextComplete()) { |
5813 |
> |
@SuppressWarnings("unchecked") |
5814 |
> |
MapReduceValuesToDoubleTask<K,V> |
5815 |
> |
t = (MapReduceValuesToDoubleTask<K,V>)c, |
5816 |
> |
s = t.rights; |
5817 |
> |
while (s != null) { |
5818 |
> |
t.result = reducer.applyAsDouble(t.result, s.result); |
5819 |
> |
s = t.rights = s.nextRight; |
5820 |
|
} |
6509 |
– |
else if (t.casPending(c, c - 1)) |
6510 |
– |
break; |
5821 |
|
} |
6512 |
– |
} catch (Throwable ex) { |
6513 |
– |
return tryCompleteComputation(ex); |
6514 |
– |
} |
6515 |
– |
MapReduceValuesToDoubleTask<K,V> s = rights; |
6516 |
– |
if (s != null && !inForkJoinPool()) { |
6517 |
– |
do { |
6518 |
– |
if (s.tryUnfork()) |
6519 |
– |
s.exec(); |
6520 |
– |
} while ((s = s.nextRight) != null); |
5822 |
|
} |
6522 |
– |
return false; |
5823 |
|
} |
6524 |
– |
public final Double getRawResult() { return result; } |
5824 |
|
} |
5825 |
|
|
5826 |
< |
@SuppressWarnings("serial") static final class MapReduceEntriesToDoubleTask<K,V> |
5826 |
> |
@SuppressWarnings("serial") |
5827 |
> |
static final class MapReduceEntriesToDoubleTask<K,V> |
5828 |
|
extends BulkTask<K,V,Double> { |
5829 |
< |
final ObjectToDouble<Map.Entry<K,V>> transformer; |
5830 |
< |
final DoubleByDoubleToDouble reducer; |
5829 |
> |
final ToDoubleFunction<Map.Entry<K,V>> transformer; |
5830 |
> |
final DoubleBinaryOperator reducer; |
5831 |
|
final double basis; |
5832 |
|
double result; |
5833 |
|
MapReduceEntriesToDoubleTask<K,V> rights, nextRight; |
5834 |
|
MapReduceEntriesToDoubleTask |
5835 |
< |
(ConcurrentHashMap<K,V> m, BulkTask<K,V,?> p, int b, |
5835 |
> |
(BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t, |
5836 |
|
MapReduceEntriesToDoubleTask<K,V> nextRight, |
5837 |
< |
ObjectToDouble<Map.Entry<K,V>> transformer, |
5837 |
> |
ToDoubleFunction<Map.Entry<K,V>> transformer, |
5838 |
|
double basis, |
5839 |
< |
DoubleByDoubleToDouble reducer) { |
5840 |
< |
super(m, p, b); this.nextRight = nextRight; |
5839 |
> |
DoubleBinaryOperator reducer) { |
5840 |
> |
super(p, b, i, f, t); this.nextRight = nextRight; |
5841 |
|
this.transformer = transformer; |
5842 |
|
this.basis = basis; this.reducer = reducer; |
5843 |
|
} |
5844 |
< |
@SuppressWarnings("unchecked") public final boolean exec() { |
5845 |
< |
final ObjectToDouble<Map.Entry<K,V>> transformer = |
5846 |
< |
this.transformer; |
5847 |
< |
final DoubleByDoubleToDouble reducer = this.reducer; |
5848 |
< |
if (transformer == null || reducer == null) |
5849 |
< |
return abortOnNullFunction(); |
5850 |
< |
try { |
5851 |
< |
final double id = this.basis; |
5852 |
< |
for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) { |
5853 |
< |
do {} while (!casPending(c = pending, c+1)); |
5844 |
> |
public final Double getRawResult() { return result; } |
5845 |
> |
public final void compute() { |
5846 |
> |
final ToDoubleFunction<Map.Entry<K,V>> transformer; |
5847 |
> |
final DoubleBinaryOperator reducer; |
5848 |
> |
if ((transformer = this.transformer) != null && |
5849 |
> |
(reducer = this.reducer) != null) { |
5850 |
> |
double r = this.basis; |
5851 |
> |
for (int i = baseIndex, f, h; batch > 0 && |
5852 |
> |
(h = ((f = baseLimit) + i) >>> 1) > i;) { |
5853 |
> |
addToPendingCount(1); |
5854 |
|
(rights = new MapReduceEntriesToDoubleTask<K,V> |
5855 |
< |
(map, this, b >>>= 1, rights, transformer, id, reducer)).fork(); |
5855 |
> |
(this, batch >>>= 1, baseLimit = h, f, tab, |
5856 |
> |
rights, transformer, r, reducer)).fork(); |
5857 |
|
} |
5858 |
< |
double r = id; |
5859 |
< |
Object v; |
6559 |
< |
while ((v = advance()) != null) |
6560 |
< |
r = reducer.apply(r, transformer.apply(entryFor((K)nextKey, (V)v))); |
5858 |
> |
for (Node<K,V> p; (p = advance()) != null; ) |
5859 |
> |
r = reducer.applyAsDouble(r, transformer.applyAsDouble(p)); |
5860 |
|
result = r; |
5861 |
< |
for (MapReduceEntriesToDoubleTask<K,V> t = this, s;;) { |
5862 |
< |
int c; BulkTask<K,V,?> par; |
5863 |
< |
if ((c = t.pending) == 0) { |
5864 |
< |
for (s = t.rights; s != null; s = t.rights = s.nextRight) { |
5865 |
< |
t.result = reducer.apply(t.result, s.result); |
5866 |
< |
} |
5867 |
< |
if ((par = t.parent) == null || |
5868 |
< |
!(par instanceof MapReduceEntriesToDoubleTask)) { |
5869 |
< |
t.quietlyComplete(); |
6571 |
< |
break; |
6572 |
< |
} |
6573 |
< |
t = (MapReduceEntriesToDoubleTask<K,V>)par; |
5861 |
> |
CountedCompleter<?> c; |
5862 |
> |
for (c = firstComplete(); c != null; c = c.nextComplete()) { |
5863 |
> |
@SuppressWarnings("unchecked") |
5864 |
> |
MapReduceEntriesToDoubleTask<K,V> |
5865 |
> |
t = (MapReduceEntriesToDoubleTask<K,V>)c, |
5866 |
> |
s = t.rights; |
5867 |
> |
while (s != null) { |
5868 |
> |
t.result = reducer.applyAsDouble(t.result, s.result); |
5869 |
> |
s = t.rights = s.nextRight; |
5870 |
|
} |
6575 |
– |
else if (t.casPending(c, c - 1)) |
6576 |
– |
break; |
5871 |
|
} |
6578 |
– |
} catch (Throwable ex) { |
6579 |
– |
return tryCompleteComputation(ex); |
6580 |
– |
} |
6581 |
– |
MapReduceEntriesToDoubleTask<K,V> s = rights; |
6582 |
– |
if (s != null && !inForkJoinPool()) { |
6583 |
– |
do { |
6584 |
– |
if (s.tryUnfork()) |
6585 |
– |
s.exec(); |
6586 |
– |
} while ((s = s.nextRight) != null); |
5872 |
|
} |
6588 |
– |
return false; |
5873 |
|
} |
6590 |
– |
public final Double getRawResult() { return result; } |
5874 |
|
} |
5875 |
|
|
5876 |
< |
@SuppressWarnings("serial") static final class MapReduceMappingsToDoubleTask<K,V> |
5876 |
> |
@SuppressWarnings("serial") |
5877 |
> |
static final class MapReduceMappingsToDoubleTask<K,V> |
5878 |
|
extends BulkTask<K,V,Double> { |
5879 |
< |
final ObjectByObjectToDouble<? super K, ? super V> transformer; |
5880 |
< |
final DoubleByDoubleToDouble reducer; |
5879 |
> |
final ToDoubleBiFunction<? super K, ? super V> transformer; |
5880 |
> |
final DoubleBinaryOperator reducer; |
5881 |
|
final double basis; |
5882 |
|
double result; |
5883 |
|
MapReduceMappingsToDoubleTask<K,V> rights, nextRight; |
5884 |
|
MapReduceMappingsToDoubleTask |
5885 |
< |
(ConcurrentHashMap<K,V> m, BulkTask<K,V,?> p, int b, |
5885 |
> |
(BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t, |
5886 |
|
MapReduceMappingsToDoubleTask<K,V> nextRight, |
5887 |
< |
ObjectByObjectToDouble<? super K, ? super V> transformer, |
5887 |
> |
ToDoubleBiFunction<? super K, ? super V> transformer, |
5888 |
|
double basis, |
5889 |
< |
DoubleByDoubleToDouble reducer) { |
5890 |
< |
super(m, p, b); this.nextRight = nextRight; |
5889 |
> |
DoubleBinaryOperator reducer) { |
5890 |
> |
super(p, b, i, f, t); this.nextRight = nextRight; |
5891 |
|
this.transformer = transformer; |
5892 |
|
this.basis = basis; this.reducer = reducer; |
5893 |
|
} |
5894 |
< |
@SuppressWarnings("unchecked") public final boolean exec() { |
5895 |
< |
final ObjectByObjectToDouble<? super K, ? super V> transformer = |
5896 |
< |
this.transformer; |
5897 |
< |
final DoubleByDoubleToDouble reducer = this.reducer; |
5898 |
< |
if (transformer == null || reducer == null) |
5899 |
< |
return abortOnNullFunction(); |
5900 |
< |
try { |
5901 |
< |
final double id = this.basis; |
5902 |
< |
for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) { |
5903 |
< |
do {} while (!casPending(c = pending, c+1)); |
5894 |
> |
public final Double getRawResult() { return result; } |
5895 |
> |
public final void compute() { |
5896 |
> |
final ToDoubleBiFunction<? super K, ? super V> transformer; |
5897 |
> |
final DoubleBinaryOperator reducer; |
5898 |
> |
if ((transformer = this.transformer) != null && |
5899 |
> |
(reducer = this.reducer) != null) { |
5900 |
> |
double r = this.basis; |
5901 |
> |
for (int i = baseIndex, f, h; batch > 0 && |
5902 |
> |
(h = ((f = baseLimit) + i) >>> 1) > i;) { |
5903 |
> |
addToPendingCount(1); |
5904 |
|
(rights = new MapReduceMappingsToDoubleTask<K,V> |
5905 |
< |
(map, this, b >>>= 1, rights, transformer, id, reducer)).fork(); |
5905 |
> |
(this, batch >>>= 1, baseLimit = h, f, tab, |
5906 |
> |
rights, transformer, r, reducer)).fork(); |
5907 |
|
} |
5908 |
< |
double r = id; |
5909 |
< |
Object v; |
6625 |
< |
while ((v = advance()) != null) |
6626 |
< |
r = reducer.apply(r, transformer.apply((K)nextKey, (V)v)); |
5908 |
> |
for (Node<K,V> p; (p = advance()) != null; ) |
5909 |
> |
r = reducer.applyAsDouble(r, transformer.applyAsDouble(p.key, p.val)); |
5910 |
|
result = r; |
5911 |
< |
for (MapReduceMappingsToDoubleTask<K,V> t = this, s;;) { |
5912 |
< |
int c; BulkTask<K,V,?> par; |
5913 |
< |
if ((c = t.pending) == 0) { |
5914 |
< |
for (s = t.rights; s != null; s = t.rights = s.nextRight) { |
5915 |
< |
t.result = reducer.apply(t.result, s.result); |
5916 |
< |
} |
5917 |
< |
if ((par = t.parent) == null || |
5918 |
< |
!(par instanceof MapReduceMappingsToDoubleTask)) { |
5919 |
< |
t.quietlyComplete(); |
6637 |
< |
break; |
6638 |
< |
} |
6639 |
< |
t = (MapReduceMappingsToDoubleTask<K,V>)par; |
5911 |
> |
CountedCompleter<?> c; |
5912 |
> |
for (c = firstComplete(); c != null; c = c.nextComplete()) { |
5913 |
> |
@SuppressWarnings("unchecked") |
5914 |
> |
MapReduceMappingsToDoubleTask<K,V> |
5915 |
> |
t = (MapReduceMappingsToDoubleTask<K,V>)c, |
5916 |
> |
s = t.rights; |
5917 |
> |
while (s != null) { |
5918 |
> |
t.result = reducer.applyAsDouble(t.result, s.result); |
5919 |
> |
s = t.rights = s.nextRight; |
5920 |
|
} |
6641 |
– |
else if (t.casPending(c, c - 1)) |
6642 |
– |
break; |
5921 |
|
} |
6644 |
– |
} catch (Throwable ex) { |
6645 |
– |
return tryCompleteComputation(ex); |
6646 |
– |
} |
6647 |
– |
MapReduceMappingsToDoubleTask<K,V> s = rights; |
6648 |
– |
if (s != null && !inForkJoinPool()) { |
6649 |
– |
do { |
6650 |
– |
if (s.tryUnfork()) |
6651 |
– |
s.exec(); |
6652 |
– |
} while ((s = s.nextRight) != null); |
5922 |
|
} |
6654 |
– |
return false; |
5923 |
|
} |
6656 |
– |
public final Double getRawResult() { return result; } |
5924 |
|
} |
5925 |
|
|
5926 |
< |
@SuppressWarnings("serial") static final class MapReduceKeysToLongTask<K,V> |
5926 |
> |
@SuppressWarnings("serial") |
5927 |
> |
static final class MapReduceKeysToLongTask<K,V> |
5928 |
|
extends BulkTask<K,V,Long> { |
5929 |
< |
final ObjectToLong<? super K> transformer; |
5930 |
< |
final LongByLongToLong reducer; |
5929 |
> |
final ToLongFunction<? super K> transformer; |
5930 |
> |
final LongBinaryOperator reducer; |
5931 |
|
final long basis; |
5932 |
|
long result; |
5933 |
|
MapReduceKeysToLongTask<K,V> rights, nextRight; |
5934 |
|
MapReduceKeysToLongTask |
5935 |
< |
(ConcurrentHashMap<K,V> m, BulkTask<K,V,?> p, int b, |
5935 |
> |
(BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t, |
5936 |
|
MapReduceKeysToLongTask<K,V> nextRight, |
5937 |
< |
ObjectToLong<? super K> transformer, |
5937 |
> |
ToLongFunction<? super K> transformer, |
5938 |
|
long basis, |
5939 |
< |
LongByLongToLong reducer) { |
5940 |
< |
super(m, p, b); this.nextRight = nextRight; |
5939 |
> |
LongBinaryOperator reducer) { |
5940 |
> |
super(p, b, i, f, t); this.nextRight = nextRight; |
5941 |
|
this.transformer = transformer; |
5942 |
|
this.basis = basis; this.reducer = reducer; |
5943 |
|
} |
5944 |
< |
@SuppressWarnings("unchecked") public final boolean exec() { |
5945 |
< |
final ObjectToLong<? super K> transformer = |
5946 |
< |
this.transformer; |
5947 |
< |
final LongByLongToLong reducer = this.reducer; |
5948 |
< |
if (transformer == null || reducer == null) |
5949 |
< |
return abortOnNullFunction(); |
5950 |
< |
try { |
5951 |
< |
final long id = this.basis; |
5952 |
< |
for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) { |
5953 |
< |
do {} while (!casPending(c = pending, c+1)); |
5944 |
> |
public final Long getRawResult() { return result; } |
5945 |
> |
public final void compute() { |
5946 |
> |
final ToLongFunction<? super K> transformer; |
5947 |
> |
final LongBinaryOperator reducer; |
5948 |
> |
if ((transformer = this.transformer) != null && |
5949 |
> |
(reducer = this.reducer) != null) { |
5950 |
> |
long r = this.basis; |
5951 |
> |
for (int i = baseIndex, f, h; batch > 0 && |
5952 |
> |
(h = ((f = baseLimit) + i) >>> 1) > i;) { |
5953 |
> |
addToPendingCount(1); |
5954 |
|
(rights = new MapReduceKeysToLongTask<K,V> |
5955 |
< |
(map, this, b >>>= 1, rights, transformer, id, reducer)).fork(); |
5955 |
> |
(this, batch >>>= 1, baseLimit = h, f, tab, |
5956 |
> |
rights, transformer, r, reducer)).fork(); |
5957 |
|
} |
5958 |
< |
long r = id; |
5959 |
< |
while (advance() != null) |
6691 |
< |
r = reducer.apply(r, transformer.apply((K)nextKey)); |
5958 |
> |
for (Node<K,V> p; (p = advance()) != null; ) |
5959 |
> |
r = reducer.applyAsLong(r, transformer.applyAsLong(p.key)); |
5960 |
|
result = r; |
5961 |
< |
for (MapReduceKeysToLongTask<K,V> t = this, s;;) { |
5962 |
< |
int c; BulkTask<K,V,?> par; |
5963 |
< |
if ((c = t.pending) == 0) { |
5964 |
< |
for (s = t.rights; s != null; s = t.rights = s.nextRight) { |
5965 |
< |
t.result = reducer.apply(t.result, s.result); |
5966 |
< |
} |
5967 |
< |
if ((par = t.parent) == null || |
5968 |
< |
!(par instanceof MapReduceKeysToLongTask)) { |
5969 |
< |
t.quietlyComplete(); |
6702 |
< |
break; |
6703 |
< |
} |
6704 |
< |
t = (MapReduceKeysToLongTask<K,V>)par; |
5961 |
> |
CountedCompleter<?> c; |
5962 |
> |
for (c = firstComplete(); c != null; c = c.nextComplete()) { |
5963 |
> |
@SuppressWarnings("unchecked") |
5964 |
> |
MapReduceKeysToLongTask<K,V> |
5965 |
> |
t = (MapReduceKeysToLongTask<K,V>)c, |
5966 |
> |
s = t.rights; |
5967 |
> |
while (s != null) { |
5968 |
> |
t.result = reducer.applyAsLong(t.result, s.result); |
5969 |
> |
s = t.rights = s.nextRight; |
5970 |
|
} |
6706 |
– |
else if (t.casPending(c, c - 1)) |
6707 |
– |
break; |
5971 |
|
} |
6709 |
– |
} catch (Throwable ex) { |
6710 |
– |
return tryCompleteComputation(ex); |
5972 |
|
} |
6712 |
– |
MapReduceKeysToLongTask<K,V> s = rights; |
6713 |
– |
if (s != null && !inForkJoinPool()) { |
6714 |
– |
do { |
6715 |
– |
if (s.tryUnfork()) |
6716 |
– |
s.exec(); |
6717 |
– |
} while ((s = s.nextRight) != null); |
6718 |
– |
} |
6719 |
– |
return false; |
5973 |
|
} |
6721 |
– |
public final Long getRawResult() { return result; } |
5974 |
|
} |
5975 |
|
|
5976 |
< |
@SuppressWarnings("serial") static final class MapReduceValuesToLongTask<K,V> |
5976 |
> |
@SuppressWarnings("serial") |
5977 |
> |
static final class MapReduceValuesToLongTask<K,V> |
5978 |
|
extends BulkTask<K,V,Long> { |
5979 |
< |
final ObjectToLong<? super V> transformer; |
5980 |
< |
final LongByLongToLong reducer; |
5979 |
> |
final ToLongFunction<? super V> transformer; |
5980 |
> |
final LongBinaryOperator reducer; |
5981 |
|
final long basis; |
5982 |
|
long result; |
5983 |
|
MapReduceValuesToLongTask<K,V> rights, nextRight; |
5984 |
|
MapReduceValuesToLongTask |
5985 |
< |
(ConcurrentHashMap<K,V> m, BulkTask<K,V,?> p, int b, |
5985 |
> |
(BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t, |
5986 |
|
MapReduceValuesToLongTask<K,V> nextRight, |
5987 |
< |
ObjectToLong<? super V> transformer, |
5987 |
> |
ToLongFunction<? super V> transformer, |
5988 |
|
long basis, |
5989 |
< |
LongByLongToLong reducer) { |
5990 |
< |
super(m, p, b); this.nextRight = nextRight; |
5989 |
> |
LongBinaryOperator reducer) { |
5990 |
> |
super(p, b, i, f, t); this.nextRight = nextRight; |
5991 |
|
this.transformer = transformer; |
5992 |
|
this.basis = basis; this.reducer = reducer; |
5993 |
|
} |
5994 |
< |
@SuppressWarnings("unchecked") public final boolean exec() { |
5995 |
< |
final ObjectToLong<? super V> transformer = |
5996 |
< |
this.transformer; |
5997 |
< |
final LongByLongToLong reducer = this.reducer; |
5998 |
< |
if (transformer == null || reducer == null) |
5999 |
< |
return abortOnNullFunction(); |
6000 |
< |
try { |
6001 |
< |
final long id = this.basis; |
6002 |
< |
for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) { |
6003 |
< |
do {} while (!casPending(c = pending, c+1)); |
5994 |
> |
public final Long getRawResult() { return result; } |
5995 |
> |
public final void compute() { |
5996 |
> |
final ToLongFunction<? super V> transformer; |
5997 |
> |
final LongBinaryOperator reducer; |
5998 |
> |
if ((transformer = this.transformer) != null && |
5999 |
> |
(reducer = this.reducer) != null) { |
6000 |
> |
long r = this.basis; |
6001 |
> |
for (int i = baseIndex, f, h; batch > 0 && |
6002 |
> |
(h = ((f = baseLimit) + i) >>> 1) > i;) { |
6003 |
> |
addToPendingCount(1); |
6004 |
|
(rights = new MapReduceValuesToLongTask<K,V> |
6005 |
< |
(map, this, b >>>= 1, rights, transformer, id, reducer)).fork(); |
6005 |
> |
(this, batch >>>= 1, baseLimit = h, f, tab, |
6006 |
> |
rights, transformer, r, reducer)).fork(); |
6007 |
|
} |
6008 |
< |
long r = id; |
6009 |
< |
Object v; |
6756 |
< |
while ((v = advance()) != null) |
6757 |
< |
r = reducer.apply(r, transformer.apply((V)v)); |
6008 |
> |
for (Node<K,V> p; (p = advance()) != null; ) |
6009 |
> |
r = reducer.applyAsLong(r, transformer.applyAsLong(p.val)); |
6010 |
|
result = r; |
6011 |
< |
for (MapReduceValuesToLongTask<K,V> t = this, s;;) { |
6012 |
< |
int c; BulkTask<K,V,?> par; |
6013 |
< |
if ((c = t.pending) == 0) { |
6014 |
< |
for (s = t.rights; s != null; s = t.rights = s.nextRight) { |
6015 |
< |
t.result = reducer.apply(t.result, s.result); |
6016 |
< |
} |
6017 |
< |
if ((par = t.parent) == null || |
6018 |
< |
!(par instanceof MapReduceValuesToLongTask)) { |
6019 |
< |
t.quietlyComplete(); |
6768 |
< |
break; |
6769 |
< |
} |
6770 |
< |
t = (MapReduceValuesToLongTask<K,V>)par; |
6011 |
> |
CountedCompleter<?> c; |
6012 |
> |
for (c = firstComplete(); c != null; c = c.nextComplete()) { |
6013 |
> |
@SuppressWarnings("unchecked") |
6014 |
> |
MapReduceValuesToLongTask<K,V> |
6015 |
> |
t = (MapReduceValuesToLongTask<K,V>)c, |
6016 |
> |
s = t.rights; |
6017 |
> |
while (s != null) { |
6018 |
> |
t.result = reducer.applyAsLong(t.result, s.result); |
6019 |
> |
s = t.rights = s.nextRight; |
6020 |
|
} |
6772 |
– |
else if (t.casPending(c, c - 1)) |
6773 |
– |
break; |
6021 |
|
} |
6775 |
– |
} catch (Throwable ex) { |
6776 |
– |
return tryCompleteComputation(ex); |
6777 |
– |
} |
6778 |
– |
MapReduceValuesToLongTask<K,V> s = rights; |
6779 |
– |
if (s != null && !inForkJoinPool()) { |
6780 |
– |
do { |
6781 |
– |
if (s.tryUnfork()) |
6782 |
– |
s.exec(); |
6783 |
– |
} while ((s = s.nextRight) != null); |
6022 |
|
} |
6785 |
– |
return false; |
6023 |
|
} |
6787 |
– |
public final Long getRawResult() { return result; } |
6024 |
|
} |
6025 |
|
|
6026 |
< |
@SuppressWarnings("serial") static final class MapReduceEntriesToLongTask<K,V> |
6026 |
> |
@SuppressWarnings("serial") |
6027 |
> |
static final class MapReduceEntriesToLongTask<K,V> |
6028 |
|
extends BulkTask<K,V,Long> { |
6029 |
< |
final ObjectToLong<Map.Entry<K,V>> transformer; |
6030 |
< |
final LongByLongToLong reducer; |
6029 |
> |
final ToLongFunction<Map.Entry<K,V>> transformer; |
6030 |
> |
final LongBinaryOperator reducer; |
6031 |
|
final long basis; |
6032 |
|
long result; |
6033 |
|
MapReduceEntriesToLongTask<K,V> rights, nextRight; |
6034 |
|
MapReduceEntriesToLongTask |
6035 |
< |
(ConcurrentHashMap<K,V> m, BulkTask<K,V,?> p, int b, |
6035 |
> |
(BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t, |
6036 |
|
MapReduceEntriesToLongTask<K,V> nextRight, |
6037 |
< |
ObjectToLong<Map.Entry<K,V>> transformer, |
6037 |
> |
ToLongFunction<Map.Entry<K,V>> transformer, |
6038 |
|
long basis, |
6039 |
< |
LongByLongToLong reducer) { |
6040 |
< |
super(m, p, b); this.nextRight = nextRight; |
6039 |
> |
LongBinaryOperator reducer) { |
6040 |
> |
super(p, b, i, f, t); this.nextRight = nextRight; |
6041 |
|
this.transformer = transformer; |
6042 |
|
this.basis = basis; this.reducer = reducer; |
6043 |
|
} |
6044 |
< |
@SuppressWarnings("unchecked") public final boolean exec() { |
6045 |
< |
final ObjectToLong<Map.Entry<K,V>> transformer = |
6046 |
< |
this.transformer; |
6047 |
< |
final LongByLongToLong reducer = this.reducer; |
6048 |
< |
if (transformer == null || reducer == null) |
6049 |
< |
return abortOnNullFunction(); |
6050 |
< |
try { |
6051 |
< |
final long id = this.basis; |
6052 |
< |
for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) { |
6053 |
< |
do {} while (!casPending(c = pending, c+1)); |
6044 |
> |
public final Long getRawResult() { return result; } |
6045 |
> |
public final void compute() { |
6046 |
> |
final ToLongFunction<Map.Entry<K,V>> transformer; |
6047 |
> |
final LongBinaryOperator reducer; |
6048 |
> |
if ((transformer = this.transformer) != null && |
6049 |
> |
(reducer = this.reducer) != null) { |
6050 |
> |
long r = this.basis; |
6051 |
> |
for (int i = baseIndex, f, h; batch > 0 && |
6052 |
> |
(h = ((f = baseLimit) + i) >>> 1) > i;) { |
6053 |
> |
addToPendingCount(1); |
6054 |
|
(rights = new MapReduceEntriesToLongTask<K,V> |
6055 |
< |
(map, this, b >>>= 1, rights, transformer, id, reducer)).fork(); |
6055 |
> |
(this, batch >>>= 1, baseLimit = h, f, tab, |
6056 |
> |
rights, transformer, r, reducer)).fork(); |
6057 |
|
} |
6058 |
< |
long r = id; |
6059 |
< |
Object v; |
6822 |
< |
while ((v = advance()) != null) |
6823 |
< |
r = reducer.apply(r, transformer.apply(entryFor((K)nextKey, (V)v))); |
6058 |
> |
for (Node<K,V> p; (p = advance()) != null; ) |
6059 |
> |
r = reducer.applyAsLong(r, transformer.applyAsLong(p)); |
6060 |
|
result = r; |
6061 |
< |
for (MapReduceEntriesToLongTask<K,V> t = this, s;;) { |
6062 |
< |
int c; BulkTask<K,V,?> par; |
6063 |
< |
if ((c = t.pending) == 0) { |
6064 |
< |
for (s = t.rights; s != null; s = t.rights = s.nextRight) { |
6065 |
< |
t.result = reducer.apply(t.result, s.result); |
6066 |
< |
} |
6067 |
< |
if ((par = t.parent) == null || |
6068 |
< |
!(par instanceof MapReduceEntriesToLongTask)) { |
6069 |
< |
t.quietlyComplete(); |
6834 |
< |
break; |
6835 |
< |
} |
6836 |
< |
t = (MapReduceEntriesToLongTask<K,V>)par; |
6061 |
> |
CountedCompleter<?> c; |
6062 |
> |
for (c = firstComplete(); c != null; c = c.nextComplete()) { |
6063 |
> |
@SuppressWarnings("unchecked") |
6064 |
> |
MapReduceEntriesToLongTask<K,V> |
6065 |
> |
t = (MapReduceEntriesToLongTask<K,V>)c, |
6066 |
> |
s = t.rights; |
6067 |
> |
while (s != null) { |
6068 |
> |
t.result = reducer.applyAsLong(t.result, s.result); |
6069 |
> |
s = t.rights = s.nextRight; |
6070 |
|
} |
6838 |
– |
else if (t.casPending(c, c - 1)) |
6839 |
– |
break; |
6071 |
|
} |
6841 |
– |
} catch (Throwable ex) { |
6842 |
– |
return tryCompleteComputation(ex); |
6843 |
– |
} |
6844 |
– |
MapReduceEntriesToLongTask<K,V> s = rights; |
6845 |
– |
if (s != null && !inForkJoinPool()) { |
6846 |
– |
do { |
6847 |
– |
if (s.tryUnfork()) |
6848 |
– |
s.exec(); |
6849 |
– |
} while ((s = s.nextRight) != null); |
6072 |
|
} |
6851 |
– |
return false; |
6073 |
|
} |
6853 |
– |
public final Long getRawResult() { return result; } |
6074 |
|
} |
6075 |
|
|
6076 |
< |
@SuppressWarnings("serial") static final class MapReduceMappingsToLongTask<K,V> |
6076 |
> |
@SuppressWarnings("serial") |
6077 |
> |
static final class MapReduceMappingsToLongTask<K,V> |
6078 |
|
extends BulkTask<K,V,Long> { |
6079 |
< |
final ObjectByObjectToLong<? super K, ? super V> transformer; |
6080 |
< |
final LongByLongToLong reducer; |
6079 |
> |
final ToLongBiFunction<? super K, ? super V> transformer; |
6080 |
> |
final LongBinaryOperator reducer; |
6081 |
|
final long basis; |
6082 |
|
long result; |
6083 |
|
MapReduceMappingsToLongTask<K,V> rights, nextRight; |
6084 |
|
MapReduceMappingsToLongTask |
6085 |
< |
(ConcurrentHashMap<K,V> m, BulkTask<K,V,?> p, int b, |
6085 |
> |
(BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t, |
6086 |
|
MapReduceMappingsToLongTask<K,V> nextRight, |
6087 |
< |
ObjectByObjectToLong<? super K, ? super V> transformer, |
6087 |
> |
ToLongBiFunction<? super K, ? super V> transformer, |
6088 |
|
long basis, |
6089 |
< |
LongByLongToLong reducer) { |
6090 |
< |
super(m, p, b); this.nextRight = nextRight; |
6089 |
> |
LongBinaryOperator reducer) { |
6090 |
> |
super(p, b, i, f, t); this.nextRight = nextRight; |
6091 |
|
this.transformer = transformer; |
6092 |
|
this.basis = basis; this.reducer = reducer; |
6093 |
|
} |
6094 |
< |
@SuppressWarnings("unchecked") public final boolean exec() { |
6095 |
< |
final ObjectByObjectToLong<? super K, ? super V> transformer = |
6096 |
< |
this.transformer; |
6097 |
< |
final LongByLongToLong reducer = this.reducer; |
6098 |
< |
if (transformer == null || reducer == null) |
6099 |
< |
return abortOnNullFunction(); |
6100 |
< |
try { |
6101 |
< |
final long id = this.basis; |
6102 |
< |
for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) { |
6103 |
< |
do {} while (!casPending(c = pending, c+1)); |
6094 |
> |
public final Long getRawResult() { return result; } |
6095 |
> |
public final void compute() { |
6096 |
> |
final ToLongBiFunction<? super K, ? super V> transformer; |
6097 |
> |
final LongBinaryOperator reducer; |
6098 |
> |
if ((transformer = this.transformer) != null && |
6099 |
> |
(reducer = this.reducer) != null) { |
6100 |
> |
long r = this.basis; |
6101 |
> |
for (int i = baseIndex, f, h; batch > 0 && |
6102 |
> |
(h = ((f = baseLimit) + i) >>> 1) > i;) { |
6103 |
> |
addToPendingCount(1); |
6104 |
|
(rights = new MapReduceMappingsToLongTask<K,V> |
6105 |
< |
(map, this, b >>>= 1, rights, transformer, id, reducer)).fork(); |
6105 |
> |
(this, batch >>>= 1, baseLimit = h, f, tab, |
6106 |
> |
rights, transformer, r, reducer)).fork(); |
6107 |
|
} |
6108 |
< |
long r = id; |
6109 |
< |
Object v; |
6888 |
< |
while ((v = advance()) != null) |
6889 |
< |
r = reducer.apply(r, transformer.apply((K)nextKey, (V)v)); |
6108 |
> |
for (Node<K,V> p; (p = advance()) != null; ) |
6109 |
> |
r = reducer.applyAsLong(r, transformer.applyAsLong(p.key, p.val)); |
6110 |
|
result = r; |
6111 |
< |
for (MapReduceMappingsToLongTask<K,V> t = this, s;;) { |
6112 |
< |
int c; BulkTask<K,V,?> par; |
6113 |
< |
if ((c = t.pending) == 0) { |
6114 |
< |
for (s = t.rights; s != null; s = t.rights = s.nextRight) { |
6115 |
< |
t.result = reducer.apply(t.result, s.result); |
6116 |
< |
} |
6117 |
< |
if ((par = t.parent) == null || |
6118 |
< |
!(par instanceof MapReduceMappingsToLongTask)) { |
6119 |
< |
t.quietlyComplete(); |
6900 |
< |
break; |
6901 |
< |
} |
6902 |
< |
t = (MapReduceMappingsToLongTask<K,V>)par; |
6111 |
> |
CountedCompleter<?> c; |
6112 |
> |
for (c = firstComplete(); c != null; c = c.nextComplete()) { |
6113 |
> |
@SuppressWarnings("unchecked") |
6114 |
> |
MapReduceMappingsToLongTask<K,V> |
6115 |
> |
t = (MapReduceMappingsToLongTask<K,V>)c, |
6116 |
> |
s = t.rights; |
6117 |
> |
while (s != null) { |
6118 |
> |
t.result = reducer.applyAsLong(t.result, s.result); |
6119 |
> |
s = t.rights = s.nextRight; |
6120 |
|
} |
6904 |
– |
else if (t.casPending(c, c - 1)) |
6905 |
– |
break; |
6121 |
|
} |
6907 |
– |
} catch (Throwable ex) { |
6908 |
– |
return tryCompleteComputation(ex); |
6122 |
|
} |
6910 |
– |
MapReduceMappingsToLongTask<K,V> s = rights; |
6911 |
– |
if (s != null && !inForkJoinPool()) { |
6912 |
– |
do { |
6913 |
– |
if (s.tryUnfork()) |
6914 |
– |
s.exec(); |
6915 |
– |
} while ((s = s.nextRight) != null); |
6916 |
– |
} |
6917 |
– |
return false; |
6123 |
|
} |
6919 |
– |
public final Long getRawResult() { return result; } |
6124 |
|
} |
6125 |
|
|
6126 |
< |
@SuppressWarnings("serial") static final class MapReduceKeysToIntTask<K,V> |
6126 |
> |
@SuppressWarnings("serial") |
6127 |
> |
static final class MapReduceKeysToIntTask<K,V> |
6128 |
|
extends BulkTask<K,V,Integer> { |
6129 |
< |
final ObjectToInt<? super K> transformer; |
6130 |
< |
final IntByIntToInt reducer; |
6129 |
> |
final ToIntFunction<? super K> transformer; |
6130 |
> |
final IntBinaryOperator reducer; |
6131 |
|
final int basis; |
6132 |
|
int result; |
6133 |
|
MapReduceKeysToIntTask<K,V> rights, nextRight; |
6134 |
|
MapReduceKeysToIntTask |
6135 |
< |
(ConcurrentHashMap<K,V> m, BulkTask<K,V,?> p, int b, |
6135 |
> |
(BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t, |
6136 |
|
MapReduceKeysToIntTask<K,V> nextRight, |
6137 |
< |
ObjectToInt<? super K> transformer, |
6137 |
> |
ToIntFunction<? super K> transformer, |
6138 |
|
int basis, |
6139 |
< |
IntByIntToInt reducer) { |
6140 |
< |
super(m, p, b); this.nextRight = nextRight; |
6139 |
> |
IntBinaryOperator reducer) { |
6140 |
> |
super(p, b, i, f, t); this.nextRight = nextRight; |
6141 |
|
this.transformer = transformer; |
6142 |
|
this.basis = basis; this.reducer = reducer; |
6143 |
|
} |
6144 |
< |
@SuppressWarnings("unchecked") public final boolean exec() { |
6145 |
< |
final ObjectToInt<? super K> transformer = |
6146 |
< |
this.transformer; |
6147 |
< |
final IntByIntToInt reducer = this.reducer; |
6148 |
< |
if (transformer == null || reducer == null) |
6149 |
< |
return abortOnNullFunction(); |
6150 |
< |
try { |
6151 |
< |
final int id = this.basis; |
6152 |
< |
for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) { |
6153 |
< |
do {} while (!casPending(c = pending, c+1)); |
6144 |
> |
public final Integer getRawResult() { return result; } |
6145 |
> |
public final void compute() { |
6146 |
> |
final ToIntFunction<? super K> transformer; |
6147 |
> |
final IntBinaryOperator reducer; |
6148 |
> |
if ((transformer = this.transformer) != null && |
6149 |
> |
(reducer = this.reducer) != null) { |
6150 |
> |
int r = this.basis; |
6151 |
> |
for (int i = baseIndex, f, h; batch > 0 && |
6152 |
> |
(h = ((f = baseLimit) + i) >>> 1) > i;) { |
6153 |
> |
addToPendingCount(1); |
6154 |
|
(rights = new MapReduceKeysToIntTask<K,V> |
6155 |
< |
(map, this, b >>>= 1, rights, transformer, id, reducer)).fork(); |
6155 |
> |
(this, batch >>>= 1, baseLimit = h, f, tab, |
6156 |
> |
rights, transformer, r, reducer)).fork(); |
6157 |
|
} |
6158 |
< |
int r = id; |
6159 |
< |
while (advance() != null) |
6954 |
< |
r = reducer.apply(r, transformer.apply((K)nextKey)); |
6158 |
> |
for (Node<K,V> p; (p = advance()) != null; ) |
6159 |
> |
r = reducer.applyAsInt(r, transformer.applyAsInt(p.key)); |
6160 |
|
result = r; |
6161 |
< |
for (MapReduceKeysToIntTask<K,V> t = this, s;;) { |
6162 |
< |
int c; BulkTask<K,V,?> par; |
6163 |
< |
if ((c = t.pending) == 0) { |
6164 |
< |
for (s = t.rights; s != null; s = t.rights = s.nextRight) { |
6165 |
< |
t.result = reducer.apply(t.result, s.result); |
6166 |
< |
} |
6167 |
< |
if ((par = t.parent) == null || |
6168 |
< |
!(par instanceof MapReduceKeysToIntTask)) { |
6169 |
< |
t.quietlyComplete(); |
6965 |
< |
break; |
6966 |
< |
} |
6967 |
< |
t = (MapReduceKeysToIntTask<K,V>)par; |
6161 |
> |
CountedCompleter<?> c; |
6162 |
> |
for (c = firstComplete(); c != null; c = c.nextComplete()) { |
6163 |
> |
@SuppressWarnings("unchecked") |
6164 |
> |
MapReduceKeysToIntTask<K,V> |
6165 |
> |
t = (MapReduceKeysToIntTask<K,V>)c, |
6166 |
> |
s = t.rights; |
6167 |
> |
while (s != null) { |
6168 |
> |
t.result = reducer.applyAsInt(t.result, s.result); |
6169 |
> |
s = t.rights = s.nextRight; |
6170 |
|
} |
6969 |
– |
else if (t.casPending(c, c - 1)) |
6970 |
– |
break; |
6171 |
|
} |
6972 |
– |
} catch (Throwable ex) { |
6973 |
– |
return tryCompleteComputation(ex); |
6974 |
– |
} |
6975 |
– |
MapReduceKeysToIntTask<K,V> s = rights; |
6976 |
– |
if (s != null && !inForkJoinPool()) { |
6977 |
– |
do { |
6978 |
– |
if (s.tryUnfork()) |
6979 |
– |
s.exec(); |
6980 |
– |
} while ((s = s.nextRight) != null); |
6172 |
|
} |
6982 |
– |
return false; |
6173 |
|
} |
6984 |
– |
public final Integer getRawResult() { return result; } |
6174 |
|
} |
6175 |
|
|
6176 |
< |
@SuppressWarnings("serial") static final class MapReduceValuesToIntTask<K,V> |
6176 |
> |
@SuppressWarnings("serial") |
6177 |
> |
static final class MapReduceValuesToIntTask<K,V> |
6178 |
|
extends BulkTask<K,V,Integer> { |
6179 |
< |
final ObjectToInt<? super V> transformer; |
6180 |
< |
final IntByIntToInt reducer; |
6179 |
> |
final ToIntFunction<? super V> transformer; |
6180 |
> |
final IntBinaryOperator reducer; |
6181 |
|
final int basis; |
6182 |
|
int result; |
6183 |
|
MapReduceValuesToIntTask<K,V> rights, nextRight; |
6184 |
|
MapReduceValuesToIntTask |
6185 |
< |
(ConcurrentHashMap<K,V> m, BulkTask<K,V,?> p, int b, |
6185 |
> |
(BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t, |
6186 |
|
MapReduceValuesToIntTask<K,V> nextRight, |
6187 |
< |
ObjectToInt<? super V> transformer, |
6187 |
> |
ToIntFunction<? super V> transformer, |
6188 |
|
int basis, |
6189 |
< |
IntByIntToInt reducer) { |
6190 |
< |
super(m, p, b); this.nextRight = nextRight; |
6189 |
> |
IntBinaryOperator reducer) { |
6190 |
> |
super(p, b, i, f, t); this.nextRight = nextRight; |
6191 |
|
this.transformer = transformer; |
6192 |
|
this.basis = basis; this.reducer = reducer; |
6193 |
|
} |
6194 |
< |
@SuppressWarnings("unchecked") public final boolean exec() { |
6195 |
< |
final ObjectToInt<? super V> transformer = |
6196 |
< |
this.transformer; |
6197 |
< |
final IntByIntToInt reducer = this.reducer; |
6198 |
< |
if (transformer == null || reducer == null) |
6199 |
< |
return abortOnNullFunction(); |
6200 |
< |
try { |
6201 |
< |
final int id = this.basis; |
6202 |
< |
for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) { |
6203 |
< |
do {} while (!casPending(c = pending, c+1)); |
6194 |
> |
public final Integer getRawResult() { return result; } |
6195 |
> |
public final void compute() { |
6196 |
> |
final ToIntFunction<? super V> transformer; |
6197 |
> |
final IntBinaryOperator reducer; |
6198 |
> |
if ((transformer = this.transformer) != null && |
6199 |
> |
(reducer = this.reducer) != null) { |
6200 |
> |
int r = this.basis; |
6201 |
> |
for (int i = baseIndex, f, h; batch > 0 && |
6202 |
> |
(h = ((f = baseLimit) + i) >>> 1) > i;) { |
6203 |
> |
addToPendingCount(1); |
6204 |
|
(rights = new MapReduceValuesToIntTask<K,V> |
6205 |
< |
(map, this, b >>>= 1, rights, transformer, id, reducer)).fork(); |
6205 |
> |
(this, batch >>>= 1, baseLimit = h, f, tab, |
6206 |
> |
rights, transformer, r, reducer)).fork(); |
6207 |
|
} |
6208 |
< |
int r = id; |
6209 |
< |
Object v; |
7019 |
< |
while ((v = advance()) != null) |
7020 |
< |
r = reducer.apply(r, transformer.apply((V)v)); |
6208 |
> |
for (Node<K,V> p; (p = advance()) != null; ) |
6209 |
> |
r = reducer.applyAsInt(r, transformer.applyAsInt(p.val)); |
6210 |
|
result = r; |
6211 |
< |
for (MapReduceValuesToIntTask<K,V> t = this, s;;) { |
6212 |
< |
int c; BulkTask<K,V,?> par; |
6213 |
< |
if ((c = t.pending) == 0) { |
6214 |
< |
for (s = t.rights; s != null; s = t.rights = s.nextRight) { |
6215 |
< |
t.result = reducer.apply(t.result, s.result); |
6216 |
< |
} |
6217 |
< |
if ((par = t.parent) == null || |
6218 |
< |
!(par instanceof MapReduceValuesToIntTask)) { |
6219 |
< |
t.quietlyComplete(); |
7031 |
< |
break; |
7032 |
< |
} |
7033 |
< |
t = (MapReduceValuesToIntTask<K,V>)par; |
6211 |
> |
CountedCompleter<?> c; |
6212 |
> |
for (c = firstComplete(); c != null; c = c.nextComplete()) { |
6213 |
> |
@SuppressWarnings("unchecked") |
6214 |
> |
MapReduceValuesToIntTask<K,V> |
6215 |
> |
t = (MapReduceValuesToIntTask<K,V>)c, |
6216 |
> |
s = t.rights; |
6217 |
> |
while (s != null) { |
6218 |
> |
t.result = reducer.applyAsInt(t.result, s.result); |
6219 |
> |
s = t.rights = s.nextRight; |
6220 |
|
} |
7035 |
– |
else if (t.casPending(c, c - 1)) |
7036 |
– |
break; |
6221 |
|
} |
7038 |
– |
} catch (Throwable ex) { |
7039 |
– |
return tryCompleteComputation(ex); |
6222 |
|
} |
7041 |
– |
MapReduceValuesToIntTask<K,V> s = rights; |
7042 |
– |
if (s != null && !inForkJoinPool()) { |
7043 |
– |
do { |
7044 |
– |
if (s.tryUnfork()) |
7045 |
– |
s.exec(); |
7046 |
– |
} while ((s = s.nextRight) != null); |
7047 |
– |
} |
7048 |
– |
return false; |
6223 |
|
} |
7050 |
– |
public final Integer getRawResult() { return result; } |
6224 |
|
} |
6225 |
|
|
6226 |
< |
@SuppressWarnings("serial") static final class MapReduceEntriesToIntTask<K,V> |
6226 |
> |
@SuppressWarnings("serial") |
6227 |
> |
static final class MapReduceEntriesToIntTask<K,V> |
6228 |
|
extends BulkTask<K,V,Integer> { |
6229 |
< |
final ObjectToInt<Map.Entry<K,V>> transformer; |
6230 |
< |
final IntByIntToInt reducer; |
6229 |
> |
final ToIntFunction<Map.Entry<K,V>> transformer; |
6230 |
> |
final IntBinaryOperator reducer; |
6231 |
|
final int basis; |
6232 |
|
int result; |
6233 |
|
MapReduceEntriesToIntTask<K,V> rights, nextRight; |
6234 |
|
MapReduceEntriesToIntTask |
6235 |
< |
(ConcurrentHashMap<K,V> m, BulkTask<K,V,?> p, int b, |
6235 |
> |
(BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t, |
6236 |
|
MapReduceEntriesToIntTask<K,V> nextRight, |
6237 |
< |
ObjectToInt<Map.Entry<K,V>> transformer, |
6237 |
> |
ToIntFunction<Map.Entry<K,V>> transformer, |
6238 |
|
int basis, |
6239 |
< |
IntByIntToInt reducer) { |
6240 |
< |
super(m, p, b); this.nextRight = nextRight; |
6239 |
> |
IntBinaryOperator reducer) { |
6240 |
> |
super(p, b, i, f, t); this.nextRight = nextRight; |
6241 |
|
this.transformer = transformer; |
6242 |
|
this.basis = basis; this.reducer = reducer; |
6243 |
|
} |
6244 |
< |
@SuppressWarnings("unchecked") public final boolean exec() { |
6245 |
< |
final ObjectToInt<Map.Entry<K,V>> transformer = |
6246 |
< |
this.transformer; |
6247 |
< |
final IntByIntToInt reducer = this.reducer; |
6248 |
< |
if (transformer == null || reducer == null) |
6249 |
< |
return abortOnNullFunction(); |
6250 |
< |
try { |
6251 |
< |
final int id = this.basis; |
6252 |
< |
for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) { |
6253 |
< |
do {} while (!casPending(c = pending, c+1)); |
6244 |
> |
public final Integer getRawResult() { return result; } |
6245 |
> |
public final void compute() { |
6246 |
> |
final ToIntFunction<Map.Entry<K,V>> transformer; |
6247 |
> |
final IntBinaryOperator reducer; |
6248 |
> |
if ((transformer = this.transformer) != null && |
6249 |
> |
(reducer = this.reducer) != null) { |
6250 |
> |
int r = this.basis; |
6251 |
> |
for (int i = baseIndex, f, h; batch > 0 && |
6252 |
> |
(h = ((f = baseLimit) + i) >>> 1) > i;) { |
6253 |
> |
addToPendingCount(1); |
6254 |
|
(rights = new MapReduceEntriesToIntTask<K,V> |
6255 |
< |
(map, this, b >>>= 1, rights, transformer, id, reducer)).fork(); |
6255 |
> |
(this, batch >>>= 1, baseLimit = h, f, tab, |
6256 |
> |
rights, transformer, r, reducer)).fork(); |
6257 |
|
} |
6258 |
< |
int r = id; |
6259 |
< |
Object v; |
7085 |
< |
while ((v = advance()) != null) |
7086 |
< |
r = reducer.apply(r, transformer.apply(entryFor((K)nextKey, (V)v))); |
6258 |
> |
for (Node<K,V> p; (p = advance()) != null; ) |
6259 |
> |
r = reducer.applyAsInt(r, transformer.applyAsInt(p)); |
6260 |
|
result = r; |
6261 |
< |
for (MapReduceEntriesToIntTask<K,V> t = this, s;;) { |
6262 |
< |
int c; BulkTask<K,V,?> par; |
6263 |
< |
if ((c = t.pending) == 0) { |
6264 |
< |
for (s = t.rights; s != null; s = t.rights = s.nextRight) { |
6265 |
< |
t.result = reducer.apply(t.result, s.result); |
6266 |
< |
} |
6267 |
< |
if ((par = t.parent) == null || |
6268 |
< |
!(par instanceof MapReduceEntriesToIntTask)) { |
6269 |
< |
t.quietlyComplete(); |
7097 |
< |
break; |
7098 |
< |
} |
7099 |
< |
t = (MapReduceEntriesToIntTask<K,V>)par; |
6261 |
> |
CountedCompleter<?> c; |
6262 |
> |
for (c = firstComplete(); c != null; c = c.nextComplete()) { |
6263 |
> |
@SuppressWarnings("unchecked") |
6264 |
> |
MapReduceEntriesToIntTask<K,V> |
6265 |
> |
t = (MapReduceEntriesToIntTask<K,V>)c, |
6266 |
> |
s = t.rights; |
6267 |
> |
while (s != null) { |
6268 |
> |
t.result = reducer.applyAsInt(t.result, s.result); |
6269 |
> |
s = t.rights = s.nextRight; |
6270 |
|
} |
7101 |
– |
else if (t.casPending(c, c - 1)) |
7102 |
– |
break; |
6271 |
|
} |
7104 |
– |
} catch (Throwable ex) { |
7105 |
– |
return tryCompleteComputation(ex); |
7106 |
– |
} |
7107 |
– |
MapReduceEntriesToIntTask<K,V> s = rights; |
7108 |
– |
if (s != null && !inForkJoinPool()) { |
7109 |
– |
do { |
7110 |
– |
if (s.tryUnfork()) |
7111 |
– |
s.exec(); |
7112 |
– |
} while ((s = s.nextRight) != null); |
6272 |
|
} |
7114 |
– |
return false; |
6273 |
|
} |
7116 |
– |
public final Integer getRawResult() { return result; } |
6274 |
|
} |
6275 |
|
|
6276 |
< |
@SuppressWarnings("serial") static final class MapReduceMappingsToIntTask<K,V> |
6276 |
> |
@SuppressWarnings("serial") |
6277 |
> |
static final class MapReduceMappingsToIntTask<K,V> |
6278 |
|
extends BulkTask<K,V,Integer> { |
6279 |
< |
final ObjectByObjectToInt<? super K, ? super V> transformer; |
6280 |
< |
final IntByIntToInt reducer; |
6279 |
> |
final ToIntBiFunction<? super K, ? super V> transformer; |
6280 |
> |
final IntBinaryOperator reducer; |
6281 |
|
final int basis; |
6282 |
|
int result; |
6283 |
|
MapReduceMappingsToIntTask<K,V> rights, nextRight; |
6284 |
|
MapReduceMappingsToIntTask |
6285 |
< |
(ConcurrentHashMap<K,V> m, BulkTask<K,V,?> p, int b, |
6286 |
< |
MapReduceMappingsToIntTask<K,V> rights, |
6287 |
< |
ObjectByObjectToInt<? super K, ? super V> transformer, |
6285 |
> |
(BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t, |
6286 |
> |
MapReduceMappingsToIntTask<K,V> nextRight, |
6287 |
> |
ToIntBiFunction<? super K, ? super V> transformer, |
6288 |
|
int basis, |
6289 |
< |
IntByIntToInt reducer) { |
6290 |
< |
super(m, p, b); this.nextRight = nextRight; |
6289 |
> |
IntBinaryOperator reducer) { |
6290 |
> |
super(p, b, i, f, t); this.nextRight = nextRight; |
6291 |
|
this.transformer = transformer; |
6292 |
|
this.basis = basis; this.reducer = reducer; |
6293 |
|
} |
6294 |
< |
@SuppressWarnings("unchecked") public final boolean exec() { |
6295 |
< |
final ObjectByObjectToInt<? super K, ? super V> transformer = |
6296 |
< |
this.transformer; |
6297 |
< |
final IntByIntToInt reducer = this.reducer; |
6298 |
< |
if (transformer == null || reducer == null) |
6299 |
< |
return abortOnNullFunction(); |
6300 |
< |
try { |
6301 |
< |
final int id = this.basis; |
6302 |
< |
for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) { |
6303 |
< |
do {} while (!casPending(c = pending, c+1)); |
6294 |
> |
public final Integer getRawResult() { return result; } |
6295 |
> |
public final void compute() { |
6296 |
> |
final ToIntBiFunction<? super K, ? super V> transformer; |
6297 |
> |
final IntBinaryOperator reducer; |
6298 |
> |
if ((transformer = this.transformer) != null && |
6299 |
> |
(reducer = this.reducer) != null) { |
6300 |
> |
int r = this.basis; |
6301 |
> |
for (int i = baseIndex, f, h; batch > 0 && |
6302 |
> |
(h = ((f = baseLimit) + i) >>> 1) > i;) { |
6303 |
> |
addToPendingCount(1); |
6304 |
|
(rights = new MapReduceMappingsToIntTask<K,V> |
6305 |
< |
(map, this, b >>>= 1, rights, transformer, id, reducer)).fork(); |
6305 |
> |
(this, batch >>>= 1, baseLimit = h, f, tab, |
6306 |
> |
rights, transformer, r, reducer)).fork(); |
6307 |
|
} |
6308 |
< |
int r = id; |
6309 |
< |
Object v; |
7151 |
< |
while ((v = advance()) != null) |
7152 |
< |
r = reducer.apply(r, transformer.apply((K)nextKey, (V)v)); |
6308 |
> |
for (Node<K,V> p; (p = advance()) != null; ) |
6309 |
> |
r = reducer.applyAsInt(r, transformer.applyAsInt(p.key, p.val)); |
6310 |
|
result = r; |
6311 |
< |
for (MapReduceMappingsToIntTask<K,V> t = this, s;;) { |
6312 |
< |
int c; BulkTask<K,V,?> par; |
6313 |
< |
if ((c = t.pending) == 0) { |
6314 |
< |
for (s = t.rights; s != null; s = t.rights = s.nextRight) { |
6315 |
< |
t.result = reducer.apply(t.result, s.result); |
6316 |
< |
} |
6317 |
< |
if ((par = t.parent) == null || |
6318 |
< |
!(par instanceof MapReduceMappingsToIntTask)) { |
6319 |
< |
t.quietlyComplete(); |
7163 |
< |
break; |
7164 |
< |
} |
7165 |
< |
t = (MapReduceMappingsToIntTask<K,V>)par; |
6311 |
> |
CountedCompleter<?> c; |
6312 |
> |
for (c = firstComplete(); c != null; c = c.nextComplete()) { |
6313 |
> |
@SuppressWarnings("unchecked") |
6314 |
> |
MapReduceMappingsToIntTask<K,V> |
6315 |
> |
t = (MapReduceMappingsToIntTask<K,V>)c, |
6316 |
> |
s = t.rights; |
6317 |
> |
while (s != null) { |
6318 |
> |
t.result = reducer.applyAsInt(t.result, s.result); |
6319 |
> |
s = t.rights = s.nextRight; |
6320 |
|
} |
7167 |
– |
else if (t.casPending(c, c - 1)) |
7168 |
– |
break; |
6321 |
|
} |
7170 |
– |
} catch (Throwable ex) { |
7171 |
– |
return tryCompleteComputation(ex); |
7172 |
– |
} |
7173 |
– |
MapReduceMappingsToIntTask<K,V> s = rights; |
7174 |
– |
if (s != null && !inForkJoinPool()) { |
7175 |
– |
do { |
7176 |
– |
if (s.tryUnfork()) |
7177 |
– |
s.exec(); |
7178 |
– |
} while ((s = s.nextRight) != null); |
6322 |
|
} |
7180 |
– |
return false; |
6323 |
|
} |
7182 |
– |
public final Integer getRawResult() { return result; } |
6324 |
|
} |
6325 |
|
|
6326 |
|
// Unsafe mechanics |
6327 |
< |
private static final sun.misc.Unsafe UNSAFE; |
6328 |
< |
private static final long counterOffset; |
6329 |
< |
private static final long sizeCtlOffset; |
6330 |
< |
private static final long ABASE; |
6327 |
> |
private static final Unsafe U = Unsafe.getUnsafe(); |
6328 |
> |
private static final long SIZECTL; |
6329 |
> |
private static final long TRANSFERINDEX; |
6330 |
> |
private static final long BASECOUNT; |
6331 |
> |
private static final long CELLSBUSY; |
6332 |
> |
private static final long CELLVALUE; |
6333 |
> |
private static final int ABASE; |
6334 |
|
private static final int ASHIFT; |
6335 |
|
|
6336 |
|
static { |
7193 |
– |
int ss; |
6337 |
|
try { |
6338 |
< |
UNSAFE = sun.misc.Unsafe.getUnsafe(); |
6339 |
< |
Class<?> k = ConcurrentHashMap.class; |
6340 |
< |
counterOffset = UNSAFE.objectFieldOffset |
6341 |
< |
(k.getDeclaredField("counter")); |
6342 |
< |
sizeCtlOffset = UNSAFE.objectFieldOffset |
6343 |
< |
(k.getDeclaredField("sizeCtl")); |
6344 |
< |
Class<?> sc = Node[].class; |
6345 |
< |
ABASE = UNSAFE.arrayBaseOffset(sc); |
6346 |
< |
ss = UNSAFE.arrayIndexScale(sc); |
6347 |
< |
} catch (Exception e) { |
6348 |
< |
throw new Error(e); |
6349 |
< |
} |
6350 |
< |
if ((ss & (ss-1)) != 0) |
6351 |
< |
throw new Error("data type scale not a power of two"); |
6352 |
< |
ASHIFT = 31 - Integer.numberOfLeadingZeros(ss); |
6338 |
> |
SIZECTL = U.objectFieldOffset |
6339 |
> |
(ConcurrentHashMap.class.getDeclaredField("sizeCtl")); |
6340 |
> |
TRANSFERINDEX = U.objectFieldOffset |
6341 |
> |
(ConcurrentHashMap.class.getDeclaredField("transferIndex")); |
6342 |
> |
BASECOUNT = U.objectFieldOffset |
6343 |
> |
(ConcurrentHashMap.class.getDeclaredField("baseCount")); |
6344 |
> |
CELLSBUSY = U.objectFieldOffset |
6345 |
> |
(ConcurrentHashMap.class.getDeclaredField("cellsBusy")); |
6346 |
> |
|
6347 |
> |
CELLVALUE = U.objectFieldOffset |
6348 |
> |
(CounterCell.class.getDeclaredField("value")); |
6349 |
> |
|
6350 |
> |
ABASE = U.arrayBaseOffset(Node[].class); |
6351 |
> |
int scale = U.arrayIndexScale(Node[].class); |
6352 |
> |
if ((scale & (scale - 1)) != 0) |
6353 |
> |
throw new ExceptionInInitializerError("array index scale not a power of two"); |
6354 |
> |
ASHIFT = 31 - Integer.numberOfLeadingZeros(scale); |
6355 |
> |
} catch (ReflectiveOperationException e) { |
6356 |
> |
throw new ExceptionInInitializerError(e); |
6357 |
> |
} |
6358 |
> |
|
6359 |
> |
// Reduce the risk of rare disastrous classloading in first call to |
6360 |
> |
// LockSupport.park: https://bugs.openjdk.java.net/browse/JDK-8074773 |
6361 |
> |
Class<?> ensureLoaded = LockSupport.class; |
6362 |
> |
|
6363 |
> |
// Eager class load observed to help JIT during startup |
6364 |
> |
ensureLoaded = ReservationNode.class; |
6365 |
|
} |
6366 |
|
} |