6 |
|
|
7 |
|
package jsr166e; |
8 |
|
|
9 |
< |
import java.util.Comparator; |
10 |
< |
import java.util.Arrays; |
11 |
< |
import java.util.Map; |
12 |
< |
import java.util.Set; |
13 |
< |
import java.util.Collection; |
14 |
< |
import java.util.AbstractMap; |
15 |
< |
import java.util.AbstractSet; |
16 |
< |
import java.util.AbstractCollection; |
17 |
< |
import java.util.Hashtable; |
18 |
< |
import java.util.HashMap; |
19 |
< |
import java.util.Iterator; |
20 |
< |
import java.util.Enumeration; |
21 |
< |
import java.util.ConcurrentModificationException; |
22 |
< |
import java.util.NoSuchElementException; |
23 |
< |
import java.util.concurrent.ConcurrentMap; |
24 |
< |
import java.util.concurrent.ThreadLocalRandom; |
25 |
< |
import java.util.concurrent.locks.LockSupport; |
26 |
< |
import java.util.concurrent.locks.AbstractQueuedSynchronizer; |
27 |
< |
import java.util.concurrent.atomic.AtomicReference; |
9 |
> |
import jsr166e.ForkJoinPool; |
10 |
|
|
11 |
+ |
import java.io.ObjectStreamField; |
12 |
|
import java.io.Serializable; |
13 |
< |
|
14 |
< |
import java.util.Comparator; |
13 |
> |
import java.lang.reflect.ParameterizedType; |
14 |
> |
import java.lang.reflect.Type; |
15 |
> |
import java.util.AbstractMap; |
16 |
|
import java.util.Arrays; |
33 |
– |
import java.util.Map; |
34 |
– |
import java.util.Set; |
17 |
|
import java.util.Collection; |
18 |
< |
import java.util.AbstractMap; |
19 |
< |
import java.util.AbstractSet; |
20 |
< |
import java.util.AbstractCollection; |
39 |
< |
import java.util.Hashtable; |
18 |
> |
import java.util.Comparator; |
19 |
> |
import java.util.ConcurrentModificationException; |
20 |
> |
import java.util.Enumeration; |
21 |
|
import java.util.HashMap; |
22 |
+ |
import java.util.Hashtable; |
23 |
|
import java.util.Iterator; |
24 |
< |
import java.util.Enumeration; |
43 |
< |
import java.util.ConcurrentModificationException; |
24 |
> |
import java.util.Map; |
25 |
|
import java.util.NoSuchElementException; |
26 |
+ |
import java.util.Set; |
27 |
|
import java.util.concurrent.ConcurrentMap; |
46 |
– |
import java.util.concurrent.ThreadLocalRandom; |
47 |
– |
import java.util.concurrent.locks.LockSupport; |
48 |
– |
import java.util.concurrent.locks.AbstractQueuedSynchronizer; |
28 |
|
import java.util.concurrent.atomic.AtomicReference; |
29 |
< |
|
30 |
< |
import java.io.Serializable; |
29 |
> |
import java.util.concurrent.atomic.AtomicInteger; |
30 |
> |
import java.util.concurrent.locks.LockSupport; |
31 |
> |
import java.util.concurrent.locks.ReentrantLock; |
32 |
|
|
33 |
|
/** |
34 |
|
* A hash table supporting full concurrency of retrievals and |
82 |
|
* expected {@code concurrencyLevel} as an additional hint for |
83 |
|
* internal sizing. Note that using many keys with exactly the same |
84 |
|
* {@code hashCode()} is a sure way to slow down performance of any |
85 |
< |
* hash table. |
85 |
> |
* hash table. To ameliorate impact, when keys are {@link Comparable}, |
86 |
> |
* this class may use comparison order among keys to help break ties. |
87 |
|
* |
88 |
|
* <p>A {@link Set} projection of a ConcurrentHashMapV8 may be created |
89 |
|
* (using {@link #newKeySet()} or {@link #newKeySet(int)}), or viewed |
91 |
|
* mapped values are (perhaps transiently) not used or all take the |
92 |
|
* same mapping value. |
93 |
|
* |
113 |
– |
* <p>A ConcurrentHashMapV8 can be used as scalable frequency map (a |
114 |
– |
* form of histogram or multiset) by using {@link LongAdder} values |
115 |
– |
* and initializing via {@link #computeIfAbsent}. For example, to add |
116 |
– |
* a count to a {@code ConcurrentHashMapV8<String,LongAdder> freqs}, you |
117 |
– |
* can use {@code freqs.computeIfAbsent(k -> new |
118 |
– |
* LongAdder()).increment();} |
119 |
– |
* |
94 |
|
* <p>This class and its views and iterators implement all of the |
95 |
|
* <em>optional</em> methods of the {@link Map} and {@link Iterator} |
96 |
|
* interfaces. |
98 |
|
* <p>Like {@link Hashtable} but unlike {@link HashMap}, this class |
99 |
|
* does <em>not</em> allow {@code null} to be used as a key or value. |
100 |
|
* |
101 |
< |
* <p>ConcurrentHashMapV8s support parallel operations using the {@link |
102 |
< |
* ForkJoinPool#commonPool}. (Tasks that may be used in other contexts |
103 |
< |
* are available in class {@link ForkJoinTasks}). These operations are |
104 |
< |
* designed to be safely, and often sensibly, applied even with maps |
105 |
< |
* that are being concurrently updated by other threads; for example, |
106 |
< |
* when computing a snapshot summary of the values in a shared |
107 |
< |
* registry. There are three kinds of operation, each with four |
108 |
< |
* forms, accepting functions with Keys, Values, Entries, and (Key, |
109 |
< |
* Value) arguments and/or return values. (The first three forms are |
110 |
< |
* also available via the {@link #keySet()}, {@link #values()} and |
111 |
< |
* {@link #entrySet()} views). Because the elements of a |
112 |
< |
* ConcurrentHashMapV8 are not ordered in any particular way, and may be |
113 |
< |
* processed in different orders in different parallel executions, the |
114 |
< |
* correctness of supplied functions should not depend on any |
115 |
< |
* ordering, or on any other objects or values that may transiently |
142 |
< |
* change while computation is in progress; and except for forEach |
143 |
< |
* actions, should ideally be side-effect-free. |
101 |
> |
* <p>ConcurrentHashMapV8s support a set of sequential and parallel bulk |
102 |
> |
* operations that are designed |
103 |
> |
* to be safely, and often sensibly, applied even with maps that are |
104 |
> |
* being concurrently updated by other threads; for example, when |
105 |
> |
* computing a snapshot summary of the values in a shared registry. |
106 |
> |
* There are three kinds of operation, each with four forms, accepting |
107 |
> |
* functions with Keys, Values, Entries, and (Key, Value) arguments |
108 |
> |
* and/or return values. Because the elements of a ConcurrentHashMapV8 |
109 |
> |
* are not ordered in any particular way, and may be processed in |
110 |
> |
* different orders in different parallel executions, the correctness |
111 |
> |
* of supplied functions should not depend on any ordering, or on any |
112 |
> |
* other objects or values that may transiently change while |
113 |
> |
* computation is in progress; and except for forEach actions, should |
114 |
> |
* ideally be side-effect-free. Bulk operations on {@link java.util.Map.Entry} |
115 |
> |
* objects do not support method {@code setValue}. |
116 |
|
* |
117 |
|
* <ul> |
118 |
|
* <li> forEach: Perform a given action on each element. |
139 |
|
* <li> Reductions to scalar doubles, longs, and ints, using a |
140 |
|
* given basis value.</li> |
141 |
|
* |
170 |
– |
* </li> |
142 |
|
* </ul> |
143 |
+ |
* </li> |
144 |
|
* </ul> |
145 |
|
* |
146 |
+ |
* <p>These bulk operations accept a {@code parallelismThreshold} |
147 |
+ |
* argument. Methods proceed sequentially if the current map size is |
148 |
+ |
* estimated to be less than the given threshold. Using a value of |
149 |
+ |
* {@code Long.MAX_VALUE} suppresses all parallelism. Using a value |
150 |
+ |
* of {@code 1} results in maximal parallelism by partitioning into |
151 |
+ |
* enough subtasks to fully utilize the {@link |
152 |
+ |
* ForkJoinPool#commonPool()} that is used for all parallel |
153 |
+ |
* computations. Normally, you would initially choose one of these |
154 |
+ |
* extreme values, and then measure performance of using in-between |
155 |
+ |
* values that trade off overhead versus throughput. |
156 |
+ |
* |
157 |
|
* <p>The concurrency properties of bulk operations follow |
158 |
|
* from those of ConcurrentHashMapV8: Any non-null result returned |
159 |
|
* from {@code get(key)} and related access methods bears a |
196 |
|
* exceptions, or would have done so if the first exception had |
197 |
|
* not occurred. |
198 |
|
* |
199 |
< |
* <p>Parallel speedups for bulk operations compared to sequential |
200 |
< |
* processing are common but not guaranteed. Operations involving |
201 |
< |
* brief functions on small maps may execute more slowly than |
202 |
< |
* sequential loops if the underlying work to parallelize the |
203 |
< |
* computation is more expensive than the computation itself. |
204 |
< |
* Similarly, parallelization may not lead to much actual parallelism |
205 |
< |
* if all processors are busy performing unrelated tasks. |
199 |
> |
* <p>Speedups for parallel compared to sequential forms are common |
200 |
> |
* but not guaranteed. Parallel operations involving brief functions |
201 |
> |
* on small maps may execute more slowly than sequential forms if the |
202 |
> |
* underlying work to parallelize the computation is more expensive |
203 |
> |
* than the computation itself. Similarly, parallelization may not |
204 |
> |
* lead to much actual parallelism if all processors are busy |
205 |
> |
* performing unrelated tasks. |
206 |
|
* |
207 |
|
* <p>All arguments to all task methods must be non-null. |
208 |
|
* |
219 |
|
* @param <K> the type of keys maintained by this map |
220 |
|
* @param <V> the type of mapped values |
221 |
|
*/ |
222 |
< |
public class ConcurrentHashMapV8<K, V> |
223 |
< |
implements ConcurrentMap<K, V>, Serializable { |
222 |
> |
public class ConcurrentHashMapV8<K,V> extends AbstractMap<K,V> |
223 |
> |
implements ConcurrentMap<K,V>, Serializable { |
224 |
|
private static final long serialVersionUID = 7249069246763182397L; |
225 |
|
|
226 |
|
/** |
227 |
< |
* A partitionable iterator. A Spliterator can be traversed |
228 |
< |
* directly, but can also be partitioned (before traversal) by |
229 |
< |
* creating another Spliterator that covers a non-overlapping |
247 |
< |
* portion of the elements, and so may be amenable to parallel |
248 |
< |
* execution. |
249 |
< |
* |
250 |
< |
* <p>This interface exports a subset of expected JDK8 |
251 |
< |
* functionality. |
252 |
< |
* |
253 |
< |
* <p>Sample usage: Here is one (of the several) ways to compute |
254 |
< |
* the sum of the values held in a map using the ForkJoin |
255 |
< |
* framework. As illustrated here, Spliterators are well suited to |
256 |
< |
* designs in which a task repeatedly splits off half its work |
257 |
< |
* into forked subtasks until small enough to process directly, |
258 |
< |
* and then joins these subtasks. Variants of this style can also |
259 |
< |
* be used in completion-based designs. |
260 |
< |
* |
261 |
< |
* <pre> |
262 |
< |
* {@code ConcurrentHashMapV8<String, Long> m = ... |
263 |
< |
* // split as if have 8 * parallelism, for load balance |
264 |
< |
* int n = m.size(); |
265 |
< |
* int p = aForkJoinPool.getParallelism() * 8; |
266 |
< |
* int split = (n < p)? n : p; |
267 |
< |
* long sum = aForkJoinPool.invoke(new SumValues(m.valueSpliterator(), split, null)); |
268 |
< |
* // ... |
269 |
< |
* static class SumValues extends RecursiveTask<Long> { |
270 |
< |
* final Spliterator<Long> s; |
271 |
< |
* final int split; // split while > 1 |
272 |
< |
* final SumValues nextJoin; // records forked subtasks to join |
273 |
< |
* SumValues(Spliterator<Long> s, int depth, SumValues nextJoin) { |
274 |
< |
* this.s = s; this.depth = depth; this.nextJoin = nextJoin; |
275 |
< |
* } |
276 |
< |
* public Long compute() { |
277 |
< |
* long sum = 0; |
278 |
< |
* SumValues subtasks = null; // fork subtasks |
279 |
< |
* for (int s = split >>> 1; s > 0; s >>>= 1) |
280 |
< |
* (subtasks = new SumValues(s.split(), s, subtasks)).fork(); |
281 |
< |
* while (s.hasNext()) // directly process remaining elements |
282 |
< |
* sum += s.next(); |
283 |
< |
* for (SumValues t = subtasks; t != null; t = t.nextJoin) |
284 |
< |
* sum += t.join(); // collect subtask results |
285 |
< |
* return sum; |
286 |
< |
* } |
287 |
< |
* } |
288 |
< |
* }</pre> |
227 |
> |
* An object for traversing and partitioning elements of a source. |
228 |
> |
* This interface provides a subset of the functionality of JDK8 |
229 |
> |
* java.util.Spliterator. |
230 |
|
*/ |
231 |
< |
public static interface Spliterator<T> extends Iterator<T> { |
231 |
> |
public static interface ConcurrentHashMapSpliterator<T> { |
232 |
|
/** |
233 |
< |
* Returns a Spliterator covering approximately half of the |
234 |
< |
* elements, guaranteed not to overlap with those subsequently |
235 |
< |
* returned by this Spliterator. After invoking this method, |
236 |
< |
* the current Spliterator will <em>not</em> produce any of |
237 |
< |
* the elements of the returned Spliterator, but the two |
238 |
< |
* Spliterators together will produce all of the elements that |
239 |
< |
* would have been produced by this Spliterator had this |
240 |
< |
* method not been called. The exact number of elements |
241 |
< |
* produced by the returned Spliterator is not guaranteed, and |
301 |
< |
* may be zero (i.e., with {@code hasNext()} reporting {@code |
302 |
< |
* false}) if this Spliterator cannot be further split. |
303 |
< |
* |
304 |
< |
* @return a Spliterator covering approximately half of the |
305 |
< |
* elements |
306 |
< |
* @throws IllegalStateException if this Spliterator has |
307 |
< |
* already commenced traversing elements |
233 |
> |
* If possible, returns a new spliterator covering |
234 |
> |
* approximately one half of the elements, which will not be |
235 |
> |
* covered by this spliterator. Returns null if cannot be |
236 |
> |
* split. |
237 |
> |
*/ |
238 |
> |
ConcurrentHashMapSpliterator<T> trySplit(); |
239 |
> |
/** |
240 |
> |
* Returns an estimate of the number of elements covered by |
241 |
> |
* this Spliterator. |
242 |
|
*/ |
243 |
< |
Spliterator<T> split(); |
243 |
> |
long estimateSize(); |
244 |
> |
|
245 |
> |
/** Applies the action to each untraversed element */ |
246 |
> |
void forEachRemaining(Action<? super T> action); |
247 |
> |
/** If an element remains, applies the action and returns true. */ |
248 |
> |
boolean tryAdvance(Action<? super T> action); |
249 |
|
} |
250 |
|
|
251 |
+ |
// Sams |
252 |
+ |
/** Interface describing a void action of one argument */ |
253 |
+ |
public interface Action<A> { void apply(A a); } |
254 |
+ |
/** Interface describing a void action of two arguments */ |
255 |
+ |
public interface BiAction<A,B> { void apply(A a, B b); } |
256 |
+ |
/** Interface describing a function of one argument */ |
257 |
+ |
public interface Fun<A,T> { T apply(A a); } |
258 |
+ |
/** Interface describing a function of two arguments */ |
259 |
+ |
public interface BiFun<A,B,T> { T apply(A a, B b); } |
260 |
+ |
/** Interface describing a function mapping its argument to a double */ |
261 |
+ |
public interface ObjectToDouble<A> { double apply(A a); } |
262 |
+ |
/** Interface describing a function mapping its argument to a long */ |
263 |
+ |
public interface ObjectToLong<A> { long apply(A a); } |
264 |
+ |
/** Interface describing a function mapping its argument to an int */ |
265 |
+ |
public interface ObjectToInt<A> {int apply(A a); } |
266 |
+ |
/** Interface describing a function mapping two arguments to a double */ |
267 |
+ |
public interface ObjectByObjectToDouble<A,B> { double apply(A a, B b); } |
268 |
+ |
/** Interface describing a function mapping two arguments to a long */ |
269 |
+ |
public interface ObjectByObjectToLong<A,B> { long apply(A a, B b); } |
270 |
+ |
/** Interface describing a function mapping two arguments to an int */ |
271 |
+ |
public interface ObjectByObjectToInt<A,B> {int apply(A a, B b); } |
272 |
+ |
/** Interface describing a function mapping two doubles to a double */ |
273 |
+ |
public interface DoubleByDoubleToDouble { double apply(double a, double b); } |
274 |
+ |
/** Interface describing a function mapping two longs to a long */ |
275 |
+ |
public interface LongByLongToLong { long apply(long a, long b); } |
276 |
+ |
/** Interface describing a function mapping two ints to an int */ |
277 |
+ |
public interface IntByIntToInt { int apply(int a, int b); } |
278 |
|
|
279 |
|
/* |
280 |
|
* Overview: |
286 |
|
* the same or better than java.util.HashMap, and to support high |
287 |
|
* initial insertion rates on an empty table by many threads. |
288 |
|
* |
289 |
< |
* Each key-value mapping is held in a Node. Because Node fields |
290 |
< |
* can contain special values, they are defined using plain Object |
291 |
< |
* types. Similarly in turn, all internal methods that use them |
292 |
< |
* work off Object types. And similarly, so do the internal |
293 |
< |
* methods of auxiliary iterator and view classes. All public |
294 |
< |
* generic typed methods relay in/out of these internal methods, |
295 |
< |
* supplying null-checks and casts as needed. This also allows |
296 |
< |
* many of the public methods to be factored into a smaller number |
297 |
< |
* of internal methods (although sadly not so for the five |
298 |
< |
* variants of put-related operations). The validation-based |
299 |
< |
* approach explained below leads to a lot of code sprawl because |
300 |
< |
* retry-control precludes factoring into smaller methods. |
289 |
> |
* This map usually acts as a binned (bucketed) hash table. Each |
290 |
> |
* key-value mapping is held in a Node. Most nodes are instances |
291 |
> |
* of the basic Node class with hash, key, value, and next |
292 |
> |
* fields. However, various subclasses exist: TreeNodes are |
293 |
> |
* arranged in balanced trees, not lists. TreeBins hold the roots |
294 |
> |
* of sets of TreeNodes. ForwardingNodes are placed at the heads |
295 |
> |
* of bins during resizing. ReservationNodes are used as |
296 |
> |
* placeholders while establishing values in computeIfAbsent and |
297 |
> |
* related methods. The types TreeBin, ForwardingNode, and |
298 |
> |
* ReservationNode do not hold normal user keys, values, or |
299 |
> |
* hashes, and are readily distinguishable during search etc |
300 |
> |
* because they have negative hash fields and null key and value |
301 |
> |
* fields. (These special nodes are either uncommon or transient, |
302 |
> |
* so the impact of carrying around some unused fields is |
303 |
> |
* insignificant.) |
304 |
|
* |
305 |
|
* The table is lazily initialized to a power-of-two size upon the |
306 |
|
* first insertion. Each bin in the table normally contains a |
308 |
|
* Table accesses require volatile/atomic reads, writes, and |
309 |
|
* CASes. Because there is no other way to arrange this without |
310 |
|
* adding further indirections, we use intrinsics |
311 |
< |
* (sun.misc.Unsafe) operations. The lists of nodes within bins |
312 |
< |
* are always accurately traversable under volatile reads, so long |
313 |
< |
* as lookups check hash code and non-nullness of value before |
314 |
< |
* checking key equality. |
315 |
< |
* |
316 |
< |
* We use the top two bits of Node hash fields for control |
348 |
< |
* purposes -- they are available anyway because of addressing |
349 |
< |
* constraints. As explained further below, these top bits are |
350 |
< |
* used as follows: |
351 |
< |
* 00 - Normal |
352 |
< |
* 01 - Locked |
353 |
< |
* 11 - Locked and may have a thread waiting for lock |
354 |
< |
* 10 - Node is a forwarding node |
355 |
< |
* |
356 |
< |
* The lower 30 bits of each Node's hash field contain a |
357 |
< |
* transformation of the key's hash code, except for forwarding |
358 |
< |
* nodes, for which the lower bits are zero (and so always have |
359 |
< |
* hash field == MOVED). |
311 |
> |
* (sun.misc.Unsafe) operations. |
312 |
> |
* |
313 |
> |
* We use the top (sign) bit of Node hash fields for control |
314 |
> |
* purposes -- it is available anyway because of addressing |
315 |
> |
* constraints. Nodes with negative hash fields are specially |
316 |
> |
* handled or ignored in map methods. |
317 |
|
* |
318 |
|
* Insertion (via put or its variants) of the first node in an |
319 |
|
* empty bin is performed by just CASing it to the bin. This is |
322 |
|
* delete, and replace) require locks. We do not want to waste |
323 |
|
* the space required to associate a distinct lock object with |
324 |
|
* each bin, so instead use the first node of a bin list itself as |
325 |
< |
* a lock. Blocking support for these locks relies on the builtin |
326 |
< |
* "synchronized" monitors. However, we also need a tryLock |
370 |
< |
* construction, so we overlay these by using bits of the Node |
371 |
< |
* hash field for lock control (see above), and so normally use |
372 |
< |
* builtin monitors only for blocking and signalling using |
373 |
< |
* wait/notifyAll constructions. See Node.tryAwaitLock. |
325 |
> |
* a lock. Locking support for these locks relies on builtin |
326 |
> |
* "synchronized" monitors. |
327 |
|
* |
328 |
|
* Using the first node of a list as a lock does not by itself |
329 |
|
* suffice though: When a node is locked, any update must first |
330 |
|
* validate that it is still the first node after locking it, and |
331 |
|
* retry if not. Because new nodes are always appended to lists, |
332 |
|
* once a node is first in a bin, it remains first until deleted |
333 |
< |
* or the bin becomes invalidated (upon resizing). However, |
381 |
< |
* operations that only conditionally update may inspect nodes |
382 |
< |
* until the point of update. This is a converse of sorts to the |
383 |
< |
* lazy locking technique described by Herlihy & Shavit. |
333 |
> |
* or the bin becomes invalidated (upon resizing). |
334 |
|
* |
335 |
|
* The main disadvantage of per-bin locks is that other update |
336 |
|
* operations on other nodes in a bin list protected by the same |
363 |
|
* sometimes deviate significantly from uniform randomness. This |
364 |
|
* includes the case when N > (1<<30), so some keys MUST collide. |
365 |
|
* Similarly for dumb or hostile usages in which multiple keys are |
366 |
< |
* designed to have identical hash codes. Also, although we guard |
367 |
< |
* against the worst effects of this (see method spread), sets of |
368 |
< |
* hashes may differ only in bits that do not impact their bin |
369 |
< |
* index for a given power-of-two mask. So we use a secondary |
370 |
< |
* strategy that applies when the number of nodes in a bin exceeds |
371 |
< |
* a threshold, and at least one of the keys implements |
422 |
< |
* Comparable. These TreeBins use a balanced tree to hold nodes |
423 |
< |
* (a specialized form of red-black trees), bounding search time |
424 |
< |
* to O(log N). Each search step in a TreeBin is around twice as |
366 |
> |
* designed to have identical hash codes or ones that differs only |
367 |
> |
* in masked-out high bits. So we use a secondary strategy that |
368 |
> |
* applies when the number of nodes in a bin exceeds a |
369 |
> |
* threshold. These TreeBins use a balanced tree to hold nodes (a |
370 |
> |
* specialized form of red-black trees), bounding search time to |
371 |
> |
* O(log N). Each search step in a TreeBin is at least twice as |
372 |
|
* slow as in a regular list, but given that N cannot exceed |
373 |
|
* (1<<64) (before running out of addresses) this bounds search |
374 |
|
* steps, lock hold times, etc, to reasonable constants (roughly |
379 |
|
* iterators in the same way. |
380 |
|
* |
381 |
|
* The table is resized when occupancy exceeds a percentage |
382 |
< |
* threshold (nominally, 0.75, but see below). Only a single |
383 |
< |
* thread performs the resize (using field "sizeCtl", to arrange |
384 |
< |
* exclusion), but the table otherwise remains usable for reads |
385 |
< |
* and updates. Resizing proceeds by transferring bins, one by |
386 |
< |
* one, from the table to the next table. Because we are using |
387 |
< |
* power-of-two expansion, the elements from each bin must either |
388 |
< |
* stay at same index, or move with a power of two offset. We |
389 |
< |
* eliminate unnecessary node creation by catching cases where old |
390 |
< |
* nodes can be reused because their next fields won't change. On |
391 |
< |
* average, only about one-sixth of them need cloning when a table |
392 |
< |
* doubles. The nodes they replace will be garbage collectable as |
393 |
< |
* soon as they are no longer referenced by any reader thread that |
394 |
< |
* may be in the midst of concurrently traversing table. Upon |
395 |
< |
* transfer, the old table bin contains only a special forwarding |
396 |
< |
* node (with hash field "MOVED") that contains the next table as |
397 |
< |
* its key. On encountering a forwarding node, access and update |
398 |
< |
* operations restart, using the new table. |
399 |
< |
* |
400 |
< |
* Each bin transfer requires its bin lock. However, unlike other |
401 |
< |
* cases, a transfer can skip a bin if it fails to acquire its |
402 |
< |
* lock, and revisit it later (unless it is a TreeBin). Method |
403 |
< |
* rebuild maintains a buffer of TRANSFER_BUFFER_SIZE bins that |
404 |
< |
* have been skipped because of failure to acquire a lock, and |
405 |
< |
* blocks only if none are available (i.e., only very rarely). |
406 |
< |
* The transfer operation must also ensure that all accessible |
407 |
< |
* bins in both the old and new table are usable by any traversal. |
408 |
< |
* When there are no lock acquisition failures, this is arranged |
409 |
< |
* simply by proceeding from the last bin (table.length - 1) up |
410 |
< |
* towards the first. Upon seeing a forwarding node, traversals |
411 |
< |
* (see class Iter) arrange to move to the new table |
412 |
< |
* without revisiting nodes. However, when any node is skipped |
413 |
< |
* during a transfer, all earlier table bins may have become |
414 |
< |
* visible, so are initialized with a reverse-forwarding node back |
415 |
< |
* to the old table until the new ones are established. (This |
416 |
< |
* sometimes requires transiently locking a forwarding node, which |
417 |
< |
* is possible under the above encoding.) These more expensive |
418 |
< |
* mechanics trigger only when necessary. |
382 |
> |
* threshold (nominally, 0.75, but see below). Any thread |
383 |
> |
* noticing an overfull bin may assist in resizing after the |
384 |
> |
* initiating thread allocates and sets up the replacement |
385 |
> |
* array. However, rather than stalling, these other threads may |
386 |
> |
* proceed with insertions etc. The use of TreeBins shields us |
387 |
> |
* from the worst case effects of overfilling while resizes are in |
388 |
> |
* progress. Resizing proceeds by transferring bins, one by one, |
389 |
> |
* from the table to the next table. To enable concurrency, the |
390 |
> |
* next table must be (incrementally) prefilled with place-holders |
391 |
> |
* serving as reverse forwarders to the old table. Because we are |
392 |
> |
* using power-of-two expansion, the elements from each bin must |
393 |
> |
* either stay at same index, or move with a power of two |
394 |
> |
* offset. We eliminate unnecessary node creation by catching |
395 |
> |
* cases where old nodes can be reused because their next fields |
396 |
> |
* won't change. On average, only about one-sixth of them need |
397 |
> |
* cloning when a table doubles. The nodes they replace will be |
398 |
> |
* garbage collectable as soon as they are no longer referenced by |
399 |
> |
* any reader thread that may be in the midst of concurrently |
400 |
> |
* traversing table. Upon transfer, the old table bin contains |
401 |
> |
* only a special forwarding node (with hash field "MOVED") that |
402 |
> |
* contains the next table as its key. On encountering a |
403 |
> |
* forwarding node, access and update operations restart, using |
404 |
> |
* the new table. |
405 |
> |
* |
406 |
> |
* Each bin transfer requires its bin lock, which can stall |
407 |
> |
* waiting for locks while resizing. However, because other |
408 |
> |
* threads can join in and help resize rather than contend for |
409 |
> |
* locks, average aggregate waits become shorter as resizing |
410 |
> |
* progresses. The transfer operation must also ensure that all |
411 |
> |
* accessible bins in both the old and new table are usable by any |
412 |
> |
* traversal. This is arranged by proceeding from the last bin |
413 |
> |
* (table.length - 1) up towards the first. Upon seeing a |
414 |
> |
* forwarding node, traversals (see class Traverser) arrange to |
415 |
> |
* move to the new table without revisiting nodes. However, to |
416 |
> |
* ensure that no intervening nodes are skipped, bin splitting can |
417 |
> |
* only begin after the associated reverse-forwarders are in |
418 |
> |
* place. |
419 |
|
* |
420 |
|
* The traversal scheme also applies to partial traversals of |
421 |
|
* ranges of bins (via an alternate Traverser constructor) |
430 |
|
* These cases attempt to override the initial capacity settings, |
431 |
|
* but harmlessly fail to take effect in cases of races. |
432 |
|
* |
433 |
< |
* The element count is maintained using a LongAdder, which avoids |
434 |
< |
* contention on updates but can encounter cache thrashing if read |
435 |
< |
* too frequently during concurrent access. To avoid reading so |
436 |
< |
* often, resizing is attempted either when a bin lock is |
437 |
< |
* contended, or upon adding to a bin already holding two or more |
438 |
< |
* nodes (checked before adding in the xIfAbsent methods, after |
439 |
< |
* adding in others). Under uniform hash distributions, the |
440 |
< |
* probability of this occurring at threshold is around 13%, |
441 |
< |
* meaning that only about 1 in 8 puts check threshold (and after |
442 |
< |
* resizing, many fewer do so). But this approximation has high |
443 |
< |
* variance for small table sizes, so we check on any collision |
444 |
< |
* for sizes <= 64. The bulk putAll operation further reduces |
445 |
< |
* contention by only committing count updates upon these size |
446 |
< |
* checks. |
433 |
> |
* The element count is maintained using a specialization of |
434 |
> |
* LongAdder. We need to incorporate a specialization rather than |
435 |
> |
* just use a LongAdder in order to access implicit |
436 |
> |
* contention-sensing that leads to creation of multiple |
437 |
> |
* CounterCells. The counter mechanics avoid contention on |
438 |
> |
* updates but can encounter cache thrashing if read too |
439 |
> |
* frequently during concurrent access. To avoid reading so often, |
440 |
> |
* resizing under contention is attempted only upon adding to a |
441 |
> |
* bin already holding two or more nodes. Under uniform hash |
442 |
> |
* distributions, the probability of this occurring at threshold |
443 |
> |
* is around 13%, meaning that only about 1 in 8 puts check |
444 |
> |
* threshold (and after resizing, many fewer do so). |
445 |
> |
* |
446 |
> |
* TreeBins use a special form of comparison for search and |
447 |
> |
* related operations (which is the main reason we cannot use |
448 |
> |
* existing collections such as TreeMaps). TreeBins contain |
449 |
> |
* Comparable elements, but may contain others, as well as |
450 |
> |
* elements that are Comparable but not necessarily Comparable for |
451 |
> |
* the same T, so we cannot invoke compareTo among them. To handle |
452 |
> |
* this, the tree is ordered primarily by hash value, then by |
453 |
> |
* Comparable.compareTo order if applicable. On lookup at a node, |
454 |
> |
* if elements are not comparable or compare as 0 then both left |
455 |
> |
* and right children may need to be searched in the case of tied |
456 |
> |
* hash values. (This corresponds to the full list search that |
457 |
> |
* would be necessary if all elements were non-Comparable and had |
458 |
> |
* tied hashes.) On insertion, to keep a total ordering (or as |
459 |
> |
* close as is required here) across rebalancings, we compare |
460 |
> |
* classes and identityHashCodes as tie-breakers. The red-black |
461 |
> |
* balancing code is updated from pre-jdk-collections |
462 |
> |
* (http://gee.cs.oswego.edu/dl/classes/collections/RBCell.java) |
463 |
> |
* based in turn on Cormen, Leiserson, and Rivest "Introduction to |
464 |
> |
* Algorithms" (CLR). |
465 |
> |
* |
466 |
> |
* TreeBins also require an additional locking mechanism. While |
467 |
> |
* list traversal is always possible by readers even during |
468 |
> |
* updates, tree traversal is not, mainly because of tree-rotations |
469 |
> |
* that may change the root node and/or its linkages. TreeBins |
470 |
> |
* include a simple read-write lock mechanism parasitic on the |
471 |
> |
* main bin-synchronization strategy: Structural adjustments |
472 |
> |
* associated with an insertion or removal are already bin-locked |
473 |
> |
* (and so cannot conflict with other writers) but must wait for |
474 |
> |
* ongoing readers to finish. Since there can be only one such |
475 |
> |
* waiter, we use a simple scheme using a single "waiter" field to |
476 |
> |
* block writers. However, readers need never block. If the root |
477 |
> |
* lock is held, they proceed along the slow traversal path (via |
478 |
> |
* next-pointers) until the lock becomes available or the list is |
479 |
> |
* exhausted, whichever comes first. These cases are not fast, but |
480 |
> |
* maximize aggregate expected throughput. |
481 |
|
* |
482 |
|
* Maintaining API and serialization compatibility with previous |
483 |
|
* versions of this class introduces several oddities. Mainly: We |
487 |
|
* time that we can guarantee to honor it.) We also declare an |
488 |
|
* unused "Segment" class that is instantiated in minimal form |
489 |
|
* only when serializing. |
490 |
+ |
* |
491 |
+ |
* Also, solely for compatibility with previous versions of this |
492 |
+ |
* class, it extends AbstractMap, even though all of its methods |
493 |
+ |
* are overridden, so it is just useless baggage. |
494 |
+ |
* |
495 |
+ |
* This file is organized to make things a little easier to follow |
496 |
+ |
* while reading than they might otherwise: First the main static |
497 |
+ |
* declarations and utilities, then fields, then main public |
498 |
+ |
* methods (with a few factorings of multiple public methods into |
499 |
+ |
* internal ones), then sizing methods, trees, traversers, and |
500 |
+ |
* bulk operations. |
501 |
|
*/ |
502 |
|
|
503 |
|
/* ---------------- Constants -------------- */ |
539 |
|
private static final float LOAD_FACTOR = 0.75f; |
540 |
|
|
541 |
|
/** |
550 |
– |
* The buffer size for skipped bins during transfers. The |
551 |
– |
* value is arbitrary but should be large enough to avoid |
552 |
– |
* most locking stalls during resizes. |
553 |
– |
*/ |
554 |
– |
private static final int TRANSFER_BUFFER_SIZE = 32; |
555 |
– |
|
556 |
– |
/** |
542 |
|
* The bin count threshold for using a tree rather than list for a |
543 |
< |
* bin. The value reflects the approximate break-even point for |
544 |
< |
* using tree-based operations. |
545 |
< |
*/ |
546 |
< |
private static final int TREE_THRESHOLD = 8; |
547 |
< |
|
563 |
< |
/* |
564 |
< |
* Encodings for special uses of Node hash fields. See above for |
565 |
< |
* explanation. |
543 |
> |
* bin. Bins are converted to trees when adding an element to a |
544 |
> |
* bin with at least this many nodes. The value must be greater |
545 |
> |
* than 2, and should be at least 8 to mesh with assumptions in |
546 |
> |
* tree removal about conversion back to plain bins upon |
547 |
> |
* shrinkage. |
548 |
|
*/ |
549 |
< |
static final int MOVED = 0x80000000; // hash field for forwarding nodes |
568 |
< |
static final int LOCKED = 0x40000000; // set/tested only as a bit |
569 |
< |
static final int WAITING = 0xc0000000; // both bits set/tested together |
570 |
< |
static final int HASH_BITS = 0x3fffffff; // usable bits of normal node hash |
571 |
< |
|
572 |
< |
/* ---------------- Fields -------------- */ |
549 |
> |
static final int TREEIFY_THRESHOLD = 8; |
550 |
|
|
551 |
|
/** |
552 |
< |
* The array of bins. Lazily initialized upon first insertion. |
553 |
< |
* Size is always a power of two. Accessed directly by iterators. |
552 |
> |
* The bin count threshold for untreeifying a (split) bin during a |
553 |
> |
* resize operation. Should be less than TREEIFY_THRESHOLD, and at |
554 |
> |
* most 6 to mesh with shrinkage detection under removal. |
555 |
|
*/ |
556 |
< |
transient volatile Node[] table; |
556 |
> |
static final int UNTREEIFY_THRESHOLD = 6; |
557 |
|
|
558 |
|
/** |
559 |
< |
* The counter maintaining number of elements. |
559 |
> |
* The smallest table capacity for which bins may be treeified. |
560 |
> |
* (Otherwise the table is resized if too many nodes in a bin.) |
561 |
> |
* The value should be at least 4 * TREEIFY_THRESHOLD to avoid |
562 |
> |
* conflicts between resizing and treeification thresholds. |
563 |
|
*/ |
564 |
< |
private transient final LongAdder counter; |
564 |
> |
static final int MIN_TREEIFY_CAPACITY = 64; |
565 |
|
|
566 |
|
/** |
567 |
< |
* Table initialization and resizing control. When negative, the |
568 |
< |
* 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. |
567 |
> |
* Minimum number of rebinnings per transfer step. Ranges are |
568 |
> |
* subdivided to allow multiple resizer threads. This value |
569 |
> |
* serves as a lower bound to avoid resizers encountering |
570 |
> |
* excessive memory contention. The value should be at least |
571 |
> |
* DEFAULT_CAPACITY. |
572 |
|
*/ |
573 |
< |
private transient volatile int sizeCtl; |
593 |
< |
|
594 |
< |
// views |
595 |
< |
private transient KeySetView<K,V> keySet; |
596 |
< |
private transient ValuesView<K,V> values; |
597 |
< |
private transient EntrySetView<K,V> entrySet; |
598 |
< |
|
599 |
< |
/** For serialization compatibility. Null unless serialized; see below */ |
600 |
< |
private Segment<K,V>[] segments; |
601 |
< |
|
602 |
< |
/* ---------------- Table element access -------------- */ |
573 |
> |
private static final int MIN_TRANSFER_STRIDE = 16; |
574 |
|
|
575 |
|
/* |
576 |
< |
* Volatile access methods are used for table elements as well as |
606 |
< |
* elements of in-progress next table while resizing. Uses are |
607 |
< |
* null checked by callers, and implicitly bounds-checked, relying |
608 |
< |
* on the invariants that tab arrays have non-zero size, and all |
609 |
< |
* indices are masked with (tab.length - 1) which is never |
610 |
< |
* negative and always less than length. Note that, to be correct |
611 |
< |
* wrt arbitrary concurrency errors by users, bounds checks must |
612 |
< |
* operate on local variables, which accounts for some odd-looking |
613 |
< |
* inline assignments below. |
576 |
> |
* Encodings for Node hash fields. See above for explanation. |
577 |
|
*/ |
578 |
< |
|
579 |
< |
static final Node tabAt(Node[] tab, int i) { // used by Iter |
580 |
< |
return (Node)UNSAFE.getObjectVolatile(tab, ((long)i<<ASHIFT)+ABASE); |
581 |
< |
} |
582 |
< |
|
583 |
< |
private static final boolean casTabAt(Node[] tab, int i, Node c, Node v) { |
584 |
< |
return UNSAFE.compareAndSwapObject(tab, ((long)i<<ASHIFT)+ABASE, c, v); |
585 |
< |
} |
586 |
< |
|
587 |
< |
private static final void setTabAt(Node[] tab, int i, Node v) { |
588 |
< |
UNSAFE.putObjectVolatile(tab, ((long)i<<ASHIFT)+ABASE, v); |
589 |
< |
} |
578 |
> |
static final int MOVED = -1; // hash for forwarding nodes |
579 |
> |
static final int TREEBIN = -2; // hash for roots of trees |
580 |
> |
static final int RESERVED = -3; // hash for transient reservations |
581 |
> |
static final int HASH_BITS = 0x7fffffff; // usable bits of normal node hash |
582 |
> |
|
583 |
> |
/** Number of CPUS, to place bounds on some sizings */ |
584 |
> |
static final int NCPU = Runtime.getRuntime().availableProcessors(); |
585 |
> |
|
586 |
> |
/** For serialization compatibility. */ |
587 |
> |
private static final ObjectStreamField[] serialPersistentFields = { |
588 |
> |
new ObjectStreamField("segments", Segment[].class), |
589 |
> |
new ObjectStreamField("segmentMask", Integer.TYPE), |
590 |
> |
new ObjectStreamField("segmentShift", Integer.TYPE) |
591 |
> |
}; |
592 |
|
|
593 |
|
/* ---------------- Nodes -------------- */ |
594 |
|
|
595 |
|
/** |
596 |
< |
* Key-value entry. Note that this is never exported out as a |
597 |
< |
* user-visible Map.Entry (see MapEntry below). Nodes with a hash |
598 |
< |
* field of MOVED are special, and do not contain user keys or |
599 |
< |
* values. Otherwise, keys are never null, and null val fields |
600 |
< |
* indicate that a node is in the process of being deleted or |
601 |
< |
* created. For purposes of read-only access, a key may be read |
602 |
< |
* before a val, but can only be used after checking val to be |
603 |
< |
* non-null. |
604 |
< |
*/ |
605 |
< |
static class Node { |
606 |
< |
volatile int hash; |
607 |
< |
final Object key; |
643 |
< |
volatile Object val; |
644 |
< |
volatile Node next; |
596 |
> |
* Key-value entry. This class is never exported out as a |
597 |
> |
* user-mutable Map.Entry (i.e., one supporting setValue; see |
598 |
> |
* MapEntry below), but can be used for read-only traversals used |
599 |
> |
* in bulk tasks. Subclasses of Node with a negative hash field |
600 |
> |
* are special, and contain null keys and values (but are never |
601 |
> |
* exported). Otherwise, keys and vals are never null. |
602 |
> |
*/ |
603 |
> |
static class Node<K,V> implements Map.Entry<K,V> { |
604 |
> |
final int hash; |
605 |
> |
final K key; |
606 |
> |
volatile V val; |
607 |
> |
volatile Node<K,V> next; |
608 |
|
|
609 |
< |
Node(int hash, Object key, Object val, Node next) { |
609 |
> |
Node(int hash, K key, V val, Node<K,V> next) { |
610 |
|
this.hash = hash; |
611 |
|
this.key = key; |
612 |
|
this.val = val; |
613 |
|
this.next = next; |
614 |
|
} |
615 |
|
|
616 |
< |
/** CompareAndSet the hash field */ |
617 |
< |
final boolean casHash(int cmp, int val) { |
618 |
< |
return UNSAFE.compareAndSwapInt(this, hashOffset, cmp, val); |
619 |
< |
} |
620 |
< |
|
621 |
< |
/** The number of spins before blocking for a lock */ |
659 |
< |
static final int MAX_SPINS = |
660 |
< |
Runtime.getRuntime().availableProcessors() > 1 ? 64 : 1; |
661 |
< |
|
662 |
< |
/** |
663 |
< |
* Spins a while if LOCKED bit set and this node is the first |
664 |
< |
* of its bin, and then sets WAITING bits on hash field and |
665 |
< |
* blocks (once) if they are still set. It is OK for this |
666 |
< |
* method to return even if lock is not available upon exit, |
667 |
< |
* which enables these simple single-wait mechanics. |
668 |
< |
* |
669 |
< |
* The corresponding signalling operation is performed within |
670 |
< |
* callers: Upon detecting that WAITING has been set when |
671 |
< |
* unlocking lock (via a failed CAS from non-waiting LOCKED |
672 |
< |
* state), unlockers acquire the sync lock and perform a |
673 |
< |
* notifyAll. |
674 |
< |
* |
675 |
< |
* The initial sanity check on tab and bounds is not currently |
676 |
< |
* necessary in the only usages of this method, but enables |
677 |
< |
* use in other future contexts. |
678 |
< |
*/ |
679 |
< |
final void tryAwaitLock(Node[] tab, int i) { |
680 |
< |
if (tab != null && i >= 0 && i < tab.length) { // sanity check |
681 |
< |
int r = ThreadLocalRandom.current().nextInt(); // randomize spins |
682 |
< |
int spins = MAX_SPINS, h; |
683 |
< |
while (tabAt(tab, i) == this && ((h = hash) & LOCKED) != 0) { |
684 |
< |
if (spins >= 0) { |
685 |
< |
r ^= r << 1; r ^= r >>> 3; r ^= r << 10; // xorshift |
686 |
< |
if (r >= 0 && --spins == 0) |
687 |
< |
Thread.yield(); // yield before block |
688 |
< |
} |
689 |
< |
else if (casHash(h, h | WAITING)) { |
690 |
< |
synchronized (this) { |
691 |
< |
if (tabAt(tab, i) == this && |
692 |
< |
(hash & WAITING) == WAITING) { |
693 |
< |
try { |
694 |
< |
wait(); |
695 |
< |
} catch (InterruptedException ie) { |
696 |
< |
try { |
697 |
< |
Thread.currentThread().interrupt(); |
698 |
< |
} catch (SecurityException ignore) { |
699 |
< |
} |
700 |
< |
} |
701 |
< |
} |
702 |
< |
else |
703 |
< |
notifyAll(); // possibly won race vs signaller |
704 |
< |
} |
705 |
< |
break; |
706 |
< |
} |
707 |
< |
} |
708 |
< |
} |
709 |
< |
} |
710 |
< |
|
711 |
< |
// Unsafe mechanics for casHash |
712 |
< |
private static final sun.misc.Unsafe UNSAFE; |
713 |
< |
private static final long hashOffset; |
714 |
< |
|
715 |
< |
static { |
716 |
< |
try { |
717 |
< |
UNSAFE = getUnsafe(); |
718 |
< |
Class<?> k = Node.class; |
719 |
< |
hashOffset = UNSAFE.objectFieldOffset |
720 |
< |
(k.getDeclaredField("hash")); |
721 |
< |
} catch (Exception e) { |
722 |
< |
throw new Error(e); |
723 |
< |
} |
724 |
< |
} |
725 |
< |
} |
726 |
< |
|
727 |
< |
/* ---------------- TreeBins -------------- */ |
728 |
< |
|
729 |
< |
/** |
730 |
< |
* Nodes for use in TreeBins |
731 |
< |
*/ |
732 |
< |
static final class TreeNode extends Node { |
733 |
< |
TreeNode parent; // red-black tree links |
734 |
< |
TreeNode left; |
735 |
< |
TreeNode right; |
736 |
< |
TreeNode prev; // needed to unlink next upon deletion |
737 |
< |
boolean red; |
738 |
< |
|
739 |
< |
TreeNode(int hash, Object key, Object val, Node next, TreeNode parent) { |
740 |
< |
super(hash, key, val, next); |
741 |
< |
this.parent = parent; |
742 |
< |
} |
743 |
< |
} |
744 |
< |
|
745 |
< |
/** |
746 |
< |
* A specialized form of red-black tree for use in bins |
747 |
< |
* whose size exceeds a threshold. |
748 |
< |
* |
749 |
< |
* TreeBins use a special form of comparison for search and |
750 |
< |
* related operations (which is the main reason we cannot use |
751 |
< |
* existing collections such as TreeMaps). TreeBins contain |
752 |
< |
* Comparable elements, but may contain others, as well as |
753 |
< |
* elements that are Comparable but not necessarily Comparable<T> |
754 |
< |
* for the same T, so we cannot invoke compareTo among them. To |
755 |
< |
* handle this, the tree is ordered primarily by hash value, then |
756 |
< |
* by getClass().getName() order, and then by Comparator order |
757 |
< |
* among elements of the same class. On lookup at a node, if |
758 |
< |
* elements are not comparable or compare as 0, both left and |
759 |
< |
* right children may need to be searched in the case of tied hash |
760 |
< |
* values. (This corresponds to the full list search that would be |
761 |
< |
* necessary if all elements were non-Comparable and had tied |
762 |
< |
* hashes.) The red-black balancing code is updated from |
763 |
< |
* pre-jdk-collections |
764 |
< |
* (http://gee.cs.oswego.edu/dl/classes/collections/RBCell.java) |
765 |
< |
* based in turn on Cormen, Leiserson, and Rivest "Introduction to |
766 |
< |
* Algorithms" (CLR). |
767 |
< |
* |
768 |
< |
* TreeBins also maintain a separate locking discipline than |
769 |
< |
* regular bins. Because they are forwarded via special MOVED |
770 |
< |
* nodes at bin heads (which can never change once established), |
771 |
< |
* we cannot use those nodes as locks. Instead, TreeBin |
772 |
< |
* extends AbstractQueuedSynchronizer to support a simple form of |
773 |
< |
* read-write lock. For update operations and table validation, |
774 |
< |
* the exclusive form of lock behaves in the same way as bin-head |
775 |
< |
* locks. However, lookups use shared read-lock mechanics to allow |
776 |
< |
* multiple readers in the absence of writers. Additionally, |
777 |
< |
* these lookups do not ever block: While the lock is not |
778 |
< |
* available, they proceed along the slow traversal path (via |
779 |
< |
* next-pointers) until the lock becomes available or the list is |
780 |
< |
* exhausted, whichever comes first. (These cases are not fast, |
781 |
< |
* but maximize aggregate expected throughput.) The AQS mechanics |
782 |
< |
* for doing this are straightforward. The lock state is held as |
783 |
< |
* AQS getState(). Read counts are negative; the write count (1) |
784 |
< |
* is positive. There are no signalling preferences among readers |
785 |
< |
* and writers. Since we don't need to export full Lock API, we |
786 |
< |
* just override the minimal AQS methods and use them directly. |
787 |
< |
*/ |
788 |
< |
static final class TreeBin extends AbstractQueuedSynchronizer { |
789 |
< |
private static final long serialVersionUID = 2249069246763182397L; |
790 |
< |
transient TreeNode root; // root of tree |
791 |
< |
transient TreeNode first; // head of next-pointer list |
792 |
< |
|
793 |
< |
/* AQS overrides */ |
794 |
< |
public final boolean isHeldExclusively() { return getState() > 0; } |
795 |
< |
public final boolean tryAcquire(int ignore) { |
796 |
< |
if (compareAndSetState(0, 1)) { |
797 |
< |
setExclusiveOwnerThread(Thread.currentThread()); |
798 |
< |
return true; |
799 |
< |
} |
800 |
< |
return false; |
801 |
< |
} |
802 |
< |
public final boolean tryRelease(int ignore) { |
803 |
< |
setExclusiveOwnerThread(null); |
804 |
< |
setState(0); |
805 |
< |
return true; |
806 |
< |
} |
807 |
< |
public final int tryAcquireShared(int ignore) { |
808 |
< |
for (int c;;) { |
809 |
< |
if ((c = getState()) > 0) |
810 |
< |
return -1; |
811 |
< |
if (compareAndSetState(c, c -1)) |
812 |
< |
return 1; |
813 |
< |
} |
814 |
< |
} |
815 |
< |
public final boolean tryReleaseShared(int ignore) { |
816 |
< |
int c; |
817 |
< |
do {} while (!compareAndSetState(c = getState(), c + 1)); |
818 |
< |
return c == -1; |
819 |
< |
} |
820 |
< |
|
821 |
< |
/** From CLR */ |
822 |
< |
private void rotateLeft(TreeNode p) { |
823 |
< |
if (p != null) { |
824 |
< |
TreeNode r = p.right, pp, rl; |
825 |
< |
if ((rl = p.right = r.left) != null) |
826 |
< |
rl.parent = p; |
827 |
< |
if ((pp = r.parent = p.parent) == null) |
828 |
< |
root = r; |
829 |
< |
else if (pp.left == p) |
830 |
< |
pp.left = r; |
831 |
< |
else |
832 |
< |
pp.right = r; |
833 |
< |
r.left = p; |
834 |
< |
p.parent = r; |
835 |
< |
} |
836 |
< |
} |
837 |
< |
|
838 |
< |
/** From CLR */ |
839 |
< |
private void rotateRight(TreeNode p) { |
840 |
< |
if (p != null) { |
841 |
< |
TreeNode l = p.left, pp, lr; |
842 |
< |
if ((lr = p.left = l.right) != null) |
843 |
< |
lr.parent = p; |
844 |
< |
if ((pp = l.parent = p.parent) == null) |
845 |
< |
root = l; |
846 |
< |
else if (pp.right == p) |
847 |
< |
pp.right = l; |
848 |
< |
else |
849 |
< |
pp.left = l; |
850 |
< |
l.right = p; |
851 |
< |
p.parent = l; |
852 |
< |
} |
853 |
< |
} |
854 |
< |
|
855 |
< |
/** |
856 |
< |
* Returns the TreeNode (or null if not found) for the given key |
857 |
< |
* starting at given root. |
858 |
< |
*/ |
859 |
< |
@SuppressWarnings("unchecked") final TreeNode getTreeNode |
860 |
< |
(int h, Object k, TreeNode p) { |
861 |
< |
Class<?> c = k.getClass(); |
862 |
< |
while (p != null) { |
863 |
< |
int dir, ph; Object pk; Class<?> pc; |
864 |
< |
if ((ph = p.hash) == h) { |
865 |
< |
if ((pk = p.key) == k || k.equals(pk)) |
866 |
< |
return p; |
867 |
< |
if (c != (pc = pk.getClass()) || |
868 |
< |
!(k instanceof Comparable) || |
869 |
< |
(dir = ((Comparable)k).compareTo((Comparable)pk)) == 0) { |
870 |
< |
dir = (c == pc) ? 0 : c.getName().compareTo(pc.getName()); |
871 |
< |
TreeNode r = null, s = null, pl, pr; |
872 |
< |
if (dir >= 0) { |
873 |
< |
if ((pl = p.left) != null && h <= pl.hash) |
874 |
< |
s = pl; |
875 |
< |
} |
876 |
< |
else if ((pr = p.right) != null && h >= pr.hash) |
877 |
< |
s = pr; |
878 |
< |
if (s != null && (r = getTreeNode(h, k, s)) != null) |
879 |
< |
return r; |
880 |
< |
} |
881 |
< |
} |
882 |
< |
else |
883 |
< |
dir = (h < ph) ? -1 : 1; |
884 |
< |
p = (dir > 0) ? p.right : p.left; |
885 |
< |
} |
886 |
< |
return null; |
616 |
> |
public final K getKey() { return key; } |
617 |
> |
public final V getValue() { return val; } |
618 |
> |
public final int hashCode() { return key.hashCode() ^ val.hashCode(); } |
619 |
> |
public final String toString(){ return key + "=" + val; } |
620 |
> |
public final V setValue(V value) { |
621 |
> |
throw new UnsupportedOperationException(); |
622 |
|
} |
623 |
|
|
624 |
< |
/** |
625 |
< |
* Wrapper for getTreeNode used by CHM.get. Tries to obtain |
626 |
< |
* read-lock to call getTreeNode, but during failure to get |
627 |
< |
* lock, searches along next links. |
628 |
< |
*/ |
629 |
< |
final Object getValue(int h, Object k) { |
630 |
< |
Node r = null; |
896 |
< |
int c = getState(); // Must read lock state first |
897 |
< |
for (Node e = first; e != null; e = e.next) { |
898 |
< |
if (c <= 0 && compareAndSetState(c, c - 1)) { |
899 |
< |
try { |
900 |
< |
r = getTreeNode(h, k, root); |
901 |
< |
} finally { |
902 |
< |
releaseShared(0); |
903 |
< |
} |
904 |
< |
break; |
905 |
< |
} |
906 |
< |
else if ((e.hash & HASH_BITS) == h && k.equals(e.key)) { |
907 |
< |
r = e; |
908 |
< |
break; |
909 |
< |
} |
910 |
< |
else |
911 |
< |
c = getState(); |
912 |
< |
} |
913 |
< |
return r == null ? null : r.val; |
624 |
> |
public final boolean equals(Object o) { |
625 |
> |
Object k, v, u; Map.Entry<?,?> e; |
626 |
> |
return ((o instanceof Map.Entry) && |
627 |
> |
(k = (e = (Map.Entry<?,?>)o).getKey()) != null && |
628 |
> |
(v = e.getValue()) != null && |
629 |
> |
(k == key || k.equals(key)) && |
630 |
> |
(v == (u = val) || v.equals(u))); |
631 |
|
} |
632 |
|
|
633 |
|
/** |
634 |
< |
* Finds or adds a node. |
918 |
< |
* @return null if added |
634 |
> |
* Virtualized support for map.get(); overridden in subclasses. |
635 |
|
*/ |
636 |
< |
@SuppressWarnings("unchecked") final TreeNode putTreeNode |
637 |
< |
(int h, Object k, Object v) { |
638 |
< |
Class<?> c = k.getClass(); |
639 |
< |
TreeNode pp = root, p = null; |
640 |
< |
int dir = 0; |
641 |
< |
while (pp != null) { // find existing node or leaf to insert at |
642 |
< |
int ph; Object pk; Class<?> pc; |
643 |
< |
p = pp; |
644 |
< |
if ((ph = p.hash) == h) { |
929 |
< |
if ((pk = p.key) == k || k.equals(pk)) |
930 |
< |
return p; |
931 |
< |
if (c != (pc = pk.getClass()) || |
932 |
< |
!(k instanceof Comparable) || |
933 |
< |
(dir = ((Comparable)k).compareTo((Comparable)pk)) == 0) { |
934 |
< |
dir = (c == pc) ? 0 : c.getName().compareTo(pc.getName()); |
935 |
< |
TreeNode r = null, s = null, pl, pr; |
936 |
< |
if (dir >= 0) { |
937 |
< |
if ((pl = p.left) != null && h <= pl.hash) |
938 |
< |
s = pl; |
939 |
< |
} |
940 |
< |
else if ((pr = p.right) != null && h >= pr.hash) |
941 |
< |
s = pr; |
942 |
< |
if (s != null && (r = getTreeNode(h, k, s)) != null) |
943 |
< |
return r; |
944 |
< |
} |
945 |
< |
} |
946 |
< |
else |
947 |
< |
dir = (h < ph) ? -1 : 1; |
948 |
< |
pp = (dir > 0) ? p.right : p.left; |
949 |
< |
} |
950 |
< |
|
951 |
< |
TreeNode f = first; |
952 |
< |
TreeNode x = first = new TreeNode(h, k, v, f, p); |
953 |
< |
if (p == null) |
954 |
< |
root = x; |
955 |
< |
else { // attach and rebalance; adapted from CLR |
956 |
< |
TreeNode xp, xpp; |
957 |
< |
if (f != null) |
958 |
< |
f.prev = x; |
959 |
< |
if (dir <= 0) |
960 |
< |
p.left = x; |
961 |
< |
else |
962 |
< |
p.right = x; |
963 |
< |
x.red = true; |
964 |
< |
while (x != null && (xp = x.parent) != null && xp.red && |
965 |
< |
(xpp = xp.parent) != null) { |
966 |
< |
TreeNode xppl = xpp.left; |
967 |
< |
if (xp == xppl) { |
968 |
< |
TreeNode y = xpp.right; |
969 |
< |
if (y != null && y.red) { |
970 |
< |
y.red = false; |
971 |
< |
xp.red = false; |
972 |
< |
xpp.red = true; |
973 |
< |
x = xpp; |
974 |
< |
} |
975 |
< |
else { |
976 |
< |
if (x == xp.right) { |
977 |
< |
rotateLeft(x = xp); |
978 |
< |
xpp = (xp = x.parent) == null ? null : xp.parent; |
979 |
< |
} |
980 |
< |
if (xp != null) { |
981 |
< |
xp.red = false; |
982 |
< |
if (xpp != null) { |
983 |
< |
xpp.red = true; |
984 |
< |
rotateRight(xpp); |
985 |
< |
} |
986 |
< |
} |
987 |
< |
} |
988 |
< |
} |
989 |
< |
else { |
990 |
< |
TreeNode y = xppl; |
991 |
< |
if (y != null && y.red) { |
992 |
< |
y.red = false; |
993 |
< |
xp.red = false; |
994 |
< |
xpp.red = true; |
995 |
< |
x = xpp; |
996 |
< |
} |
997 |
< |
else { |
998 |
< |
if (x == xp.left) { |
999 |
< |
rotateRight(x = xp); |
1000 |
< |
xpp = (xp = x.parent) == null ? null : xp.parent; |
1001 |
< |
} |
1002 |
< |
if (xp != null) { |
1003 |
< |
xp.red = false; |
1004 |
< |
if (xpp != null) { |
1005 |
< |
xpp.red = true; |
1006 |
< |
rotateLeft(xpp); |
1007 |
< |
} |
1008 |
< |
} |
1009 |
< |
} |
1010 |
< |
} |
1011 |
< |
} |
1012 |
< |
TreeNode r = root; |
1013 |
< |
if (r != null && r.red) |
1014 |
< |
r.red = false; |
636 |
> |
Node<K,V> find(int h, Object k) { |
637 |
> |
Node<K,V> e = this; |
638 |
> |
if (k != null) { |
639 |
> |
do { |
640 |
> |
K ek; |
641 |
> |
if (e.hash == h && |
642 |
> |
((ek = e.key) == k || (ek != null && k.equals(ek)))) |
643 |
> |
return e; |
644 |
> |
} while ((e = e.next) != null); |
645 |
|
} |
646 |
|
return null; |
647 |
|
} |
1018 |
– |
|
1019 |
– |
/** |
1020 |
– |
* Removes the given node, that must be present before this |
1021 |
– |
* call. This is messier than typical red-black deletion code |
1022 |
– |
* because we cannot swap the contents of an interior node |
1023 |
– |
* with a leaf successor that is pinned by "next" pointers |
1024 |
– |
* that are accessible independently of lock. So instead we |
1025 |
– |
* swap the tree linkages. |
1026 |
– |
*/ |
1027 |
– |
final void deleteTreeNode(TreeNode p) { |
1028 |
– |
TreeNode next = (TreeNode)p.next; // unlink traversal pointers |
1029 |
– |
TreeNode pred = p.prev; |
1030 |
– |
if (pred == null) |
1031 |
– |
first = next; |
1032 |
– |
else |
1033 |
– |
pred.next = next; |
1034 |
– |
if (next != null) |
1035 |
– |
next.prev = pred; |
1036 |
– |
TreeNode replacement; |
1037 |
– |
TreeNode pl = p.left; |
1038 |
– |
TreeNode pr = p.right; |
1039 |
– |
if (pl != null && pr != null) { |
1040 |
– |
TreeNode s = pr, sl; |
1041 |
– |
while ((sl = s.left) != null) // find successor |
1042 |
– |
s = sl; |
1043 |
– |
boolean c = s.red; s.red = p.red; p.red = c; // swap colors |
1044 |
– |
TreeNode sr = s.right; |
1045 |
– |
TreeNode pp = p.parent; |
1046 |
– |
if (s == pr) { // p was s's direct parent |
1047 |
– |
p.parent = s; |
1048 |
– |
s.right = p; |
1049 |
– |
} |
1050 |
– |
else { |
1051 |
– |
TreeNode sp = s.parent; |
1052 |
– |
if ((p.parent = sp) != null) { |
1053 |
– |
if (s == sp.left) |
1054 |
– |
sp.left = p; |
1055 |
– |
else |
1056 |
– |
sp.right = p; |
1057 |
– |
} |
1058 |
– |
if ((s.right = pr) != null) |
1059 |
– |
pr.parent = s; |
1060 |
– |
} |
1061 |
– |
p.left = null; |
1062 |
– |
if ((p.right = sr) != null) |
1063 |
– |
sr.parent = p; |
1064 |
– |
if ((s.left = pl) != null) |
1065 |
– |
pl.parent = s; |
1066 |
– |
if ((s.parent = pp) == null) |
1067 |
– |
root = s; |
1068 |
– |
else if (p == pp.left) |
1069 |
– |
pp.left = s; |
1070 |
– |
else |
1071 |
– |
pp.right = s; |
1072 |
– |
replacement = sr; |
1073 |
– |
} |
1074 |
– |
else |
1075 |
– |
replacement = (pl != null) ? pl : pr; |
1076 |
– |
TreeNode pp = p.parent; |
1077 |
– |
if (replacement == null) { |
1078 |
– |
if (pp == null) { |
1079 |
– |
root = null; |
1080 |
– |
return; |
1081 |
– |
} |
1082 |
– |
replacement = p; |
1083 |
– |
} |
1084 |
– |
else { |
1085 |
– |
replacement.parent = pp; |
1086 |
– |
if (pp == null) |
1087 |
– |
root = replacement; |
1088 |
– |
else if (p == pp.left) |
1089 |
– |
pp.left = replacement; |
1090 |
– |
else |
1091 |
– |
pp.right = replacement; |
1092 |
– |
p.left = p.right = p.parent = null; |
1093 |
– |
} |
1094 |
– |
if (!p.red) { // rebalance, from CLR |
1095 |
– |
TreeNode x = replacement; |
1096 |
– |
while (x != null) { |
1097 |
– |
TreeNode xp, xpl; |
1098 |
– |
if (x.red || (xp = x.parent) == null) { |
1099 |
– |
x.red = false; |
1100 |
– |
break; |
1101 |
– |
} |
1102 |
– |
if (x == (xpl = xp.left)) { |
1103 |
– |
TreeNode sib = xp.right; |
1104 |
– |
if (sib != null && sib.red) { |
1105 |
– |
sib.red = false; |
1106 |
– |
xp.red = true; |
1107 |
– |
rotateLeft(xp); |
1108 |
– |
sib = (xp = x.parent) == null ? null : xp.right; |
1109 |
– |
} |
1110 |
– |
if (sib == null) |
1111 |
– |
x = xp; |
1112 |
– |
else { |
1113 |
– |
TreeNode sl = sib.left, sr = sib.right; |
1114 |
– |
if ((sr == null || !sr.red) && |
1115 |
– |
(sl == null || !sl.red)) { |
1116 |
– |
sib.red = true; |
1117 |
– |
x = xp; |
1118 |
– |
} |
1119 |
– |
else { |
1120 |
– |
if (sr == null || !sr.red) { |
1121 |
– |
if (sl != null) |
1122 |
– |
sl.red = false; |
1123 |
– |
sib.red = true; |
1124 |
– |
rotateRight(sib); |
1125 |
– |
sib = (xp = x.parent) == null ? null : xp.right; |
1126 |
– |
} |
1127 |
– |
if (sib != null) { |
1128 |
– |
sib.red = (xp == null) ? false : xp.red; |
1129 |
– |
if ((sr = sib.right) != null) |
1130 |
– |
sr.red = false; |
1131 |
– |
} |
1132 |
– |
if (xp != null) { |
1133 |
– |
xp.red = false; |
1134 |
– |
rotateLeft(xp); |
1135 |
– |
} |
1136 |
– |
x = root; |
1137 |
– |
} |
1138 |
– |
} |
1139 |
– |
} |
1140 |
– |
else { // symmetric |
1141 |
– |
TreeNode sib = xpl; |
1142 |
– |
if (sib != null && sib.red) { |
1143 |
– |
sib.red = false; |
1144 |
– |
xp.red = true; |
1145 |
– |
rotateRight(xp); |
1146 |
– |
sib = (xp = x.parent) == null ? null : xp.left; |
1147 |
– |
} |
1148 |
– |
if (sib == null) |
1149 |
– |
x = xp; |
1150 |
– |
else { |
1151 |
– |
TreeNode sl = sib.left, sr = sib.right; |
1152 |
– |
if ((sl == null || !sl.red) && |
1153 |
– |
(sr == null || !sr.red)) { |
1154 |
– |
sib.red = true; |
1155 |
– |
x = xp; |
1156 |
– |
} |
1157 |
– |
else { |
1158 |
– |
if (sl == null || !sl.red) { |
1159 |
– |
if (sr != null) |
1160 |
– |
sr.red = false; |
1161 |
– |
sib.red = true; |
1162 |
– |
rotateLeft(sib); |
1163 |
– |
sib = (xp = x.parent) == null ? null : xp.left; |
1164 |
– |
} |
1165 |
– |
if (sib != null) { |
1166 |
– |
sib.red = (xp == null) ? false : xp.red; |
1167 |
– |
if ((sl = sib.left) != null) |
1168 |
– |
sl.red = false; |
1169 |
– |
} |
1170 |
– |
if (xp != null) { |
1171 |
– |
xp.red = false; |
1172 |
– |
rotateRight(xp); |
1173 |
– |
} |
1174 |
– |
x = root; |
1175 |
– |
} |
1176 |
– |
} |
1177 |
– |
} |
1178 |
– |
} |
1179 |
– |
} |
1180 |
– |
if (p == replacement && (pp = p.parent) != null) { |
1181 |
– |
if (p == pp.left) // detach pointers |
1182 |
– |
pp.left = null; |
1183 |
– |
else if (p == pp.right) |
1184 |
– |
pp.right = null; |
1185 |
– |
p.parent = null; |
1186 |
– |
} |
1187 |
– |
} |
648 |
|
} |
649 |
|
|
650 |
< |
/* ---------------- Collision reduction methods -------------- */ |
650 |
> |
/* ---------------- Static utilities -------------- */ |
651 |
|
|
652 |
|
/** |
653 |
< |
* Spreads higher bits to lower, and also forces top 2 bits to 0. |
654 |
< |
* Because the table uses power-of-two masking, sets of hashes |
655 |
< |
* that vary only in bits above the current mask will always |
656 |
< |
* collide. (Among known examples are sets of Float keys holding |
657 |
< |
* consecutive whole numbers in small tables.) To counter this, |
658 |
< |
* we apply a transform that spreads the impact of higher bits |
653 |
> |
* Spreads (XORs) higher bits of hash to lower and also forces top |
654 |
> |
* bit to 0. Because the table uses power-of-two masking, sets of |
655 |
> |
* hashes that vary only in bits above the current mask will |
656 |
> |
* always collide. (Among known examples are sets of Float keys |
657 |
> |
* holding consecutive whole numbers in small tables.) So we |
658 |
> |
* apply a transform that spreads the impact of higher bits |
659 |
|
* downward. There is a tradeoff between speed, utility, and |
660 |
|
* quality of bit-spreading. Because many common sets of hashes |
661 |
< |
* are already reasonably distributed across bits (so don't benefit |
662 |
< |
* from spreading), and because we use trees to handle large sets |
663 |
< |
* of collisions in bins, we don't need excessively high quality. |
664 |
< |
*/ |
665 |
< |
private static final int spread(int h) { |
666 |
< |
h ^= (h >>> 18) ^ (h >>> 12); |
1207 |
< |
return (h ^ (h >>> 10)) & HASH_BITS; |
1208 |
< |
} |
1209 |
< |
|
1210 |
< |
/** |
1211 |
< |
* Replaces a list bin with a tree bin. Call only when locked. |
1212 |
< |
* Fails to replace if the given key is non-comparable or table |
1213 |
< |
* is, or needs, resizing. |
661 |
> |
* are already reasonably distributed (so don't benefit from |
662 |
> |
* spreading), and because we use trees to handle large sets of |
663 |
> |
* collisions in bins, we just XOR some shifted bits in the |
664 |
> |
* cheapest possible way to reduce systematic lossage, as well as |
665 |
> |
* to incorporate impact of the highest bits that would otherwise |
666 |
> |
* never be used in index calculations because of table bounds. |
667 |
|
*/ |
668 |
< |
private final void replaceWithTreeBin(Node[] tab, int index, Object key) { |
669 |
< |
if ((key instanceof Comparable) && |
1217 |
< |
(tab.length >= MAXIMUM_CAPACITY || counter.sum() < (long)sizeCtl)) { |
1218 |
< |
TreeBin t = new TreeBin(); |
1219 |
< |
for (Node e = tabAt(tab, index); e != null; e = e.next) |
1220 |
< |
t.putTreeNode(e.hash & HASH_BITS, e.key, e.val); |
1221 |
< |
setTabAt(tab, index, new Node(MOVED, t, null, null)); |
1222 |
< |
} |
1223 |
< |
} |
1224 |
< |
|
1225 |
< |
/* ---------------- Internal access and update methods -------------- */ |
1226 |
< |
|
1227 |
< |
/** Implementation for get and containsKey */ |
1228 |
< |
private final Object internalGet(Object k) { |
1229 |
< |
int h = spread(k.hashCode()); |
1230 |
< |
retry: for (Node[] tab = table; tab != null;) { |
1231 |
< |
Node e, p; Object ek, ev; int eh; // locals to read fields once |
1232 |
< |
for (e = tabAt(tab, (tab.length - 1) & h); e != null; e = e.next) { |
1233 |
< |
if ((eh = e.hash) == MOVED) { |
1234 |
< |
if ((ek = e.key) instanceof TreeBin) // search TreeBin |
1235 |
< |
return ((TreeBin)ek).getValue(h, k); |
1236 |
< |
else { // restart with new table |
1237 |
< |
tab = (Node[])ek; |
1238 |
< |
continue retry; |
1239 |
< |
} |
1240 |
< |
} |
1241 |
< |
else if ((eh & HASH_BITS) == h && (ev = e.val) != null && |
1242 |
< |
((ek = e.key) == k || k.equals(ek))) |
1243 |
< |
return ev; |
1244 |
< |
} |
1245 |
< |
break; |
1246 |
< |
} |
1247 |
< |
return null; |
668 |
> |
static final int spread(int h) { |
669 |
> |
return (h ^ (h >>> 16)) & HASH_BITS; |
670 |
|
} |
671 |
|
|
672 |
|
/** |
1251 |
– |
* Implementation for the four public remove/replace methods: |
1252 |
– |
* Replaces node value with v, conditional upon match of cv if |
1253 |
– |
* non-null. If resulting value is null, delete. |
1254 |
– |
*/ |
1255 |
– |
private final Object internalReplace(Object k, Object v, Object cv) { |
1256 |
– |
int h = spread(k.hashCode()); |
1257 |
– |
Object oldVal = null; |
1258 |
– |
for (Node[] tab = table;;) { |
1259 |
– |
Node f; int i, fh; Object fk; |
1260 |
– |
if (tab == null || |
1261 |
– |
(f = tabAt(tab, i = (tab.length - 1) & h)) == null) |
1262 |
– |
break; |
1263 |
– |
else if ((fh = f.hash) == MOVED) { |
1264 |
– |
if ((fk = f.key) instanceof TreeBin) { |
1265 |
– |
TreeBin t = (TreeBin)fk; |
1266 |
– |
boolean validated = false; |
1267 |
– |
boolean deleted = false; |
1268 |
– |
t.acquire(0); |
1269 |
– |
try { |
1270 |
– |
if (tabAt(tab, i) == f) { |
1271 |
– |
validated = true; |
1272 |
– |
TreeNode p = t.getTreeNode(h, k, t.root); |
1273 |
– |
if (p != null) { |
1274 |
– |
Object pv = p.val; |
1275 |
– |
if (cv == null || cv == pv || cv.equals(pv)) { |
1276 |
– |
oldVal = pv; |
1277 |
– |
if ((p.val = v) == null) { |
1278 |
– |
deleted = true; |
1279 |
– |
t.deleteTreeNode(p); |
1280 |
– |
} |
1281 |
– |
} |
1282 |
– |
} |
1283 |
– |
} |
1284 |
– |
} finally { |
1285 |
– |
t.release(0); |
1286 |
– |
} |
1287 |
– |
if (validated) { |
1288 |
– |
if (deleted) |
1289 |
– |
counter.add(-1L); |
1290 |
– |
break; |
1291 |
– |
} |
1292 |
– |
} |
1293 |
– |
else |
1294 |
– |
tab = (Node[])fk; |
1295 |
– |
} |
1296 |
– |
else if ((fh & HASH_BITS) != h && f.next == null) // precheck |
1297 |
– |
break; // rules out possible existence |
1298 |
– |
else if ((fh & LOCKED) != 0) { |
1299 |
– |
checkForResize(); // try resizing if can't get lock |
1300 |
– |
f.tryAwaitLock(tab, i); |
1301 |
– |
} |
1302 |
– |
else if (f.casHash(fh, fh | LOCKED)) { |
1303 |
– |
boolean validated = false; |
1304 |
– |
boolean deleted = false; |
1305 |
– |
try { |
1306 |
– |
if (tabAt(tab, i) == f) { |
1307 |
– |
validated = true; |
1308 |
– |
for (Node e = f, pred = null;;) { |
1309 |
– |
Object ek, ev; |
1310 |
– |
if ((e.hash & HASH_BITS) == h && |
1311 |
– |
((ev = e.val) != null) && |
1312 |
– |
((ek = e.key) == k || k.equals(ek))) { |
1313 |
– |
if (cv == null || cv == ev || cv.equals(ev)) { |
1314 |
– |
oldVal = ev; |
1315 |
– |
if ((e.val = v) == null) { |
1316 |
– |
deleted = true; |
1317 |
– |
Node en = e.next; |
1318 |
– |
if (pred != null) |
1319 |
– |
pred.next = en; |
1320 |
– |
else |
1321 |
– |
setTabAt(tab, i, en); |
1322 |
– |
} |
1323 |
– |
} |
1324 |
– |
break; |
1325 |
– |
} |
1326 |
– |
pred = e; |
1327 |
– |
if ((e = e.next) == null) |
1328 |
– |
break; |
1329 |
– |
} |
1330 |
– |
} |
1331 |
– |
} finally { |
1332 |
– |
if (!f.casHash(fh | LOCKED, fh)) { |
1333 |
– |
f.hash = fh; |
1334 |
– |
synchronized (f) { f.notifyAll(); }; |
1335 |
– |
} |
1336 |
– |
} |
1337 |
– |
if (validated) { |
1338 |
– |
if (deleted) |
1339 |
– |
counter.add(-1L); |
1340 |
– |
break; |
1341 |
– |
} |
1342 |
– |
} |
1343 |
– |
} |
1344 |
– |
return oldVal; |
1345 |
– |
} |
1346 |
– |
|
1347 |
– |
/* |
1348 |
– |
* Internal versions of the six insertion methods, each a |
1349 |
– |
* little more complicated than the last. All have |
1350 |
– |
* the same basic structure as the first (internalPut): |
1351 |
– |
* 1. If table uninitialized, create |
1352 |
– |
* 2. If bin empty, try to CAS new node |
1353 |
– |
* 3. If bin stale, use new table |
1354 |
– |
* 4. if bin converted to TreeBin, validate and relay to TreeBin methods |
1355 |
– |
* 5. Lock and validate; if valid, scan and add or update |
1356 |
– |
* |
1357 |
– |
* The others interweave other checks and/or alternative actions: |
1358 |
– |
* * Plain put checks for and performs resize after insertion. |
1359 |
– |
* * putIfAbsent prescans for mapping without lock (and fails to add |
1360 |
– |
* if present), which also makes pre-emptive resize checks worthwhile. |
1361 |
– |
* * computeIfAbsent extends form used in putIfAbsent with additional |
1362 |
– |
* mechanics to deal with, calls, potential exceptions and null |
1363 |
– |
* returns from function call. |
1364 |
– |
* * compute uses the same function-call mechanics, but without |
1365 |
– |
* the prescans |
1366 |
– |
* * merge acts as putIfAbsent in the absent case, but invokes the |
1367 |
– |
* update function if present |
1368 |
– |
* * putAll attempts to pre-allocate enough table space |
1369 |
– |
* and more lazily performs count updates and checks. |
1370 |
– |
* |
1371 |
– |
* Someday when details settle down a bit more, it might be worth |
1372 |
– |
* some factoring to reduce sprawl. |
1373 |
– |
*/ |
1374 |
– |
|
1375 |
– |
/** Implementation for put */ |
1376 |
– |
private final Object internalPut(Object k, Object v) { |
1377 |
– |
int h = spread(k.hashCode()); |
1378 |
– |
int count = 0; |
1379 |
– |
for (Node[] tab = table;;) { |
1380 |
– |
int i; Node f; int fh; Object fk; |
1381 |
– |
if (tab == null) |
1382 |
– |
tab = initTable(); |
1383 |
– |
else if ((f = tabAt(tab, i = (tab.length - 1) & h)) == null) { |
1384 |
– |
if (casTabAt(tab, i, null, new Node(h, k, v, null))) |
1385 |
– |
break; // no lock when adding to empty bin |
1386 |
– |
} |
1387 |
– |
else if ((fh = f.hash) == MOVED) { |
1388 |
– |
if ((fk = f.key) instanceof TreeBin) { |
1389 |
– |
TreeBin t = (TreeBin)fk; |
1390 |
– |
Object oldVal = null; |
1391 |
– |
t.acquire(0); |
1392 |
– |
try { |
1393 |
– |
if (tabAt(tab, i) == f) { |
1394 |
– |
count = 2; |
1395 |
– |
TreeNode p = t.putTreeNode(h, k, v); |
1396 |
– |
if (p != null) { |
1397 |
– |
oldVal = p.val; |
1398 |
– |
p.val = v; |
1399 |
– |
} |
1400 |
– |
} |
1401 |
– |
} finally { |
1402 |
– |
t.release(0); |
1403 |
– |
} |
1404 |
– |
if (count != 0) { |
1405 |
– |
if (oldVal != null) |
1406 |
– |
return oldVal; |
1407 |
– |
break; |
1408 |
– |
} |
1409 |
– |
} |
1410 |
– |
else |
1411 |
– |
tab = (Node[])fk; |
1412 |
– |
} |
1413 |
– |
else if ((fh & LOCKED) != 0) { |
1414 |
– |
checkForResize(); |
1415 |
– |
f.tryAwaitLock(tab, i); |
1416 |
– |
} |
1417 |
– |
else if (f.casHash(fh, fh | LOCKED)) { |
1418 |
– |
Object oldVal = null; |
1419 |
– |
try { // needed in case equals() throws |
1420 |
– |
if (tabAt(tab, i) == f) { |
1421 |
– |
count = 1; |
1422 |
– |
for (Node e = f;; ++count) { |
1423 |
– |
Object ek, ev; |
1424 |
– |
if ((e.hash & HASH_BITS) == h && |
1425 |
– |
(ev = e.val) != null && |
1426 |
– |
((ek = e.key) == k || k.equals(ek))) { |
1427 |
– |
oldVal = ev; |
1428 |
– |
e.val = v; |
1429 |
– |
break; |
1430 |
– |
} |
1431 |
– |
Node last = e; |
1432 |
– |
if ((e = e.next) == null) { |
1433 |
– |
last.next = new Node(h, k, v, null); |
1434 |
– |
if (count >= TREE_THRESHOLD) |
1435 |
– |
replaceWithTreeBin(tab, i, k); |
1436 |
– |
break; |
1437 |
– |
} |
1438 |
– |
} |
1439 |
– |
} |
1440 |
– |
} finally { // unlock and signal if needed |
1441 |
– |
if (!f.casHash(fh | LOCKED, fh)) { |
1442 |
– |
f.hash = fh; |
1443 |
– |
synchronized (f) { f.notifyAll(); }; |
1444 |
– |
} |
1445 |
– |
} |
1446 |
– |
if (count != 0) { |
1447 |
– |
if (oldVal != null) |
1448 |
– |
return oldVal; |
1449 |
– |
if (tab.length <= 64) |
1450 |
– |
count = 2; |
1451 |
– |
break; |
1452 |
– |
} |
1453 |
– |
} |
1454 |
– |
} |
1455 |
– |
counter.add(1L); |
1456 |
– |
if (count > 1) |
1457 |
– |
checkForResize(); |
1458 |
– |
return null; |
1459 |
– |
} |
1460 |
– |
|
1461 |
– |
/** Implementation for putIfAbsent */ |
1462 |
– |
private final Object internalPutIfAbsent(Object k, Object v) { |
1463 |
– |
int h = spread(k.hashCode()); |
1464 |
– |
int count = 0; |
1465 |
– |
for (Node[] tab = table;;) { |
1466 |
– |
int i; Node f; int fh; Object fk, fv; |
1467 |
– |
if (tab == null) |
1468 |
– |
tab = initTable(); |
1469 |
– |
else if ((f = tabAt(tab, i = (tab.length - 1) & h)) == null) { |
1470 |
– |
if (casTabAt(tab, i, null, new Node(h, k, v, null))) |
1471 |
– |
break; |
1472 |
– |
} |
1473 |
– |
else if ((fh = f.hash) == MOVED) { |
1474 |
– |
if ((fk = f.key) instanceof TreeBin) { |
1475 |
– |
TreeBin t = (TreeBin)fk; |
1476 |
– |
Object oldVal = null; |
1477 |
– |
t.acquire(0); |
1478 |
– |
try { |
1479 |
– |
if (tabAt(tab, i) == f) { |
1480 |
– |
count = 2; |
1481 |
– |
TreeNode p = t.putTreeNode(h, k, v); |
1482 |
– |
if (p != null) |
1483 |
– |
oldVal = p.val; |
1484 |
– |
} |
1485 |
– |
} finally { |
1486 |
– |
t.release(0); |
1487 |
– |
} |
1488 |
– |
if (count != 0) { |
1489 |
– |
if (oldVal != null) |
1490 |
– |
return oldVal; |
1491 |
– |
break; |
1492 |
– |
} |
1493 |
– |
} |
1494 |
– |
else |
1495 |
– |
tab = (Node[])fk; |
1496 |
– |
} |
1497 |
– |
else if ((fh & HASH_BITS) == h && (fv = f.val) != null && |
1498 |
– |
((fk = f.key) == k || k.equals(fk))) |
1499 |
– |
return fv; |
1500 |
– |
else { |
1501 |
– |
Node g = f.next; |
1502 |
– |
if (g != null) { // at least 2 nodes -- search and maybe resize |
1503 |
– |
for (Node e = g;;) { |
1504 |
– |
Object ek, ev; |
1505 |
– |
if ((e.hash & HASH_BITS) == h && (ev = e.val) != null && |
1506 |
– |
((ek = e.key) == k || k.equals(ek))) |
1507 |
– |
return ev; |
1508 |
– |
if ((e = e.next) == null) { |
1509 |
– |
checkForResize(); |
1510 |
– |
break; |
1511 |
– |
} |
1512 |
– |
} |
1513 |
– |
} |
1514 |
– |
if (((fh = f.hash) & LOCKED) != 0) { |
1515 |
– |
checkForResize(); |
1516 |
– |
f.tryAwaitLock(tab, i); |
1517 |
– |
} |
1518 |
– |
else if (tabAt(tab, i) == f && f.casHash(fh, fh | LOCKED)) { |
1519 |
– |
Object oldVal = null; |
1520 |
– |
try { |
1521 |
– |
if (tabAt(tab, i) == f) { |
1522 |
– |
count = 1; |
1523 |
– |
for (Node e = f;; ++count) { |
1524 |
– |
Object ek, ev; |
1525 |
– |
if ((e.hash & HASH_BITS) == h && |
1526 |
– |
(ev = e.val) != null && |
1527 |
– |
((ek = e.key) == k || k.equals(ek))) { |
1528 |
– |
oldVal = ev; |
1529 |
– |
break; |
1530 |
– |
} |
1531 |
– |
Node last = e; |
1532 |
– |
if ((e = e.next) == null) { |
1533 |
– |
last.next = new Node(h, k, v, null); |
1534 |
– |
if (count >= TREE_THRESHOLD) |
1535 |
– |
replaceWithTreeBin(tab, i, k); |
1536 |
– |
break; |
1537 |
– |
} |
1538 |
– |
} |
1539 |
– |
} |
1540 |
– |
} finally { |
1541 |
– |
if (!f.casHash(fh | LOCKED, fh)) { |
1542 |
– |
f.hash = fh; |
1543 |
– |
synchronized (f) { f.notifyAll(); }; |
1544 |
– |
} |
1545 |
– |
} |
1546 |
– |
if (count != 0) { |
1547 |
– |
if (oldVal != null) |
1548 |
– |
return oldVal; |
1549 |
– |
if (tab.length <= 64) |
1550 |
– |
count = 2; |
1551 |
– |
break; |
1552 |
– |
} |
1553 |
– |
} |
1554 |
– |
} |
1555 |
– |
} |
1556 |
– |
counter.add(1L); |
1557 |
– |
if (count > 1) |
1558 |
– |
checkForResize(); |
1559 |
– |
return null; |
1560 |
– |
} |
1561 |
– |
|
1562 |
– |
/** Implementation for computeIfAbsent */ |
1563 |
– |
private final Object internalComputeIfAbsent(K k, |
1564 |
– |
Fun<? super K, ?> mf) { |
1565 |
– |
int h = spread(k.hashCode()); |
1566 |
– |
Object val = null; |
1567 |
– |
int count = 0; |
1568 |
– |
for (Node[] tab = table;;) { |
1569 |
– |
Node f; int i, fh; Object fk, fv; |
1570 |
– |
if (tab == null) |
1571 |
– |
tab = initTable(); |
1572 |
– |
else if ((f = tabAt(tab, i = (tab.length - 1) & h)) == null) { |
1573 |
– |
Node node = new Node(fh = h | LOCKED, k, null, null); |
1574 |
– |
if (casTabAt(tab, i, null, node)) { |
1575 |
– |
count = 1; |
1576 |
– |
try { |
1577 |
– |
if ((val = mf.apply(k)) != null) |
1578 |
– |
node.val = val; |
1579 |
– |
} finally { |
1580 |
– |
if (val == null) |
1581 |
– |
setTabAt(tab, i, null); |
1582 |
– |
if (!node.casHash(fh, h)) { |
1583 |
– |
node.hash = h; |
1584 |
– |
synchronized (node) { node.notifyAll(); }; |
1585 |
– |
} |
1586 |
– |
} |
1587 |
– |
} |
1588 |
– |
if (count != 0) |
1589 |
– |
break; |
1590 |
– |
} |
1591 |
– |
else if ((fh = f.hash) == MOVED) { |
1592 |
– |
if ((fk = f.key) instanceof TreeBin) { |
1593 |
– |
TreeBin t = (TreeBin)fk; |
1594 |
– |
boolean added = false; |
1595 |
– |
t.acquire(0); |
1596 |
– |
try { |
1597 |
– |
if (tabAt(tab, i) == f) { |
1598 |
– |
count = 1; |
1599 |
– |
TreeNode p = t.getTreeNode(h, k, t.root); |
1600 |
– |
if (p != null) |
1601 |
– |
val = p.val; |
1602 |
– |
else if ((val = mf.apply(k)) != null) { |
1603 |
– |
added = true; |
1604 |
– |
count = 2; |
1605 |
– |
t.putTreeNode(h, k, val); |
1606 |
– |
} |
1607 |
– |
} |
1608 |
– |
} finally { |
1609 |
– |
t.release(0); |
1610 |
– |
} |
1611 |
– |
if (count != 0) { |
1612 |
– |
if (!added) |
1613 |
– |
return val; |
1614 |
– |
break; |
1615 |
– |
} |
1616 |
– |
} |
1617 |
– |
else |
1618 |
– |
tab = (Node[])fk; |
1619 |
– |
} |
1620 |
– |
else if ((fh & HASH_BITS) == h && (fv = f.val) != null && |
1621 |
– |
((fk = f.key) == k || k.equals(fk))) |
1622 |
– |
return fv; |
1623 |
– |
else { |
1624 |
– |
Node g = f.next; |
1625 |
– |
if (g != null) { |
1626 |
– |
for (Node e = g;;) { |
1627 |
– |
Object ek, ev; |
1628 |
– |
if ((e.hash & HASH_BITS) == h && (ev = e.val) != null && |
1629 |
– |
((ek = e.key) == k || k.equals(ek))) |
1630 |
– |
return ev; |
1631 |
– |
if ((e = e.next) == null) { |
1632 |
– |
checkForResize(); |
1633 |
– |
break; |
1634 |
– |
} |
1635 |
– |
} |
1636 |
– |
} |
1637 |
– |
if (((fh = f.hash) & LOCKED) != 0) { |
1638 |
– |
checkForResize(); |
1639 |
– |
f.tryAwaitLock(tab, i); |
1640 |
– |
} |
1641 |
– |
else if (tabAt(tab, i) == f && f.casHash(fh, fh | LOCKED)) { |
1642 |
– |
boolean added = false; |
1643 |
– |
try { |
1644 |
– |
if (tabAt(tab, i) == f) { |
1645 |
– |
count = 1; |
1646 |
– |
for (Node e = f;; ++count) { |
1647 |
– |
Object ek, ev; |
1648 |
– |
if ((e.hash & HASH_BITS) == h && |
1649 |
– |
(ev = e.val) != null && |
1650 |
– |
((ek = e.key) == k || k.equals(ek))) { |
1651 |
– |
val = ev; |
1652 |
– |
break; |
1653 |
– |
} |
1654 |
– |
Node last = e; |
1655 |
– |
if ((e = e.next) == null) { |
1656 |
– |
if ((val = mf.apply(k)) != null) { |
1657 |
– |
added = true; |
1658 |
– |
last.next = new Node(h, k, val, null); |
1659 |
– |
if (count >= TREE_THRESHOLD) |
1660 |
– |
replaceWithTreeBin(tab, i, k); |
1661 |
– |
} |
1662 |
– |
break; |
1663 |
– |
} |
1664 |
– |
} |
1665 |
– |
} |
1666 |
– |
} finally { |
1667 |
– |
if (!f.casHash(fh | LOCKED, fh)) { |
1668 |
– |
f.hash = fh; |
1669 |
– |
synchronized (f) { f.notifyAll(); }; |
1670 |
– |
} |
1671 |
– |
} |
1672 |
– |
if (count != 0) { |
1673 |
– |
if (!added) |
1674 |
– |
return val; |
1675 |
– |
if (tab.length <= 64) |
1676 |
– |
count = 2; |
1677 |
– |
break; |
1678 |
– |
} |
1679 |
– |
} |
1680 |
– |
} |
1681 |
– |
} |
1682 |
– |
if (val != null) { |
1683 |
– |
counter.add(1L); |
1684 |
– |
if (count > 1) |
1685 |
– |
checkForResize(); |
1686 |
– |
} |
1687 |
– |
return val; |
1688 |
– |
} |
1689 |
– |
|
1690 |
– |
/** Implementation for compute */ |
1691 |
– |
@SuppressWarnings("unchecked") private final Object internalCompute |
1692 |
– |
(K k, boolean onlyIfPresent, BiFun<? super K, ? super V, ? extends V> mf) { |
1693 |
– |
int h = spread(k.hashCode()); |
1694 |
– |
Object val = null; |
1695 |
– |
int delta = 0; |
1696 |
– |
int count = 0; |
1697 |
– |
for (Node[] tab = table;;) { |
1698 |
– |
Node f; int i, fh; Object fk; |
1699 |
– |
if (tab == null) |
1700 |
– |
tab = initTable(); |
1701 |
– |
else if ((f = tabAt(tab, i = (tab.length - 1) & h)) == null) { |
1702 |
– |
if (onlyIfPresent) |
1703 |
– |
break; |
1704 |
– |
Node node = new Node(fh = h | LOCKED, k, null, null); |
1705 |
– |
if (casTabAt(tab, i, null, node)) { |
1706 |
– |
try { |
1707 |
– |
count = 1; |
1708 |
– |
if ((val = mf.apply(k, null)) != null) { |
1709 |
– |
node.val = val; |
1710 |
– |
delta = 1; |
1711 |
– |
} |
1712 |
– |
} finally { |
1713 |
– |
if (delta == 0) |
1714 |
– |
setTabAt(tab, i, null); |
1715 |
– |
if (!node.casHash(fh, h)) { |
1716 |
– |
node.hash = h; |
1717 |
– |
synchronized (node) { node.notifyAll(); }; |
1718 |
– |
} |
1719 |
– |
} |
1720 |
– |
} |
1721 |
– |
if (count != 0) |
1722 |
– |
break; |
1723 |
– |
} |
1724 |
– |
else if ((fh = f.hash) == MOVED) { |
1725 |
– |
if ((fk = f.key) instanceof TreeBin) { |
1726 |
– |
TreeBin t = (TreeBin)fk; |
1727 |
– |
t.acquire(0); |
1728 |
– |
try { |
1729 |
– |
if (tabAt(tab, i) == f) { |
1730 |
– |
count = 1; |
1731 |
– |
TreeNode p = t.getTreeNode(h, k, t.root); |
1732 |
– |
Object pv = (p == null) ? null : p.val; |
1733 |
– |
if ((val = mf.apply(k, (V)pv)) != null) { |
1734 |
– |
if (p != null) |
1735 |
– |
p.val = val; |
1736 |
– |
else { |
1737 |
– |
count = 2; |
1738 |
– |
delta = 1; |
1739 |
– |
t.putTreeNode(h, k, val); |
1740 |
– |
} |
1741 |
– |
} |
1742 |
– |
else if (p != null) { |
1743 |
– |
delta = -1; |
1744 |
– |
t.deleteTreeNode(p); |
1745 |
– |
} |
1746 |
– |
} |
1747 |
– |
} finally { |
1748 |
– |
t.release(0); |
1749 |
– |
} |
1750 |
– |
if (count != 0) |
1751 |
– |
break; |
1752 |
– |
} |
1753 |
– |
else |
1754 |
– |
tab = (Node[])fk; |
1755 |
– |
} |
1756 |
– |
else if ((fh & LOCKED) != 0) { |
1757 |
– |
checkForResize(); |
1758 |
– |
f.tryAwaitLock(tab, i); |
1759 |
– |
} |
1760 |
– |
else if (f.casHash(fh, fh | LOCKED)) { |
1761 |
– |
try { |
1762 |
– |
if (tabAt(tab, i) == f) { |
1763 |
– |
count = 1; |
1764 |
– |
for (Node e = f, pred = null;; ++count) { |
1765 |
– |
Object ek, ev; |
1766 |
– |
if ((e.hash & HASH_BITS) == h && |
1767 |
– |
(ev = e.val) != null && |
1768 |
– |
((ek = e.key) == k || k.equals(ek))) { |
1769 |
– |
val = mf.apply(k, (V)ev); |
1770 |
– |
if (val != null) |
1771 |
– |
e.val = val; |
1772 |
– |
else { |
1773 |
– |
delta = -1; |
1774 |
– |
Node en = e.next; |
1775 |
– |
if (pred != null) |
1776 |
– |
pred.next = en; |
1777 |
– |
else |
1778 |
– |
setTabAt(tab, i, en); |
1779 |
– |
} |
1780 |
– |
break; |
1781 |
– |
} |
1782 |
– |
pred = e; |
1783 |
– |
if ((e = e.next) == null) { |
1784 |
– |
if (!onlyIfPresent && (val = mf.apply(k, null)) != null) { |
1785 |
– |
pred.next = new Node(h, k, val, null); |
1786 |
– |
delta = 1; |
1787 |
– |
if (count >= TREE_THRESHOLD) |
1788 |
– |
replaceWithTreeBin(tab, i, k); |
1789 |
– |
} |
1790 |
– |
break; |
1791 |
– |
} |
1792 |
– |
} |
1793 |
– |
} |
1794 |
– |
} finally { |
1795 |
– |
if (!f.casHash(fh | LOCKED, fh)) { |
1796 |
– |
f.hash = fh; |
1797 |
– |
synchronized (f) { f.notifyAll(); }; |
1798 |
– |
} |
1799 |
– |
} |
1800 |
– |
if (count != 0) { |
1801 |
– |
if (tab.length <= 64) |
1802 |
– |
count = 2; |
1803 |
– |
break; |
1804 |
– |
} |
1805 |
– |
} |
1806 |
– |
} |
1807 |
– |
if (delta != 0) { |
1808 |
– |
counter.add((long)delta); |
1809 |
– |
if (count > 1) |
1810 |
– |
checkForResize(); |
1811 |
– |
} |
1812 |
– |
return val; |
1813 |
– |
} |
1814 |
– |
|
1815 |
– |
/** Implementation for merge */ |
1816 |
– |
@SuppressWarnings("unchecked") private final Object internalMerge |
1817 |
– |
(K k, V v, BiFun<? super V, ? super V, ? extends V> mf) { |
1818 |
– |
int h = spread(k.hashCode()); |
1819 |
– |
Object val = null; |
1820 |
– |
int delta = 0; |
1821 |
– |
int count = 0; |
1822 |
– |
for (Node[] tab = table;;) { |
1823 |
– |
int i; Node f; int fh; Object fk, fv; |
1824 |
– |
if (tab == null) |
1825 |
– |
tab = initTable(); |
1826 |
– |
else if ((f = tabAt(tab, i = (tab.length - 1) & h)) == null) { |
1827 |
– |
if (casTabAt(tab, i, null, new Node(h, k, v, null))) { |
1828 |
– |
delta = 1; |
1829 |
– |
val = v; |
1830 |
– |
break; |
1831 |
– |
} |
1832 |
– |
} |
1833 |
– |
else if ((fh = f.hash) == MOVED) { |
1834 |
– |
if ((fk = f.key) instanceof TreeBin) { |
1835 |
– |
TreeBin t = (TreeBin)fk; |
1836 |
– |
t.acquire(0); |
1837 |
– |
try { |
1838 |
– |
if (tabAt(tab, i) == f) { |
1839 |
– |
count = 1; |
1840 |
– |
TreeNode p = t.getTreeNode(h, k, t.root); |
1841 |
– |
val = (p == null) ? v : mf.apply((V)p.val, v); |
1842 |
– |
if (val != null) { |
1843 |
– |
if (p != null) |
1844 |
– |
p.val = val; |
1845 |
– |
else { |
1846 |
– |
count = 2; |
1847 |
– |
delta = 1; |
1848 |
– |
t.putTreeNode(h, k, val); |
1849 |
– |
} |
1850 |
– |
} |
1851 |
– |
else if (p != null) { |
1852 |
– |
delta = -1; |
1853 |
– |
t.deleteTreeNode(p); |
1854 |
– |
} |
1855 |
– |
} |
1856 |
– |
} finally { |
1857 |
– |
t.release(0); |
1858 |
– |
} |
1859 |
– |
if (count != 0) |
1860 |
– |
break; |
1861 |
– |
} |
1862 |
– |
else |
1863 |
– |
tab = (Node[])fk; |
1864 |
– |
} |
1865 |
– |
else if ((fh & LOCKED) != 0) { |
1866 |
– |
checkForResize(); |
1867 |
– |
f.tryAwaitLock(tab, i); |
1868 |
– |
} |
1869 |
– |
else if (f.casHash(fh, fh | LOCKED)) { |
1870 |
– |
try { |
1871 |
– |
if (tabAt(tab, i) == f) { |
1872 |
– |
count = 1; |
1873 |
– |
for (Node e = f, pred = null;; ++count) { |
1874 |
– |
Object ek, ev; |
1875 |
– |
if ((e.hash & HASH_BITS) == h && |
1876 |
– |
(ev = e.val) != null && |
1877 |
– |
((ek = e.key) == k || k.equals(ek))) { |
1878 |
– |
val = mf.apply(v, (V)ev); |
1879 |
– |
if (val != null) |
1880 |
– |
e.val = val; |
1881 |
– |
else { |
1882 |
– |
delta = -1; |
1883 |
– |
Node en = e.next; |
1884 |
– |
if (pred != null) |
1885 |
– |
pred.next = en; |
1886 |
– |
else |
1887 |
– |
setTabAt(tab, i, en); |
1888 |
– |
} |
1889 |
– |
break; |
1890 |
– |
} |
1891 |
– |
pred = e; |
1892 |
– |
if ((e = e.next) == null) { |
1893 |
– |
val = v; |
1894 |
– |
pred.next = new Node(h, k, val, null); |
1895 |
– |
delta = 1; |
1896 |
– |
if (count >= TREE_THRESHOLD) |
1897 |
– |
replaceWithTreeBin(tab, i, k); |
1898 |
– |
break; |
1899 |
– |
} |
1900 |
– |
} |
1901 |
– |
} |
1902 |
– |
} finally { |
1903 |
– |
if (!f.casHash(fh | LOCKED, fh)) { |
1904 |
– |
f.hash = fh; |
1905 |
– |
synchronized (f) { f.notifyAll(); }; |
1906 |
– |
} |
1907 |
– |
} |
1908 |
– |
if (count != 0) { |
1909 |
– |
if (tab.length <= 64) |
1910 |
– |
count = 2; |
1911 |
– |
break; |
1912 |
– |
} |
1913 |
– |
} |
1914 |
– |
} |
1915 |
– |
if (delta != 0) { |
1916 |
– |
counter.add((long)delta); |
1917 |
– |
if (count > 1) |
1918 |
– |
checkForResize(); |
1919 |
– |
} |
1920 |
– |
return val; |
1921 |
– |
} |
1922 |
– |
|
1923 |
– |
/** Implementation for putAll */ |
1924 |
– |
private final void internalPutAll(Map<?, ?> m) { |
1925 |
– |
tryPresize(m.size()); |
1926 |
– |
long delta = 0L; // number of uncommitted additions |
1927 |
– |
boolean npe = false; // to throw exception on exit for nulls |
1928 |
– |
try { // to clean up counts on other exceptions |
1929 |
– |
for (Map.Entry<?, ?> entry : m.entrySet()) { |
1930 |
– |
Object k, v; |
1931 |
– |
if (entry == null || (k = entry.getKey()) == null || |
1932 |
– |
(v = entry.getValue()) == null) { |
1933 |
– |
npe = true; |
1934 |
– |
break; |
1935 |
– |
} |
1936 |
– |
int h = spread(k.hashCode()); |
1937 |
– |
for (Node[] tab = table;;) { |
1938 |
– |
int i; Node f; int fh; Object fk; |
1939 |
– |
if (tab == null) |
1940 |
– |
tab = initTable(); |
1941 |
– |
else if ((f = tabAt(tab, i = (tab.length - 1) & h)) == null){ |
1942 |
– |
if (casTabAt(tab, i, null, new Node(h, k, v, null))) { |
1943 |
– |
++delta; |
1944 |
– |
break; |
1945 |
– |
} |
1946 |
– |
} |
1947 |
– |
else if ((fh = f.hash) == MOVED) { |
1948 |
– |
if ((fk = f.key) instanceof TreeBin) { |
1949 |
– |
TreeBin t = (TreeBin)fk; |
1950 |
– |
boolean validated = false; |
1951 |
– |
t.acquire(0); |
1952 |
– |
try { |
1953 |
– |
if (tabAt(tab, i) == f) { |
1954 |
– |
validated = true; |
1955 |
– |
TreeNode p = t.getTreeNode(h, k, t.root); |
1956 |
– |
if (p != null) |
1957 |
– |
p.val = v; |
1958 |
– |
else { |
1959 |
– |
t.putTreeNode(h, k, v); |
1960 |
– |
++delta; |
1961 |
– |
} |
1962 |
– |
} |
1963 |
– |
} finally { |
1964 |
– |
t.release(0); |
1965 |
– |
} |
1966 |
– |
if (validated) |
1967 |
– |
break; |
1968 |
– |
} |
1969 |
– |
else |
1970 |
– |
tab = (Node[])fk; |
1971 |
– |
} |
1972 |
– |
else if ((fh & LOCKED) != 0) { |
1973 |
– |
counter.add(delta); |
1974 |
– |
delta = 0L; |
1975 |
– |
checkForResize(); |
1976 |
– |
f.tryAwaitLock(tab, i); |
1977 |
– |
} |
1978 |
– |
else if (f.casHash(fh, fh | LOCKED)) { |
1979 |
– |
int count = 0; |
1980 |
– |
try { |
1981 |
– |
if (tabAt(tab, i) == f) { |
1982 |
– |
count = 1; |
1983 |
– |
for (Node e = f;; ++count) { |
1984 |
– |
Object ek, ev; |
1985 |
– |
if ((e.hash & HASH_BITS) == h && |
1986 |
– |
(ev = e.val) != null && |
1987 |
– |
((ek = e.key) == k || k.equals(ek))) { |
1988 |
– |
e.val = v; |
1989 |
– |
break; |
1990 |
– |
} |
1991 |
– |
Node last = e; |
1992 |
– |
if ((e = e.next) == null) { |
1993 |
– |
++delta; |
1994 |
– |
last.next = new Node(h, k, v, null); |
1995 |
– |
if (count >= TREE_THRESHOLD) |
1996 |
– |
replaceWithTreeBin(tab, i, k); |
1997 |
– |
break; |
1998 |
– |
} |
1999 |
– |
} |
2000 |
– |
} |
2001 |
– |
} finally { |
2002 |
– |
if (!f.casHash(fh | LOCKED, fh)) { |
2003 |
– |
f.hash = fh; |
2004 |
– |
synchronized (f) { f.notifyAll(); }; |
2005 |
– |
} |
2006 |
– |
} |
2007 |
– |
if (count != 0) { |
2008 |
– |
if (count > 1) { |
2009 |
– |
counter.add(delta); |
2010 |
– |
delta = 0L; |
2011 |
– |
checkForResize(); |
2012 |
– |
} |
2013 |
– |
break; |
2014 |
– |
} |
2015 |
– |
} |
2016 |
– |
} |
2017 |
– |
} |
2018 |
– |
} finally { |
2019 |
– |
if (delta != 0) |
2020 |
– |
counter.add(delta); |
2021 |
– |
} |
2022 |
– |
if (npe) |
2023 |
– |
throw new NullPointerException(); |
2024 |
– |
} |
2025 |
– |
|
2026 |
– |
/* ---------------- Table Initialization and Resizing -------------- */ |
2027 |
– |
|
2028 |
– |
/** |
673 |
|
* Returns a power of two table size for the given desired capacity. |
674 |
|
* See Hackers Delight, sec 3.2 |
675 |
|
*/ |
684 |
|
} |
685 |
|
|
686 |
|
/** |
687 |
< |
* Initializes table, using the size recorded in sizeCtl. |
687 |
> |
* Returns x's Class if it is of the form "class C implements |
688 |
> |
* Comparable<C>", else null. |
689 |
|
*/ |
690 |
< |
private final Node[] initTable() { |
691 |
< |
Node[] tab; int sc; |
692 |
< |
while ((tab = table) == null) { |
693 |
< |
if ((sc = sizeCtl) < 0) |
694 |
< |
Thread.yield(); // lost initialization race; just spin |
695 |
< |
else if (UNSAFE.compareAndSwapInt(this, sizeCtlOffset, sc, -1)) { |
696 |
< |
try { |
697 |
< |
if ((tab = table) == null) { |
698 |
< |
int n = (sc > 0) ? sc : DEFAULT_CAPACITY; |
699 |
< |
tab = table = new Node[n]; |
700 |
< |
sc = n - (n >>> 2); |
701 |
< |
} |
702 |
< |
} finally { |
2058 |
< |
sizeCtl = sc; |
2059 |
< |
} |
2060 |
< |
break; |
2061 |
< |
} |
2062 |
< |
} |
2063 |
< |
return tab; |
2064 |
< |
} |
2065 |
< |
|
2066 |
< |
/** |
2067 |
< |
* If table is too small and not already resizing, creates next |
2068 |
< |
* table and transfers bins. Rechecks occupancy after a transfer |
2069 |
< |
* to see if another resize is already needed because resizings |
2070 |
< |
* are lagging additions. |
2071 |
< |
*/ |
2072 |
< |
private final void checkForResize() { |
2073 |
< |
Node[] tab; int n, sc; |
2074 |
< |
while ((tab = table) != null && |
2075 |
< |
(n = tab.length) < MAXIMUM_CAPACITY && |
2076 |
< |
(sc = sizeCtl) >= 0 && counter.sum() >= (long)sc && |
2077 |
< |
UNSAFE.compareAndSwapInt(this, sizeCtlOffset, sc, -1)) { |
2078 |
< |
try { |
2079 |
< |
if (tab == table) { |
2080 |
< |
table = rebuild(tab); |
2081 |
< |
sc = (n << 1) - (n >>> 1); |
690 |
> |
static Class<?> comparableClassFor(Object x) { |
691 |
> |
if (x instanceof Comparable) { |
692 |
> |
Class<?> c; Type[] ts, as; Type t; ParameterizedType p; |
693 |
> |
if ((c = x.getClass()) == String.class) // bypass checks |
694 |
> |
return c; |
695 |
> |
if ((ts = c.getGenericInterfaces()) != null) { |
696 |
> |
for (int i = 0; i < ts.length; ++i) { |
697 |
> |
if (((t = ts[i]) instanceof ParameterizedType) && |
698 |
> |
((p = (ParameterizedType)t).getRawType() == |
699 |
> |
Comparable.class) && |
700 |
> |
(as = p.getActualTypeArguments()) != null && |
701 |
> |
as.length == 1 && as[0] == c) // type arg is c |
702 |
> |
return c; |
703 |
|
} |
2083 |
– |
} finally { |
2084 |
– |
sizeCtl = sc; |
704 |
|
} |
705 |
|
} |
706 |
+ |
return null; |
707 |
|
} |
708 |
|
|
709 |
|
/** |
710 |
< |
* Tries to presize table to accommodate the given number of elements. |
711 |
< |
* |
2092 |
< |
* @param size number of elements (doesn't need to be perfectly accurate) |
710 |
> |
* Returns k.compareTo(x) if x matches kc (k's screened comparable |
711 |
> |
* class), else 0. |
712 |
|
*/ |
713 |
< |
private final void tryPresize(int size) { |
714 |
< |
int c = (size >= (MAXIMUM_CAPACITY >>> 1)) ? MAXIMUM_CAPACITY : |
715 |
< |
tableSizeFor(size + (size >>> 1) + 1); |
716 |
< |
int sc; |
2098 |
< |
while ((sc = sizeCtl) >= 0) { |
2099 |
< |
Node[] tab = table; int n; |
2100 |
< |
if (tab == null || (n = tab.length) == 0) { |
2101 |
< |
n = (sc > c) ? sc : c; |
2102 |
< |
if (UNSAFE.compareAndSwapInt(this, sizeCtlOffset, sc, -1)) { |
2103 |
< |
try { |
2104 |
< |
if (table == tab) { |
2105 |
< |
table = new Node[n]; |
2106 |
< |
sc = n - (n >>> 2); |
2107 |
< |
} |
2108 |
< |
} finally { |
2109 |
< |
sizeCtl = sc; |
2110 |
< |
} |
2111 |
< |
} |
2112 |
< |
} |
2113 |
< |
else if (c <= sc || n >= MAXIMUM_CAPACITY) |
2114 |
< |
break; |
2115 |
< |
else if (UNSAFE.compareAndSwapInt(this, sizeCtlOffset, sc, -1)) { |
2116 |
< |
try { |
2117 |
< |
if (table == tab) { |
2118 |
< |
table = rebuild(tab); |
2119 |
< |
sc = (n << 1) - (n >>> 1); |
2120 |
< |
} |
2121 |
< |
} finally { |
2122 |
< |
sizeCtl = sc; |
2123 |
< |
} |
2124 |
< |
} |
2125 |
< |
} |
713 |
> |
@SuppressWarnings({"rawtypes","unchecked"}) // for cast to Comparable |
714 |
> |
static int compareComparables(Class<?> kc, Object k, Object x) { |
715 |
> |
return (x == null || x.getClass() != kc ? 0 : |
716 |
> |
((Comparable)k).compareTo(x)); |
717 |
|
} |
718 |
|
|
719 |
+ |
/* ---------------- Table element access -------------- */ |
720 |
+ |
|
721 |
|
/* |
722 |
< |
* Moves and/or copies the nodes in each bin to new table. See |
723 |
< |
* above for explanation. |
724 |
< |
* |
725 |
< |
* @return the new table |
726 |
< |
*/ |
727 |
< |
private static final Node[] rebuild(Node[] tab) { |
728 |
< |
int n = tab.length; |
729 |
< |
Node[] nextTab = new Node[n << 1]; |
730 |
< |
Node fwd = new Node(MOVED, nextTab, null, null); |
731 |
< |
int[] buffer = null; // holds bins to revisit; null until needed |
732 |
< |
Node rev = null; // reverse forwarder; null until needed |
733 |
< |
int nbuffered = 0; // the number of bins in buffer list |
734 |
< |
int bufferIndex = 0; // buffer index of current buffered bin |
735 |
< |
int bin = n - 1; // current non-buffered bin or -1 if none |
736 |
< |
|
737 |
< |
for (int i = bin;;) { // start upwards sweep |
738 |
< |
int fh; Node f; |
739 |
< |
if ((f = tabAt(tab, i)) == null) { |
740 |
< |
if (bin >= 0) { // Unbuffered; no lock needed (or available) |
741 |
< |
if (!casTabAt(tab, i, f, fwd)) |
742 |
< |
continue; |
743 |
< |
} |
744 |
< |
else { // transiently use a locked forwarding node |
2152 |
< |
Node g = new Node(MOVED|LOCKED, nextTab, null, null); |
2153 |
< |
if (!casTabAt(tab, i, f, g)) |
2154 |
< |
continue; |
2155 |
< |
setTabAt(nextTab, i, null); |
2156 |
< |
setTabAt(nextTab, i + n, null); |
2157 |
< |
setTabAt(tab, i, fwd); |
2158 |
< |
if (!g.casHash(MOVED|LOCKED, MOVED)) { |
2159 |
< |
g.hash = MOVED; |
2160 |
< |
synchronized (g) { g.notifyAll(); } |
2161 |
< |
} |
2162 |
< |
} |
2163 |
< |
} |
2164 |
< |
else if ((fh = f.hash) == MOVED) { |
2165 |
< |
Object fk = f.key; |
2166 |
< |
if (fk instanceof TreeBin) { |
2167 |
< |
TreeBin t = (TreeBin)fk; |
2168 |
< |
boolean validated = false; |
2169 |
< |
t.acquire(0); |
2170 |
< |
try { |
2171 |
< |
if (tabAt(tab, i) == f) { |
2172 |
< |
validated = true; |
2173 |
< |
splitTreeBin(nextTab, i, t); |
2174 |
< |
setTabAt(tab, i, fwd); |
2175 |
< |
} |
2176 |
< |
} finally { |
2177 |
< |
t.release(0); |
2178 |
< |
} |
2179 |
< |
if (!validated) |
2180 |
< |
continue; |
2181 |
< |
} |
2182 |
< |
} |
2183 |
< |
else if ((fh & LOCKED) == 0 && f.casHash(fh, fh|LOCKED)) { |
2184 |
< |
boolean validated = false; |
2185 |
< |
try { // split to lo and hi lists; copying as needed |
2186 |
< |
if (tabAt(tab, i) == f) { |
2187 |
< |
validated = true; |
2188 |
< |
splitBin(nextTab, i, f); |
2189 |
< |
setTabAt(tab, i, fwd); |
2190 |
< |
} |
2191 |
< |
} finally { |
2192 |
< |
if (!f.casHash(fh | LOCKED, fh)) { |
2193 |
< |
f.hash = fh; |
2194 |
< |
synchronized (f) { f.notifyAll(); }; |
2195 |
< |
} |
2196 |
< |
} |
2197 |
< |
if (!validated) |
2198 |
< |
continue; |
2199 |
< |
} |
2200 |
< |
else { |
2201 |
< |
if (buffer == null) // initialize buffer for revisits |
2202 |
< |
buffer = new int[TRANSFER_BUFFER_SIZE]; |
2203 |
< |
if (bin < 0 && bufferIndex > 0) { |
2204 |
< |
int j = buffer[--bufferIndex]; |
2205 |
< |
buffer[bufferIndex] = i; |
2206 |
< |
i = j; // swap with another bin |
2207 |
< |
continue; |
2208 |
< |
} |
2209 |
< |
if (bin < 0 || nbuffered >= TRANSFER_BUFFER_SIZE) { |
2210 |
< |
f.tryAwaitLock(tab, i); |
2211 |
< |
continue; // no other options -- block |
2212 |
< |
} |
2213 |
< |
if (rev == null) // initialize reverse-forwarder |
2214 |
< |
rev = new Node(MOVED, tab, null, null); |
2215 |
< |
if (tabAt(tab, i) != f || (f.hash & LOCKED) == 0) |
2216 |
< |
continue; // recheck before adding to list |
2217 |
< |
buffer[nbuffered++] = i; |
2218 |
< |
setTabAt(nextTab, i, rev); // install place-holders |
2219 |
< |
setTabAt(nextTab, i + n, rev); |
2220 |
< |
} |
2221 |
< |
|
2222 |
< |
if (bin > 0) |
2223 |
< |
i = --bin; |
2224 |
< |
else if (buffer != null && nbuffered > 0) { |
2225 |
< |
bin = -1; |
2226 |
< |
i = buffer[bufferIndex = --nbuffered]; |
2227 |
< |
} |
2228 |
< |
else |
2229 |
< |
return nextTab; |
2230 |
< |
} |
722 |
> |
* Volatile access methods are used for table elements as well as |
723 |
> |
* elements of in-progress next table while resizing. All uses of |
724 |
> |
* the tab arguments must be null checked by callers. All callers |
725 |
> |
* also paranoically precheck that tab's length is not zero (or an |
726 |
> |
* equivalent check), thus ensuring that any index argument taking |
727 |
> |
* the form of a hash value anded with (length - 1) is a valid |
728 |
> |
* index. Note that, to be correct wrt arbitrary concurrency |
729 |
> |
* errors by users, these checks must operate on local variables, |
730 |
> |
* which accounts for some odd-looking inline assignments below. |
731 |
> |
* Note that calls to setTabAt always occur within locked regions, |
732 |
> |
* and so in principle require only release ordering, not need |
733 |
> |
* full volatile semantics, but are currently coded as volatile |
734 |
> |
* writes to be conservative. |
735 |
> |
*/ |
736 |
> |
|
737 |
> |
@SuppressWarnings("unchecked") |
738 |
> |
static final <K,V> Node<K,V> tabAt(Node<K,V>[] tab, int i) { |
739 |
> |
return (Node<K,V>)U.getObjectVolatile(tab, ((long)i << ASHIFT) + ABASE); |
740 |
> |
} |
741 |
> |
|
742 |
> |
static final <K,V> boolean casTabAt(Node<K,V>[] tab, int i, |
743 |
> |
Node<K,V> c, Node<K,V> v) { |
744 |
> |
return U.compareAndSwapObject(tab, ((long)i << ASHIFT) + ABASE, c, v); |
745 |
|
} |
746 |
|
|
747 |
< |
/** |
748 |
< |
* Splits a normal bin with list headed by e into lo and hi parts; |
2235 |
< |
* installs in given table. |
2236 |
< |
*/ |
2237 |
< |
private static void splitBin(Node[] nextTab, int i, Node e) { |
2238 |
< |
int bit = nextTab.length >>> 1; // bit to split on |
2239 |
< |
int runBit = e.hash & bit; |
2240 |
< |
Node lastRun = e, lo = null, hi = null; |
2241 |
< |
for (Node p = e.next; p != null; p = p.next) { |
2242 |
< |
int b = p.hash & bit; |
2243 |
< |
if (b != runBit) { |
2244 |
< |
runBit = b; |
2245 |
< |
lastRun = p; |
2246 |
< |
} |
2247 |
< |
} |
2248 |
< |
if (runBit == 0) |
2249 |
< |
lo = lastRun; |
2250 |
< |
else |
2251 |
< |
hi = lastRun; |
2252 |
< |
for (Node p = e; p != lastRun; p = p.next) { |
2253 |
< |
int ph = p.hash & HASH_BITS; |
2254 |
< |
Object pk = p.key, pv = p.val; |
2255 |
< |
if ((ph & bit) == 0) |
2256 |
< |
lo = new Node(ph, pk, pv, lo); |
2257 |
< |
else |
2258 |
< |
hi = new Node(ph, pk, pv, hi); |
2259 |
< |
} |
2260 |
< |
setTabAt(nextTab, i, lo); |
2261 |
< |
setTabAt(nextTab, i + bit, hi); |
747 |
> |
static final <K,V> void setTabAt(Node<K,V>[] tab, int i, Node<K,V> v) { |
748 |
> |
U.putObjectVolatile(tab, ((long)i << ASHIFT) + ABASE, v); |
749 |
|
} |
750 |
|
|
751 |
+ |
/* ---------------- Fields -------------- */ |
752 |
+ |
|
753 |
|
/** |
754 |
< |
* Splits a tree bin into lo and hi parts; installs in given table. |
754 |
> |
* The array of bins. Lazily initialized upon first insertion. |
755 |
> |
* Size is always a power of two. Accessed directly by iterators. |
756 |
|
*/ |
757 |
< |
private static void splitTreeBin(Node[] nextTab, int i, TreeBin t) { |
2268 |
< |
int bit = nextTab.length >>> 1; |
2269 |
< |
TreeBin lt = new TreeBin(); |
2270 |
< |
TreeBin ht = new TreeBin(); |
2271 |
< |
int lc = 0, hc = 0; |
2272 |
< |
for (Node e = t.first; e != null; e = e.next) { |
2273 |
< |
int h = e.hash & HASH_BITS; |
2274 |
< |
Object k = e.key, v = e.val; |
2275 |
< |
if ((h & bit) == 0) { |
2276 |
< |
++lc; |
2277 |
< |
lt.putTreeNode(h, k, v); |
2278 |
< |
} |
2279 |
< |
else { |
2280 |
< |
++hc; |
2281 |
< |
ht.putTreeNode(h, k, v); |
2282 |
< |
} |
2283 |
< |
} |
2284 |
< |
Node ln, hn; // throw away trees if too small |
2285 |
< |
if (lc <= (TREE_THRESHOLD >>> 1)) { |
2286 |
< |
ln = null; |
2287 |
< |
for (Node p = lt.first; p != null; p = p.next) |
2288 |
< |
ln = new Node(p.hash, p.key, p.val, ln); |
2289 |
< |
} |
2290 |
< |
else |
2291 |
< |
ln = new Node(MOVED, lt, null, null); |
2292 |
< |
setTabAt(nextTab, i, ln); |
2293 |
< |
if (hc <= (TREE_THRESHOLD >>> 1)) { |
2294 |
< |
hn = null; |
2295 |
< |
for (Node p = ht.first; p != null; p = p.next) |
2296 |
< |
hn = new Node(p.hash, p.key, p.val, hn); |
2297 |
< |
} |
2298 |
< |
else |
2299 |
< |
hn = new Node(MOVED, ht, null, null); |
2300 |
< |
setTabAt(nextTab, i + bit, hn); |
2301 |
< |
} |
757 |
> |
transient volatile Node<K,V>[] table; |
758 |
|
|
759 |
|
/** |
760 |
< |
* Implementation for clear. Steps through each bin, removing all |
2305 |
< |
* nodes. |
760 |
> |
* The next table to use; non-null only while resizing. |
761 |
|
*/ |
762 |
< |
private final void internalClear() { |
2308 |
< |
long delta = 0L; // negative number of deletions |
2309 |
< |
int i = 0; |
2310 |
< |
Node[] tab = table; |
2311 |
< |
while (tab != null && i < tab.length) { |
2312 |
< |
int fh; Object fk; |
2313 |
< |
Node f = tabAt(tab, i); |
2314 |
< |
if (f == null) |
2315 |
< |
++i; |
2316 |
< |
else if ((fh = f.hash) == MOVED) { |
2317 |
< |
if ((fk = f.key) instanceof TreeBin) { |
2318 |
< |
TreeBin t = (TreeBin)fk; |
2319 |
< |
t.acquire(0); |
2320 |
< |
try { |
2321 |
< |
if (tabAt(tab, i) == f) { |
2322 |
< |
for (Node p = t.first; p != null; p = p.next) { |
2323 |
< |
if (p.val != null) { // (currently always true) |
2324 |
< |
p.val = null; |
2325 |
< |
--delta; |
2326 |
< |
} |
2327 |
< |
} |
2328 |
< |
t.first = null; |
2329 |
< |
t.root = null; |
2330 |
< |
++i; |
2331 |
< |
} |
2332 |
< |
} finally { |
2333 |
< |
t.release(0); |
2334 |
< |
} |
2335 |
< |
} |
2336 |
< |
else |
2337 |
< |
tab = (Node[])fk; |
2338 |
< |
} |
2339 |
< |
else if ((fh & LOCKED) != 0) { |
2340 |
< |
counter.add(delta); // opportunistically update count |
2341 |
< |
delta = 0L; |
2342 |
< |
f.tryAwaitLock(tab, i); |
2343 |
< |
} |
2344 |
< |
else if (f.casHash(fh, fh | LOCKED)) { |
2345 |
< |
try { |
2346 |
< |
if (tabAt(tab, i) == f) { |
2347 |
< |
for (Node e = f; e != null; e = e.next) { |
2348 |
< |
if (e.val != null) { // (currently always true) |
2349 |
< |
e.val = null; |
2350 |
< |
--delta; |
2351 |
< |
} |
2352 |
< |
} |
2353 |
< |
setTabAt(tab, i, null); |
2354 |
< |
++i; |
2355 |
< |
} |
2356 |
< |
} finally { |
2357 |
< |
if (!f.casHash(fh | LOCKED, fh)) { |
2358 |
< |
f.hash = fh; |
2359 |
< |
synchronized (f) { f.notifyAll(); }; |
2360 |
< |
} |
2361 |
< |
} |
2362 |
< |
} |
2363 |
< |
} |
2364 |
< |
if (delta != 0) |
2365 |
< |
counter.add(delta); |
2366 |
< |
} |
2367 |
< |
|
2368 |
< |
/* ----------------Table Traversal -------------- */ |
762 |
> |
private transient volatile Node<K,V>[] nextTable; |
763 |
|
|
764 |
|
/** |
765 |
< |
* Encapsulates traversal for methods such as containsValue; also |
766 |
< |
* serves as a base class for other iterators and bulk tasks. |
767 |
< |
* |
768 |
< |
* At each step, the iterator snapshots the key ("nextKey") and |
769 |
< |
* value ("nextVal") of a valid node (i.e., one that, at point of |
2376 |
< |
* snapshot, has a non-null user value). Because val fields can |
2377 |
< |
* change (including to null, indicating deletion), field nextVal |
2378 |
< |
* might not be accurate at point of use, but still maintains the |
2379 |
< |
* weak consistency property of holding a value that was once |
2380 |
< |
* valid. To support iterator.remove, the nextKey field is not |
2381 |
< |
* updated (nulled out) when the iterator cannot advance. |
2382 |
< |
* |
2383 |
< |
* Internal traversals directly access these fields, as in: |
2384 |
< |
* {@code while (it.advance() != null) { process(it.nextKey); }} |
2385 |
< |
* |
2386 |
< |
* Exported iterators must track whether the iterator has advanced |
2387 |
< |
* (in hasNext vs next) (by setting/checking/nulling field |
2388 |
< |
* nextVal), and then extract key, value, or key-value pairs as |
2389 |
< |
* return values of next(). |
2390 |
< |
* |
2391 |
< |
* The iterator visits once each still-valid node that was |
2392 |
< |
* reachable upon iterator construction. It might miss some that |
2393 |
< |
* were added to a bin after the bin was visited, which is OK wrt |
2394 |
< |
* consistency guarantees. Maintaining this property in the face |
2395 |
< |
* of possible ongoing resizes requires a fair amount of |
2396 |
< |
* bookkeeping state that is difficult to optimize away amidst |
2397 |
< |
* volatile accesses. Even so, traversal maintains reasonable |
2398 |
< |
* throughput. |
2399 |
< |
* |
2400 |
< |
* Normally, iteration proceeds bin-by-bin traversing lists. |
2401 |
< |
* However, if the table has been resized, then all future steps |
2402 |
< |
* must traverse both the bin at the current index as well as at |
2403 |
< |
* (index + baseSize); and so on for further resizings. To |
2404 |
< |
* paranoically cope with potential sharing by users of iterators |
2405 |
< |
* across threads, iteration terminates if a bounds checks fails |
2406 |
< |
* for a table read. |
2407 |
< |
* |
2408 |
< |
* This class extends CountedCompleter to streamline parallel |
2409 |
< |
* iteration in bulk operations. This adds only a few fields of |
2410 |
< |
* space overhead, which is small enough in cases where it is not |
2411 |
< |
* needed to not worry about it. Because CountedCompleter is |
2412 |
< |
* Serializable, but iterators need not be, we need to add warning |
2413 |
< |
* suppressions. |
2414 |
< |
*/ |
2415 |
< |
@SuppressWarnings("serial") static class Traverser<K,V,R> extends CountedCompleter<R> { |
2416 |
< |
final ConcurrentHashMapV8<K, V> map; |
2417 |
< |
Node next; // the next entry to use |
2418 |
< |
Object nextKey; // cached key field of next |
2419 |
< |
Object nextVal; // cached val field of next |
2420 |
< |
Node[] tab; // current table; updated if resized |
2421 |
< |
int index; // index of bin to use next |
2422 |
< |
int baseIndex; // current index of initial table |
2423 |
< |
int baseLimit; // index bound for initial table |
2424 |
< |
int baseSize; // initial table size |
2425 |
< |
int batch; // split control |
2426 |
< |
|
2427 |
< |
/** Creates iterator for all entries in the table. */ |
2428 |
< |
Traverser(ConcurrentHashMapV8<K, V> map) { |
2429 |
< |
this.map = map; |
2430 |
< |
} |
2431 |
< |
|
2432 |
< |
/** Creates iterator for split() methods and task constructors */ |
2433 |
< |
Traverser(ConcurrentHashMapV8<K,V> map, Traverser<K,V,?> it, int batch) { |
2434 |
< |
super(it); |
2435 |
< |
this.batch = batch; |
2436 |
< |
if ((this.map = map) != null && it != null) { // split parent |
2437 |
< |
Node[] t; |
2438 |
< |
if ((t = it.tab) == null && |
2439 |
< |
(t = it.tab = map.table) != null) |
2440 |
< |
it.baseLimit = it.baseSize = t.length; |
2441 |
< |
this.tab = t; |
2442 |
< |
this.baseSize = it.baseSize; |
2443 |
< |
int hi = this.baseLimit = it.baseLimit; |
2444 |
< |
it.baseLimit = this.index = this.baseIndex = |
2445 |
< |
(hi + it.baseIndex + 1) >>> 1; |
2446 |
< |
} |
2447 |
< |
} |
765 |
> |
* Base counter value, used mainly when there is no contention, |
766 |
> |
* but also as a fallback during table initialization |
767 |
> |
* races. Updated via CAS. |
768 |
> |
*/ |
769 |
> |
private transient volatile long baseCount; |
770 |
|
|
771 |
< |
/** |
772 |
< |
* Advances next; returns nextVal or null if terminated. |
773 |
< |
* See above for explanation. |
774 |
< |
*/ |
775 |
< |
final Object advance() { |
776 |
< |
Node e = next; |
777 |
< |
Object ev = null; |
778 |
< |
outer: do { |
779 |
< |
if (e != null) // advance past used/skipped node |
2458 |
< |
e = e.next; |
2459 |
< |
while (e == null) { // get to next non-null bin |
2460 |
< |
ConcurrentHashMapV8<K, V> m; |
2461 |
< |
Node[] t; int b, i, n; Object ek; // checks must use locals |
2462 |
< |
if ((t = tab) != null) |
2463 |
< |
n = t.length; |
2464 |
< |
else if ((m = map) != null && (t = tab = m.table) != null) |
2465 |
< |
n = baseLimit = baseSize = t.length; |
2466 |
< |
else |
2467 |
< |
break outer; |
2468 |
< |
if ((b = baseIndex) >= baseLimit || |
2469 |
< |
(i = index) < 0 || i >= n) |
2470 |
< |
break outer; |
2471 |
< |
if ((e = tabAt(t, i)) != null && e.hash == MOVED) { |
2472 |
< |
if ((ek = e.key) instanceof TreeBin) |
2473 |
< |
e = ((TreeBin)ek).first; |
2474 |
< |
else { |
2475 |
< |
tab = (Node[])ek; |
2476 |
< |
continue; // restarts due to null val |
2477 |
< |
} |
2478 |
< |
} // visit upper slots if present |
2479 |
< |
index = (i += baseSize) < n ? i : (baseIndex = b + 1); |
2480 |
< |
} |
2481 |
< |
nextKey = e.key; |
2482 |
< |
} while ((ev = e.val) == null); // skip deleted or special nodes |
2483 |
< |
next = e; |
2484 |
< |
return nextVal = ev; |
2485 |
< |
} |
771 |
> |
/** |
772 |
> |
* Table initialization and resizing control. When negative, the |
773 |
> |
* table is being initialized or resized: -1 for initialization, |
774 |
> |
* else -(1 + the number of active resizing threads). Otherwise, |
775 |
> |
* when table is null, holds the initial table size to use upon |
776 |
> |
* creation, or 0 for default. After initialization, holds the |
777 |
> |
* next element count value upon which to resize the table. |
778 |
> |
*/ |
779 |
> |
private transient volatile int sizeCtl; |
780 |
|
|
781 |
< |
public final void remove() { |
782 |
< |
Object k = nextKey; |
783 |
< |
if (k == null && (advance() == null || (k = nextKey) == null)) |
784 |
< |
throw new IllegalStateException(); |
2491 |
< |
map.internalReplace(k, null, null); |
2492 |
< |
} |
781 |
> |
/** |
782 |
> |
* The next table index (plus one) to split while resizing. |
783 |
> |
*/ |
784 |
> |
private transient volatile int transferIndex; |
785 |
|
|
786 |
< |
public final boolean hasNext() { |
787 |
< |
return nextVal != null || advance() != null; |
788 |
< |
} |
786 |
> |
/** |
787 |
> |
* The least available table index to split while resizing. |
788 |
> |
*/ |
789 |
> |
private transient volatile int transferOrigin; |
790 |
|
|
791 |
< |
public final boolean hasMoreElements() { return hasNext(); } |
791 |
> |
/** |
792 |
> |
* Spinlock (locked via CAS) used when resizing and/or creating CounterCells. |
793 |
> |
*/ |
794 |
> |
private transient volatile int cellsBusy; |
795 |
|
|
796 |
< |
public void compute() { } // default no-op CountedCompleter body |
796 |
> |
/** |
797 |
> |
* Table of counter cells. When non-null, size is a power of 2. |
798 |
> |
*/ |
799 |
> |
private transient volatile CounterCell[] counterCells; |
800 |
|
|
801 |
< |
/** |
802 |
< |
* Returns a batch value > 0 if this task should (and must) be |
803 |
< |
* split, if so, adding to pending count, and in any case |
804 |
< |
* updating batch value. The initial batch value is approx |
2506 |
< |
* exp2 of the number of times (minus one) to split task by |
2507 |
< |
* two before executing leaf action. This value is faster to |
2508 |
< |
* compute and more convenient to use as a guide to splitting |
2509 |
< |
* than is the depth, since it is used while dividing by two |
2510 |
< |
* anyway. |
2511 |
< |
*/ |
2512 |
< |
final int preSplit() { |
2513 |
< |
ConcurrentHashMapV8<K, V> m; int b; Node[] t; ForkJoinPool pool; |
2514 |
< |
if ((b = batch) < 0 && (m = map) != null) { // force initialization |
2515 |
< |
if ((t = tab) == null && (t = tab = m.table) != null) |
2516 |
< |
baseLimit = baseSize = t.length; |
2517 |
< |
if (t != null) { |
2518 |
< |
long n = m.counter.sum(); |
2519 |
< |
int par = ((pool = getPool()) == null) ? |
2520 |
< |
ForkJoinPool.getCommonPoolParallelism() : |
2521 |
< |
pool.getParallelism(); |
2522 |
< |
int sp = par << 3; // slack of 8 |
2523 |
< |
b = (n <= 0L) ? 0 : (n < (long)sp) ? (int)n : sp; |
2524 |
< |
} |
2525 |
< |
} |
2526 |
< |
b = (b <= 1 || baseIndex == baseLimit) ? 0 : (b >>> 1); |
2527 |
< |
if ((batch = b) > 0) |
2528 |
< |
addToPendingCount(1); |
2529 |
< |
return b; |
2530 |
< |
} |
801 |
> |
// views |
802 |
> |
private transient KeySetView<K,V> keySet; |
803 |
> |
private transient ValuesView<K,V> values; |
804 |
> |
private transient EntrySetView<K,V> entrySet; |
805 |
|
|
2532 |
– |
} |
806 |
|
|
807 |
|
/* ---------------- Public operations -------------- */ |
808 |
|
|
810 |
|
* Creates a new, empty map with the default initial table size (16). |
811 |
|
*/ |
812 |
|
public ConcurrentHashMapV8() { |
2540 |
– |
this.counter = new LongAdder(); |
813 |
|
} |
814 |
|
|
815 |
|
/** |
828 |
|
int cap = ((initialCapacity >= (MAXIMUM_CAPACITY >>> 1)) ? |
829 |
|
MAXIMUM_CAPACITY : |
830 |
|
tableSizeFor(initialCapacity + (initialCapacity >>> 1) + 1)); |
2559 |
– |
this.counter = new LongAdder(); |
831 |
|
this.sizeCtl = cap; |
832 |
|
} |
833 |
|
|
837 |
|
* @param m the map |
838 |
|
*/ |
839 |
|
public ConcurrentHashMapV8(Map<? extends K, ? extends V> m) { |
2569 |
– |
this.counter = new LongAdder(); |
840 |
|
this.sizeCtl = DEFAULT_CAPACITY; |
841 |
< |
internalPutAll(m); |
841 |
> |
putAll(m); |
842 |
|
} |
843 |
|
|
844 |
|
/** |
879 |
|
* nonpositive |
880 |
|
*/ |
881 |
|
public ConcurrentHashMapV8(int initialCapacity, |
882 |
< |
float loadFactor, int concurrencyLevel) { |
882 |
> |
float loadFactor, int concurrencyLevel) { |
883 |
|
if (!(loadFactor > 0.0f) || initialCapacity < 0 || concurrencyLevel <= 0) |
884 |
|
throw new IllegalArgumentException(); |
885 |
|
if (initialCapacity < concurrencyLevel) // Use at least as many bins |
887 |
|
long size = (long)(1.0 + (long)initialCapacity / loadFactor); |
888 |
|
int cap = (size >= (long)MAXIMUM_CAPACITY) ? |
889 |
|
MAXIMUM_CAPACITY : tableSizeFor((int)size); |
2620 |
– |
this.counter = new LongAdder(); |
890 |
|
this.sizeCtl = cap; |
891 |
|
} |
892 |
|
|
893 |
< |
/** |
2625 |
< |
* Creates a new {@link Set} backed by a ConcurrentHashMapV8 |
2626 |
< |
* from the given type to {@code Boolean.TRUE}. |
2627 |
< |
* |
2628 |
< |
* @return the new set |
2629 |
< |
*/ |
2630 |
< |
public static <K> KeySetView<K,Boolean> newKeySet() { |
2631 |
< |
return new KeySetView<K,Boolean>(new ConcurrentHashMapV8<K,Boolean>(), |
2632 |
< |
Boolean.TRUE); |
2633 |
< |
} |
2634 |
< |
|
2635 |
< |
/** |
2636 |
< |
* Creates a new {@link Set} backed by a ConcurrentHashMapV8 |
2637 |
< |
* from the given type to {@code Boolean.TRUE}. |
2638 |
< |
* |
2639 |
< |
* @param initialCapacity The implementation performs internal |
2640 |
< |
* sizing to accommodate this many elements. |
2641 |
< |
* @throws IllegalArgumentException if the initial capacity of |
2642 |
< |
* elements is negative |
2643 |
< |
* @return the new set |
2644 |
< |
*/ |
2645 |
< |
public static <K> KeySetView<K,Boolean> newKeySet(int initialCapacity) { |
2646 |
< |
return new KeySetView<K,Boolean>(new ConcurrentHashMapV8<K,Boolean>(initialCapacity), |
2647 |
< |
Boolean.TRUE); |
2648 |
< |
} |
2649 |
< |
|
2650 |
< |
/** |
2651 |
< |
* {@inheritDoc} |
2652 |
< |
*/ |
2653 |
< |
public boolean isEmpty() { |
2654 |
< |
return counter.sum() <= 0L; // ignore transient negative values |
2655 |
< |
} |
893 |
> |
// Original (since JDK1.2) Map methods |
894 |
|
|
895 |
|
/** |
896 |
|
* {@inheritDoc} |
897 |
|
*/ |
898 |
|
public int size() { |
899 |
< |
long n = counter.sum(); |
899 |
> |
long n = sumCount(); |
900 |
|
return ((n < 0L) ? 0 : |
901 |
|
(n > (long)Integer.MAX_VALUE) ? Integer.MAX_VALUE : |
902 |
|
(int)n); |
903 |
|
} |
904 |
|
|
905 |
|
/** |
906 |
< |
* Returns the number of mappings. This method should be used |
2669 |
< |
* instead of {@link #size} because a ConcurrentHashMapV8 may |
2670 |
< |
* contain more mappings than can be represented as an int. The |
2671 |
< |
* value returned is an estimate; the actual count may differ if |
2672 |
< |
* there are concurrent insertions or removals. |
2673 |
< |
* |
2674 |
< |
* @return the number of mappings |
906 |
> |
* {@inheritDoc} |
907 |
|
*/ |
908 |
< |
public long mappingCount() { |
909 |
< |
long n = counter.sum(); |
2678 |
< |
return (n < 0L) ? 0L : n; // ignore transient negative values |
908 |
> |
public boolean isEmpty() { |
909 |
> |
return sumCount() <= 0L; // ignore transient negative values |
910 |
|
} |
911 |
|
|
912 |
|
/** |
920 |
|
* |
921 |
|
* @throws NullPointerException if the specified key is null |
922 |
|
*/ |
923 |
< |
@SuppressWarnings("unchecked") public V get(Object key) { |
924 |
< |
if (key == null) |
925 |
< |
throw new NullPointerException(); |
926 |
< |
return (V)internalGet(key); |
927 |
< |
} |
928 |
< |
|
929 |
< |
/** |
930 |
< |
* Returns the value to which the specified key is mapped, |
931 |
< |
* or the given defaultValue if this map contains no mapping for the key. |
932 |
< |
* |
933 |
< |
* @param key the key |
934 |
< |
* @param defaultValue the value to return if this map contains |
935 |
< |
* no mapping for the given key |
936 |
< |
* @return the mapping for the key, if present; else the defaultValue |
937 |
< |
* @throws NullPointerException if the specified key is null |
938 |
< |
*/ |
939 |
< |
@SuppressWarnings("unchecked") public V getValueOrDefault(Object key, V defaultValue) { |
940 |
< |
if (key == null) |
2710 |
< |
throw new NullPointerException(); |
2711 |
< |
V v = (V) internalGet(key); |
2712 |
< |
return v == null ? defaultValue : v; |
923 |
> |
public V get(Object key) { |
924 |
> |
Node<K,V>[] tab; Node<K,V> e, p; int n, eh; K ek; |
925 |
> |
int h = spread(key.hashCode()); |
926 |
> |
if ((tab = table) != null && (n = tab.length) > 0 && |
927 |
> |
(e = tabAt(tab, (n - 1) & h)) != null) { |
928 |
> |
if ((eh = e.hash) == h) { |
929 |
> |
if ((ek = e.key) == key || (ek != null && key.equals(ek))) |
930 |
> |
return e.val; |
931 |
> |
} |
932 |
> |
else if (eh < 0) |
933 |
> |
return (p = e.find(h, key)) != null ? p.val : null; |
934 |
> |
while ((e = e.next) != null) { |
935 |
> |
if (e.hash == h && |
936 |
> |
((ek = e.key) == key || (ek != null && key.equals(ek)))) |
937 |
> |
return e.val; |
938 |
> |
} |
939 |
> |
} |
940 |
> |
return null; |
941 |
|
} |
942 |
|
|
943 |
|
/** |
944 |
|
* Tests if the specified object is a key in this table. |
945 |
|
* |
946 |
< |
* @param key possible key |
946 |
> |
* @param key possible key |
947 |
|
* @return {@code true} if and only if the specified object |
948 |
|
* is a key in this table, as determined by the |
949 |
|
* {@code equals} method; {@code false} otherwise |
950 |
|
* @throws NullPointerException if the specified key is null |
951 |
|
*/ |
952 |
|
public boolean containsKey(Object key) { |
953 |
< |
if (key == null) |
2726 |
< |
throw new NullPointerException(); |
2727 |
< |
return internalGet(key) != null; |
953 |
> |
return get(key) != null; |
954 |
|
} |
955 |
|
|
956 |
|
/** |
966 |
|
public boolean containsValue(Object value) { |
967 |
|
if (value == null) |
968 |
|
throw new NullPointerException(); |
969 |
< |
Object v; |
970 |
< |
Traverser<K,V,Object> it = new Traverser<K,V,Object>(this); |
971 |
< |
while ((v = it.advance()) != null) { |
972 |
< |
if (v == value || value.equals(v)) |
973 |
< |
return true; |
969 |
> |
Node<K,V>[] t; |
970 |
> |
if ((t = table) != null) { |
971 |
> |
Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length); |
972 |
> |
for (Node<K,V> p; (p = it.advance()) != null; ) { |
973 |
> |
V v; |
974 |
> |
if ((v = p.val) == value || (v != null && value.equals(v))) |
975 |
> |
return true; |
976 |
> |
} |
977 |
|
} |
978 |
|
return false; |
979 |
|
} |
980 |
|
|
981 |
|
/** |
2753 |
– |
* Legacy method testing if some key maps into the specified value |
2754 |
– |
* in this table. This method is identical in functionality to |
2755 |
– |
* {@link #containsValue}, and exists solely to ensure |
2756 |
– |
* full compatibility with class {@link java.util.Hashtable}, |
2757 |
– |
* which supported this method prior to introduction of the |
2758 |
– |
* Java Collections framework. |
2759 |
– |
* |
2760 |
– |
* @param value a value to search for |
2761 |
– |
* @return {@code true} if and only if some key maps to the |
2762 |
– |
* {@code value} argument in this table as |
2763 |
– |
* determined by the {@code equals} method; |
2764 |
– |
* {@code false} otherwise |
2765 |
– |
* @throws NullPointerException if the specified value is null |
2766 |
– |
*/ |
2767 |
– |
public boolean contains(Object value) { |
2768 |
– |
return containsValue(value); |
2769 |
– |
} |
2770 |
– |
|
2771 |
– |
/** |
982 |
|
* Maps the specified key to the specified value in this table. |
983 |
|
* Neither the key nor the value can be null. |
984 |
|
* |
991 |
|
* {@code null} if there was no mapping for {@code key} |
992 |
|
* @throws NullPointerException if the specified key or value is null |
993 |
|
*/ |
994 |
< |
@SuppressWarnings("unchecked") public V put(K key, V value) { |
995 |
< |
if (key == null || value == null) |
994 |
> |
public V put(K key, V value) { |
995 |
> |
return putVal(key, value, false); |
996 |
> |
} |
997 |
> |
|
998 |
> |
/** Implementation for put and putIfAbsent */ |
999 |
> |
final V putVal(K key, V value, boolean onlyIfAbsent) { |
1000 |
> |
if (key == null || value == null) throw new NullPointerException(); |
1001 |
> |
int hash = spread(key.hashCode()); |
1002 |
> |
int binCount = 0; |
1003 |
> |
for (Node<K,V>[] tab = table;;) { |
1004 |
> |
Node<K,V> f; int n, i, fh; |
1005 |
> |
if (tab == null || (n = tab.length) == 0) |
1006 |
> |
tab = initTable(); |
1007 |
> |
else if ((f = tabAt(tab, i = (n - 1) & hash)) == null) { |
1008 |
> |
if (casTabAt(tab, i, null, |
1009 |
> |
new Node<K,V>(hash, key, value, null))) |
1010 |
> |
break; // no lock when adding to empty bin |
1011 |
> |
} |
1012 |
> |
else if ((fh = f.hash) == MOVED) |
1013 |
> |
tab = helpTransfer(tab, f); |
1014 |
> |
else { |
1015 |
> |
V oldVal = null; |
1016 |
> |
synchronized (f) { |
1017 |
> |
if (tabAt(tab, i) == f) { |
1018 |
> |
if (fh >= 0) { |
1019 |
> |
binCount = 1; |
1020 |
> |
for (Node<K,V> e = f;; ++binCount) { |
1021 |
> |
K ek; |
1022 |
> |
if (e.hash == hash && |
1023 |
> |
((ek = e.key) == key || |
1024 |
> |
(ek != null && key.equals(ek)))) { |
1025 |
> |
oldVal = e.val; |
1026 |
> |
if (!onlyIfAbsent) |
1027 |
> |
e.val = value; |
1028 |
> |
break; |
1029 |
> |
} |
1030 |
> |
Node<K,V> pred = e; |
1031 |
> |
if ((e = e.next) == null) { |
1032 |
> |
pred.next = new Node<K,V>(hash, key, |
1033 |
> |
value, null); |
1034 |
> |
break; |
1035 |
> |
} |
1036 |
> |
} |
1037 |
> |
} |
1038 |
> |
else if (f instanceof TreeBin) { |
1039 |
> |
Node<K,V> p; |
1040 |
> |
binCount = 2; |
1041 |
> |
if ((p = ((TreeBin<K,V>)f).putTreeVal(hash, key, |
1042 |
> |
value)) != null) { |
1043 |
> |
oldVal = p.val; |
1044 |
> |
if (!onlyIfAbsent) |
1045 |
> |
p.val = value; |
1046 |
> |
} |
1047 |
> |
} |
1048 |
> |
} |
1049 |
> |
} |
1050 |
> |
if (binCount != 0) { |
1051 |
> |
if (binCount >= TREEIFY_THRESHOLD) |
1052 |
> |
treeifyBin(tab, i); |
1053 |
> |
if (oldVal != null) |
1054 |
> |
return oldVal; |
1055 |
> |
break; |
1056 |
> |
} |
1057 |
> |
} |
1058 |
> |
} |
1059 |
> |
addCount(1L, binCount); |
1060 |
> |
return null; |
1061 |
> |
} |
1062 |
> |
|
1063 |
> |
/** |
1064 |
> |
* Copies all of the mappings from the specified map to this one. |
1065 |
> |
* These mappings replace any mappings that this map had for any of the |
1066 |
> |
* keys currently in the specified map. |
1067 |
> |
* |
1068 |
> |
* @param m mappings to be stored in this map |
1069 |
> |
*/ |
1070 |
> |
public void putAll(Map<? extends K, ? extends V> m) { |
1071 |
> |
tryPresize(m.size()); |
1072 |
> |
for (Map.Entry<? extends K, ? extends V> e : m.entrySet()) |
1073 |
> |
putVal(e.getKey(), e.getValue(), false); |
1074 |
> |
} |
1075 |
> |
|
1076 |
> |
/** |
1077 |
> |
* Removes the key (and its corresponding value) from this map. |
1078 |
> |
* This method does nothing if the key is not in the map. |
1079 |
> |
* |
1080 |
> |
* @param key the key that needs to be removed |
1081 |
> |
* @return the previous value associated with {@code key}, or |
1082 |
> |
* {@code null} if there was no mapping for {@code key} |
1083 |
> |
* @throws NullPointerException if the specified key is null |
1084 |
> |
*/ |
1085 |
> |
public V remove(Object key) { |
1086 |
> |
return replaceNode(key, null, null); |
1087 |
> |
} |
1088 |
> |
|
1089 |
> |
/** |
1090 |
> |
* Implementation for the four public remove/replace methods: |
1091 |
> |
* Replaces node value with v, conditional upon match of cv if |
1092 |
> |
* non-null. If resulting value is null, delete. |
1093 |
> |
*/ |
1094 |
> |
final V replaceNode(Object key, V value, Object cv) { |
1095 |
> |
int hash = spread(key.hashCode()); |
1096 |
> |
for (Node<K,V>[] tab = table;;) { |
1097 |
> |
Node<K,V> f; int n, i, fh; |
1098 |
> |
if (tab == null || (n = tab.length) == 0 || |
1099 |
> |
(f = tabAt(tab, i = (n - 1) & hash)) == null) |
1100 |
> |
break; |
1101 |
> |
else if ((fh = f.hash) == MOVED) |
1102 |
> |
tab = helpTransfer(tab, f); |
1103 |
> |
else { |
1104 |
> |
V oldVal = null; |
1105 |
> |
boolean validated = false; |
1106 |
> |
synchronized (f) { |
1107 |
> |
if (tabAt(tab, i) == f) { |
1108 |
> |
if (fh >= 0) { |
1109 |
> |
validated = true; |
1110 |
> |
for (Node<K,V> e = f, pred = null;;) { |
1111 |
> |
K ek; |
1112 |
> |
if (e.hash == hash && |
1113 |
> |
((ek = e.key) == key || |
1114 |
> |
(ek != null && key.equals(ek)))) { |
1115 |
> |
V ev = e.val; |
1116 |
> |
if (cv == null || cv == ev || |
1117 |
> |
(ev != null && cv.equals(ev))) { |
1118 |
> |
oldVal = ev; |
1119 |
> |
if (value != null) |
1120 |
> |
e.val = value; |
1121 |
> |
else if (pred != null) |
1122 |
> |
pred.next = e.next; |
1123 |
> |
else |
1124 |
> |
setTabAt(tab, i, e.next); |
1125 |
> |
} |
1126 |
> |
break; |
1127 |
> |
} |
1128 |
> |
pred = e; |
1129 |
> |
if ((e = e.next) == null) |
1130 |
> |
break; |
1131 |
> |
} |
1132 |
> |
} |
1133 |
> |
else if (f instanceof TreeBin) { |
1134 |
> |
validated = true; |
1135 |
> |
TreeBin<K,V> t = (TreeBin<K,V>)f; |
1136 |
> |
TreeNode<K,V> r, p; |
1137 |
> |
if ((r = t.root) != null && |
1138 |
> |
(p = r.findTreeNode(hash, key, null)) != null) { |
1139 |
> |
V pv = p.val; |
1140 |
> |
if (cv == null || cv == pv || |
1141 |
> |
(pv != null && cv.equals(pv))) { |
1142 |
> |
oldVal = pv; |
1143 |
> |
if (value != null) |
1144 |
> |
p.val = value; |
1145 |
> |
else if (t.removeTreeNode(p)) |
1146 |
> |
setTabAt(tab, i, untreeify(t.first)); |
1147 |
> |
} |
1148 |
> |
} |
1149 |
> |
} |
1150 |
> |
} |
1151 |
> |
} |
1152 |
> |
if (validated) { |
1153 |
> |
if (oldVal != null) { |
1154 |
> |
if (value == null) |
1155 |
> |
addCount(-1L, -1); |
1156 |
> |
return oldVal; |
1157 |
> |
} |
1158 |
> |
break; |
1159 |
> |
} |
1160 |
> |
} |
1161 |
> |
} |
1162 |
> |
return null; |
1163 |
> |
} |
1164 |
> |
|
1165 |
> |
/** |
1166 |
> |
* Removes all of the mappings from this map. |
1167 |
> |
*/ |
1168 |
> |
public void clear() { |
1169 |
> |
long delta = 0L; // negative number of deletions |
1170 |
> |
int i = 0; |
1171 |
> |
Node<K,V>[] tab = table; |
1172 |
> |
while (tab != null && i < tab.length) { |
1173 |
> |
int fh; |
1174 |
> |
Node<K,V> f = tabAt(tab, i); |
1175 |
> |
if (f == null) |
1176 |
> |
++i; |
1177 |
> |
else if ((fh = f.hash) == MOVED) { |
1178 |
> |
tab = helpTransfer(tab, f); |
1179 |
> |
i = 0; // restart |
1180 |
> |
} |
1181 |
> |
else { |
1182 |
> |
synchronized (f) { |
1183 |
> |
if (tabAt(tab, i) == f) { |
1184 |
> |
Node<K,V> p = (fh >= 0 ? f : |
1185 |
> |
(f instanceof TreeBin) ? |
1186 |
> |
((TreeBin<K,V>)f).first : null); |
1187 |
> |
while (p != null) { |
1188 |
> |
--delta; |
1189 |
> |
p = p.next; |
1190 |
> |
} |
1191 |
> |
setTabAt(tab, i++, null); |
1192 |
> |
} |
1193 |
> |
} |
1194 |
> |
} |
1195 |
> |
} |
1196 |
> |
if (delta != 0L) |
1197 |
> |
addCount(delta, -1); |
1198 |
> |
} |
1199 |
> |
|
1200 |
> |
/** |
1201 |
> |
* Returns a {@link Set} view of the keys contained in this map. |
1202 |
> |
* The set is backed by the map, so changes to the map are |
1203 |
> |
* reflected in the set, and vice-versa. The set supports element |
1204 |
> |
* removal, which removes the corresponding mapping from this map, |
1205 |
> |
* via the {@code Iterator.remove}, {@code Set.remove}, |
1206 |
> |
* {@code removeAll}, {@code retainAll}, and {@code clear} |
1207 |
> |
* operations. It does not support the {@code add} or |
1208 |
> |
* {@code addAll} operations. |
1209 |
> |
* |
1210 |
> |
* <p>The view's {@code iterator} is a "weakly consistent" iterator |
1211 |
> |
* that will never throw {@link ConcurrentModificationException}, |
1212 |
> |
* and guarantees to traverse elements as they existed upon |
1213 |
> |
* construction of the iterator, and may (but is not guaranteed to) |
1214 |
> |
* reflect any modifications subsequent to construction. |
1215 |
> |
* |
1216 |
> |
* @return the set view |
1217 |
> |
*/ |
1218 |
> |
public KeySetView<K,V> keySet() { |
1219 |
> |
KeySetView<K,V> ks; |
1220 |
> |
return (ks = keySet) != null ? ks : (keySet = new KeySetView<K,V>(this, null)); |
1221 |
> |
} |
1222 |
> |
|
1223 |
> |
/** |
1224 |
> |
* Returns a {@link Collection} view of the values contained in this map. |
1225 |
> |
* The collection is backed by the map, so changes to the map are |
1226 |
> |
* reflected in the collection, and vice-versa. The collection |
1227 |
> |
* supports element removal, which removes the corresponding |
1228 |
> |
* mapping from this map, via the {@code Iterator.remove}, |
1229 |
> |
* {@code Collection.remove}, {@code removeAll}, |
1230 |
> |
* {@code retainAll}, and {@code clear} operations. It does not |
1231 |
> |
* support the {@code add} or {@code addAll} operations. |
1232 |
> |
* |
1233 |
> |
* <p>The view's {@code iterator} is a "weakly consistent" iterator |
1234 |
> |
* that will never throw {@link ConcurrentModificationException}, |
1235 |
> |
* and guarantees to traverse elements as they existed upon |
1236 |
> |
* construction of the iterator, and may (but is not guaranteed to) |
1237 |
> |
* reflect any modifications subsequent to construction. |
1238 |
> |
* |
1239 |
> |
* @return the collection view |
1240 |
> |
*/ |
1241 |
> |
public Collection<V> values() { |
1242 |
> |
ValuesView<K,V> vs; |
1243 |
> |
return (vs = values) != null ? vs : (values = new ValuesView<K,V>(this)); |
1244 |
> |
} |
1245 |
> |
|
1246 |
> |
/** |
1247 |
> |
* Returns a {@link Set} view of the mappings contained in this map. |
1248 |
> |
* The set is backed by the map, so changes to the map are |
1249 |
> |
* reflected in the set, and vice-versa. The set supports element |
1250 |
> |
* removal, which removes the corresponding mapping from the map, |
1251 |
> |
* via the {@code Iterator.remove}, {@code Set.remove}, |
1252 |
> |
* {@code removeAll}, {@code retainAll}, and {@code clear} |
1253 |
> |
* operations. |
1254 |
> |
* |
1255 |
> |
* <p>The view's {@code iterator} is a "weakly consistent" iterator |
1256 |
> |
* that will never throw {@link ConcurrentModificationException}, |
1257 |
> |
* and guarantees to traverse elements as they existed upon |
1258 |
> |
* construction of the iterator, and may (but is not guaranteed to) |
1259 |
> |
* reflect any modifications subsequent to construction. |
1260 |
> |
* |
1261 |
> |
* @return the set view |
1262 |
> |
*/ |
1263 |
> |
public Set<Map.Entry<K,V>> entrySet() { |
1264 |
> |
EntrySetView<K,V> es; |
1265 |
> |
return (es = entrySet) != null ? es : (entrySet = new EntrySetView<K,V>(this)); |
1266 |
> |
} |
1267 |
> |
|
1268 |
> |
/** |
1269 |
> |
* Returns the hash code value for this {@link Map}, i.e., |
1270 |
> |
* the sum of, for each key-value pair in the map, |
1271 |
> |
* {@code key.hashCode() ^ value.hashCode()}. |
1272 |
> |
* |
1273 |
> |
* @return the hash code value for this map |
1274 |
> |
*/ |
1275 |
> |
public int hashCode() { |
1276 |
> |
int h = 0; |
1277 |
> |
Node<K,V>[] t; |
1278 |
> |
if ((t = table) != null) { |
1279 |
> |
Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length); |
1280 |
> |
for (Node<K,V> p; (p = it.advance()) != null; ) |
1281 |
> |
h += p.key.hashCode() ^ p.val.hashCode(); |
1282 |
> |
} |
1283 |
> |
return h; |
1284 |
> |
} |
1285 |
> |
|
1286 |
> |
/** |
1287 |
> |
* Returns a string representation of this map. The string |
1288 |
> |
* representation consists of a list of key-value mappings (in no |
1289 |
> |
* particular order) enclosed in braces ("{@code {}}"). Adjacent |
1290 |
> |
* mappings are separated by the characters {@code ", "} (comma |
1291 |
> |
* and space). Each key-value mapping is rendered as the key |
1292 |
> |
* followed by an equals sign ("{@code =}") followed by the |
1293 |
> |
* associated value. |
1294 |
> |
* |
1295 |
> |
* @return a string representation of this map |
1296 |
> |
*/ |
1297 |
> |
public String toString() { |
1298 |
> |
Node<K,V>[] t; |
1299 |
> |
int f = (t = table) == null ? 0 : t.length; |
1300 |
> |
Traverser<K,V> it = new Traverser<K,V>(t, f, 0, f); |
1301 |
> |
StringBuilder sb = new StringBuilder(); |
1302 |
> |
sb.append('{'); |
1303 |
> |
Node<K,V> p; |
1304 |
> |
if ((p = it.advance()) != null) { |
1305 |
> |
for (;;) { |
1306 |
> |
K k = p.key; |
1307 |
> |
V v = p.val; |
1308 |
> |
sb.append(k == this ? "(this Map)" : k); |
1309 |
> |
sb.append('='); |
1310 |
> |
sb.append(v == this ? "(this Map)" : v); |
1311 |
> |
if ((p = it.advance()) == null) |
1312 |
> |
break; |
1313 |
> |
sb.append(',').append(' '); |
1314 |
> |
} |
1315 |
> |
} |
1316 |
> |
return sb.append('}').toString(); |
1317 |
> |
} |
1318 |
> |
|
1319 |
> |
/** |
1320 |
> |
* Compares the specified object with this map for equality. |
1321 |
> |
* Returns {@code true} if the given object is a map with the same |
1322 |
> |
* mappings as this map. This operation may return misleading |
1323 |
> |
* results if either map is concurrently modified during execution |
1324 |
> |
* of this method. |
1325 |
> |
* |
1326 |
> |
* @param o object to be compared for equality with this map |
1327 |
> |
* @return {@code true} if the specified object is equal to this map |
1328 |
> |
*/ |
1329 |
> |
public boolean equals(Object o) { |
1330 |
> |
if (o != this) { |
1331 |
> |
if (!(o instanceof Map)) |
1332 |
> |
return false; |
1333 |
> |
Map<?,?> m = (Map<?,?>) o; |
1334 |
> |
Node<K,V>[] t; |
1335 |
> |
int f = (t = table) == null ? 0 : t.length; |
1336 |
> |
Traverser<K,V> it = new Traverser<K,V>(t, f, 0, f); |
1337 |
> |
for (Node<K,V> p; (p = it.advance()) != null; ) { |
1338 |
> |
V val = p.val; |
1339 |
> |
Object v = m.get(p.key); |
1340 |
> |
if (v == null || (v != val && !v.equals(val))) |
1341 |
> |
return false; |
1342 |
> |
} |
1343 |
> |
for (Map.Entry<?,?> e : m.entrySet()) { |
1344 |
> |
Object mk, mv, v; |
1345 |
> |
if ((mk = e.getKey()) == null || |
1346 |
> |
(mv = e.getValue()) == null || |
1347 |
> |
(v = get(mk)) == null || |
1348 |
> |
(mv != v && !mv.equals(v))) |
1349 |
> |
return false; |
1350 |
> |
} |
1351 |
> |
} |
1352 |
> |
return true; |
1353 |
> |
} |
1354 |
> |
|
1355 |
> |
/** |
1356 |
> |
* Stripped-down version of helper class used in previous version, |
1357 |
> |
* declared for the sake of serialization compatibility |
1358 |
> |
*/ |
1359 |
> |
static class Segment<K,V> extends ReentrantLock implements Serializable { |
1360 |
> |
private static final long serialVersionUID = 2249069246763182397L; |
1361 |
> |
final float loadFactor; |
1362 |
> |
Segment(float lf) { this.loadFactor = lf; } |
1363 |
> |
} |
1364 |
> |
|
1365 |
> |
/** |
1366 |
> |
* Saves the state of the {@code ConcurrentHashMapV8} instance to a |
1367 |
> |
* stream (i.e., serializes it). |
1368 |
> |
* @param s the stream |
1369 |
> |
* @serialData |
1370 |
> |
* the key (Object) and value (Object) |
1371 |
> |
* for each key-value mapping, followed by a null pair. |
1372 |
> |
* The key-value mappings are emitted in no particular order. |
1373 |
> |
*/ |
1374 |
> |
private void writeObject(java.io.ObjectOutputStream s) |
1375 |
> |
throws java.io.IOException { |
1376 |
> |
// For serialization compatibility |
1377 |
> |
// Emulate segment calculation from previous version of this class |
1378 |
> |
int sshift = 0; |
1379 |
> |
int ssize = 1; |
1380 |
> |
while (ssize < DEFAULT_CONCURRENCY_LEVEL) { |
1381 |
> |
++sshift; |
1382 |
> |
ssize <<= 1; |
1383 |
> |
} |
1384 |
> |
int segmentShift = 32 - sshift; |
1385 |
> |
int segmentMask = ssize - 1; |
1386 |
> |
@SuppressWarnings("unchecked") Segment<K,V>[] segments = (Segment<K,V>[]) |
1387 |
> |
new Segment<?,?>[DEFAULT_CONCURRENCY_LEVEL]; |
1388 |
> |
for (int i = 0; i < segments.length; ++i) |
1389 |
> |
segments[i] = new Segment<K,V>(LOAD_FACTOR); |
1390 |
> |
s.putFields().put("segments", segments); |
1391 |
> |
s.putFields().put("segmentShift", segmentShift); |
1392 |
> |
s.putFields().put("segmentMask", segmentMask); |
1393 |
> |
s.writeFields(); |
1394 |
> |
|
1395 |
> |
Node<K,V>[] t; |
1396 |
> |
if ((t = table) != null) { |
1397 |
> |
Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length); |
1398 |
> |
for (Node<K,V> p; (p = it.advance()) != null; ) { |
1399 |
> |
s.writeObject(p.key); |
1400 |
> |
s.writeObject(p.val); |
1401 |
> |
} |
1402 |
> |
} |
1403 |
> |
s.writeObject(null); |
1404 |
> |
s.writeObject(null); |
1405 |
> |
segments = null; // throw away |
1406 |
> |
} |
1407 |
> |
|
1408 |
> |
/** |
1409 |
> |
* Reconstitutes the instance from a stream (that is, deserializes it). |
1410 |
> |
* @param s the stream |
1411 |
> |
*/ |
1412 |
> |
private void readObject(java.io.ObjectInputStream s) |
1413 |
> |
throws java.io.IOException, ClassNotFoundException { |
1414 |
> |
/* |
1415 |
> |
* To improve performance in typical cases, we create nodes |
1416 |
> |
* while reading, then place in table once size is known. |
1417 |
> |
* However, we must also validate uniqueness and deal with |
1418 |
> |
* overpopulated bins while doing so, which requires |
1419 |
> |
* specialized versions of putVal mechanics. |
1420 |
> |
*/ |
1421 |
> |
sizeCtl = -1; // force exclusion for table construction |
1422 |
> |
s.defaultReadObject(); |
1423 |
> |
long size = 0L; |
1424 |
> |
Node<K,V> p = null; |
1425 |
> |
for (;;) { |
1426 |
> |
@SuppressWarnings("unchecked") K k = (K) s.readObject(); |
1427 |
> |
@SuppressWarnings("unchecked") V v = (V) s.readObject(); |
1428 |
> |
if (k != null && v != null) { |
1429 |
> |
p = new Node<K,V>(spread(k.hashCode()), k, v, p); |
1430 |
> |
++size; |
1431 |
> |
} |
1432 |
> |
else |
1433 |
> |
break; |
1434 |
> |
} |
1435 |
> |
if (size == 0L) |
1436 |
> |
sizeCtl = 0; |
1437 |
> |
else { |
1438 |
> |
int n; |
1439 |
> |
if (size >= (long)(MAXIMUM_CAPACITY >>> 1)) |
1440 |
> |
n = MAXIMUM_CAPACITY; |
1441 |
> |
else { |
1442 |
> |
int sz = (int)size; |
1443 |
> |
n = tableSizeFor(sz + (sz >>> 1) + 1); |
1444 |
> |
} |
1445 |
> |
@SuppressWarnings({"rawtypes","unchecked"}) |
1446 |
> |
Node<K,V>[] tab = (Node<K,V>[])new Node[n]; |
1447 |
> |
int mask = n - 1; |
1448 |
> |
long added = 0L; |
1449 |
> |
while (p != null) { |
1450 |
> |
boolean insertAtFront; |
1451 |
> |
Node<K,V> next = p.next, first; |
1452 |
> |
int h = p.hash, j = h & mask; |
1453 |
> |
if ((first = tabAt(tab, j)) == null) |
1454 |
> |
insertAtFront = true; |
1455 |
> |
else { |
1456 |
> |
K k = p.key; |
1457 |
> |
if (first.hash < 0) { |
1458 |
> |
TreeBin<K,V> t = (TreeBin<K,V>)first; |
1459 |
> |
if (t.putTreeVal(h, k, p.val) == null) |
1460 |
> |
++added; |
1461 |
> |
insertAtFront = false; |
1462 |
> |
} |
1463 |
> |
else { |
1464 |
> |
int binCount = 0; |
1465 |
> |
insertAtFront = true; |
1466 |
> |
Node<K,V> q; K qk; |
1467 |
> |
for (q = first; q != null; q = q.next) { |
1468 |
> |
if (q.hash == h && |
1469 |
> |
((qk = q.key) == k || |
1470 |
> |
(qk != null && k.equals(qk)))) { |
1471 |
> |
insertAtFront = false; |
1472 |
> |
break; |
1473 |
> |
} |
1474 |
> |
++binCount; |
1475 |
> |
} |
1476 |
> |
if (insertAtFront && binCount >= TREEIFY_THRESHOLD) { |
1477 |
> |
insertAtFront = false; |
1478 |
> |
++added; |
1479 |
> |
p.next = first; |
1480 |
> |
TreeNode<K,V> hd = null, tl = null; |
1481 |
> |
for (q = p; q != null; q = q.next) { |
1482 |
> |
TreeNode<K,V> t = new TreeNode<K,V> |
1483 |
> |
(q.hash, q.key, q.val, null, null); |
1484 |
> |
if ((t.prev = tl) == null) |
1485 |
> |
hd = t; |
1486 |
> |
else |
1487 |
> |
tl.next = t; |
1488 |
> |
tl = t; |
1489 |
> |
} |
1490 |
> |
setTabAt(tab, j, new TreeBin<K,V>(hd)); |
1491 |
> |
} |
1492 |
> |
} |
1493 |
> |
} |
1494 |
> |
if (insertAtFront) { |
1495 |
> |
++added; |
1496 |
> |
p.next = first; |
1497 |
> |
setTabAt(tab, j, p); |
1498 |
> |
} |
1499 |
> |
p = next; |
1500 |
> |
} |
1501 |
> |
table = tab; |
1502 |
> |
sizeCtl = n - (n >>> 2); |
1503 |
> |
baseCount = added; |
1504 |
> |
} |
1505 |
> |
} |
1506 |
> |
|
1507 |
> |
// ConcurrentMap methods |
1508 |
> |
|
1509 |
> |
/** |
1510 |
> |
* {@inheritDoc} |
1511 |
> |
* |
1512 |
> |
* @return the previous value associated with the specified key, |
1513 |
> |
* or {@code null} if there was no mapping for the key |
1514 |
> |
* @throws NullPointerException if the specified key or value is null |
1515 |
> |
*/ |
1516 |
> |
public V putIfAbsent(K key, V value) { |
1517 |
> |
return putVal(key, value, true); |
1518 |
> |
} |
1519 |
> |
|
1520 |
> |
/** |
1521 |
> |
* {@inheritDoc} |
1522 |
> |
* |
1523 |
> |
* @throws NullPointerException if the specified key is null |
1524 |
> |
*/ |
1525 |
> |
public boolean remove(Object key, Object value) { |
1526 |
> |
if (key == null) |
1527 |
|
throw new NullPointerException(); |
1528 |
< |
return (V)internalPut(key, value); |
1528 |
> |
return value != null && replaceNode(key, null, value) != null; |
1529 |
> |
} |
1530 |
> |
|
1531 |
> |
/** |
1532 |
> |
* {@inheritDoc} |
1533 |
> |
* |
1534 |
> |
* @throws NullPointerException if any of the arguments are null |
1535 |
> |
*/ |
1536 |
> |
public boolean replace(K key, V oldValue, V newValue) { |
1537 |
> |
if (key == null || oldValue == null || newValue == null) |
1538 |
> |
throw new NullPointerException(); |
1539 |
> |
return replaceNode(key, newValue, oldValue) != null; |
1540 |
|
} |
1541 |
|
|
1542 |
|
/** |
1546 |
|
* or {@code null} if there was no mapping for the key |
1547 |
|
* @throws NullPointerException if the specified key or value is null |
1548 |
|
*/ |
1549 |
< |
@SuppressWarnings("unchecked") public V putIfAbsent(K key, V value) { |
1549 |
> |
public V replace(K key, V value) { |
1550 |
|
if (key == null || value == null) |
1551 |
|
throw new NullPointerException(); |
1552 |
< |
return (V)internalPutIfAbsent(key, value); |
1552 |
> |
return replaceNode(key, value, null); |
1553 |
|
} |
1554 |
|
|
1555 |
+ |
// Overrides of JDK8+ Map extension method defaults |
1556 |
+ |
|
1557 |
|
/** |
1558 |
< |
* Copies all of the mappings from the specified map to this one. |
1559 |
< |
* These mappings replace any mappings that this map had for any of the |
1560 |
< |
* keys currently in the specified map. |
1558 |
> |
* Returns the value to which the specified key is mapped, or the |
1559 |
> |
* given default value if this map contains no mapping for the |
1560 |
> |
* key. |
1561 |
|
* |
1562 |
< |
* @param m mappings to be stored in this map |
1562 |
> |
* @param key the key whose associated value is to be returned |
1563 |
> |
* @param defaultValue the value to return if this map contains |
1564 |
> |
* no mapping for the given key |
1565 |
> |
* @return the mapping for the key, if present; else the default value |
1566 |
> |
* @throws NullPointerException if the specified key is null |
1567 |
|
*/ |
1568 |
< |
public void putAll(Map<? extends K, ? extends V> m) { |
1569 |
< |
internalPutAll(m); |
1568 |
> |
public V getOrDefault(Object key, V defaultValue) { |
1569 |
> |
V v; |
1570 |
> |
return (v = get(key)) == null ? defaultValue : v; |
1571 |
> |
} |
1572 |
> |
|
1573 |
> |
public void forEach(BiAction<? super K, ? super V> action) { |
1574 |
> |
if (action == null) throw new NullPointerException(); |
1575 |
> |
Node<K,V>[] t; |
1576 |
> |
if ((t = table) != null) { |
1577 |
> |
Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length); |
1578 |
> |
for (Node<K,V> p; (p = it.advance()) != null; ) { |
1579 |
> |
action.apply(p.key, p.val); |
1580 |
> |
} |
1581 |
> |
} |
1582 |
> |
} |
1583 |
> |
|
1584 |
> |
public void replaceAll(BiFun<? super K, ? super V, ? extends V> function) { |
1585 |
> |
if (function == null) throw new NullPointerException(); |
1586 |
> |
Node<K,V>[] t; |
1587 |
> |
if ((t = table) != null) { |
1588 |
> |
Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length); |
1589 |
> |
for (Node<K,V> p; (p = it.advance()) != null; ) { |
1590 |
> |
V oldValue = p.val; |
1591 |
> |
for (K key = p.key;;) { |
1592 |
> |
V newValue = function.apply(key, oldValue); |
1593 |
> |
if (newValue == null) |
1594 |
> |
throw new NullPointerException(); |
1595 |
> |
if (replaceNode(key, newValue, oldValue) != null || |
1596 |
> |
(oldValue = get(key)) == null) |
1597 |
> |
break; |
1598 |
> |
} |
1599 |
> |
} |
1600 |
> |
} |
1601 |
|
} |
1602 |
|
|
1603 |
|
/** |
1604 |
|
* If the specified key is not already associated with a value, |
1605 |
< |
* computes its value using the given mappingFunction and enters |
1606 |
< |
* it into the map unless null. This is equivalent to |
1607 |
< |
* <pre> {@code |
1608 |
< |
* if (map.containsKey(key)) |
1609 |
< |
* return map.get(key); |
1610 |
< |
* value = mappingFunction.apply(key); |
1611 |
< |
* if (value != null) |
2823 |
< |
* map.put(key, value); |
2824 |
< |
* return value;}</pre> |
2825 |
< |
* |
2826 |
< |
* except that the action is performed atomically. If the |
2827 |
< |
* function returns {@code null} no mapping is recorded. If the |
2828 |
< |
* function itself throws an (unchecked) exception, the exception |
2829 |
< |
* is rethrown to its caller, and no mapping is recorded. Some |
2830 |
< |
* attempted update operations on this map by other threads may be |
2831 |
< |
* blocked while computation is in progress, so the computation |
2832 |
< |
* should be short and simple, and must not attempt to update any |
2833 |
< |
* other mappings of this Map. The most appropriate usage is to |
2834 |
< |
* construct a new object serving as an initial mapped value, or |
2835 |
< |
* memoized result, as in: |
2836 |
< |
* |
2837 |
< |
* <pre> {@code |
2838 |
< |
* map.computeIfAbsent(key, new Fun<K, V>() { |
2839 |
< |
* public V map(K k) { return new Value(f(k)); }});}</pre> |
1605 |
> |
* attempts to compute its value using the given mapping function |
1606 |
> |
* and enters it into this map unless {@code null}. The entire |
1607 |
> |
* method invocation is performed atomically, so the function is |
1608 |
> |
* applied at most once per key. Some attempted update operations |
1609 |
> |
* on this map by other threads may be blocked while computation |
1610 |
> |
* is in progress, so the computation should be short and simple, |
1611 |
> |
* and must not attempt to update any other mappings of this map. |
1612 |
|
* |
1613 |
|
* @param key key with which the specified value is to be associated |
1614 |
|
* @param mappingFunction the function to compute a value |
1622 |
|
* @throws RuntimeException or Error if the mappingFunction does so, |
1623 |
|
* in which case the mapping is left unestablished |
1624 |
|
*/ |
1625 |
< |
@SuppressWarnings("unchecked") public V computeIfAbsent |
2854 |
< |
(K key, Fun<? super K, ? extends V> mappingFunction) { |
1625 |
> |
public V computeIfAbsent(K key, Fun<? super K, ? extends V> mappingFunction) { |
1626 |
|
if (key == null || mappingFunction == null) |
1627 |
|
throw new NullPointerException(); |
1628 |
< |
return (V)internalComputeIfAbsent(key, mappingFunction); |
1628 |
> |
int h = spread(key.hashCode()); |
1629 |
> |
V val = null; |
1630 |
> |
int binCount = 0; |
1631 |
> |
for (Node<K,V>[] tab = table;;) { |
1632 |
> |
Node<K,V> f; int n, i, fh; |
1633 |
> |
if (tab == null || (n = tab.length) == 0) |
1634 |
> |
tab = initTable(); |
1635 |
> |
else if ((f = tabAt(tab, i = (n - 1) & h)) == null) { |
1636 |
> |
Node<K,V> r = new ReservationNode<K,V>(); |
1637 |
> |
synchronized (r) { |
1638 |
> |
if (casTabAt(tab, i, null, r)) { |
1639 |
> |
binCount = 1; |
1640 |
> |
Node<K,V> node = null; |
1641 |
> |
try { |
1642 |
> |
if ((val = mappingFunction.apply(key)) != null) |
1643 |
> |
node = new Node<K,V>(h, key, val, null); |
1644 |
> |
} finally { |
1645 |
> |
setTabAt(tab, i, node); |
1646 |
> |
} |
1647 |
> |
} |
1648 |
> |
} |
1649 |
> |
if (binCount != 0) |
1650 |
> |
break; |
1651 |
> |
} |
1652 |
> |
else if ((fh = f.hash) == MOVED) |
1653 |
> |
tab = helpTransfer(tab, f); |
1654 |
> |
else { |
1655 |
> |
boolean added = false; |
1656 |
> |
synchronized (f) { |
1657 |
> |
if (tabAt(tab, i) == f) { |
1658 |
> |
if (fh >= 0) { |
1659 |
> |
binCount = 1; |
1660 |
> |
for (Node<K,V> e = f;; ++binCount) { |
1661 |
> |
K ek; V ev; |
1662 |
> |
if (e.hash == h && |
1663 |
> |
((ek = e.key) == key || |
1664 |
> |
(ek != null && key.equals(ek)))) { |
1665 |
> |
val = e.val; |
1666 |
> |
break; |
1667 |
> |
} |
1668 |
> |
Node<K,V> pred = e; |
1669 |
> |
if ((e = e.next) == null) { |
1670 |
> |
if ((val = mappingFunction.apply(key)) != null) { |
1671 |
> |
added = true; |
1672 |
> |
pred.next = new Node<K,V>(h, key, val, null); |
1673 |
> |
} |
1674 |
> |
break; |
1675 |
> |
} |
1676 |
> |
} |
1677 |
> |
} |
1678 |
> |
else if (f instanceof TreeBin) { |
1679 |
> |
binCount = 2; |
1680 |
> |
TreeBin<K,V> t = (TreeBin<K,V>)f; |
1681 |
> |
TreeNode<K,V> r, p; |
1682 |
> |
if ((r = t.root) != null && |
1683 |
> |
(p = r.findTreeNode(h, key, null)) != null) |
1684 |
> |
val = p.val; |
1685 |
> |
else if ((val = mappingFunction.apply(key)) != null) { |
1686 |
> |
added = true; |
1687 |
> |
t.putTreeVal(h, key, val); |
1688 |
> |
} |
1689 |
> |
} |
1690 |
> |
} |
1691 |
> |
} |
1692 |
> |
if (binCount != 0) { |
1693 |
> |
if (binCount >= TREEIFY_THRESHOLD) |
1694 |
> |
treeifyBin(tab, i); |
1695 |
> |
if (!added) |
1696 |
> |
return val; |
1697 |
> |
break; |
1698 |
> |
} |
1699 |
> |
} |
1700 |
> |
} |
1701 |
> |
if (val != null) |
1702 |
> |
addCount(1L, binCount); |
1703 |
> |
return val; |
1704 |
|
} |
1705 |
|
|
1706 |
|
/** |
1707 |
< |
* If the given key is present, computes a new mapping value given a key and |
1708 |
< |
* its current mapped value. This is equivalent to |
1709 |
< |
* <pre> {@code |
1710 |
< |
* if (map.containsKey(key)) { |
1711 |
< |
* value = remappingFunction.apply(key, map.get(key)); |
1712 |
< |
* if (value != null) |
1713 |
< |
* map.put(key, value); |
2868 |
< |
* else |
2869 |
< |
* map.remove(key); |
2870 |
< |
* } |
2871 |
< |
* }</pre> |
2872 |
< |
* |
2873 |
< |
* except that the action is performed atomically. If the |
2874 |
< |
* function returns {@code null}, the mapping is removed. If the |
2875 |
< |
* function itself throws an (unchecked) exception, the exception |
2876 |
< |
* is rethrown to its caller, and the current mapping is left |
2877 |
< |
* unchanged. Some attempted update operations on this map by |
2878 |
< |
* other threads may be blocked while computation is in progress, |
2879 |
< |
* so the computation should be short and simple, and must not |
2880 |
< |
* attempt to update any other mappings of this Map. For example, |
2881 |
< |
* to either create or append new messages to a value mapping: |
1707 |
> |
* If the value for the specified key is present, attempts to |
1708 |
> |
* compute a new mapping given the key and its current mapped |
1709 |
> |
* value. The entire method invocation is performed atomically. |
1710 |
> |
* Some attempted update operations on this map by other threads |
1711 |
> |
* may be blocked while computation is in progress, so the |
1712 |
> |
* computation should be short and simple, and must not attempt to |
1713 |
> |
* update any other mappings of this map. |
1714 |
|
* |
1715 |
< |
* @param key key with which the specified value is to be associated |
1715 |
> |
* @param key key with which a value may be associated |
1716 |
|
* @param remappingFunction the function to compute a value |
1717 |
|
* @return the new value associated with the specified key, or null if none |
1718 |
|
* @throws NullPointerException if the specified key or remappingFunction |
1723 |
|
* @throws RuntimeException or Error if the remappingFunction does so, |
1724 |
|
* in which case the mapping is unchanged |
1725 |
|
*/ |
1726 |
< |
@SuppressWarnings("unchecked") public V computeIfPresent |
2895 |
< |
(K key, BiFun<? super K, ? super V, ? extends V> remappingFunction) { |
1726 |
> |
public V computeIfPresent(K key, BiFun<? super K, ? super V, ? extends V> remappingFunction) { |
1727 |
|
if (key == null || remappingFunction == null) |
1728 |
|
throw new NullPointerException(); |
1729 |
< |
return (V)internalCompute(key, true, remappingFunction); |
1729 |
> |
int h = spread(key.hashCode()); |
1730 |
> |
V val = null; |
1731 |
> |
int delta = 0; |
1732 |
> |
int binCount = 0; |
1733 |
> |
for (Node<K,V>[] tab = table;;) { |
1734 |
> |
Node<K,V> f; int n, i, fh; |
1735 |
> |
if (tab == null || (n = tab.length) == 0) |
1736 |
> |
tab = initTable(); |
1737 |
> |
else if ((f = tabAt(tab, i = (n - 1) & h)) == null) |
1738 |
> |
break; |
1739 |
> |
else if ((fh = f.hash) == MOVED) |
1740 |
> |
tab = helpTransfer(tab, f); |
1741 |
> |
else { |
1742 |
> |
synchronized (f) { |
1743 |
> |
if (tabAt(tab, i) == f) { |
1744 |
> |
if (fh >= 0) { |
1745 |
> |
binCount = 1; |
1746 |
> |
for (Node<K,V> e = f, pred = null;; ++binCount) { |
1747 |
> |
K ek; |
1748 |
> |
if (e.hash == h && |
1749 |
> |
((ek = e.key) == key || |
1750 |
> |
(ek != null && key.equals(ek)))) { |
1751 |
> |
val = remappingFunction.apply(key, e.val); |
1752 |
> |
if (val != null) |
1753 |
> |
e.val = val; |
1754 |
> |
else { |
1755 |
> |
delta = -1; |
1756 |
> |
Node<K,V> en = e.next; |
1757 |
> |
if (pred != null) |
1758 |
> |
pred.next = en; |
1759 |
> |
else |
1760 |
> |
setTabAt(tab, i, en); |
1761 |
> |
} |
1762 |
> |
break; |
1763 |
> |
} |
1764 |
> |
pred = e; |
1765 |
> |
if ((e = e.next) == null) |
1766 |
> |
break; |
1767 |
> |
} |
1768 |
> |
} |
1769 |
> |
else if (f instanceof TreeBin) { |
1770 |
> |
binCount = 2; |
1771 |
> |
TreeBin<K,V> t = (TreeBin<K,V>)f; |
1772 |
> |
TreeNode<K,V> r, p; |
1773 |
> |
if ((r = t.root) != null && |
1774 |
> |
(p = r.findTreeNode(h, key, null)) != null) { |
1775 |
> |
val = remappingFunction.apply(key, p.val); |
1776 |
> |
if (val != null) |
1777 |
> |
p.val = val; |
1778 |
> |
else { |
1779 |
> |
delta = -1; |
1780 |
> |
if (t.removeTreeNode(p)) |
1781 |
> |
setTabAt(tab, i, untreeify(t.first)); |
1782 |
> |
} |
1783 |
> |
} |
1784 |
> |
} |
1785 |
> |
} |
1786 |
> |
} |
1787 |
> |
if (binCount != 0) |
1788 |
> |
break; |
1789 |
> |
} |
1790 |
> |
} |
1791 |
> |
if (delta != 0) |
1792 |
> |
addCount((long)delta, binCount); |
1793 |
> |
return val; |
1794 |
|
} |
1795 |
|
|
1796 |
|
/** |
1797 |
< |
* Computes a new mapping value given a key and |
1798 |
< |
* its current mapped value (or {@code null} if there is no current |
1799 |
< |
* mapping). This is equivalent to |
1800 |
< |
* <pre> {@code |
1801 |
< |
* value = remappingFunction.apply(key, map.get(key)); |
1802 |
< |
* if (value != null) |
1803 |
< |
* map.put(key, value); |
2909 |
< |
* else |
2910 |
< |
* map.remove(key); |
2911 |
< |
* }</pre> |
2912 |
< |
* |
2913 |
< |
* except that the action is performed atomically. If the |
2914 |
< |
* function returns {@code null}, the mapping is removed. If the |
2915 |
< |
* function itself throws an (unchecked) exception, the exception |
2916 |
< |
* is rethrown to its caller, and the current mapping is left |
2917 |
< |
* unchanged. Some attempted update operations on this map by |
2918 |
< |
* other threads may be blocked while computation is in progress, |
2919 |
< |
* so the computation should be short and simple, and must not |
2920 |
< |
* attempt to update any other mappings of this Map. For example, |
2921 |
< |
* to either create or append new messages to a value mapping: |
2922 |
< |
* |
2923 |
< |
* <pre> {@code |
2924 |
< |
* Map<Key, String> map = ...; |
2925 |
< |
* final String msg = ...; |
2926 |
< |
* map.compute(key, new BiFun<Key, String, String>() { |
2927 |
< |
* public String apply(Key k, String v) { |
2928 |
< |
* return (v == null) ? msg : v + msg;});}}</pre> |
1797 |
> |
* Attempts to compute a mapping for the specified key and its |
1798 |
> |
* current mapped value (or {@code null} if there is no current |
1799 |
> |
* mapping). The entire method invocation is performed atomically. |
1800 |
> |
* Some attempted update operations on this map by other threads |
1801 |
> |
* may be blocked while computation is in progress, so the |
1802 |
> |
* computation should be short and simple, and must not attempt to |
1803 |
> |
* update any other mappings of this Map. |
1804 |
|
* |
1805 |
|
* @param key key with which the specified value is to be associated |
1806 |
|
* @param remappingFunction the function to compute a value |
1813 |
|
* @throws RuntimeException or Error if the remappingFunction does so, |
1814 |
|
* in which case the mapping is unchanged |
1815 |
|
*/ |
1816 |
< |
@SuppressWarnings("unchecked") public V compute |
1817 |
< |
(K key, BiFun<? super K, ? super V, ? extends V> remappingFunction) { |
1816 |
> |
public V compute(K key, |
1817 |
> |
BiFun<? super K, ? super V, ? extends V> remappingFunction) { |
1818 |
|
if (key == null || remappingFunction == null) |
1819 |
|
throw new NullPointerException(); |
1820 |
< |
return (V)internalCompute(key, false, remappingFunction); |
1820 |
> |
int h = spread(key.hashCode()); |
1821 |
> |
V val = null; |
1822 |
> |
int delta = 0; |
1823 |
> |
int binCount = 0; |
1824 |
> |
for (Node<K,V>[] tab = table;;) { |
1825 |
> |
Node<K,V> f; int n, i, fh; |
1826 |
> |
if (tab == null || (n = tab.length) == 0) |
1827 |
> |
tab = initTable(); |
1828 |
> |
else if ((f = tabAt(tab, i = (n - 1) & h)) == null) { |
1829 |
> |
Node<K,V> r = new ReservationNode<K,V>(); |
1830 |
> |
synchronized (r) { |
1831 |
> |
if (casTabAt(tab, i, null, r)) { |
1832 |
> |
binCount = 1; |
1833 |
> |
Node<K,V> node = null; |
1834 |
> |
try { |
1835 |
> |
if ((val = remappingFunction.apply(key, null)) != null) { |
1836 |
> |
delta = 1; |
1837 |
> |
node = new Node<K,V>(h, key, val, null); |
1838 |
> |
} |
1839 |
> |
} finally { |
1840 |
> |
setTabAt(tab, i, node); |
1841 |
> |
} |
1842 |
> |
} |
1843 |
> |
} |
1844 |
> |
if (binCount != 0) |
1845 |
> |
break; |
1846 |
> |
} |
1847 |
> |
else if ((fh = f.hash) == MOVED) |
1848 |
> |
tab = helpTransfer(tab, f); |
1849 |
> |
else { |
1850 |
> |
synchronized (f) { |
1851 |
> |
if (tabAt(tab, i) == f) { |
1852 |
> |
if (fh >= 0) { |
1853 |
> |
binCount = 1; |
1854 |
> |
for (Node<K,V> e = f, pred = null;; ++binCount) { |
1855 |
> |
K ek; |
1856 |
> |
if (e.hash == h && |
1857 |
> |
((ek = e.key) == key || |
1858 |
> |
(ek != null && key.equals(ek)))) { |
1859 |
> |
val = remappingFunction.apply(key, e.val); |
1860 |
> |
if (val != null) |
1861 |
> |
e.val = val; |
1862 |
> |
else { |
1863 |
> |
delta = -1; |
1864 |
> |
Node<K,V> 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 = remappingFunction.apply(key, null); |
1875 |
> |
if (val != null) { |
1876 |
> |
delta = 1; |
1877 |
> |
pred.next = |
1878 |
> |
new Node<K,V>(h, key, val, null); |
1879 |
> |
} |
1880 |
> |
break; |
1881 |
> |
} |
1882 |
> |
} |
1883 |
> |
} |
1884 |
> |
else if (f instanceof TreeBin) { |
1885 |
> |
binCount = 1; |
1886 |
> |
TreeBin<K,V> t = (TreeBin<K,V>)f; |
1887 |
> |
TreeNode<K,V> r, p; |
1888 |
> |
if ((r = t.root) != null) |
1889 |
> |
p = r.findTreeNode(h, key, null); |
1890 |
> |
else |
1891 |
> |
p = null; |
1892 |
> |
V pv = (p == null) ? null : p.val; |
1893 |
> |
val = remappingFunction.apply(key, pv); |
1894 |
> |
if (val != null) { |
1895 |
> |
if (p != null) |
1896 |
> |
p.val = val; |
1897 |
> |
else { |
1898 |
> |
delta = 1; |
1899 |
> |
t.putTreeVal(h, key, val); |
1900 |
> |
} |
1901 |
> |
} |
1902 |
> |
else if (p != null) { |
1903 |
> |
delta = -1; |
1904 |
> |
if (t.removeTreeNode(p)) |
1905 |
> |
setTabAt(tab, i, untreeify(t.first)); |
1906 |
> |
} |
1907 |
> |
} |
1908 |
> |
} |
1909 |
> |
} |
1910 |
> |
if (binCount != 0) { |
1911 |
> |
if (binCount >= TREEIFY_THRESHOLD) |
1912 |
> |
treeifyBin(tab, i); |
1913 |
> |
break; |
1914 |
> |
} |
1915 |
> |
} |
1916 |
> |
} |
1917 |
> |
if (delta != 0) |
1918 |
> |
addCount((long)delta, binCount); |
1919 |
> |
return val; |
1920 |
|
} |
1921 |
|
|
1922 |
|
/** |
1923 |
< |
* If the specified key is not already associated |
1924 |
< |
* with a value, associate it with the given value. |
1925 |
< |
* Otherwise, replace the value with the results of |
1926 |
< |
* the given remapping function. This is equivalent to: |
1927 |
< |
* <pre> {@code |
1928 |
< |
* if (!map.containsKey(key)) |
1929 |
< |
* map.put(value); |
1930 |
< |
* else { |
1931 |
< |
* newValue = remappingFunction.apply(map.get(key), value); |
1932 |
< |
* if (value != null) |
1933 |
< |
* map.put(key, value); |
1934 |
< |
* else |
1935 |
< |
* map.remove(key); |
1936 |
< |
* } |
1937 |
< |
* }</pre> |
1938 |
< |
* except that the action is performed atomically. If the |
1939 |
< |
* function returns {@code null}, the mapping is removed. If the |
1940 |
< |
* function itself throws an (unchecked) exception, the exception |
2967 |
< |
* is rethrown to its caller, and the current mapping is left |
2968 |
< |
* unchanged. Some attempted update operations on this map by |
2969 |
< |
* other threads may be blocked while computation is in progress, |
2970 |
< |
* so the computation should be short and simple, and must not |
2971 |
< |
* attempt to update any other mappings of this Map. |
1923 |
> |
* If the specified key is not already associated with a |
1924 |
> |
* (non-null) value, associates it with the given value. |
1925 |
> |
* Otherwise, replaces the value with the results of the given |
1926 |
> |
* remapping function, or removes if {@code null}. The entire |
1927 |
> |
* method invocation is performed atomically. Some attempted |
1928 |
> |
* update operations on this map by other threads may be blocked |
1929 |
> |
* while computation is in progress, so the computation should be |
1930 |
> |
* short and simple, and must not attempt to update any other |
1931 |
> |
* mappings of this Map. |
1932 |
> |
* |
1933 |
> |
* @param key key with which the specified value is to be associated |
1934 |
> |
* @param value the value to use if absent |
1935 |
> |
* @param remappingFunction the function to recompute a value if present |
1936 |
> |
* @return the new value associated with the specified key, or null if none |
1937 |
> |
* @throws NullPointerException if the specified key or the |
1938 |
> |
* remappingFunction is null |
1939 |
> |
* @throws RuntimeException or Error if the remappingFunction does so, |
1940 |
> |
* in which case the mapping is unchanged |
1941 |
|
*/ |
1942 |
< |
@SuppressWarnings("unchecked") public V merge |
2974 |
< |
(K key, V value, BiFun<? super V, ? super V, ? extends V> remappingFunction) { |
1942 |
> |
public V merge(K key, V value, BiFun<? super V, ? super V, ? extends V> remappingFunction) { |
1943 |
|
if (key == null || value == null || remappingFunction == null) |
1944 |
|
throw new NullPointerException(); |
1945 |
< |
return (V)internalMerge(key, value, remappingFunction); |
1945 |
> |
int h = spread(key.hashCode()); |
1946 |
> |
V val = null; |
1947 |
> |
int delta = 0; |
1948 |
> |
int binCount = 0; |
1949 |
> |
for (Node<K,V>[] tab = table;;) { |
1950 |
> |
Node<K,V> f; int n, i, fh; |
1951 |
> |
if (tab == null || (n = tab.length) == 0) |
1952 |
> |
tab = initTable(); |
1953 |
> |
else if ((f = tabAt(tab, i = (n - 1) & h)) == null) { |
1954 |
> |
if (casTabAt(tab, i, null, new Node<K,V>(h, key, value, null))) { |
1955 |
> |
delta = 1; |
1956 |
> |
val = value; |
1957 |
> |
break; |
1958 |
> |
} |
1959 |
> |
} |
1960 |
> |
else if ((fh = f.hash) == MOVED) |
1961 |
> |
tab = helpTransfer(tab, f); |
1962 |
> |
else { |
1963 |
> |
synchronized (f) { |
1964 |
> |
if (tabAt(tab, i) == f) { |
1965 |
> |
if (fh >= 0) { |
1966 |
> |
binCount = 1; |
1967 |
> |
for (Node<K,V> e = f, pred = null;; ++binCount) { |
1968 |
> |
K ek; |
1969 |
> |
if (e.hash == h && |
1970 |
> |
((ek = e.key) == key || |
1971 |
> |
(ek != null && key.equals(ek)))) { |
1972 |
> |
val = remappingFunction.apply(e.val, value); |
1973 |
> |
if (val != null) |
1974 |
> |
e.val = val; |
1975 |
> |
else { |
1976 |
> |
delta = -1; |
1977 |
> |
Node<K,V> en = e.next; |
1978 |
> |
if (pred != null) |
1979 |
> |
pred.next = en; |
1980 |
> |
else |
1981 |
> |
setTabAt(tab, i, en); |
1982 |
> |
} |
1983 |
> |
break; |
1984 |
> |
} |
1985 |
> |
pred = e; |
1986 |
> |
if ((e = e.next) == null) { |
1987 |
> |
delta = 1; |
1988 |
> |
val = value; |
1989 |
> |
pred.next = |
1990 |
> |
new Node<K,V>(h, key, val, null); |
1991 |
> |
break; |
1992 |
> |
} |
1993 |
> |
} |
1994 |
> |
} |
1995 |
> |
else if (f instanceof TreeBin) { |
1996 |
> |
binCount = 2; |
1997 |
> |
TreeBin<K,V> t = (TreeBin<K,V>)f; |
1998 |
> |
TreeNode<K,V> r = t.root; |
1999 |
> |
TreeNode<K,V> p = (r == null) ? null : |
2000 |
> |
r.findTreeNode(h, key, null); |
2001 |
> |
val = (p == null) ? value : |
2002 |
> |
remappingFunction.apply(p.val, value); |
2003 |
> |
if (val != null) { |
2004 |
> |
if (p != null) |
2005 |
> |
p.val = val; |
2006 |
> |
else { |
2007 |
> |
delta = 1; |
2008 |
> |
t.putTreeVal(h, key, val); |
2009 |
> |
} |
2010 |
> |
} |
2011 |
> |
else if (p != null) { |
2012 |
> |
delta = -1; |
2013 |
> |
if (t.removeTreeNode(p)) |
2014 |
> |
setTabAt(tab, i, untreeify(t.first)); |
2015 |
> |
} |
2016 |
> |
} |
2017 |
> |
} |
2018 |
> |
} |
2019 |
> |
if (binCount != 0) { |
2020 |
> |
if (binCount >= TREEIFY_THRESHOLD) |
2021 |
> |
treeifyBin(tab, i); |
2022 |
> |
break; |
2023 |
> |
} |
2024 |
> |
} |
2025 |
> |
} |
2026 |
> |
if (delta != 0) |
2027 |
> |
addCount((long)delta, binCount); |
2028 |
> |
return val; |
2029 |
|
} |
2030 |
|
|
2031 |
+ |
// Hashtable legacy methods |
2032 |
+ |
|
2033 |
|
/** |
2034 |
< |
* Removes the key (and its corresponding value) from this map. |
2035 |
< |
* This method does nothing if the key is not in the map. |
2034 |
> |
* Legacy method testing if some key maps into the specified value |
2035 |
> |
* in this table. This method is identical in functionality to |
2036 |
> |
* {@link #containsValue(Object)}, and exists solely to ensure |
2037 |
> |
* full compatibility with class {@link java.util.Hashtable}, |
2038 |
> |
* which supported this method prior to introduction of the |
2039 |
> |
* Java Collections framework. |
2040 |
|
* |
2041 |
< |
* @param key the key that needs to be removed |
2042 |
< |
* @return the previous value associated with {@code key}, or |
2043 |
< |
* {@code null} if there was no mapping for {@code key} |
2044 |
< |
* @throws NullPointerException if the specified key is null |
2041 |
> |
* @param value a value to search for |
2042 |
> |
* @return {@code true} if and only if some key maps to the |
2043 |
> |
* {@code value} argument in this table as |
2044 |
> |
* determined by the {@code equals} method; |
2045 |
> |
* {@code false} otherwise |
2046 |
> |
* @throws NullPointerException if the specified value is null |
2047 |
|
*/ |
2048 |
< |
@SuppressWarnings("unchecked") public V remove(Object key) { |
2049 |
< |
if (key == null) |
2991 |
< |
throw new NullPointerException(); |
2992 |
< |
return (V)internalReplace(key, null, null); |
2048 |
> |
@Deprecated public boolean contains(Object value) { |
2049 |
> |
return containsValue(value); |
2050 |
|
} |
2051 |
|
|
2052 |
|
/** |
2053 |
< |
* {@inheritDoc} |
2053 |
> |
* Returns an enumeration of the keys in this table. |
2054 |
|
* |
2055 |
< |
* @throws NullPointerException if the specified key is null |
2055 |
> |
* @return an enumeration of the keys in this table |
2056 |
> |
* @see #keySet() |
2057 |
|
*/ |
2058 |
< |
public boolean remove(Object key, Object value) { |
2059 |
< |
if (key == null) |
2060 |
< |
throw new NullPointerException(); |
2061 |
< |
if (value == null) |
3004 |
< |
return false; |
3005 |
< |
return internalReplace(key, null, value) != null; |
2058 |
> |
public Enumeration<K> keys() { |
2059 |
> |
Node<K,V>[] t; |
2060 |
> |
int f = (t = table) == null ? 0 : t.length; |
2061 |
> |
return new KeyIterator<K,V>(t, f, 0, f, this); |
2062 |
|
} |
2063 |
|
|
2064 |
|
/** |
2065 |
< |
* {@inheritDoc} |
2065 |
> |
* Returns an enumeration of the values in this table. |
2066 |
|
* |
2067 |
< |
* @throws NullPointerException if any of the arguments are null |
2067 |
> |
* @return an enumeration of the values in this table |
2068 |
> |
* @see #values() |
2069 |
|
*/ |
2070 |
< |
public boolean replace(K key, V oldValue, V newValue) { |
2071 |
< |
if (key == null || oldValue == null || newValue == null) |
2072 |
< |
throw new NullPointerException(); |
2073 |
< |
return internalReplace(key, newValue, oldValue) != null; |
2070 |
> |
public Enumeration<V> elements() { |
2071 |
> |
Node<K,V>[] t; |
2072 |
> |
int f = (t = table) == null ? 0 : t.length; |
2073 |
> |
return new ValueIterator<K,V>(t, f, 0, f, this); |
2074 |
|
} |
2075 |
|
|
2076 |
+ |
// ConcurrentHashMapV8-only methods |
2077 |
+ |
|
2078 |
|
/** |
2079 |
< |
* {@inheritDoc} |
2079 |
> |
* Returns the number of mappings. This method should be used |
2080 |
> |
* instead of {@link #size} because a ConcurrentHashMapV8 may |
2081 |
> |
* contain more mappings than can be represented as an int. The |
2082 |
> |
* value returned is an estimate; the actual count may differ if |
2083 |
> |
* there are concurrent insertions or removals. |
2084 |
|
* |
2085 |
< |
* @return the previous value associated with the specified key, |
2086 |
< |
* or {@code null} if there was no mapping for the key |
3024 |
< |
* @throws NullPointerException if the specified key or value is null |
2085 |
> |
* @return the number of mappings |
2086 |
> |
* @since 1.8 |
2087 |
|
*/ |
2088 |
< |
@SuppressWarnings("unchecked") public V replace(K key, V value) { |
2089 |
< |
if (key == null || value == null) |
2090 |
< |
throw new NullPointerException(); |
3029 |
< |
return (V)internalReplace(key, value, null); |
2088 |
> |
public long mappingCount() { |
2089 |
> |
long n = sumCount(); |
2090 |
> |
return (n < 0L) ? 0L : n; // ignore transient negative values |
2091 |
|
} |
2092 |
|
|
2093 |
|
/** |
2094 |
< |
* Removes all of the mappings from this map. |
2094 |
> |
* Creates a new {@link Set} backed by a ConcurrentHashMapV8 |
2095 |
> |
* from the given type to {@code Boolean.TRUE}. |
2096 |
> |
* |
2097 |
> |
* @return the new set |
2098 |
> |
* @since 1.8 |
2099 |
|
*/ |
2100 |
< |
public void clear() { |
2101 |
< |
internalClear(); |
2100 |
> |
public static <K> KeySetView<K,Boolean> newKeySet() { |
2101 |
> |
return new KeySetView<K,Boolean> |
2102 |
> |
(new ConcurrentHashMapV8<K,Boolean>(), Boolean.TRUE); |
2103 |
|
} |
2104 |
|
|
2105 |
|
/** |
2106 |
< |
* Returns a {@link Set} view of the keys contained in this map. |
2107 |
< |
* The set is backed by the map, so changes to the map are |
3042 |
< |
* reflected in the set, and vice-versa. |
2106 |
> |
* Creates a new {@link Set} backed by a ConcurrentHashMapV8 |
2107 |
> |
* from the given type to {@code Boolean.TRUE}. |
2108 |
|
* |
2109 |
< |
* @return the set view |
2109 |
> |
* @param initialCapacity The implementation performs internal |
2110 |
> |
* sizing to accommodate this many elements. |
2111 |
> |
* @return the new set |
2112 |
> |
* @throws IllegalArgumentException if the initial capacity of |
2113 |
> |
* elements is negative |
2114 |
> |
* @since 1.8 |
2115 |
|
*/ |
2116 |
< |
public KeySetView<K,V> keySet() { |
2117 |
< |
KeySetView<K,V> ks = keySet; |
2118 |
< |
return (ks != null) ? ks : (keySet = new KeySetView<K,V>(this, null)); |
2116 |
> |
public static <K> KeySetView<K,Boolean> newKeySet(int initialCapacity) { |
2117 |
> |
return new KeySetView<K,Boolean> |
2118 |
> |
(new ConcurrentHashMapV8<K,Boolean>(initialCapacity), Boolean.TRUE); |
2119 |
|
} |
2120 |
|
|
2121 |
|
/** |
2122 |
|
* Returns a {@link Set} view of the keys in this map, using the |
2123 |
|
* given common mapped value for any additions (i.e., {@link |
2124 |
< |
* Collection#add} and {@link Collection#addAll}). This is of |
2125 |
< |
* course only appropriate if it is acceptable to use the same |
2126 |
< |
* value for all additions from this view. |
2124 |
> |
* Collection#add} and {@link Collection#addAll(Collection)}). |
2125 |
> |
* This is of course only appropriate if it is acceptable to use |
2126 |
> |
* the same value for all additions from this view. |
2127 |
|
* |
2128 |
< |
* @param mappedValue the mapped value to use for any |
3059 |
< |
* additions. |
2128 |
> |
* @param mappedValue the mapped value to use for any additions |
2129 |
|
* @return the set view |
2130 |
|
* @throws NullPointerException if the mappedValue is null |
2131 |
|
*/ |
2135 |
|
return new KeySetView<K,V>(this, mappedValue); |
2136 |
|
} |
2137 |
|
|
2138 |
+ |
/* ---------------- Special Nodes -------------- */ |
2139 |
+ |
|
2140 |
|
/** |
2141 |
< |
* Returns a {@link Collection} view of the values contained in this map. |
3071 |
< |
* The collection is backed by the map, so changes to the map are |
3072 |
< |
* reflected in the collection, and vice-versa. |
2141 |
> |
* A node inserted at head of bins during transfer operations. |
2142 |
|
*/ |
2143 |
< |
public ValuesView<K,V> values() { |
2144 |
< |
ValuesView<K,V> vs = values; |
2145 |
< |
return (vs != null) ? vs : (values = new ValuesView<K,V>(this)); |
2143 |
> |
static final class ForwardingNode<K,V> extends Node<K,V> { |
2144 |
> |
final Node<K,V>[] nextTable; |
2145 |
> |
ForwardingNode(Node<K,V>[] tab) { |
2146 |
> |
super(MOVED, null, null, null); |
2147 |
> |
this.nextTable = tab; |
2148 |
> |
} |
2149 |
> |
|
2150 |
> |
Node<K,V> find(int h, Object k) { |
2151 |
> |
// loop to avoid arbitrarily deep recursion on forwarding nodes |
2152 |
> |
outer: for (Node<K,V>[] tab = nextTable;;) { |
2153 |
> |
Node<K,V> e; int n; |
2154 |
> |
if (k == null || tab == null || (n = tab.length) == 0 || |
2155 |
> |
(e = tabAt(tab, (n - 1) & h)) == null) |
2156 |
> |
return null; |
2157 |
> |
for (;;) { |
2158 |
> |
int eh; K ek; |
2159 |
> |
if ((eh = e.hash) == h && |
2160 |
> |
((ek = e.key) == k || (ek != null && k.equals(ek)))) |
2161 |
> |
return e; |
2162 |
> |
if (eh < 0) { |
2163 |
> |
if (e instanceof ForwardingNode) { |
2164 |
> |
tab = ((ForwardingNode<K,V>)e).nextTable; |
2165 |
> |
continue outer; |
2166 |
> |
} |
2167 |
> |
else |
2168 |
> |
return e.find(h, k); |
2169 |
> |
} |
2170 |
> |
if ((e = e.next) == null) |
2171 |
> |
return null; |
2172 |
> |
} |
2173 |
> |
} |
2174 |
> |
} |
2175 |
|
} |
2176 |
|
|
2177 |
|
/** |
2178 |
< |
* Returns a {@link Set} view of the mappings contained in this map. |
3081 |
< |
* The set is backed by the map, so changes to the map are |
3082 |
< |
* reflected in the set, and vice-versa. The set supports element |
3083 |
< |
* removal, which removes the corresponding mapping from the map, |
3084 |
< |
* via the {@code Iterator.remove}, {@code Set.remove}, |
3085 |
< |
* {@code removeAll}, {@code retainAll}, and {@code clear} |
3086 |
< |
* operations. It does not support the {@code add} or |
3087 |
< |
* {@code addAll} operations. |
3088 |
< |
* |
3089 |
< |
* <p>The view's {@code iterator} is a "weakly consistent" iterator |
3090 |
< |
* that will never throw {@link ConcurrentModificationException}, |
3091 |
< |
* and guarantees to traverse elements as they existed upon |
3092 |
< |
* construction of the iterator, and may (but is not guaranteed to) |
3093 |
< |
* reflect any modifications subsequent to construction. |
2178 |
> |
* A place-holder node used in computeIfAbsent and compute |
2179 |
|
*/ |
2180 |
< |
public Set<Map.Entry<K,V>> entrySet() { |
2181 |
< |
EntrySetView<K,V> es = entrySet; |
2182 |
< |
return (es != null) ? es : (entrySet = new EntrySetView<K,V>(this)); |
2180 |
> |
static final class ReservationNode<K,V> extends Node<K,V> { |
2181 |
> |
ReservationNode() { |
2182 |
> |
super(RESERVED, null, null, null); |
2183 |
> |
} |
2184 |
> |
|
2185 |
> |
Node<K,V> find(int h, Object k) { |
2186 |
> |
return null; |
2187 |
> |
} |
2188 |
|
} |
2189 |
|
|
2190 |
+ |
/* ---------------- Table Initialization and Resizing -------------- */ |
2191 |
+ |
|
2192 |
|
/** |
2193 |
< |
* Returns an enumeration of the keys in this table. |
3102 |
< |
* |
3103 |
< |
* @return an enumeration of the keys in this table |
3104 |
< |
* @see #keySet() |
2193 |
> |
* Initializes table, using the size recorded in sizeCtl. |
2194 |
|
*/ |
2195 |
< |
public Enumeration<K> keys() { |
2196 |
< |
return new KeyIterator<K,V>(this); |
2195 |
> |
private final Node<K,V>[] initTable() { |
2196 |
> |
Node<K,V>[] tab; int sc; |
2197 |
> |
while ((tab = table) == null || tab.length == 0) { |
2198 |
> |
if ((sc = sizeCtl) < 0) |
2199 |
> |
Thread.yield(); // lost initialization race; just spin |
2200 |
> |
else if (U.compareAndSwapInt(this, SIZECTL, sc, -1)) { |
2201 |
> |
try { |
2202 |
> |
if ((tab = table) == null || tab.length == 0) { |
2203 |
> |
int n = (sc > 0) ? sc : DEFAULT_CAPACITY; |
2204 |
> |
@SuppressWarnings({"rawtypes","unchecked"}) |
2205 |
> |
Node<K,V>[] nt = (Node<K,V>[])new Node[n]; |
2206 |
> |
table = tab = nt; |
2207 |
> |
sc = n - (n >>> 2); |
2208 |
> |
} |
2209 |
> |
} finally { |
2210 |
> |
sizeCtl = sc; |
2211 |
> |
} |
2212 |
> |
break; |
2213 |
> |
} |
2214 |
> |
} |
2215 |
> |
return tab; |
2216 |
|
} |
2217 |
|
|
2218 |
|
/** |
2219 |
< |
* Returns an enumeration of the values in this table. |
2220 |
< |
* |
2221 |
< |
* @return an enumeration of the values in this table |
2222 |
< |
* @see #values() |
2219 |
> |
* Adds to count, and if table is too small and not already |
2220 |
> |
* resizing, initiates transfer. If already resizing, helps |
2221 |
> |
* perform transfer if work is available. Rechecks occupancy |
2222 |
> |
* after a transfer to see if another resize is already needed |
2223 |
> |
* because resizings are lagging additions. |
2224 |
> |
* |
2225 |
> |
* @param x the count to add |
2226 |
> |
* @param check if <0, don't check resize, if <= 1 only check if uncontended |
2227 |
> |
*/ |
2228 |
> |
private final void addCount(long x, int check) { |
2229 |
> |
CounterCell[] as; long b, s; |
2230 |
> |
if ((as = counterCells) != null || |
2231 |
> |
!U.compareAndSwapLong(this, BASECOUNT, b = baseCount, s = b + x)) { |
2232 |
> |
CounterHashCode hc; CounterCell a; long v; int m; |
2233 |
> |
boolean uncontended = true; |
2234 |
> |
if ((hc = threadCounterHashCode.get()) == null || |
2235 |
> |
as == null || (m = as.length - 1) < 0 || |
2236 |
> |
(a = as[m & hc.code]) == null || |
2237 |
> |
!(uncontended = |
2238 |
> |
U.compareAndSwapLong(a, CELLVALUE, v = a.value, v + x))) { |
2239 |
> |
fullAddCount(x, hc, uncontended); |
2240 |
> |
return; |
2241 |
> |
} |
2242 |
> |
if (check <= 1) |
2243 |
> |
return; |
2244 |
> |
s = sumCount(); |
2245 |
> |
} |
2246 |
> |
if (check >= 0) { |
2247 |
> |
Node<K,V>[] tab, nt; int sc; |
2248 |
> |
while (s >= (long)(sc = sizeCtl) && (tab = table) != null && |
2249 |
> |
tab.length < MAXIMUM_CAPACITY) { |
2250 |
> |
if (sc < 0) { |
2251 |
> |
if (sc == -1 || transferIndex <= transferOrigin || |
2252 |
> |
(nt = nextTable) == null) |
2253 |
> |
break; |
2254 |
> |
if (U.compareAndSwapInt(this, SIZECTL, sc, sc - 1)) |
2255 |
> |
transfer(tab, nt); |
2256 |
> |
} |
2257 |
> |
else if (U.compareAndSwapInt(this, SIZECTL, sc, -2)) |
2258 |
> |
transfer(tab, null); |
2259 |
> |
s = sumCount(); |
2260 |
> |
} |
2261 |
> |
} |
2262 |
> |
} |
2263 |
> |
|
2264 |
> |
/** |
2265 |
> |
* Helps transfer if a resize is in progress. |
2266 |
|
*/ |
2267 |
< |
public Enumeration<V> elements() { |
2268 |
< |
return new ValueIterator<K,V>(this); |
2267 |
> |
final Node<K,V>[] helpTransfer(Node<K,V>[] tab, Node<K,V> f) { |
2268 |
> |
Node<K,V>[] nextTab; int sc; |
2269 |
> |
if ((f instanceof ForwardingNode) && |
2270 |
> |
(nextTab = ((ForwardingNode<K,V>)f).nextTable) != null) { |
2271 |
> |
if (nextTab == nextTable && tab == table && |
2272 |
> |
transferIndex > transferOrigin && (sc = sizeCtl) < -1 && |
2273 |
> |
U.compareAndSwapInt(this, SIZECTL, sc, sc - 1)) |
2274 |
> |
transfer(tab, nextTab); |
2275 |
> |
return nextTab; |
2276 |
> |
} |
2277 |
> |
return table; |
2278 |
|
} |
2279 |
|
|
2280 |
|
/** |
2281 |
< |
* Returns a partitionable iterator of the keys in this map. |
2281 |
> |
* Tries to presize table to accommodate the given number of elements. |
2282 |
|
* |
2283 |
< |
* @return a partitionable iterator of the keys in this map |
2283 |
> |
* @param size number of elements (doesn't need to be perfectly accurate) |
2284 |
|
*/ |
2285 |
< |
public Spliterator<K> keySpliterator() { |
2286 |
< |
return new KeyIterator<K,V>(this); |
2285 |
> |
private final void tryPresize(int size) { |
2286 |
> |
int c = (size >= (MAXIMUM_CAPACITY >>> 1)) ? MAXIMUM_CAPACITY : |
2287 |
> |
tableSizeFor(size + (size >>> 1) + 1); |
2288 |
> |
int sc; |
2289 |
> |
while ((sc = sizeCtl) >= 0) { |
2290 |
> |
Node<K,V>[] tab = table; int n; |
2291 |
> |
if (tab == null || (n = tab.length) == 0) { |
2292 |
> |
n = (sc > c) ? sc : c; |
2293 |
> |
if (U.compareAndSwapInt(this, SIZECTL, sc, -1)) { |
2294 |
> |
try { |
2295 |
> |
if (table == tab) { |
2296 |
> |
@SuppressWarnings({"rawtypes","unchecked"}) |
2297 |
> |
Node<K,V>[] nt = (Node<K,V>[])new Node[n]; |
2298 |
> |
table = nt; |
2299 |
> |
sc = n - (n >>> 2); |
2300 |
> |
} |
2301 |
> |
} finally { |
2302 |
> |
sizeCtl = sc; |
2303 |
> |
} |
2304 |
> |
} |
2305 |
> |
} |
2306 |
> |
else if (c <= sc || n >= MAXIMUM_CAPACITY) |
2307 |
> |
break; |
2308 |
> |
else if (tab == table && |
2309 |
> |
U.compareAndSwapInt(this, SIZECTL, sc, -2)) |
2310 |
> |
transfer(tab, null); |
2311 |
> |
} |
2312 |
|
} |
2313 |
|
|
2314 |
|
/** |
2315 |
< |
* Returns a partitionable iterator of the values in this map. |
2316 |
< |
* |
3132 |
< |
* @return a partitionable iterator of the values in this map |
2315 |
> |
* Moves and/or copies the nodes in each bin to new table. See |
2316 |
> |
* above for explanation. |
2317 |
|
*/ |
2318 |
< |
public Spliterator<V> valueSpliterator() { |
2319 |
< |
return new ValueIterator<K,V>(this); |
2318 |
> |
private final void transfer(Node<K,V>[] tab, Node<K,V>[] nextTab) { |
2319 |
> |
int n = tab.length, stride; |
2320 |
> |
if ((stride = (NCPU > 1) ? (n >>> 3) / NCPU : n) < MIN_TRANSFER_STRIDE) |
2321 |
> |
stride = MIN_TRANSFER_STRIDE; // subdivide range |
2322 |
> |
if (nextTab == null) { // initiating |
2323 |
> |
try { |
2324 |
> |
@SuppressWarnings({"rawtypes","unchecked"}) |
2325 |
> |
Node<K,V>[] nt = (Node<K,V>[])new Node[n << 1]; |
2326 |
> |
nextTab = nt; |
2327 |
> |
} catch (Throwable ex) { // try to cope with OOME |
2328 |
> |
sizeCtl = Integer.MAX_VALUE; |
2329 |
> |
return; |
2330 |
> |
} |
2331 |
> |
nextTable = nextTab; |
2332 |
> |
transferOrigin = n; |
2333 |
> |
transferIndex = n; |
2334 |
> |
ForwardingNode<K,V> rev = new ForwardingNode<K,V>(tab); |
2335 |
> |
for (int k = n; k > 0;) { // progressively reveal ready slots |
2336 |
> |
int nextk = (k > stride) ? k - stride : 0; |
2337 |
> |
for (int m = nextk; m < k; ++m) |
2338 |
> |
nextTab[m] = rev; |
2339 |
> |
for (int m = n + nextk; m < n + k; ++m) |
2340 |
> |
nextTab[m] = rev; |
2341 |
> |
U.putOrderedInt(this, TRANSFERORIGIN, k = nextk); |
2342 |
> |
} |
2343 |
> |
} |
2344 |
> |
int nextn = nextTab.length; |
2345 |
> |
ForwardingNode<K,V> fwd = new ForwardingNode<K,V>(nextTab); |
2346 |
> |
boolean advance = true; |
2347 |
> |
boolean finishing = false; // to ensure sweep before committing nextTab |
2348 |
> |
for (int i = 0, bound = 0;;) { |
2349 |
> |
int nextIndex, nextBound, fh; Node<K,V> f; |
2350 |
> |
while (advance) { |
2351 |
> |
if (--i >= bound || finishing) |
2352 |
> |
advance = false; |
2353 |
> |
else if ((nextIndex = transferIndex) <= transferOrigin) { |
2354 |
> |
i = -1; |
2355 |
> |
advance = false; |
2356 |
> |
} |
2357 |
> |
else if (U.compareAndSwapInt |
2358 |
> |
(this, TRANSFERINDEX, nextIndex, |
2359 |
> |
nextBound = (nextIndex > stride ? |
2360 |
> |
nextIndex - stride : 0))) { |
2361 |
> |
bound = nextBound; |
2362 |
> |
i = nextIndex - 1; |
2363 |
> |
advance = false; |
2364 |
> |
} |
2365 |
> |
} |
2366 |
> |
if (i < 0 || i >= n || i + n >= nextn) { |
2367 |
> |
if (finishing) { |
2368 |
> |
nextTable = null; |
2369 |
> |
table = nextTab; |
2370 |
> |
sizeCtl = (n << 1) - (n >>> 1); |
2371 |
> |
return; |
2372 |
> |
} |
2373 |
> |
for (int sc;;) { |
2374 |
> |
if (U.compareAndSwapInt(this, SIZECTL, sc = sizeCtl, ++sc)) { |
2375 |
> |
if (sc != -1) |
2376 |
> |
return; |
2377 |
> |
finishing = advance = true; |
2378 |
> |
i = n; // recheck before commit |
2379 |
> |
break; |
2380 |
> |
} |
2381 |
> |
} |
2382 |
> |
} |
2383 |
> |
else if ((f = tabAt(tab, i)) == null) { |
2384 |
> |
if (casTabAt(tab, i, null, fwd)) { |
2385 |
> |
setTabAt(nextTab, i, null); |
2386 |
> |
setTabAt(nextTab, i + n, null); |
2387 |
> |
advance = true; |
2388 |
> |
} |
2389 |
> |
} |
2390 |
> |
else if ((fh = f.hash) == MOVED) |
2391 |
> |
advance = true; // already processed |
2392 |
> |
else { |
2393 |
> |
synchronized (f) { |
2394 |
> |
if (tabAt(tab, i) == f) { |
2395 |
> |
Node<K,V> ln, hn; |
2396 |
> |
if (fh >= 0) { |
2397 |
> |
int runBit = fh & n; |
2398 |
> |
Node<K,V> lastRun = f; |
2399 |
> |
for (Node<K,V> p = f.next; p != null; p = p.next) { |
2400 |
> |
int b = p.hash & n; |
2401 |
> |
if (b != runBit) { |
2402 |
> |
runBit = b; |
2403 |
> |
lastRun = p; |
2404 |
> |
} |
2405 |
> |
} |
2406 |
> |
if (runBit == 0) { |
2407 |
> |
ln = lastRun; |
2408 |
> |
hn = null; |
2409 |
> |
} |
2410 |
> |
else { |
2411 |
> |
hn = lastRun; |
2412 |
> |
ln = null; |
2413 |
> |
} |
2414 |
> |
for (Node<K,V> p = f; p != lastRun; p = p.next) { |
2415 |
> |
int ph = p.hash; K pk = p.key; V pv = p.val; |
2416 |
> |
if ((ph & n) == 0) |
2417 |
> |
ln = new Node<K,V>(ph, pk, pv, ln); |
2418 |
> |
else |
2419 |
> |
hn = new Node<K,V>(ph, pk, pv, hn); |
2420 |
> |
} |
2421 |
> |
setTabAt(nextTab, i, ln); |
2422 |
> |
setTabAt(nextTab, i + n, hn); |
2423 |
> |
setTabAt(tab, i, fwd); |
2424 |
> |
advance = true; |
2425 |
> |
} |
2426 |
> |
else if (f instanceof TreeBin) { |
2427 |
> |
TreeBin<K,V> t = (TreeBin<K,V>)f; |
2428 |
> |
TreeNode<K,V> lo = null, loTail = null; |
2429 |
> |
TreeNode<K,V> hi = null, hiTail = null; |
2430 |
> |
int lc = 0, hc = 0; |
2431 |
> |
for (Node<K,V> e = t.first; e != null; e = e.next) { |
2432 |
> |
int h = e.hash; |
2433 |
> |
TreeNode<K,V> p = new TreeNode<K,V> |
2434 |
> |
(h, e.key, e.val, null, null); |
2435 |
> |
if ((h & n) == 0) { |
2436 |
> |
if ((p.prev = loTail) == null) |
2437 |
> |
lo = p; |
2438 |
> |
else |
2439 |
> |
loTail.next = p; |
2440 |
> |
loTail = p; |
2441 |
> |
++lc; |
2442 |
> |
} |
2443 |
> |
else { |
2444 |
> |
if ((p.prev = hiTail) == null) |
2445 |
> |
hi = p; |
2446 |
> |
else |
2447 |
> |
hiTail.next = p; |
2448 |
> |
hiTail = p; |
2449 |
> |
++hc; |
2450 |
> |
} |
2451 |
> |
} |
2452 |
> |
ln = (lc <= UNTREEIFY_THRESHOLD) ? untreeify(lo) : |
2453 |
> |
(hc != 0) ? new TreeBin<K,V>(lo) : t; |
2454 |
> |
hn = (hc <= UNTREEIFY_THRESHOLD) ? untreeify(hi) : |
2455 |
> |
(lc != 0) ? new TreeBin<K,V>(hi) : t; |
2456 |
> |
setTabAt(nextTab, i, ln); |
2457 |
> |
setTabAt(nextTab, i + n, hn); |
2458 |
> |
setTabAt(tab, i, fwd); |
2459 |
> |
advance = true; |
2460 |
> |
} |
2461 |
> |
} |
2462 |
> |
} |
2463 |
> |
} |
2464 |
> |
} |
2465 |
|
} |
2466 |
|
|
2467 |
+ |
/* ---------------- Conversion from/to TreeBins -------------- */ |
2468 |
+ |
|
2469 |
|
/** |
2470 |
< |
* Returns a partitionable iterator of the entries in this map. |
2471 |
< |
* |
3141 |
< |
* @return a partitionable iterator of the entries in this map |
2470 |
> |
* Replaces all linked nodes in bin at given index unless table is |
2471 |
> |
* too small, in which case resizes instead. |
2472 |
|
*/ |
2473 |
< |
public Spliterator<Map.Entry<K,V>> entrySpliterator() { |
2474 |
< |
return new EntryIterator<K,V>(this); |
2473 |
> |
private final void treeifyBin(Node<K,V>[] tab, int index) { |
2474 |
> |
Node<K,V> b; int n, sc; |
2475 |
> |
if (tab != null) { |
2476 |
> |
if ((n = tab.length) < MIN_TREEIFY_CAPACITY) { |
2477 |
> |
if (tab == table && (sc = sizeCtl) >= 0 && |
2478 |
> |
U.compareAndSwapInt(this, SIZECTL, sc, -2)) |
2479 |
> |
transfer(tab, null); |
2480 |
> |
} |
2481 |
> |
else if ((b = tabAt(tab, index)) != null && b.hash >= 0) { |
2482 |
> |
synchronized (b) { |
2483 |
> |
if (tabAt(tab, index) == b) { |
2484 |
> |
TreeNode<K,V> hd = null, tl = null; |
2485 |
> |
for (Node<K,V> e = b; e != null; e = e.next) { |
2486 |
> |
TreeNode<K,V> p = |
2487 |
> |
new TreeNode<K,V>(e.hash, e.key, e.val, |
2488 |
> |
null, null); |
2489 |
> |
if ((p.prev = tl) == null) |
2490 |
> |
hd = p; |
2491 |
> |
else |
2492 |
> |
tl.next = p; |
2493 |
> |
tl = p; |
2494 |
> |
} |
2495 |
> |
setTabAt(tab, index, new TreeBin<K,V>(hd)); |
2496 |
> |
} |
2497 |
> |
} |
2498 |
> |
} |
2499 |
> |
} |
2500 |
|
} |
2501 |
|
|
2502 |
|
/** |
2503 |
< |
* Returns the hash code value for this {@link Map}, i.e., |
3149 |
< |
* the sum of, for each key-value pair in the map, |
3150 |
< |
* {@code key.hashCode() ^ value.hashCode()}. |
3151 |
< |
* |
3152 |
< |
* @return the hash code value for this map |
2503 |
> |
* Returns a list on non-TreeNodes replacing those in given list. |
2504 |
|
*/ |
2505 |
< |
public int hashCode() { |
2506 |
< |
int h = 0; |
2507 |
< |
Traverser<K,V,Object> it = new Traverser<K,V,Object>(this); |
2508 |
< |
Object v; |
2509 |
< |
while ((v = it.advance()) != null) { |
2510 |
< |
h += it.nextKey.hashCode() ^ v.hashCode(); |
2505 |
> |
static <K,V> Node<K,V> untreeify(Node<K,V> b) { |
2506 |
> |
Node<K,V> hd = null, tl = null; |
2507 |
> |
for (Node<K,V> q = b; q != null; q = q.next) { |
2508 |
> |
Node<K,V> p = new Node<K,V>(q.hash, q.key, q.val, null); |
2509 |
> |
if (tl == null) |
2510 |
> |
hd = p; |
2511 |
> |
else |
2512 |
> |
tl.next = p; |
2513 |
> |
tl = p; |
2514 |
|
} |
2515 |
< |
return h; |
2515 |
> |
return hd; |
2516 |
|
} |
2517 |
|
|
2518 |
+ |
/* ---------------- TreeNodes -------------- */ |
2519 |
+ |
|
2520 |
|
/** |
2521 |
< |
* Returns a string representation of this map. The string |
3166 |
< |
* representation consists of a list of key-value mappings (in no |
3167 |
< |
* particular order) enclosed in braces ("{@code {}}"). Adjacent |
3168 |
< |
* mappings are separated by the characters {@code ", "} (comma |
3169 |
< |
* and space). Each key-value mapping is rendered as the key |
3170 |
< |
* followed by an equals sign ("{@code =}") followed by the |
3171 |
< |
* associated value. |
3172 |
< |
* |
3173 |
< |
* @return a string representation of this map |
2521 |
> |
* Nodes for use in TreeBins |
2522 |
|
*/ |
2523 |
< |
public String toString() { |
2524 |
< |
Traverser<K,V,Object> it = new Traverser<K,V,Object>(this); |
2525 |
< |
StringBuilder sb = new StringBuilder(); |
2526 |
< |
sb.append('{'); |
2527 |
< |
Object v; |
2528 |
< |
if ((v = it.advance()) != null) { |
2529 |
< |
for (;;) { |
2530 |
< |
Object k = it.nextKey; |
2531 |
< |
sb.append(k == this ? "(this Map)" : k); |
2532 |
< |
sb.append('='); |
2533 |
< |
sb.append(v == this ? "(this Map)" : v); |
2534 |
< |
if ((v = it.advance()) == null) |
2523 |
> |
static final class TreeNode<K,V> extends Node<K,V> { |
2524 |
> |
TreeNode<K,V> parent; // red-black tree links |
2525 |
> |
TreeNode<K,V> left; |
2526 |
> |
TreeNode<K,V> right; |
2527 |
> |
TreeNode<K,V> prev; // needed to unlink next upon deletion |
2528 |
> |
boolean red; |
2529 |
> |
|
2530 |
> |
TreeNode(int hash, K key, V val, Node<K,V> next, |
2531 |
> |
TreeNode<K,V> parent) { |
2532 |
> |
super(hash, key, val, next); |
2533 |
> |
this.parent = parent; |
2534 |
> |
} |
2535 |
> |
|
2536 |
> |
Node<K,V> find(int h, Object k) { |
2537 |
> |
return findTreeNode(h, k, null); |
2538 |
> |
} |
2539 |
> |
|
2540 |
> |
/** |
2541 |
> |
* Returns the TreeNode (or null if not found) for the given key |
2542 |
> |
* starting at given root. |
2543 |
> |
*/ |
2544 |
> |
final TreeNode<K,V> findTreeNode(int h, Object k, Class<?> kc) { |
2545 |
> |
if (k != null) { |
2546 |
> |
TreeNode<K,V> p = this; |
2547 |
> |
do { |
2548 |
> |
int ph, dir; K pk; TreeNode<K,V> q; |
2549 |
> |
TreeNode<K,V> pl = p.left, pr = p.right; |
2550 |
> |
if ((ph = p.hash) > h) |
2551 |
> |
p = pl; |
2552 |
> |
else if (ph < h) |
2553 |
> |
p = pr; |
2554 |
> |
else if ((pk = p.key) == k || (pk != null && k.equals(pk))) |
2555 |
> |
return p; |
2556 |
> |
else if (pl == null) |
2557 |
> |
p = pr; |
2558 |
> |
else if (pr == null) |
2559 |
> |
p = pl; |
2560 |
> |
else if ((kc != null || |
2561 |
> |
(kc = comparableClassFor(k)) != null) && |
2562 |
> |
(dir = compareComparables(kc, k, pk)) != 0) |
2563 |
> |
p = (dir < 0) ? pl : pr; |
2564 |
> |
else if ((q = pr.findTreeNode(h, k, kc)) != null) |
2565 |
> |
return q; |
2566 |
> |
else |
2567 |
> |
p = pl; |
2568 |
> |
} while (p != null); |
2569 |
> |
} |
2570 |
> |
return null; |
2571 |
> |
} |
2572 |
> |
} |
2573 |
> |
|
2574 |
> |
/* ---------------- TreeBins -------------- */ |
2575 |
> |
|
2576 |
> |
/** |
2577 |
> |
* TreeNodes used at the heads of bins. TreeBins do not hold user |
2578 |
> |
* keys or values, but instead point to list of TreeNodes and |
2579 |
> |
* their root. They also maintain a parasitic read-write lock |
2580 |
> |
* forcing writers (who hold bin lock) to wait for readers (who do |
2581 |
> |
* not) to complete before tree restructuring operations. |
2582 |
> |
*/ |
2583 |
> |
static final class TreeBin<K,V> extends Node<K,V> { |
2584 |
> |
TreeNode<K,V> root; |
2585 |
> |
volatile TreeNode<K,V> first; |
2586 |
> |
volatile Thread waiter; |
2587 |
> |
volatile int lockState; |
2588 |
> |
// values for lockState |
2589 |
> |
static final int WRITER = 1; // set while holding write lock |
2590 |
> |
static final int WAITER = 2; // set when waiting for write lock |
2591 |
> |
static final int READER = 4; // increment value for setting read lock |
2592 |
> |
|
2593 |
> |
/** |
2594 |
> |
* Tie-breaking utility for ordering insertions when equal |
2595 |
> |
* hashCodes and non-comparable. We don't require a total |
2596 |
> |
* order, just a consistent insertion rule to maintain |
2597 |
> |
* equivalence across rebalancings. Tie-breaking further than |
2598 |
> |
* necessary simplifies testing a bit. |
2599 |
> |
*/ |
2600 |
> |
static int tieBreakOrder(Object a, Object b) { |
2601 |
> |
int d; |
2602 |
> |
if (a == null || b == null || |
2603 |
> |
(d = a.getClass().getName(). |
2604 |
> |
compareTo(b.getClass().getName())) == 0) |
2605 |
> |
d = (System.identityHashCode(a) <= System.identityHashCode(b) ? |
2606 |
> |
-1 : 1); |
2607 |
> |
return d; |
2608 |
> |
} |
2609 |
> |
|
2610 |
> |
/** |
2611 |
> |
* Creates bin with initial set of nodes headed by b. |
2612 |
> |
*/ |
2613 |
> |
TreeBin(TreeNode<K,V> b) { |
2614 |
> |
super(TREEBIN, null, null, null); |
2615 |
> |
this.first = b; |
2616 |
> |
TreeNode<K,V> r = null; |
2617 |
> |
for (TreeNode<K,V> x = b, next; x != null; x = next) { |
2618 |
> |
next = (TreeNode<K,V>)x.next; |
2619 |
> |
x.left = x.right = null; |
2620 |
> |
if (r == null) { |
2621 |
> |
x.parent = null; |
2622 |
> |
x.red = false; |
2623 |
> |
r = x; |
2624 |
> |
} |
2625 |
> |
else { |
2626 |
> |
K k = x.key; |
2627 |
> |
int h = x.hash; |
2628 |
> |
Class<?> kc = null; |
2629 |
> |
for (TreeNode<K,V> p = r;;) { |
2630 |
> |
int dir, ph; |
2631 |
> |
K pk = p.key; |
2632 |
> |
if ((ph = p.hash) > h) |
2633 |
> |
dir = -1; |
2634 |
> |
else if (ph < h) |
2635 |
> |
dir = 1; |
2636 |
> |
else if ((kc == null && |
2637 |
> |
(kc = comparableClassFor(k)) == null) || |
2638 |
> |
(dir = compareComparables(kc, k, pk)) == 0) |
2639 |
> |
dir = tieBreakOrder(k, pk); |
2640 |
> |
TreeNode<K,V> xp = p; |
2641 |
> |
if ((p = (dir <= 0) ? p.left : p.right) == null) { |
2642 |
> |
x.parent = xp; |
2643 |
> |
if (dir <= 0) |
2644 |
> |
xp.left = x; |
2645 |
> |
else |
2646 |
> |
xp.right = x; |
2647 |
> |
r = balanceInsertion(r, x); |
2648 |
> |
break; |
2649 |
> |
} |
2650 |
> |
} |
2651 |
> |
} |
2652 |
> |
} |
2653 |
> |
this.root = r; |
2654 |
> |
assert checkInvariants(root); |
2655 |
> |
} |
2656 |
> |
|
2657 |
> |
/** |
2658 |
> |
* Acquires write lock for tree restructuring. |
2659 |
> |
*/ |
2660 |
> |
private final void lockRoot() { |
2661 |
> |
if (!U.compareAndSwapInt(this, LOCKSTATE, 0, WRITER)) |
2662 |
> |
contendedLock(); // offload to separate method |
2663 |
> |
} |
2664 |
> |
|
2665 |
> |
/** |
2666 |
> |
* Releases write lock for tree restructuring. |
2667 |
> |
*/ |
2668 |
> |
private final void unlockRoot() { |
2669 |
> |
lockState = 0; |
2670 |
> |
} |
2671 |
> |
|
2672 |
> |
/** |
2673 |
> |
* Possibly blocks awaiting root lock. |
2674 |
> |
*/ |
2675 |
> |
private final void contendedLock() { |
2676 |
> |
boolean waiting = false; |
2677 |
> |
for (int s;;) { |
2678 |
> |
if (((s = lockState) & WRITER) == 0) { |
2679 |
> |
if (U.compareAndSwapInt(this, LOCKSTATE, s, WRITER)) { |
2680 |
> |
if (waiting) |
2681 |
> |
waiter = null; |
2682 |
> |
return; |
2683 |
> |
} |
2684 |
> |
} |
2685 |
> |
else if ((s | WAITER) == 0) { |
2686 |
> |
if (U.compareAndSwapInt(this, LOCKSTATE, s, s | WAITER)) { |
2687 |
> |
waiting = true; |
2688 |
> |
waiter = Thread.currentThread(); |
2689 |
> |
} |
2690 |
> |
} |
2691 |
> |
else if (waiting) |
2692 |
> |
LockSupport.park(this); |
2693 |
> |
} |
2694 |
> |
} |
2695 |
> |
|
2696 |
> |
/** |
2697 |
> |
* Returns matching node or null if none. Tries to search |
2698 |
> |
* using tree comparisons from root, but continues linear |
2699 |
> |
* search when lock not available. |
2700 |
> |
*/ |
2701 |
> |
final Node<K,V> find(int h, Object k) { |
2702 |
> |
if (k != null) { |
2703 |
> |
for (Node<K,V> e = first; e != null; e = e.next) { |
2704 |
> |
int s; K ek; |
2705 |
> |
if (((s = lockState) & (WAITER|WRITER)) != 0) { |
2706 |
> |
if (e.hash == h && |
2707 |
> |
((ek = e.key) == k || (ek != null && k.equals(ek)))) |
2708 |
> |
return e; |
2709 |
> |
} |
2710 |
> |
else if (U.compareAndSwapInt(this, LOCKSTATE, s, |
2711 |
> |
s + READER)) { |
2712 |
> |
TreeNode<K,V> r, p; |
2713 |
> |
try { |
2714 |
> |
p = ((r = root) == null ? null : |
2715 |
> |
r.findTreeNode(h, k, null)); |
2716 |
> |
} finally { |
2717 |
> |
Thread w; |
2718 |
> |
int ls; |
2719 |
> |
do {} while (!U.compareAndSwapInt |
2720 |
> |
(this, LOCKSTATE, |
2721 |
> |
ls = lockState, ls - READER)); |
2722 |
> |
if (ls == (READER|WAITER) && (w = waiter) != null) |
2723 |
> |
LockSupport.unpark(w); |
2724 |
> |
} |
2725 |
> |
return p; |
2726 |
> |
} |
2727 |
> |
} |
2728 |
> |
} |
2729 |
> |
return null; |
2730 |
> |
} |
2731 |
> |
|
2732 |
> |
/** |
2733 |
> |
* Finds or adds a node. |
2734 |
> |
* @return null if added |
2735 |
> |
*/ |
2736 |
> |
final TreeNode<K,V> putTreeVal(int h, K k, V v) { |
2737 |
> |
Class<?> kc = null; |
2738 |
> |
boolean searched = false; |
2739 |
> |
for (TreeNode<K,V> p = root;;) { |
2740 |
> |
int dir, ph; K pk; |
2741 |
> |
if (p == null) { |
2742 |
> |
first = root = new TreeNode<K,V>(h, k, v, null, null); |
2743 |
|
break; |
2744 |
< |
sb.append(',').append(' '); |
2744 |
> |
} |
2745 |
> |
else if ((ph = p.hash) > h) |
2746 |
> |
dir = -1; |
2747 |
> |
else if (ph < h) |
2748 |
> |
dir = 1; |
2749 |
> |
else if ((pk = p.key) == k || (pk != null && k.equals(pk))) |
2750 |
> |
return p; |
2751 |
> |
else if ((kc == null && |
2752 |
> |
(kc = comparableClassFor(k)) == null) || |
2753 |
> |
(dir = compareComparables(kc, k, pk)) == 0) { |
2754 |
> |
if (!searched) { |
2755 |
> |
TreeNode<K,V> q, ch; |
2756 |
> |
searched = true; |
2757 |
> |
if (((ch = p.left) != null && |
2758 |
> |
(q = ch.findTreeNode(h, k, kc)) != null) || |
2759 |
> |
((ch = p.right) != null && |
2760 |
> |
(q = ch.findTreeNode(h, k, kc)) != null)) |
2761 |
> |
return q; |
2762 |
> |
} |
2763 |
> |
dir = tieBreakOrder(k, pk); |
2764 |
> |
} |
2765 |
> |
|
2766 |
> |
TreeNode<K,V> xp = p; |
2767 |
> |
if ((p = (dir <= 0) ? p.left : p.right) == null) { |
2768 |
> |
TreeNode<K,V> x, f = first; |
2769 |
> |
first = x = new TreeNode<K,V>(h, k, v, f, xp); |
2770 |
> |
if (f != null) |
2771 |
> |
f.prev = x; |
2772 |
> |
if (dir <= 0) |
2773 |
> |
xp.left = x; |
2774 |
> |
else |
2775 |
> |
xp.right = x; |
2776 |
> |
if (!xp.red) |
2777 |
> |
x.red = true; |
2778 |
> |
else { |
2779 |
> |
lockRoot(); |
2780 |
> |
try { |
2781 |
> |
root = balanceInsertion(root, x); |
2782 |
> |
} finally { |
2783 |
> |
unlockRoot(); |
2784 |
> |
} |
2785 |
> |
} |
2786 |
> |
break; |
2787 |
> |
} |
2788 |
> |
} |
2789 |
> |
assert checkInvariants(root); |
2790 |
> |
return null; |
2791 |
> |
} |
2792 |
> |
|
2793 |
> |
/** |
2794 |
> |
* Removes the given node, that must be present before this |
2795 |
> |
* call. This is messier than typical red-black deletion code |
2796 |
> |
* because we cannot swap the contents of an interior node |
2797 |
> |
* with a leaf successor that is pinned by "next" pointers |
2798 |
> |
* that are accessible independently of lock. So instead we |
2799 |
> |
* swap the tree linkages. |
2800 |
> |
* |
2801 |
> |
* @return true if now too small, so should be untreeified |
2802 |
> |
*/ |
2803 |
> |
final boolean removeTreeNode(TreeNode<K,V> p) { |
2804 |
> |
TreeNode<K,V> next = (TreeNode<K,V>)p.next; |
2805 |
> |
TreeNode<K,V> pred = p.prev; // unlink traversal pointers |
2806 |
> |
TreeNode<K,V> r, rl; |
2807 |
> |
if (pred == null) |
2808 |
> |
first = next; |
2809 |
> |
else |
2810 |
> |
pred.next = next; |
2811 |
> |
if (next != null) |
2812 |
> |
next.prev = pred; |
2813 |
> |
if (first == null) { |
2814 |
> |
root = null; |
2815 |
> |
return true; |
2816 |
> |
} |
2817 |
> |
if ((r = root) == null || r.right == null || // too small |
2818 |
> |
(rl = r.left) == null || rl.left == null) |
2819 |
> |
return true; |
2820 |
> |
lockRoot(); |
2821 |
> |
try { |
2822 |
> |
TreeNode<K,V> replacement; |
2823 |
> |
TreeNode<K,V> pl = p.left; |
2824 |
> |
TreeNode<K,V> pr = p.right; |
2825 |
> |
if (pl != null && pr != null) { |
2826 |
> |
TreeNode<K,V> s = pr, sl; |
2827 |
> |
while ((sl = s.left) != null) // find successor |
2828 |
> |
s = sl; |
2829 |
> |
boolean c = s.red; s.red = p.red; p.red = c; // swap colors |
2830 |
> |
TreeNode<K,V> sr = s.right; |
2831 |
> |
TreeNode<K,V> pp = p.parent; |
2832 |
> |
if (s == pr) { // p was s's direct parent |
2833 |
> |
p.parent = s; |
2834 |
> |
s.right = p; |
2835 |
> |
} |
2836 |
> |
else { |
2837 |
> |
TreeNode<K,V> sp = s.parent; |
2838 |
> |
if ((p.parent = sp) != null) { |
2839 |
> |
if (s == sp.left) |
2840 |
> |
sp.left = p; |
2841 |
> |
else |
2842 |
> |
sp.right = p; |
2843 |
> |
} |
2844 |
> |
if ((s.right = pr) != null) |
2845 |
> |
pr.parent = s; |
2846 |
> |
} |
2847 |
> |
p.left = null; |
2848 |
> |
if ((p.right = sr) != null) |
2849 |
> |
sr.parent = p; |
2850 |
> |
if ((s.left = pl) != null) |
2851 |
> |
pl.parent = s; |
2852 |
> |
if ((s.parent = pp) == null) |
2853 |
> |
r = s; |
2854 |
> |
else if (p == pp.left) |
2855 |
> |
pp.left = s; |
2856 |
> |
else |
2857 |
> |
pp.right = s; |
2858 |
> |
if (sr != null) |
2859 |
> |
replacement = sr; |
2860 |
> |
else |
2861 |
> |
replacement = p; |
2862 |
> |
} |
2863 |
> |
else if (pl != null) |
2864 |
> |
replacement = pl; |
2865 |
> |
else if (pr != null) |
2866 |
> |
replacement = pr; |
2867 |
> |
else |
2868 |
> |
replacement = p; |
2869 |
> |
if (replacement != p) { |
2870 |
> |
TreeNode<K,V> pp = replacement.parent = p.parent; |
2871 |
> |
if (pp == null) |
2872 |
> |
r = replacement; |
2873 |
> |
else if (p == pp.left) |
2874 |
> |
pp.left = replacement; |
2875 |
> |
else |
2876 |
> |
pp.right = replacement; |
2877 |
> |
p.left = p.right = p.parent = null; |
2878 |
> |
} |
2879 |
> |
|
2880 |
> |
root = (p.red) ? r : balanceDeletion(r, replacement); |
2881 |
> |
|
2882 |
> |
if (p == replacement) { // detach pointers |
2883 |
> |
TreeNode<K,V> pp; |
2884 |
> |
if ((pp = p.parent) != null) { |
2885 |
> |
if (p == pp.left) |
2886 |
> |
pp.left = null; |
2887 |
> |
else if (p == pp.right) |
2888 |
> |
pp.right = null; |
2889 |
> |
p.parent = null; |
2890 |
> |
} |
2891 |
> |
} |
2892 |
> |
} finally { |
2893 |
> |
unlockRoot(); |
2894 |
> |
} |
2895 |
> |
assert checkInvariants(root); |
2896 |
> |
return false; |
2897 |
> |
} |
2898 |
> |
|
2899 |
> |
/* ------------------------------------------------------------ */ |
2900 |
> |
// Red-black tree methods, all adapted from CLR |
2901 |
> |
|
2902 |
> |
static <K,V> TreeNode<K,V> rotateLeft(TreeNode<K,V> root, |
2903 |
> |
TreeNode<K,V> p) { |
2904 |
> |
TreeNode<K,V> r, pp, rl; |
2905 |
> |
if (p != null && (r = p.right) != null) { |
2906 |
> |
if ((rl = p.right = r.left) != null) |
2907 |
> |
rl.parent = p; |
2908 |
> |
if ((pp = r.parent = p.parent) == null) |
2909 |
> |
(root = r).red = false; |
2910 |
> |
else if (pp.left == p) |
2911 |
> |
pp.left = r; |
2912 |
> |
else |
2913 |
> |
pp.right = r; |
2914 |
> |
r.left = p; |
2915 |
> |
p.parent = r; |
2916 |
> |
} |
2917 |
> |
return root; |
2918 |
> |
} |
2919 |
> |
|
2920 |
> |
static <K,V> TreeNode<K,V> rotateRight(TreeNode<K,V> root, |
2921 |
> |
TreeNode<K,V> p) { |
2922 |
> |
TreeNode<K,V> l, pp, lr; |
2923 |
> |
if (p != null && (l = p.left) != null) { |
2924 |
> |
if ((lr = p.left = l.right) != null) |
2925 |
> |
lr.parent = p; |
2926 |
> |
if ((pp = l.parent = p.parent) == null) |
2927 |
> |
(root = l).red = false; |
2928 |
> |
else if (pp.right == p) |
2929 |
> |
pp.right = l; |
2930 |
> |
else |
2931 |
> |
pp.left = l; |
2932 |
> |
l.right = p; |
2933 |
> |
p.parent = l; |
2934 |
> |
} |
2935 |
> |
return root; |
2936 |
> |
} |
2937 |
> |
|
2938 |
> |
static <K,V> TreeNode<K,V> balanceInsertion(TreeNode<K,V> root, |
2939 |
> |
TreeNode<K,V> x) { |
2940 |
> |
x.red = true; |
2941 |
> |
for (TreeNode<K,V> xp, xpp, xppl, xppr;;) { |
2942 |
> |
if ((xp = x.parent) == null) { |
2943 |
> |
x.red = false; |
2944 |
> |
return x; |
2945 |
> |
} |
2946 |
> |
else if (!xp.red || (xpp = xp.parent) == null) |
2947 |
> |
return root; |
2948 |
> |
if (xp == (xppl = xpp.left)) { |
2949 |
> |
if ((xppr = xpp.right) != null && xppr.red) { |
2950 |
> |
xppr.red = false; |
2951 |
> |
xp.red = false; |
2952 |
> |
xpp.red = true; |
2953 |
> |
x = xpp; |
2954 |
> |
} |
2955 |
> |
else { |
2956 |
> |
if (x == xp.right) { |
2957 |
> |
root = rotateLeft(root, x = xp); |
2958 |
> |
xpp = (xp = x.parent) == null ? null : xp.parent; |
2959 |
> |
} |
2960 |
> |
if (xp != null) { |
2961 |
> |
xp.red = false; |
2962 |
> |
if (xpp != null) { |
2963 |
> |
xpp.red = true; |
2964 |
> |
root = rotateRight(root, xpp); |
2965 |
> |
} |
2966 |
> |
} |
2967 |
> |
} |
2968 |
> |
} |
2969 |
> |
else { |
2970 |
> |
if (xppl != null && xppl.red) { |
2971 |
> |
xppl.red = false; |
2972 |
> |
xp.red = false; |
2973 |
> |
xpp.red = true; |
2974 |
> |
x = xpp; |
2975 |
> |
} |
2976 |
> |
else { |
2977 |
> |
if (x == xp.left) { |
2978 |
> |
root = rotateRight(root, x = xp); |
2979 |
> |
xpp = (xp = x.parent) == null ? null : xp.parent; |
2980 |
> |
} |
2981 |
> |
if (xp != null) { |
2982 |
> |
xp.red = false; |
2983 |
> |
if (xpp != null) { |
2984 |
> |
xpp.red = true; |
2985 |
> |
root = rotateLeft(root, xpp); |
2986 |
> |
} |
2987 |
> |
} |
2988 |
> |
} |
2989 |
> |
} |
2990 |
> |
} |
2991 |
> |
} |
2992 |
> |
|
2993 |
> |
static <K,V> TreeNode<K,V> balanceDeletion(TreeNode<K,V> root, |
2994 |
> |
TreeNode<K,V> x) { |
2995 |
> |
for (TreeNode<K,V> xp, xpl, xpr;;) { |
2996 |
> |
if (x == null || x == root) |
2997 |
> |
return root; |
2998 |
> |
else if ((xp = x.parent) == null) { |
2999 |
> |
x.red = false; |
3000 |
> |
return x; |
3001 |
> |
} |
3002 |
> |
else if (x.red) { |
3003 |
> |
x.red = false; |
3004 |
> |
return root; |
3005 |
> |
} |
3006 |
> |
else if ((xpl = xp.left) == x) { |
3007 |
> |
if ((xpr = xp.right) != null && xpr.red) { |
3008 |
> |
xpr.red = false; |
3009 |
> |
xp.red = true; |
3010 |
> |
root = rotateLeft(root, xp); |
3011 |
> |
xpr = (xp = x.parent) == null ? null : xp.right; |
3012 |
> |
} |
3013 |
> |
if (xpr == null) |
3014 |
> |
x = xp; |
3015 |
> |
else { |
3016 |
> |
TreeNode<K,V> sl = xpr.left, sr = xpr.right; |
3017 |
> |
if ((sr == null || !sr.red) && |
3018 |
> |
(sl == null || !sl.red)) { |
3019 |
> |
xpr.red = true; |
3020 |
> |
x = xp; |
3021 |
> |
} |
3022 |
> |
else { |
3023 |
> |
if (sr == null || !sr.red) { |
3024 |
> |
if (sl != null) |
3025 |
> |
sl.red = false; |
3026 |
> |
xpr.red = true; |
3027 |
> |
root = rotateRight(root, xpr); |
3028 |
> |
xpr = (xp = x.parent) == null ? |
3029 |
> |
null : xp.right; |
3030 |
> |
} |
3031 |
> |
if (xpr != null) { |
3032 |
> |
xpr.red = (xp == null) ? false : xp.red; |
3033 |
> |
if ((sr = xpr.right) != null) |
3034 |
> |
sr.red = false; |
3035 |
> |
} |
3036 |
> |
if (xp != null) { |
3037 |
> |
xp.red = false; |
3038 |
> |
root = rotateLeft(root, xp); |
3039 |
> |
} |
3040 |
> |
x = root; |
3041 |
> |
} |
3042 |
> |
} |
3043 |
> |
} |
3044 |
> |
else { // symmetric |
3045 |
> |
if (xpl != null && xpl.red) { |
3046 |
> |
xpl.red = false; |
3047 |
> |
xp.red = true; |
3048 |
> |
root = rotateRight(root, xp); |
3049 |
> |
xpl = (xp = x.parent) == null ? null : xp.left; |
3050 |
> |
} |
3051 |
> |
if (xpl == null) |
3052 |
> |
x = xp; |
3053 |
> |
else { |
3054 |
> |
TreeNode<K,V> sl = xpl.left, sr = xpl.right; |
3055 |
> |
if ((sl == null || !sl.red) && |
3056 |
> |
(sr == null || !sr.red)) { |
3057 |
> |
xpl.red = true; |
3058 |
> |
x = xp; |
3059 |
> |
} |
3060 |
> |
else { |
3061 |
> |
if (sl == null || !sl.red) { |
3062 |
> |
if (sr != null) |
3063 |
> |
sr.red = false; |
3064 |
> |
xpl.red = true; |
3065 |
> |
root = rotateLeft(root, xpl); |
3066 |
> |
xpl = (xp = x.parent) == null ? |
3067 |
> |
null : xp.left; |
3068 |
> |
} |
3069 |
> |
if (xpl != null) { |
3070 |
> |
xpl.red = (xp == null) ? false : xp.red; |
3071 |
> |
if ((sl = xpl.left) != null) |
3072 |
> |
sl.red = false; |
3073 |
> |
} |
3074 |
> |
if (xp != null) { |
3075 |
> |
xp.red = false; |
3076 |
> |
root = rotateRight(root, xp); |
3077 |
> |
} |
3078 |
> |
x = root; |
3079 |
> |
} |
3080 |
> |
} |
3081 |
> |
} |
3082 |
> |
} |
3083 |
> |
} |
3084 |
> |
|
3085 |
> |
/** |
3086 |
> |
* Recursive invariant check |
3087 |
> |
*/ |
3088 |
> |
static <K,V> boolean checkInvariants(TreeNode<K,V> t) { |
3089 |
> |
TreeNode<K,V> tp = t.parent, tl = t.left, tr = t.right, |
3090 |
> |
tb = t.prev, tn = (TreeNode<K,V>)t.next; |
3091 |
> |
if (tb != null && tb.next != t) |
3092 |
> |
return false; |
3093 |
> |
if (tn != null && tn.prev != t) |
3094 |
> |
return false; |
3095 |
> |
if (tp != null && t != tp.left && t != tp.right) |
3096 |
> |
return false; |
3097 |
> |
if (tl != null && (tl.parent != t || tl.hash > t.hash)) |
3098 |
> |
return false; |
3099 |
> |
if (tr != null && (tr.parent != t || tr.hash < t.hash)) |
3100 |
> |
return false; |
3101 |
> |
if (t.red && tl != null && tl.red && tr != null && tr.red) |
3102 |
> |
return false; |
3103 |
> |
if (tl != null && !checkInvariants(tl)) |
3104 |
> |
return false; |
3105 |
> |
if (tr != null && !checkInvariants(tr)) |
3106 |
> |
return false; |
3107 |
> |
return true; |
3108 |
> |
} |
3109 |
> |
|
3110 |
> |
private static final sun.misc.Unsafe U; |
3111 |
> |
private static final long LOCKSTATE; |
3112 |
> |
static { |
3113 |
> |
try { |
3114 |
> |
U = getUnsafe(); |
3115 |
> |
Class<?> k = TreeBin.class; |
3116 |
> |
LOCKSTATE = U.objectFieldOffset |
3117 |
> |
(k.getDeclaredField("lockState")); |
3118 |
> |
} catch (Exception e) { |
3119 |
> |
throw new Error(e); |
3120 |
|
} |
3121 |
|
} |
3191 |
– |
return sb.append('}').toString(); |
3122 |
|
} |
3123 |
|
|
3124 |
+ |
/* ----------------Table Traversal -------------- */ |
3125 |
+ |
|
3126 |
|
/** |
3127 |
< |
* Compares the specified object with this map for equality. |
3128 |
< |
* Returns {@code true} if the given object is a map with the same |
3197 |
< |
* mappings as this map. This operation may return misleading |
3198 |
< |
* results if either map is concurrently modified during execution |
3199 |
< |
* of this method. |
3127 |
> |
* Encapsulates traversal for methods such as containsValue; also |
3128 |
> |
* serves as a base class for other iterators and spliterators. |
3129 |
|
* |
3130 |
< |
* @param o object to be compared for equality with this map |
3131 |
< |
* @return {@code true} if the specified object is equal to this map |
3130 |
> |
* Method advance visits once each still-valid node that was |
3131 |
> |
* reachable upon iterator construction. It might miss some that |
3132 |
> |
* were added to a bin after the bin was visited, which is OK wrt |
3133 |
> |
* consistency guarantees. Maintaining this property in the face |
3134 |
> |
* of possible ongoing resizes requires a fair amount of |
3135 |
> |
* bookkeeping state that is difficult to optimize away amidst |
3136 |
> |
* volatile accesses. Even so, traversal maintains reasonable |
3137 |
> |
* throughput. |
3138 |
> |
* |
3139 |
> |
* Normally, iteration proceeds bin-by-bin traversing lists. |
3140 |
> |
* However, if the table has been resized, then all future steps |
3141 |
> |
* must traverse both the bin at the current index as well as at |
3142 |
> |
* (index + baseSize); and so on for further resizings. To |
3143 |
> |
* paranoically cope with potential sharing by users of iterators |
3144 |
> |
* across threads, iteration terminates if a bounds checks fails |
3145 |
> |
* for a table read. |
3146 |
|
*/ |
3147 |
< |
public boolean equals(Object o) { |
3148 |
< |
if (o != this) { |
3149 |
< |
if (!(o instanceof Map)) |
3150 |
< |
return false; |
3151 |
< |
Map<?,?> m = (Map<?,?>) o; |
3152 |
< |
Traverser<K,V,Object> it = new Traverser<K,V,Object>(this); |
3153 |
< |
Object val; |
3154 |
< |
while ((val = it.advance()) != null) { |
3155 |
< |
Object v = m.get(it.nextKey); |
3156 |
< |
if (v == null || (v != val && !v.equals(val))) |
3157 |
< |
return false; |
3158 |
< |
} |
3159 |
< |
for (Map.Entry<?,?> e : m.entrySet()) { |
3160 |
< |
Object mk, mv, v; |
3161 |
< |
if ((mk = e.getKey()) == null || |
3162 |
< |
(mv = e.getValue()) == null || |
3163 |
< |
(v = internalGet(mk)) == null || |
3164 |
< |
(mv != v && !mv.equals(v))) |
3165 |
< |
return false; |
3147 |
> |
static class Traverser<K,V> { |
3148 |
> |
Node<K,V>[] tab; // current table; updated if resized |
3149 |
> |
Node<K,V> next; // the next entry to use |
3150 |
> |
int index; // index of bin to use next |
3151 |
> |
int baseIndex; // current index of initial table |
3152 |
> |
int baseLimit; // index bound for initial table |
3153 |
> |
final int baseSize; // initial table size |
3154 |
> |
|
3155 |
> |
Traverser(Node<K,V>[] tab, int size, int index, int limit) { |
3156 |
> |
this.tab = tab; |
3157 |
> |
this.baseSize = size; |
3158 |
> |
this.baseIndex = this.index = index; |
3159 |
> |
this.baseLimit = limit; |
3160 |
> |
this.next = null; |
3161 |
> |
} |
3162 |
> |
|
3163 |
> |
/** |
3164 |
> |
* Advances if possible, returning next valid node, or null if none. |
3165 |
> |
*/ |
3166 |
> |
final Node<K,V> advance() { |
3167 |
> |
Node<K,V> e; |
3168 |
> |
if ((e = next) != null) |
3169 |
> |
e = e.next; |
3170 |
> |
for (;;) { |
3171 |
> |
Node<K,V>[] t; int i, n; K ek; // must use locals in checks |
3172 |
> |
if (e != null) |
3173 |
> |
return next = e; |
3174 |
> |
if (baseIndex >= baseLimit || (t = tab) == null || |
3175 |
> |
(n = t.length) <= (i = index) || i < 0) |
3176 |
> |
return next = null; |
3177 |
> |
if ((e = tabAt(t, index)) != null && e.hash < 0) { |
3178 |
> |
if (e instanceof ForwardingNode) { |
3179 |
> |
tab = ((ForwardingNode<K,V>)e).nextTable; |
3180 |
> |
e = null; |
3181 |
> |
continue; |
3182 |
> |
} |
3183 |
> |
else if (e instanceof TreeBin) |
3184 |
> |
e = ((TreeBin<K,V>)e).first; |
3185 |
> |
else |
3186 |
> |
e = null; |
3187 |
> |
} |
3188 |
> |
if ((index += baseSize) >= n) |
3189 |
> |
index = ++baseIndex; // visit upper slots if present |
3190 |
|
} |
3191 |
|
} |
3225 |
– |
return true; |
3192 |
|
} |
3193 |
|
|
3194 |
< |
/* ----------------Iterators -------------- */ |
3195 |
< |
|
3196 |
< |
@SuppressWarnings("serial") static final class KeyIterator<K,V> extends Traverser<K,V,Object> |
3197 |
< |
implements Spliterator<K>, Enumeration<K> { |
3198 |
< |
KeyIterator(ConcurrentHashMapV8<K, V> map) { super(map); } |
3199 |
< |
KeyIterator(ConcurrentHashMapV8<K, V> map, Traverser<K,V,Object> it) { |
3200 |
< |
super(map, it, -1); |
3194 |
> |
/** |
3195 |
> |
* Base of key, value, and entry Iterators. Adds fields to |
3196 |
> |
* Traverser to support iterator.remove. |
3197 |
> |
*/ |
3198 |
> |
static class BaseIterator<K,V> extends Traverser<K,V> { |
3199 |
> |
final ConcurrentHashMapV8<K,V> map; |
3200 |
> |
Node<K,V> lastReturned; |
3201 |
> |
BaseIterator(Node<K,V>[] tab, int size, int index, int limit, |
3202 |
> |
ConcurrentHashMapV8<K,V> map) { |
3203 |
> |
super(tab, size, index, limit); |
3204 |
> |
this.map = map; |
3205 |
> |
advance(); |
3206 |
|
} |
3207 |
< |
public KeyIterator<K,V> split() { |
3208 |
< |
if (nextKey != null) |
3207 |
> |
|
3208 |
> |
public final boolean hasNext() { return next != null; } |
3209 |
> |
public final boolean hasMoreElements() { return next != null; } |
3210 |
> |
|
3211 |
> |
public final void remove() { |
3212 |
> |
Node<K,V> p; |
3213 |
> |
if ((p = lastReturned) == null) |
3214 |
|
throw new IllegalStateException(); |
3215 |
< |
return new KeyIterator<K,V>(map, this); |
3215 |
> |
lastReturned = null; |
3216 |
> |
map.replaceNode(p.key, null, null); |
3217 |
> |
} |
3218 |
> |
} |
3219 |
> |
|
3220 |
> |
static final class KeyIterator<K,V> extends BaseIterator<K,V> |
3221 |
> |
implements Iterator<K>, Enumeration<K> { |
3222 |
> |
KeyIterator(Node<K,V>[] tab, int index, int size, int limit, |
3223 |
> |
ConcurrentHashMapV8<K,V> map) { |
3224 |
> |
super(tab, index, size, limit, map); |
3225 |
|
} |
3226 |
< |
@SuppressWarnings("unchecked") public final K next() { |
3227 |
< |
if (nextVal == null && advance() == null) |
3226 |
> |
|
3227 |
> |
public final K next() { |
3228 |
> |
Node<K,V> p; |
3229 |
> |
if ((p = next) == null) |
3230 |
|
throw new NoSuchElementException(); |
3231 |
< |
Object k = nextKey; |
3232 |
< |
nextVal = null; |
3233 |
< |
return (K) k; |
3231 |
> |
K k = p.key; |
3232 |
> |
lastReturned = p; |
3233 |
> |
advance(); |
3234 |
> |
return k; |
3235 |
|
} |
3236 |
|
|
3237 |
|
public final K nextElement() { return next(); } |
3238 |
|
} |
3239 |
|
|
3240 |
< |
@SuppressWarnings("serial") static final class ValueIterator<K,V> extends Traverser<K,V,Object> |
3241 |
< |
implements Spliterator<V>, Enumeration<V> { |
3242 |
< |
ValueIterator(ConcurrentHashMapV8<K, V> map) { super(map); } |
3243 |
< |
ValueIterator(ConcurrentHashMapV8<K, V> map, Traverser<K,V,Object> it) { |
3244 |
< |
super(map, it, -1); |
3257 |
< |
} |
3258 |
< |
public ValueIterator<K,V> split() { |
3259 |
< |
if (nextKey != null) |
3260 |
< |
throw new IllegalStateException(); |
3261 |
< |
return new ValueIterator<K,V>(map, this); |
3240 |
> |
static final class ValueIterator<K,V> extends BaseIterator<K,V> |
3241 |
> |
implements Iterator<V>, Enumeration<V> { |
3242 |
> |
ValueIterator(Node<K,V>[] tab, int index, int size, int limit, |
3243 |
> |
ConcurrentHashMapV8<K,V> map) { |
3244 |
> |
super(tab, index, size, limit, map); |
3245 |
|
} |
3246 |
|
|
3247 |
< |
@SuppressWarnings("unchecked") public final V next() { |
3248 |
< |
Object v; |
3249 |
< |
if ((v = nextVal) == null && (v = advance()) == null) |
3247 |
> |
public final V next() { |
3248 |
> |
Node<K,V> p; |
3249 |
> |
if ((p = next) == null) |
3250 |
|
throw new NoSuchElementException(); |
3251 |
< |
nextVal = null; |
3252 |
< |
return (V) v; |
3251 |
> |
V v = p.val; |
3252 |
> |
lastReturned = p; |
3253 |
> |
advance(); |
3254 |
> |
return v; |
3255 |
|
} |
3256 |
|
|
3257 |
|
public final V nextElement() { return next(); } |
3258 |
|
} |
3259 |
|
|
3260 |
< |
@SuppressWarnings("serial") static final class EntryIterator<K,V> extends Traverser<K,V,Object> |
3261 |
< |
implements Spliterator<Map.Entry<K,V>> { |
3262 |
< |
EntryIterator(ConcurrentHashMapV8<K, V> map) { super(map); } |
3263 |
< |
EntryIterator(ConcurrentHashMapV8<K, V> map, Traverser<K,V,Object> it) { |
3264 |
< |
super(map, it, -1); |
3280 |
< |
} |
3281 |
< |
public EntryIterator<K,V> split() { |
3282 |
< |
if (nextKey != null) |
3283 |
< |
throw new IllegalStateException(); |
3284 |
< |
return new EntryIterator<K,V>(map, this); |
3260 |
> |
static final class EntryIterator<K,V> extends BaseIterator<K,V> |
3261 |
> |
implements Iterator<Map.Entry<K,V>> { |
3262 |
> |
EntryIterator(Node<K,V>[] tab, int index, int size, int limit, |
3263 |
> |
ConcurrentHashMapV8<K,V> map) { |
3264 |
> |
super(tab, index, size, limit, map); |
3265 |
|
} |
3266 |
|
|
3267 |
< |
@SuppressWarnings("unchecked") public final Map.Entry<K,V> next() { |
3268 |
< |
Object v; |
3269 |
< |
if ((v = nextVal) == null && (v = advance()) == null) |
3267 |
> |
public final Map.Entry<K,V> next() { |
3268 |
> |
Node<K,V> p; |
3269 |
> |
if ((p = next) == null) |
3270 |
|
throw new NoSuchElementException(); |
3271 |
< |
Object k = nextKey; |
3272 |
< |
nextVal = null; |
3273 |
< |
return new MapEntry<K,V>((K)k, (V)v, map); |
3271 |
> |
K k = p.key; |
3272 |
> |
V v = p.val; |
3273 |
> |
lastReturned = p; |
3274 |
> |
advance(); |
3275 |
> |
return new MapEntry<K,V>(k, v, map); |
3276 |
|
} |
3277 |
|
} |
3278 |
|
|
3279 |
|
/** |
3280 |
< |
* Exported Entry for iterators |
3280 |
> |
* Exported Entry for EntryIterator |
3281 |
|
*/ |
3282 |
< |
static final class MapEntry<K,V> implements Map.Entry<K, V> { |
3282 |
> |
static final class MapEntry<K,V> implements Map.Entry<K,V> { |
3283 |
|
final K key; // non-null |
3284 |
|
V val; // non-null |
3285 |
< |
final ConcurrentHashMapV8<K, V> map; |
3286 |
< |
MapEntry(K key, V val, ConcurrentHashMapV8<K, V> map) { |
3285 |
> |
final ConcurrentHashMapV8<K,V> map; |
3286 |
> |
MapEntry(K key, V val, ConcurrentHashMapV8<K,V> map) { |
3287 |
|
this.key = key; |
3288 |
|
this.val = val; |
3289 |
|
this.map = map; |
3290 |
|
} |
3291 |
< |
public final K getKey() { return key; } |
3292 |
< |
public final V getValue() { return val; } |
3293 |
< |
public final int hashCode() { return key.hashCode() ^ val.hashCode(); } |
3294 |
< |
public final String toString(){ return key + "=" + val; } |
3291 |
> |
public K getKey() { return key; } |
3292 |
> |
public V getValue() { return val; } |
3293 |
> |
public int hashCode() { return key.hashCode() ^ val.hashCode(); } |
3294 |
> |
public String toString() { return key + "=" + val; } |
3295 |
|
|
3296 |
< |
public final boolean equals(Object o) { |
3296 |
> |
public boolean equals(Object o) { |
3297 |
|
Object k, v; Map.Entry<?,?> e; |
3298 |
|
return ((o instanceof Map.Entry) && |
3299 |
|
(k = (e = (Map.Entry<?,?>)o).getKey()) != null && |
3307 |
|
* value to return is somewhat arbitrary here. Since we do not |
3308 |
|
* necessarily track asynchronous changes, the most recent |
3309 |
|
* "previous" value could be different from what we return (or |
3310 |
< |
* could even have been removed in which case the put will |
3310 |
> |
* could even have been removed, in which case the put will |
3311 |
|
* re-establish). We do not and cannot guarantee more. |
3312 |
|
*/ |
3313 |
< |
public final V setValue(V value) { |
3313 |
> |
public V setValue(V value) { |
3314 |
|
if (value == null) throw new NullPointerException(); |
3315 |
|
V v = val; |
3316 |
|
val = value; |
3319 |
|
} |
3320 |
|
} |
3321 |
|
|
3322 |
< |
/** |
3323 |
< |
* Returns exportable snapshot entry for the given key and value |
3324 |
< |
* when write-through can't or shouldn't be used. |
3325 |
< |
*/ |
3326 |
< |
static <K,V> AbstractMap.SimpleEntry<K,V> entryFor(K k, V v) { |
3327 |
< |
return new AbstractMap.SimpleEntry<K,V>(k, v); |
3328 |
< |
} |
3322 |
> |
static final class KeySpliterator<K,V> extends Traverser<K,V> |
3323 |
> |
implements ConcurrentHashMapSpliterator<K> { |
3324 |
> |
long est; // size estimate |
3325 |
> |
KeySpliterator(Node<K,V>[] tab, int size, int index, int limit, |
3326 |
> |
long est) { |
3327 |
> |
super(tab, size, index, limit); |
3328 |
> |
this.est = est; |
3329 |
> |
} |
3330 |
> |
|
3331 |
> |
public ConcurrentHashMapSpliterator<K> trySplit() { |
3332 |
> |
int i, f, h; |
3333 |
> |
return (h = ((i = baseIndex) + (f = baseLimit)) >>> 1) <= i ? null : |
3334 |
> |
new KeySpliterator<K,V>(tab, baseSize, baseLimit = h, |
3335 |
> |
f, est >>>= 1); |
3336 |
> |
} |
3337 |
|
|
3338 |
< |
/* ---------------- Serialization Support -------------- */ |
3338 |
> |
public void forEachRemaining(Action<? super K> action) { |
3339 |
> |
if (action == null) throw new NullPointerException(); |
3340 |
> |
for (Node<K,V> p; (p = advance()) != null;) |
3341 |
> |
action.apply(p.key); |
3342 |
> |
} |
3343 |
> |
|
3344 |
> |
public boolean tryAdvance(Action<? super K> action) { |
3345 |
> |
if (action == null) throw new NullPointerException(); |
3346 |
> |
Node<K,V> p; |
3347 |
> |
if ((p = advance()) == null) |
3348 |
> |
return false; |
3349 |
> |
action.apply(p.key); |
3350 |
> |
return true; |
3351 |
> |
} |
3352 |
> |
|
3353 |
> |
public long estimateSize() { return est; } |
3354 |
|
|
3350 |
– |
/** |
3351 |
– |
* Stripped-down version of helper class used in previous version, |
3352 |
– |
* declared for the sake of serialization compatibility |
3353 |
– |
*/ |
3354 |
– |
static class Segment<K,V> implements Serializable { |
3355 |
– |
private static final long serialVersionUID = 2249069246763182397L; |
3356 |
– |
final float loadFactor; |
3357 |
– |
Segment(float lf) { this.loadFactor = lf; } |
3355 |
|
} |
3356 |
|
|
3357 |
< |
/** |
3358 |
< |
* Saves the state of the {@code ConcurrentHashMapV8} instance to a |
3359 |
< |
* stream (i.e., serializes it). |
3360 |
< |
* @param s the stream |
3361 |
< |
* @serialData |
3362 |
< |
* the key (Object) and value (Object) |
3363 |
< |
* for each key-value mapping, followed by a null pair. |
3367 |
< |
* The key-value mappings are emitted in no particular order. |
3368 |
< |
*/ |
3369 |
< |
@SuppressWarnings("unchecked") private void writeObject(java.io.ObjectOutputStream s) |
3370 |
< |
throws java.io.IOException { |
3371 |
< |
if (segments == null) { // for serialization compatibility |
3372 |
< |
segments = (Segment<K,V>[]) |
3373 |
< |
new Segment<?,?>[DEFAULT_CONCURRENCY_LEVEL]; |
3374 |
< |
for (int i = 0; i < segments.length; ++i) |
3375 |
< |
segments[i] = new Segment<K,V>(LOAD_FACTOR); |
3376 |
< |
} |
3377 |
< |
s.defaultWriteObject(); |
3378 |
< |
Traverser<K,V,Object> it = new Traverser<K,V,Object>(this); |
3379 |
< |
Object v; |
3380 |
< |
while ((v = it.advance()) != null) { |
3381 |
< |
s.writeObject(it.nextKey); |
3382 |
< |
s.writeObject(v); |
3357 |
> |
static final class ValueSpliterator<K,V> extends Traverser<K,V> |
3358 |
> |
implements ConcurrentHashMapSpliterator<V> { |
3359 |
> |
long est; // size estimate |
3360 |
> |
ValueSpliterator(Node<K,V>[] tab, int size, int index, int limit, |
3361 |
> |
long est) { |
3362 |
> |
super(tab, size, index, limit); |
3363 |
> |
this.est = est; |
3364 |
|
} |
3384 |
– |
s.writeObject(null); |
3385 |
– |
s.writeObject(null); |
3386 |
– |
segments = null; // throw away |
3387 |
– |
} |
3365 |
|
|
3366 |
< |
/** |
3367 |
< |
* Reconstitutes the instance from a stream (that is, deserializes it). |
3368 |
< |
* @param s the stream |
3369 |
< |
*/ |
3370 |
< |
@SuppressWarnings("unchecked") private void readObject(java.io.ObjectInputStream s) |
3371 |
< |
throws java.io.IOException, ClassNotFoundException { |
3395 |
< |
s.defaultReadObject(); |
3396 |
< |
this.segments = null; // unneeded |
3397 |
< |
// initialize transient final field |
3398 |
< |
UNSAFE.putObjectVolatile(this, counterOffset, new LongAdder()); |
3366 |
> |
public ConcurrentHashMapSpliterator<V> trySplit() { |
3367 |
> |
int i, f, h; |
3368 |
> |
return (h = ((i = baseIndex) + (f = baseLimit)) >>> 1) <= i ? null : |
3369 |
> |
new ValueSpliterator<K,V>(tab, baseSize, baseLimit = h, |
3370 |
> |
f, est >>>= 1); |
3371 |
> |
} |
3372 |
|
|
3373 |
< |
// Create all nodes, then place in table once size is known |
3374 |
< |
long size = 0L; |
3375 |
< |
Node p = null; |
3376 |
< |
for (;;) { |
3404 |
< |
K k = (K) s.readObject(); |
3405 |
< |
V v = (V) s.readObject(); |
3406 |
< |
if (k != null && v != null) { |
3407 |
< |
int h = spread(k.hashCode()); |
3408 |
< |
p = new Node(h, k, v, p); |
3409 |
< |
++size; |
3410 |
< |
} |
3411 |
< |
else |
3412 |
< |
break; |
3373 |
> |
public void forEachRemaining(Action<? super V> action) { |
3374 |
> |
if (action == null) throw new NullPointerException(); |
3375 |
> |
for (Node<K,V> p; (p = advance()) != null;) |
3376 |
> |
action.apply(p.val); |
3377 |
|
} |
3378 |
< |
if (p != null) { |
3379 |
< |
boolean init = false; |
3380 |
< |
int n; |
3381 |
< |
if (size >= (long)(MAXIMUM_CAPACITY >>> 1)) |
3382 |
< |
n = MAXIMUM_CAPACITY; |
3383 |
< |
else { |
3384 |
< |
int sz = (int)size; |
3385 |
< |
n = tableSizeFor(sz + (sz >>> 1) + 1); |
3422 |
< |
} |
3423 |
< |
int sc = sizeCtl; |
3424 |
< |
boolean collide = false; |
3425 |
< |
if (n > sc && |
3426 |
< |
UNSAFE.compareAndSwapInt(this, sizeCtlOffset, sc, -1)) { |
3427 |
< |
try { |
3428 |
< |
if (table == null) { |
3429 |
< |
init = true; |
3430 |
< |
Node[] tab = new Node[n]; |
3431 |
< |
int mask = n - 1; |
3432 |
< |
while (p != null) { |
3433 |
< |
int j = p.hash & mask; |
3434 |
< |
Node next = p.next; |
3435 |
< |
Node q = p.next = tabAt(tab, j); |
3436 |
< |
setTabAt(tab, j, p); |
3437 |
< |
if (!collide && q != null && q.hash == p.hash) |
3438 |
< |
collide = true; |
3439 |
< |
p = next; |
3440 |
< |
} |
3441 |
< |
table = tab; |
3442 |
< |
counter.add(size); |
3443 |
< |
sc = n - (n >>> 2); |
3444 |
< |
} |
3445 |
< |
} finally { |
3446 |
< |
sizeCtl = sc; |
3447 |
< |
} |
3448 |
< |
if (collide) { // rescan and convert to TreeBins |
3449 |
< |
Node[] tab = table; |
3450 |
< |
for (int i = 0; i < tab.length; ++i) { |
3451 |
< |
int c = 0; |
3452 |
< |
for (Node e = tabAt(tab, i); e != null; e = e.next) { |
3453 |
< |
if (++c > TREE_THRESHOLD && |
3454 |
< |
(e.key instanceof Comparable)) { |
3455 |
< |
replaceWithTreeBin(tab, i, e.key); |
3456 |
< |
break; |
3457 |
< |
} |
3458 |
< |
} |
3459 |
< |
} |
3460 |
< |
} |
3461 |
< |
} |
3462 |
< |
if (!init) { // Can only happen if unsafely published. |
3463 |
< |
while (p != null) { |
3464 |
< |
internalPut(p.key, p.val); |
3465 |
< |
p = p.next; |
3466 |
< |
} |
3467 |
< |
} |
3378 |
> |
|
3379 |
> |
public boolean tryAdvance(Action<? super V> action) { |
3380 |
> |
if (action == null) throw new NullPointerException(); |
3381 |
> |
Node<K,V> p; |
3382 |
> |
if ((p = advance()) == null) |
3383 |
> |
return false; |
3384 |
> |
action.apply(p.val); |
3385 |
> |
return true; |
3386 |
|
} |
3387 |
+ |
|
3388 |
+ |
public long estimateSize() { return est; } |
3389 |
+ |
|
3390 |
|
} |
3391 |
|
|
3392 |
+ |
static final class EntrySpliterator<K,V> extends Traverser<K,V> |
3393 |
+ |
implements ConcurrentHashMapSpliterator<Map.Entry<K,V>> { |
3394 |
+ |
final ConcurrentHashMapV8<K,V> map; // To export MapEntry |
3395 |
+ |
long est; // size estimate |
3396 |
+ |
EntrySpliterator(Node<K,V>[] tab, int size, int index, int limit, |
3397 |
+ |
long est, ConcurrentHashMapV8<K,V> map) { |
3398 |
+ |
super(tab, size, index, limit); |
3399 |
+ |
this.map = map; |
3400 |
+ |
this.est = est; |
3401 |
+ |
} |
3402 |
|
|
3403 |
< |
// ------------------------------------------------------- |
3403 |
> |
public ConcurrentHashMapSpliterator<Map.Entry<K,V>> trySplit() { |
3404 |
> |
int i, f, h; |
3405 |
> |
return (h = ((i = baseIndex) + (f = baseLimit)) >>> 1) <= i ? null : |
3406 |
> |
new EntrySpliterator<K,V>(tab, baseSize, baseLimit = h, |
3407 |
> |
f, est >>>= 1, map); |
3408 |
> |
} |
3409 |
|
|
3410 |
< |
// Sams |
3411 |
< |
/** Interface describing a void action of one argument */ |
3412 |
< |
public interface Action<A> { void apply(A a); } |
3413 |
< |
/** Interface describing a void action of two arguments */ |
3414 |
< |
public interface BiAction<A,B> { void apply(A a, B b); } |
3415 |
< |
/** Interface describing a function of one argument */ |
3416 |
< |
public interface Fun<A,T> { T apply(A a); } |
3417 |
< |
/** Interface describing a function of two arguments */ |
3418 |
< |
public interface BiFun<A,B,T> { T apply(A a, B b); } |
3419 |
< |
/** Interface describing a function of no arguments */ |
3420 |
< |
public interface Generator<T> { T apply(); } |
3421 |
< |
/** Interface describing a function mapping its argument to a double */ |
3422 |
< |
public interface ObjectToDouble<A> { double apply(A a); } |
3423 |
< |
/** Interface describing a function mapping its argument to a long */ |
3488 |
< |
public interface ObjectToLong<A> { long apply(A a); } |
3489 |
< |
/** Interface describing a function mapping its argument to an int */ |
3490 |
< |
public interface ObjectToInt<A> {int apply(A a); } |
3491 |
< |
/** Interface describing a function mapping two arguments to a double */ |
3492 |
< |
public interface ObjectByObjectToDouble<A,B> { double apply(A a, B b); } |
3493 |
< |
/** Interface describing a function mapping two arguments to a long */ |
3494 |
< |
public interface ObjectByObjectToLong<A,B> { long apply(A a, B b); } |
3495 |
< |
/** Interface describing a function mapping two arguments to an int */ |
3496 |
< |
public interface ObjectByObjectToInt<A,B> {int apply(A a, B b); } |
3497 |
< |
/** Interface describing a function mapping a double to a double */ |
3498 |
< |
public interface DoubleToDouble { double apply(double a); } |
3499 |
< |
/** Interface describing a function mapping a long to a long */ |
3500 |
< |
public interface LongToLong { long apply(long a); } |
3501 |
< |
/** Interface describing a function mapping an int to an int */ |
3502 |
< |
public interface IntToInt { int apply(int a); } |
3503 |
< |
/** Interface describing a function mapping two doubles to a double */ |
3504 |
< |
public interface DoubleByDoubleToDouble { double apply(double a, double b); } |
3505 |
< |
/** Interface describing a function mapping two longs to a long */ |
3506 |
< |
public interface LongByLongToLong { long apply(long a, long b); } |
3507 |
< |
/** Interface describing a function mapping two ints to an int */ |
3508 |
< |
public interface IntByIntToInt { int apply(int a, int b); } |
3410 |
> |
public void forEachRemaining(Action<? super Map.Entry<K,V>> action) { |
3411 |
> |
if (action == null) throw new NullPointerException(); |
3412 |
> |
for (Node<K,V> p; (p = advance()) != null; ) |
3413 |
> |
action.apply(new MapEntry<K,V>(p.key, p.val, map)); |
3414 |
> |
} |
3415 |
> |
|
3416 |
> |
public boolean tryAdvance(Action<? super Map.Entry<K,V>> action) { |
3417 |
> |
if (action == null) throw new NullPointerException(); |
3418 |
> |
Node<K,V> p; |
3419 |
> |
if ((p = advance()) == null) |
3420 |
> |
return false; |
3421 |
> |
action.apply(new MapEntry<K,V>(p.key, p.val, map)); |
3422 |
> |
return true; |
3423 |
> |
} |
3424 |
|
|
3425 |
+ |
public long estimateSize() { return est; } |
3426 |
|
|
3427 |
< |
// ------------------------------------------------------- |
3427 |
> |
} |
3428 |
> |
|
3429 |
> |
// Parallel bulk operations |
3430 |
> |
|
3431 |
> |
/** |
3432 |
> |
* Computes initial batch value for bulk tasks. The returned value |
3433 |
> |
* is approximately exp2 of the number of times (minus one) to |
3434 |
> |
* split task by two before executing leaf action. This value is |
3435 |
> |
* faster to compute and more convenient to use as a guide to |
3436 |
> |
* splitting than is the depth, since it is used while dividing by |
3437 |
> |
* two anyway. |
3438 |
> |
*/ |
3439 |
> |
final int batchFor(long b) { |
3440 |
> |
long n; |
3441 |
> |
if (b == Long.MAX_VALUE || (n = sumCount()) <= 1L || n < b) |
3442 |
> |
return 0; |
3443 |
> |
int sp = ForkJoinPool.getCommonPoolParallelism() << 2; // slack of 4 |
3444 |
> |
return (b <= 0L || (n /= b) >= sp) ? sp : (int)n; |
3445 |
> |
} |
3446 |
|
|
3447 |
|
/** |
3448 |
|
* Performs the given action for each (key, value). |
3449 |
|
* |
3450 |
+ |
* @param parallelismThreshold the (estimated) number of elements |
3451 |
+ |
* needed for this operation to be executed in parallel |
3452 |
|
* @param action the action |
3453 |
+ |
* @since 1.8 |
3454 |
|
*/ |
3455 |
< |
public void forEach(BiAction<K,V> action) { |
3456 |
< |
ForkJoinTasks.forEach |
3457 |
< |
(this, action).invoke(); |
3455 |
> |
public void forEach(long parallelismThreshold, |
3456 |
> |
BiAction<? super K,? super V> action) { |
3457 |
> |
if (action == null) throw new NullPointerException(); |
3458 |
> |
new ForEachMappingTask<K,V> |
3459 |
> |
(null, batchFor(parallelismThreshold), 0, 0, table, |
3460 |
> |
action).invoke(); |
3461 |
|
} |
3462 |
|
|
3463 |
|
/** |
3464 |
|
* Performs the given action for each non-null transformation |
3465 |
|
* of each (key, value). |
3466 |
|
* |
3467 |
+ |
* @param parallelismThreshold the (estimated) number of elements |
3468 |
+ |
* needed for this operation to be executed in parallel |
3469 |
|
* @param transformer a function returning the transformation |
3470 |
< |
* for an element, or null of there is no transformation (in |
3471 |
< |
* which case the action is not applied). |
3470 |
> |
* for an element, or null if there is no transformation (in |
3471 |
> |
* which case the action is not applied) |
3472 |
|
* @param action the action |
3473 |
+ |
* @since 1.8 |
3474 |
|
*/ |
3475 |
< |
public <U> void forEach(BiFun<? super K, ? super V, ? extends U> transformer, |
3476 |
< |
Action<U> action) { |
3477 |
< |
ForkJoinTasks.forEach |
3478 |
< |
(this, transformer, action).invoke(); |
3475 |
> |
public <U> void forEach(long parallelismThreshold, |
3476 |
> |
BiFun<? super K, ? super V, ? extends U> transformer, |
3477 |
> |
Action<? super U> action) { |
3478 |
> |
if (transformer == null || action == null) |
3479 |
> |
throw new NullPointerException(); |
3480 |
> |
new ForEachTransformedMappingTask<K,V,U> |
3481 |
> |
(null, batchFor(parallelismThreshold), 0, 0, table, |
3482 |
> |
transformer, action).invoke(); |
3483 |
|
} |
3484 |
|
|
3485 |
|
/** |
3489 |
|
* results of any other parallel invocations of the search |
3490 |
|
* function are ignored. |
3491 |
|
* |
3492 |
+ |
* @param parallelismThreshold the (estimated) number of elements |
3493 |
+ |
* needed for this operation to be executed in parallel |
3494 |
|
* @param searchFunction a function returning a non-null |
3495 |
|
* result on success, else null |
3496 |
|
* @return a non-null result from applying the given search |
3497 |
|
* function on each (key, value), or null if none |
3498 |
+ |
* @since 1.8 |
3499 |
|
*/ |
3500 |
< |
public <U> U search(BiFun<? super K, ? super V, ? extends U> searchFunction) { |
3501 |
< |
return ForkJoinTasks.search |
3502 |
< |
(this, searchFunction).invoke(); |
3500 |
> |
public <U> U search(long parallelismThreshold, |
3501 |
> |
BiFun<? super K, ? super V, ? extends U> searchFunction) { |
3502 |
> |
if (searchFunction == null) throw new NullPointerException(); |
3503 |
> |
return new SearchMappingsTask<K,V,U> |
3504 |
> |
(null, batchFor(parallelismThreshold), 0, 0, table, |
3505 |
> |
searchFunction, new AtomicReference<U>()).invoke(); |
3506 |
|
} |
3507 |
|
|
3508 |
|
/** |
3510 |
|
* of all (key, value) pairs using the given reducer to |
3511 |
|
* combine values, or null if none. |
3512 |
|
* |
3513 |
+ |
* @param parallelismThreshold the (estimated) number of elements |
3514 |
+ |
* needed for this operation to be executed in parallel |
3515 |
|
* @param transformer a function returning the transformation |
3516 |
< |
* for an element, or null of there is no transformation (in |
3517 |
< |
* which case it is not combined). |
3516 |
> |
* for an element, or null if there is no transformation (in |
3517 |
> |
* which case it is not combined) |
3518 |
|
* @param reducer a commutative associative combining function |
3519 |
|
* @return the result of accumulating the given transformation |
3520 |
|
* of all (key, value) pairs |
3521 |
+ |
* @since 1.8 |
3522 |
|
*/ |
3523 |
< |
public <U> U reduce(BiFun<? super K, ? super V, ? extends U> transformer, |
3523 |
> |
public <U> U reduce(long parallelismThreshold, |
3524 |
> |
BiFun<? super K, ? super V, ? extends U> transformer, |
3525 |
|
BiFun<? super U, ? super U, ? extends U> reducer) { |
3526 |
< |
return ForkJoinTasks.reduce |
3527 |
< |
(this, transformer, reducer).invoke(); |
3526 |
> |
if (transformer == null || reducer == null) |
3527 |
> |
throw new NullPointerException(); |
3528 |
> |
return new MapReduceMappingsTask<K,V,U> |
3529 |
> |
(null, batchFor(parallelismThreshold), 0, 0, table, |
3530 |
> |
null, transformer, reducer).invoke(); |
3531 |
|
} |
3532 |
|
|
3533 |
|
/** |
3535 |
|
* of all (key, value) pairs using the given reducer to |
3536 |
|
* combine values, and the given basis as an identity value. |
3537 |
|
* |
3538 |
+ |
* @param parallelismThreshold the (estimated) number of elements |
3539 |
+ |
* needed for this operation to be executed in parallel |
3540 |
|
* @param transformer a function returning the transformation |
3541 |
|
* for an element |
3542 |
|
* @param basis the identity (initial default value) for the reduction |
3543 |
|
* @param reducer a commutative associative combining function |
3544 |
|
* @return the result of accumulating the given transformation |
3545 |
|
* of all (key, value) pairs |
3546 |
+ |
* @since 1.8 |
3547 |
|
*/ |
3548 |
< |
public double reduceToDouble(ObjectByObjectToDouble<? super K, ? super V> transformer, |
3548 |
> |
public double reduceToDouble(long parallelismThreshold, |
3549 |
> |
ObjectByObjectToDouble<? super K, ? super V> transformer, |
3550 |
|
double basis, |
3551 |
|
DoubleByDoubleToDouble reducer) { |
3552 |
< |
return ForkJoinTasks.reduceToDouble |
3553 |
< |
(this, transformer, basis, reducer).invoke(); |
3552 |
> |
if (transformer == null || reducer == null) |
3553 |
> |
throw new NullPointerException(); |
3554 |
> |
return new MapReduceMappingsToDoubleTask<K,V> |
3555 |
> |
(null, batchFor(parallelismThreshold), 0, 0, table, |
3556 |
> |
null, transformer, basis, reducer).invoke(); |
3557 |
|
} |
3558 |
|
|
3559 |
|
/** |
3561 |
|
* of all (key, value) pairs using the given reducer to |
3562 |
|
* combine values, and the given basis as an identity value. |
3563 |
|
* |
3564 |
+ |
* @param parallelismThreshold the (estimated) number of elements |
3565 |
+ |
* needed for this operation to be executed in parallel |
3566 |
|
* @param transformer a function returning the transformation |
3567 |
|
* for an element |
3568 |
|
* @param basis the identity (initial default value) for the reduction |
3569 |
|
* @param reducer a commutative associative combining function |
3570 |
|
* @return the result of accumulating the given transformation |
3571 |
|
* of all (key, value) pairs |
3572 |
+ |
* @since 1.8 |
3573 |
|
*/ |
3574 |
< |
public long reduceToLong(ObjectByObjectToLong<? super K, ? super V> transformer, |
3574 |
> |
public long reduceToLong(long parallelismThreshold, |
3575 |
> |
ObjectByObjectToLong<? super K, ? super V> transformer, |
3576 |
|
long basis, |
3577 |
|
LongByLongToLong reducer) { |
3578 |
< |
return ForkJoinTasks.reduceToLong |
3579 |
< |
(this, transformer, basis, reducer).invoke(); |
3578 |
> |
if (transformer == null || reducer == null) |
3579 |
> |
throw new NullPointerException(); |
3580 |
> |
return new MapReduceMappingsToLongTask<K,V> |
3581 |
> |
(null, batchFor(parallelismThreshold), 0, 0, table, |
3582 |
> |
null, transformer, basis, reducer).invoke(); |
3583 |
|
} |
3584 |
|
|
3585 |
|
/** |
3587 |
|
* of all (key, value) pairs using the given reducer to |
3588 |
|
* combine values, and the given basis as an identity value. |
3589 |
|
* |
3590 |
+ |
* @param parallelismThreshold the (estimated) number of elements |
3591 |
+ |
* needed for this operation to be executed in parallel |
3592 |
|
* @param transformer a function returning the transformation |
3593 |
|
* for an element |
3594 |
|
* @param basis the identity (initial default value) for the reduction |
3595 |
|
* @param reducer a commutative associative combining function |
3596 |
|
* @return the result of accumulating the given transformation |
3597 |
|
* of all (key, value) pairs |
3598 |
+ |
* @since 1.8 |
3599 |
|
*/ |
3600 |
< |
public int reduceToInt(ObjectByObjectToInt<? super K, ? super V> transformer, |
3600 |
> |
public int reduceToInt(long parallelismThreshold, |
3601 |
> |
ObjectByObjectToInt<? super K, ? super V> transformer, |
3602 |
|
int basis, |
3603 |
|
IntByIntToInt reducer) { |
3604 |
< |
return ForkJoinTasks.reduceToInt |
3605 |
< |
(this, transformer, basis, reducer).invoke(); |
3604 |
> |
if (transformer == null || reducer == null) |
3605 |
> |
throw new NullPointerException(); |
3606 |
> |
return new MapReduceMappingsToIntTask<K,V> |
3607 |
> |
(null, batchFor(parallelismThreshold), 0, 0, table, |
3608 |
> |
null, transformer, basis, reducer).invoke(); |
3609 |
|
} |
3610 |
|
|
3611 |
|
/** |
3612 |
|
* Performs the given action for each key. |
3613 |
|
* |
3614 |
+ |
* @param parallelismThreshold the (estimated) number of elements |
3615 |
+ |
* needed for this operation to be executed in parallel |
3616 |
|
* @param action the action |
3617 |
+ |
* @since 1.8 |
3618 |
|
*/ |
3619 |
< |
public void forEachKey(Action<K> action) { |
3620 |
< |
ForkJoinTasks.forEachKey |
3621 |
< |
(this, action).invoke(); |
3619 |
> |
public void forEachKey(long parallelismThreshold, |
3620 |
> |
Action<? super K> action) { |
3621 |
> |
if (action == null) throw new NullPointerException(); |
3622 |
> |
new ForEachKeyTask<K,V> |
3623 |
> |
(null, batchFor(parallelismThreshold), 0, 0, table, |
3624 |
> |
action).invoke(); |
3625 |
|
} |
3626 |
|
|
3627 |
|
/** |
3628 |
|
* Performs the given action for each non-null transformation |
3629 |
|
* of each key. |
3630 |
|
* |
3631 |
+ |
* @param parallelismThreshold the (estimated) number of elements |
3632 |
+ |
* needed for this operation to be executed in parallel |
3633 |
|
* @param transformer a function returning the transformation |
3634 |
< |
* for an element, or null of there is no transformation (in |
3635 |
< |
* which case the action is not applied). |
3634 |
> |
* for an element, or null if there is no transformation (in |
3635 |
> |
* which case the action is not applied) |
3636 |
|
* @param action the action |
3637 |
+ |
* @since 1.8 |
3638 |
|
*/ |
3639 |
< |
public <U> void forEachKey(Fun<? super K, ? extends U> transformer, |
3640 |
< |
Action<U> action) { |
3641 |
< |
ForkJoinTasks.forEachKey |
3642 |
< |
(this, transformer, action).invoke(); |
3639 |
> |
public <U> void forEachKey(long parallelismThreshold, |
3640 |
> |
Fun<? super K, ? extends U> transformer, |
3641 |
> |
Action<? super U> action) { |
3642 |
> |
if (transformer == null || action == null) |
3643 |
> |
throw new NullPointerException(); |
3644 |
> |
new ForEachTransformedKeyTask<K,V,U> |
3645 |
> |
(null, batchFor(parallelismThreshold), 0, 0, table, |
3646 |
> |
transformer, action).invoke(); |
3647 |
|
} |
3648 |
|
|
3649 |
|
/** |
3653 |
|
* any other parallel invocations of the search function are |
3654 |
|
* ignored. |
3655 |
|
* |
3656 |
+ |
* @param parallelismThreshold the (estimated) number of elements |
3657 |
+ |
* needed for this operation to be executed in parallel |
3658 |
|
* @param searchFunction a function returning a non-null |
3659 |
|
* result on success, else null |
3660 |
|
* @return a non-null result from applying the given search |
3661 |
|
* function on each key, or null if none |
3662 |
+ |
* @since 1.8 |
3663 |
|
*/ |
3664 |
< |
public <U> U searchKeys(Fun<? super K, ? extends U> searchFunction) { |
3665 |
< |
return ForkJoinTasks.searchKeys |
3666 |
< |
(this, searchFunction).invoke(); |
3664 |
> |
public <U> U searchKeys(long parallelismThreshold, |
3665 |
> |
Fun<? super K, ? extends U> searchFunction) { |
3666 |
> |
if (searchFunction == null) throw new NullPointerException(); |
3667 |
> |
return new SearchKeysTask<K,V,U> |
3668 |
> |
(null, batchFor(parallelismThreshold), 0, 0, table, |
3669 |
> |
searchFunction, new AtomicReference<U>()).invoke(); |
3670 |
|
} |
3671 |
|
|
3672 |
|
/** |
3673 |
|
* Returns the result of accumulating all keys using the given |
3674 |
|
* reducer to combine values, or null if none. |
3675 |
|
* |
3676 |
+ |
* @param parallelismThreshold the (estimated) number of elements |
3677 |
+ |
* needed for this operation to be executed in parallel |
3678 |
|
* @param reducer a commutative associative combining function |
3679 |
|
* @return the result of accumulating all keys using the given |
3680 |
|
* reducer to combine values, or null if none |
3681 |
+ |
* @since 1.8 |
3682 |
|
*/ |
3683 |
< |
public K reduceKeys(BiFun<? super K, ? super K, ? extends K> reducer) { |
3684 |
< |
return ForkJoinTasks.reduceKeys |
3685 |
< |
(this, reducer).invoke(); |
3683 |
> |
public K reduceKeys(long parallelismThreshold, |
3684 |
> |
BiFun<? super K, ? super K, ? extends K> reducer) { |
3685 |
> |
if (reducer == null) throw new NullPointerException(); |
3686 |
> |
return new ReduceKeysTask<K,V> |
3687 |
> |
(null, batchFor(parallelismThreshold), 0, 0, table, |
3688 |
> |
null, reducer).invoke(); |
3689 |
|
} |
3690 |
|
|
3691 |
|
/** |
3693 |
|
* of all keys using the given reducer to combine values, or |
3694 |
|
* null if none. |
3695 |
|
* |
3696 |
+ |
* @param parallelismThreshold the (estimated) number of elements |
3697 |
+ |
* needed for this operation to be executed in parallel |
3698 |
|
* @param transformer a function returning the transformation |
3699 |
< |
* for an element, or null of there is no transformation (in |
3700 |
< |
* which case it is not combined). |
3699 |
> |
* for an element, or null if there is no transformation (in |
3700 |
> |
* which case it is not combined) |
3701 |
|
* @param reducer a commutative associative combining function |
3702 |
|
* @return the result of accumulating the given transformation |
3703 |
|
* of all keys |
3704 |
+ |
* @since 1.8 |
3705 |
|
*/ |
3706 |
< |
public <U> U reduceKeys(Fun<? super K, ? extends U> transformer, |
3707 |
< |
BiFun<? super U, ? super U, ? extends U> reducer) { |
3708 |
< |
return ForkJoinTasks.reduceKeys |
3709 |
< |
(this, transformer, reducer).invoke(); |
3706 |
> |
public <U> U reduceKeys(long parallelismThreshold, |
3707 |
> |
Fun<? super K, ? extends U> transformer, |
3708 |
> |
BiFun<? super U, ? super U, ? extends U> reducer) { |
3709 |
> |
if (transformer == null || reducer == null) |
3710 |
> |
throw new NullPointerException(); |
3711 |
> |
return new MapReduceKeysTask<K,V,U> |
3712 |
> |
(null, batchFor(parallelismThreshold), 0, 0, table, |
3713 |
> |
null, transformer, reducer).invoke(); |
3714 |
|
} |
3715 |
|
|
3716 |
|
/** |
3718 |
|
* of all keys using the given reducer to combine values, and |
3719 |
|
* the given basis as an identity value. |
3720 |
|
* |
3721 |
+ |
* @param parallelismThreshold the (estimated) number of elements |
3722 |
+ |
* needed for this operation to be executed in parallel |
3723 |
|
* @param transformer a function returning the transformation |
3724 |
|
* for an element |
3725 |
|
* @param basis the identity (initial default value) for the reduction |
3726 |
|
* @param reducer a commutative associative combining function |
3727 |
< |
* @return the result of accumulating the given transformation |
3727 |
> |
* @return the result of accumulating the given transformation |
3728 |
|
* of all keys |
3729 |
+ |
* @since 1.8 |
3730 |
|
*/ |
3731 |
< |
public double reduceKeysToDouble(ObjectToDouble<? super K> transformer, |
3731 |
> |
public double reduceKeysToDouble(long parallelismThreshold, |
3732 |
> |
ObjectToDouble<? super K> transformer, |
3733 |
|
double basis, |
3734 |
|
DoubleByDoubleToDouble reducer) { |
3735 |
< |
return ForkJoinTasks.reduceKeysToDouble |
3736 |
< |
(this, transformer, basis, reducer).invoke(); |
3735 |
> |
if (transformer == null || reducer == null) |
3736 |
> |
throw new NullPointerException(); |
3737 |
> |
return new MapReduceKeysToDoubleTask<K,V> |
3738 |
> |
(null, batchFor(parallelismThreshold), 0, 0, table, |
3739 |
> |
null, transformer, basis, reducer).invoke(); |
3740 |
|
} |
3741 |
|
|
3742 |
|
/** |
3744 |
|
* of all keys using the given reducer to combine values, and |
3745 |
|
* the given basis as an identity value. |
3746 |
|
* |
3747 |
+ |
* @param parallelismThreshold the (estimated) number of elements |
3748 |
+ |
* needed for this operation to be executed in parallel |
3749 |
|
* @param transformer a function returning the transformation |
3750 |
|
* for an element |
3751 |
|
* @param basis the identity (initial default value) for the reduction |
3752 |
|
* @param reducer a commutative associative combining function |
3753 |
|
* @return the result of accumulating the given transformation |
3754 |
|
* of all keys |
3755 |
+ |
* @since 1.8 |
3756 |
|
*/ |
3757 |
< |
public long reduceKeysToLong(ObjectToLong<? super K> transformer, |
3757 |
> |
public long reduceKeysToLong(long parallelismThreshold, |
3758 |
> |
ObjectToLong<? super K> transformer, |
3759 |
|
long basis, |
3760 |
|
LongByLongToLong reducer) { |
3761 |
< |
return ForkJoinTasks.reduceKeysToLong |
3762 |
< |
(this, transformer, basis, reducer).invoke(); |
3761 |
> |
if (transformer == null || reducer == null) |
3762 |
> |
throw new NullPointerException(); |
3763 |
> |
return new MapReduceKeysToLongTask<K,V> |
3764 |
> |
(null, batchFor(parallelismThreshold), 0, 0, table, |
3765 |
> |
null, transformer, basis, reducer).invoke(); |
3766 |
|
} |
3767 |
|
|
3768 |
|
/** |
3770 |
|
* of all keys using the given reducer to combine values, and |
3771 |
|
* the given basis as an identity value. |
3772 |
|
* |
3773 |
+ |
* @param parallelismThreshold the (estimated) number of elements |
3774 |
+ |
* needed for this operation to be executed in parallel |
3775 |
|
* @param transformer a function returning the transformation |
3776 |
|
* for an element |
3777 |
|
* @param basis the identity (initial default value) for the reduction |
3778 |
|
* @param reducer a commutative associative combining function |
3779 |
|
* @return the result of accumulating the given transformation |
3780 |
|
* of all keys |
3781 |
+ |
* @since 1.8 |
3782 |
|
*/ |
3783 |
< |
public int reduceKeysToInt(ObjectToInt<? super K> transformer, |
3783 |
> |
public int reduceKeysToInt(long parallelismThreshold, |
3784 |
> |
ObjectToInt<? super K> transformer, |
3785 |
|
int basis, |
3786 |
|
IntByIntToInt reducer) { |
3787 |
< |
return ForkJoinTasks.reduceKeysToInt |
3788 |
< |
(this, transformer, basis, reducer).invoke(); |
3787 |
> |
if (transformer == null || reducer == null) |
3788 |
> |
throw new NullPointerException(); |
3789 |
> |
return new MapReduceKeysToIntTask<K,V> |
3790 |
> |
(null, batchFor(parallelismThreshold), 0, 0, table, |
3791 |
> |
null, transformer, basis, reducer).invoke(); |
3792 |
|
} |
3793 |
|
|
3794 |
|
/** |
3795 |
|
* Performs the given action for each value. |
3796 |
|
* |
3797 |
+ |
* @param parallelismThreshold the (estimated) number of elements |
3798 |
+ |
* needed for this operation to be executed in parallel |
3799 |
|
* @param action the action |
3800 |
+ |
* @since 1.8 |
3801 |
|
*/ |
3802 |
< |
public void forEachValue(Action<V> action) { |
3803 |
< |
ForkJoinTasks.forEachValue |
3804 |
< |
(this, action).invoke(); |
3802 |
> |
public void forEachValue(long parallelismThreshold, |
3803 |
> |
Action<? super V> action) { |
3804 |
> |
if (action == null) |
3805 |
> |
throw new NullPointerException(); |
3806 |
> |
new ForEachValueTask<K,V> |
3807 |
> |
(null, batchFor(parallelismThreshold), 0, 0, table, |
3808 |
> |
action).invoke(); |
3809 |
|
} |
3810 |
|
|
3811 |
|
/** |
3812 |
|
* Performs the given action for each non-null transformation |
3813 |
|
* of each value. |
3814 |
|
* |
3815 |
+ |
* @param parallelismThreshold the (estimated) number of elements |
3816 |
+ |
* needed for this operation to be executed in parallel |
3817 |
|
* @param transformer a function returning the transformation |
3818 |
< |
* for an element, or null of there is no transformation (in |
3819 |
< |
* which case the action is not applied). |
3818 |
> |
* for an element, or null if there is no transformation (in |
3819 |
> |
* which case the action is not applied) |
3820 |
> |
* @param action the action |
3821 |
> |
* @since 1.8 |
3822 |
|
*/ |
3823 |
< |
public <U> void forEachValue(Fun<? super V, ? extends U> transformer, |
3824 |
< |
Action<U> action) { |
3825 |
< |
ForkJoinTasks.forEachValue |
3826 |
< |
(this, transformer, action).invoke(); |
3823 |
> |
public <U> void forEachValue(long parallelismThreshold, |
3824 |
> |
Fun<? super V, ? extends U> transformer, |
3825 |
> |
Action<? super U> action) { |
3826 |
> |
if (transformer == null || action == null) |
3827 |
> |
throw new NullPointerException(); |
3828 |
> |
new ForEachTransformedValueTask<K,V,U> |
3829 |
> |
(null, batchFor(parallelismThreshold), 0, 0, table, |
3830 |
> |
transformer, action).invoke(); |
3831 |
|
} |
3832 |
|
|
3833 |
|
/** |
3837 |
|
* any other parallel invocations of the search function are |
3838 |
|
* ignored. |
3839 |
|
* |
3840 |
+ |
* @param parallelismThreshold the (estimated) number of elements |
3841 |
+ |
* needed for this operation to be executed in parallel |
3842 |
|
* @param searchFunction a function returning a non-null |
3843 |
|
* result on success, else null |
3844 |
|
* @return a non-null result from applying the given search |
3845 |
|
* function on each value, or null if none |
3846 |
< |
* |
3846 |
> |
* @since 1.8 |
3847 |
|
*/ |
3848 |
< |
public <U> U searchValues(Fun<? super V, ? extends U> searchFunction) { |
3849 |
< |
return ForkJoinTasks.searchValues |
3850 |
< |
(this, searchFunction).invoke(); |
3848 |
> |
public <U> U searchValues(long parallelismThreshold, |
3849 |
> |
Fun<? super V, ? extends U> searchFunction) { |
3850 |
> |
if (searchFunction == null) throw new NullPointerException(); |
3851 |
> |
return new SearchValuesTask<K,V,U> |
3852 |
> |
(null, batchFor(parallelismThreshold), 0, 0, table, |
3853 |
> |
searchFunction, new AtomicReference<U>()).invoke(); |
3854 |
|
} |
3855 |
|
|
3856 |
|
/** |
3857 |
|
* Returns the result of accumulating all values using the |
3858 |
|
* given reducer to combine values, or null if none. |
3859 |
|
* |
3860 |
+ |
* @param parallelismThreshold the (estimated) number of elements |
3861 |
+ |
* needed for this operation to be executed in parallel |
3862 |
|
* @param reducer a commutative associative combining function |
3863 |
< |
* @return the result of accumulating all values |
3863 |
> |
* @return the result of accumulating all values |
3864 |
> |
* @since 1.8 |
3865 |
|
*/ |
3866 |
< |
public V reduceValues(BiFun<? super V, ? super V, ? extends V> reducer) { |
3867 |
< |
return ForkJoinTasks.reduceValues |
3868 |
< |
(this, reducer).invoke(); |
3866 |
> |
public V reduceValues(long parallelismThreshold, |
3867 |
> |
BiFun<? super V, ? super V, ? extends V> reducer) { |
3868 |
> |
if (reducer == null) throw new NullPointerException(); |
3869 |
> |
return new ReduceValuesTask<K,V> |
3870 |
> |
(null, batchFor(parallelismThreshold), 0, 0, table, |
3871 |
> |
null, reducer).invoke(); |
3872 |
|
} |
3873 |
|
|
3874 |
|
/** |
3876 |
|
* of all values using the given reducer to combine values, or |
3877 |
|
* null if none. |
3878 |
|
* |
3879 |
+ |
* @param parallelismThreshold the (estimated) number of elements |
3880 |
+ |
* needed for this operation to be executed in parallel |
3881 |
|
* @param transformer a function returning the transformation |
3882 |
< |
* for an element, or null of there is no transformation (in |
3883 |
< |
* which case it is not combined). |
3882 |
> |
* for an element, or null if there is no transformation (in |
3883 |
> |
* which case it is not combined) |
3884 |
|
* @param reducer a commutative associative combining function |
3885 |
|
* @return the result of accumulating the given transformation |
3886 |
|
* of all values |
3887 |
+ |
* @since 1.8 |
3888 |
|
*/ |
3889 |
< |
public <U> U reduceValues(Fun<? super V, ? extends U> transformer, |
3889 |
> |
public <U> U reduceValues(long parallelismThreshold, |
3890 |
> |
Fun<? super V, ? extends U> transformer, |
3891 |
|
BiFun<? super U, ? super U, ? extends U> reducer) { |
3892 |
< |
return ForkJoinTasks.reduceValues |
3893 |
< |
(this, transformer, reducer).invoke(); |
3892 |
> |
if (transformer == null || reducer == null) |
3893 |
> |
throw new NullPointerException(); |
3894 |
> |
return new MapReduceValuesTask<K,V,U> |
3895 |
> |
(null, batchFor(parallelismThreshold), 0, 0, table, |
3896 |
> |
null, transformer, reducer).invoke(); |
3897 |
|
} |
3898 |
|
|
3899 |
|
/** |
3901 |
|
* of all values using the given reducer to combine values, |
3902 |
|
* and the given basis as an identity value. |
3903 |
|
* |
3904 |
+ |
* @param parallelismThreshold the (estimated) number of elements |
3905 |
+ |
* needed for this operation to be executed in parallel |
3906 |
|
* @param transformer a function returning the transformation |
3907 |
|
* for an element |
3908 |
|
* @param basis the identity (initial default value) for the reduction |
3909 |
|
* @param reducer a commutative associative combining function |
3910 |
|
* @return the result of accumulating the given transformation |
3911 |
|
* of all values |
3912 |
+ |
* @since 1.8 |
3913 |
|
*/ |
3914 |
< |
public double reduceValuesToDouble(ObjectToDouble<? super V> transformer, |
3914 |
> |
public double reduceValuesToDouble(long parallelismThreshold, |
3915 |
> |
ObjectToDouble<? super V> transformer, |
3916 |
|
double basis, |
3917 |
|
DoubleByDoubleToDouble reducer) { |
3918 |
< |
return ForkJoinTasks.reduceValuesToDouble |
3919 |
< |
(this, transformer, basis, reducer).invoke(); |
3918 |
> |
if (transformer == null || reducer == null) |
3919 |
> |
throw new NullPointerException(); |
3920 |
> |
return new MapReduceValuesToDoubleTask<K,V> |
3921 |
> |
(null, batchFor(parallelismThreshold), 0, 0, table, |
3922 |
> |
null, transformer, basis, reducer).invoke(); |
3923 |
|
} |
3924 |
|
|
3925 |
|
/** |
3927 |
|
* of all values using the given reducer to combine values, |
3928 |
|
* and the given basis as an identity value. |
3929 |
|
* |
3930 |
+ |
* @param parallelismThreshold the (estimated) number of elements |
3931 |
+ |
* needed for this operation to be executed in parallel |
3932 |
|
* @param transformer a function returning the transformation |
3933 |
|
* for an element |
3934 |
|
* @param basis the identity (initial default value) for the reduction |
3935 |
|
* @param reducer a commutative associative combining function |
3936 |
|
* @return the result of accumulating the given transformation |
3937 |
|
* of all values |
3938 |
+ |
* @since 1.8 |
3939 |
|
*/ |
3940 |
< |
public long reduceValuesToLong(ObjectToLong<? super V> transformer, |
3940 |
> |
public long reduceValuesToLong(long parallelismThreshold, |
3941 |
> |
ObjectToLong<? super V> transformer, |
3942 |
|
long basis, |
3943 |
|
LongByLongToLong reducer) { |
3944 |
< |
return ForkJoinTasks.reduceValuesToLong |
3945 |
< |
(this, transformer, basis, reducer).invoke(); |
3944 |
> |
if (transformer == null || reducer == null) |
3945 |
> |
throw new NullPointerException(); |
3946 |
> |
return new MapReduceValuesToLongTask<K,V> |
3947 |
> |
(null, batchFor(parallelismThreshold), 0, 0, table, |
3948 |
> |
null, transformer, basis, reducer).invoke(); |
3949 |
|
} |
3950 |
|
|
3951 |
|
/** |
3953 |
|
* of all values using the given reducer to combine values, |
3954 |
|
* and the given basis as an identity value. |
3955 |
|
* |
3956 |
+ |
* @param parallelismThreshold the (estimated) number of elements |
3957 |
+ |
* needed for this operation to be executed in parallel |
3958 |
|
* @param transformer a function returning the transformation |
3959 |
|
* for an element |
3960 |
|
* @param basis the identity (initial default value) for the reduction |
3961 |
|
* @param reducer a commutative associative combining function |
3962 |
|
* @return the result of accumulating the given transformation |
3963 |
|
* of all values |
3964 |
+ |
* @since 1.8 |
3965 |
|
*/ |
3966 |
< |
public int reduceValuesToInt(ObjectToInt<? super V> transformer, |
3966 |
> |
public int reduceValuesToInt(long parallelismThreshold, |
3967 |
> |
ObjectToInt<? super V> transformer, |
3968 |
|
int basis, |
3969 |
|
IntByIntToInt reducer) { |
3970 |
< |
return ForkJoinTasks.reduceValuesToInt |
3971 |
< |
(this, transformer, basis, reducer).invoke(); |
3970 |
> |
if (transformer == null || reducer == null) |
3971 |
> |
throw new NullPointerException(); |
3972 |
> |
return new MapReduceValuesToIntTask<K,V> |
3973 |
> |
(null, batchFor(parallelismThreshold), 0, 0, table, |
3974 |
> |
null, transformer, basis, reducer).invoke(); |
3975 |
|
} |
3976 |
|
|
3977 |
|
/** |
3978 |
|
* Performs the given action for each entry. |
3979 |
|
* |
3980 |
+ |
* @param parallelismThreshold the (estimated) number of elements |
3981 |
+ |
* needed for this operation to be executed in parallel |
3982 |
|
* @param action the action |
3983 |
+ |
* @since 1.8 |
3984 |
|
*/ |
3985 |
< |
public void forEachEntry(Action<Map.Entry<K,V>> action) { |
3986 |
< |
ForkJoinTasks.forEachEntry |
3987 |
< |
(this, action).invoke(); |
3985 |
> |
public void forEachEntry(long parallelismThreshold, |
3986 |
> |
Action<? super Map.Entry<K,V>> action) { |
3987 |
> |
if (action == null) throw new NullPointerException(); |
3988 |
> |
new ForEachEntryTask<K,V>(null, batchFor(parallelismThreshold), 0, 0, table, |
3989 |
> |
action).invoke(); |
3990 |
|
} |
3991 |
|
|
3992 |
|
/** |
3993 |
|
* Performs the given action for each non-null transformation |
3994 |
|
* of each entry. |
3995 |
|
* |
3996 |
+ |
* @param parallelismThreshold the (estimated) number of elements |
3997 |
+ |
* needed for this operation to be executed in parallel |
3998 |
|
* @param transformer a function returning the transformation |
3999 |
< |
* for an element, or null of there is no transformation (in |
4000 |
< |
* which case the action is not applied). |
3999 |
> |
* for an element, or null if there is no transformation (in |
4000 |
> |
* which case the action is not applied) |
4001 |
|
* @param action the action |
4002 |
+ |
* @since 1.8 |
4003 |
|
*/ |
4004 |
< |
public <U> void forEachEntry(Fun<Map.Entry<K,V>, ? extends U> transformer, |
4005 |
< |
Action<U> action) { |
4006 |
< |
ForkJoinTasks.forEachEntry |
4007 |
< |
(this, transformer, action).invoke(); |
4004 |
> |
public <U> void forEachEntry(long parallelismThreshold, |
4005 |
> |
Fun<Map.Entry<K,V>, ? extends U> transformer, |
4006 |
> |
Action<? super U> action) { |
4007 |
> |
if (transformer == null || action == null) |
4008 |
> |
throw new NullPointerException(); |
4009 |
> |
new ForEachTransformedEntryTask<K,V,U> |
4010 |
> |
(null, batchFor(parallelismThreshold), 0, 0, table, |
4011 |
> |
transformer, action).invoke(); |
4012 |
|
} |
4013 |
|
|
4014 |
|
/** |
4018 |
|
* any other parallel invocations of the search function are |
4019 |
|
* ignored. |
4020 |
|
* |
4021 |
+ |
* @param parallelismThreshold the (estimated) number of elements |
4022 |
+ |
* needed for this operation to be executed in parallel |
4023 |
|
* @param searchFunction a function returning a non-null |
4024 |
|
* result on success, else null |
4025 |
|
* @return a non-null result from applying the given search |
4026 |
|
* function on each entry, or null if none |
4027 |
+ |
* @since 1.8 |
4028 |
|
*/ |
4029 |
< |
public <U> U searchEntries(Fun<Map.Entry<K,V>, ? extends U> searchFunction) { |
4030 |
< |
return ForkJoinTasks.searchEntries |
4031 |
< |
(this, searchFunction).invoke(); |
4029 |
> |
public <U> U searchEntries(long parallelismThreshold, |
4030 |
> |
Fun<Map.Entry<K,V>, ? extends U> searchFunction) { |
4031 |
> |
if (searchFunction == null) throw new NullPointerException(); |
4032 |
> |
return new SearchEntriesTask<K,V,U> |
4033 |
> |
(null, batchFor(parallelismThreshold), 0, 0, table, |
4034 |
> |
searchFunction, new AtomicReference<U>()).invoke(); |
4035 |
|
} |
4036 |
|
|
4037 |
|
/** |
4038 |
|
* Returns the result of accumulating all entries using the |
4039 |
|
* given reducer to combine values, or null if none. |
4040 |
|
* |
4041 |
+ |
* @param parallelismThreshold the (estimated) number of elements |
4042 |
+ |
* needed for this operation to be executed in parallel |
4043 |
|
* @param reducer a commutative associative combining function |
4044 |
|
* @return the result of accumulating all entries |
4045 |
+ |
* @since 1.8 |
4046 |
|
*/ |
4047 |
< |
public Map.Entry<K,V> reduceEntries(BiFun<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer) { |
4048 |
< |
return ForkJoinTasks.reduceEntries |
4049 |
< |
(this, reducer).invoke(); |
4047 |
> |
public Map.Entry<K,V> reduceEntries(long parallelismThreshold, |
4048 |
> |
BiFun<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer) { |
4049 |
> |
if (reducer == null) throw new NullPointerException(); |
4050 |
> |
return new ReduceEntriesTask<K,V> |
4051 |
> |
(null, batchFor(parallelismThreshold), 0, 0, table, |
4052 |
> |
null, reducer).invoke(); |
4053 |
|
} |
4054 |
|
|
4055 |
|
/** |
4057 |
|
* of all entries using the given reducer to combine values, |
4058 |
|
* or null if none. |
4059 |
|
* |
4060 |
+ |
* @param parallelismThreshold the (estimated) number of elements |
4061 |
+ |
* needed for this operation to be executed in parallel |
4062 |
|
* @param transformer a function returning the transformation |
4063 |
< |
* for an element, or null of there is no transformation (in |
4064 |
< |
* which case it is not combined). |
4063 |
> |
* for an element, or null if there is no transformation (in |
4064 |
> |
* which case it is not combined) |
4065 |
|
* @param reducer a commutative associative combining function |
4066 |
|
* @return the result of accumulating the given transformation |
4067 |
|
* of all entries |
4068 |
+ |
* @since 1.8 |
4069 |
|
*/ |
4070 |
< |
public <U> U reduceEntries(Fun<Map.Entry<K,V>, ? extends U> transformer, |
4070 |
> |
public <U> U reduceEntries(long parallelismThreshold, |
4071 |
> |
Fun<Map.Entry<K,V>, ? extends U> transformer, |
4072 |
|
BiFun<? super U, ? super U, ? extends U> reducer) { |
4073 |
< |
return ForkJoinTasks.reduceEntries |
4074 |
< |
(this, transformer, reducer).invoke(); |
4073 |
> |
if (transformer == null || reducer == null) |
4074 |
> |
throw new NullPointerException(); |
4075 |
> |
return new MapReduceEntriesTask<K,V,U> |
4076 |
> |
(null, batchFor(parallelismThreshold), 0, 0, table, |
4077 |
> |
null, transformer, reducer).invoke(); |
4078 |
|
} |
4079 |
|
|
4080 |
|
/** |
4082 |
|
* of all entries using the given reducer to combine values, |
4083 |
|
* and the given basis as an identity value. |
4084 |
|
* |
4085 |
+ |
* @param parallelismThreshold the (estimated) number of elements |
4086 |
+ |
* needed for this operation to be executed in parallel |
4087 |
|
* @param transformer a function returning the transformation |
4088 |
|
* for an element |
4089 |
|
* @param basis the identity (initial default value) for the reduction |
4090 |
|
* @param reducer a commutative associative combining function |
4091 |
|
* @return the result of accumulating the given transformation |
4092 |
|
* of all entries |
4093 |
+ |
* @since 1.8 |
4094 |
|
*/ |
4095 |
< |
public double reduceEntriesToDouble(ObjectToDouble<Map.Entry<K,V>> transformer, |
4095 |
> |
public double reduceEntriesToDouble(long parallelismThreshold, |
4096 |
> |
ObjectToDouble<Map.Entry<K,V>> transformer, |
4097 |
|
double basis, |
4098 |
|
DoubleByDoubleToDouble reducer) { |
4099 |
< |
return ForkJoinTasks.reduceEntriesToDouble |
4100 |
< |
(this, transformer, basis, reducer).invoke(); |
4099 |
> |
if (transformer == null || reducer == null) |
4100 |
> |
throw new NullPointerException(); |
4101 |
> |
return new MapReduceEntriesToDoubleTask<K,V> |
4102 |
> |
(null, batchFor(parallelismThreshold), 0, 0, table, |
4103 |
> |
null, transformer, basis, reducer).invoke(); |
4104 |
|
} |
4105 |
|
|
4106 |
|
/** |
4108 |
|
* of all entries using the given reducer to combine values, |
4109 |
|
* and the given basis as an identity value. |
4110 |
|
* |
4111 |
+ |
* @param parallelismThreshold the (estimated) number of elements |
4112 |
+ |
* needed for this operation to be executed in parallel |
4113 |
|
* @param transformer a function returning the transformation |
4114 |
|
* for an element |
4115 |
|
* @param basis the identity (initial default value) for the reduction |
4116 |
|
* @param reducer a commutative associative combining function |
4117 |
< |
* @return the result of accumulating the given transformation |
4117 |
> |
* @return the result of accumulating the given transformation |
4118 |
|
* of all entries |
4119 |
+ |
* @since 1.8 |
4120 |
|
*/ |
4121 |
< |
public long reduceEntriesToLong(ObjectToLong<Map.Entry<K,V>> transformer, |
4121 |
> |
public long reduceEntriesToLong(long parallelismThreshold, |
4122 |
> |
ObjectToLong<Map.Entry<K,V>> transformer, |
4123 |
|
long basis, |
4124 |
|
LongByLongToLong reducer) { |
4125 |
< |
return ForkJoinTasks.reduceEntriesToLong |
4126 |
< |
(this, transformer, basis, reducer).invoke(); |
4125 |
> |
if (transformer == null || reducer == null) |
4126 |
> |
throw new NullPointerException(); |
4127 |
> |
return new MapReduceEntriesToLongTask<K,V> |
4128 |
> |
(null, batchFor(parallelismThreshold), 0, 0, table, |
4129 |
> |
null, transformer, basis, reducer).invoke(); |
4130 |
|
} |
4131 |
|
|
4132 |
|
/** |
4134 |
|
* of all entries using the given reducer to combine values, |
4135 |
|
* and the given basis as an identity value. |
4136 |
|
* |
4137 |
+ |
* @param parallelismThreshold the (estimated) number of elements |
4138 |
+ |
* needed for this operation to be executed in parallel |
4139 |
|
* @param transformer a function returning the transformation |
4140 |
|
* for an element |
4141 |
|
* @param basis the identity (initial default value) for the reduction |
4142 |
|
* @param reducer a commutative associative combining function |
4143 |
|
* @return the result of accumulating the given transformation |
4144 |
|
* of all entries |
4145 |
+ |
* @since 1.8 |
4146 |
|
*/ |
4147 |
< |
public int reduceEntriesToInt(ObjectToInt<Map.Entry<K,V>> transformer, |
4147 |
> |
public int reduceEntriesToInt(long parallelismThreshold, |
4148 |
> |
ObjectToInt<Map.Entry<K,V>> transformer, |
4149 |
|
int basis, |
4150 |
|
IntByIntToInt reducer) { |
4151 |
< |
return ForkJoinTasks.reduceEntriesToInt |
4152 |
< |
(this, transformer, basis, reducer).invoke(); |
4151 |
> |
if (transformer == null || reducer == null) |
4152 |
> |
throw new NullPointerException(); |
4153 |
> |
return new MapReduceEntriesToIntTask<K,V> |
4154 |
> |
(null, batchFor(parallelismThreshold), 0, 0, table, |
4155 |
> |
null, transformer, basis, reducer).invoke(); |
4156 |
|
} |
4157 |
|
|
4158 |
+ |
|
4159 |
|
/* ----------------Views -------------- */ |
4160 |
|
|
4161 |
|
/** |
4162 |
|
* Base class for views. |
4163 |
|
*/ |
4164 |
< |
static abstract class CHMView<K, V> { |
4165 |
< |
final ConcurrentHashMapV8<K, V> map; |
4166 |
< |
CHMView(ConcurrentHashMapV8<K, V> map) { this.map = map; } |
4164 |
> |
abstract static class CollectionView<K,V,E> |
4165 |
> |
implements Collection<E>, java.io.Serializable { |
4166 |
> |
private static final long serialVersionUID = 7249069246763182397L; |
4167 |
> |
final ConcurrentHashMapV8<K,V> map; |
4168 |
> |
CollectionView(ConcurrentHashMapV8<K,V> map) { this.map = map; } |
4169 |
|
|
4170 |
|
/** |
4171 |
|
* Returns the map backing this view. |
4174 |
|
*/ |
4175 |
|
public ConcurrentHashMapV8<K,V> getMap() { return map; } |
4176 |
|
|
4177 |
< |
public final int size() { return map.size(); } |
4178 |
< |
public final boolean isEmpty() { return map.isEmpty(); } |
4179 |
< |
public final void clear() { map.clear(); } |
4177 |
> |
/** |
4178 |
> |
* Removes all of the elements from this view, by removing all |
4179 |
> |
* the mappings from the map backing this view. |
4180 |
> |
*/ |
4181 |
> |
public final void clear() { map.clear(); } |
4182 |
> |
public final int size() { return map.size(); } |
4183 |
> |
public final boolean isEmpty() { return map.isEmpty(); } |
4184 |
|
|
4185 |
|
// implementations below rely on concrete classes supplying these |
4186 |
< |
abstract public Iterator<?> iterator(); |
4187 |
< |
abstract public boolean contains(Object o); |
4188 |
< |
abstract public boolean remove(Object o); |
4186 |
> |
// abstract methods |
4187 |
> |
/** |
4188 |
> |
* Returns a "weakly consistent" iterator that will never |
4189 |
> |
* throw {@link ConcurrentModificationException}, and |
4190 |
> |
* guarantees to traverse elements as they existed upon |
4191 |
> |
* construction of the iterator, and may (but is not |
4192 |
> |
* guaranteed to) reflect any modifications subsequent to |
4193 |
> |
* construction. |
4194 |
> |
*/ |
4195 |
> |
public abstract Iterator<E> iterator(); |
4196 |
> |
public abstract boolean contains(Object o); |
4197 |
> |
public abstract boolean remove(Object o); |
4198 |
|
|
4199 |
|
private static final String oomeMsg = "Required array size too large"; |
4200 |
|
|
4201 |
|
public final Object[] toArray() { |
4202 |
|
long sz = map.mappingCount(); |
4203 |
< |
if (sz > (long)(MAX_ARRAY_SIZE)) |
4203 |
> |
if (sz > MAX_ARRAY_SIZE) |
4204 |
|
throw new OutOfMemoryError(oomeMsg); |
4205 |
|
int n = (int)sz; |
4206 |
|
Object[] r = new Object[n]; |
4207 |
|
int i = 0; |
4208 |
< |
Iterator<?> it = iterator(); |
4053 |
< |
while (it.hasNext()) { |
4208 |
> |
for (E e : this) { |
4209 |
|
if (i == n) { |
4210 |
|
if (n >= MAX_ARRAY_SIZE) |
4211 |
|
throw new OutOfMemoryError(oomeMsg); |
4215 |
|
n += (n >>> 1) + 1; |
4216 |
|
r = Arrays.copyOf(r, n); |
4217 |
|
} |
4218 |
< |
r[i++] = it.next(); |
4218 |
> |
r[i++] = e; |
4219 |
|
} |
4220 |
|
return (i == n) ? r : Arrays.copyOf(r, i); |
4221 |
|
} |
4222 |
|
|
4223 |
< |
@SuppressWarnings("unchecked") public final <T> T[] toArray(T[] a) { |
4223 |
> |
@SuppressWarnings("unchecked") |
4224 |
> |
public final <T> T[] toArray(T[] a) { |
4225 |
|
long sz = map.mappingCount(); |
4226 |
< |
if (sz > (long)(MAX_ARRAY_SIZE)) |
4226 |
> |
if (sz > MAX_ARRAY_SIZE) |
4227 |
|
throw new OutOfMemoryError(oomeMsg); |
4228 |
|
int m = (int)sz; |
4229 |
|
T[] r = (a.length >= m) ? a : |
4231 |
|
.newInstance(a.getClass().getComponentType(), m); |
4232 |
|
int n = r.length; |
4233 |
|
int i = 0; |
4234 |
< |
Iterator<?> it = iterator(); |
4079 |
< |
while (it.hasNext()) { |
4234 |
> |
for (E e : this) { |
4235 |
|
if (i == n) { |
4236 |
|
if (n >= MAX_ARRAY_SIZE) |
4237 |
|
throw new OutOfMemoryError(oomeMsg); |
4241 |
|
n += (n >>> 1) + 1; |
4242 |
|
r = Arrays.copyOf(r, n); |
4243 |
|
} |
4244 |
< |
r[i++] = (T)it.next(); |
4244 |
> |
r[i++] = (T)e; |
4245 |
|
} |
4246 |
|
if (a == r && i < n) { |
4247 |
|
r[i] = null; // null-terminate |
4250 |
|
return (i == n) ? r : Arrays.copyOf(r, i); |
4251 |
|
} |
4252 |
|
|
4253 |
< |
public final int hashCode() { |
4254 |
< |
int h = 0; |
4255 |
< |
for (Iterator<?> it = iterator(); it.hasNext();) |
4256 |
< |
h += it.next().hashCode(); |
4257 |
< |
return h; |
4258 |
< |
} |
4259 |
< |
|
4253 |
> |
/** |
4254 |
> |
* Returns a string representation of this collection. |
4255 |
> |
* The string representation consists of the string representations |
4256 |
> |
* of the collection's elements in the order they are returned by |
4257 |
> |
* its iterator, enclosed in square brackets ({@code "[]"}). |
4258 |
> |
* Adjacent elements are separated by the characters {@code ", "} |
4259 |
> |
* (comma and space). Elements are converted to strings as by |
4260 |
> |
* {@link String#valueOf(Object)}. |
4261 |
> |
* |
4262 |
> |
* @return a string representation of this collection |
4263 |
> |
*/ |
4264 |
|
public final String toString() { |
4265 |
|
StringBuilder sb = new StringBuilder(); |
4266 |
|
sb.append('['); |
4267 |
< |
Iterator<?> it = iterator(); |
4267 |
> |
Iterator<E> it = iterator(); |
4268 |
|
if (it.hasNext()) { |
4269 |
|
for (;;) { |
4270 |
|
Object e = it.next(); |
4279 |
|
|
4280 |
|
public final boolean containsAll(Collection<?> c) { |
4281 |
|
if (c != this) { |
4282 |
< |
for (Iterator<?> it = c.iterator(); it.hasNext();) { |
4124 |
< |
Object e = it.next(); |
4282 |
> |
for (Object e : c) { |
4283 |
|
if (e == null || !contains(e)) |
4284 |
|
return false; |
4285 |
|
} |
4289 |
|
|
4290 |
|
public final boolean removeAll(Collection<?> c) { |
4291 |
|
boolean modified = false; |
4292 |
< |
for (Iterator<?> it = iterator(); it.hasNext();) { |
4292 |
> |
for (Iterator<E> it = iterator(); it.hasNext();) { |
4293 |
|
if (c.contains(it.next())) { |
4294 |
|
it.remove(); |
4295 |
|
modified = true; |
4300 |
|
|
4301 |
|
public final boolean retainAll(Collection<?> c) { |
4302 |
|
boolean modified = false; |
4303 |
< |
for (Iterator<?> it = iterator(); it.hasNext();) { |
4303 |
> |
for (Iterator<E> it = iterator(); it.hasNext();) { |
4304 |
|
if (!c.contains(it.next())) { |
4305 |
|
it.remove(); |
4306 |
|
modified = true; |
4314 |
|
/** |
4315 |
|
* A view of a ConcurrentHashMapV8 as a {@link Set} of keys, in |
4316 |
|
* which additions may optionally be enabled by mapping to a |
4317 |
< |
* common value. This class cannot be directly instantiated. See |
4318 |
< |
* {@link #keySet}, {@link #keySet(Object)}, {@link #newKeySet()}, |
4319 |
< |
* {@link #newKeySet(int)}. |
4317 |
> |
* common value. This class cannot be directly instantiated. |
4318 |
> |
* See {@link #keySet() keySet()}, |
4319 |
> |
* {@link #keySet(Object) keySet(V)}, |
4320 |
> |
* {@link #newKeySet() newKeySet()}, |
4321 |
> |
* {@link #newKeySet(int) newKeySet(int)}. |
4322 |
> |
* |
4323 |
> |
* @since 1.8 |
4324 |
|
*/ |
4325 |
< |
public static class KeySetView<K,V> extends CHMView<K,V> implements Set<K>, java.io.Serializable { |
4325 |
> |
public static class KeySetView<K,V> extends CollectionView<K,V,K> |
4326 |
> |
implements Set<K>, java.io.Serializable { |
4327 |
|
private static final long serialVersionUID = 7249069246763182397L; |
4328 |
|
private final V value; |
4329 |
< |
KeySetView(ConcurrentHashMapV8<K, V> map, V value) { // non-public |
4329 |
> |
KeySetView(ConcurrentHashMapV8<K,V> map, V value) { // non-public |
4330 |
|
super(map); |
4331 |
|
this.value = value; |
4332 |
|
} |
4336 |
|
* or {@code null} if additions are not supported. |
4337 |
|
* |
4338 |
|
* @return the default mapped value for additions, or {@code null} |
4339 |
< |
* if not supported. |
4339 |
> |
* if not supported |
4340 |
|
*/ |
4341 |
|
public V getMappedValue() { return value; } |
4342 |
|
|
4343 |
< |
// implement Set API |
4344 |
< |
|
4343 |
> |
/** |
4344 |
> |
* {@inheritDoc} |
4345 |
> |
* @throws NullPointerException if the specified key is null |
4346 |
> |
*/ |
4347 |
|
public boolean contains(Object o) { return map.containsKey(o); } |
4183 |
– |
public boolean remove(Object o) { return map.remove(o) != null; } |
4348 |
|
|
4349 |
|
/** |
4350 |
< |
* Returns a "weakly consistent" iterator that will never |
4351 |
< |
* throw {@link ConcurrentModificationException}, and |
4352 |
< |
* guarantees to traverse elements as they existed upon |
4353 |
< |
* construction of the iterator, and may (but is not |
4354 |
< |
* guaranteed to) reflect any modifications subsequent to |
4355 |
< |
* construction. |
4350 |
> |
* Removes the key from this map view, by removing the key (and its |
4351 |
> |
* corresponding value) from the backing map. This method does |
4352 |
> |
* nothing if the key is not in the map. |
4353 |
> |
* |
4354 |
> |
* @param o the key to be removed from the backing map |
4355 |
> |
* @return {@code true} if the backing map contained the specified key |
4356 |
> |
* @throws NullPointerException if the specified key is null |
4357 |
> |
*/ |
4358 |
> |
public boolean remove(Object o) { return map.remove(o) != null; } |
4359 |
> |
|
4360 |
> |
/** |
4361 |
> |
* @return an iterator over the keys of the backing map |
4362 |
> |
*/ |
4363 |
> |
public Iterator<K> iterator() { |
4364 |
> |
Node<K,V>[] t; |
4365 |
> |
ConcurrentHashMapV8<K,V> m = map; |
4366 |
> |
int f = (t = m.table) == null ? 0 : t.length; |
4367 |
> |
return new KeyIterator<K,V>(t, f, 0, f, m); |
4368 |
> |
} |
4369 |
> |
|
4370 |
> |
/** |
4371 |
> |
* Adds the specified key to this set view by mapping the key to |
4372 |
> |
* the default mapped value in the backing map, if defined. |
4373 |
|
* |
4374 |
< |
* @return an iterator over the keys of this map |
4374 |
> |
* @param e key to be added |
4375 |
> |
* @return {@code true} if this set changed as a result of the call |
4376 |
> |
* @throws NullPointerException if the specified key is null |
4377 |
> |
* @throws UnsupportedOperationException if no default mapped value |
4378 |
> |
* for additions was provided |
4379 |
|
*/ |
4195 |
– |
public Iterator<K> iterator() { return new KeyIterator<K,V>(map); } |
4380 |
|
public boolean add(K e) { |
4381 |
|
V v; |
4382 |
|
if ((v = value) == null) |
4383 |
|
throw new UnsupportedOperationException(); |
4384 |
< |
if (e == null) |
4201 |
< |
throw new NullPointerException(); |
4202 |
< |
return map.internalPutIfAbsent(e, v) == null; |
4384 |
> |
return map.putVal(e, v, true) == null; |
4385 |
|
} |
4386 |
+ |
|
4387 |
+ |
/** |
4388 |
+ |
* Adds all of the elements in the specified collection to this set, |
4389 |
+ |
* as if by calling {@link #add} on each one. |
4390 |
+ |
* |
4391 |
+ |
* @param c the elements to be inserted into this set |
4392 |
+ |
* @return {@code true} if this set changed as a result of the call |
4393 |
+ |
* @throws NullPointerException if the collection or any of its |
4394 |
+ |
* elements are {@code null} |
4395 |
+ |
* @throws UnsupportedOperationException if no default mapped value |
4396 |
+ |
* for additions was provided |
4397 |
+ |
*/ |
4398 |
|
public boolean addAll(Collection<? extends K> c) { |
4399 |
|
boolean added = false; |
4400 |
|
V v; |
4401 |
|
if ((v = value) == null) |
4402 |
|
throw new UnsupportedOperationException(); |
4403 |
|
for (K e : c) { |
4404 |
< |
if (e == null) |
4211 |
< |
throw new NullPointerException(); |
4212 |
< |
if (map.internalPutIfAbsent(e, v) == null) |
4404 |
> |
if (map.putVal(e, v, true) == null) |
4405 |
|
added = true; |
4406 |
|
} |
4407 |
|
return added; |
4408 |
|
} |
4409 |
+ |
|
4410 |
+ |
public int hashCode() { |
4411 |
+ |
int h = 0; |
4412 |
+ |
for (K e : this) |
4413 |
+ |
h += e.hashCode(); |
4414 |
+ |
return h; |
4415 |
+ |
} |
4416 |
+ |
|
4417 |
|
public boolean equals(Object o) { |
4418 |
|
Set<?> c; |
4419 |
|
return ((o instanceof Set) && |
4421 |
|
(containsAll(c) && c.containsAll(this)))); |
4422 |
|
} |
4423 |
|
|
4424 |
< |
/** |
4425 |
< |
* Performs the given action for each key. |
4426 |
< |
* |
4427 |
< |
* @param action the action |
4428 |
< |
*/ |
4429 |
< |
public void forEach(Action<K> action) { |
4230 |
< |
ForkJoinTasks.forEachKey |
4231 |
< |
(map, action).invoke(); |
4232 |
< |
} |
4233 |
< |
|
4234 |
< |
/** |
4235 |
< |
* Performs the given action for each non-null transformation |
4236 |
< |
* of each key. |
4237 |
< |
* |
4238 |
< |
* @param transformer a function returning the transformation |
4239 |
< |
* for an element, or null of there is no transformation (in |
4240 |
< |
* which case the action is not applied). |
4241 |
< |
* @param action the action |
4242 |
< |
*/ |
4243 |
< |
public <U> void forEach(Fun<? super K, ? extends U> transformer, |
4244 |
< |
Action<U> action) { |
4245 |
< |
ForkJoinTasks.forEachKey |
4246 |
< |
(map, transformer, action).invoke(); |
4247 |
< |
} |
4248 |
< |
|
4249 |
< |
/** |
4250 |
< |
* Returns a non-null result from applying the given search |
4251 |
< |
* function on each key, or null if none. Upon success, |
4252 |
< |
* further element processing is suppressed and the results of |
4253 |
< |
* any other parallel invocations of the search function are |
4254 |
< |
* ignored. |
4255 |
< |
* |
4256 |
< |
* @param searchFunction a function returning a non-null |
4257 |
< |
* result on success, else null |
4258 |
< |
* @return a non-null result from applying the given search |
4259 |
< |
* function on each key, or null if none |
4260 |
< |
*/ |
4261 |
< |
public <U> U search(Fun<? super K, ? extends U> searchFunction) { |
4262 |
< |
return ForkJoinTasks.searchKeys |
4263 |
< |
(map, searchFunction).invoke(); |
4264 |
< |
} |
4265 |
< |
|
4266 |
< |
/** |
4267 |
< |
* Returns the result of accumulating all keys using the given |
4268 |
< |
* reducer to combine values, or null if none. |
4269 |
< |
* |
4270 |
< |
* @param reducer a commutative associative combining function |
4271 |
< |
* @return the result of accumulating all keys using the given |
4272 |
< |
* reducer to combine values, or null if none |
4273 |
< |
*/ |
4274 |
< |
public K reduce(BiFun<? super K, ? super K, ? extends K> reducer) { |
4275 |
< |
return ForkJoinTasks.reduceKeys |
4276 |
< |
(map, reducer).invoke(); |
4277 |
< |
} |
4278 |
< |
|
4279 |
< |
/** |
4280 |
< |
* Returns the result of accumulating the given transformation |
4281 |
< |
* of all keys using the given reducer to combine values, and |
4282 |
< |
* the given basis as an identity value. |
4283 |
< |
* |
4284 |
< |
* @param transformer a function returning the transformation |
4285 |
< |
* for an element |
4286 |
< |
* @param basis the identity (initial default value) for the reduction |
4287 |
< |
* @param reducer a commutative associative combining function |
4288 |
< |
* @return the result of accumulating the given transformation |
4289 |
< |
* of all keys |
4290 |
< |
*/ |
4291 |
< |
public double reduceToDouble(ObjectToDouble<? super K> transformer, |
4292 |
< |
double basis, |
4293 |
< |
DoubleByDoubleToDouble reducer) { |
4294 |
< |
return ForkJoinTasks.reduceKeysToDouble |
4295 |
< |
(map, transformer, basis, reducer).invoke(); |
4296 |
< |
} |
4297 |
< |
|
4298 |
< |
|
4299 |
< |
/** |
4300 |
< |
* Returns the result of accumulating the given transformation |
4301 |
< |
* of all keys using the given reducer to combine values, and |
4302 |
< |
* the given basis as an identity value. |
4303 |
< |
* |
4304 |
< |
* @param transformer a function returning the transformation |
4305 |
< |
* for an element |
4306 |
< |
* @param basis the identity (initial default value) for the reduction |
4307 |
< |
* @param reducer a commutative associative combining function |
4308 |
< |
* @return the result of accumulating the given transformation |
4309 |
< |
* of all keys |
4310 |
< |
*/ |
4311 |
< |
public long reduceToLong(ObjectToLong<? super K> transformer, |
4312 |
< |
long basis, |
4313 |
< |
LongByLongToLong reducer) { |
4314 |
< |
return ForkJoinTasks.reduceKeysToLong |
4315 |
< |
(map, transformer, basis, reducer).invoke(); |
4424 |
> |
public ConcurrentHashMapSpliterator<K> spliterator() { |
4425 |
> |
Node<K,V>[] t; |
4426 |
> |
ConcurrentHashMapV8<K,V> m = map; |
4427 |
> |
long n = m.sumCount(); |
4428 |
> |
int f = (t = m.table) == null ? 0 : t.length; |
4429 |
> |
return new KeySpliterator<K,V>(t, f, 0, f, n < 0L ? 0L : n); |
4430 |
|
} |
4431 |
|
|
4432 |
< |
/** |
4433 |
< |
* Returns the result of accumulating the given transformation |
4434 |
< |
* of all keys using the given reducer to combine values, and |
4435 |
< |
* the given basis as an identity value. |
4436 |
< |
* |
4437 |
< |
* @param transformer a function returning the transformation |
4438 |
< |
* for an element |
4439 |
< |
* @param basis the identity (initial default value) for the reduction |
4326 |
< |
* @param reducer a commutative associative combining function |
4327 |
< |
* @return the result of accumulating the given transformation |
4328 |
< |
* of all keys |
4329 |
< |
*/ |
4330 |
< |
public int reduceToInt(ObjectToInt<? super K> transformer, |
4331 |
< |
int basis, |
4332 |
< |
IntByIntToInt reducer) { |
4333 |
< |
return ForkJoinTasks.reduceKeysToInt |
4334 |
< |
(map, transformer, basis, reducer).invoke(); |
4432 |
> |
public void forEach(Action<? super K> action) { |
4433 |
> |
if (action == null) throw new NullPointerException(); |
4434 |
> |
Node<K,V>[] t; |
4435 |
> |
if ((t = map.table) != null) { |
4436 |
> |
Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length); |
4437 |
> |
for (Node<K,V> p; (p = it.advance()) != null; ) |
4438 |
> |
action.apply(p.key); |
4439 |
> |
} |
4440 |
|
} |
4336 |
– |
|
4441 |
|
} |
4442 |
|
|
4443 |
|
/** |
4444 |
|
* A view of a ConcurrentHashMapV8 as a {@link Collection} of |
4445 |
|
* values, in which additions are disabled. This class cannot be |
4446 |
< |
* directly instantiated. See {@link #values}, |
4343 |
< |
* |
4344 |
< |
* <p>The view's {@code iterator} is a "weakly consistent" iterator |
4345 |
< |
* that will never throw {@link ConcurrentModificationException}, |
4346 |
< |
* and guarantees to traverse elements as they existed upon |
4347 |
< |
* construction of the iterator, and may (but is not guaranteed to) |
4348 |
< |
* reflect any modifications subsequent to construction. |
4446 |
> |
* directly instantiated. See {@link #values()}. |
4447 |
|
*/ |
4448 |
< |
public static final class ValuesView<K,V> extends CHMView<K,V> |
4449 |
< |
implements Collection<V> { |
4450 |
< |
ValuesView(ConcurrentHashMapV8<K, V> map) { super(map); } |
4451 |
< |
public final boolean contains(Object o) { return map.containsValue(o); } |
4448 |
> |
static final class ValuesView<K,V> extends CollectionView<K,V,V> |
4449 |
> |
implements Collection<V>, java.io.Serializable { |
4450 |
> |
private static final long serialVersionUID = 2249069246763182397L; |
4451 |
> |
ValuesView(ConcurrentHashMapV8<K,V> map) { super(map); } |
4452 |
> |
public final boolean contains(Object o) { |
4453 |
> |
return map.containsValue(o); |
4454 |
> |
} |
4455 |
> |
|
4456 |
|
public final boolean remove(Object o) { |
4457 |
|
if (o != null) { |
4458 |
< |
Iterator<V> it = new ValueIterator<K,V>(map); |
4357 |
< |
while (it.hasNext()) { |
4458 |
> |
for (Iterator<V> it = iterator(); it.hasNext();) { |
4459 |
|
if (o.equals(it.next())) { |
4460 |
|
it.remove(); |
4461 |
|
return true; |
4465 |
|
return false; |
4466 |
|
} |
4467 |
|
|
4367 |
– |
/** |
4368 |
– |
* Returns a "weakly consistent" iterator that will never |
4369 |
– |
* throw {@link ConcurrentModificationException}, and |
4370 |
– |
* guarantees to traverse elements as they existed upon |
4371 |
– |
* construction of the iterator, and may (but is not |
4372 |
– |
* guaranteed to) reflect any modifications subsequent to |
4373 |
– |
* construction. |
4374 |
– |
* |
4375 |
– |
* @return an iterator over the values of this map |
4376 |
– |
*/ |
4468 |
|
public final Iterator<V> iterator() { |
4469 |
< |
return new ValueIterator<K,V>(map); |
4469 |
> |
ConcurrentHashMapV8<K,V> m = map; |
4470 |
> |
Node<K,V>[] t; |
4471 |
> |
int f = (t = m.table) == null ? 0 : t.length; |
4472 |
> |
return new ValueIterator<K,V>(t, f, 0, f, m); |
4473 |
|
} |
4474 |
+ |
|
4475 |
|
public final boolean add(V e) { |
4476 |
|
throw new UnsupportedOperationException(); |
4477 |
|
} |
4479 |
|
throw new UnsupportedOperationException(); |
4480 |
|
} |
4481 |
|
|
4482 |
< |
/** |
4483 |
< |
* Performs the given action for each value. |
4484 |
< |
* |
4485 |
< |
* @param action the action |
4486 |
< |
*/ |
4487 |
< |
public void forEach(Action<V> action) { |
4393 |
< |
ForkJoinTasks.forEachValue |
4394 |
< |
(map, action).invoke(); |
4395 |
< |
} |
4396 |
< |
|
4397 |
< |
/** |
4398 |
< |
* Performs the given action for each non-null transformation |
4399 |
< |
* of each value. |
4400 |
< |
* |
4401 |
< |
* @param transformer a function returning the transformation |
4402 |
< |
* for an element, or null of there is no transformation (in |
4403 |
< |
* which case the action is not applied). |
4404 |
< |
*/ |
4405 |
< |
public <U> void forEach(Fun<? super V, ? extends U> transformer, |
4406 |
< |
Action<U> action) { |
4407 |
< |
ForkJoinTasks.forEachValue |
4408 |
< |
(map, transformer, action).invoke(); |
4482 |
> |
public ConcurrentHashMapSpliterator<V> spliterator() { |
4483 |
> |
Node<K,V>[] t; |
4484 |
> |
ConcurrentHashMapV8<K,V> m = map; |
4485 |
> |
long n = m.sumCount(); |
4486 |
> |
int f = (t = m.table) == null ? 0 : t.length; |
4487 |
> |
return new ValueSpliterator<K,V>(t, f, 0, f, n < 0L ? 0L : n); |
4488 |
|
} |
4489 |
|
|
4490 |
< |
/** |
4491 |
< |
* Returns a non-null result from applying the given search |
4492 |
< |
* function on each value, or null if none. Upon success, |
4493 |
< |
* further element processing is suppressed and the results of |
4494 |
< |
* any other parallel invocations of the search function are |
4495 |
< |
* ignored. |
4496 |
< |
* |
4497 |
< |
* @param searchFunction a function returning a non-null |
4419 |
< |
* result on success, else null |
4420 |
< |
* @return a non-null result from applying the given search |
4421 |
< |
* function on each value, or null if none |
4422 |
< |
* |
4423 |
< |
*/ |
4424 |
< |
public <U> U search(Fun<? super V, ? extends U> searchFunction) { |
4425 |
< |
return ForkJoinTasks.searchValues |
4426 |
< |
(map, searchFunction).invoke(); |
4427 |
< |
} |
4428 |
< |
|
4429 |
< |
/** |
4430 |
< |
* Returns the result of accumulating all values using the |
4431 |
< |
* given reducer to combine values, or null if none. |
4432 |
< |
* |
4433 |
< |
* @param reducer a commutative associative combining function |
4434 |
< |
* @return the result of accumulating all values |
4435 |
< |
*/ |
4436 |
< |
public V reduce(BiFun<? super V, ? super V, ? extends V> reducer) { |
4437 |
< |
return ForkJoinTasks.reduceValues |
4438 |
< |
(map, reducer).invoke(); |
4439 |
< |
} |
4440 |
< |
|
4441 |
< |
/** |
4442 |
< |
* Returns the result of accumulating the given transformation |
4443 |
< |
* of all values using the given reducer to combine values, or |
4444 |
< |
* null if none. |
4445 |
< |
* |
4446 |
< |
* @param transformer a function returning the transformation |
4447 |
< |
* for an element, or null of there is no transformation (in |
4448 |
< |
* which case it is not combined). |
4449 |
< |
* @param reducer a commutative associative combining function |
4450 |
< |
* @return the result of accumulating the given transformation |
4451 |
< |
* of all values |
4452 |
< |
*/ |
4453 |
< |
public <U> U reduce(Fun<? super V, ? extends U> transformer, |
4454 |
< |
BiFun<? super U, ? super U, ? extends U> reducer) { |
4455 |
< |
return ForkJoinTasks.reduceValues |
4456 |
< |
(map, transformer, reducer).invoke(); |
4457 |
< |
} |
4458 |
< |
|
4459 |
< |
/** |
4460 |
< |
* Returns the result of accumulating the given transformation |
4461 |
< |
* of all values using the given reducer to combine values, |
4462 |
< |
* and the given basis as an identity value. |
4463 |
< |
* |
4464 |
< |
* @param transformer a function returning the transformation |
4465 |
< |
* for an element |
4466 |
< |
* @param basis the identity (initial default value) for the reduction |
4467 |
< |
* @param reducer a commutative associative combining function |
4468 |
< |
* @return the result of accumulating the given transformation |
4469 |
< |
* of all values |
4470 |
< |
*/ |
4471 |
< |
public double reduceToDouble(ObjectToDouble<? super V> transformer, |
4472 |
< |
double basis, |
4473 |
< |
DoubleByDoubleToDouble reducer) { |
4474 |
< |
return ForkJoinTasks.reduceValuesToDouble |
4475 |
< |
(map, transformer, basis, reducer).invoke(); |
4476 |
< |
} |
4477 |
< |
|
4478 |
< |
/** |
4479 |
< |
* Returns the result of accumulating the given transformation |
4480 |
< |
* of all values using the given reducer to combine values, |
4481 |
< |
* and the given basis as an identity value. |
4482 |
< |
* |
4483 |
< |
* @param transformer a function returning the transformation |
4484 |
< |
* for an element |
4485 |
< |
* @param basis the identity (initial default value) for the reduction |
4486 |
< |
* @param reducer a commutative associative combining function |
4487 |
< |
* @return the result of accumulating the given transformation |
4488 |
< |
* of all values |
4489 |
< |
*/ |
4490 |
< |
public long reduceToLong(ObjectToLong<? super V> transformer, |
4491 |
< |
long basis, |
4492 |
< |
LongByLongToLong reducer) { |
4493 |
< |
return ForkJoinTasks.reduceValuesToLong |
4494 |
< |
(map, transformer, basis, reducer).invoke(); |
4495 |
< |
} |
4496 |
< |
|
4497 |
< |
/** |
4498 |
< |
* Returns the result of accumulating the given transformation |
4499 |
< |
* of all values using the given reducer to combine values, |
4500 |
< |
* and the given basis as an identity value. |
4501 |
< |
* |
4502 |
< |
* @param transformer a function returning the transformation |
4503 |
< |
* for an element |
4504 |
< |
* @param basis the identity (initial default value) for the reduction |
4505 |
< |
* @param reducer a commutative associative combining function |
4506 |
< |
* @return the result of accumulating the given transformation |
4507 |
< |
* of all values |
4508 |
< |
*/ |
4509 |
< |
public int reduceToInt(ObjectToInt<? super V> transformer, |
4510 |
< |
int basis, |
4511 |
< |
IntByIntToInt reducer) { |
4512 |
< |
return ForkJoinTasks.reduceValuesToInt |
4513 |
< |
(map, transformer, basis, reducer).invoke(); |
4490 |
> |
public void forEach(Action<? super V> action) { |
4491 |
> |
if (action == null) throw new NullPointerException(); |
4492 |
> |
Node<K,V>[] t; |
4493 |
> |
if ((t = map.table) != null) { |
4494 |
> |
Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length); |
4495 |
> |
for (Node<K,V> p; (p = it.advance()) != null; ) |
4496 |
> |
action.apply(p.val); |
4497 |
> |
} |
4498 |
|
} |
4515 |
– |
|
4499 |
|
} |
4500 |
|
|
4501 |
|
/** |
4502 |
|
* A view of a ConcurrentHashMapV8 as a {@link Set} of (key, value) |
4503 |
|
* entries. This class cannot be directly instantiated. See |
4504 |
< |
* {@link #entrySet}. |
4504 |
> |
* {@link #entrySet()}. |
4505 |
|
*/ |
4506 |
< |
public static final class EntrySetView<K,V> extends CHMView<K,V> |
4507 |
< |
implements Set<Map.Entry<K,V>> { |
4508 |
< |
EntrySetView(ConcurrentHashMapV8<K, V> map) { super(map); } |
4509 |
< |
public final boolean contains(Object o) { |
4506 |
> |
static final class EntrySetView<K,V> extends CollectionView<K,V,Map.Entry<K,V>> |
4507 |
> |
implements Set<Map.Entry<K,V>>, java.io.Serializable { |
4508 |
> |
private static final long serialVersionUID = 2249069246763182397L; |
4509 |
> |
EntrySetView(ConcurrentHashMapV8<K,V> map) { super(map); } |
4510 |
> |
|
4511 |
> |
public boolean contains(Object o) { |
4512 |
|
Object k, v, r; Map.Entry<?,?> e; |
4513 |
|
return ((o instanceof Map.Entry) && |
4514 |
|
(k = (e = (Map.Entry<?,?>)o).getKey()) != null && |
4516 |
|
(v = e.getValue()) != null && |
4517 |
|
(v == r || v.equals(r))); |
4518 |
|
} |
4519 |
< |
public final boolean remove(Object o) { |
4519 |
> |
|
4520 |
> |
public boolean remove(Object o) { |
4521 |
|
Object k, v; Map.Entry<?,?> e; |
4522 |
|
return ((o instanceof Map.Entry) && |
4523 |
|
(k = (e = (Map.Entry<?,?>)o).getKey()) != null && |
4526 |
|
} |
4527 |
|
|
4528 |
|
/** |
4529 |
< |
* Returns a "weakly consistent" iterator that will never |
4544 |
< |
* throw {@link ConcurrentModificationException}, and |
4545 |
< |
* guarantees to traverse elements as they existed upon |
4546 |
< |
* construction of the iterator, and may (but is not |
4547 |
< |
* guaranteed to) reflect any modifications subsequent to |
4548 |
< |
* construction. |
4549 |
< |
* |
4550 |
< |
* @return an iterator over the entries of this map |
4529 |
> |
* @return an iterator over the entries of the backing map |
4530 |
|
*/ |
4531 |
< |
public final Iterator<Map.Entry<K,V>> iterator() { |
4532 |
< |
return new EntryIterator<K,V>(map); |
4531 |
> |
public Iterator<Map.Entry<K,V>> iterator() { |
4532 |
> |
ConcurrentHashMapV8<K,V> m = map; |
4533 |
> |
Node<K,V>[] t; |
4534 |
> |
int f = (t = m.table) == null ? 0 : t.length; |
4535 |
> |
return new EntryIterator<K,V>(t, f, 0, f, m); |
4536 |
|
} |
4537 |
|
|
4538 |
< |
public final boolean add(Entry<K,V> e) { |
4539 |
< |
K key = e.getKey(); |
4558 |
< |
V value = e.getValue(); |
4559 |
< |
if (key == null || value == null) |
4560 |
< |
throw new NullPointerException(); |
4561 |
< |
return map.internalPut(key, value) == null; |
4538 |
> |
public boolean add(Entry<K,V> e) { |
4539 |
> |
return map.putVal(e.getKey(), e.getValue(), false) == null; |
4540 |
|
} |
4541 |
< |
public final boolean addAll(Collection<? extends Entry<K,V>> c) { |
4541 |
> |
|
4542 |
> |
public boolean addAll(Collection<? extends Entry<K,V>> c) { |
4543 |
|
boolean added = false; |
4544 |
|
for (Entry<K,V> e : c) { |
4545 |
|
if (add(e)) |
4547 |
|
} |
4548 |
|
return added; |
4549 |
|
} |
4550 |
< |
public boolean equals(Object o) { |
4550 |
> |
|
4551 |
> |
public final int hashCode() { |
4552 |
> |
int h = 0; |
4553 |
> |
Node<K,V>[] t; |
4554 |
> |
if ((t = map.table) != null) { |
4555 |
> |
Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length); |
4556 |
> |
for (Node<K,V> p; (p = it.advance()) != null; ) { |
4557 |
> |
h += p.hashCode(); |
4558 |
> |
} |
4559 |
> |
} |
4560 |
> |
return h; |
4561 |
> |
} |
4562 |
> |
|
4563 |
> |
public final boolean equals(Object o) { |
4564 |
|
Set<?> c; |
4565 |
|
return ((o instanceof Set) && |
4566 |
|
((c = (Set<?>)o) == this || |
4567 |
|
(containsAll(c) && c.containsAll(this)))); |
4568 |
|
} |
4569 |
|
|
4570 |
< |
/** |
4571 |
< |
* Performs the given action for each entry. |
4572 |
< |
* |
4573 |
< |
* @param action the action |
4574 |
< |
*/ |
4575 |
< |
public void forEach(Action<Map.Entry<K,V>> action) { |
4584 |
< |
ForkJoinTasks.forEachEntry |
4585 |
< |
(map, action).invoke(); |
4586 |
< |
} |
4587 |
< |
|
4588 |
< |
/** |
4589 |
< |
* Performs the given action for each non-null transformation |
4590 |
< |
* of each entry. |
4591 |
< |
* |
4592 |
< |
* @param transformer a function returning the transformation |
4593 |
< |
* for an element, or null of there is no transformation (in |
4594 |
< |
* which case the action is not applied). |
4595 |
< |
* @param action the action |
4596 |
< |
*/ |
4597 |
< |
public <U> void forEach(Fun<Map.Entry<K,V>, ? extends U> transformer, |
4598 |
< |
Action<U> action) { |
4599 |
< |
ForkJoinTasks.forEachEntry |
4600 |
< |
(map, transformer, action).invoke(); |
4601 |
< |
} |
4602 |
< |
|
4603 |
< |
/** |
4604 |
< |
* Returns a non-null result from applying the given search |
4605 |
< |
* function on each entry, or null if none. Upon success, |
4606 |
< |
* further element processing is suppressed and the results of |
4607 |
< |
* any other parallel invocations of the search function are |
4608 |
< |
* ignored. |
4609 |
< |
* |
4610 |
< |
* @param searchFunction a function returning a non-null |
4611 |
< |
* result on success, else null |
4612 |
< |
* @return a non-null result from applying the given search |
4613 |
< |
* function on each entry, or null if none |
4614 |
< |
*/ |
4615 |
< |
public <U> U search(Fun<Map.Entry<K,V>, ? extends U> searchFunction) { |
4616 |
< |
return ForkJoinTasks.searchEntries |
4617 |
< |
(map, searchFunction).invoke(); |
4618 |
< |
} |
4619 |
< |
|
4620 |
< |
/** |
4621 |
< |
* Returns the result of accumulating all entries using the |
4622 |
< |
* given reducer to combine values, or null if none. |
4623 |
< |
* |
4624 |
< |
* @param reducer a commutative associative combining function |
4625 |
< |
* @return the result of accumulating all entries |
4626 |
< |
*/ |
4627 |
< |
public Map.Entry<K,V> reduce(BiFun<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer) { |
4628 |
< |
return ForkJoinTasks.reduceEntries |
4629 |
< |
(map, reducer).invoke(); |
4630 |
< |
} |
4631 |
< |
|
4632 |
< |
/** |
4633 |
< |
* Returns the result of accumulating the given transformation |
4634 |
< |
* of all entries using the given reducer to combine values, |
4635 |
< |
* or null if none. |
4636 |
< |
* |
4637 |
< |
* @param transformer a function returning the transformation |
4638 |
< |
* for an element, or null of there is no transformation (in |
4639 |
< |
* which case it is not combined). |
4640 |
< |
* @param reducer a commutative associative combining function |
4641 |
< |
* @return the result of accumulating the given transformation |
4642 |
< |
* of all entries |
4643 |
< |
*/ |
4644 |
< |
public <U> U reduce(Fun<Map.Entry<K,V>, ? extends U> transformer, |
4645 |
< |
BiFun<? super U, ? super U, ? extends U> reducer) { |
4646 |
< |
return ForkJoinTasks.reduceEntries |
4647 |
< |
(map, transformer, reducer).invoke(); |
4570 |
> |
public ConcurrentHashMapSpliterator<Map.Entry<K,V>> spliterator() { |
4571 |
> |
Node<K,V>[] t; |
4572 |
> |
ConcurrentHashMapV8<K,V> m = map; |
4573 |
> |
long n = m.sumCount(); |
4574 |
> |
int f = (t = m.table) == null ? 0 : t.length; |
4575 |
> |
return new EntrySpliterator<K,V>(t, f, 0, f, n < 0L ? 0L : n, m); |
4576 |
|
} |
4577 |
|
|
4578 |
< |
/** |
4579 |
< |
* Returns the result of accumulating the given transformation |
4580 |
< |
* of all entries using the given reducer to combine values, |
4581 |
< |
* and the given basis as an identity value. |
4582 |
< |
* |
4583 |
< |
* @param transformer a function returning the transformation |
4584 |
< |
* for an element |
4585 |
< |
* @param basis the identity (initial default value) for the reduction |
4658 |
< |
* @param reducer a commutative associative combining function |
4659 |
< |
* @return the result of accumulating the given transformation |
4660 |
< |
* of all entries |
4661 |
< |
*/ |
4662 |
< |
public double reduceToDouble(ObjectToDouble<Map.Entry<K,V>> transformer, |
4663 |
< |
double basis, |
4664 |
< |
DoubleByDoubleToDouble reducer) { |
4665 |
< |
return ForkJoinTasks.reduceEntriesToDouble |
4666 |
< |
(map, transformer, basis, reducer).invoke(); |
4667 |
< |
} |
4668 |
< |
|
4669 |
< |
/** |
4670 |
< |
* Returns the result of accumulating the given transformation |
4671 |
< |
* of all entries using the given reducer to combine values, |
4672 |
< |
* and the given basis as an identity value. |
4673 |
< |
* |
4674 |
< |
* @param transformer a function returning the transformation |
4675 |
< |
* for an element |
4676 |
< |
* @param basis the identity (initial default value) for the reduction |
4677 |
< |
* @param reducer a commutative associative combining function |
4678 |
< |
* @return the result of accumulating the given transformation |
4679 |
< |
* of all entries |
4680 |
< |
*/ |
4681 |
< |
public long reduceToLong(ObjectToLong<Map.Entry<K,V>> transformer, |
4682 |
< |
long basis, |
4683 |
< |
LongByLongToLong reducer) { |
4684 |
< |
return ForkJoinTasks.reduceEntriesToLong |
4685 |
< |
(map, transformer, basis, reducer).invoke(); |
4686 |
< |
} |
4687 |
< |
|
4688 |
< |
/** |
4689 |
< |
* Returns the result of accumulating the given transformation |
4690 |
< |
* of all entries using the given reducer to combine values, |
4691 |
< |
* and the given basis as an identity value. |
4692 |
< |
* |
4693 |
< |
* @param transformer a function returning the transformation |
4694 |
< |
* for an element |
4695 |
< |
* @param basis the identity (initial default value) for the reduction |
4696 |
< |
* @param reducer a commutative associative combining function |
4697 |
< |
* @return the result of accumulating the given transformation |
4698 |
< |
* of all entries |
4699 |
< |
*/ |
4700 |
< |
public int reduceToInt(ObjectToInt<Map.Entry<K,V>> transformer, |
4701 |
< |
int basis, |
4702 |
< |
IntByIntToInt reducer) { |
4703 |
< |
return ForkJoinTasks.reduceEntriesToInt |
4704 |
< |
(map, transformer, basis, reducer).invoke(); |
4578 |
> |
public void forEach(Action<? super Map.Entry<K,V>> action) { |
4579 |
> |
if (action == null) throw new NullPointerException(); |
4580 |
> |
Node<K,V>[] t; |
4581 |
> |
if ((t = map.table) != null) { |
4582 |
> |
Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length); |
4583 |
> |
for (Node<K,V> p; (p = it.advance()) != null; ) |
4584 |
> |
action.apply(new MapEntry<K,V>(p.key, p.val, map)); |
4585 |
> |
} |
4586 |
|
} |
4587 |
|
|
4588 |
|
} |
4589 |
|
|
4590 |
< |
// --------------------------------------------------------------------- |
4590 |
> |
// ------------------------------------------------------- |
4591 |
|
|
4592 |
|
/** |
4593 |
< |
* Predefined tasks for performing bulk parallel operations on |
4594 |
< |
* ConcurrentHashMapV8s. These tasks follow the forms and rules used |
4714 |
< |
* for bulk operations. Each method has the same name, but returns |
4715 |
< |
* a task rather than invoking it. These methods may be useful in |
4716 |
< |
* custom applications such as submitting a task without waiting |
4717 |
< |
* for completion, using a custom pool, or combining with other |
4718 |
< |
* tasks. |
4593 |
> |
* Base class for bulk tasks. Repeats some fields and code from |
4594 |
> |
* class Traverser, because we need to subclass CountedCompleter. |
4595 |
|
*/ |
4596 |
< |
public static class ForkJoinTasks { |
4597 |
< |
private ForkJoinTasks() {} |
4598 |
< |
|
4599 |
< |
/** |
4600 |
< |
* Returns a task that when invoked, performs the given |
4601 |
< |
* action for each (key, value) |
4602 |
< |
* |
4603 |
< |
* @param map the map |
4604 |
< |
* @param action the action |
4605 |
< |
* @return the task |
4606 |
< |
*/ |
4607 |
< |
public static <K,V> ForkJoinTask<Void> forEach |
4608 |
< |
(ConcurrentHashMapV8<K,V> map, |
4609 |
< |
BiAction<K,V> action) { |
4610 |
< |
if (action == null) throw new NullPointerException(); |
4611 |
< |
return new ForEachMappingTask<K,V>(map, null, -1, action); |
4612 |
< |
} |
4613 |
< |
|
4614 |
< |
/** |
4615 |
< |
* Returns a task that when invoked, performs the given |
4616 |
< |
* action for each non-null transformation of each (key, value) |
4741 |
< |
* |
4742 |
< |
* @param map the map |
4743 |
< |
* @param transformer a function returning the transformation |
4744 |
< |
* for an element, or null if there is no transformation (in |
4745 |
< |
* which case the action is not applied) |
4746 |
< |
* @param action the action |
4747 |
< |
* @return the task |
4748 |
< |
*/ |
4749 |
< |
public static <K,V,U> ForkJoinTask<Void> forEach |
4750 |
< |
(ConcurrentHashMapV8<K,V> map, |
4751 |
< |
BiFun<? super K, ? super V, ? extends U> transformer, |
4752 |
< |
Action<U> action) { |
4753 |
< |
if (transformer == null || action == null) |
4754 |
< |
throw new NullPointerException(); |
4755 |
< |
return new ForEachTransformedMappingTask<K,V,U> |
4756 |
< |
(map, null, -1, transformer, action); |
4757 |
< |
} |
4758 |
< |
|
4759 |
< |
/** |
4760 |
< |
* Returns a task that when invoked, returns a non-null result |
4761 |
< |
* from applying the given search function on each (key, |
4762 |
< |
* value), or null if none. Upon success, further element |
4763 |
< |
* processing is suppressed and the results of any other |
4764 |
< |
* parallel invocations of the search function are ignored. |
4765 |
< |
* |
4766 |
< |
* @param map the map |
4767 |
< |
* @param searchFunction a function returning a non-null |
4768 |
< |
* result on success, else null |
4769 |
< |
* @return the task |
4770 |
< |
*/ |
4771 |
< |
public static <K,V,U> ForkJoinTask<U> search |
4772 |
< |
(ConcurrentHashMapV8<K,V> map, |
4773 |
< |
BiFun<? super K, ? super V, ? extends U> searchFunction) { |
4774 |
< |
if (searchFunction == null) throw new NullPointerException(); |
4775 |
< |
return new SearchMappingsTask<K,V,U> |
4776 |
< |
(map, null, -1, searchFunction, |
4777 |
< |
new AtomicReference<U>()); |
4778 |
< |
} |
4779 |
< |
|
4780 |
< |
/** |
4781 |
< |
* Returns a task that when invoked, returns the result of |
4782 |
< |
* accumulating the given transformation of all (key, value) pairs |
4783 |
< |
* using the given reducer to combine values, or null if none. |
4784 |
< |
* |
4785 |
< |
* @param map the map |
4786 |
< |
* @param transformer a function returning the transformation |
4787 |
< |
* for an element, or null if there is no transformation (in |
4788 |
< |
* which case it is not combined). |
4789 |
< |
* @param reducer a commutative associative combining function |
4790 |
< |
* @return the task |
4791 |
< |
*/ |
4792 |
< |
public static <K,V,U> ForkJoinTask<U> reduce |
4793 |
< |
(ConcurrentHashMapV8<K,V> map, |
4794 |
< |
BiFun<? super K, ? super V, ? extends U> transformer, |
4795 |
< |
BiFun<? super U, ? super U, ? extends U> reducer) { |
4796 |
< |
if (transformer == null || reducer == null) |
4797 |
< |
throw new NullPointerException(); |
4798 |
< |
return new MapReduceMappingsTask<K,V,U> |
4799 |
< |
(map, null, -1, null, transformer, reducer); |
4800 |
< |
} |
4801 |
< |
|
4802 |
< |
/** |
4803 |
< |
* Returns a task that when invoked, returns the result of |
4804 |
< |
* accumulating the given transformation of all (key, value) pairs |
4805 |
< |
* using the given reducer to combine values, and the given |
4806 |
< |
* basis as an identity value. |
4807 |
< |
* |
4808 |
< |
* @param map the map |
4809 |
< |
* @param transformer a function returning the transformation |
4810 |
< |
* for an element |
4811 |
< |
* @param basis the identity (initial default value) for the reduction |
4812 |
< |
* @param reducer a commutative associative combining function |
4813 |
< |
* @return the task |
4814 |
< |
*/ |
4815 |
< |
public static <K,V> ForkJoinTask<Double> reduceToDouble |
4816 |
< |
(ConcurrentHashMapV8<K,V> map, |
4817 |
< |
ObjectByObjectToDouble<? super K, ? super V> transformer, |
4818 |
< |
double basis, |
4819 |
< |
DoubleByDoubleToDouble reducer) { |
4820 |
< |
if (transformer == null || reducer == null) |
4821 |
< |
throw new NullPointerException(); |
4822 |
< |
return new MapReduceMappingsToDoubleTask<K,V> |
4823 |
< |
(map, null, -1, null, transformer, basis, reducer); |
4824 |
< |
} |
4825 |
< |
|
4826 |
< |
/** |
4827 |
< |
* Returns a task that when invoked, returns the result of |
4828 |
< |
* accumulating the given transformation of all (key, value) pairs |
4829 |
< |
* using the given reducer to combine values, and the given |
4830 |
< |
* basis as an identity value. |
4831 |
< |
* |
4832 |
< |
* @param map the map |
4833 |
< |
* @param transformer a function returning the transformation |
4834 |
< |
* for an element |
4835 |
< |
* @param basis the identity (initial default value) for the reduction |
4836 |
< |
* @param reducer a commutative associative combining function |
4837 |
< |
* @return the task |
4838 |
< |
*/ |
4839 |
< |
public static <K,V> ForkJoinTask<Long> reduceToLong |
4840 |
< |
(ConcurrentHashMapV8<K,V> map, |
4841 |
< |
ObjectByObjectToLong<? super K, ? super V> transformer, |
4842 |
< |
long basis, |
4843 |
< |
LongByLongToLong reducer) { |
4844 |
< |
if (transformer == null || reducer == null) |
4845 |
< |
throw new NullPointerException(); |
4846 |
< |
return new MapReduceMappingsToLongTask<K,V> |
4847 |
< |
(map, null, -1, null, transformer, basis, reducer); |
4848 |
< |
} |
4849 |
< |
|
4850 |
< |
/** |
4851 |
< |
* Returns a task that when invoked, returns the result of |
4852 |
< |
* accumulating the given transformation of all (key, value) pairs |
4853 |
< |
* using the given reducer to combine values, and the given |
4854 |
< |
* basis as an identity value. |
4855 |
< |
* |
4856 |
< |
* @param transformer a function returning the transformation |
4857 |
< |
* for an element |
4858 |
< |
* @param basis the identity (initial default value) for the reduction |
4859 |
< |
* @param reducer a commutative associative combining function |
4860 |
< |
* @return the task |
4861 |
< |
*/ |
4862 |
< |
public static <K,V> ForkJoinTask<Integer> reduceToInt |
4863 |
< |
(ConcurrentHashMapV8<K,V> map, |
4864 |
< |
ObjectByObjectToInt<? super K, ? super V> transformer, |
4865 |
< |
int basis, |
4866 |
< |
IntByIntToInt reducer) { |
4867 |
< |
if (transformer == null || reducer == null) |
4868 |
< |
throw new NullPointerException(); |
4869 |
< |
return new MapReduceMappingsToIntTask<K,V> |
4870 |
< |
(map, null, -1, null, transformer, basis, reducer); |
4871 |
< |
} |
4872 |
< |
|
4873 |
< |
/** |
4874 |
< |
* Returns a task that when invoked, performs the given action |
4875 |
< |
* for each key. |
4876 |
< |
* |
4877 |
< |
* @param map the map |
4878 |
< |
* @param action the action |
4879 |
< |
* @return the task |
4880 |
< |
*/ |
4881 |
< |
public static <K,V> ForkJoinTask<Void> forEachKey |
4882 |
< |
(ConcurrentHashMapV8<K,V> map, |
4883 |
< |
Action<K> action) { |
4884 |
< |
if (action == null) throw new NullPointerException(); |
4885 |
< |
return new ForEachKeyTask<K,V>(map, null, -1, action); |
4886 |
< |
} |
4887 |
< |
|
4888 |
< |
/** |
4889 |
< |
* Returns a task that when invoked, performs the given action |
4890 |
< |
* for each non-null transformation of each key. |
4891 |
< |
* |
4892 |
< |
* @param map the map |
4893 |
< |
* @param transformer a function returning the transformation |
4894 |
< |
* for an element, or null if there is no transformation (in |
4895 |
< |
* which case the action is not applied) |
4896 |
< |
* @param action the action |
4897 |
< |
* @return the task |
4898 |
< |
*/ |
4899 |
< |
public static <K,V,U> ForkJoinTask<Void> forEachKey |
4900 |
< |
(ConcurrentHashMapV8<K,V> map, |
4901 |
< |
Fun<? super K, ? extends U> transformer, |
4902 |
< |
Action<U> action) { |
4903 |
< |
if (transformer == null || action == null) |
4904 |
< |
throw new NullPointerException(); |
4905 |
< |
return new ForEachTransformedKeyTask<K,V,U> |
4906 |
< |
(map, null, -1, transformer, action); |
4907 |
< |
} |
4908 |
< |
|
4909 |
< |
/** |
4910 |
< |
* Returns a task that when invoked, returns a non-null result |
4911 |
< |
* from applying the given search function on each key, or |
4912 |
< |
* null if none. Upon success, further element processing is |
4913 |
< |
* suppressed and the results of any other parallel |
4914 |
< |
* invocations of the search function are ignored. |
4915 |
< |
* |
4916 |
< |
* @param map the map |
4917 |
< |
* @param searchFunction a function returning a non-null |
4918 |
< |
* result on success, else null |
4919 |
< |
* @return the task |
4920 |
< |
*/ |
4921 |
< |
public static <K,V,U> ForkJoinTask<U> searchKeys |
4922 |
< |
(ConcurrentHashMapV8<K,V> map, |
4923 |
< |
Fun<? super K, ? extends U> searchFunction) { |
4924 |
< |
if (searchFunction == null) throw new NullPointerException(); |
4925 |
< |
return new SearchKeysTask<K,V,U> |
4926 |
< |
(map, null, -1, searchFunction, |
4927 |
< |
new AtomicReference<U>()); |
4928 |
< |
} |
4929 |
< |
|
4930 |
< |
/** |
4931 |
< |
* Returns a task that when invoked, returns the result of |
4932 |
< |
* accumulating all keys using the given reducer to combine |
4933 |
< |
* values, or null if none. |
4934 |
< |
* |
4935 |
< |
* @param map the map |
4936 |
< |
* @param reducer a commutative associative combining function |
4937 |
< |
* @return the task |
4938 |
< |
*/ |
4939 |
< |
public static <K,V> ForkJoinTask<K> reduceKeys |
4940 |
< |
(ConcurrentHashMapV8<K,V> map, |
4941 |
< |
BiFun<? super K, ? super K, ? extends K> reducer) { |
4942 |
< |
if (reducer == null) throw new NullPointerException(); |
4943 |
< |
return new ReduceKeysTask<K,V> |
4944 |
< |
(map, null, -1, null, reducer); |
4945 |
< |
} |
4946 |
< |
|
4947 |
< |
/** |
4948 |
< |
* Returns a task that when invoked, returns the result of |
4949 |
< |
* accumulating the given transformation of all keys using the given |
4950 |
< |
* reducer to combine values, or null if none. |
4951 |
< |
* |
4952 |
< |
* @param map the map |
4953 |
< |
* @param transformer a function returning the transformation |
4954 |
< |
* for an element, or null if there is no transformation (in |
4955 |
< |
* which case it is not combined). |
4956 |
< |
* @param reducer a commutative associative combining function |
4957 |
< |
* @return the task |
4958 |
< |
*/ |
4959 |
< |
public static <K,V,U> ForkJoinTask<U> reduceKeys |
4960 |
< |
(ConcurrentHashMapV8<K,V> map, |
4961 |
< |
Fun<? super K, ? extends U> transformer, |
4962 |
< |
BiFun<? super U, ? super U, ? extends U> reducer) { |
4963 |
< |
if (transformer == null || reducer == null) |
4964 |
< |
throw new NullPointerException(); |
4965 |
< |
return new MapReduceKeysTask<K,V,U> |
4966 |
< |
(map, null, -1, null, transformer, reducer); |
4967 |
< |
} |
4968 |
< |
|
4969 |
< |
/** |
4970 |
< |
* Returns a task that when invoked, returns the result of |
4971 |
< |
* accumulating the given transformation of all keys using the given |
4972 |
< |
* reducer to combine values, and the given basis as an |
4973 |
< |
* identity value. |
4974 |
< |
* |
4975 |
< |
* @param map the map |
4976 |
< |
* @param transformer a function returning the transformation |
4977 |
< |
* for an element |
4978 |
< |
* @param basis the identity (initial default value) for the reduction |
4979 |
< |
* @param reducer a commutative associative combining function |
4980 |
< |
* @return the task |
4981 |
< |
*/ |
4982 |
< |
public static <K,V> ForkJoinTask<Double> reduceKeysToDouble |
4983 |
< |
(ConcurrentHashMapV8<K,V> map, |
4984 |
< |
ObjectToDouble<? super K> transformer, |
4985 |
< |
double basis, |
4986 |
< |
DoubleByDoubleToDouble reducer) { |
4987 |
< |
if (transformer == null || reducer == null) |
4988 |
< |
throw new NullPointerException(); |
4989 |
< |
return new MapReduceKeysToDoubleTask<K,V> |
4990 |
< |
(map, null, -1, null, transformer, basis, reducer); |
4991 |
< |
} |
4992 |
< |
|
4993 |
< |
/** |
4994 |
< |
* Returns a task that when invoked, returns the result of |
4995 |
< |
* accumulating the given transformation of all keys using the given |
4996 |
< |
* reducer to combine values, and the given basis as an |
4997 |
< |
* identity value. |
4998 |
< |
* |
4999 |
< |
* @param map the map |
5000 |
< |
* @param transformer a function returning the transformation |
5001 |
< |
* for an element |
5002 |
< |
* @param basis the identity (initial default value) for the reduction |
5003 |
< |
* @param reducer a commutative associative combining function |
5004 |
< |
* @return the task |
5005 |
< |
*/ |
5006 |
< |
public static <K,V> ForkJoinTask<Long> reduceKeysToLong |
5007 |
< |
(ConcurrentHashMapV8<K,V> map, |
5008 |
< |
ObjectToLong<? super K> transformer, |
5009 |
< |
long basis, |
5010 |
< |
LongByLongToLong reducer) { |
5011 |
< |
if (transformer == null || reducer == null) |
5012 |
< |
throw new NullPointerException(); |
5013 |
< |
return new MapReduceKeysToLongTask<K,V> |
5014 |
< |
(map, null, -1, null, transformer, basis, reducer); |
5015 |
< |
} |
5016 |
< |
|
5017 |
< |
/** |
5018 |
< |
* Returns a task that when invoked, returns the result of |
5019 |
< |
* accumulating the given transformation of all keys using the given |
5020 |
< |
* reducer to combine values, and the given basis as an |
5021 |
< |
* identity value. |
5022 |
< |
* |
5023 |
< |
* @param map the map |
5024 |
< |
* @param transformer a function returning the transformation |
5025 |
< |
* for an element |
5026 |
< |
* @param basis the identity (initial default value) for the reduction |
5027 |
< |
* @param reducer a commutative associative combining function |
5028 |
< |
* @return the task |
5029 |
< |
*/ |
5030 |
< |
public static <K,V> ForkJoinTask<Integer> reduceKeysToInt |
5031 |
< |
(ConcurrentHashMapV8<K,V> map, |
5032 |
< |
ObjectToInt<? super K> transformer, |
5033 |
< |
int basis, |
5034 |
< |
IntByIntToInt reducer) { |
5035 |
< |
if (transformer == null || reducer == null) |
5036 |
< |
throw new NullPointerException(); |
5037 |
< |
return new MapReduceKeysToIntTask<K,V> |
5038 |
< |
(map, null, -1, null, transformer, basis, reducer); |
5039 |
< |
} |
5040 |
< |
|
5041 |
< |
/** |
5042 |
< |
* Returns a task that when invoked, performs the given action |
5043 |
< |
* for each value. |
5044 |
< |
* |
5045 |
< |
* @param map the map |
5046 |
< |
* @param action the action |
5047 |
< |
*/ |
5048 |
< |
public static <K,V> ForkJoinTask<Void> forEachValue |
5049 |
< |
(ConcurrentHashMapV8<K,V> map, |
5050 |
< |
Action<V> action) { |
5051 |
< |
if (action == null) throw new NullPointerException(); |
5052 |
< |
return new ForEachValueTask<K,V>(map, null, -1, action); |
5053 |
< |
} |
5054 |
< |
|
5055 |
< |
/** |
5056 |
< |
* Returns a task that when invoked, performs the given action |
5057 |
< |
* for each non-null transformation of each value. |
5058 |
< |
* |
5059 |
< |
* @param map the map |
5060 |
< |
* @param transformer a function returning the transformation |
5061 |
< |
* for an element, or null if there is no transformation (in |
5062 |
< |
* which case the action is not applied) |
5063 |
< |
* @param action the action |
5064 |
< |
*/ |
5065 |
< |
public static <K,V,U> ForkJoinTask<Void> forEachValue |
5066 |
< |
(ConcurrentHashMapV8<K,V> map, |
5067 |
< |
Fun<? super V, ? extends U> transformer, |
5068 |
< |
Action<U> action) { |
5069 |
< |
if (transformer == null || action == null) |
5070 |
< |
throw new NullPointerException(); |
5071 |
< |
return new ForEachTransformedValueTask<K,V,U> |
5072 |
< |
(map, null, -1, transformer, action); |
5073 |
< |
} |
5074 |
< |
|
5075 |
< |
/** |
5076 |
< |
* Returns a task that when invoked, returns a non-null result |
5077 |
< |
* from applying the given search function on each value, or |
5078 |
< |
* null if none. Upon success, further element processing is |
5079 |
< |
* suppressed and the results of any other parallel |
5080 |
< |
* invocations of the search function are ignored. |
5081 |
< |
* |
5082 |
< |
* @param map the map |
5083 |
< |
* @param searchFunction a function returning a non-null |
5084 |
< |
* result on success, else null |
5085 |
< |
* @return the task |
5086 |
< |
*/ |
5087 |
< |
public static <K,V,U> ForkJoinTask<U> searchValues |
5088 |
< |
(ConcurrentHashMapV8<K,V> map, |
5089 |
< |
Fun<? super V, ? extends U> searchFunction) { |
5090 |
< |
if (searchFunction == null) throw new NullPointerException(); |
5091 |
< |
return new SearchValuesTask<K,V,U> |
5092 |
< |
(map, null, -1, searchFunction, |
5093 |
< |
new AtomicReference<U>()); |
5094 |
< |
} |
5095 |
< |
|
5096 |
< |
/** |
5097 |
< |
* Returns a task that when invoked, returns the result of |
5098 |
< |
* accumulating all values using the given reducer to combine |
5099 |
< |
* values, or null if none. |
5100 |
< |
* |
5101 |
< |
* @param map the map |
5102 |
< |
* @param reducer a commutative associative combining function |
5103 |
< |
* @return the task |
5104 |
< |
*/ |
5105 |
< |
public static <K,V> ForkJoinTask<V> reduceValues |
5106 |
< |
(ConcurrentHashMapV8<K,V> map, |
5107 |
< |
BiFun<? super V, ? super V, ? extends V> reducer) { |
5108 |
< |
if (reducer == null) throw new NullPointerException(); |
5109 |
< |
return new ReduceValuesTask<K,V> |
5110 |
< |
(map, null, -1, null, reducer); |
5111 |
< |
} |
5112 |
< |
|
5113 |
< |
/** |
5114 |
< |
* Returns a task that when invoked, returns the result of |
5115 |
< |
* accumulating the given transformation of all values using the |
5116 |
< |
* given reducer to combine values, or null if none. |
5117 |
< |
* |
5118 |
< |
* @param map the map |
5119 |
< |
* @param transformer a function returning the transformation |
5120 |
< |
* for an element, or null if there is no transformation (in |
5121 |
< |
* which case it is not combined). |
5122 |
< |
* @param reducer a commutative associative combining function |
5123 |
< |
* @return the task |
5124 |
< |
*/ |
5125 |
< |
public static <K,V,U> ForkJoinTask<U> reduceValues |
5126 |
< |
(ConcurrentHashMapV8<K,V> map, |
5127 |
< |
Fun<? super V, ? extends U> transformer, |
5128 |
< |
BiFun<? super U, ? super U, ? extends U> reducer) { |
5129 |
< |
if (transformer == null || reducer == null) |
5130 |
< |
throw new NullPointerException(); |
5131 |
< |
return new MapReduceValuesTask<K,V,U> |
5132 |
< |
(map, null, -1, null, transformer, reducer); |
5133 |
< |
} |
5134 |
< |
|
5135 |
< |
/** |
5136 |
< |
* Returns a task that when invoked, returns the result of |
5137 |
< |
* accumulating the given transformation of all values using the |
5138 |
< |
* given reducer to combine values, and the given basis as an |
5139 |
< |
* identity value. |
5140 |
< |
* |
5141 |
< |
* @param map the map |
5142 |
< |
* @param transformer a function returning the transformation |
5143 |
< |
* for an element |
5144 |
< |
* @param basis the identity (initial default value) for the reduction |
5145 |
< |
* @param reducer a commutative associative combining function |
5146 |
< |
* @return the task |
5147 |
< |
*/ |
5148 |
< |
public static <K,V> ForkJoinTask<Double> reduceValuesToDouble |
5149 |
< |
(ConcurrentHashMapV8<K,V> map, |
5150 |
< |
ObjectToDouble<? super V> transformer, |
5151 |
< |
double basis, |
5152 |
< |
DoubleByDoubleToDouble reducer) { |
5153 |
< |
if (transformer == null || reducer == null) |
5154 |
< |
throw new NullPointerException(); |
5155 |
< |
return new MapReduceValuesToDoubleTask<K,V> |
5156 |
< |
(map, null, -1, null, transformer, basis, reducer); |
5157 |
< |
} |
5158 |
< |
|
5159 |
< |
/** |
5160 |
< |
* Returns a task that when invoked, returns the result of |
5161 |
< |
* accumulating the given transformation of all values using the |
5162 |
< |
* given reducer to combine values, and the given basis as an |
5163 |
< |
* identity value. |
5164 |
< |
* |
5165 |
< |
* @param map the map |
5166 |
< |
* @param transformer a function returning the transformation |
5167 |
< |
* for an element |
5168 |
< |
* @param basis the identity (initial default value) for the reduction |
5169 |
< |
* @param reducer a commutative associative combining function |
5170 |
< |
* @return the task |
5171 |
< |
*/ |
5172 |
< |
public static <K,V> ForkJoinTask<Long> reduceValuesToLong |
5173 |
< |
(ConcurrentHashMapV8<K,V> map, |
5174 |
< |
ObjectToLong<? super V> transformer, |
5175 |
< |
long basis, |
5176 |
< |
LongByLongToLong reducer) { |
5177 |
< |
if (transformer == null || reducer == null) |
5178 |
< |
throw new NullPointerException(); |
5179 |
< |
return new MapReduceValuesToLongTask<K,V> |
5180 |
< |
(map, null, -1, null, transformer, basis, reducer); |
5181 |
< |
} |
5182 |
< |
|
5183 |
< |
/** |
5184 |
< |
* Returns a task that when invoked, returns the result of |
5185 |
< |
* accumulating the given transformation of all values using the |
5186 |
< |
* given reducer to combine values, and the given basis as an |
5187 |
< |
* identity value. |
5188 |
< |
* |
5189 |
< |
* @param map the map |
5190 |
< |
* @param transformer a function returning the transformation |
5191 |
< |
* for an element |
5192 |
< |
* @param basis the identity (initial default value) for the reduction |
5193 |
< |
* @param reducer a commutative associative combining function |
5194 |
< |
* @return the task |
5195 |
< |
*/ |
5196 |
< |
public static <K,V> ForkJoinTask<Integer> reduceValuesToInt |
5197 |
< |
(ConcurrentHashMapV8<K,V> map, |
5198 |
< |
ObjectToInt<? super V> transformer, |
5199 |
< |
int basis, |
5200 |
< |
IntByIntToInt reducer) { |
5201 |
< |
if (transformer == null || reducer == null) |
5202 |
< |
throw new NullPointerException(); |
5203 |
< |
return new MapReduceValuesToIntTask<K,V> |
5204 |
< |
(map, null, -1, null, transformer, basis, reducer); |
5205 |
< |
} |
5206 |
< |
|
5207 |
< |
/** |
5208 |
< |
* Returns a task that when invoked, perform the given action |
5209 |
< |
* for each entry. |
5210 |
< |
* |
5211 |
< |
* @param map the map |
5212 |
< |
* @param action the action |
5213 |
< |
*/ |
5214 |
< |
public static <K,V> ForkJoinTask<Void> forEachEntry |
5215 |
< |
(ConcurrentHashMapV8<K,V> map, |
5216 |
< |
Action<Map.Entry<K,V>> action) { |
5217 |
< |
if (action == null) throw new NullPointerException(); |
5218 |
< |
return new ForEachEntryTask<K,V>(map, null, -1, action); |
5219 |
< |
} |
5220 |
< |
|
5221 |
< |
/** |
5222 |
< |
* Returns a task that when invoked, perform the given action |
5223 |
< |
* for each non-null transformation of each entry. |
5224 |
< |
* |
5225 |
< |
* @param map the map |
5226 |
< |
* @param transformer a function returning the transformation |
5227 |
< |
* for an element, or null if there is no transformation (in |
5228 |
< |
* which case the action is not applied) |
5229 |
< |
* @param action the action |
5230 |
< |
*/ |
5231 |
< |
public static <K,V,U> ForkJoinTask<Void> forEachEntry |
5232 |
< |
(ConcurrentHashMapV8<K,V> map, |
5233 |
< |
Fun<Map.Entry<K,V>, ? extends U> transformer, |
5234 |
< |
Action<U> action) { |
5235 |
< |
if (transformer == null || action == null) |
5236 |
< |
throw new NullPointerException(); |
5237 |
< |
return new ForEachTransformedEntryTask<K,V,U> |
5238 |
< |
(map, null, -1, transformer, action); |
5239 |
< |
} |
5240 |
< |
|
5241 |
< |
/** |
5242 |
< |
* Returns a task that when invoked, returns a non-null result |
5243 |
< |
* from applying the given search function on each entry, or |
5244 |
< |
* null if none. Upon success, further element processing is |
5245 |
< |
* suppressed and the results of any other parallel |
5246 |
< |
* invocations of the search function are ignored. |
5247 |
< |
* |
5248 |
< |
* @param map the map |
5249 |
< |
* @param searchFunction a function returning a non-null |
5250 |
< |
* result on success, else null |
5251 |
< |
* @return the task |
5252 |
< |
*/ |
5253 |
< |
public static <K,V,U> ForkJoinTask<U> searchEntries |
5254 |
< |
(ConcurrentHashMapV8<K,V> map, |
5255 |
< |
Fun<Map.Entry<K,V>, ? extends U> searchFunction) { |
5256 |
< |
if (searchFunction == null) throw new NullPointerException(); |
5257 |
< |
return new SearchEntriesTask<K,V,U> |
5258 |
< |
(map, null, -1, searchFunction, |
5259 |
< |
new AtomicReference<U>()); |
5260 |
< |
} |
5261 |
< |
|
5262 |
< |
/** |
5263 |
< |
* Returns a task that when invoked, returns the result of |
5264 |
< |
* accumulating all entries using the given reducer to combine |
5265 |
< |
* values, or null if none. |
5266 |
< |
* |
5267 |
< |
* @param map the map |
5268 |
< |
* @param reducer a commutative associative combining function |
5269 |
< |
* @return the task |
5270 |
< |
*/ |
5271 |
< |
public static <K,V> ForkJoinTask<Map.Entry<K,V>> reduceEntries |
5272 |
< |
(ConcurrentHashMapV8<K,V> map, |
5273 |
< |
BiFun<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer) { |
5274 |
< |
if (reducer == null) throw new NullPointerException(); |
5275 |
< |
return new ReduceEntriesTask<K,V> |
5276 |
< |
(map, null, -1, null, reducer); |
5277 |
< |
} |
5278 |
< |
|
5279 |
< |
/** |
5280 |
< |
* Returns a task that when invoked, returns the result of |
5281 |
< |
* accumulating the given transformation of all entries using the |
5282 |
< |
* given reducer to combine values, or null if none. |
5283 |
< |
* |
5284 |
< |
* @param map the map |
5285 |
< |
* @param transformer a function returning the transformation |
5286 |
< |
* for an element, or null if there is no transformation (in |
5287 |
< |
* which case it is not combined). |
5288 |
< |
* @param reducer a commutative associative combining function |
5289 |
< |
* @return the task |
5290 |
< |
*/ |
5291 |
< |
public static <K,V,U> ForkJoinTask<U> reduceEntries |
5292 |
< |
(ConcurrentHashMapV8<K,V> map, |
5293 |
< |
Fun<Map.Entry<K,V>, ? extends U> transformer, |
5294 |
< |
BiFun<? super U, ? super U, ? extends U> reducer) { |
5295 |
< |
if (transformer == null || reducer == null) |
5296 |
< |
throw new NullPointerException(); |
5297 |
< |
return new MapReduceEntriesTask<K,V,U> |
5298 |
< |
(map, null, -1, null, transformer, reducer); |
5299 |
< |
} |
5300 |
< |
|
5301 |
< |
/** |
5302 |
< |
* Returns a task that when invoked, returns the result of |
5303 |
< |
* accumulating the given transformation of all entries using the |
5304 |
< |
* given reducer to combine values, and the given basis as an |
5305 |
< |
* identity value. |
5306 |
< |
* |
5307 |
< |
* @param map the map |
5308 |
< |
* @param transformer a function returning the transformation |
5309 |
< |
* for an element |
5310 |
< |
* @param basis the identity (initial default value) for the reduction |
5311 |
< |
* @param reducer a commutative associative combining function |
5312 |
< |
* @return the task |
5313 |
< |
*/ |
5314 |
< |
public static <K,V> ForkJoinTask<Double> reduceEntriesToDouble |
5315 |
< |
(ConcurrentHashMapV8<K,V> map, |
5316 |
< |
ObjectToDouble<Map.Entry<K,V>> transformer, |
5317 |
< |
double basis, |
5318 |
< |
DoubleByDoubleToDouble reducer) { |
5319 |
< |
if (transformer == null || reducer == null) |
5320 |
< |
throw new NullPointerException(); |
5321 |
< |
return new MapReduceEntriesToDoubleTask<K,V> |
5322 |
< |
(map, null, -1, null, transformer, basis, reducer); |
5323 |
< |
} |
5324 |
< |
|
5325 |
< |
/** |
5326 |
< |
* Returns a task that when invoked, returns the result of |
5327 |
< |
* accumulating the given transformation of all entries using the |
5328 |
< |
* given reducer to combine values, and the given basis as an |
5329 |
< |
* identity value. |
5330 |
< |
* |
5331 |
< |
* @param map the map |
5332 |
< |
* @param transformer a function returning the transformation |
5333 |
< |
* for an element |
5334 |
< |
* @param basis the identity (initial default value) for the reduction |
5335 |
< |
* @param reducer a commutative associative combining function |
5336 |
< |
* @return the task |
5337 |
< |
*/ |
5338 |
< |
public static <K,V> ForkJoinTask<Long> reduceEntriesToLong |
5339 |
< |
(ConcurrentHashMapV8<K,V> map, |
5340 |
< |
ObjectToLong<Map.Entry<K,V>> transformer, |
5341 |
< |
long basis, |
5342 |
< |
LongByLongToLong reducer) { |
5343 |
< |
if (transformer == null || reducer == null) |
5344 |
< |
throw new NullPointerException(); |
5345 |
< |
return new MapReduceEntriesToLongTask<K,V> |
5346 |
< |
(map, null, -1, null, transformer, basis, reducer); |
4596 |
> |
abstract static class BulkTask<K,V,R> extends CountedCompleter<R> { |
4597 |
> |
Node<K,V>[] tab; // same as Traverser |
4598 |
> |
Node<K,V> next; |
4599 |
> |
int index; |
4600 |
> |
int baseIndex; |
4601 |
> |
int baseLimit; |
4602 |
> |
final int baseSize; |
4603 |
> |
int batch; // split control |
4604 |
> |
|
4605 |
> |
BulkTask(BulkTask<K,V,?> par, int b, int i, int f, Node<K,V>[] t) { |
4606 |
> |
super(par); |
4607 |
> |
this.batch = b; |
4608 |
> |
this.index = this.baseIndex = i; |
4609 |
> |
if ((this.tab = t) == null) |
4610 |
> |
this.baseSize = this.baseLimit = 0; |
4611 |
> |
else if (par == null) |
4612 |
> |
this.baseSize = this.baseLimit = t.length; |
4613 |
> |
else { |
4614 |
> |
this.baseLimit = f; |
4615 |
> |
this.baseSize = par.baseSize; |
4616 |
> |
} |
4617 |
|
} |
4618 |
|
|
4619 |
|
/** |
4620 |
< |
* Returns a task that when invoked, returns the result of |
5351 |
< |
* accumulating the given transformation of all entries using the |
5352 |
< |
* given reducer to combine values, and the given basis as an |
5353 |
< |
* identity value. |
5354 |
< |
* |
5355 |
< |
* @param map the map |
5356 |
< |
* @param transformer a function returning the transformation |
5357 |
< |
* for an element |
5358 |
< |
* @param basis the identity (initial default value) for the reduction |
5359 |
< |
* @param reducer a commutative associative combining function |
5360 |
< |
* @return the task |
4620 |
> |
* Same as Traverser version |
4621 |
|
*/ |
4622 |
< |
public static <K,V> ForkJoinTask<Integer> reduceEntriesToInt |
4623 |
< |
(ConcurrentHashMapV8<K,V> map, |
4624 |
< |
ObjectToInt<Map.Entry<K,V>> transformer, |
4625 |
< |
int basis, |
4626 |
< |
IntByIntToInt reducer) { |
4627 |
< |
if (transformer == null || reducer == null) |
4628 |
< |
throw new NullPointerException(); |
4629 |
< |
return new MapReduceEntriesToIntTask<K,V> |
4630 |
< |
(map, null, -1, null, transformer, basis, reducer); |
4622 |
> |
final Node<K,V> advance() { |
4623 |
> |
Node<K,V> e; |
4624 |
> |
if ((e = next) != null) |
4625 |
> |
e = e.next; |
4626 |
> |
for (;;) { |
4627 |
> |
Node<K,V>[] t; int i, n; K ek; // must use locals in checks |
4628 |
> |
if (e != null) |
4629 |
> |
return next = e; |
4630 |
> |
if (baseIndex >= baseLimit || (t = tab) == null || |
4631 |
> |
(n = t.length) <= (i = index) || i < 0) |
4632 |
> |
return next = null; |
4633 |
> |
if ((e = tabAt(t, index)) != null && e.hash < 0) { |
4634 |
> |
if (e instanceof ForwardingNode) { |
4635 |
> |
tab = ((ForwardingNode<K,V>)e).nextTable; |
4636 |
> |
e = null; |
4637 |
> |
continue; |
4638 |
> |
} |
4639 |
> |
else if (e instanceof TreeBin) |
4640 |
> |
e = ((TreeBin<K,V>)e).first; |
4641 |
> |
else |
4642 |
> |
e = null; |
4643 |
> |
} |
4644 |
> |
if ((index += baseSize) >= n) |
4645 |
> |
index = ++baseIndex; // visit upper slots if present |
4646 |
> |
} |
4647 |
|
} |
4648 |
|
} |
4649 |
|
|
5374 |
– |
// ------------------------------------------------------- |
5375 |
– |
|
4650 |
|
/* |
4651 |
|
* Task classes. Coded in a regular but ugly format/style to |
4652 |
|
* simplify checks that each variant differs in the right way from |
4653 |
< |
* others. |
4654 |
< |
*/ |
4655 |
< |
|
4656 |
< |
@SuppressWarnings("serial") static final class ForEachKeyTask<K,V> |
4657 |
< |
extends Traverser<K,V,Void> { |
4658 |
< |
final Action<K> action; |
4653 |
> |
* others. The null screenings exist because compilers cannot tell |
4654 |
> |
* that we've already null-checked task arguments, so we force |
4655 |
> |
* simplest hoisted bypass to help avoid convoluted traps. |
4656 |
> |
*/ |
4657 |
> |
@SuppressWarnings("serial") |
4658 |
> |
static final class ForEachKeyTask<K,V> |
4659 |
> |
extends BulkTask<K,V,Void> { |
4660 |
> |
final Action<? super K> action; |
4661 |
|
ForEachKeyTask |
4662 |
< |
(ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b, |
4663 |
< |
Action<K> action) { |
4664 |
< |
super(m, p, b); |
4662 |
> |
(BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t, |
4663 |
> |
Action<? super K> action) { |
4664 |
> |
super(p, b, i, f, t); |
4665 |
|
this.action = action; |
4666 |
|
} |
4667 |
< |
@SuppressWarnings("unchecked") public final void compute() { |
4668 |
< |
final Action<K> action; |
4669 |
< |
if ((action = this.action) == null) |
4670 |
< |
throw new NullPointerException(); |
4671 |
< |
for (int b; (b = preSplit()) > 0;) |
4672 |
< |
new ForEachKeyTask<K,V>(map, this, b, action).fork(); |
4673 |
< |
while (advance() != null) |
4674 |
< |
action.apply((K)nextKey); |
4675 |
< |
propagateCompletion(); |
4667 |
> |
public final void compute() { |
4668 |
> |
final Action<? super K> action; |
4669 |
> |
if ((action = this.action) != null) { |
4670 |
> |
for (int i = baseIndex, f, h; batch > 0 && |
4671 |
> |
(h = ((f = baseLimit) + i) >>> 1) > i;) { |
4672 |
> |
addToPendingCount(1); |
4673 |
> |
new ForEachKeyTask<K,V> |
4674 |
> |
(this, batch >>>= 1, baseLimit = h, f, tab, |
4675 |
> |
action).fork(); |
4676 |
> |
} |
4677 |
> |
for (Node<K,V> p; (p = advance()) != null;) |
4678 |
> |
action.apply(p.key); |
4679 |
> |
propagateCompletion(); |
4680 |
> |
} |
4681 |
|
} |
4682 |
|
} |
4683 |
|
|
4684 |
< |
@SuppressWarnings("serial") static final class ForEachValueTask<K,V> |
4685 |
< |
extends Traverser<K,V,Void> { |
4686 |
< |
final Action<V> action; |
4684 |
> |
@SuppressWarnings("serial") |
4685 |
> |
static final class ForEachValueTask<K,V> |
4686 |
> |
extends BulkTask<K,V,Void> { |
4687 |
> |
final Action<? super V> action; |
4688 |
|
ForEachValueTask |
4689 |
< |
(ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b, |
4690 |
< |
Action<V> action) { |
4691 |
< |
super(m, p, b); |
4689 |
> |
(BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t, |
4690 |
> |
Action<? super V> action) { |
4691 |
> |
super(p, b, i, f, t); |
4692 |
|
this.action = action; |
4693 |
|
} |
4694 |
< |
@SuppressWarnings("unchecked") public final void compute() { |
4695 |
< |
final Action<V> action; |
4696 |
< |
if ((action = this.action) == null) |
4697 |
< |
throw new NullPointerException(); |
4698 |
< |
for (int b; (b = preSplit()) > 0;) |
4699 |
< |
new ForEachValueTask<K,V>(map, this, b, action).fork(); |
4700 |
< |
Object v; |
4701 |
< |
while ((v = advance()) != null) |
4702 |
< |
action.apply((V)v); |
4703 |
< |
propagateCompletion(); |
4694 |
> |
public final void compute() { |
4695 |
> |
final Action<? super V> action; |
4696 |
> |
if ((action = this.action) != null) { |
4697 |
> |
for (int i = baseIndex, f, h; batch > 0 && |
4698 |
> |
(h = ((f = baseLimit) + i) >>> 1) > i;) { |
4699 |
> |
addToPendingCount(1); |
4700 |
> |
new ForEachValueTask<K,V> |
4701 |
> |
(this, batch >>>= 1, baseLimit = h, f, tab, |
4702 |
> |
action).fork(); |
4703 |
> |
} |
4704 |
> |
for (Node<K,V> p; (p = advance()) != null;) |
4705 |
> |
action.apply(p.val); |
4706 |
> |
propagateCompletion(); |
4707 |
> |
} |
4708 |
|
} |
4709 |
|
} |
4710 |
|
|
4711 |
< |
@SuppressWarnings("serial") static final class ForEachEntryTask<K,V> |
4712 |
< |
extends Traverser<K,V,Void> { |
4713 |
< |
final Action<Entry<K,V>> action; |
4711 |
> |
@SuppressWarnings("serial") |
4712 |
> |
static final class ForEachEntryTask<K,V> |
4713 |
> |
extends BulkTask<K,V,Void> { |
4714 |
> |
final Action<? super Entry<K,V>> action; |
4715 |
|
ForEachEntryTask |
4716 |
< |
(ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b, |
4717 |
< |
Action<Entry<K,V>> action) { |
4718 |
< |
super(m, p, b); |
4716 |
> |
(BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t, |
4717 |
> |
Action<? super Entry<K,V>> action) { |
4718 |
> |
super(p, b, i, f, t); |
4719 |
|
this.action = action; |
4720 |
|
} |
4721 |
< |
@SuppressWarnings("unchecked") public final void compute() { |
4722 |
< |
final Action<Entry<K,V>> action; |
4723 |
< |
if ((action = this.action) == null) |
4724 |
< |
throw new NullPointerException(); |
4725 |
< |
for (int b; (b = preSplit()) > 0;) |
4726 |
< |
new ForEachEntryTask<K,V>(map, this, b, action).fork(); |
4727 |
< |
Object v; |
4728 |
< |
while ((v = advance()) != null) |
4729 |
< |
action.apply(entryFor((K)nextKey, (V)v)); |
4730 |
< |
propagateCompletion(); |
4721 |
> |
public final void compute() { |
4722 |
> |
final Action<? super Entry<K,V>> action; |
4723 |
> |
if ((action = this.action) != null) { |
4724 |
> |
for (int i = baseIndex, f, h; batch > 0 && |
4725 |
> |
(h = ((f = baseLimit) + i) >>> 1) > i;) { |
4726 |
> |
addToPendingCount(1); |
4727 |
> |
new ForEachEntryTask<K,V> |
4728 |
> |
(this, batch >>>= 1, baseLimit = h, f, tab, |
4729 |
> |
action).fork(); |
4730 |
> |
} |
4731 |
> |
for (Node<K,V> p; (p = advance()) != null; ) |
4732 |
> |
action.apply(p); |
4733 |
> |
propagateCompletion(); |
4734 |
> |
} |
4735 |
|
} |
4736 |
|
} |
4737 |
|
|
4738 |
< |
@SuppressWarnings("serial") static final class ForEachMappingTask<K,V> |
4739 |
< |
extends Traverser<K,V,Void> { |
4740 |
< |
final BiAction<K,V> action; |
4738 |
> |
@SuppressWarnings("serial") |
4739 |
> |
static final class ForEachMappingTask<K,V> |
4740 |
> |
extends BulkTask<K,V,Void> { |
4741 |
> |
final BiAction<? super K, ? super V> action; |
4742 |
|
ForEachMappingTask |
4743 |
< |
(ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b, |
4744 |
< |
BiAction<K,V> action) { |
4745 |
< |
super(m, p, b); |
4743 |
> |
(BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t, |
4744 |
> |
BiAction<? super K,? super V> action) { |
4745 |
> |
super(p, b, i, f, t); |
4746 |
|
this.action = action; |
4747 |
|
} |
4748 |
< |
@SuppressWarnings("unchecked") public final void compute() { |
4749 |
< |
final BiAction<K,V> action; |
4750 |
< |
if ((action = this.action) == null) |
4751 |
< |
throw new NullPointerException(); |
4752 |
< |
for (int b; (b = preSplit()) > 0;) |
4753 |
< |
new ForEachMappingTask<K,V>(map, this, b, action).fork(); |
4754 |
< |
Object v; |
4755 |
< |
while ((v = advance()) != null) |
4756 |
< |
action.apply((K)nextKey, (V)v); |
4757 |
< |
propagateCompletion(); |
4748 |
> |
public final void compute() { |
4749 |
> |
final BiAction<? super K, ? super V> action; |
4750 |
> |
if ((action = this.action) != null) { |
4751 |
> |
for (int i = baseIndex, f, h; batch > 0 && |
4752 |
> |
(h = ((f = baseLimit) + i) >>> 1) > i;) { |
4753 |
> |
addToPendingCount(1); |
4754 |
> |
new ForEachMappingTask<K,V> |
4755 |
> |
(this, batch >>>= 1, baseLimit = h, f, tab, |
4756 |
> |
action).fork(); |
4757 |
> |
} |
4758 |
> |
for (Node<K,V> p; (p = advance()) != null; ) |
4759 |
> |
action.apply(p.key, p.val); |
4760 |
> |
propagateCompletion(); |
4761 |
> |
} |
4762 |
|
} |
4763 |
|
} |
4764 |
|
|
4765 |
< |
@SuppressWarnings("serial") static final class ForEachTransformedKeyTask<K,V,U> |
4766 |
< |
extends Traverser<K,V,Void> { |
4765 |
> |
@SuppressWarnings("serial") |
4766 |
> |
static final class ForEachTransformedKeyTask<K,V,U> |
4767 |
> |
extends BulkTask<K,V,Void> { |
4768 |
|
final Fun<? super K, ? extends U> transformer; |
4769 |
< |
final Action<U> action; |
4769 |
> |
final Action<? super U> action; |
4770 |
|
ForEachTransformedKeyTask |
4771 |
< |
(ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b, |
4772 |
< |
Fun<? super K, ? extends U> transformer, Action<U> action) { |
4773 |
< |
super(m, p, b); |
4771 |
> |
(BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t, |
4772 |
> |
Fun<? super K, ? extends U> transformer, Action<? super U> action) { |
4773 |
> |
super(p, b, i, f, t); |
4774 |
|
this.transformer = transformer; this.action = action; |
4775 |
|
} |
4776 |
< |
@SuppressWarnings("unchecked") public final void compute() { |
4776 |
> |
public final void compute() { |
4777 |
|
final Fun<? super K, ? extends U> transformer; |
4778 |
< |
final Action<U> action; |
4779 |
< |
if ((transformer = this.transformer) == null || |
4780 |
< |
(action = this.action) == null) |
4781 |
< |
throw new NullPointerException(); |
4782 |
< |
for (int b; (b = preSplit()) > 0;) |
4783 |
< |
new ForEachTransformedKeyTask<K,V,U> |
4784 |
< |
(map, this, b, transformer, action).fork(); |
4785 |
< |
U u; |
4786 |
< |
while (advance() != null) { |
4787 |
< |
if ((u = transformer.apply((K)nextKey)) != null) |
4788 |
< |
action.apply(u); |
4778 |
> |
final Action<? super U> action; |
4779 |
> |
if ((transformer = this.transformer) != null && |
4780 |
> |
(action = this.action) != null) { |
4781 |
> |
for (int i = baseIndex, f, h; batch > 0 && |
4782 |
> |
(h = ((f = baseLimit) + i) >>> 1) > i;) { |
4783 |
> |
addToPendingCount(1); |
4784 |
> |
new ForEachTransformedKeyTask<K,V,U> |
4785 |
> |
(this, batch >>>= 1, baseLimit = h, f, tab, |
4786 |
> |
transformer, action).fork(); |
4787 |
> |
} |
4788 |
> |
for (Node<K,V> p; (p = advance()) != null; ) { |
4789 |
> |
U u; |
4790 |
> |
if ((u = transformer.apply(p.key)) != null) |
4791 |
> |
action.apply(u); |
4792 |
> |
} |
4793 |
> |
propagateCompletion(); |
4794 |
|
} |
5493 |
– |
propagateCompletion(); |
4795 |
|
} |
4796 |
|
} |
4797 |
|
|
4798 |
< |
@SuppressWarnings("serial") static final class ForEachTransformedValueTask<K,V,U> |
4799 |
< |
extends Traverser<K,V,Void> { |
4798 |
> |
@SuppressWarnings("serial") |
4799 |
> |
static final class ForEachTransformedValueTask<K,V,U> |
4800 |
> |
extends BulkTask<K,V,Void> { |
4801 |
|
final Fun<? super V, ? extends U> transformer; |
4802 |
< |
final Action<U> action; |
4802 |
> |
final Action<? super U> action; |
4803 |
|
ForEachTransformedValueTask |
4804 |
< |
(ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b, |
4805 |
< |
Fun<? super V, ? extends U> transformer, Action<U> action) { |
4806 |
< |
super(m, p, b); |
4804 |
> |
(BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t, |
4805 |
> |
Fun<? super V, ? extends U> transformer, Action<? super U> action) { |
4806 |
> |
super(p, b, i, f, t); |
4807 |
|
this.transformer = transformer; this.action = action; |
4808 |
|
} |
4809 |
< |
@SuppressWarnings("unchecked") public final void compute() { |
4809 |
> |
public final void compute() { |
4810 |
|
final Fun<? super V, ? extends U> transformer; |
4811 |
< |
final Action<U> action; |
4812 |
< |
if ((transformer = this.transformer) == null || |
4813 |
< |
(action = this.action) == null) |
4814 |
< |
throw new NullPointerException(); |
4815 |
< |
for (int b; (b = preSplit()) > 0;) |
4816 |
< |
new ForEachTransformedValueTask<K,V,U> |
4817 |
< |
(map, this, b, transformer, action).fork(); |
4818 |
< |
Object v; U u; |
4819 |
< |
while ((v = advance()) != null) { |
4820 |
< |
if ((u = transformer.apply((V)v)) != null) |
4821 |
< |
action.apply(u); |
4811 |
> |
final Action<? super U> action; |
4812 |
> |
if ((transformer = this.transformer) != null && |
4813 |
> |
(action = this.action) != null) { |
4814 |
> |
for (int i = baseIndex, f, h; batch > 0 && |
4815 |
> |
(h = ((f = baseLimit) + i) >>> 1) > i;) { |
4816 |
> |
addToPendingCount(1); |
4817 |
> |
new ForEachTransformedValueTask<K,V,U> |
4818 |
> |
(this, batch >>>= 1, baseLimit = h, f, tab, |
4819 |
> |
transformer, action).fork(); |
4820 |
> |
} |
4821 |
> |
for (Node<K,V> p; (p = advance()) != null; ) { |
4822 |
> |
U u; |
4823 |
> |
if ((u = transformer.apply(p.val)) != null) |
4824 |
> |
action.apply(u); |
4825 |
> |
} |
4826 |
> |
propagateCompletion(); |
4827 |
|
} |
5521 |
– |
propagateCompletion(); |
4828 |
|
} |
4829 |
|
} |
4830 |
|
|
4831 |
< |
@SuppressWarnings("serial") static final class ForEachTransformedEntryTask<K,V,U> |
4832 |
< |
extends Traverser<K,V,Void> { |
4831 |
> |
@SuppressWarnings("serial") |
4832 |
> |
static final class ForEachTransformedEntryTask<K,V,U> |
4833 |
> |
extends BulkTask<K,V,Void> { |
4834 |
|
final Fun<Map.Entry<K,V>, ? extends U> transformer; |
4835 |
< |
final Action<U> action; |
4835 |
> |
final Action<? super U> action; |
4836 |
|
ForEachTransformedEntryTask |
4837 |
< |
(ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b, |
4838 |
< |
Fun<Map.Entry<K,V>, ? extends U> transformer, Action<U> action) { |
4839 |
< |
super(m, p, b); |
4837 |
> |
(BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t, |
4838 |
> |
Fun<Map.Entry<K,V>, ? extends U> transformer, Action<? super U> action) { |
4839 |
> |
super(p, b, i, f, t); |
4840 |
|
this.transformer = transformer; this.action = action; |
4841 |
|
} |
4842 |
< |
@SuppressWarnings("unchecked") public final void compute() { |
4842 |
> |
public final void compute() { |
4843 |
|
final Fun<Map.Entry<K,V>, ? extends U> transformer; |
4844 |
< |
final Action<U> action; |
4845 |
< |
if ((transformer = this.transformer) == null || |
4846 |
< |
(action = this.action) == null) |
4847 |
< |
throw new NullPointerException(); |
4848 |
< |
for (int b; (b = preSplit()) > 0;) |
4849 |
< |
new ForEachTransformedEntryTask<K,V,U> |
4850 |
< |
(map, this, b, transformer, action).fork(); |
4851 |
< |
Object v; U u; |
4852 |
< |
while ((v = advance()) != null) { |
4853 |
< |
if ((u = transformer.apply(entryFor((K)nextKey, (V)v))) != null) |
4854 |
< |
action.apply(u); |
4844 |
> |
final Action<? super U> action; |
4845 |
> |
if ((transformer = this.transformer) != null && |
4846 |
> |
(action = this.action) != null) { |
4847 |
> |
for (int i = baseIndex, f, h; batch > 0 && |
4848 |
> |
(h = ((f = baseLimit) + i) >>> 1) > i;) { |
4849 |
> |
addToPendingCount(1); |
4850 |
> |
new ForEachTransformedEntryTask<K,V,U> |
4851 |
> |
(this, batch >>>= 1, baseLimit = h, f, tab, |
4852 |
> |
transformer, action).fork(); |
4853 |
> |
} |
4854 |
> |
for (Node<K,V> p; (p = advance()) != null; ) { |
4855 |
> |
U u; |
4856 |
> |
if ((u = transformer.apply(p)) != null) |
4857 |
> |
action.apply(u); |
4858 |
> |
} |
4859 |
> |
propagateCompletion(); |
4860 |
|
} |
5549 |
– |
propagateCompletion(); |
4861 |
|
} |
4862 |
|
} |
4863 |
|
|
4864 |
< |
@SuppressWarnings("serial") static final class ForEachTransformedMappingTask<K,V,U> |
4865 |
< |
extends Traverser<K,V,Void> { |
4864 |
> |
@SuppressWarnings("serial") |
4865 |
> |
static final class ForEachTransformedMappingTask<K,V,U> |
4866 |
> |
extends BulkTask<K,V,Void> { |
4867 |
|
final BiFun<? super K, ? super V, ? extends U> transformer; |
4868 |
< |
final Action<U> action; |
4868 |
> |
final Action<? super U> action; |
4869 |
|
ForEachTransformedMappingTask |
4870 |
< |
(ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b, |
4870 |
> |
(BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t, |
4871 |
|
BiFun<? super K, ? super V, ? extends U> transformer, |
4872 |
< |
Action<U> action) { |
4873 |
< |
super(m, p, b); |
4872 |
> |
Action<? super U> action) { |
4873 |
> |
super(p, b, i, f, t); |
4874 |
|
this.transformer = transformer; this.action = action; |
4875 |
|
} |
4876 |
< |
@SuppressWarnings("unchecked") public final void compute() { |
4876 |
> |
public final void compute() { |
4877 |
|
final BiFun<? super K, ? super V, ? extends U> transformer; |
4878 |
< |
final Action<U> action; |
4879 |
< |
if ((transformer = this.transformer) == null || |
4880 |
< |
(action = this.action) == null) |
4881 |
< |
throw new NullPointerException(); |
4882 |
< |
for (int b; (b = preSplit()) > 0;) |
4883 |
< |
new ForEachTransformedMappingTask<K,V,U> |
4884 |
< |
(map, this, b, transformer, action).fork(); |
4885 |
< |
Object v; U u; |
4886 |
< |
while ((v = advance()) != null) { |
4887 |
< |
if ((u = transformer.apply((K)nextKey, (V)v)) != null) |
4888 |
< |
action.apply(u); |
4878 |
> |
final Action<? super U> action; |
4879 |
> |
if ((transformer = this.transformer) != null && |
4880 |
> |
(action = this.action) != null) { |
4881 |
> |
for (int i = baseIndex, f, h; batch > 0 && |
4882 |
> |
(h = ((f = baseLimit) + i) >>> 1) > i;) { |
4883 |
> |
addToPendingCount(1); |
4884 |
> |
new ForEachTransformedMappingTask<K,V,U> |
4885 |
> |
(this, batch >>>= 1, baseLimit = h, f, tab, |
4886 |
> |
transformer, action).fork(); |
4887 |
> |
} |
4888 |
> |
for (Node<K,V> p; (p = advance()) != null; ) { |
4889 |
> |
U u; |
4890 |
> |
if ((u = transformer.apply(p.key, p.val)) != null) |
4891 |
> |
action.apply(u); |
4892 |
> |
} |
4893 |
> |
propagateCompletion(); |
4894 |
|
} |
5578 |
– |
propagateCompletion(); |
4895 |
|
} |
4896 |
|
} |
4897 |
|
|
4898 |
< |
@SuppressWarnings("serial") static final class SearchKeysTask<K,V,U> |
4899 |
< |
extends Traverser<K,V,U> { |
4898 |
> |
@SuppressWarnings("serial") |
4899 |
> |
static final class SearchKeysTask<K,V,U> |
4900 |
> |
extends BulkTask<K,V,U> { |
4901 |
|
final Fun<? super K, ? extends U> searchFunction; |
4902 |
|
final AtomicReference<U> result; |
4903 |
|
SearchKeysTask |
4904 |
< |
(ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b, |
4904 |
> |
(BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t, |
4905 |
|
Fun<? super K, ? extends U> searchFunction, |
4906 |
|
AtomicReference<U> result) { |
4907 |
< |
super(m, p, b); |
4907 |
> |
super(p, b, i, f, t); |
4908 |
|
this.searchFunction = searchFunction; this.result = result; |
4909 |
|
} |
4910 |
|
public final U getRawResult() { return result.get(); } |
4911 |
< |
@SuppressWarnings("unchecked") public final void compute() { |
4911 |
> |
public final void compute() { |
4912 |
|
final Fun<? super K, ? extends U> searchFunction; |
4913 |
|
final AtomicReference<U> result; |
4914 |
< |
if ((searchFunction = this.searchFunction) == null || |
4915 |
< |
(result = this.result) == null) |
4916 |
< |
throw new NullPointerException(); |
4917 |
< |
for (int b;;) { |
4918 |
< |
if (result.get() != null) |
4919 |
< |
return; |
4920 |
< |
if ((b = preSplit()) <= 0) |
4921 |
< |
break; |
4922 |
< |
new SearchKeysTask<K,V,U> |
4923 |
< |
(map, this, b, searchFunction, result).fork(); |
4924 |
< |
} |
4925 |
< |
while (result.get() == null) { |
4926 |
< |
U u; |
4927 |
< |
if (advance() == null) { |
4928 |
< |
propagateCompletion(); |
4929 |
< |
break; |
4930 |
< |
} |
4931 |
< |
if ((u = searchFunction.apply((K)nextKey)) != null) { |
4932 |
< |
if (result.compareAndSet(null, u)) |
4933 |
< |
quietlyCompleteRoot(); |
4934 |
< |
break; |
4914 |
> |
if ((searchFunction = this.searchFunction) != null && |
4915 |
> |
(result = this.result) != null) { |
4916 |
> |
for (int i = baseIndex, f, h; batch > 0 && |
4917 |
> |
(h = ((f = baseLimit) + i) >>> 1) > i;) { |
4918 |
> |
if (result.get() != null) |
4919 |
> |
return; |
4920 |
> |
addToPendingCount(1); |
4921 |
> |
new SearchKeysTask<K,V,U> |
4922 |
> |
(this, batch >>>= 1, baseLimit = h, f, tab, |
4923 |
> |
searchFunction, result).fork(); |
4924 |
> |
} |
4925 |
> |
while (result.get() == null) { |
4926 |
> |
U u; |
4927 |
> |
Node<K,V> p; |
4928 |
> |
if ((p = advance()) == null) { |
4929 |
> |
propagateCompletion(); |
4930 |
> |
break; |
4931 |
> |
} |
4932 |
> |
if ((u = searchFunction.apply(p.key)) != null) { |
4933 |
> |
if (result.compareAndSet(null, u)) |
4934 |
> |
quietlyCompleteRoot(); |
4935 |
> |
break; |
4936 |
> |
} |
4937 |
|
} |
4938 |
|
} |
4939 |
|
} |
4940 |
|
} |
4941 |
|
|
4942 |
< |
@SuppressWarnings("serial") static final class SearchValuesTask<K,V,U> |
4943 |
< |
extends Traverser<K,V,U> { |
4942 |
> |
@SuppressWarnings("serial") |
4943 |
> |
static final class SearchValuesTask<K,V,U> |
4944 |
> |
extends BulkTask<K,V,U> { |
4945 |
|
final Fun<? super V, ? extends U> searchFunction; |
4946 |
|
final AtomicReference<U> result; |
4947 |
|
SearchValuesTask |
4948 |
< |
(ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b, |
4948 |
> |
(BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t, |
4949 |
|
Fun<? super V, ? extends U> searchFunction, |
4950 |
|
AtomicReference<U> result) { |
4951 |
< |
super(m, p, b); |
4951 |
> |
super(p, b, i, f, t); |
4952 |
|
this.searchFunction = searchFunction; this.result = result; |
4953 |
|
} |
4954 |
|
public final U getRawResult() { return result.get(); } |
4955 |
< |
@SuppressWarnings("unchecked") public final void compute() { |
4955 |
> |
public final void compute() { |
4956 |
|
final Fun<? super V, ? extends U> searchFunction; |
4957 |
|
final AtomicReference<U> result; |
4958 |
< |
if ((searchFunction = this.searchFunction) == null || |
4959 |
< |
(result = this.result) == null) |
4960 |
< |
throw new NullPointerException(); |
4961 |
< |
for (int b;;) { |
4962 |
< |
if (result.get() != null) |
4963 |
< |
return; |
4964 |
< |
if ((b = preSplit()) <= 0) |
4965 |
< |
break; |
4966 |
< |
new SearchValuesTask<K,V,U> |
4967 |
< |
(map, this, b, searchFunction, result).fork(); |
4968 |
< |
} |
4969 |
< |
while (result.get() == null) { |
4970 |
< |
Object v; U u; |
4971 |
< |
if ((v = advance()) == null) { |
4972 |
< |
propagateCompletion(); |
4973 |
< |
break; |
4974 |
< |
} |
4975 |
< |
if ((u = searchFunction.apply((V)v)) != null) { |
4976 |
< |
if (result.compareAndSet(null, u)) |
4977 |
< |
quietlyCompleteRoot(); |
4978 |
< |
break; |
4958 |
> |
if ((searchFunction = this.searchFunction) != null && |
4959 |
> |
(result = this.result) != null) { |
4960 |
> |
for (int i = baseIndex, f, h; batch > 0 && |
4961 |
> |
(h = ((f = baseLimit) + i) >>> 1) > i;) { |
4962 |
> |
if (result.get() != null) |
4963 |
> |
return; |
4964 |
> |
addToPendingCount(1); |
4965 |
> |
new SearchValuesTask<K,V,U> |
4966 |
> |
(this, batch >>>= 1, baseLimit = h, f, tab, |
4967 |
> |
searchFunction, result).fork(); |
4968 |
> |
} |
4969 |
> |
while (result.get() == null) { |
4970 |
> |
U u; |
4971 |
> |
Node<K,V> p; |
4972 |
> |
if ((p = advance()) == null) { |
4973 |
> |
propagateCompletion(); |
4974 |
> |
break; |
4975 |
> |
} |
4976 |
> |
if ((u = searchFunction.apply(p.val)) != null) { |
4977 |
> |
if (result.compareAndSet(null, u)) |
4978 |
> |
quietlyCompleteRoot(); |
4979 |
> |
break; |
4980 |
> |
} |
4981 |
|
} |
4982 |
|
} |
4983 |
|
} |
4984 |
|
} |
4985 |
|
|
4986 |
< |
@SuppressWarnings("serial") static final class SearchEntriesTask<K,V,U> |
4987 |
< |
extends Traverser<K,V,U> { |
4986 |
> |
@SuppressWarnings("serial") |
4987 |
> |
static final class SearchEntriesTask<K,V,U> |
4988 |
> |
extends BulkTask<K,V,U> { |
4989 |
|
final Fun<Entry<K,V>, ? extends U> searchFunction; |
4990 |
|
final AtomicReference<U> result; |
4991 |
|
SearchEntriesTask |
4992 |
< |
(ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b, |
4992 |
> |
(BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t, |
4993 |
|
Fun<Entry<K,V>, ? extends U> searchFunction, |
4994 |
|
AtomicReference<U> result) { |
4995 |
< |
super(m, p, b); |
4995 |
> |
super(p, b, i, f, t); |
4996 |
|
this.searchFunction = searchFunction; this.result = result; |
4997 |
|
} |
4998 |
|
public final U getRawResult() { return result.get(); } |
4999 |
< |
@SuppressWarnings("unchecked") public final void compute() { |
4999 |
> |
public final void compute() { |
5000 |
|
final Fun<Entry<K,V>, ? extends U> searchFunction; |
5001 |
|
final AtomicReference<U> result; |
5002 |
< |
if ((searchFunction = this.searchFunction) == null || |
5003 |
< |
(result = this.result) == null) |
5004 |
< |
throw new NullPointerException(); |
5005 |
< |
for (int b;;) { |
5006 |
< |
if (result.get() != null) |
5007 |
< |
return; |
5008 |
< |
if ((b = preSplit()) <= 0) |
5009 |
< |
break; |
5010 |
< |
new SearchEntriesTask<K,V,U> |
5011 |
< |
(map, this, b, searchFunction, result).fork(); |
5012 |
< |
} |
5013 |
< |
while (result.get() == null) { |
5014 |
< |
Object v; U u; |
5015 |
< |
if ((v = advance()) == null) { |
5016 |
< |
propagateCompletion(); |
5017 |
< |
break; |
5018 |
< |
} |
5019 |
< |
if ((u = searchFunction.apply(entryFor((K)nextKey, (V)v))) != null) { |
5020 |
< |
if (result.compareAndSet(null, u)) |
5021 |
< |
quietlyCompleteRoot(); |
5022 |
< |
return; |
5002 |
> |
if ((searchFunction = this.searchFunction) != null && |
5003 |
> |
(result = this.result) != null) { |
5004 |
> |
for (int i = baseIndex, f, h; batch > 0 && |
5005 |
> |
(h = ((f = baseLimit) + i) >>> 1) > i;) { |
5006 |
> |
if (result.get() != null) |
5007 |
> |
return; |
5008 |
> |
addToPendingCount(1); |
5009 |
> |
new SearchEntriesTask<K,V,U> |
5010 |
> |
(this, batch >>>= 1, baseLimit = h, f, tab, |
5011 |
> |
searchFunction, result).fork(); |
5012 |
> |
} |
5013 |
> |
while (result.get() == null) { |
5014 |
> |
U u; |
5015 |
> |
Node<K,V> p; |
5016 |
> |
if ((p = advance()) == null) { |
5017 |
> |
propagateCompletion(); |
5018 |
> |
break; |
5019 |
> |
} |
5020 |
> |
if ((u = searchFunction.apply(p)) != null) { |
5021 |
> |
if (result.compareAndSet(null, u)) |
5022 |
> |
quietlyCompleteRoot(); |
5023 |
> |
return; |
5024 |
> |
} |
5025 |
|
} |
5026 |
|
} |
5027 |
|
} |
5028 |
|
} |
5029 |
|
|
5030 |
< |
@SuppressWarnings("serial") static final class SearchMappingsTask<K,V,U> |
5031 |
< |
extends Traverser<K,V,U> { |
5030 |
> |
@SuppressWarnings("serial") |
5031 |
> |
static final class SearchMappingsTask<K,V,U> |
5032 |
> |
extends BulkTask<K,V,U> { |
5033 |
|
final BiFun<? super K, ? super V, ? extends U> searchFunction; |
5034 |
|
final AtomicReference<U> result; |
5035 |
|
SearchMappingsTask |
5036 |
< |
(ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b, |
5036 |
> |
(BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t, |
5037 |
|
BiFun<? super K, ? super V, ? extends U> searchFunction, |
5038 |
|
AtomicReference<U> result) { |
5039 |
< |
super(m, p, b); |
5039 |
> |
super(p, b, i, f, t); |
5040 |
|
this.searchFunction = searchFunction; this.result = result; |
5041 |
|
} |
5042 |
|
public final U getRawResult() { return result.get(); } |
5043 |
< |
@SuppressWarnings("unchecked") public final void compute() { |
5043 |
> |
public final void compute() { |
5044 |
|
final BiFun<? super K, ? super V, ? extends U> searchFunction; |
5045 |
|
final AtomicReference<U> result; |
5046 |
< |
if ((searchFunction = this.searchFunction) == null || |
5047 |
< |
(result = this.result) == null) |
5048 |
< |
throw new NullPointerException(); |
5049 |
< |
for (int b;;) { |
5050 |
< |
if (result.get() != null) |
5051 |
< |
return; |
5052 |
< |
if ((b = preSplit()) <= 0) |
5053 |
< |
break; |
5054 |
< |
new SearchMappingsTask<K,V,U> |
5055 |
< |
(map, this, b, searchFunction, result).fork(); |
5056 |
< |
} |
5057 |
< |
while (result.get() == null) { |
5058 |
< |
Object v; U u; |
5059 |
< |
if ((v = advance()) == null) { |
5060 |
< |
propagateCompletion(); |
5061 |
< |
break; |
5062 |
< |
} |
5063 |
< |
if ((u = searchFunction.apply((K)nextKey, (V)v)) != null) { |
5064 |
< |
if (result.compareAndSet(null, u)) |
5065 |
< |
quietlyCompleteRoot(); |
5066 |
< |
break; |
5046 |
> |
if ((searchFunction = this.searchFunction) != null && |
5047 |
> |
(result = this.result) != null) { |
5048 |
> |
for (int i = baseIndex, f, h; batch > 0 && |
5049 |
> |
(h = ((f = baseLimit) + i) >>> 1) > i;) { |
5050 |
> |
if (result.get() != null) |
5051 |
> |
return; |
5052 |
> |
addToPendingCount(1); |
5053 |
> |
new SearchMappingsTask<K,V,U> |
5054 |
> |
(this, batch >>>= 1, baseLimit = h, f, tab, |
5055 |
> |
searchFunction, result).fork(); |
5056 |
> |
} |
5057 |
> |
while (result.get() == null) { |
5058 |
> |
U u; |
5059 |
> |
Node<K,V> p; |
5060 |
> |
if ((p = advance()) == null) { |
5061 |
> |
propagateCompletion(); |
5062 |
> |
break; |
5063 |
> |
} |
5064 |
> |
if ((u = searchFunction.apply(p.key, p.val)) != null) { |
5065 |
> |
if (result.compareAndSet(null, u)) |
5066 |
> |
quietlyCompleteRoot(); |
5067 |
> |
break; |
5068 |
> |
} |
5069 |
|
} |
5070 |
|
} |
5071 |
|
} |
5072 |
|
} |
5073 |
|
|
5074 |
< |
@SuppressWarnings("serial") static final class ReduceKeysTask<K,V> |
5075 |
< |
extends Traverser<K,V,K> { |
5074 |
> |
@SuppressWarnings("serial") |
5075 |
> |
static final class ReduceKeysTask<K,V> |
5076 |
> |
extends BulkTask<K,V,K> { |
5077 |
|
final BiFun<? super K, ? super K, ? extends K> reducer; |
5078 |
|
K result; |
5079 |
|
ReduceKeysTask<K,V> rights, nextRight; |
5080 |
|
ReduceKeysTask |
5081 |
< |
(ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b, |
5081 |
> |
(BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t, |
5082 |
|
ReduceKeysTask<K,V> nextRight, |
5083 |
|
BiFun<? super K, ? super K, ? extends K> reducer) { |
5084 |
< |
super(m, p, b); this.nextRight = nextRight; |
5084 |
> |
super(p, b, i, f, t); this.nextRight = nextRight; |
5085 |
|
this.reducer = reducer; |
5086 |
|
} |
5087 |
|
public final K getRawResult() { return result; } |
5088 |
< |
@SuppressWarnings("unchecked") public final void compute() { |
5089 |
< |
final BiFun<? super K, ? super K, ? extends K> reducer = |
5090 |
< |
this.reducer; |
5091 |
< |
if (reducer == null) |
5092 |
< |
throw new NullPointerException(); |
5093 |
< |
for (int b; (b = preSplit()) > 0;) |
5094 |
< |
(rights = new ReduceKeysTask<K,V> |
5095 |
< |
(map, this, b, rights, reducer)).fork(); |
5096 |
< |
K r = null; |
5097 |
< |
while (advance() != null) { |
5098 |
< |
K u = (K)nextKey; |
5099 |
< |
r = (r == null) ? u : reducer.apply(r, u); |
5100 |
< |
} |
5101 |
< |
result = r; |
5102 |
< |
CountedCompleter<?> c; |
5103 |
< |
for (c = firstComplete(); c != null; c = c.nextComplete()) { |
5104 |
< |
ReduceKeysTask<K,V> |
5105 |
< |
t = (ReduceKeysTask<K,V>)c, |
5106 |
< |
s = t.rights; |
5107 |
< |
while (s != null) { |
5108 |
< |
K tr, sr; |
5109 |
< |
if ((sr = s.result) != null) |
5110 |
< |
t.result = (((tr = t.result) == null) ? sr : |
5111 |
< |
reducer.apply(tr, sr)); |
5112 |
< |
s = t.rights = s.nextRight; |
5088 |
> |
public final void compute() { |
5089 |
> |
final BiFun<? super K, ? super K, ? extends K> reducer; |
5090 |
> |
if ((reducer = this.reducer) != null) { |
5091 |
> |
for (int i = baseIndex, f, h; batch > 0 && |
5092 |
> |
(h = ((f = baseLimit) + i) >>> 1) > i;) { |
5093 |
> |
addToPendingCount(1); |
5094 |
> |
(rights = new ReduceKeysTask<K,V> |
5095 |
> |
(this, batch >>>= 1, baseLimit = h, f, tab, |
5096 |
> |
rights, reducer)).fork(); |
5097 |
> |
} |
5098 |
> |
K r = null; |
5099 |
> |
for (Node<K,V> p; (p = advance()) != null; ) { |
5100 |
> |
K u = p.key; |
5101 |
> |
r = (r == null) ? u : u == null ? r : reducer.apply(r, u); |
5102 |
> |
} |
5103 |
> |
result = r; |
5104 |
> |
CountedCompleter<?> c; |
5105 |
> |
for (c = firstComplete(); c != null; c = c.nextComplete()) { |
5106 |
> |
@SuppressWarnings("unchecked") ReduceKeysTask<K,V> |
5107 |
> |
t = (ReduceKeysTask<K,V>)c, |
5108 |
> |
s = t.rights; |
5109 |
> |
while (s != null) { |
5110 |
> |
K tr, sr; |
5111 |
> |
if ((sr = s.result) != null) |
5112 |
> |
t.result = (((tr = t.result) == null) ? sr : |
5113 |
> |
reducer.apply(tr, sr)); |
5114 |
> |
s = t.rights = s.nextRight; |
5115 |
> |
} |
5116 |
|
} |
5117 |
|
} |
5118 |
|
} |
5119 |
|
} |
5120 |
|
|
5121 |
< |
@SuppressWarnings("serial") static final class ReduceValuesTask<K,V> |
5122 |
< |
extends Traverser<K,V,V> { |
5121 |
> |
@SuppressWarnings("serial") |
5122 |
> |
static final class ReduceValuesTask<K,V> |
5123 |
> |
extends BulkTask<K,V,V> { |
5124 |
|
final BiFun<? super V, ? super V, ? extends V> reducer; |
5125 |
|
V result; |
5126 |
|
ReduceValuesTask<K,V> rights, nextRight; |
5127 |
|
ReduceValuesTask |
5128 |
< |
(ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b, |
5128 |
> |
(BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t, |
5129 |
|
ReduceValuesTask<K,V> nextRight, |
5130 |
|
BiFun<? super V, ? super V, ? extends V> reducer) { |
5131 |
< |
super(m, p, b); this.nextRight = nextRight; |
5131 |
> |
super(p, b, i, f, t); this.nextRight = nextRight; |
5132 |
|
this.reducer = reducer; |
5133 |
|
} |
5134 |
|
public final V getRawResult() { return result; } |
5135 |
< |
@SuppressWarnings("unchecked") public final void compute() { |
5136 |
< |
final BiFun<? super V, ? super V, ? extends V> reducer = |
5137 |
< |
this.reducer; |
5138 |
< |
if (reducer == null) |
5139 |
< |
throw new NullPointerException(); |
5140 |
< |
for (int b; (b = preSplit()) > 0;) |
5141 |
< |
(rights = new ReduceValuesTask<K,V> |
5142 |
< |
(map, this, b, rights, reducer)).fork(); |
5143 |
< |
V r = null; |
5144 |
< |
Object v; |
5145 |
< |
while ((v = advance()) != null) { |
5146 |
< |
V u = (V)v; |
5147 |
< |
r = (r == null) ? u : reducer.apply(r, u); |
5148 |
< |
} |
5149 |
< |
result = r; |
5150 |
< |
CountedCompleter<?> c; |
5151 |
< |
for (c = firstComplete(); c != null; c = c.nextComplete()) { |
5152 |
< |
ReduceValuesTask<K,V> |
5153 |
< |
t = (ReduceValuesTask<K,V>)c, |
5154 |
< |
s = t.rights; |
5155 |
< |
while (s != null) { |
5156 |
< |
V tr, sr; |
5157 |
< |
if ((sr = s.result) != null) |
5158 |
< |
t.result = (((tr = t.result) == null) ? sr : |
5159 |
< |
reducer.apply(tr, sr)); |
5160 |
< |
s = t.rights = s.nextRight; |
5135 |
> |
public final void compute() { |
5136 |
> |
final BiFun<? super V, ? super V, ? extends V> reducer; |
5137 |
> |
if ((reducer = this.reducer) != null) { |
5138 |
> |
for (int i = baseIndex, f, h; batch > 0 && |
5139 |
> |
(h = ((f = baseLimit) + i) >>> 1) > i;) { |
5140 |
> |
addToPendingCount(1); |
5141 |
> |
(rights = new ReduceValuesTask<K,V> |
5142 |
> |
(this, batch >>>= 1, baseLimit = h, f, tab, |
5143 |
> |
rights, reducer)).fork(); |
5144 |
> |
} |
5145 |
> |
V r = null; |
5146 |
> |
for (Node<K,V> p; (p = advance()) != null; ) { |
5147 |
> |
V v = p.val; |
5148 |
> |
r = (r == null) ? v : reducer.apply(r, v); |
5149 |
> |
} |
5150 |
> |
result = r; |
5151 |
> |
CountedCompleter<?> c; |
5152 |
> |
for (c = firstComplete(); c != null; c = c.nextComplete()) { |
5153 |
> |
@SuppressWarnings("unchecked") ReduceValuesTask<K,V> |
5154 |
> |
t = (ReduceValuesTask<K,V>)c, |
5155 |
> |
s = t.rights; |
5156 |
> |
while (s != null) { |
5157 |
> |
V tr, sr; |
5158 |
> |
if ((sr = s.result) != null) |
5159 |
> |
t.result = (((tr = t.result) == null) ? sr : |
5160 |
> |
reducer.apply(tr, sr)); |
5161 |
> |
s = t.rights = s.nextRight; |
5162 |
> |
} |
5163 |
|
} |
5164 |
|
} |
5165 |
|
} |
5166 |
|
} |
5167 |
|
|
5168 |
< |
@SuppressWarnings("serial") static final class ReduceEntriesTask<K,V> |
5169 |
< |
extends Traverser<K,V,Map.Entry<K,V>> { |
5168 |
> |
@SuppressWarnings("serial") |
5169 |
> |
static final class ReduceEntriesTask<K,V> |
5170 |
> |
extends BulkTask<K,V,Map.Entry<K,V>> { |
5171 |
|
final BiFun<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer; |
5172 |
|
Map.Entry<K,V> result; |
5173 |
|
ReduceEntriesTask<K,V> rights, nextRight; |
5174 |
|
ReduceEntriesTask |
5175 |
< |
(ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b, |
5175 |
> |
(BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t, |
5176 |
|
ReduceEntriesTask<K,V> nextRight, |
5177 |
|
BiFun<Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer) { |
5178 |
< |
super(m, p, b); this.nextRight = nextRight; |
5178 |
> |
super(p, b, i, f, t); this.nextRight = nextRight; |
5179 |
|
this.reducer = reducer; |
5180 |
|
} |
5181 |
|
public final Map.Entry<K,V> getRawResult() { return result; } |
5182 |
< |
@SuppressWarnings("unchecked") public final void compute() { |
5183 |
< |
final BiFun<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer = |
5184 |
< |
this.reducer; |
5185 |
< |
if (reducer == null) |
5186 |
< |
throw new NullPointerException(); |
5187 |
< |
for (int b; (b = preSplit()) > 0;) |
5188 |
< |
(rights = new ReduceEntriesTask<K,V> |
5189 |
< |
(map, this, b, rights, reducer)).fork(); |
5190 |
< |
Map.Entry<K,V> r = null; |
5191 |
< |
Object v; |
5192 |
< |
while ((v = advance()) != null) { |
5193 |
< |
Map.Entry<K,V> u = entryFor((K)nextKey, (V)v); |
5194 |
< |
r = (r == null) ? u : reducer.apply(r, u); |
5195 |
< |
} |
5196 |
< |
result = r; |
5197 |
< |
CountedCompleter<?> c; |
5198 |
< |
for (c = firstComplete(); c != null; c = c.nextComplete()) { |
5199 |
< |
ReduceEntriesTask<K,V> |
5200 |
< |
t = (ReduceEntriesTask<K,V>)c, |
5201 |
< |
s = t.rights; |
5202 |
< |
while (s != null) { |
5203 |
< |
Map.Entry<K,V> tr, sr; |
5204 |
< |
if ((sr = s.result) != null) |
5205 |
< |
t.result = (((tr = t.result) == null) ? sr : |
5206 |
< |
reducer.apply(tr, sr)); |
5207 |
< |
s = t.rights = s.nextRight; |
5182 |
> |
public final void compute() { |
5183 |
> |
final BiFun<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer; |
5184 |
> |
if ((reducer = this.reducer) != null) { |
5185 |
> |
for (int i = baseIndex, f, h; batch > 0 && |
5186 |
> |
(h = ((f = baseLimit) + i) >>> 1) > i;) { |
5187 |
> |
addToPendingCount(1); |
5188 |
> |
(rights = new ReduceEntriesTask<K,V> |
5189 |
> |
(this, batch >>>= 1, baseLimit = h, f, tab, |
5190 |
> |
rights, reducer)).fork(); |
5191 |
> |
} |
5192 |
> |
Map.Entry<K,V> r = null; |
5193 |
> |
for (Node<K,V> p; (p = advance()) != null; ) |
5194 |
> |
r = (r == null) ? p : reducer.apply(r, p); |
5195 |
> |
result = r; |
5196 |
> |
CountedCompleter<?> c; |
5197 |
> |
for (c = firstComplete(); c != null; c = c.nextComplete()) { |
5198 |
> |
@SuppressWarnings("unchecked") ReduceEntriesTask<K,V> |
5199 |
> |
t = (ReduceEntriesTask<K,V>)c, |
5200 |
> |
s = t.rights; |
5201 |
> |
while (s != null) { |
5202 |
> |
Map.Entry<K,V> tr, sr; |
5203 |
> |
if ((sr = s.result) != null) |
5204 |
> |
t.result = (((tr = t.result) == null) ? sr : |
5205 |
> |
reducer.apply(tr, sr)); |
5206 |
> |
s = t.rights = s.nextRight; |
5207 |
> |
} |
5208 |
|
} |
5209 |
|
} |
5210 |
|
} |
5211 |
|
} |
5212 |
|
|
5213 |
< |
@SuppressWarnings("serial") static final class MapReduceKeysTask<K,V,U> |
5214 |
< |
extends Traverser<K,V,U> { |
5213 |
> |
@SuppressWarnings("serial") |
5214 |
> |
static final class MapReduceKeysTask<K,V,U> |
5215 |
> |
extends BulkTask<K,V,U> { |
5216 |
|
final Fun<? super K, ? extends U> transformer; |
5217 |
|
final BiFun<? super U, ? super U, ? extends U> reducer; |
5218 |
|
U result; |
5219 |
|
MapReduceKeysTask<K,V,U> rights, nextRight; |
5220 |
|
MapReduceKeysTask |
5221 |
< |
(ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b, |
5221 |
> |
(BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t, |
5222 |
|
MapReduceKeysTask<K,V,U> nextRight, |
5223 |
|
Fun<? super K, ? extends U> transformer, |
5224 |
|
BiFun<? super U, ? super U, ? extends U> reducer) { |
5225 |
< |
super(m, p, b); this.nextRight = nextRight; |
5225 |
> |
super(p, b, i, f, t); this.nextRight = nextRight; |
5226 |
|
this.transformer = transformer; |
5227 |
|
this.reducer = reducer; |
5228 |
|
} |
5229 |
|
public final U getRawResult() { return result; } |
5230 |
< |
@SuppressWarnings("unchecked") public final void compute() { |
5231 |
< |
final Fun<? super K, ? extends U> transformer = |
5232 |
< |
this.transformer; |
5233 |
< |
final BiFun<? super U, ? super U, ? extends U> reducer = |
5234 |
< |
this.reducer; |
5235 |
< |
if (transformer == null || reducer == null) |
5236 |
< |
throw new NullPointerException(); |
5237 |
< |
for (int b; (b = preSplit()) > 0;) |
5238 |
< |
(rights = new MapReduceKeysTask<K,V,U> |
5239 |
< |
(map, this, b, rights, transformer, reducer)).fork(); |
5240 |
< |
U r = null, u; |
5241 |
< |
while (advance() != null) { |
5242 |
< |
if ((u = transformer.apply((K)nextKey)) != null) |
5243 |
< |
r = (r == null) ? u : reducer.apply(r, u); |
5244 |
< |
} |
5245 |
< |
result = r; |
5246 |
< |
CountedCompleter<?> c; |
5247 |
< |
for (c = firstComplete(); c != null; c = c.nextComplete()) { |
5248 |
< |
MapReduceKeysTask<K,V,U> |
5249 |
< |
t = (MapReduceKeysTask<K,V,U>)c, |
5250 |
< |
s = t.rights; |
5251 |
< |
while (s != null) { |
5252 |
< |
U tr, sr; |
5253 |
< |
if ((sr = s.result) != null) |
5254 |
< |
t.result = (((tr = t.result) == null) ? sr : |
5255 |
< |
reducer.apply(tr, sr)); |
5256 |
< |
s = t.rights = s.nextRight; |
5230 |
> |
public final void compute() { |
5231 |
> |
final Fun<? super K, ? extends U> transformer; |
5232 |
> |
final BiFun<? super U, ? super U, ? extends U> reducer; |
5233 |
> |
if ((transformer = this.transformer) != null && |
5234 |
> |
(reducer = this.reducer) != null) { |
5235 |
> |
for (int i = baseIndex, f, h; batch > 0 && |
5236 |
> |
(h = ((f = baseLimit) + i) >>> 1) > i;) { |
5237 |
> |
addToPendingCount(1); |
5238 |
> |
(rights = new MapReduceKeysTask<K,V,U> |
5239 |
> |
(this, batch >>>= 1, baseLimit = h, f, tab, |
5240 |
> |
rights, transformer, reducer)).fork(); |
5241 |
> |
} |
5242 |
> |
U r = null; |
5243 |
> |
for (Node<K,V> p; (p = advance()) != null; ) { |
5244 |
> |
U u; |
5245 |
> |
if ((u = transformer.apply(p.key)) != null) |
5246 |
> |
r = (r == null) ? u : reducer.apply(r, u); |
5247 |
> |
} |
5248 |
> |
result = r; |
5249 |
> |
CountedCompleter<?> c; |
5250 |
> |
for (c = firstComplete(); c != null; c = c.nextComplete()) { |
5251 |
> |
@SuppressWarnings("unchecked") MapReduceKeysTask<K,V,U> |
5252 |
> |
t = (MapReduceKeysTask<K,V,U>)c, |
5253 |
> |
s = t.rights; |
5254 |
> |
while (s != null) { |
5255 |
> |
U tr, sr; |
5256 |
> |
if ((sr = s.result) != null) |
5257 |
> |
t.result = (((tr = t.result) == null) ? sr : |
5258 |
> |
reducer.apply(tr, sr)); |
5259 |
> |
s = t.rights = s.nextRight; |
5260 |
> |
} |
5261 |
|
} |
5262 |
|
} |
5263 |
|
} |
5264 |
|
} |
5265 |
|
|
5266 |
< |
@SuppressWarnings("serial") static final class MapReduceValuesTask<K,V,U> |
5267 |
< |
extends Traverser<K,V,U> { |
5266 |
> |
@SuppressWarnings("serial") |
5267 |
> |
static final class MapReduceValuesTask<K,V,U> |
5268 |
> |
extends BulkTask<K,V,U> { |
5269 |
|
final Fun<? super V, ? extends U> transformer; |
5270 |
|
final BiFun<? super U, ? super U, ? extends U> reducer; |
5271 |
|
U result; |
5272 |
|
MapReduceValuesTask<K,V,U> rights, nextRight; |
5273 |
|
MapReduceValuesTask |
5274 |
< |
(ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b, |
5274 |
> |
(BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t, |
5275 |
|
MapReduceValuesTask<K,V,U> nextRight, |
5276 |
|
Fun<? super V, ? extends U> transformer, |
5277 |
|
BiFun<? super U, ? super U, ? extends U> reducer) { |
5278 |
< |
super(m, p, b); this.nextRight = nextRight; |
5278 |
> |
super(p, b, i, f, t); this.nextRight = nextRight; |
5279 |
|
this.transformer = transformer; |
5280 |
|
this.reducer = reducer; |
5281 |
|
} |
5282 |
|
public final U getRawResult() { return result; } |
5283 |
< |
@SuppressWarnings("unchecked") public final void compute() { |
5284 |
< |
final Fun<? super V, ? extends U> transformer = |
5285 |
< |
this.transformer; |
5286 |
< |
final BiFun<? super U, ? super U, ? extends U> reducer = |
5287 |
< |
this.reducer; |
5288 |
< |
if (transformer == null || reducer == null) |
5289 |
< |
throw new NullPointerException(); |
5290 |
< |
for (int b; (b = preSplit()) > 0;) |
5291 |
< |
(rights = new MapReduceValuesTask<K,V,U> |
5292 |
< |
(map, this, b, rights, transformer, reducer)).fork(); |
5293 |
< |
U r = null, u; |
5294 |
< |
Object v; |
5295 |
< |
while ((v = advance()) != null) { |
5296 |
< |
if ((u = transformer.apply((V)v)) != null) |
5297 |
< |
r = (r == null) ? u : reducer.apply(r, u); |
5298 |
< |
} |
5299 |
< |
result = r; |
5300 |
< |
CountedCompleter<?> c; |
5301 |
< |
for (c = firstComplete(); c != null; c = c.nextComplete()) { |
5302 |
< |
MapReduceValuesTask<K,V,U> |
5303 |
< |
t = (MapReduceValuesTask<K,V,U>)c, |
5304 |
< |
s = t.rights; |
5305 |
< |
while (s != null) { |
5306 |
< |
U tr, sr; |
5307 |
< |
if ((sr = s.result) != null) |
5308 |
< |
t.result = (((tr = t.result) == null) ? sr : |
5309 |
< |
reducer.apply(tr, sr)); |
5310 |
< |
s = t.rights = s.nextRight; |
5283 |
> |
public final void compute() { |
5284 |
> |
final Fun<? super V, ? extends U> transformer; |
5285 |
> |
final BiFun<? super U, ? super U, ? extends U> reducer; |
5286 |
> |
if ((transformer = this.transformer) != null && |
5287 |
> |
(reducer = this.reducer) != null) { |
5288 |
> |
for (int i = baseIndex, f, h; batch > 0 && |
5289 |
> |
(h = ((f = baseLimit) + i) >>> 1) > i;) { |
5290 |
> |
addToPendingCount(1); |
5291 |
> |
(rights = new MapReduceValuesTask<K,V,U> |
5292 |
> |
(this, batch >>>= 1, baseLimit = h, f, tab, |
5293 |
> |
rights, transformer, reducer)).fork(); |
5294 |
> |
} |
5295 |
> |
U r = null; |
5296 |
> |
for (Node<K,V> p; (p = advance()) != null; ) { |
5297 |
> |
U u; |
5298 |
> |
if ((u = transformer.apply(p.val)) != null) |
5299 |
> |
r = (r == null) ? u : reducer.apply(r, u); |
5300 |
> |
} |
5301 |
> |
result = r; |
5302 |
> |
CountedCompleter<?> c; |
5303 |
> |
for (c = firstComplete(); c != null; c = c.nextComplete()) { |
5304 |
> |
@SuppressWarnings("unchecked") MapReduceValuesTask<K,V,U> |
5305 |
> |
t = (MapReduceValuesTask<K,V,U>)c, |
5306 |
> |
s = t.rights; |
5307 |
> |
while (s != null) { |
5308 |
> |
U tr, sr; |
5309 |
> |
if ((sr = s.result) != null) |
5310 |
> |
t.result = (((tr = t.result) == null) ? sr : |
5311 |
> |
reducer.apply(tr, sr)); |
5312 |
> |
s = t.rights = s.nextRight; |
5313 |
> |
} |
5314 |
|
} |
5315 |
|
} |
5316 |
|
} |
5317 |
|
} |
5318 |
|
|
5319 |
< |
@SuppressWarnings("serial") static final class MapReduceEntriesTask<K,V,U> |
5320 |
< |
extends Traverser<K,V,U> { |
5319 |
> |
@SuppressWarnings("serial") |
5320 |
> |
static final class MapReduceEntriesTask<K,V,U> |
5321 |
> |
extends BulkTask<K,V,U> { |
5322 |
|
final Fun<Map.Entry<K,V>, ? extends U> transformer; |
5323 |
|
final BiFun<? super U, ? super U, ? extends U> reducer; |
5324 |
|
U result; |
5325 |
|
MapReduceEntriesTask<K,V,U> rights, nextRight; |
5326 |
|
MapReduceEntriesTask |
5327 |
< |
(ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b, |
5327 |
> |
(BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t, |
5328 |
|
MapReduceEntriesTask<K,V,U> nextRight, |
5329 |
|
Fun<Map.Entry<K,V>, ? extends U> transformer, |
5330 |
|
BiFun<? super U, ? super U, ? extends U> reducer) { |
5331 |
< |
super(m, p, b); this.nextRight = nextRight; |
5331 |
> |
super(p, b, i, f, t); this.nextRight = nextRight; |
5332 |
|
this.transformer = transformer; |
5333 |
|
this.reducer = reducer; |
5334 |
|
} |
5335 |
|
public final U getRawResult() { return result; } |
5336 |
< |
@SuppressWarnings("unchecked") public final void compute() { |
5337 |
< |
final Fun<Map.Entry<K,V>, ? extends U> transformer = |
5338 |
< |
this.transformer; |
5339 |
< |
final BiFun<? super U, ? super U, ? extends U> reducer = |
5340 |
< |
this.reducer; |
5341 |
< |
if (transformer == null || reducer == null) |
5342 |
< |
throw new NullPointerException(); |
5343 |
< |
for (int b; (b = preSplit()) > 0;) |
5344 |
< |
(rights = new MapReduceEntriesTask<K,V,U> |
5345 |
< |
(map, this, b, rights, transformer, reducer)).fork(); |
5346 |
< |
U r = null, u; |
5347 |
< |
Object v; |
5348 |
< |
while ((v = advance()) != null) { |
5349 |
< |
if ((u = transformer.apply(entryFor((K)nextKey, (V)v))) != null) |
5350 |
< |
r = (r == null) ? u : reducer.apply(r, u); |
5351 |
< |
} |
5352 |
< |
result = r; |
5353 |
< |
CountedCompleter<?> c; |
5354 |
< |
for (c = firstComplete(); c != null; c = c.nextComplete()) { |
5355 |
< |
MapReduceEntriesTask<K,V,U> |
5356 |
< |
t = (MapReduceEntriesTask<K,V,U>)c, |
5357 |
< |
s = t.rights; |
5358 |
< |
while (s != null) { |
5359 |
< |
U tr, sr; |
5360 |
< |
if ((sr = s.result) != null) |
5361 |
< |
t.result = (((tr = t.result) == null) ? sr : |
5362 |
< |
reducer.apply(tr, sr)); |
5363 |
< |
s = t.rights = s.nextRight; |
5336 |
> |
public final void compute() { |
5337 |
> |
final Fun<Map.Entry<K,V>, ? extends U> transformer; |
5338 |
> |
final BiFun<? super U, ? super U, ? extends U> reducer; |
5339 |
> |
if ((transformer = this.transformer) != null && |
5340 |
> |
(reducer = this.reducer) != null) { |
5341 |
> |
for (int i = baseIndex, f, h; batch > 0 && |
5342 |
> |
(h = ((f = baseLimit) + i) >>> 1) > i;) { |
5343 |
> |
addToPendingCount(1); |
5344 |
> |
(rights = new MapReduceEntriesTask<K,V,U> |
5345 |
> |
(this, batch >>>= 1, baseLimit = h, f, tab, |
5346 |
> |
rights, transformer, reducer)).fork(); |
5347 |
> |
} |
5348 |
> |
U r = null; |
5349 |
> |
for (Node<K,V> p; (p = advance()) != null; ) { |
5350 |
> |
U u; |
5351 |
> |
if ((u = transformer.apply(p)) != null) |
5352 |
> |
r = (r == null) ? u : reducer.apply(r, u); |
5353 |
> |
} |
5354 |
> |
result = r; |
5355 |
> |
CountedCompleter<?> c; |
5356 |
> |
for (c = firstComplete(); c != null; c = c.nextComplete()) { |
5357 |
> |
@SuppressWarnings("unchecked") MapReduceEntriesTask<K,V,U> |
5358 |
> |
t = (MapReduceEntriesTask<K,V,U>)c, |
5359 |
> |
s = t.rights; |
5360 |
> |
while (s != null) { |
5361 |
> |
U tr, sr; |
5362 |
> |
if ((sr = s.result) != null) |
5363 |
> |
t.result = (((tr = t.result) == null) ? sr : |
5364 |
> |
reducer.apply(tr, sr)); |
5365 |
> |
s = t.rights = s.nextRight; |
5366 |
> |
} |
5367 |
|
} |
5368 |
|
} |
5369 |
|
} |
5370 |
|
} |
5371 |
|
|
5372 |
< |
@SuppressWarnings("serial") static final class MapReduceMappingsTask<K,V,U> |
5373 |
< |
extends Traverser<K,V,U> { |
5372 |
> |
@SuppressWarnings("serial") |
5373 |
> |
static final class MapReduceMappingsTask<K,V,U> |
5374 |
> |
extends BulkTask<K,V,U> { |
5375 |
|
final BiFun<? super K, ? super V, ? extends U> transformer; |
5376 |
|
final BiFun<? super U, ? super U, ? extends U> reducer; |
5377 |
|
U result; |
5378 |
|
MapReduceMappingsTask<K,V,U> rights, nextRight; |
5379 |
|
MapReduceMappingsTask |
5380 |
< |
(ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b, |
5380 |
> |
(BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t, |
5381 |
|
MapReduceMappingsTask<K,V,U> nextRight, |
5382 |
|
BiFun<? super K, ? super V, ? extends U> transformer, |
5383 |
|
BiFun<? super U, ? super U, ? extends U> reducer) { |
5384 |
< |
super(m, p, b); this.nextRight = nextRight; |
5384 |
> |
super(p, b, i, f, t); this.nextRight = nextRight; |
5385 |
|
this.transformer = transformer; |
5386 |
|
this.reducer = reducer; |
5387 |
|
} |
5388 |
|
public final U getRawResult() { return result; } |
5389 |
< |
@SuppressWarnings("unchecked") public final void compute() { |
5390 |
< |
final BiFun<? super K, ? super V, ? extends U> transformer = |
5391 |
< |
this.transformer; |
5392 |
< |
final BiFun<? super U, ? super U, ? extends U> reducer = |
5393 |
< |
this.reducer; |
5394 |
< |
if (transformer == null || reducer == null) |
5395 |
< |
throw new NullPointerException(); |
5396 |
< |
for (int b; (b = preSplit()) > 0;) |
5397 |
< |
(rights = new MapReduceMappingsTask<K,V,U> |
5398 |
< |
(map, this, b, rights, transformer, reducer)).fork(); |
5399 |
< |
U r = null, u; |
5400 |
< |
Object v; |
5401 |
< |
while ((v = advance()) != null) { |
5402 |
< |
if ((u = transformer.apply((K)nextKey, (V)v)) != null) |
5403 |
< |
r = (r == null) ? u : reducer.apply(r, u); |
5404 |
< |
} |
5405 |
< |
result = r; |
5406 |
< |
CountedCompleter<?> c; |
5407 |
< |
for (c = firstComplete(); c != null; c = c.nextComplete()) { |
5408 |
< |
MapReduceMappingsTask<K,V,U> |
5409 |
< |
t = (MapReduceMappingsTask<K,V,U>)c, |
5410 |
< |
s = t.rights; |
5411 |
< |
while (s != null) { |
5412 |
< |
U tr, sr; |
5413 |
< |
if ((sr = s.result) != null) |
5414 |
< |
t.result = (((tr = t.result) == null) ? sr : |
5415 |
< |
reducer.apply(tr, sr)); |
5416 |
< |
s = t.rights = s.nextRight; |
5389 |
> |
public final void compute() { |
5390 |
> |
final BiFun<? super K, ? super V, ? extends U> transformer; |
5391 |
> |
final BiFun<? super U, ? super U, ? extends U> reducer; |
5392 |
> |
if ((transformer = this.transformer) != null && |
5393 |
> |
(reducer = this.reducer) != null) { |
5394 |
> |
for (int i = baseIndex, f, h; batch > 0 && |
5395 |
> |
(h = ((f = baseLimit) + i) >>> 1) > i;) { |
5396 |
> |
addToPendingCount(1); |
5397 |
> |
(rights = new MapReduceMappingsTask<K,V,U> |
5398 |
> |
(this, batch >>>= 1, baseLimit = h, f, tab, |
5399 |
> |
rights, transformer, reducer)).fork(); |
5400 |
> |
} |
5401 |
> |
U r = null; |
5402 |
> |
for (Node<K,V> p; (p = advance()) != null; ) { |
5403 |
> |
U u; |
5404 |
> |
if ((u = transformer.apply(p.key, p.val)) != null) |
5405 |
> |
r = (r == null) ? u : reducer.apply(r, u); |
5406 |
> |
} |
5407 |
> |
result = r; |
5408 |
> |
CountedCompleter<?> c; |
5409 |
> |
for (c = firstComplete(); c != null; c = c.nextComplete()) { |
5410 |
> |
@SuppressWarnings("unchecked") MapReduceMappingsTask<K,V,U> |
5411 |
> |
t = (MapReduceMappingsTask<K,V,U>)c, |
5412 |
> |
s = t.rights; |
5413 |
> |
while (s != null) { |
5414 |
> |
U tr, sr; |
5415 |
> |
if ((sr = s.result) != null) |
5416 |
> |
t.result = (((tr = t.result) == null) ? sr : |
5417 |
> |
reducer.apply(tr, sr)); |
5418 |
> |
s = t.rights = s.nextRight; |
5419 |
> |
} |
5420 |
|
} |
5421 |
|
} |
5422 |
|
} |
5423 |
|
} |
5424 |
|
|
5425 |
< |
@SuppressWarnings("serial") static final class MapReduceKeysToDoubleTask<K,V> |
5426 |
< |
extends Traverser<K,V,Double> { |
5425 |
> |
@SuppressWarnings("serial") |
5426 |
> |
static final class MapReduceKeysToDoubleTask<K,V> |
5427 |
> |
extends BulkTask<K,V,Double> { |
5428 |
|
final ObjectToDouble<? super K> transformer; |
5429 |
|
final DoubleByDoubleToDouble reducer; |
5430 |
|
final double basis; |
5431 |
|
double result; |
5432 |
|
MapReduceKeysToDoubleTask<K,V> rights, nextRight; |
5433 |
|
MapReduceKeysToDoubleTask |
5434 |
< |
(ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b, |
5434 |
> |
(BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t, |
5435 |
|
MapReduceKeysToDoubleTask<K,V> nextRight, |
5436 |
|
ObjectToDouble<? super K> transformer, |
5437 |
|
double basis, |
5438 |
|
DoubleByDoubleToDouble reducer) { |
5439 |
< |
super(m, p, b); this.nextRight = nextRight; |
5439 |
> |
super(p, b, i, f, t); this.nextRight = nextRight; |
5440 |
|
this.transformer = transformer; |
5441 |
|
this.basis = basis; this.reducer = reducer; |
5442 |
|
} |
5443 |
|
public final Double getRawResult() { return result; } |
5444 |
< |
@SuppressWarnings("unchecked") public final void compute() { |
5445 |
< |
final ObjectToDouble<? super K> transformer = |
5446 |
< |
this.transformer; |
5447 |
< |
final DoubleByDoubleToDouble reducer = this.reducer; |
5448 |
< |
if (transformer == null || reducer == null) |
5449 |
< |
throw new NullPointerException(); |
5450 |
< |
double r = this.basis; |
5451 |
< |
for (int b; (b = preSplit()) > 0;) |
5452 |
< |
(rights = new MapReduceKeysToDoubleTask<K,V> |
5453 |
< |
(map, this, b, rights, transformer, r, reducer)).fork(); |
5454 |
< |
while (advance() != null) |
5455 |
< |
r = reducer.apply(r, transformer.apply((K)nextKey)); |
5456 |
< |
result = r; |
5457 |
< |
CountedCompleter<?> c; |
5458 |
< |
for (c = firstComplete(); c != null; c = c.nextComplete()) { |
5459 |
< |
MapReduceKeysToDoubleTask<K,V> |
5460 |
< |
t = (MapReduceKeysToDoubleTask<K,V>)c, |
5461 |
< |
s = t.rights; |
5462 |
< |
while (s != null) { |
5463 |
< |
t.result = reducer.apply(t.result, s.result); |
5464 |
< |
s = t.rights = s.nextRight; |
5444 |
> |
public final void compute() { |
5445 |
> |
final ObjectToDouble<? super K> transformer; |
5446 |
> |
final DoubleByDoubleToDouble reducer; |
5447 |
> |
if ((transformer = this.transformer) != null && |
5448 |
> |
(reducer = this.reducer) != null) { |
5449 |
> |
double r = this.basis; |
5450 |
> |
for (int i = baseIndex, f, h; batch > 0 && |
5451 |
> |
(h = ((f = baseLimit) + i) >>> 1) > i;) { |
5452 |
> |
addToPendingCount(1); |
5453 |
> |
(rights = new MapReduceKeysToDoubleTask<K,V> |
5454 |
> |
(this, batch >>>= 1, baseLimit = h, f, tab, |
5455 |
> |
rights, transformer, r, reducer)).fork(); |
5456 |
> |
} |
5457 |
> |
for (Node<K,V> p; (p = advance()) != null; ) |
5458 |
> |
r = reducer.apply(r, transformer.apply(p.key)); |
5459 |
> |
result = r; |
5460 |
> |
CountedCompleter<?> c; |
5461 |
> |
for (c = firstComplete(); c != null; c = c.nextComplete()) { |
5462 |
> |
@SuppressWarnings("unchecked") MapReduceKeysToDoubleTask<K,V> |
5463 |
> |
t = (MapReduceKeysToDoubleTask<K,V>)c, |
5464 |
> |
s = t.rights; |
5465 |
> |
while (s != null) { |
5466 |
> |
t.result = reducer.apply(t.result, s.result); |
5467 |
> |
s = t.rights = s.nextRight; |
5468 |
> |
} |
5469 |
|
} |
5470 |
|
} |
5471 |
|
} |
5472 |
|
} |
5473 |
|
|
5474 |
< |
@SuppressWarnings("serial") static final class MapReduceValuesToDoubleTask<K,V> |
5475 |
< |
extends Traverser<K,V,Double> { |
5474 |
> |
@SuppressWarnings("serial") |
5475 |
> |
static final class MapReduceValuesToDoubleTask<K,V> |
5476 |
> |
extends BulkTask<K,V,Double> { |
5477 |
|
final ObjectToDouble<? super V> transformer; |
5478 |
|
final DoubleByDoubleToDouble reducer; |
5479 |
|
final double basis; |
5480 |
|
double result; |
5481 |
|
MapReduceValuesToDoubleTask<K,V> rights, nextRight; |
5482 |
|
MapReduceValuesToDoubleTask |
5483 |
< |
(ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b, |
5483 |
> |
(BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t, |
5484 |
|
MapReduceValuesToDoubleTask<K,V> nextRight, |
5485 |
|
ObjectToDouble<? super V> transformer, |
5486 |
|
double basis, |
5487 |
|
DoubleByDoubleToDouble reducer) { |
5488 |
< |
super(m, p, b); this.nextRight = nextRight; |
5488 |
> |
super(p, b, i, f, t); this.nextRight = nextRight; |
5489 |
|
this.transformer = transformer; |
5490 |
|
this.basis = basis; this.reducer = reducer; |
5491 |
|
} |
5492 |
|
public final Double getRawResult() { return result; } |
5493 |
< |
@SuppressWarnings("unchecked") public final void compute() { |
5494 |
< |
final ObjectToDouble<? super V> transformer = |
5495 |
< |
this.transformer; |
5496 |
< |
final DoubleByDoubleToDouble reducer = this.reducer; |
5497 |
< |
if (transformer == null || reducer == null) |
5498 |
< |
throw new NullPointerException(); |
5499 |
< |
double r = this.basis; |
5500 |
< |
for (int b; (b = preSplit()) > 0;) |
5501 |
< |
(rights = new MapReduceValuesToDoubleTask<K,V> |
5502 |
< |
(map, this, b, rights, transformer, r, reducer)).fork(); |
5503 |
< |
Object v; |
5504 |
< |
while ((v = advance()) != null) |
5505 |
< |
r = reducer.apply(r, transformer.apply((V)v)); |
5506 |
< |
result = r; |
5507 |
< |
CountedCompleter<?> c; |
5508 |
< |
for (c = firstComplete(); c != null; c = c.nextComplete()) { |
5509 |
< |
MapReduceValuesToDoubleTask<K,V> |
5510 |
< |
t = (MapReduceValuesToDoubleTask<K,V>)c, |
5511 |
< |
s = t.rights; |
5512 |
< |
while (s != null) { |
5513 |
< |
t.result = reducer.apply(t.result, s.result); |
5514 |
< |
s = t.rights = s.nextRight; |
5493 |
> |
public final void compute() { |
5494 |
> |
final ObjectToDouble<? super V> transformer; |
5495 |
> |
final DoubleByDoubleToDouble reducer; |
5496 |
> |
if ((transformer = this.transformer) != null && |
5497 |
> |
(reducer = this.reducer) != null) { |
5498 |
> |
double r = this.basis; |
5499 |
> |
for (int i = baseIndex, f, h; batch > 0 && |
5500 |
> |
(h = ((f = baseLimit) + i) >>> 1) > i;) { |
5501 |
> |
addToPendingCount(1); |
5502 |
> |
(rights = new MapReduceValuesToDoubleTask<K,V> |
5503 |
> |
(this, batch >>>= 1, baseLimit = h, f, tab, |
5504 |
> |
rights, transformer, r, reducer)).fork(); |
5505 |
> |
} |
5506 |
> |
for (Node<K,V> p; (p = advance()) != null; ) |
5507 |
> |
r = reducer.apply(r, transformer.apply(p.val)); |
5508 |
> |
result = r; |
5509 |
> |
CountedCompleter<?> c; |
5510 |
> |
for (c = firstComplete(); c != null; c = c.nextComplete()) { |
5511 |
> |
@SuppressWarnings("unchecked") MapReduceValuesToDoubleTask<K,V> |
5512 |
> |
t = (MapReduceValuesToDoubleTask<K,V>)c, |
5513 |
> |
s = t.rights; |
5514 |
> |
while (s != null) { |
5515 |
> |
t.result = reducer.apply(t.result, s.result); |
5516 |
> |
s = t.rights = s.nextRight; |
5517 |
> |
} |
5518 |
|
} |
5519 |
|
} |
5520 |
|
} |
5521 |
|
} |
5522 |
|
|
5523 |
< |
@SuppressWarnings("serial") static final class MapReduceEntriesToDoubleTask<K,V> |
5524 |
< |
extends Traverser<K,V,Double> { |
5523 |
> |
@SuppressWarnings("serial") |
5524 |
> |
static final class MapReduceEntriesToDoubleTask<K,V> |
5525 |
> |
extends BulkTask<K,V,Double> { |
5526 |
|
final ObjectToDouble<Map.Entry<K,V>> transformer; |
5527 |
|
final DoubleByDoubleToDouble reducer; |
5528 |
|
final double basis; |
5529 |
|
double result; |
5530 |
|
MapReduceEntriesToDoubleTask<K,V> rights, nextRight; |
5531 |
|
MapReduceEntriesToDoubleTask |
5532 |
< |
(ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b, |
5532 |
> |
(BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t, |
5533 |
|
MapReduceEntriesToDoubleTask<K,V> nextRight, |
5534 |
|
ObjectToDouble<Map.Entry<K,V>> transformer, |
5535 |
|
double basis, |
5536 |
|
DoubleByDoubleToDouble reducer) { |
5537 |
< |
super(m, p, b); this.nextRight = nextRight; |
5537 |
> |
super(p, b, i, f, t); this.nextRight = nextRight; |
5538 |
|
this.transformer = transformer; |
5539 |
|
this.basis = basis; this.reducer = reducer; |
5540 |
|
} |
5541 |
|
public final Double getRawResult() { return result; } |
5542 |
< |
@SuppressWarnings("unchecked") public final void compute() { |
5543 |
< |
final ObjectToDouble<Map.Entry<K,V>> transformer = |
5544 |
< |
this.transformer; |
5545 |
< |
final DoubleByDoubleToDouble reducer = this.reducer; |
5546 |
< |
if (transformer == null || reducer == null) |
5547 |
< |
throw new NullPointerException(); |
5548 |
< |
double r = this.basis; |
5549 |
< |
for (int b; (b = preSplit()) > 0;) |
5550 |
< |
(rights = new MapReduceEntriesToDoubleTask<K,V> |
5551 |
< |
(map, this, b, rights, transformer, r, reducer)).fork(); |
5552 |
< |
Object v; |
5553 |
< |
while ((v = advance()) != null) |
5554 |
< |
r = reducer.apply(r, transformer.apply(entryFor((K)nextKey, (V)v))); |
5555 |
< |
result = r; |
5556 |
< |
CountedCompleter<?> c; |
5557 |
< |
for (c = firstComplete(); c != null; c = c.nextComplete()) { |
5558 |
< |
MapReduceEntriesToDoubleTask<K,V> |
5559 |
< |
t = (MapReduceEntriesToDoubleTask<K,V>)c, |
5560 |
< |
s = t.rights; |
5561 |
< |
while (s != null) { |
5562 |
< |
t.result = reducer.apply(t.result, s.result); |
5563 |
< |
s = t.rights = s.nextRight; |
5542 |
> |
public final void compute() { |
5543 |
> |
final ObjectToDouble<Map.Entry<K,V>> transformer; |
5544 |
> |
final DoubleByDoubleToDouble reducer; |
5545 |
> |
if ((transformer = this.transformer) != null && |
5546 |
> |
(reducer = this.reducer) != null) { |
5547 |
> |
double r = this.basis; |
5548 |
> |
for (int i = baseIndex, f, h; batch > 0 && |
5549 |
> |
(h = ((f = baseLimit) + i) >>> 1) > i;) { |
5550 |
> |
addToPendingCount(1); |
5551 |
> |
(rights = new MapReduceEntriesToDoubleTask<K,V> |
5552 |
> |
(this, batch >>>= 1, baseLimit = h, f, tab, |
5553 |
> |
rights, transformer, r, reducer)).fork(); |
5554 |
> |
} |
5555 |
> |
for (Node<K,V> p; (p = advance()) != null; ) |
5556 |
> |
r = reducer.apply(r, transformer.apply(p)); |
5557 |
> |
result = r; |
5558 |
> |
CountedCompleter<?> c; |
5559 |
> |
for (c = firstComplete(); c != null; c = c.nextComplete()) { |
5560 |
> |
@SuppressWarnings("unchecked") MapReduceEntriesToDoubleTask<K,V> |
5561 |
> |
t = (MapReduceEntriesToDoubleTask<K,V>)c, |
5562 |
> |
s = t.rights; |
5563 |
> |
while (s != null) { |
5564 |
> |
t.result = reducer.apply(t.result, s.result); |
5565 |
> |
s = t.rights = s.nextRight; |
5566 |
> |
} |
5567 |
|
} |
5568 |
|
} |
5569 |
|
} |
5570 |
|
} |
5571 |
|
|
5572 |
< |
@SuppressWarnings("serial") static final class MapReduceMappingsToDoubleTask<K,V> |
5573 |
< |
extends Traverser<K,V,Double> { |
5572 |
> |
@SuppressWarnings("serial") |
5573 |
> |
static final class MapReduceMappingsToDoubleTask<K,V> |
5574 |
> |
extends BulkTask<K,V,Double> { |
5575 |
|
final ObjectByObjectToDouble<? super K, ? super V> transformer; |
5576 |
|
final DoubleByDoubleToDouble reducer; |
5577 |
|
final double basis; |
5578 |
|
double result; |
5579 |
|
MapReduceMappingsToDoubleTask<K,V> rights, nextRight; |
5580 |
|
MapReduceMappingsToDoubleTask |
5581 |
< |
(ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b, |
5581 |
> |
(BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t, |
5582 |
|
MapReduceMappingsToDoubleTask<K,V> nextRight, |
5583 |
|
ObjectByObjectToDouble<? super K, ? super V> transformer, |
5584 |
|
double basis, |
5585 |
|
DoubleByDoubleToDouble reducer) { |
5586 |
< |
super(m, p, b); this.nextRight = nextRight; |
5586 |
> |
super(p, b, i, f, t); this.nextRight = nextRight; |
5587 |
|
this.transformer = transformer; |
5588 |
|
this.basis = basis; this.reducer = reducer; |
5589 |
|
} |
5590 |
|
public final Double getRawResult() { return result; } |
5591 |
< |
@SuppressWarnings("unchecked") public final void compute() { |
5592 |
< |
final ObjectByObjectToDouble<? super K, ? super V> transformer = |
5593 |
< |
this.transformer; |
5594 |
< |
final DoubleByDoubleToDouble reducer = this.reducer; |
5595 |
< |
if (transformer == null || reducer == null) |
5596 |
< |
throw new NullPointerException(); |
5597 |
< |
double r = this.basis; |
5598 |
< |
for (int b; (b = preSplit()) > 0;) |
5599 |
< |
(rights = new MapReduceMappingsToDoubleTask<K,V> |
5600 |
< |
(map, this, b, rights, transformer, r, reducer)).fork(); |
5601 |
< |
Object v; |
5602 |
< |
while ((v = advance()) != null) |
5603 |
< |
r = reducer.apply(r, transformer.apply((K)nextKey, (V)v)); |
5604 |
< |
result = r; |
5605 |
< |
CountedCompleter<?> c; |
5606 |
< |
for (c = firstComplete(); c != null; c = c.nextComplete()) { |
5607 |
< |
MapReduceMappingsToDoubleTask<K,V> |
5608 |
< |
t = (MapReduceMappingsToDoubleTask<K,V>)c, |
5609 |
< |
s = t.rights; |
5610 |
< |
while (s != null) { |
5611 |
< |
t.result = reducer.apply(t.result, s.result); |
5612 |
< |
s = t.rights = s.nextRight; |
5591 |
> |
public final void compute() { |
5592 |
> |
final ObjectByObjectToDouble<? super K, ? super V> transformer; |
5593 |
> |
final DoubleByDoubleToDouble reducer; |
5594 |
> |
if ((transformer = this.transformer) != null && |
5595 |
> |
(reducer = this.reducer) != null) { |
5596 |
> |
double r = this.basis; |
5597 |
> |
for (int i = baseIndex, f, h; batch > 0 && |
5598 |
> |
(h = ((f = baseLimit) + i) >>> 1) > i;) { |
5599 |
> |
addToPendingCount(1); |
5600 |
> |
(rights = new MapReduceMappingsToDoubleTask<K,V> |
5601 |
> |
(this, batch >>>= 1, baseLimit = h, f, tab, |
5602 |
> |
rights, transformer, r, reducer)).fork(); |
5603 |
> |
} |
5604 |
> |
for (Node<K,V> p; (p = advance()) != null; ) |
5605 |
> |
r = reducer.apply(r, transformer.apply(p.key, p.val)); |
5606 |
> |
result = r; |
5607 |
> |
CountedCompleter<?> c; |
5608 |
> |
for (c = firstComplete(); c != null; c = c.nextComplete()) { |
5609 |
> |
@SuppressWarnings("unchecked") MapReduceMappingsToDoubleTask<K,V> |
5610 |
> |
t = (MapReduceMappingsToDoubleTask<K,V>)c, |
5611 |
> |
s = t.rights; |
5612 |
> |
while (s != null) { |
5613 |
> |
t.result = reducer.apply(t.result, s.result); |
5614 |
> |
s = t.rights = s.nextRight; |
5615 |
> |
} |
5616 |
|
} |
5617 |
|
} |
5618 |
|
} |
5619 |
|
} |
5620 |
|
|
5621 |
< |
@SuppressWarnings("serial") static final class MapReduceKeysToLongTask<K,V> |
5622 |
< |
extends Traverser<K,V,Long> { |
5621 |
> |
@SuppressWarnings("serial") |
5622 |
> |
static final class MapReduceKeysToLongTask<K,V> |
5623 |
> |
extends BulkTask<K,V,Long> { |
5624 |
|
final ObjectToLong<? super K> transformer; |
5625 |
|
final LongByLongToLong reducer; |
5626 |
|
final long basis; |
5627 |
|
long result; |
5628 |
|
MapReduceKeysToLongTask<K,V> rights, nextRight; |
5629 |
|
MapReduceKeysToLongTask |
5630 |
< |
(ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b, |
5630 |
> |
(BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t, |
5631 |
|
MapReduceKeysToLongTask<K,V> nextRight, |
5632 |
|
ObjectToLong<? super K> transformer, |
5633 |
|
long basis, |
5634 |
|
LongByLongToLong reducer) { |
5635 |
< |
super(m, p, b); this.nextRight = nextRight; |
5635 |
> |
super(p, b, i, f, t); this.nextRight = nextRight; |
5636 |
|
this.transformer = transformer; |
5637 |
|
this.basis = basis; this.reducer = reducer; |
5638 |
|
} |
5639 |
|
public final Long getRawResult() { return result; } |
5640 |
< |
@SuppressWarnings("unchecked") public final void compute() { |
5641 |
< |
final ObjectToLong<? super K> transformer = |
5642 |
< |
this.transformer; |
5643 |
< |
final LongByLongToLong reducer = this.reducer; |
5644 |
< |
if (transformer == null || reducer == null) |
5645 |
< |
throw new NullPointerException(); |
5646 |
< |
long r = this.basis; |
5647 |
< |
for (int b; (b = preSplit()) > 0;) |
5648 |
< |
(rights = new MapReduceKeysToLongTask<K,V> |
5649 |
< |
(map, this, b, rights, transformer, r, reducer)).fork(); |
5650 |
< |
while (advance() != null) |
5651 |
< |
r = reducer.apply(r, transformer.apply((K)nextKey)); |
5652 |
< |
result = r; |
5653 |
< |
CountedCompleter<?> c; |
5654 |
< |
for (c = firstComplete(); c != null; c = c.nextComplete()) { |
5655 |
< |
MapReduceKeysToLongTask<K,V> |
5656 |
< |
t = (MapReduceKeysToLongTask<K,V>)c, |
5657 |
< |
s = t.rights; |
5658 |
< |
while (s != null) { |
5659 |
< |
t.result = reducer.apply(t.result, s.result); |
5660 |
< |
s = t.rights = s.nextRight; |
5640 |
> |
public final void compute() { |
5641 |
> |
final ObjectToLong<? super K> transformer; |
5642 |
> |
final LongByLongToLong reducer; |
5643 |
> |
if ((transformer = this.transformer) != null && |
5644 |
> |
(reducer = this.reducer) != null) { |
5645 |
> |
long r = this.basis; |
5646 |
> |
for (int i = baseIndex, f, h; batch > 0 && |
5647 |
> |
(h = ((f = baseLimit) + i) >>> 1) > i;) { |
5648 |
> |
addToPendingCount(1); |
5649 |
> |
(rights = new MapReduceKeysToLongTask<K,V> |
5650 |
> |
(this, batch >>>= 1, baseLimit = h, f, tab, |
5651 |
> |
rights, transformer, r, reducer)).fork(); |
5652 |
> |
} |
5653 |
> |
for (Node<K,V> p; (p = advance()) != null; ) |
5654 |
> |
r = reducer.apply(r, transformer.apply(p.key)); |
5655 |
> |
result = r; |
5656 |
> |
CountedCompleter<?> c; |
5657 |
> |
for (c = firstComplete(); c != null; c = c.nextComplete()) { |
5658 |
> |
@SuppressWarnings("unchecked") MapReduceKeysToLongTask<K,V> |
5659 |
> |
t = (MapReduceKeysToLongTask<K,V>)c, |
5660 |
> |
s = t.rights; |
5661 |
> |
while (s != null) { |
5662 |
> |
t.result = reducer.apply(t.result, s.result); |
5663 |
> |
s = t.rights = s.nextRight; |
5664 |
> |
} |
5665 |
|
} |
5666 |
|
} |
5667 |
|
} |
5668 |
|
} |
5669 |
|
|
5670 |
< |
@SuppressWarnings("serial") static final class MapReduceValuesToLongTask<K,V> |
5671 |
< |
extends Traverser<K,V,Long> { |
5670 |
> |
@SuppressWarnings("serial") |
5671 |
> |
static final class MapReduceValuesToLongTask<K,V> |
5672 |
> |
extends BulkTask<K,V,Long> { |
5673 |
|
final ObjectToLong<? super V> transformer; |
5674 |
|
final LongByLongToLong reducer; |
5675 |
|
final long basis; |
5676 |
|
long result; |
5677 |
|
MapReduceValuesToLongTask<K,V> rights, nextRight; |
5678 |
|
MapReduceValuesToLongTask |
5679 |
< |
(ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b, |
5679 |
> |
(BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t, |
5680 |
|
MapReduceValuesToLongTask<K,V> nextRight, |
5681 |
|
ObjectToLong<? super V> transformer, |
5682 |
|
long basis, |
5683 |
|
LongByLongToLong reducer) { |
5684 |
< |
super(m, p, b); this.nextRight = nextRight; |
5684 |
> |
super(p, b, i, f, t); this.nextRight = nextRight; |
5685 |
|
this.transformer = transformer; |
5686 |
|
this.basis = basis; this.reducer = reducer; |
5687 |
|
} |
5688 |
|
public final Long getRawResult() { return result; } |
5689 |
< |
@SuppressWarnings("unchecked") public final void compute() { |
5690 |
< |
final ObjectToLong<? super V> transformer = |
5691 |
< |
this.transformer; |
5692 |
< |
final LongByLongToLong reducer = this.reducer; |
5693 |
< |
if (transformer == null || reducer == null) |
5694 |
< |
throw new NullPointerException(); |
5695 |
< |
long r = this.basis; |
5696 |
< |
for (int b; (b = preSplit()) > 0;) |
5697 |
< |
(rights = new MapReduceValuesToLongTask<K,V> |
5698 |
< |
(map, this, b, rights, transformer, r, reducer)).fork(); |
5699 |
< |
Object v; |
5700 |
< |
while ((v = advance()) != null) |
5701 |
< |
r = reducer.apply(r, transformer.apply((V)v)); |
5702 |
< |
result = r; |
5703 |
< |
CountedCompleter<?> c; |
5704 |
< |
for (c = firstComplete(); c != null; c = c.nextComplete()) { |
5705 |
< |
MapReduceValuesToLongTask<K,V> |
5706 |
< |
t = (MapReduceValuesToLongTask<K,V>)c, |
5707 |
< |
s = t.rights; |
5708 |
< |
while (s != null) { |
5709 |
< |
t.result = reducer.apply(t.result, s.result); |
5710 |
< |
s = t.rights = s.nextRight; |
5689 |
> |
public final void compute() { |
5690 |
> |
final ObjectToLong<? super V> transformer; |
5691 |
> |
final LongByLongToLong reducer; |
5692 |
> |
if ((transformer = this.transformer) != null && |
5693 |
> |
(reducer = this.reducer) != null) { |
5694 |
> |
long r = this.basis; |
5695 |
> |
for (int i = baseIndex, f, h; batch > 0 && |
5696 |
> |
(h = ((f = baseLimit) + i) >>> 1) > i;) { |
5697 |
> |
addToPendingCount(1); |
5698 |
> |
(rights = new MapReduceValuesToLongTask<K,V> |
5699 |
> |
(this, batch >>>= 1, baseLimit = h, f, tab, |
5700 |
> |
rights, transformer, r, reducer)).fork(); |
5701 |
> |
} |
5702 |
> |
for (Node<K,V> p; (p = advance()) != null; ) |
5703 |
> |
r = reducer.apply(r, transformer.apply(p.val)); |
5704 |
> |
result = r; |
5705 |
> |
CountedCompleter<?> c; |
5706 |
> |
for (c = firstComplete(); c != null; c = c.nextComplete()) { |
5707 |
> |
@SuppressWarnings("unchecked") MapReduceValuesToLongTask<K,V> |
5708 |
> |
t = (MapReduceValuesToLongTask<K,V>)c, |
5709 |
> |
s = t.rights; |
5710 |
> |
while (s != null) { |
5711 |
> |
t.result = reducer.apply(t.result, s.result); |
5712 |
> |
s = t.rights = s.nextRight; |
5713 |
> |
} |
5714 |
|
} |
5715 |
|
} |
5716 |
|
} |
5717 |
|
} |
5718 |
|
|
5719 |
< |
@SuppressWarnings("serial") static final class MapReduceEntriesToLongTask<K,V> |
5720 |
< |
extends Traverser<K,V,Long> { |
5719 |
> |
@SuppressWarnings("serial") |
5720 |
> |
static final class MapReduceEntriesToLongTask<K,V> |
5721 |
> |
extends BulkTask<K,V,Long> { |
5722 |
|
final ObjectToLong<Map.Entry<K,V>> transformer; |
5723 |
|
final LongByLongToLong reducer; |
5724 |
|
final long basis; |
5725 |
|
long result; |
5726 |
|
MapReduceEntriesToLongTask<K,V> rights, nextRight; |
5727 |
|
MapReduceEntriesToLongTask |
5728 |
< |
(ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b, |
5728 |
> |
(BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t, |
5729 |
|
MapReduceEntriesToLongTask<K,V> nextRight, |
5730 |
|
ObjectToLong<Map.Entry<K,V>> transformer, |
5731 |
|
long basis, |
5732 |
|
LongByLongToLong reducer) { |
5733 |
< |
super(m, p, b); this.nextRight = nextRight; |
5733 |
> |
super(p, b, i, f, t); this.nextRight = nextRight; |
5734 |
|
this.transformer = transformer; |
5735 |
|
this.basis = basis; this.reducer = reducer; |
5736 |
|
} |
5737 |
|
public final Long getRawResult() { return result; } |
5738 |
< |
@SuppressWarnings("unchecked") public final void compute() { |
5739 |
< |
final ObjectToLong<Map.Entry<K,V>> transformer = |
5740 |
< |
this.transformer; |
5741 |
< |
final LongByLongToLong reducer = this.reducer; |
5742 |
< |
if (transformer == null || reducer == null) |
5743 |
< |
throw new NullPointerException(); |
5744 |
< |
long r = this.basis; |
5745 |
< |
for (int b; (b = preSplit()) > 0;) |
5746 |
< |
(rights = new MapReduceEntriesToLongTask<K,V> |
5747 |
< |
(map, this, b, rights, transformer, r, reducer)).fork(); |
5748 |
< |
Object v; |
5749 |
< |
while ((v = advance()) != null) |
5750 |
< |
r = reducer.apply(r, transformer.apply(entryFor((K)nextKey, (V)v))); |
5751 |
< |
result = r; |
5752 |
< |
CountedCompleter<?> c; |
5753 |
< |
for (c = firstComplete(); c != null; c = c.nextComplete()) { |
5754 |
< |
MapReduceEntriesToLongTask<K,V> |
5755 |
< |
t = (MapReduceEntriesToLongTask<K,V>)c, |
5756 |
< |
s = t.rights; |
5757 |
< |
while (s != null) { |
5758 |
< |
t.result = reducer.apply(t.result, s.result); |
5759 |
< |
s = t.rights = s.nextRight; |
5738 |
> |
public final void compute() { |
5739 |
> |
final ObjectToLong<Map.Entry<K,V>> transformer; |
5740 |
> |
final LongByLongToLong reducer; |
5741 |
> |
if ((transformer = this.transformer) != null && |
5742 |
> |
(reducer = this.reducer) != null) { |
5743 |
> |
long r = this.basis; |
5744 |
> |
for (int i = baseIndex, f, h; batch > 0 && |
5745 |
> |
(h = ((f = baseLimit) + i) >>> 1) > i;) { |
5746 |
> |
addToPendingCount(1); |
5747 |
> |
(rights = new MapReduceEntriesToLongTask<K,V> |
5748 |
> |
(this, batch >>>= 1, baseLimit = h, f, tab, |
5749 |
> |
rights, transformer, r, reducer)).fork(); |
5750 |
> |
} |
5751 |
> |
for (Node<K,V> p; (p = advance()) != null; ) |
5752 |
> |
r = reducer.apply(r, transformer.apply(p)); |
5753 |
> |
result = r; |
5754 |
> |
CountedCompleter<?> c; |
5755 |
> |
for (c = firstComplete(); c != null; c = c.nextComplete()) { |
5756 |
> |
@SuppressWarnings("unchecked") MapReduceEntriesToLongTask<K,V> |
5757 |
> |
t = (MapReduceEntriesToLongTask<K,V>)c, |
5758 |
> |
s = t.rights; |
5759 |
> |
while (s != null) { |
5760 |
> |
t.result = reducer.apply(t.result, s.result); |
5761 |
> |
s = t.rights = s.nextRight; |
5762 |
> |
} |
5763 |
|
} |
5764 |
|
} |
5765 |
|
} |
5766 |
|
} |
5767 |
|
|
5768 |
< |
@SuppressWarnings("serial") static final class MapReduceMappingsToLongTask<K,V> |
5769 |
< |
extends Traverser<K,V,Long> { |
5768 |
> |
@SuppressWarnings("serial") |
5769 |
> |
static final class MapReduceMappingsToLongTask<K,V> |
5770 |
> |
extends BulkTask<K,V,Long> { |
5771 |
|
final ObjectByObjectToLong<? super K, ? super V> transformer; |
5772 |
|
final LongByLongToLong reducer; |
5773 |
|
final long basis; |
5774 |
|
long result; |
5775 |
|
MapReduceMappingsToLongTask<K,V> rights, nextRight; |
5776 |
|
MapReduceMappingsToLongTask |
5777 |
< |
(ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b, |
5777 |
> |
(BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t, |
5778 |
|
MapReduceMappingsToLongTask<K,V> nextRight, |
5779 |
|
ObjectByObjectToLong<? super K, ? super V> transformer, |
5780 |
|
long basis, |
5781 |
|
LongByLongToLong reducer) { |
5782 |
< |
super(m, p, b); this.nextRight = nextRight; |
5782 |
> |
super(p, b, i, f, t); this.nextRight = nextRight; |
5783 |
|
this.transformer = transformer; |
5784 |
|
this.basis = basis; this.reducer = reducer; |
5785 |
|
} |
5786 |
|
public final Long getRawResult() { return result; } |
5787 |
< |
@SuppressWarnings("unchecked") public final void compute() { |
5788 |
< |
final ObjectByObjectToLong<? super K, ? super V> transformer = |
5789 |
< |
this.transformer; |
5790 |
< |
final LongByLongToLong reducer = this.reducer; |
5791 |
< |
if (transformer == null || reducer == null) |
5792 |
< |
throw new NullPointerException(); |
5793 |
< |
long r = this.basis; |
5794 |
< |
for (int b; (b = preSplit()) > 0;) |
5795 |
< |
(rights = new MapReduceMappingsToLongTask<K,V> |
5796 |
< |
(map, this, b, rights, transformer, r, reducer)).fork(); |
5797 |
< |
Object v; |
5798 |
< |
while ((v = advance()) != null) |
5799 |
< |
r = reducer.apply(r, transformer.apply((K)nextKey, (V)v)); |
5800 |
< |
result = r; |
5801 |
< |
CountedCompleter<?> c; |
5802 |
< |
for (c = firstComplete(); c != null; c = c.nextComplete()) { |
5803 |
< |
MapReduceMappingsToLongTask<K,V> |
5804 |
< |
t = (MapReduceMappingsToLongTask<K,V>)c, |
5805 |
< |
s = t.rights; |
5806 |
< |
while (s != null) { |
5807 |
< |
t.result = reducer.apply(t.result, s.result); |
5808 |
< |
s = t.rights = s.nextRight; |
5787 |
> |
public final void compute() { |
5788 |
> |
final ObjectByObjectToLong<? super K, ? super V> transformer; |
5789 |
> |
final LongByLongToLong reducer; |
5790 |
> |
if ((transformer = this.transformer) != null && |
5791 |
> |
(reducer = this.reducer) != null) { |
5792 |
> |
long r = this.basis; |
5793 |
> |
for (int i = baseIndex, f, h; batch > 0 && |
5794 |
> |
(h = ((f = baseLimit) + i) >>> 1) > i;) { |
5795 |
> |
addToPendingCount(1); |
5796 |
> |
(rights = new MapReduceMappingsToLongTask<K,V> |
5797 |
> |
(this, batch >>>= 1, baseLimit = h, f, tab, |
5798 |
> |
rights, transformer, r, reducer)).fork(); |
5799 |
> |
} |
5800 |
> |
for (Node<K,V> p; (p = advance()) != null; ) |
5801 |
> |
r = reducer.apply(r, transformer.apply(p.key, p.val)); |
5802 |
> |
result = r; |
5803 |
> |
CountedCompleter<?> c; |
5804 |
> |
for (c = firstComplete(); c != null; c = c.nextComplete()) { |
5805 |
> |
@SuppressWarnings("unchecked") MapReduceMappingsToLongTask<K,V> |
5806 |
> |
t = (MapReduceMappingsToLongTask<K,V>)c, |
5807 |
> |
s = t.rights; |
5808 |
> |
while (s != null) { |
5809 |
> |
t.result = reducer.apply(t.result, s.result); |
5810 |
> |
s = t.rights = s.nextRight; |
5811 |
> |
} |
5812 |
|
} |
5813 |
|
} |
5814 |
|
} |
5815 |
|
} |
5816 |
|
|
5817 |
< |
@SuppressWarnings("serial") static final class MapReduceKeysToIntTask<K,V> |
5818 |
< |
extends Traverser<K,V,Integer> { |
5817 |
> |
@SuppressWarnings("serial") |
5818 |
> |
static final class MapReduceKeysToIntTask<K,V> |
5819 |
> |
extends BulkTask<K,V,Integer> { |
5820 |
|
final ObjectToInt<? super K> transformer; |
5821 |
|
final IntByIntToInt reducer; |
5822 |
|
final int basis; |
5823 |
|
int result; |
5824 |
|
MapReduceKeysToIntTask<K,V> rights, nextRight; |
5825 |
|
MapReduceKeysToIntTask |
5826 |
< |
(ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b, |
5826 |
> |
(BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t, |
5827 |
|
MapReduceKeysToIntTask<K,V> nextRight, |
5828 |
|
ObjectToInt<? super K> transformer, |
5829 |
|
int basis, |
5830 |
|
IntByIntToInt reducer) { |
5831 |
< |
super(m, p, b); this.nextRight = nextRight; |
5831 |
> |
super(p, b, i, f, t); this.nextRight = nextRight; |
5832 |
|
this.transformer = transformer; |
5833 |
|
this.basis = basis; this.reducer = reducer; |
5834 |
|
} |
5835 |
|
public final Integer getRawResult() { return result; } |
5836 |
< |
@SuppressWarnings("unchecked") public final void compute() { |
5837 |
< |
final ObjectToInt<? super K> transformer = |
5838 |
< |
this.transformer; |
5839 |
< |
final IntByIntToInt reducer = this.reducer; |
5840 |
< |
if (transformer == null || reducer == null) |
5841 |
< |
throw new NullPointerException(); |
5842 |
< |
int r = this.basis; |
5843 |
< |
for (int b; (b = preSplit()) > 0;) |
5844 |
< |
(rights = new MapReduceKeysToIntTask<K,V> |
5845 |
< |
(map, this, b, rights, transformer, r, reducer)).fork(); |
5846 |
< |
while (advance() != null) |
5847 |
< |
r = reducer.apply(r, transformer.apply((K)nextKey)); |
5848 |
< |
result = r; |
5849 |
< |
CountedCompleter<?> c; |
5850 |
< |
for (c = firstComplete(); c != null; c = c.nextComplete()) { |
5851 |
< |
MapReduceKeysToIntTask<K,V> |
5852 |
< |
t = (MapReduceKeysToIntTask<K,V>)c, |
5853 |
< |
s = t.rights; |
5854 |
< |
while (s != null) { |
5855 |
< |
t.result = reducer.apply(t.result, s.result); |
5856 |
< |
s = t.rights = s.nextRight; |
5836 |
> |
public final void compute() { |
5837 |
> |
final ObjectToInt<? super K> transformer; |
5838 |
> |
final IntByIntToInt reducer; |
5839 |
> |
if ((transformer = this.transformer) != null && |
5840 |
> |
(reducer = this.reducer) != null) { |
5841 |
> |
int r = this.basis; |
5842 |
> |
for (int i = baseIndex, f, h; batch > 0 && |
5843 |
> |
(h = ((f = baseLimit) + i) >>> 1) > i;) { |
5844 |
> |
addToPendingCount(1); |
5845 |
> |
(rights = new MapReduceKeysToIntTask<K,V> |
5846 |
> |
(this, batch >>>= 1, baseLimit = h, f, tab, |
5847 |
> |
rights, transformer, r, reducer)).fork(); |
5848 |
> |
} |
5849 |
> |
for (Node<K,V> p; (p = advance()) != null; ) |
5850 |
> |
r = reducer.apply(r, transformer.apply(p.key)); |
5851 |
> |
result = r; |
5852 |
> |
CountedCompleter<?> c; |
5853 |
> |
for (c = firstComplete(); c != null; c = c.nextComplete()) { |
5854 |
> |
@SuppressWarnings("unchecked") MapReduceKeysToIntTask<K,V> |
5855 |
> |
t = (MapReduceKeysToIntTask<K,V>)c, |
5856 |
> |
s = t.rights; |
5857 |
> |
while (s != null) { |
5858 |
> |
t.result = reducer.apply(t.result, s.result); |
5859 |
> |
s = t.rights = s.nextRight; |
5860 |
> |
} |
5861 |
|
} |
5862 |
|
} |
5863 |
|
} |
5864 |
|
} |
5865 |
|
|
5866 |
< |
@SuppressWarnings("serial") static final class MapReduceValuesToIntTask<K,V> |
5867 |
< |
extends Traverser<K,V,Integer> { |
5866 |
> |
@SuppressWarnings("serial") |
5867 |
> |
static final class MapReduceValuesToIntTask<K,V> |
5868 |
> |
extends BulkTask<K,V,Integer> { |
5869 |
|
final ObjectToInt<? super V> transformer; |
5870 |
|
final IntByIntToInt reducer; |
5871 |
|
final int basis; |
5872 |
|
int result; |
5873 |
|
MapReduceValuesToIntTask<K,V> rights, nextRight; |
5874 |
|
MapReduceValuesToIntTask |
5875 |
< |
(ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b, |
5875 |
> |
(BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t, |
5876 |
|
MapReduceValuesToIntTask<K,V> nextRight, |
5877 |
|
ObjectToInt<? super V> transformer, |
5878 |
|
int basis, |
5879 |
|
IntByIntToInt reducer) { |
5880 |
< |
super(m, p, b); this.nextRight = nextRight; |
5880 |
> |
super(p, b, i, f, t); this.nextRight = nextRight; |
5881 |
|
this.transformer = transformer; |
5882 |
|
this.basis = basis; this.reducer = reducer; |
5883 |
|
} |
5884 |
|
public final Integer getRawResult() { return result; } |
5885 |
< |
@SuppressWarnings("unchecked") public final void compute() { |
5886 |
< |
final ObjectToInt<? super V> transformer = |
5887 |
< |
this.transformer; |
5888 |
< |
final IntByIntToInt reducer = this.reducer; |
5889 |
< |
if (transformer == null || reducer == null) |
5890 |
< |
throw new NullPointerException(); |
5891 |
< |
int r = this.basis; |
5892 |
< |
for (int b; (b = preSplit()) > 0;) |
5893 |
< |
(rights = new MapReduceValuesToIntTask<K,V> |
5894 |
< |
(map, this, b, rights, transformer, r, reducer)).fork(); |
5895 |
< |
Object v; |
5896 |
< |
while ((v = advance()) != null) |
5897 |
< |
r = reducer.apply(r, transformer.apply((V)v)); |
5898 |
< |
result = r; |
5899 |
< |
CountedCompleter<?> c; |
5900 |
< |
for (c = firstComplete(); c != null; c = c.nextComplete()) { |
5901 |
< |
MapReduceValuesToIntTask<K,V> |
5902 |
< |
t = (MapReduceValuesToIntTask<K,V>)c, |
5903 |
< |
s = t.rights; |
5904 |
< |
while (s != null) { |
5905 |
< |
t.result = reducer.apply(t.result, s.result); |
5906 |
< |
s = t.rights = s.nextRight; |
5885 |
> |
public final void compute() { |
5886 |
> |
final ObjectToInt<? super V> transformer; |
5887 |
> |
final IntByIntToInt reducer; |
5888 |
> |
if ((transformer = this.transformer) != null && |
5889 |
> |
(reducer = this.reducer) != null) { |
5890 |
> |
int r = this.basis; |
5891 |
> |
for (int i = baseIndex, f, h; batch > 0 && |
5892 |
> |
(h = ((f = baseLimit) + i) >>> 1) > i;) { |
5893 |
> |
addToPendingCount(1); |
5894 |
> |
(rights = new MapReduceValuesToIntTask<K,V> |
5895 |
> |
(this, batch >>>= 1, baseLimit = h, f, tab, |
5896 |
> |
rights, transformer, r, reducer)).fork(); |
5897 |
> |
} |
5898 |
> |
for (Node<K,V> p; (p = advance()) != null; ) |
5899 |
> |
r = reducer.apply(r, transformer.apply(p.val)); |
5900 |
> |
result = r; |
5901 |
> |
CountedCompleter<?> c; |
5902 |
> |
for (c = firstComplete(); c != null; c = c.nextComplete()) { |
5903 |
> |
@SuppressWarnings("unchecked") MapReduceValuesToIntTask<K,V> |
5904 |
> |
t = (MapReduceValuesToIntTask<K,V>)c, |
5905 |
> |
s = t.rights; |
5906 |
> |
while (s != null) { |
5907 |
> |
t.result = reducer.apply(t.result, s.result); |
5908 |
> |
s = t.rights = s.nextRight; |
5909 |
> |
} |
5910 |
|
} |
5911 |
|
} |
5912 |
|
} |
5913 |
|
} |
5914 |
|
|
5915 |
< |
@SuppressWarnings("serial") static final class MapReduceEntriesToIntTask<K,V> |
5916 |
< |
extends Traverser<K,V,Integer> { |
5915 |
> |
@SuppressWarnings("serial") |
5916 |
> |
static final class MapReduceEntriesToIntTask<K,V> |
5917 |
> |
extends BulkTask<K,V,Integer> { |
5918 |
|
final ObjectToInt<Map.Entry<K,V>> transformer; |
5919 |
|
final IntByIntToInt reducer; |
5920 |
|
final int basis; |
5921 |
|
int result; |
5922 |
|
MapReduceEntriesToIntTask<K,V> rights, nextRight; |
5923 |
|
MapReduceEntriesToIntTask |
5924 |
< |
(ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b, |
5924 |
> |
(BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t, |
5925 |
|
MapReduceEntriesToIntTask<K,V> nextRight, |
5926 |
|
ObjectToInt<Map.Entry<K,V>> transformer, |
5927 |
|
int basis, |
5928 |
|
IntByIntToInt reducer) { |
5929 |
< |
super(m, p, b); this.nextRight = nextRight; |
5929 |
> |
super(p, b, i, f, t); this.nextRight = nextRight; |
5930 |
|
this.transformer = transformer; |
5931 |
|
this.basis = basis; this.reducer = reducer; |
5932 |
|
} |
5933 |
|
public final Integer getRawResult() { return result; } |
5934 |
< |
@SuppressWarnings("unchecked") public final void compute() { |
5935 |
< |
final ObjectToInt<Map.Entry<K,V>> transformer = |
5936 |
< |
this.transformer; |
5937 |
< |
final IntByIntToInt reducer = this.reducer; |
5938 |
< |
if (transformer == null || reducer == null) |
5939 |
< |
throw new NullPointerException(); |
5940 |
< |
int r = this.basis; |
5941 |
< |
for (int b; (b = preSplit()) > 0;) |
5942 |
< |
(rights = new MapReduceEntriesToIntTask<K,V> |
5943 |
< |
(map, this, b, rights, transformer, r, reducer)).fork(); |
5944 |
< |
Object v; |
5945 |
< |
while ((v = advance()) != null) |
5946 |
< |
r = reducer.apply(r, transformer.apply(entryFor((K)nextKey, (V)v))); |
5947 |
< |
result = r; |
5948 |
< |
CountedCompleter<?> c; |
5949 |
< |
for (c = firstComplete(); c != null; c = c.nextComplete()) { |
5950 |
< |
MapReduceEntriesToIntTask<K,V> |
5951 |
< |
t = (MapReduceEntriesToIntTask<K,V>)c, |
5952 |
< |
s = t.rights; |
5953 |
< |
while (s != null) { |
5954 |
< |
t.result = reducer.apply(t.result, s.result); |
5955 |
< |
s = t.rights = s.nextRight; |
5934 |
> |
public final void compute() { |
5935 |
> |
final ObjectToInt<Map.Entry<K,V>> transformer; |
5936 |
> |
final IntByIntToInt reducer; |
5937 |
> |
if ((transformer = this.transformer) != null && |
5938 |
> |
(reducer = this.reducer) != null) { |
5939 |
> |
int r = this.basis; |
5940 |
> |
for (int i = baseIndex, f, h; batch > 0 && |
5941 |
> |
(h = ((f = baseLimit) + i) >>> 1) > i;) { |
5942 |
> |
addToPendingCount(1); |
5943 |
> |
(rights = new MapReduceEntriesToIntTask<K,V> |
5944 |
> |
(this, batch >>>= 1, baseLimit = h, f, tab, |
5945 |
> |
rights, transformer, r, reducer)).fork(); |
5946 |
> |
} |
5947 |
> |
for (Node<K,V> p; (p = advance()) != null; ) |
5948 |
> |
r = reducer.apply(r, transformer.apply(p)); |
5949 |
> |
result = r; |
5950 |
> |
CountedCompleter<?> c; |
5951 |
> |
for (c = firstComplete(); c != null; c = c.nextComplete()) { |
5952 |
> |
@SuppressWarnings("unchecked") MapReduceEntriesToIntTask<K,V> |
5953 |
> |
t = (MapReduceEntriesToIntTask<K,V>)c, |
5954 |
> |
s = t.rights; |
5955 |
> |
while (s != null) { |
5956 |
> |
t.result = reducer.apply(t.result, s.result); |
5957 |
> |
s = t.rights = s.nextRight; |
5958 |
> |
} |
5959 |
|
} |
5960 |
|
} |
5961 |
|
} |
5962 |
|
} |
5963 |
|
|
5964 |
< |
@SuppressWarnings("serial") static final class MapReduceMappingsToIntTask<K,V> |
5965 |
< |
extends Traverser<K,V,Integer> { |
5964 |
> |
@SuppressWarnings("serial") |
5965 |
> |
static final class MapReduceMappingsToIntTask<K,V> |
5966 |
> |
extends BulkTask<K,V,Integer> { |
5967 |
|
final ObjectByObjectToInt<? super K, ? super V> transformer; |
5968 |
|
final IntByIntToInt reducer; |
5969 |
|
final int basis; |
5970 |
|
int result; |
5971 |
|
MapReduceMappingsToIntTask<K,V> rights, nextRight; |
5972 |
|
MapReduceMappingsToIntTask |
5973 |
< |
(ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b, |
5973 |
> |
(BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t, |
5974 |
|
MapReduceMappingsToIntTask<K,V> nextRight, |
5975 |
|
ObjectByObjectToInt<? super K, ? super V> transformer, |
5976 |
|
int basis, |
5977 |
|
IntByIntToInt reducer) { |
5978 |
< |
super(m, p, b); this.nextRight = nextRight; |
5978 |
> |
super(p, b, i, f, t); this.nextRight = nextRight; |
5979 |
|
this.transformer = transformer; |
5980 |
|
this.basis = basis; this.reducer = reducer; |
5981 |
|
} |
5982 |
|
public final Integer getRawResult() { return result; } |
5983 |
< |
@SuppressWarnings("unchecked") public final void compute() { |
5984 |
< |
final ObjectByObjectToInt<? super K, ? super V> transformer = |
5985 |
< |
this.transformer; |
5986 |
< |
final IntByIntToInt reducer = this.reducer; |
5987 |
< |
if (transformer == null || reducer == null) |
5988 |
< |
throw new NullPointerException(); |
5989 |
< |
int r = this.basis; |
5990 |
< |
for (int b; (b = preSplit()) > 0;) |
5991 |
< |
(rights = new MapReduceMappingsToIntTask<K,V> |
5992 |
< |
(map, this, b, rights, transformer, r, reducer)).fork(); |
5993 |
< |
Object v; |
5994 |
< |
while ((v = advance()) != null) |
5995 |
< |
r = reducer.apply(r, transformer.apply((K)nextKey, (V)v)); |
5996 |
< |
result = r; |
5997 |
< |
CountedCompleter<?> c; |
5998 |
< |
for (c = firstComplete(); c != null; c = c.nextComplete()) { |
5999 |
< |
MapReduceMappingsToIntTask<K,V> |
6000 |
< |
t = (MapReduceMappingsToIntTask<K,V>)c, |
6001 |
< |
s = t.rights; |
6002 |
< |
while (s != null) { |
6003 |
< |
t.result = reducer.apply(t.result, s.result); |
6004 |
< |
s = t.rights = s.nextRight; |
5983 |
> |
public final void compute() { |
5984 |
> |
final ObjectByObjectToInt<? super K, ? super V> transformer; |
5985 |
> |
final IntByIntToInt reducer; |
5986 |
> |
if ((transformer = this.transformer) != null && |
5987 |
> |
(reducer = this.reducer) != null) { |
5988 |
> |
int r = this.basis; |
5989 |
> |
for (int i = baseIndex, f, h; batch > 0 && |
5990 |
> |
(h = ((f = baseLimit) + i) >>> 1) > i;) { |
5991 |
> |
addToPendingCount(1); |
5992 |
> |
(rights = new MapReduceMappingsToIntTask<K,V> |
5993 |
> |
(this, batch >>>= 1, baseLimit = h, f, tab, |
5994 |
> |
rights, transformer, r, reducer)).fork(); |
5995 |
> |
} |
5996 |
> |
for (Node<K,V> p; (p = advance()) != null; ) |
5997 |
> |
r = reducer.apply(r, transformer.apply(p.key, p.val)); |
5998 |
> |
result = r; |
5999 |
> |
CountedCompleter<?> c; |
6000 |
> |
for (c = firstComplete(); c != null; c = c.nextComplete()) { |
6001 |
> |
@SuppressWarnings("unchecked") MapReduceMappingsToIntTask<K,V> |
6002 |
> |
t = (MapReduceMappingsToIntTask<K,V>)c, |
6003 |
> |
s = t.rights; |
6004 |
> |
while (s != null) { |
6005 |
> |
t.result = reducer.apply(t.result, s.result); |
6006 |
> |
s = t.rights = s.nextRight; |
6007 |
> |
} |
6008 |
|
} |
6009 |
|
} |
6010 |
|
} |
6011 |
|
} |
6012 |
|
|
6013 |
+ |
/* ---------------- Counters -------------- */ |
6014 |
+ |
|
6015 |
+ |
// Adapted from LongAdder and Striped64. |
6016 |
+ |
// See their internal docs for explanation. |
6017 |
+ |
|
6018 |
+ |
// A padded cell for distributing counts |
6019 |
+ |
static final class CounterCell { |
6020 |
+ |
volatile long p0, p1, p2, p3, p4, p5, p6; |
6021 |
+ |
volatile long value; |
6022 |
+ |
volatile long q0, q1, q2, q3, q4, q5, q6; |
6023 |
+ |
CounterCell(long x) { value = x; } |
6024 |
+ |
} |
6025 |
+ |
|
6026 |
+ |
/** |
6027 |
+ |
* Holder for the thread-local hash code determining which |
6028 |
+ |
* CounterCell to use. The code is initialized via the |
6029 |
+ |
* counterHashCodeGenerator, but may be moved upon collisions. |
6030 |
+ |
*/ |
6031 |
+ |
static final class CounterHashCode { |
6032 |
+ |
int code; |
6033 |
+ |
} |
6034 |
+ |
|
6035 |
+ |
/** |
6036 |
+ |
* Generates initial value for per-thread CounterHashCodes. |
6037 |
+ |
*/ |
6038 |
+ |
static final AtomicInteger counterHashCodeGenerator = new AtomicInteger(); |
6039 |
+ |
|
6040 |
+ |
/** |
6041 |
+ |
* Increment for counterHashCodeGenerator. See class ThreadLocal |
6042 |
+ |
* for explanation. |
6043 |
+ |
*/ |
6044 |
+ |
static final int SEED_INCREMENT = 0x61c88647; |
6045 |
+ |
|
6046 |
+ |
/** |
6047 |
+ |
* Per-thread counter hash codes. Shared across all instances. |
6048 |
+ |
*/ |
6049 |
+ |
static final ThreadLocal<CounterHashCode> threadCounterHashCode = |
6050 |
+ |
new ThreadLocal<CounterHashCode>(); |
6051 |
+ |
|
6052 |
+ |
|
6053 |
+ |
final long sumCount() { |
6054 |
+ |
CounterCell[] as = counterCells; CounterCell a; |
6055 |
+ |
long sum = baseCount; |
6056 |
+ |
if (as != null) { |
6057 |
+ |
for (int i = 0; i < as.length; ++i) { |
6058 |
+ |
if ((a = as[i]) != null) |
6059 |
+ |
sum += a.value; |
6060 |
+ |
} |
6061 |
+ |
} |
6062 |
+ |
return sum; |
6063 |
+ |
} |
6064 |
+ |
|
6065 |
+ |
// See LongAdder version for explanation |
6066 |
+ |
private final void fullAddCount(long x, CounterHashCode hc, |
6067 |
+ |
boolean wasUncontended) { |
6068 |
+ |
int h; |
6069 |
+ |
if (hc == null) { |
6070 |
+ |
hc = new CounterHashCode(); |
6071 |
+ |
int s = counterHashCodeGenerator.addAndGet(SEED_INCREMENT); |
6072 |
+ |
h = hc.code = (s == 0) ? 1 : s; // Avoid zero |
6073 |
+ |
threadCounterHashCode.set(hc); |
6074 |
+ |
} |
6075 |
+ |
else |
6076 |
+ |
h = hc.code; |
6077 |
+ |
boolean collide = false; // True if last slot nonempty |
6078 |
+ |
for (;;) { |
6079 |
+ |
CounterCell[] as; CounterCell a; int n; long v; |
6080 |
+ |
if ((as = counterCells) != null && (n = as.length) > 0) { |
6081 |
+ |
if ((a = as[(n - 1) & h]) == null) { |
6082 |
+ |
if (cellsBusy == 0) { // Try to attach new Cell |
6083 |
+ |
CounterCell r = new CounterCell(x); // Optimistic create |
6084 |
+ |
if (cellsBusy == 0 && |
6085 |
+ |
U.compareAndSwapInt(this, CELLSBUSY, 0, 1)) { |
6086 |
+ |
boolean created = false; |
6087 |
+ |
try { // Recheck under lock |
6088 |
+ |
CounterCell[] rs; int m, j; |
6089 |
+ |
if ((rs = counterCells) != null && |
6090 |
+ |
(m = rs.length) > 0 && |
6091 |
+ |
rs[j = (m - 1) & h] == null) { |
6092 |
+ |
rs[j] = r; |
6093 |
+ |
created = true; |
6094 |
+ |
} |
6095 |
+ |
} finally { |
6096 |
+ |
cellsBusy = 0; |
6097 |
+ |
} |
6098 |
+ |
if (created) |
6099 |
+ |
break; |
6100 |
+ |
continue; // Slot is now non-empty |
6101 |
+ |
} |
6102 |
+ |
} |
6103 |
+ |
collide = false; |
6104 |
+ |
} |
6105 |
+ |
else if (!wasUncontended) // CAS already known to fail |
6106 |
+ |
wasUncontended = true; // Continue after rehash |
6107 |
+ |
else if (U.compareAndSwapLong(a, CELLVALUE, v = a.value, v + x)) |
6108 |
+ |
break; |
6109 |
+ |
else if (counterCells != as || n >= NCPU) |
6110 |
+ |
collide = false; // At max size or stale |
6111 |
+ |
else if (!collide) |
6112 |
+ |
collide = true; |
6113 |
+ |
else if (cellsBusy == 0 && |
6114 |
+ |
U.compareAndSwapInt(this, CELLSBUSY, 0, 1)) { |
6115 |
+ |
try { |
6116 |
+ |
if (counterCells == as) {// Expand table unless stale |
6117 |
+ |
CounterCell[] rs = new CounterCell[n << 1]; |
6118 |
+ |
for (int i = 0; i < n; ++i) |
6119 |
+ |
rs[i] = as[i]; |
6120 |
+ |
counterCells = rs; |
6121 |
+ |
} |
6122 |
+ |
} finally { |
6123 |
+ |
cellsBusy = 0; |
6124 |
+ |
} |
6125 |
+ |
collide = false; |
6126 |
+ |
continue; // Retry with expanded table |
6127 |
+ |
} |
6128 |
+ |
h ^= h << 13; // Rehash |
6129 |
+ |
h ^= h >>> 17; |
6130 |
+ |
h ^= h << 5; |
6131 |
+ |
} |
6132 |
+ |
else if (cellsBusy == 0 && counterCells == as && |
6133 |
+ |
U.compareAndSwapInt(this, CELLSBUSY, 0, 1)) { |
6134 |
+ |
boolean init = false; |
6135 |
+ |
try { // Initialize table |
6136 |
+ |
if (counterCells == as) { |
6137 |
+ |
CounterCell[] rs = new CounterCell[2]; |
6138 |
+ |
rs[h & 1] = new CounterCell(x); |
6139 |
+ |
counterCells = rs; |
6140 |
+ |
init = true; |
6141 |
+ |
} |
6142 |
+ |
} finally { |
6143 |
+ |
cellsBusy = 0; |
6144 |
+ |
} |
6145 |
+ |
if (init) |
6146 |
+ |
break; |
6147 |
+ |
} |
6148 |
+ |
else if (U.compareAndSwapLong(this, BASECOUNT, v = baseCount, v + x)) |
6149 |
+ |
break; // Fall back on using base |
6150 |
+ |
} |
6151 |
+ |
hc.code = h; // Record index for next time |
6152 |
+ |
} |
6153 |
+ |
|
6154 |
|
// Unsafe mechanics |
6155 |
< |
private static final sun.misc.Unsafe UNSAFE; |
6156 |
< |
private static final long counterOffset; |
6157 |
< |
private static final long sizeCtlOffset; |
6155 |
> |
private static final sun.misc.Unsafe U; |
6156 |
> |
private static final long SIZECTL; |
6157 |
> |
private static final long TRANSFERINDEX; |
6158 |
> |
private static final long TRANSFERORIGIN; |
6159 |
> |
private static final long BASECOUNT; |
6160 |
> |
private static final long CELLSBUSY; |
6161 |
> |
private static final long CELLVALUE; |
6162 |
|
private static final long ABASE; |
6163 |
|
private static final int ASHIFT; |
6164 |
|
|
6165 |
|
static { |
6617 |
– |
int ss; |
6166 |
|
try { |
6167 |
< |
UNSAFE = getUnsafe(); |
6167 |
> |
U = getUnsafe(); |
6168 |
|
Class<?> k = ConcurrentHashMapV8.class; |
6169 |
< |
counterOffset = UNSAFE.objectFieldOffset |
6622 |
< |
(k.getDeclaredField("counter")); |
6623 |
< |
sizeCtlOffset = UNSAFE.objectFieldOffset |
6169 |
> |
SIZECTL = U.objectFieldOffset |
6170 |
|
(k.getDeclaredField("sizeCtl")); |
6171 |
< |
Class<?> sc = Node[].class; |
6172 |
< |
ABASE = UNSAFE.arrayBaseOffset(sc); |
6173 |
< |
ss = UNSAFE.arrayIndexScale(sc); |
6171 |
> |
TRANSFERINDEX = U.objectFieldOffset |
6172 |
> |
(k.getDeclaredField("transferIndex")); |
6173 |
> |
TRANSFERORIGIN = U.objectFieldOffset |
6174 |
> |
(k.getDeclaredField("transferOrigin")); |
6175 |
> |
BASECOUNT = U.objectFieldOffset |
6176 |
> |
(k.getDeclaredField("baseCount")); |
6177 |
> |
CELLSBUSY = U.objectFieldOffset |
6178 |
> |
(k.getDeclaredField("cellsBusy")); |
6179 |
> |
Class<?> ck = CounterCell.class; |
6180 |
> |
CELLVALUE = U.objectFieldOffset |
6181 |
> |
(ck.getDeclaredField("value")); |
6182 |
> |
Class<?> ak = Node[].class; |
6183 |
> |
ABASE = U.arrayBaseOffset(ak); |
6184 |
> |
int scale = U.arrayIndexScale(ak); |
6185 |
> |
if ((scale & (scale - 1)) != 0) |
6186 |
> |
throw new Error("data type scale not a power of two"); |
6187 |
> |
ASHIFT = 31 - Integer.numberOfLeadingZeros(scale); |
6188 |
|
} catch (Exception e) { |
6189 |
|
throw new Error(e); |
6190 |
|
} |
6631 |
– |
if ((ss & (ss-1)) != 0) |
6632 |
– |
throw new Error("data type scale not a power of two"); |
6633 |
– |
ASHIFT = 31 - Integer.numberOfLeadingZeros(ss); |
6191 |
|
} |
6192 |
|
|
6193 |
|
/** |
6200 |
|
private static sun.misc.Unsafe getUnsafe() { |
6201 |
|
try { |
6202 |
|
return sun.misc.Unsafe.getUnsafe(); |
6203 |
< |
} catch (SecurityException se) { |
6204 |
< |
try { |
6205 |
< |
return java.security.AccessController.doPrivileged |
6206 |
< |
(new java.security |
6207 |
< |
.PrivilegedExceptionAction<sun.misc.Unsafe>() { |
6208 |
< |
public sun.misc.Unsafe run() throws Exception { |
6209 |
< |
java.lang.reflect.Field f = sun.misc |
6210 |
< |
.Unsafe.class.getDeclaredField("theUnsafe"); |
6211 |
< |
f.setAccessible(true); |
6212 |
< |
return (sun.misc.Unsafe) f.get(null); |
6213 |
< |
}}); |
6214 |
< |
} catch (java.security.PrivilegedActionException e) { |
6215 |
< |
throw new RuntimeException("Could not initialize intrinsics", |
6216 |
< |
e.getCause()); |
6217 |
< |
} |
6203 |
> |
} catch (SecurityException tryReflectionInstead) {} |
6204 |
> |
try { |
6205 |
> |
return java.security.AccessController.doPrivileged |
6206 |
> |
(new java.security.PrivilegedExceptionAction<sun.misc.Unsafe>() { |
6207 |
> |
public sun.misc.Unsafe run() throws Exception { |
6208 |
> |
Class<sun.misc.Unsafe> k = sun.misc.Unsafe.class; |
6209 |
> |
for (java.lang.reflect.Field f : k.getDeclaredFields()) { |
6210 |
> |
f.setAccessible(true); |
6211 |
> |
Object x = f.get(null); |
6212 |
> |
if (k.isInstance(x)) |
6213 |
> |
return k.cast(x); |
6214 |
> |
} |
6215 |
> |
throw new NoSuchFieldError("the Unsafe"); |
6216 |
> |
}}); |
6217 |
> |
} catch (java.security.PrivilegedActionException e) { |
6218 |
> |
throw new RuntimeException("Could not initialize intrinsics", |
6219 |
> |
e.getCause()); |
6220 |
|
} |
6221 |
|
} |
6222 |
|
} |