ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/jsr166e/ConcurrentHashMapV8.java
Revision: 1.72
Committed: Tue Oct 30 16:05:35 2012 UTC (11 years, 6 months ago) by jsr166
Branch: MAIN
Changes since 1.71: +1 -1 lines
Log Message:
whitespace

File Contents

# User Rev Content
1 dl 1.1 /*
2     * Written by Doug Lea with assistance from members of JCP JSR-166
3     * Expert Group and released to the public domain, as explained at
4     * http://creativecommons.org/publicdomain/zero/1.0/
5     */
6    
7     package jsr166e;
8 dl 1.71
9 dl 1.52 import java.util.Comparator;
10 dl 1.24 import java.util.Arrays;
11 dl 1.1 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 dl 1.37 import java.util.concurrent.ThreadLocalRandom;
25 dl 1.24 import java.util.concurrent.locks.LockSupport;
26 dl 1.38 import java.util.concurrent.locks.AbstractQueuedSynchronizer;
27 dl 1.52 import java.util.concurrent.atomic.AtomicReference;
28    
29 dl 1.1 import java.io.Serializable;
30    
31     /**
32     * A hash table supporting full concurrency of retrievals and
33     * high expected concurrency for updates. This class obeys the
34     * same functional specification as {@link java.util.Hashtable}, and
35     * includes versions of methods corresponding to each method of
36     * {@code Hashtable}. However, even though all operations are
37     * thread-safe, retrieval operations do <em>not</em> entail locking,
38     * and there is <em>not</em> any support for locking the entire table
39     * in a way that prevents all access. This class is fully
40     * interoperable with {@code Hashtable} in programs that rely on its
41     * thread safety but not on its synchronization details.
42     *
43     * <p> Retrieval operations (including {@code get}) generally do not
44     * block, so may overlap with update operations (including {@code put}
45     * and {@code remove}). Retrievals reflect the results of the most
46     * recently <em>completed</em> update operations holding upon their
47 dl 1.59 * onset. (More formally, an update operation for a given key bears a
48     * <em>happens-before</em> relation with any (non-null) retrieval for
49     * that key reporting the updated value.) For aggregate operations
50     * such as {@code putAll} and {@code clear}, concurrent retrievals may
51     * reflect insertion or removal of only some entries. Similarly,
52     * Iterators and Enumerations return elements reflecting the state of
53     * the hash table at some point at or since the creation of the
54     * iterator/enumeration. They do <em>not</em> throw {@link
55     * ConcurrentModificationException}. However, iterators are designed
56     * to be used by only one thread at a time. Bear in mind that the
57     * results of aggregate status methods including {@code size}, {@code
58     * isEmpty}, and {@code containsValue} are typically useful only when
59     * a map is not undergoing concurrent updates in other threads.
60     * Otherwise the results of these methods reflect transient states
61     * that may be adequate for monitoring or estimation purposes, but not
62     * for program control.
63 dl 1.1 *
64 dl 1.16 * <p> The table is dynamically expanded when there are too many
65     * collisions (i.e., keys that have distinct hash codes but fall into
66     * the same slot modulo the table size), with the expected average
67 dl 1.24 * effect of maintaining roughly two bins per mapping (corresponding
68     * to a 0.75 load factor threshold for resizing). There may be much
69     * variance around this average as mappings are added and removed, but
70     * overall, this maintains a commonly accepted time/space tradeoff for
71     * hash tables. However, resizing this or any other kind of hash
72     * table may be a relatively slow operation. When possible, it is a
73     * good idea to provide a size estimate as an optional {@code
74 dl 1.16 * initialCapacity} constructor argument. An additional optional
75     * {@code loadFactor} constructor argument provides a further means of
76     * customizing initial table capacity by specifying the table density
77     * to be used in calculating the amount of space to allocate for the
78     * given number of elements. Also, for compatibility with previous
79     * versions of this class, constructors may optionally specify an
80     * expected {@code concurrencyLevel} as an additional hint for
81     * internal sizing. Note that using many keys with exactly the same
82 jsr166 1.31 * {@code hashCode()} is a sure way to slow down performance of any
83 dl 1.16 * hash table.
84 dl 1.1 *
85 dl 1.71 * <p> A {@link Set} projection of a ConcurrentHashMapV8 may be created
86 dl 1.70 * (using {@link #newKeySet()} or {@link #newKeySet(int)}), or viewed
87     * (using {@link #keySet(Object)} when only keys are of interest, and the
88     * mapped values are (perhaps transiently) not used or all take the
89     * same mapping value.
90     *
91     * <p> A ConcurrentHashMapV8 can be used as scalable frequency map (a
92     * form of histogram or multiset) by using {@link LongAdder} values
93     * and initializing via {@link #computeIfAbsent}. For example, to add
94     * a count to a {@code ConcurrentHashMapV8<String,LongAdder> freqs}, you
95     * can use {@code freqs.computeIfAbsent(k -> new
96     * LongAdder()).increment();}
97     *
98 dl 1.1 * <p>This class and its views and iterators implement all of the
99     * <em>optional</em> methods of the {@link Map} and {@link Iterator}
100     * interfaces.
101     *
102     * <p> Like {@link Hashtable} but unlike {@link HashMap}, this class
103     * does <em>not</em> allow {@code null} to be used as a key or value.
104     *
105 dl 1.70 * <p>ConcurrentHashMapV8s support parallel operations using the {@link
106     * ForkJoinPool#commonPool}. (Task that may be used in other contexts
107     * are available in class {@link ForkJoinTasks}). These operations are
108     * designed to be safely, and often sensibly, applied even with maps
109     * that are being concurrently updated by other threads; for example,
110     * when computing a snapshot summary of the values in a shared
111     * registry. There are three kinds of operation, each with four
112     * forms, accepting functions with Keys, Values, Entries, and (Key,
113     * Value) arguments and/or return values. Because the elements of a
114     * ConcurrentHashMapV8 are not ordered in any particular way, and may be
115     * processed in different orders in different parallel executions, the
116     * correctness of supplied functions should not depend on any
117     * ordering, or on any other objects or values that may transiently
118     * change while computation is in progress; and except for forEach
119     * actions, should ideally be side-effect-free.
120     *
121     * <ul>
122     * <li> forEach: Perform a given action on each element.
123     * A variant form applies a given transformation on each element
124     * before performing the action.</li>
125     *
126     * <li> search: Return the first available non-null result of
127     * applying a given function on each element; skipping further
128     * search when a result is found.</li>
129     *
130     * <li> reduce: Accumulate each element. The supplied reduction
131     * function cannot rely on ordering (more formally, it should be
132     * both associative and commutative). There are five variants:
133     *
134     * <ul>
135     *
136     * <li> Plain reductions. (There is not a form of this method for
137     * (key, value) function arguments since there is no corresponding
138     * return type.)</li>
139     *
140     * <li> Mapped reductions that accumulate the results of a given
141     * function applied to each element.</li>
142     *
143     * <li> Reductions to scalar doubles, longs, and ints, using a
144     * given basis value.</li>
145     *
146     * </li>
147     * </ul>
148     * </ul>
149     *
150     * <p>The concurrency properties of bulk operations follow
151     * from those of ConcurrentHashMapV8: Any non-null result returned
152     * from {@code get(key)} and related access methods bears a
153     * happens-before relation with the associated insertion or
154     * update. The result of any bulk operation reflects the
155     * composition of these per-element relations (but is not
156     * necessarily atomic with respect to the map as a whole unless it
157     * is somehow known to be quiescent). Conversely, because keys
158     * and values in the map are never null, null serves as a reliable
159     * atomic indicator of the current lack of any result. To
160     * maintain this property, null serves as an implicit basis for
161     * all non-scalar reduction operations. For the double, long, and
162     * int versions, the basis should be one that, when combined with
163     * any other value, returns that other value (more formally, it
164     * should be the identity element for the reduction). Most common
165     * reductions have these properties; for example, computing a sum
166     * with basis 0 or a minimum with basis MAX_VALUE.
167     *
168     * <p>Search and transformation functions provided as arguments
169     * should similarly return null to indicate the lack of any result
170     * (in which case it is not used). In the case of mapped
171     * reductions, this also enables transformations to serve as
172     * filters, returning null (or, in the case of primitive
173     * specializations, the identity basis) if the element should not
174     * be combined. You can create compound transformations and
175     * filterings by composing them yourself under this "null means
176     * there is nothing there now" rule before using them in search or
177     * reduce operations.
178     *
179     * <p>Methods accepting and/or returning Entry arguments maintain
180     * key-value associations. They may be useful for example when
181     * finding the key for the greatest value. Note that "plain" Entry
182     * arguments can be supplied using {@code new
183     * AbstractMap.SimpleEntry(k,v)}.
184     *
185     * <p> Bulk operations may complete abruptly, throwing an
186     * exception encountered in the application of a supplied
187     * function. Bear in mind when handling such exceptions that other
188     * concurrently executing functions could also have thrown
189     * exceptions, or would have done so if the first exception had
190     * not occurred.
191     *
192     * <p>Parallel speedups for bulk operations compared to sequential
193     * processing are common but not guaranteed. Operations involving
194     * brief functions on small maps may execute more slowly than
195     * sequential loops if the underlying work to parallelize the
196     * computation is more expensive than the computation
197     * itself. Similarly, parallelization may not lead to much actual
198     * parallelism if all processors are busy performing unrelated tasks.
199     *
200     * <p> All arguments to all task methods must be non-null.
201     *
202     * <p><em>jsr166e note: During transition, this class
203     * uses nested functional interfaces with different names but the
204     * same forms as those expected for JDK8.<em>
205     *
206 dl 1.1 * <p>This class is a member of the
207     * <a href="{@docRoot}/../technotes/guides/collections/index.html">
208     * Java Collections Framework</a>.
209     *
210 jsr166 1.22 * @since 1.5
211 dl 1.1 * @author Doug Lea
212     * @param <K> the type of keys maintained by this map
213     * @param <V> the type of mapped values
214     */
215     public class ConcurrentHashMapV8<K, V>
216 dl 1.52 implements ConcurrentMap<K, V>, Serializable {
217 dl 1.1 private static final long serialVersionUID = 7249069246763182397L;
218    
219     /**
220 dl 1.41 * A partitionable iterator. A Spliterator can be traversed
221     * directly, but can also be partitioned (before traversal) by
222     * creating another Spliterator that covers a non-overlapping
223     * portion of the elements, and so may be amenable to parallel
224     * execution.
225     *
226     * <p> This interface exports a subset of expected JDK8
227     * functionality.
228     *
229     * <p>Sample usage: Here is one (of the several) ways to compute
230     * the sum of the values held in a map using the ForkJoin
231     * framework. As illustrated here, Spliterators are well suited to
232     * designs in which a task repeatedly splits off half its work
233     * into forked subtasks until small enough to process directly,
234 jsr166 1.44 * and then joins these subtasks. Variants of this style can also
235     * be used in completion-based designs.
236 dl 1.41 *
237     * <pre>
238     * {@code ConcurrentHashMapV8<String, Long> m = ...
239 dl 1.52 * // split as if have 8 * parallelism, for load balance
240     * int n = m.size();
241     * int p = aForkJoinPool.getParallelism() * 8;
242     * int split = (n < p)? n : p;
243     * long sum = aForkJoinPool.invoke(new SumValues(m.valueSpliterator(), split, null));
244 dl 1.41 * // ...
245     * static class SumValues extends RecursiveTask<Long> {
246     * final Spliterator<Long> s;
247 dl 1.52 * final int split; // split while > 1
248 dl 1.41 * final SumValues nextJoin; // records forked subtasks to join
249     * SumValues(Spliterator<Long> s, int depth, SumValues nextJoin) {
250     * this.s = s; this.depth = depth; this.nextJoin = nextJoin;
251     * }
252     * public Long compute() {
253     * long sum = 0;
254     * SumValues subtasks = null; // fork subtasks
255 dl 1.52 * for (int s = split >>> 1; s > 0; s >>>= 1)
256     * (subtasks = new SumValues(s.split(), s, subtasks)).fork();
257 dl 1.41 * while (s.hasNext()) // directly process remaining elements
258     * sum += s.next();
259     * for (SumValues t = subtasks; t != null; t = t.nextJoin)
260     * sum += t.join(); // collect subtask results
261     * return sum;
262     * }
263     * }
264     * }</pre>
265     */
266     public static interface Spliterator<T> extends Iterator<T> {
267     /**
268     * Returns a Spliterator covering approximately half of the
269     * elements, guaranteed not to overlap with those subsequently
270     * returned by this Spliterator. After invoking this method,
271     * the current Spliterator will <em>not</em> produce any of
272     * the elements of the returned Spliterator, but the two
273     * Spliterators together will produce all of the elements that
274     * would have been produced by this Spliterator had this
275     * method not been called. The exact number of elements
276     * produced by the returned Spliterator is not guaranteed, and
277     * may be zero (i.e., with {@code hasNext()} reporting {@code
278     * false}) if this Spliterator cannot be further split.
279     *
280     * @return a Spliterator covering approximately half of the
281     * elements
282     * @throws IllegalStateException if this Spliterator has
283 jsr166 1.45 * already commenced traversing elements
284 dl 1.41 */
285     Spliterator<T> split();
286     }
287    
288 dl 1.70 /**
289     * A view of a ConcurrentHashMapV8 as a {@link Set} of keys, in
290     * which additions may optionally be enabled by mapping to a
291     * common value. This class cannot be directly instantiated. See
292     * {@link #keySet}, {@link #keySet(Object)}, {@link #newKeySet()},
293     * {@link #newKeySet(int)}.
294     *
295     * <p>The view's {@code iterator} is a "weakly consistent" iterator
296     * that will never throw {@link ConcurrentModificationException},
297     * and guarantees to traverse elements as they existed upon
298     * construction of the iterator, and may (but is not guaranteed to)
299     * reflect any modifications subsequent to construction.
300     */
301     public static class KeySetView<K,V> extends CHMView<K,V> implements Set<K>, java.io.Serializable {
302     private static final long serialVersionUID = 7249069246763182397L;
303     private final V value;
304     KeySetView(ConcurrentHashMapV8<K, V> map, V value) { // non-public
305     super(map);
306     this.value = value;
307     }
308    
309     /**
310     * Returns the map backing this view.
311     *
312     * @return the map backing this view
313     */
314     public ConcurrentHashMapV8<K,V> getMap() { return map; }
315    
316     /**
317     * Returns the default mapped value for additions,
318     * or {@code null} if additions are not supported.
319     *
320     * @return the default mapped value for additions, or {@code null}
321     * if not supported.
322     */
323     public V getMappedValue() { return value; }
324    
325     // implement Set API
326    
327     public boolean contains(Object o) { return map.containsKey(o); }
328     public boolean remove(Object o) { return map.remove(o) != null; }
329     public Iterator<K> iterator() { return new KeyIterator<K,V>(map); }
330     public boolean add(K e) {
331     V v;
332     if ((v = value) == null)
333     throw new UnsupportedOperationException();
334     if (e == null)
335     throw new NullPointerException();
336     return map.internalPutIfAbsent(e, v) == null;
337     }
338     public boolean addAll(Collection<? extends K> c) {
339     boolean added = false;
340     V v;
341     if ((v = value) == null)
342     throw new UnsupportedOperationException();
343     for (K e : c) {
344     if (e == null)
345     throw new NullPointerException();
346     if (map.internalPutIfAbsent(e, v) == null)
347     added = true;
348     }
349     return added;
350     }
351     public boolean equals(Object o) {
352     Set<?> c;
353     return ((o instanceof Set) &&
354     ((c = (Set<?>)o) == this ||
355     (containsAll(c) && c.containsAll(this))));
356     }
357     }
358    
359 dl 1.1 /*
360     * Overview:
361     *
362     * The primary design goal of this hash table is to maintain
363     * concurrent readability (typically method get(), but also
364     * iterators and related methods) while minimizing update
365 dl 1.24 * contention. Secondary goals are to keep space consumption about
366     * the same or better than java.util.HashMap, and to support high
367     * initial insertion rates on an empty table by many threads.
368 dl 1.1 *
369     * Each key-value mapping is held in a Node. Because Node fields
370     * can contain special values, they are defined using plain Object
371     * types. Similarly in turn, all internal methods that use them
372 dl 1.14 * work off Object types. And similarly, so do the internal
373     * methods of auxiliary iterator and view classes. All public
374     * generic typed methods relay in/out of these internal methods,
375 dl 1.27 * supplying null-checks and casts as needed. This also allows
376     * many of the public methods to be factored into a smaller number
377     * of internal methods (although sadly not so for the five
378 dl 1.38 * variants of put-related operations). The validation-based
379     * approach explained below leads to a lot of code sprawl because
380     * retry-control precludes factoring into smaller methods.
381 dl 1.1 *
382     * The table is lazily initialized to a power-of-two size upon the
383 dl 1.38 * first insertion. Each bin in the table normally contains a
384     * list of Nodes (most often, the list has only zero or one Node).
385     * Table accesses require volatile/atomic reads, writes, and
386     * CASes. Because there is no other way to arrange this without
387     * adding further indirections, we use intrinsics
388     * (sun.misc.Unsafe) operations. The lists of nodes within bins
389     * are always accurately traversable under volatile reads, so long
390     * as lookups check hash code and non-nullness of value before
391     * checking key equality.
392 dl 1.24 *
393     * We use the top two bits of Node hash fields for control
394     * purposes -- they are available anyway because of addressing
395     * constraints. As explained further below, these top bits are
396 dl 1.27 * used as follows:
397 dl 1.24 * 00 - Normal
398     * 01 - Locked
399     * 11 - Locked and may have a thread waiting for lock
400     * 10 - Node is a forwarding node
401     *
402     * The lower 30 bits of each Node's hash field contain a
403 dl 1.38 * transformation of the key's hash code, except for forwarding
404     * nodes, for which the lower bits are zero (and so always have
405     * hash field == MOVED).
406 dl 1.14 *
407 dl 1.27 * Insertion (via put or its variants) of the first node in an
408 dl 1.14 * empty bin is performed by just CASing it to the bin. This is
409 dl 1.38 * by far the most common case for put operations under most
410     * key/hash distributions. Other update operations (insert,
411     * delete, and replace) require locks. We do not want to waste
412     * the space required to associate a distinct lock object with
413     * each bin, so instead use the first node of a bin list itself as
414     * a lock. Blocking support for these locks relies on the builtin
415     * "synchronized" monitors. However, we also need a tryLock
416     * construction, so we overlay these by using bits of the Node
417     * hash field for lock control (see above), and so normally use
418     * builtin monitors only for blocking and signalling using
419     * wait/notifyAll constructions. See Node.tryAwaitLock.
420 dl 1.24 *
421     * Using the first node of a list as a lock does not by itself
422     * suffice though: When a node is locked, any update must first
423     * validate that it is still the first node after locking it, and
424     * retry if not. Because new nodes are always appended to lists,
425     * once a node is first in a bin, it remains first until deleted
426 dl 1.27 * or the bin becomes invalidated (upon resizing). However,
427     * operations that only conditionally update may inspect nodes
428     * until the point of update. This is a converse of sorts to the
429     * lazy locking technique described by Herlihy & Shavit.
430 dl 1.14 *
431 dl 1.24 * The main disadvantage of per-bin locks is that other update
432 dl 1.14 * operations on other nodes in a bin list protected by the same
433     * lock can stall, for example when user equals() or mapping
434 dl 1.38 * functions take a long time. However, statistically, under
435     * random hash codes, this is not a common problem. Ideally, the
436     * frequency of nodes in bins follows a Poisson distribution
437 dl 1.14 * (http://en.wikipedia.org/wiki/Poisson_distribution) with a
438 dl 1.16 * parameter of about 0.5 on average, given the resizing threshold
439     * of 0.75, although with a large variance because of resizing
440     * granularity. Ignoring variance, the expected occurrences of
441     * list size k are (exp(-0.5) * pow(0.5, k) / factorial(k)). The
442 dl 1.38 * first values are:
443 dl 1.16 *
444 dl 1.38 * 0: 0.60653066
445     * 1: 0.30326533
446     * 2: 0.07581633
447     * 3: 0.01263606
448     * 4: 0.00157952
449     * 5: 0.00015795
450     * 6: 0.00001316
451     * 7: 0.00000094
452     * 8: 0.00000006
453     * more: less than 1 in ten million
454 dl 1.16 *
455     * Lock contention probability for two threads accessing distinct
456 dl 1.38 * elements is roughly 1 / (8 * #elements) under random hashes.
457     *
458     * Actual hash code distributions encountered in practice
459     * sometimes deviate significantly from uniform randomness. This
460     * includes the case when N > (1<<30), so some keys MUST collide.
461     * Similarly for dumb or hostile usages in which multiple keys are
462     * designed to have identical hash codes. Also, although we guard
463     * against the worst effects of this (see method spread), sets of
464     * hashes may differ only in bits that do not impact their bin
465     * index for a given power-of-two mask. So we use a secondary
466     * strategy that applies when the number of nodes in a bin exceeds
467     * a threshold, and at least one of the keys implements
468     * Comparable. These TreeBins use a balanced tree to hold nodes
469     * (a specialized form of red-black trees), bounding search time
470     * to O(log N). Each search step in a TreeBin is around twice as
471     * slow as in a regular list, but given that N cannot exceed
472     * (1<<64) (before running out of addresses) this bounds search
473     * steps, lock hold times, etc, to reasonable constants (roughly
474     * 100 nodes inspected per operation worst case) so long as keys
475     * are Comparable (which is very common -- String, Long, etc).
476     * TreeBin nodes (TreeNodes) also maintain the same "next"
477     * traversal pointers as regular nodes, so can be traversed in
478     * iterators in the same way.
479 dl 1.1 *
480 dl 1.38 * The table is resized when occupancy exceeds a percentage
481 dl 1.24 * threshold (nominally, 0.75, but see below). Only a single
482     * thread performs the resize (using field "sizeCtl", to arrange
483     * exclusion), but the table otherwise remains usable for reads
484     * and updates. Resizing proceeds by transferring bins, one by
485     * one, from the table to the next table. Because we are using
486     * power-of-two expansion, the elements from each bin must either
487     * stay at same index, or move with a power of two offset. We
488     * eliminate unnecessary node creation by catching cases where old
489     * nodes can be reused because their next fields won't change. On
490     * average, only about one-sixth of them need cloning when a table
491     * doubles. The nodes they replace will be garbage collectable as
492     * soon as they are no longer referenced by any reader thread that
493     * may be in the midst of concurrently traversing table. Upon
494     * transfer, the old table bin contains only a special forwarding
495     * node (with hash field "MOVED") that contains the next table as
496     * its key. On encountering a forwarding node, access and update
497     * operations restart, using the new table.
498     *
499     * Each bin transfer requires its bin lock. However, unlike other
500     * cases, a transfer can skip a bin if it fails to acquire its
501 dl 1.38 * lock, and revisit it later (unless it is a TreeBin). Method
502     * rebuild maintains a buffer of TRANSFER_BUFFER_SIZE bins that
503     * have been skipped because of failure to acquire a lock, and
504     * blocks only if none are available (i.e., only very rarely).
505     * The transfer operation must also ensure that all accessible
506     * bins in both the old and new table are usable by any traversal.
507     * When there are no lock acquisition failures, this is arranged
508     * simply by proceeding from the last bin (table.length - 1) up
509     * towards the first. Upon seeing a forwarding node, traversals
510 dl 1.52 * (see class Iter) arrange to move to the new table
511 dl 1.38 * without revisiting nodes. However, when any node is skipped
512     * during a transfer, all earlier table bins may have become
513     * visible, so are initialized with a reverse-forwarding node back
514     * to the old table until the new ones are established. (This
515     * sometimes requires transiently locking a forwarding node, which
516     * is possible under the above encoding.) These more expensive
517 dl 1.24 * mechanics trigger only when necessary.
518 dl 1.14 *
519 dl 1.24 * The traversal scheme also applies to partial traversals of
520 dl 1.52 * ranges of bins (via an alternate Traverser constructor)
521 dl 1.41 * to support partitioned aggregate operations. Also, read-only
522     * operations give up if ever forwarded to a null table, which
523     * provides support for shutdown-style clearing, which is also not
524     * currently implemented.
525 dl 1.14 *
526     * Lazy table initialization minimizes footprint until first use,
527     * and also avoids resizings when the first operation is from a
528     * putAll, constructor with map argument, or deserialization.
529 dl 1.24 * These cases attempt to override the initial capacity settings,
530     * but harmlessly fail to take effect in cases of races.
531 dl 1.1 *
532     * The element count is maintained using a LongAdder, which avoids
533     * contention on updates but can encounter cache thrashing if read
534 dl 1.14 * too frequently during concurrent access. To avoid reading so
535 dl 1.27 * often, resizing is attempted either when a bin lock is
536     * contended, or upon adding to a bin already holding two or more
537     * nodes (checked before adding in the xIfAbsent methods, after
538     * adding in others). Under uniform hash distributions, the
539     * probability of this occurring at threshold is around 13%,
540     * meaning that only about 1 in 8 puts check threshold (and after
541     * resizing, many fewer do so). But this approximation has high
542     * variance for small table sizes, so we check on any collision
543     * for sizes <= 64. The bulk putAll operation further reduces
544     * contention by only committing count updates upon these size
545     * checks.
546 dl 1.14 *
547     * Maintaining API and serialization compatibility with previous
548     * versions of this class introduces several oddities. Mainly: We
549     * leave untouched but unused constructor arguments refering to
550 dl 1.24 * concurrencyLevel. We accept a loadFactor constructor argument,
551     * but apply it only to initial table capacity (which is the only
552     * time that we can guarantee to honor it.) We also declare an
553     * unused "Segment" class that is instantiated in minimal form
554     * only when serializing.
555 dl 1.1 */
556    
557     /* ---------------- Constants -------------- */
558    
559     /**
560 dl 1.16 * The largest possible table capacity. This value must be
561     * exactly 1<<30 to stay within Java array allocation and indexing
562 dl 1.24 * bounds for power of two table sizes, and is further required
563     * because the top two bits of 32bit hash fields are used for
564     * control purposes.
565 dl 1.1 */
566 dl 1.14 private static final int MAXIMUM_CAPACITY = 1 << 30;
567 dl 1.1
568     /**
569 dl 1.14 * The default initial table capacity. Must be a power of 2
570     * (i.e., at least 1) and at most MAXIMUM_CAPACITY.
571 dl 1.1 */
572 dl 1.14 private static final int DEFAULT_CAPACITY = 16;
573 dl 1.1
574     /**
575 dl 1.24 * The largest possible (non-power of two) array size.
576     * Needed by toArray and related methods.
577     */
578     static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
579    
580     /**
581     * The default concurrency level for this table. Unused but
582     * defined for compatibility with previous versions of this class.
583     */
584     private static final int DEFAULT_CONCURRENCY_LEVEL = 16;
585    
586     /**
587 dl 1.16 * The load factor for this table. Overrides of this value in
588     * constructors affect only the initial table capacity. The
589 dl 1.24 * actual floating point value isn't normally used -- it is
590     * simpler to use expressions such as {@code n - (n >>> 2)} for
591     * the associated resizing threshold.
592 dl 1.1 */
593 dl 1.16 private static final float LOAD_FACTOR = 0.75f;
594 dl 1.1
595     /**
596 dl 1.24 * The buffer size for skipped bins during transfers. The
597     * value is arbitrary but should be large enough to avoid
598     * most locking stalls during resizes.
599     */
600     private static final int TRANSFER_BUFFER_SIZE = 32;
601    
602 dl 1.38 /**
603     * The bin count threshold for using a tree rather than list for a
604     * bin. The value reflects the approximate break-even point for
605     * using tree-based operations.
606     */
607     private static final int TREE_THRESHOLD = 8;
608    
609 dl 1.24 /*
610     * Encodings for special uses of Node hash fields. See above for
611     * explanation.
612 dl 1.1 */
613 jsr166 1.35 static final int MOVED = 0x80000000; // hash field for forwarding nodes
614 dl 1.24 static final int LOCKED = 0x40000000; // set/tested only as a bit
615     static final int WAITING = 0xc0000000; // both bits set/tested together
616     static final int HASH_BITS = 0x3fffffff; // usable bits of normal node hash
617    
618     /* ---------------- Fields -------------- */
619    
620     /**
621     * The array of bins. Lazily initialized upon first insertion.
622     * Size is always a power of two. Accessed directly by iterators.
623     */
624     transient volatile Node[] table;
625 dl 1.14
626 dl 1.16 /**
627 dl 1.24 * The counter maintaining number of elements.
628 dl 1.16 */
629 dl 1.24 private transient final LongAdder counter;
630    
631     /**
632     * Table initialization and resizing control. When negative, the
633     * table is being initialized or resized. Otherwise, when table is
634     * null, holds the initial table size to use upon creation, or 0
635     * for default. After initialization, holds the next element count
636     * value upon which to resize the table.
637     */
638     private transient volatile int sizeCtl;
639    
640     // views
641 dl 1.70 private transient KeySetView<K,V> keySet;
642 dl 1.24 private transient Values<K,V> values;
643     private transient EntrySet<K,V> entrySet;
644    
645     /** For serialization compatibility. Null unless serialized; see below */
646     private Segment<K,V>[] segments;
647 dl 1.16
648 dl 1.38 /* ---------------- Table element access -------------- */
649    
650     /*
651     * Volatile access methods are used for table elements as well as
652     * elements of in-progress next table while resizing. Uses are
653     * null checked by callers, and implicitly bounds-checked, relying
654     * on the invariants that tab arrays have non-zero size, and all
655     * indices are masked with (tab.length - 1) which is never
656     * negative and always less than length. Note that, to be correct
657     * wrt arbitrary concurrency errors by users, bounds checks must
658     * operate on local variables, which accounts for some odd-looking
659     * inline assignments below.
660     */
661    
662 dl 1.52 static final Node tabAt(Node[] tab, int i) { // used by Iter
663 dl 1.38 return (Node)UNSAFE.getObjectVolatile(tab, ((long)i<<ASHIFT)+ABASE);
664     }
665    
666     private static final boolean casTabAt(Node[] tab, int i, Node c, Node v) {
667     return UNSAFE.compareAndSwapObject(tab, ((long)i<<ASHIFT)+ABASE, c, v);
668     }
669    
670     private static final void setTabAt(Node[] tab, int i, Node v) {
671     UNSAFE.putObjectVolatile(tab, ((long)i<<ASHIFT)+ABASE, v);
672     }
673    
674 dl 1.14 /* ---------------- Nodes -------------- */
675 dl 1.1
676     /**
677 dl 1.14 * Key-value entry. Note that this is never exported out as a
678 dl 1.41 * user-visible Map.Entry (see MapEntry below). Nodes with a hash
679     * field of MOVED are special, and do not contain user keys or
680     * values. Otherwise, keys are never null, and null val fields
681     * indicate that a node is in the process of being deleted or
682     * created. For purposes of read-only access, a key may be read
683     * before a val, but can only be used after checking val to be
684     * non-null.
685 dl 1.1 */
686 dl 1.38 static class Node {
687 dl 1.24 volatile int hash;
688 dl 1.14 final Object key;
689     volatile Object val;
690     volatile Node next;
691    
692     Node(int hash, Object key, Object val, Node next) {
693     this.hash = hash;
694     this.key = key;
695     this.val = val;
696     this.next = next;
697     }
698    
699 dl 1.24 /** CompareAndSet the hash field */
700     final boolean casHash(int cmp, int val) {
701     return UNSAFE.compareAndSwapInt(this, hashOffset, cmp, val);
702     }
703 dl 1.1
704 dl 1.24 /** The number of spins before blocking for a lock */
705     static final int MAX_SPINS =
706     Runtime.getRuntime().availableProcessors() > 1 ? 64 : 1;
707 dl 1.1
708 dl 1.24 /**
709     * Spins a while if LOCKED bit set and this node is the first
710     * of its bin, and then sets WAITING bits on hash field and
711     * blocks (once) if they are still set. It is OK for this
712     * method to return even if lock is not available upon exit,
713     * which enables these simple single-wait mechanics.
714     *
715     * The corresponding signalling operation is performed within
716     * callers: Upon detecting that WAITING has been set when
717     * unlocking lock (via a failed CAS from non-waiting LOCKED
718     * state), unlockers acquire the sync lock and perform a
719     * notifyAll.
720 dl 1.61 *
721     * The initial sanity check on tab and bounds is not currently
722     * necessary in the only usages of this method, but enables
723     * use in other future contexts.
724 dl 1.24 */
725     final void tryAwaitLock(Node[] tab, int i) {
726 dl 1.61 if (tab != null && i >= 0 && i < tab.length) { // sanity check
727 dl 1.37 int r = ThreadLocalRandom.current().nextInt(); // randomize spins
728 dl 1.24 int spins = MAX_SPINS, h;
729     while (tabAt(tab, i) == this && ((h = hash) & LOCKED) != 0) {
730     if (spins >= 0) {
731 dl 1.37 r ^= r << 1; r ^= r >>> 3; r ^= r << 10; // xorshift
732     if (r >= 0 && --spins == 0)
733     Thread.yield(); // yield before block
734 dl 1.24 }
735     else if (casHash(h, h | WAITING)) {
736 jsr166 1.26 synchronized (this) {
737 dl 1.24 if (tabAt(tab, i) == this &&
738     (hash & WAITING) == WAITING) {
739     try {
740     wait();
741     } catch (InterruptedException ie) {
742     Thread.currentThread().interrupt();
743     }
744     }
745     else
746     notifyAll(); // possibly won race vs signaller
747     }
748     break;
749     }
750     }
751     }
752     }
753 dl 1.1
754 dl 1.24 // Unsafe mechanics for casHash
755     private static final sun.misc.Unsafe UNSAFE;
756     private static final long hashOffset;
757 dl 1.1
758 dl 1.24 static {
759     try {
760     UNSAFE = getUnsafe();
761     Class<?> k = Node.class;
762     hashOffset = UNSAFE.objectFieldOffset
763     (k.getDeclaredField("hash"));
764     } catch (Exception e) {
765     throw new Error(e);
766     }
767     }
768     }
769 dl 1.1
770 dl 1.38 /* ---------------- TreeBins -------------- */
771    
772     /**
773     * Nodes for use in TreeBins
774     */
775     static final class TreeNode extends Node {
776     TreeNode parent; // red-black tree links
777     TreeNode left;
778     TreeNode right;
779     TreeNode prev; // needed to unlink next upon deletion
780     boolean red;
781    
782     TreeNode(int hash, Object key, Object val, Node next, TreeNode parent) {
783     super(hash, key, val, next);
784     this.parent = parent;
785     }
786     }
787 dl 1.1
788 dl 1.38 /**
789     * A specialized form of red-black tree for use in bins
790     * whose size exceeds a threshold.
791     *
792     * TreeBins use a special form of comparison for search and
793     * related operations (which is the main reason we cannot use
794     * existing collections such as TreeMaps). TreeBins contain
795     * Comparable elements, but may contain others, as well as
796     * elements that are Comparable but not necessarily Comparable<T>
797     * for the same T, so we cannot invoke compareTo among them. To
798     * handle this, the tree is ordered primarily by hash value, then
799     * by getClass().getName() order, and then by Comparator order
800     * among elements of the same class. On lookup at a node, if
801 dl 1.41 * elements are not comparable or compare as 0, both left and
802     * right children may need to be searched in the case of tied hash
803     * values. (This corresponds to the full list search that would be
804     * necessary if all elements were non-Comparable and had tied
805     * hashes.) The red-black balancing code is updated from
806     * pre-jdk-collections
807     * (http://gee.cs.oswego.edu/dl/classes/collections/RBCell.java)
808     * based in turn on Cormen, Leiserson, and Rivest "Introduction to
809     * Algorithms" (CLR).
810 dl 1.38 *
811     * TreeBins also maintain a separate locking discipline than
812     * regular bins. Because they are forwarded via special MOVED
813     * nodes at bin heads (which can never change once established),
814 jsr166 1.51 * we cannot use those nodes as locks. Instead, TreeBin
815 dl 1.38 * extends AbstractQueuedSynchronizer to support a simple form of
816     * read-write lock. For update operations and table validation,
817     * the exclusive form of lock behaves in the same way as bin-head
818     * locks. However, lookups use shared read-lock mechanics to allow
819     * multiple readers in the absence of writers. Additionally,
820     * these lookups do not ever block: While the lock is not
821     * available, they proceed along the slow traversal path (via
822     * next-pointers) until the lock becomes available or the list is
823     * exhausted, whichever comes first. (These cases are not fast,
824     * but maximize aggregate expected throughput.) The AQS mechanics
825     * for doing this are straightforward. The lock state is held as
826     * AQS getState(). Read counts are negative; the write count (1)
827     * is positive. There are no signalling preferences among readers
828     * and writers. Since we don't need to export full Lock API, we
829     * just override the minimal AQS methods and use them directly.
830 dl 1.1 */
831 dl 1.38 static final class TreeBin extends AbstractQueuedSynchronizer {
832     private static final long serialVersionUID = 2249069246763182397L;
833 dl 1.41 transient TreeNode root; // root of tree
834     transient TreeNode first; // head of next-pointer list
835 dl 1.1
836 dl 1.38 /* AQS overrides */
837     public final boolean isHeldExclusively() { return getState() > 0; }
838     public final boolean tryAcquire(int ignore) {
839     if (compareAndSetState(0, 1)) {
840     setExclusiveOwnerThread(Thread.currentThread());
841     return true;
842     }
843     return false;
844     }
845     public final boolean tryRelease(int ignore) {
846     setExclusiveOwnerThread(null);
847     setState(0);
848     return true;
849     }
850     public final int tryAcquireShared(int ignore) {
851     for (int c;;) {
852     if ((c = getState()) > 0)
853     return -1;
854     if (compareAndSetState(c, c -1))
855     return 1;
856     }
857     }
858     public final boolean tryReleaseShared(int ignore) {
859     int c;
860     do {} while (!compareAndSetState(c = getState(), c + 1));
861     return c == -1;
862     }
863    
864 dl 1.41 /** From CLR */
865     private void rotateLeft(TreeNode p) {
866     if (p != null) {
867     TreeNode r = p.right, pp, rl;
868     if ((rl = p.right = r.left) != null)
869     rl.parent = p;
870     if ((pp = r.parent = p.parent) == null)
871     root = r;
872     else if (pp.left == p)
873     pp.left = r;
874     else
875     pp.right = r;
876     r.left = p;
877     p.parent = r;
878     }
879     }
880    
881     /** From CLR */
882     private void rotateRight(TreeNode p) {
883     if (p != null) {
884     TreeNode l = p.left, pp, lr;
885     if ((lr = p.left = l.right) != null)
886     lr.parent = p;
887     if ((pp = l.parent = p.parent) == null)
888     root = l;
889     else if (pp.right == p)
890     pp.right = l;
891     else
892     pp.left = l;
893     l.right = p;
894     p.parent = l;
895     }
896     }
897    
898 dl 1.38 /**
899 jsr166 1.56 * Returns the TreeNode (or null if not found) for the given key
900 dl 1.38 * starting at given root.
901     */
902 dl 1.61 @SuppressWarnings("unchecked") final TreeNode getTreeNode
903     (int h, Object k, TreeNode p) {
904 dl 1.38 Class<?> c = k.getClass();
905     while (p != null) {
906 dl 1.41 int dir, ph; Object pk; Class<?> pc;
907     if ((ph = p.hash) == h) {
908     if ((pk = p.key) == k || k.equals(pk))
909     return p;
910     if (c != (pc = pk.getClass()) ||
911     !(k instanceof Comparable) ||
912     (dir = ((Comparable)k).compareTo((Comparable)pk)) == 0) {
913 jsr166 1.42 dir = (c == pc) ? 0 : c.getName().compareTo(pc.getName());
914 dl 1.41 TreeNode r = null, s = null, pl, pr;
915     if (dir >= 0) {
916     if ((pl = p.left) != null && h <= pl.hash)
917     s = pl;
918     }
919     else if ((pr = p.right) != null && h >= pr.hash)
920     s = pr;
921     if (s != null && (r = getTreeNode(h, k, s)) != null)
922     return r;
923     }
924     }
925 dl 1.38 else
926 dl 1.41 dir = (h < ph) ? -1 : 1;
927     p = (dir > 0) ? p.right : p.left;
928 dl 1.38 }
929     return null;
930     }
931    
932     /**
933     * Wrapper for getTreeNode used by CHM.get. Tries to obtain
934     * read-lock to call getTreeNode, but during failure to get
935     * lock, searches along next links.
936     */
937     final Object getValue(int h, Object k) {
938     Node r = null;
939     int c = getState(); // Must read lock state first
940     for (Node e = first; e != null; e = e.next) {
941     if (c <= 0 && compareAndSetState(c, c - 1)) {
942     try {
943     r = getTreeNode(h, k, root);
944     } finally {
945     releaseShared(0);
946     }
947     break;
948     }
949     else if ((e.hash & HASH_BITS) == h && k.equals(e.key)) {
950     r = e;
951     break;
952     }
953     else
954     c = getState();
955     }
956     return r == null ? null : r.val;
957     }
958    
959     /**
960 jsr166 1.45 * Finds or adds a node.
961 dl 1.38 * @return null if added
962     */
963 dl 1.61 @SuppressWarnings("unchecked") final TreeNode putTreeNode
964     (int h, Object k, Object v) {
965 dl 1.38 Class<?> c = k.getClass();
966 dl 1.41 TreeNode pp = root, p = null;
967 dl 1.38 int dir = 0;
968 dl 1.41 while (pp != null) { // find existing node or leaf to insert at
969     int ph; Object pk; Class<?> pc;
970     p = pp;
971     if ((ph = p.hash) == h) {
972     if ((pk = p.key) == k || k.equals(pk))
973 dl 1.38 return p;
974 dl 1.41 if (c != (pc = pk.getClass()) ||
975     !(k instanceof Comparable) ||
976     (dir = ((Comparable)k).compareTo((Comparable)pk)) == 0) {
977 jsr166 1.42 dir = (c == pc) ? 0 : c.getName().compareTo(pc.getName());
978 dl 1.41 TreeNode r = null, s = null, pl, pr;
979     if (dir >= 0) {
980     if ((pl = p.left) != null && h <= pl.hash)
981     s = pl;
982     }
983     else if ((pr = p.right) != null && h >= pr.hash)
984     s = pr;
985     if (s != null && (r = getTreeNode(h, k, s)) != null)
986     return r;
987 dl 1.38 }
988     }
989 dl 1.41 else
990     dir = (h < ph) ? -1 : 1;
991     pp = (dir > 0) ? p.right : p.left;
992 dl 1.38 }
993 dl 1.41
994 dl 1.38 TreeNode f = first;
995 dl 1.41 TreeNode x = first = new TreeNode(h, k, v, f, p);
996 dl 1.38 if (p == null)
997 dl 1.41 root = x;
998     else { // attach and rebalance; adapted from CLR
999     TreeNode xp, xpp;
1000     if (f != null)
1001     f.prev = x;
1002 dl 1.38 if (dir <= 0)
1003 dl 1.41 p.left = x;
1004 dl 1.38 else
1005 dl 1.41 p.right = x;
1006     x.red = true;
1007     while (x != null && (xp = x.parent) != null && xp.red &&
1008     (xpp = xp.parent) != null) {
1009     TreeNode xppl = xpp.left;
1010     if (xp == xppl) {
1011     TreeNode y = xpp.right;
1012     if (y != null && y.red) {
1013     y.red = false;
1014     xp.red = false;
1015     xpp.red = true;
1016     x = xpp;
1017     }
1018     else {
1019     if (x == xp.right) {
1020     rotateLeft(x = xp);
1021     xpp = (xp = x.parent) == null ? null : xp.parent;
1022     }
1023     if (xp != null) {
1024     xp.red = false;
1025     if (xpp != null) {
1026     xpp.red = true;
1027     rotateRight(xpp);
1028     }
1029     }
1030     }
1031     }
1032     else {
1033     TreeNode y = xppl;
1034     if (y != null && y.red) {
1035     y.red = false;
1036     xp.red = false;
1037     xpp.red = true;
1038     x = xpp;
1039     }
1040     else {
1041     if (x == xp.left) {
1042     rotateRight(x = xp);
1043     xpp = (xp = x.parent) == null ? null : xp.parent;
1044     }
1045     if (xp != null) {
1046     xp.red = false;
1047     if (xpp != null) {
1048     xpp.red = true;
1049     rotateLeft(xpp);
1050     }
1051     }
1052     }
1053     }
1054     }
1055     TreeNode r = root;
1056     if (r != null && r.red)
1057     r.red = false;
1058 dl 1.38 }
1059     return null;
1060     }
1061 dl 1.1
1062 dl 1.38 /**
1063     * Removes the given node, that must be present before this
1064     * call. This is messier than typical red-black deletion code
1065     * because we cannot swap the contents of an interior node
1066     * with a leaf successor that is pinned by "next" pointers
1067     * that are accessible independently of lock. So instead we
1068     * swap the tree linkages.
1069     */
1070     final void deleteTreeNode(TreeNode p) {
1071     TreeNode next = (TreeNode)p.next; // unlink traversal pointers
1072     TreeNode pred = p.prev;
1073     if (pred == null)
1074     first = next;
1075     else
1076     pred.next = next;
1077     if (next != null)
1078     next.prev = pred;
1079     TreeNode replacement;
1080     TreeNode pl = p.left;
1081     TreeNode pr = p.right;
1082     if (pl != null && pr != null) {
1083 dl 1.41 TreeNode s = pr, sl;
1084     while ((sl = s.left) != null) // find successor
1085     s = sl;
1086 dl 1.38 boolean c = s.red; s.red = p.red; p.red = c; // swap colors
1087     TreeNode sr = s.right;
1088     TreeNode pp = p.parent;
1089     if (s == pr) { // p was s's direct parent
1090     p.parent = s;
1091     s.right = p;
1092     }
1093     else {
1094     TreeNode sp = s.parent;
1095     if ((p.parent = sp) != null) {
1096     if (s == sp.left)
1097     sp.left = p;
1098     else
1099     sp.right = p;
1100     }
1101     if ((s.right = pr) != null)
1102     pr.parent = s;
1103     }
1104     p.left = null;
1105     if ((p.right = sr) != null)
1106     sr.parent = p;
1107     if ((s.left = pl) != null)
1108     pl.parent = s;
1109     if ((s.parent = pp) == null)
1110     root = s;
1111     else if (p == pp.left)
1112     pp.left = s;
1113     else
1114     pp.right = s;
1115     replacement = sr;
1116     }
1117     else
1118     replacement = (pl != null) ? pl : pr;
1119     TreeNode pp = p.parent;
1120     if (replacement == null) {
1121     if (pp == null) {
1122     root = null;
1123     return;
1124     }
1125     replacement = p;
1126     }
1127     else {
1128     replacement.parent = pp;
1129     if (pp == null)
1130     root = replacement;
1131     else if (p == pp.left)
1132     pp.left = replacement;
1133     else
1134     pp.right = replacement;
1135     p.left = p.right = p.parent = null;
1136     }
1137 dl 1.41 if (!p.red) { // rebalance, from CLR
1138     TreeNode x = replacement;
1139     while (x != null) {
1140     TreeNode xp, xpl;
1141     if (x.red || (xp = x.parent) == null) {
1142     x.red = false;
1143     break;
1144 dl 1.38 }
1145 dl 1.41 if (x == (xpl = xp.left)) {
1146     TreeNode sib = xp.right;
1147     if (sib != null && sib.red) {
1148     sib.red = false;
1149     xp.red = true;
1150     rotateLeft(xp);
1151     sib = (xp = x.parent) == null ? null : xp.right;
1152 dl 1.38 }
1153 dl 1.41 if (sib == null)
1154 dl 1.38 x = xp;
1155     else {
1156 dl 1.41 TreeNode sl = sib.left, sr = sib.right;
1157     if ((sr == null || !sr.red) &&
1158     (sl == null || !sl.red)) {
1159 dl 1.38 sib.red = true;
1160 dl 1.41 x = xp;
1161 dl 1.38 }
1162 dl 1.41 else {
1163     if (sr == null || !sr.red) {
1164     if (sl != null)
1165     sl.red = false;
1166     sib.red = true;
1167     rotateRight(sib);
1168     sib = (xp = x.parent) == null ? null : xp.right;
1169     }
1170     if (sib != null) {
1171 jsr166 1.42 sib.red = (xp == null) ? false : xp.red;
1172 dl 1.41 if ((sr = sib.right) != null)
1173     sr.red = false;
1174     }
1175     if (xp != null) {
1176     xp.red = false;
1177     rotateLeft(xp);
1178     }
1179     x = root;
1180 dl 1.38 }
1181     }
1182     }
1183 dl 1.41 else { // symmetric
1184     TreeNode sib = xpl;
1185     if (sib != null && sib.red) {
1186     sib.red = false;
1187     xp.red = true;
1188     rotateRight(xp);
1189     sib = (xp = x.parent) == null ? null : xp.left;
1190     }
1191     if (sib == null)
1192 dl 1.38 x = xp;
1193     else {
1194 dl 1.41 TreeNode sl = sib.left, sr = sib.right;
1195     if ((sl == null || !sl.red) &&
1196     (sr == null || !sr.red)) {
1197 dl 1.38 sib.red = true;
1198 dl 1.41 x = xp;
1199 dl 1.38 }
1200 dl 1.41 else {
1201     if (sl == null || !sl.red) {
1202     if (sr != null)
1203     sr.red = false;
1204     sib.red = true;
1205     rotateLeft(sib);
1206     sib = (xp = x.parent) == null ? null : xp.left;
1207     }
1208     if (sib != null) {
1209 jsr166 1.42 sib.red = (xp == null) ? false : xp.red;
1210 dl 1.41 if ((sl = sib.left) != null)
1211     sl.red = false;
1212     }
1213     if (xp != null) {
1214     xp.red = false;
1215     rotateRight(xp);
1216     }
1217     x = root;
1218 dl 1.38 }
1219     }
1220     }
1221     }
1222     }
1223 dl 1.41 if (p == replacement && (pp = p.parent) != null) {
1224     if (p == pp.left) // detach pointers
1225     pp.left = null;
1226     else if (p == pp.right)
1227     pp.right = null;
1228     p.parent = null;
1229     }
1230 dl 1.38 }
1231 dl 1.1 }
1232    
1233 dl 1.38 /* ---------------- Collision reduction methods -------------- */
1234 dl 1.14
1235     /**
1236 dl 1.38 * Spreads higher bits to lower, and also forces top 2 bits to 0.
1237     * Because the table uses power-of-two masking, sets of hashes
1238     * that vary only in bits above the current mask will always
1239     * collide. (Among known examples are sets of Float keys holding
1240     * consecutive whole numbers in small tables.) To counter this,
1241     * we apply a transform that spreads the impact of higher bits
1242     * downward. There is a tradeoff between speed, utility, and
1243     * quality of bit-spreading. Because many common sets of hashes
1244 jsr166 1.40 * are already reasonably distributed across bits (so don't benefit
1245 dl 1.38 * from spreading), and because we use trees to handle large sets
1246     * of collisions in bins, we don't need excessively high quality.
1247 dl 1.14 */
1248     private static final int spread(int h) {
1249 dl 1.38 h ^= (h >>> 18) ^ (h >>> 12);
1250     return (h ^ (h >>> 10)) & HASH_BITS;
1251     }
1252    
1253     /**
1254     * Replaces a list bin with a tree bin. Call only when locked.
1255     * Fails to replace if the given key is non-comparable or table
1256     * is, or needs, resizing.
1257     */
1258     private final void replaceWithTreeBin(Node[] tab, int index, Object key) {
1259     if ((key instanceof Comparable) &&
1260     (tab.length >= MAXIMUM_CAPACITY || counter.sum() < (long)sizeCtl)) {
1261     TreeBin t = new TreeBin();
1262     for (Node e = tabAt(tab, index); e != null; e = e.next)
1263     t.putTreeNode(e.hash & HASH_BITS, e.key, e.val);
1264     setTabAt(tab, index, new Node(MOVED, t, null, null));
1265     }
1266 dl 1.14 }
1267 dl 1.1
1268 dl 1.38 /* ---------------- Internal access and update methods -------------- */
1269    
1270 dl 1.14 /** Implementation for get and containsKey */
1271 jsr166 1.4 private final Object internalGet(Object k) {
1272 dl 1.1 int h = spread(k.hashCode());
1273 dl 1.14 retry: for (Node[] tab = table; tab != null;) {
1274 dl 1.38 Node e, p; Object ek, ev; int eh; // locals to read fields once
1275 dl 1.14 for (e = tabAt(tab, (tab.length - 1) & h); e != null; e = e.next) {
1276 dl 1.24 if ((eh = e.hash) == MOVED) {
1277 dl 1.38 if ((ek = e.key) instanceof TreeBin) // search TreeBin
1278     return ((TreeBin)ek).getValue(h, k);
1279     else { // restart with new table
1280     tab = (Node[])ek;
1281     continue retry;
1282     }
1283 dl 1.1 }
1284 dl 1.38 else if ((eh & HASH_BITS) == h && (ev = e.val) != null &&
1285     ((ek = e.key) == k || k.equals(ek)))
1286 dl 1.24 return ev;
1287 dl 1.1 }
1288     break;
1289     }
1290     return null;
1291     }
1292    
1293 dl 1.27 /**
1294     * Implementation for the four public remove/replace methods:
1295     * Replaces node value with v, conditional upon match of cv if
1296     * non-null. If resulting value is null, delete.
1297     */
1298     private final Object internalReplace(Object k, Object v, Object cv) {
1299     int h = spread(k.hashCode());
1300     Object oldVal = null;
1301     for (Node[] tab = table;;) {
1302 dl 1.38 Node f; int i, fh; Object fk;
1303 dl 1.27 if (tab == null ||
1304     (f = tabAt(tab, i = (tab.length - 1) & h)) == null)
1305     break;
1306 dl 1.38 else if ((fh = f.hash) == MOVED) {
1307     if ((fk = f.key) instanceof TreeBin) {
1308     TreeBin t = (TreeBin)fk;
1309     boolean validated = false;
1310     boolean deleted = false;
1311     t.acquire(0);
1312     try {
1313     if (tabAt(tab, i) == f) {
1314     validated = true;
1315     TreeNode p = t.getTreeNode(h, k, t.root);
1316     if (p != null) {
1317     Object pv = p.val;
1318     if (cv == null || cv == pv || cv.equals(pv)) {
1319     oldVal = pv;
1320     if ((p.val = v) == null) {
1321     deleted = true;
1322     t.deleteTreeNode(p);
1323     }
1324     }
1325     }
1326     }
1327     } finally {
1328     t.release(0);
1329     }
1330     if (validated) {
1331     if (deleted)
1332     counter.add(-1L);
1333     break;
1334     }
1335     }
1336     else
1337     tab = (Node[])fk;
1338     }
1339 dl 1.27 else if ((fh & HASH_BITS) != h && f.next == null) // precheck
1340     break; // rules out possible existence
1341     else if ((fh & LOCKED) != 0) {
1342     checkForResize(); // try resizing if can't get lock
1343     f.tryAwaitLock(tab, i);
1344     }
1345     else if (f.casHash(fh, fh | LOCKED)) {
1346     boolean validated = false;
1347     boolean deleted = false;
1348     try {
1349     if (tabAt(tab, i) == f) {
1350     validated = true;
1351     for (Node e = f, pred = null;;) {
1352     Object ek, ev;
1353     if ((e.hash & HASH_BITS) == h &&
1354     ((ev = e.val) != null) &&
1355     ((ek = e.key) == k || k.equals(ek))) {
1356     if (cv == null || cv == ev || cv.equals(ev)) {
1357     oldVal = ev;
1358     if ((e.val = v) == null) {
1359     deleted = true;
1360     Node en = e.next;
1361     if (pred != null)
1362     pred.next = en;
1363     else
1364     setTabAt(tab, i, en);
1365     }
1366     }
1367     break;
1368     }
1369     pred = e;
1370     if ((e = e.next) == null)
1371     break;
1372     }
1373     }
1374     } finally {
1375     if (!f.casHash(fh | LOCKED, fh)) {
1376     f.hash = fh;
1377 jsr166 1.30 synchronized (f) { f.notifyAll(); };
1378 dl 1.27 }
1379     }
1380     if (validated) {
1381     if (deleted)
1382     counter.add(-1L);
1383     break;
1384     }
1385     }
1386     }
1387     return oldVal;
1388     }
1389    
1390     /*
1391 dl 1.59 * Internal versions of the six insertion methods, each a
1392 dl 1.27 * little more complicated than the last. All have
1393     * the same basic structure as the first (internalPut):
1394     * 1. If table uninitialized, create
1395     * 2. If bin empty, try to CAS new node
1396     * 3. If bin stale, use new table
1397 dl 1.38 * 4. if bin converted to TreeBin, validate and relay to TreeBin methods
1398     * 5. Lock and validate; if valid, scan and add or update
1399 dl 1.27 *
1400     * The others interweave other checks and/or alternative actions:
1401     * * Plain put checks for and performs resize after insertion.
1402     * * putIfAbsent prescans for mapping without lock (and fails to add
1403     * if present), which also makes pre-emptive resize checks worthwhile.
1404     * * computeIfAbsent extends form used in putIfAbsent with additional
1405     * mechanics to deal with, calls, potential exceptions and null
1406     * returns from function call.
1407     * * compute uses the same function-call mechanics, but without
1408     * the prescans
1409 dl 1.59 * * merge acts as putIfAbsent in the absent case, but invokes the
1410     * update function if present
1411 dl 1.27 * * putAll attempts to pre-allocate enough table space
1412     * and more lazily performs count updates and checks.
1413     *
1414     * Someday when details settle down a bit more, it might be worth
1415     * some factoring to reduce sprawl.
1416     */
1417    
1418     /** Implementation for put */
1419     private final Object internalPut(Object k, Object v) {
1420 dl 1.1 int h = spread(k.hashCode());
1421 dl 1.38 int count = 0;
1422 dl 1.14 for (Node[] tab = table;;) {
1423 dl 1.38 int i; Node f; int fh; Object fk;
1424 dl 1.1 if (tab == null)
1425 dl 1.24 tab = initTable();
1426     else if ((f = tabAt(tab, i = (tab.length - 1) & h)) == null) {
1427 dl 1.2 if (casTabAt(tab, i, null, new Node(h, k, v, null)))
1428 dl 1.14 break; // no lock when adding to empty bin
1429     }
1430 dl 1.38 else if ((fh = f.hash) == MOVED) {
1431     if ((fk = f.key) instanceof TreeBin) {
1432     TreeBin t = (TreeBin)fk;
1433     Object oldVal = null;
1434     t.acquire(0);
1435     try {
1436     if (tabAt(tab, i) == f) {
1437     count = 2;
1438     TreeNode p = t.putTreeNode(h, k, v);
1439     if (p != null) {
1440     oldVal = p.val;
1441     p.val = v;
1442     }
1443     }
1444     } finally {
1445     t.release(0);
1446     }
1447     if (count != 0) {
1448     if (oldVal != null)
1449     return oldVal;
1450     break;
1451     }
1452     }
1453     else
1454     tab = (Node[])fk;
1455     }
1456 dl 1.27 else if ((fh & LOCKED) != 0) {
1457     checkForResize();
1458     f.tryAwaitLock(tab, i);
1459 dl 1.1 }
1460 dl 1.24 else if (f.casHash(fh, fh | LOCKED)) {
1461 dl 1.27 Object oldVal = null;
1462     try { // needed in case equals() throws
1463 dl 1.24 if (tabAt(tab, i) == f) {
1464 dl 1.38 count = 1;
1465     for (Node e = f;; ++count) {
1466 dl 1.24 Object ek, ev;
1467     if ((e.hash & HASH_BITS) == h &&
1468     (ev = e.val) != null &&
1469     ((ek = e.key) == k || k.equals(ek))) {
1470 dl 1.1 oldVal = ev;
1471 dl 1.27 e.val = v;
1472 dl 1.10 break;
1473 dl 1.1 }
1474 dl 1.10 Node last = e;
1475     if ((e = e.next) == null) {
1476 dl 1.2 last.next = new Node(h, k, v, null);
1477 dl 1.38 if (count >= TREE_THRESHOLD)
1478     replaceWithTreeBin(tab, i, k);
1479 dl 1.10 break;
1480 dl 1.1 }
1481     }
1482     }
1483 dl 1.24 } finally { // unlock and signal if needed
1484     if (!f.casHash(fh | LOCKED, fh)) {
1485     f.hash = fh;
1486 jsr166 1.26 synchronized (f) { f.notifyAll(); };
1487 dl 1.24 }
1488 dl 1.1 }
1489 dl 1.38 if (count != 0) {
1490 dl 1.27 if (oldVal != null)
1491     return oldVal;
1492 dl 1.38 if (tab.length <= 64)
1493     count = 2;
1494 dl 1.1 break;
1495     }
1496     }
1497     }
1498 dl 1.27 counter.add(1L);
1499 dl 1.38 if (count > 1)
1500 dl 1.27 checkForResize();
1501     return null;
1502 dl 1.1 }
1503    
1504 dl 1.27 /** Implementation for putIfAbsent */
1505     private final Object internalPutIfAbsent(Object k, Object v) {
1506 dl 1.1 int h = spread(k.hashCode());
1507 dl 1.38 int count = 0;
1508 dl 1.14 for (Node[] tab = table;;) {
1509 dl 1.27 int i; Node f; int fh; Object fk, fv;
1510     if (tab == null)
1511     tab = initTable();
1512     else if ((f = tabAt(tab, i = (tab.length - 1) & h)) == null) {
1513     if (casTabAt(tab, i, null, new Node(h, k, v, null)))
1514     break;
1515     }
1516 dl 1.38 else if ((fh = f.hash) == MOVED) {
1517     if ((fk = f.key) instanceof TreeBin) {
1518     TreeBin t = (TreeBin)fk;
1519     Object oldVal = null;
1520     t.acquire(0);
1521     try {
1522     if (tabAt(tab, i) == f) {
1523     count = 2;
1524     TreeNode p = t.putTreeNode(h, k, v);
1525     if (p != null)
1526     oldVal = p.val;
1527     }
1528     } finally {
1529     t.release(0);
1530     }
1531     if (count != 0) {
1532     if (oldVal != null)
1533     return oldVal;
1534     break;
1535     }
1536     }
1537     else
1538     tab = (Node[])fk;
1539     }
1540 dl 1.27 else if ((fh & HASH_BITS) == h && (fv = f.val) != null &&
1541     ((fk = f.key) == k || k.equals(fk)))
1542     return fv;
1543     else {
1544     Node g = f.next;
1545     if (g != null) { // at least 2 nodes -- search and maybe resize
1546     for (Node e = g;;) {
1547     Object ek, ev;
1548     if ((e.hash & HASH_BITS) == h && (ev = e.val) != null &&
1549     ((ek = e.key) == k || k.equals(ek)))
1550     return ev;
1551     if ((e = e.next) == null) {
1552     checkForResize();
1553     break;
1554     }
1555     }
1556     }
1557     if (((fh = f.hash) & LOCKED) != 0) {
1558     checkForResize();
1559     f.tryAwaitLock(tab, i);
1560     }
1561     else if (tabAt(tab, i) == f && f.casHash(fh, fh | LOCKED)) {
1562     Object oldVal = null;
1563     try {
1564     if (tabAt(tab, i) == f) {
1565 dl 1.38 count = 1;
1566     for (Node e = f;; ++count) {
1567 dl 1.27 Object ek, ev;
1568     if ((e.hash & HASH_BITS) == h &&
1569     (ev = e.val) != null &&
1570     ((ek = e.key) == k || k.equals(ek))) {
1571 dl 1.1 oldVal = ev;
1572 dl 1.27 break;
1573     }
1574     Node last = e;
1575     if ((e = e.next) == null) {
1576     last.next = new Node(h, k, v, null);
1577 dl 1.38 if (count >= TREE_THRESHOLD)
1578     replaceWithTreeBin(tab, i, k);
1579 dl 1.27 break;
1580 dl 1.1 }
1581     }
1582 dl 1.27 }
1583     } finally {
1584     if (!f.casHash(fh | LOCKED, fh)) {
1585     f.hash = fh;
1586 jsr166 1.30 synchronized (f) { f.notifyAll(); };
1587 dl 1.24 }
1588     }
1589 dl 1.38 if (count != 0) {
1590 dl 1.27 if (oldVal != null)
1591     return oldVal;
1592 dl 1.38 if (tab.length <= 64)
1593     count = 2;
1594 dl 1.27 break;
1595     }
1596     }
1597     }
1598     }
1599     counter.add(1L);
1600 dl 1.38 if (count > 1)
1601     checkForResize();
1602 dl 1.27 return null;
1603     }
1604    
1605     /** Implementation for computeIfAbsent */
1606     private final Object internalComputeIfAbsent(K k,
1607 dl 1.52 Fun<? super K, ?> mf) {
1608 dl 1.27 int h = spread(k.hashCode());
1609     Object val = null;
1610 dl 1.38 int count = 0;
1611 dl 1.27 for (Node[] tab = table;;) {
1612     Node f; int i, fh; Object fk, fv;
1613     if (tab == null)
1614     tab = initTable();
1615     else if ((f = tabAt(tab, i = (tab.length - 1) & h)) == null) {
1616     Node node = new Node(fh = h | LOCKED, k, null, null);
1617     if (casTabAt(tab, i, null, node)) {
1618 dl 1.38 count = 1;
1619 dl 1.27 try {
1620 dl 1.52 if ((val = mf.apply(k)) != null)
1621 dl 1.27 node.val = val;
1622     } finally {
1623     if (val == null)
1624     setTabAt(tab, i, null);
1625     if (!node.casHash(fh, h)) {
1626     node.hash = h;
1627 jsr166 1.30 synchronized (node) { node.notifyAll(); };
1628 dl 1.27 }
1629 dl 1.1 }
1630     }
1631 dl 1.38 if (count != 0)
1632 dl 1.24 break;
1633 dl 1.27 }
1634 dl 1.38 else if ((fh = f.hash) == MOVED) {
1635     if ((fk = f.key) instanceof TreeBin) {
1636     TreeBin t = (TreeBin)fk;
1637     boolean added = false;
1638     t.acquire(0);
1639     try {
1640     if (tabAt(tab, i) == f) {
1641     count = 1;
1642     TreeNode p = t.getTreeNode(h, k, t.root);
1643     if (p != null)
1644     val = p.val;
1645 dl 1.52 else if ((val = mf.apply(k)) != null) {
1646 dl 1.38 added = true;
1647     count = 2;
1648     t.putTreeNode(h, k, val);
1649     }
1650     }
1651     } finally {
1652     t.release(0);
1653     }
1654     if (count != 0) {
1655     if (!added)
1656     return val;
1657     break;
1658     }
1659     }
1660     else
1661     tab = (Node[])fk;
1662     }
1663 dl 1.27 else if ((fh & HASH_BITS) == h && (fv = f.val) != null &&
1664     ((fk = f.key) == k || k.equals(fk)))
1665     return fv;
1666     else {
1667     Node g = f.next;
1668     if (g != null) {
1669     for (Node e = g;;) {
1670     Object ek, ev;
1671     if ((e.hash & HASH_BITS) == h && (ev = e.val) != null &&
1672     ((ek = e.key) == k || k.equals(ek)))
1673     return ev;
1674     if ((e = e.next) == null) {
1675     checkForResize();
1676     break;
1677     }
1678     }
1679     }
1680     if (((fh = f.hash) & LOCKED) != 0) {
1681     checkForResize();
1682     f.tryAwaitLock(tab, i);
1683     }
1684     else if (tabAt(tab, i) == f && f.casHash(fh, fh | LOCKED)) {
1685 dl 1.38 boolean added = false;
1686 dl 1.27 try {
1687     if (tabAt(tab, i) == f) {
1688 dl 1.38 count = 1;
1689     for (Node e = f;; ++count) {
1690 dl 1.27 Object ek, ev;
1691     if ((e.hash & HASH_BITS) == h &&
1692     (ev = e.val) != null &&
1693     ((ek = e.key) == k || k.equals(ek))) {
1694     val = ev;
1695     break;
1696     }
1697     Node last = e;
1698     if ((e = e.next) == null) {
1699 dl 1.52 if ((val = mf.apply(k)) != null) {
1700 dl 1.38 added = true;
1701 dl 1.27 last.next = new Node(h, k, val, null);
1702 dl 1.38 if (count >= TREE_THRESHOLD)
1703     replaceWithTreeBin(tab, i, k);
1704     }
1705 dl 1.27 break;
1706     }
1707     }
1708     }
1709     } finally {
1710     if (!f.casHash(fh | LOCKED, fh)) {
1711     f.hash = fh;
1712 jsr166 1.30 synchronized (f) { f.notifyAll(); };
1713 dl 1.27 }
1714     }
1715 dl 1.38 if (count != 0) {
1716     if (!added)
1717     return val;
1718     if (tab.length <= 64)
1719     count = 2;
1720 dl 1.27 break;
1721 dl 1.38 }
1722 dl 1.1 }
1723     }
1724     }
1725 dl 1.41 if (val != null) {
1726     counter.add(1L);
1727     if (count > 1)
1728     checkForResize();
1729     }
1730 dl 1.27 return val;
1731 dl 1.1 }
1732    
1733 dl 1.27 /** Implementation for compute */
1734 dl 1.61 @SuppressWarnings("unchecked") private final Object internalCompute
1735     (K k, boolean onlyIfPresent, BiFun<? super K, ? super V, ? extends V> mf) {
1736 dl 1.1 int h = spread(k.hashCode());
1737 dl 1.27 Object val = null;
1738 dl 1.41 int delta = 0;
1739 dl 1.38 int count = 0;
1740 dl 1.27 for (Node[] tab = table;;) {
1741 dl 1.38 Node f; int i, fh; Object fk;
1742 dl 1.1 if (tab == null)
1743 dl 1.24 tab = initTable();
1744     else if ((f = tabAt(tab, i = (tab.length - 1) & h)) == null) {
1745 dl 1.52 if (onlyIfPresent)
1746     break;
1747 dl 1.24 Node node = new Node(fh = h | LOCKED, k, null, null);
1748     if (casTabAt(tab, i, null, node)) {
1749     try {
1750 dl 1.38 count = 1;
1751 dl 1.52 if ((val = mf.apply(k, null)) != null) {
1752 dl 1.24 node.val = val;
1753 dl 1.41 delta = 1;
1754 dl 1.24 }
1755     } finally {
1756 dl 1.41 if (delta == 0)
1757 dl 1.24 setTabAt(tab, i, null);
1758     if (!node.casHash(fh, h)) {
1759 dl 1.25 node.hash = h;
1760 jsr166 1.26 synchronized (node) { node.notifyAll(); };
1761 dl 1.1 }
1762     }
1763     }
1764 dl 1.38 if (count != 0)
1765 dl 1.10 break;
1766 dl 1.1 }
1767 dl 1.38 else if ((fh = f.hash) == MOVED) {
1768     if ((fk = f.key) instanceof TreeBin) {
1769     TreeBin t = (TreeBin)fk;
1770     t.acquire(0);
1771     try {
1772     if (tabAt(tab, i) == f) {
1773     count = 1;
1774     TreeNode p = t.getTreeNode(h, k, t.root);
1775 jsr166 1.39 Object pv = (p == null) ? null : p.val;
1776 dl 1.52 if ((val = mf.apply(k, (V)pv)) != null) {
1777 dl 1.38 if (p != null)
1778     p.val = val;
1779     else {
1780     count = 2;
1781 dl 1.41 delta = 1;
1782 dl 1.38 t.putTreeNode(h, k, val);
1783     }
1784     }
1785 dl 1.41 else if (p != null) {
1786     delta = -1;
1787     t.deleteTreeNode(p);
1788     }
1789 dl 1.38 }
1790     } finally {
1791     t.release(0);
1792     }
1793     if (count != 0)
1794     break;
1795     }
1796     else
1797     tab = (Node[])fk;
1798     }
1799 dl 1.27 else if ((fh & LOCKED) != 0) {
1800     checkForResize();
1801     f.tryAwaitLock(tab, i);
1802 dl 1.14 }
1803 dl 1.24 else if (f.casHash(fh, fh | LOCKED)) {
1804     try {
1805     if (tabAt(tab, i) == f) {
1806 dl 1.38 count = 1;
1807 dl 1.41 for (Node e = f, pred = null;; ++count) {
1808 dl 1.27 Object ek, ev;
1809 dl 1.24 if ((e.hash & HASH_BITS) == h &&
1810     (ev = e.val) != null &&
1811     ((ek = e.key) == k || k.equals(ek))) {
1812 dl 1.52 val = mf.apply(k, (V)ev);
1813 dl 1.27 if (val != null)
1814     e.val = val;
1815 dl 1.41 else {
1816     delta = -1;
1817     Node en = e.next;
1818     if (pred != null)
1819     pred.next = en;
1820     else
1821     setTabAt(tab, i, en);
1822     }
1823 dl 1.10 break;
1824 dl 1.1 }
1825 dl 1.41 pred = e;
1826 dl 1.10 if ((e = e.next) == null) {
1827 dl 1.52 if (!onlyIfPresent && (val = mf.apply(k, null)) != null) {
1828 dl 1.41 pred.next = new Node(h, k, val, null);
1829     delta = 1;
1830 dl 1.38 if (count >= TREE_THRESHOLD)
1831     replaceWithTreeBin(tab, i, k);
1832 dl 1.1 }
1833 dl 1.10 break;
1834 dl 1.1 }
1835     }
1836     }
1837 dl 1.24 } finally {
1838     if (!f.casHash(fh | LOCKED, fh)) {
1839     f.hash = fh;
1840 jsr166 1.26 synchronized (f) { f.notifyAll(); };
1841 dl 1.24 }
1842 dl 1.1 }
1843 dl 1.38 if (count != 0) {
1844     if (tab.length <= 64)
1845     count = 2;
1846 dl 1.10 break;
1847 dl 1.38 }
1848 dl 1.1 }
1849 dl 1.10 }
1850 dl 1.41 if (delta != 0) {
1851     counter.add((long)delta);
1852 dl 1.38 if (count > 1)
1853 dl 1.27 checkForResize();
1854     }
1855 dl 1.1 return val;
1856     }
1857    
1858 dl 1.59 /** Implementation for merge */
1859 dl 1.61 @SuppressWarnings("unchecked") private final Object internalMerge
1860     (K k, V v, BiFun<? super V, ? super V, ? extends V> mf) {
1861 dl 1.52 int h = spread(k.hashCode());
1862     Object val = null;
1863     int delta = 0;
1864     int count = 0;
1865     for (Node[] tab = table;;) {
1866     int i; Node f; int fh; Object fk, fv;
1867     if (tab == null)
1868     tab = initTable();
1869     else if ((f = tabAt(tab, i = (tab.length - 1) & h)) == null) {
1870     if (casTabAt(tab, i, null, new Node(h, k, v, null))) {
1871     delta = 1;
1872     val = v;
1873     break;
1874     }
1875     }
1876     else if ((fh = f.hash) == MOVED) {
1877     if ((fk = f.key) instanceof TreeBin) {
1878     TreeBin t = (TreeBin)fk;
1879     t.acquire(0);
1880     try {
1881     if (tabAt(tab, i) == f) {
1882     count = 1;
1883     TreeNode p = t.getTreeNode(h, k, t.root);
1884     val = (p == null) ? v : mf.apply((V)p.val, v);
1885     if (val != null) {
1886     if (p != null)
1887     p.val = val;
1888     else {
1889     count = 2;
1890     delta = 1;
1891     t.putTreeNode(h, k, val);
1892     }
1893     }
1894     else if (p != null) {
1895     delta = -1;
1896     t.deleteTreeNode(p);
1897     }
1898     }
1899     } finally {
1900     t.release(0);
1901     }
1902     if (count != 0)
1903     break;
1904     }
1905     else
1906     tab = (Node[])fk;
1907     }
1908     else if ((fh & LOCKED) != 0) {
1909     checkForResize();
1910     f.tryAwaitLock(tab, i);
1911     }
1912     else if (f.casHash(fh, fh | LOCKED)) {
1913     try {
1914     if (tabAt(tab, i) == f) {
1915     count = 1;
1916     for (Node e = f, pred = null;; ++count) {
1917     Object ek, ev;
1918     if ((e.hash & HASH_BITS) == h &&
1919     (ev = e.val) != null &&
1920     ((ek = e.key) == k || k.equals(ek))) {
1921     val = mf.apply(v, (V)ev);
1922     if (val != null)
1923     e.val = val;
1924     else {
1925     delta = -1;
1926     Node en = e.next;
1927     if (pred != null)
1928     pred.next = en;
1929     else
1930     setTabAt(tab, i, en);
1931     }
1932     break;
1933     }
1934     pred = e;
1935     if ((e = e.next) == null) {
1936     val = v;
1937     pred.next = new Node(h, k, val, null);
1938     delta = 1;
1939     if (count >= TREE_THRESHOLD)
1940     replaceWithTreeBin(tab, i, k);
1941     break;
1942     }
1943     }
1944     }
1945     } finally {
1946     if (!f.casHash(fh | LOCKED, fh)) {
1947     f.hash = fh;
1948     synchronized (f) { f.notifyAll(); };
1949     }
1950     }
1951     if (count != 0) {
1952     if (tab.length <= 64)
1953     count = 2;
1954     break;
1955     }
1956     }
1957     }
1958     if (delta != 0) {
1959     counter.add((long)delta);
1960     if (count > 1)
1961     checkForResize();
1962     }
1963     return val;
1964     }
1965    
1966 dl 1.27 /** Implementation for putAll */
1967     private final void internalPutAll(Map<?, ?> m) {
1968     tryPresize(m.size());
1969     long delta = 0L; // number of uncommitted additions
1970     boolean npe = false; // to throw exception on exit for nulls
1971     try { // to clean up counts on other exceptions
1972     for (Map.Entry<?, ?> entry : m.entrySet()) {
1973     Object k, v;
1974     if (entry == null || (k = entry.getKey()) == null ||
1975     (v = entry.getValue()) == null) {
1976     npe = true;
1977     break;
1978     }
1979     int h = spread(k.hashCode());
1980     for (Node[] tab = table;;) {
1981 dl 1.38 int i; Node f; int fh; Object fk;
1982 dl 1.27 if (tab == null)
1983     tab = initTable();
1984     else if ((f = tabAt(tab, i = (tab.length - 1) & h)) == null){
1985     if (casTabAt(tab, i, null, new Node(h, k, v, null))) {
1986     ++delta;
1987     break;
1988     }
1989     }
1990 dl 1.38 else if ((fh = f.hash) == MOVED) {
1991     if ((fk = f.key) instanceof TreeBin) {
1992     TreeBin t = (TreeBin)fk;
1993     boolean validated = false;
1994     t.acquire(0);
1995     try {
1996     if (tabAt(tab, i) == f) {
1997     validated = true;
1998     TreeNode p = t.getTreeNode(h, k, t.root);
1999     if (p != null)
2000     p.val = v;
2001     else {
2002     t.putTreeNode(h, k, v);
2003     ++delta;
2004     }
2005     }
2006     } finally {
2007     t.release(0);
2008     }
2009     if (validated)
2010     break;
2011     }
2012     else
2013     tab = (Node[])fk;
2014     }
2015 dl 1.27 else if ((fh & LOCKED) != 0) {
2016     counter.add(delta);
2017     delta = 0L;
2018     checkForResize();
2019     f.tryAwaitLock(tab, i);
2020     }
2021     else if (f.casHash(fh, fh | LOCKED)) {
2022 dl 1.38 int count = 0;
2023 dl 1.27 try {
2024     if (tabAt(tab, i) == f) {
2025 dl 1.38 count = 1;
2026     for (Node e = f;; ++count) {
2027 dl 1.27 Object ek, ev;
2028     if ((e.hash & HASH_BITS) == h &&
2029     (ev = e.val) != null &&
2030     ((ek = e.key) == k || k.equals(ek))) {
2031     e.val = v;
2032     break;
2033     }
2034     Node last = e;
2035     if ((e = e.next) == null) {
2036     ++delta;
2037     last.next = new Node(h, k, v, null);
2038 dl 1.38 if (count >= TREE_THRESHOLD)
2039     replaceWithTreeBin(tab, i, k);
2040 dl 1.27 break;
2041     }
2042     }
2043     }
2044     } finally {
2045     if (!f.casHash(fh | LOCKED, fh)) {
2046     f.hash = fh;
2047 jsr166 1.30 synchronized (f) { f.notifyAll(); };
2048 dl 1.27 }
2049     }
2050 dl 1.38 if (count != 0) {
2051     if (count > 1) {
2052 dl 1.27 counter.add(delta);
2053     delta = 0L;
2054     checkForResize();
2055 dl 1.1 }
2056 dl 1.27 break;
2057 dl 1.24 }
2058     }
2059 dl 1.1 }
2060     }
2061 dl 1.27 } finally {
2062     if (delta != 0)
2063     counter.add(delta);
2064 dl 1.1 }
2065 dl 1.27 if (npe)
2066     throw new NullPointerException();
2067 dl 1.1 }
2068    
2069 dl 1.27 /* ---------------- Table Initialization and Resizing -------------- */
2070 dl 1.24
2071     /**
2072     * Returns a power of two table size for the given desired capacity.
2073     * See Hackers Delight, sec 3.2
2074     */
2075     private static final int tableSizeFor(int c) {
2076     int n = c - 1;
2077     n |= n >>> 1;
2078     n |= n >>> 2;
2079     n |= n >>> 4;
2080     n |= n >>> 8;
2081     n |= n >>> 16;
2082     return (n < 0) ? 1 : (n >= MAXIMUM_CAPACITY) ? MAXIMUM_CAPACITY : n + 1;
2083     }
2084    
2085     /**
2086     * Initializes table, using the size recorded in sizeCtl.
2087     */
2088     private final Node[] initTable() {
2089     Node[] tab; int sc;
2090     while ((tab = table) == null) {
2091     if ((sc = sizeCtl) < 0)
2092     Thread.yield(); // lost initialization race; just spin
2093     else if (UNSAFE.compareAndSwapInt(this, sizeCtlOffset, sc, -1)) {
2094     try {
2095     if ((tab = table) == null) {
2096     int n = (sc > 0) ? sc : DEFAULT_CAPACITY;
2097     tab = table = new Node[n];
2098 dl 1.27 sc = n - (n >>> 2);
2099 dl 1.24 }
2100     } finally {
2101     sizeCtl = sc;
2102     }
2103     break;
2104     }
2105     }
2106     return tab;
2107     }
2108    
2109     /**
2110 dl 1.27 * If table is too small and not already resizing, creates next
2111     * table and transfers bins. Rechecks occupancy after a transfer
2112     * to see if another resize is already needed because resizings
2113     * are lagging additions.
2114     */
2115     private final void checkForResize() {
2116     Node[] tab; int n, sc;
2117     while ((tab = table) != null &&
2118     (n = tab.length) < MAXIMUM_CAPACITY &&
2119     (sc = sizeCtl) >= 0 && counter.sum() >= (long)sc &&
2120     UNSAFE.compareAndSwapInt(this, sizeCtlOffset, sc, -1)) {
2121 dl 1.24 try {
2122 dl 1.27 if (tab == table) {
2123 dl 1.24 table = rebuild(tab);
2124 dl 1.27 sc = (n << 1) - (n >>> 1);
2125 dl 1.24 }
2126     } finally {
2127     sizeCtl = sc;
2128     }
2129     }
2130     }
2131    
2132 dl 1.27 /**
2133     * Tries to presize table to accommodate the given number of elements.
2134     *
2135     * @param size number of elements (doesn't need to be perfectly accurate)
2136     */
2137     private final void tryPresize(int size) {
2138     int c = (size >= (MAXIMUM_CAPACITY >>> 1)) ? MAXIMUM_CAPACITY :
2139     tableSizeFor(size + (size >>> 1) + 1);
2140     int sc;
2141     while ((sc = sizeCtl) >= 0) {
2142     Node[] tab = table; int n;
2143     if (tab == null || (n = tab.length) == 0) {
2144 jsr166 1.30 n = (sc > c) ? sc : c;
2145 dl 1.27 if (UNSAFE.compareAndSwapInt(this, sizeCtlOffset, sc, -1)) {
2146     try {
2147     if (table == tab) {
2148     table = new Node[n];
2149     sc = n - (n >>> 2);
2150     }
2151     } finally {
2152     sizeCtl = sc;
2153     }
2154     }
2155     }
2156     else if (c <= sc || n >= MAXIMUM_CAPACITY)
2157     break;
2158     else if (UNSAFE.compareAndSwapInt(this, sizeCtlOffset, sc, -1)) {
2159     try {
2160     if (table == tab) {
2161     table = rebuild(tab);
2162     sc = (n << 1) - (n >>> 1);
2163     }
2164     } finally {
2165     sizeCtl = sc;
2166     }
2167     }
2168     }
2169     }
2170    
2171 dl 1.24 /*
2172     * Moves and/or copies the nodes in each bin to new table. See
2173     * above for explanation.
2174     *
2175     * @return the new table
2176     */
2177     private static final Node[] rebuild(Node[] tab) {
2178     int n = tab.length;
2179     Node[] nextTab = new Node[n << 1];
2180     Node fwd = new Node(MOVED, nextTab, null, null);
2181     int[] buffer = null; // holds bins to revisit; null until needed
2182     Node rev = null; // reverse forwarder; null until needed
2183     int nbuffered = 0; // the number of bins in buffer list
2184     int bufferIndex = 0; // buffer index of current buffered bin
2185     int bin = n - 1; // current non-buffered bin or -1 if none
2186    
2187     for (int i = bin;;) { // start upwards sweep
2188     int fh; Node f;
2189     if ((f = tabAt(tab, i)) == null) {
2190 dl 1.61 if (bin >= 0) { // Unbuffered; no lock needed (or available)
2191 dl 1.24 if (!casTabAt(tab, i, f, fwd))
2192     continue;
2193     }
2194     else { // transiently use a locked forwarding node
2195 jsr166 1.33 Node g = new Node(MOVED|LOCKED, nextTab, null, null);
2196 dl 1.24 if (!casTabAt(tab, i, f, g))
2197     continue;
2198     setTabAt(nextTab, i, null);
2199     setTabAt(nextTab, i + n, null);
2200     setTabAt(tab, i, fwd);
2201     if (!g.casHash(MOVED|LOCKED, MOVED)) {
2202     g.hash = MOVED;
2203 jsr166 1.26 synchronized (g) { g.notifyAll(); }
2204 dl 1.24 }
2205     }
2206     }
2207 dl 1.38 else if ((fh = f.hash) == MOVED) {
2208     Object fk = f.key;
2209     if (fk instanceof TreeBin) {
2210     TreeBin t = (TreeBin)fk;
2211     boolean validated = false;
2212     t.acquire(0);
2213     try {
2214     if (tabAt(tab, i) == f) {
2215     validated = true;
2216     splitTreeBin(nextTab, i, t);
2217     setTabAt(tab, i, fwd);
2218     }
2219     } finally {
2220     t.release(0);
2221     }
2222     if (!validated)
2223     continue;
2224     }
2225     }
2226     else if ((fh & LOCKED) == 0 && f.casHash(fh, fh|LOCKED)) {
2227 dl 1.24 boolean validated = false;
2228     try { // split to lo and hi lists; copying as needed
2229     if (tabAt(tab, i) == f) {
2230     validated = true;
2231 dl 1.38 splitBin(nextTab, i, f);
2232 dl 1.24 setTabAt(tab, i, fwd);
2233     }
2234     } finally {
2235     if (!f.casHash(fh | LOCKED, fh)) {
2236     f.hash = fh;
2237 jsr166 1.26 synchronized (f) { f.notifyAll(); };
2238 dl 1.24 }
2239     }
2240     if (!validated)
2241     continue;
2242     }
2243     else {
2244     if (buffer == null) // initialize buffer for revisits
2245     buffer = new int[TRANSFER_BUFFER_SIZE];
2246     if (bin < 0 && bufferIndex > 0) {
2247     int j = buffer[--bufferIndex];
2248     buffer[bufferIndex] = i;
2249     i = j; // swap with another bin
2250     continue;
2251     }
2252     if (bin < 0 || nbuffered >= TRANSFER_BUFFER_SIZE) {
2253     f.tryAwaitLock(tab, i);
2254     continue; // no other options -- block
2255     }
2256     if (rev == null) // initialize reverse-forwarder
2257     rev = new Node(MOVED, tab, null, null);
2258     if (tabAt(tab, i) != f || (f.hash & LOCKED) == 0)
2259     continue; // recheck before adding to list
2260     buffer[nbuffered++] = i;
2261     setTabAt(nextTab, i, rev); // install place-holders
2262     setTabAt(nextTab, i + n, rev);
2263     }
2264    
2265     if (bin > 0)
2266     i = --bin;
2267     else if (buffer != null && nbuffered > 0) {
2268     bin = -1;
2269     i = buffer[bufferIndex = --nbuffered];
2270     }
2271     else
2272     return nextTab;
2273     }
2274     }
2275    
2276 dl 1.27 /**
2277 jsr166 1.45 * Splits a normal bin with list headed by e into lo and hi parts;
2278     * installs in given table.
2279 dl 1.38 */
2280     private static void splitBin(Node[] nextTab, int i, Node e) {
2281     int bit = nextTab.length >>> 1; // bit to split on
2282     int runBit = e.hash & bit;
2283     Node lastRun = e, lo = null, hi = null;
2284     for (Node p = e.next; p != null; p = p.next) {
2285     int b = p.hash & bit;
2286     if (b != runBit) {
2287     runBit = b;
2288     lastRun = p;
2289     }
2290     }
2291     if (runBit == 0)
2292     lo = lastRun;
2293     else
2294     hi = lastRun;
2295     for (Node p = e; p != lastRun; p = p.next) {
2296     int ph = p.hash & HASH_BITS;
2297     Object pk = p.key, pv = p.val;
2298     if ((ph & bit) == 0)
2299     lo = new Node(ph, pk, pv, lo);
2300     else
2301     hi = new Node(ph, pk, pv, hi);
2302     }
2303     setTabAt(nextTab, i, lo);
2304     setTabAt(nextTab, i + bit, hi);
2305     }
2306    
2307     /**
2308 jsr166 1.45 * Splits a tree bin into lo and hi parts; installs in given table.
2309 dl 1.38 */
2310     private static void splitTreeBin(Node[] nextTab, int i, TreeBin t) {
2311     int bit = nextTab.length >>> 1;
2312     TreeBin lt = new TreeBin();
2313     TreeBin ht = new TreeBin();
2314     int lc = 0, hc = 0;
2315     for (Node e = t.first; e != null; e = e.next) {
2316     int h = e.hash & HASH_BITS;
2317     Object k = e.key, v = e.val;
2318     if ((h & bit) == 0) {
2319     ++lc;
2320     lt.putTreeNode(h, k, v);
2321     }
2322     else {
2323     ++hc;
2324     ht.putTreeNode(h, k, v);
2325     }
2326     }
2327     Node ln, hn; // throw away trees if too small
2328     if (lc <= (TREE_THRESHOLD >>> 1)) {
2329     ln = null;
2330     for (Node p = lt.first; p != null; p = p.next)
2331     ln = new Node(p.hash, p.key, p.val, ln);
2332     }
2333     else
2334     ln = new Node(MOVED, lt, null, null);
2335     setTabAt(nextTab, i, ln);
2336     if (hc <= (TREE_THRESHOLD >>> 1)) {
2337     hn = null;
2338     for (Node p = ht.first; p != null; p = p.next)
2339     hn = new Node(p.hash, p.key, p.val, hn);
2340     }
2341     else
2342     hn = new Node(MOVED, ht, null, null);
2343     setTabAt(nextTab, i + bit, hn);
2344     }
2345    
2346     /**
2347 dl 1.27 * Implementation for clear. Steps through each bin, removing all
2348     * nodes.
2349     */
2350     private final void internalClear() {
2351     long delta = 0L; // negative number of deletions
2352     int i = 0;
2353     Node[] tab = table;
2354     while (tab != null && i < tab.length) {
2355 dl 1.38 int fh; Object fk;
2356 dl 1.27 Node f = tabAt(tab, i);
2357     if (f == null)
2358     ++i;
2359 dl 1.38 else if ((fh = f.hash) == MOVED) {
2360     if ((fk = f.key) instanceof TreeBin) {
2361     TreeBin t = (TreeBin)fk;
2362     t.acquire(0);
2363     try {
2364     if (tabAt(tab, i) == f) {
2365     for (Node p = t.first; p != null; p = p.next) {
2366 dl 1.61 if (p.val != null) { // (currently always true)
2367     p.val = null;
2368     --delta;
2369     }
2370 dl 1.38 }
2371     t.first = null;
2372     t.root = null;
2373     ++i;
2374     }
2375     } finally {
2376     t.release(0);
2377     }
2378     }
2379     else
2380     tab = (Node[])fk;
2381     }
2382 dl 1.27 else if ((fh & LOCKED) != 0) {
2383     counter.add(delta); // opportunistically update count
2384     delta = 0L;
2385     f.tryAwaitLock(tab, i);
2386     }
2387     else if (f.casHash(fh, fh | LOCKED)) {
2388     try {
2389     if (tabAt(tab, i) == f) {
2390     for (Node e = f; e != null; e = e.next) {
2391 dl 1.61 if (e.val != null) { // (currently always true)
2392     e.val = null;
2393     --delta;
2394     }
2395 dl 1.27 }
2396     setTabAt(tab, i, null);
2397 dl 1.38 ++i;
2398 dl 1.27 }
2399     } finally {
2400     if (!f.casHash(fh | LOCKED, fh)) {
2401     f.hash = fh;
2402 jsr166 1.30 synchronized (f) { f.notifyAll(); };
2403 dl 1.27 }
2404     }
2405     }
2406     }
2407     if (delta != 0)
2408     counter.add(delta);
2409     }
2410    
2411 dl 1.14 /* ----------------Table Traversal -------------- */
2412    
2413 dl 1.1 /**
2414 dl 1.14 * Encapsulates traversal for methods such as containsValue; also
2415 dl 1.59 * serves as a base class for other iterators and bulk tasks.
2416 dl 1.14 *
2417     * At each step, the iterator snapshots the key ("nextKey") and
2418     * value ("nextVal") of a valid node (i.e., one that, at point of
2419 jsr166 1.36 * snapshot, has a non-null user value). Because val fields can
2420 dl 1.14 * change (including to null, indicating deletion), field nextVal
2421     * might not be accurate at point of use, but still maintains the
2422     * weak consistency property of holding a value that was once
2423 dl 1.70 * valid. To support iterator.remove, the nextKey field is not
2424     * updated (nulled out) when the iterator cannot advance.
2425 dl 1.14 *
2426     * Internal traversals directly access these fields, as in:
2427 dl 1.41 * {@code while (it.advance() != null) { process(it.nextKey); }}
2428 dl 1.14 *
2429 dl 1.41 * Exported iterators must track whether the iterator has advanced
2430     * (in hasNext vs next) (by setting/checking/nulling field
2431     * nextVal), and then extract key, value, or key-value pairs as
2432     * return values of next().
2433 dl 1.14 *
2434 dl 1.27 * The iterator visits once each still-valid node that was
2435     * reachable upon iterator construction. It might miss some that
2436     * were added to a bin after the bin was visited, which is OK wrt
2437     * consistency guarantees. Maintaining this property in the face
2438     * of possible ongoing resizes requires a fair amount of
2439     * bookkeeping state that is difficult to optimize away amidst
2440     * volatile accesses. Even so, traversal maintains reasonable
2441     * throughput.
2442 dl 1.14 *
2443     * Normally, iteration proceeds bin-by-bin traversing lists.
2444     * However, if the table has been resized, then all future steps
2445     * must traverse both the bin at the current index as well as at
2446     * (index + baseSize); and so on for further resizings. To
2447     * paranoically cope with potential sharing by users of iterators
2448     * across threads, iteration terminates if a bounds checks fails
2449     * for a table read.
2450 dl 1.52 *
2451     * This class extends ForkJoinTask to streamline parallel
2452     * iteration in bulk operations (see BulkTask). This adds only an
2453     * int of space overhead, which is close enough to negligible in
2454 dl 1.59 * cases where it is not needed to not worry about it. Because
2455     * ForkJoinTask is Serializable, but iterators need not be, we
2456     * need to add warning suppressions.
2457 dl 1.14 */
2458 dl 1.61 @SuppressWarnings("serial") static class Traverser<K,V,R> extends ForkJoinTask<R> {
2459 dl 1.41 final ConcurrentHashMapV8<K, V> map;
2460 dl 1.14 Node next; // the next entry to use
2461     Object nextKey; // cached key field of next
2462     Object nextVal; // cached val field of next
2463     Node[] tab; // current table; updated if resized
2464     int index; // index of bin to use next
2465     int baseIndex; // current index of initial table
2466 dl 1.41 int baseLimit; // index bound for initial table
2467 dl 1.63 int baseSize; // initial table size
2468 dl 1.14
2469     /** Creates iterator for all entries in the table. */
2470 dl 1.52 Traverser(ConcurrentHashMapV8<K, V> map) {
2471 dl 1.63 this.map = map;
2472 dl 1.14 }
2473    
2474 dl 1.52 /** Creates iterator for split() methods */
2475 dl 1.61 Traverser(Traverser<K,V,?> it) {
2476 dl 1.63 ConcurrentHashMapV8<K, V> m; Node[] t;
2477     if ((m = this.map = it.map) == null)
2478     t = null;
2479     else if ((t = it.tab) == null && // force parent tab initialization
2480     (t = it.tab = m.table) != null)
2481     it.baseLimit = it.baseSize = t.length;
2482     this.tab = t;
2483 dl 1.41 this.baseSize = it.baseSize;
2484 dl 1.61 it.baseLimit = this.index = this.baseIndex =
2485     ((this.baseLimit = it.baseLimit) + it.baseIndex + 1) >>> 1;
2486 dl 1.41 }
2487    
2488     /**
2489 jsr166 1.48 * Advances next; returns nextVal or null if terminated.
2490 dl 1.41 * See above for explanation.
2491     */
2492     final Object advance() {
2493 dl 1.70 Node e = next;
2494 dl 1.41 Object ev = null;
2495 dl 1.14 outer: do {
2496 dl 1.24 if (e != null) // advance past used/skipped node
2497 dl 1.1 e = e.next;
2498 dl 1.24 while (e == null) { // get to next non-null bin
2499 dl 1.63 ConcurrentHashMapV8<K, V> m;
2500 dl 1.38 Node[] t; int b, i, n; Object ek; // checks must use locals
2501 dl 1.63 if ((t = tab) != null)
2502     n = t.length;
2503     else if ((m = map) != null && (t = tab = m.table) != null)
2504     n = baseLimit = baseSize = t.length;
2505     else
2506 dl 1.14 break outer;
2507 dl 1.63 if ((b = baseIndex) >= baseLimit ||
2508     (i = index) < 0 || i >= n)
2509     break outer;
2510     if ((e = tabAt(t, i)) != null && e.hash == MOVED) {
2511 dl 1.38 if ((ek = e.key) instanceof TreeBin)
2512     e = ((TreeBin)ek).first;
2513     else {
2514     tab = (Node[])ek;
2515     continue; // restarts due to null val
2516     }
2517     } // visit upper slots if present
2518     index = (i += baseSize) < n ? i : (baseIndex = b + 1);
2519 dl 1.1 }
2520 dl 1.14 nextKey = e.key;
2521 dl 1.41 } while ((ev = e.val) == null); // skip deleted or special nodes
2522 dl 1.14 next = e;
2523 dl 1.41 return nextVal = ev;
2524 dl 1.1 }
2525 dl 1.41
2526     public final void remove() {
2527 dl 1.70 Object k = nextKey;
2528     if (k == null && (advance() == null || (k = nextKey) == null))
2529 dl 1.41 throw new IllegalStateException();
2530 dl 1.70 map.internalReplace(k, null, null);
2531 dl 1.41 }
2532    
2533     public final boolean hasNext() {
2534     return nextVal != null || advance() != null;
2535     }
2536    
2537     public final boolean hasMoreElements() { return hasNext(); }
2538 dl 1.52 public final void setRawResult(Object x) { }
2539     public R getRawResult() { return null; }
2540     public boolean exec() { return true; }
2541 dl 1.1 }
2542    
2543     /* ---------------- Public operations -------------- */
2544    
2545     /**
2546 jsr166 1.48 * Creates a new, empty map with the default initial table size (16).
2547 dl 1.1 */
2548 dl 1.16 public ConcurrentHashMapV8() {
2549 dl 1.14 this.counter = new LongAdder();
2550 dl 1.1 }
2551    
2552     /**
2553 dl 1.16 * Creates a new, empty map with an initial table size
2554     * accommodating the specified number of elements without the need
2555     * to dynamically resize.
2556 dl 1.1 *
2557     * @param initialCapacity The implementation performs internal
2558     * sizing to accommodate this many elements.
2559     * @throws IllegalArgumentException if the initial capacity of
2560 jsr166 1.18 * elements is negative
2561 dl 1.1 */
2562 dl 1.16 public ConcurrentHashMapV8(int initialCapacity) {
2563     if (initialCapacity < 0)
2564     throw new IllegalArgumentException();
2565     int cap = ((initialCapacity >= (MAXIMUM_CAPACITY >>> 1)) ?
2566     MAXIMUM_CAPACITY :
2567     tableSizeFor(initialCapacity + (initialCapacity >>> 1) + 1));
2568     this.counter = new LongAdder();
2569 dl 1.24 this.sizeCtl = cap;
2570 dl 1.1 }
2571    
2572     /**
2573 dl 1.16 * Creates a new map with the same mappings as the given map.
2574 dl 1.1 *
2575 dl 1.16 * @param m the map
2576 dl 1.1 */
2577 dl 1.16 public ConcurrentHashMapV8(Map<? extends K, ? extends V> m) {
2578     this.counter = new LongAdder();
2579 dl 1.24 this.sizeCtl = DEFAULT_CAPACITY;
2580 dl 1.27 internalPutAll(m);
2581 dl 1.1 }
2582    
2583     /**
2584 dl 1.16 * Creates a new, empty map with an initial table size based on
2585     * the given number of elements ({@code initialCapacity}) and
2586     * initial table density ({@code loadFactor}).
2587     *
2588     * @param initialCapacity the initial capacity. The implementation
2589     * performs internal sizing to accommodate this many elements,
2590     * given the specified load factor.
2591     * @param loadFactor the load factor (table density) for
2592 jsr166 1.18 * establishing the initial table size
2593 dl 1.16 * @throws IllegalArgumentException if the initial capacity of
2594     * elements is negative or the load factor is nonpositive
2595 jsr166 1.22 *
2596     * @since 1.6
2597 dl 1.1 */
2598 dl 1.16 public ConcurrentHashMapV8(int initialCapacity, float loadFactor) {
2599     this(initialCapacity, loadFactor, 1);
2600 dl 1.1 }
2601    
2602     /**
2603 dl 1.16 * Creates a new, empty map with an initial table size based on
2604     * the given number of elements ({@code initialCapacity}), table
2605     * density ({@code loadFactor}), and number of concurrently
2606     * updating threads ({@code concurrencyLevel}).
2607 dl 1.1 *
2608 dl 1.16 * @param initialCapacity the initial capacity. The implementation
2609     * performs internal sizing to accommodate this many elements,
2610     * given the specified load factor.
2611     * @param loadFactor the load factor (table density) for
2612 jsr166 1.18 * establishing the initial table size
2613 dl 1.16 * @param concurrencyLevel the estimated number of concurrently
2614     * updating threads. The implementation may use this value as
2615     * a sizing hint.
2616     * @throws IllegalArgumentException if the initial capacity is
2617     * negative or the load factor or concurrencyLevel are
2618 jsr166 1.18 * nonpositive
2619 dl 1.1 */
2620 dl 1.16 public ConcurrentHashMapV8(int initialCapacity,
2621     float loadFactor, int concurrencyLevel) {
2622     if (!(loadFactor > 0.0f) || initialCapacity < 0 || concurrencyLevel <= 0)
2623     throw new IllegalArgumentException();
2624     if (initialCapacity < concurrencyLevel) // Use at least as many bins
2625     initialCapacity = concurrencyLevel; // as estimated threads
2626     long size = (long)(1.0 + (long)initialCapacity / loadFactor);
2627 jsr166 1.49 int cap = (size >= (long)MAXIMUM_CAPACITY) ?
2628     MAXIMUM_CAPACITY : tableSizeFor((int)size);
2629 dl 1.16 this.counter = new LongAdder();
2630 dl 1.24 this.sizeCtl = cap;
2631 dl 1.1 }
2632    
2633     /**
2634 dl 1.70 * Creates a new {@link Set} backed by a ConcurrentHashMapV8
2635     * from the given type to {@code Boolean.TRUE}.
2636     *
2637     * @return the new set
2638     */
2639     public static <K> KeySetView<K,Boolean> newKeySet() {
2640     return new KeySetView<K,Boolean>(new ConcurrentHashMapV8<K,Boolean>(),
2641     Boolean.TRUE);
2642     }
2643    
2644     /**
2645     * Creates a new {@link Set} backed by a ConcurrentHashMapV8
2646     * from the given type to {@code Boolean.TRUE}.
2647     *
2648     * @param initialCapacity The implementation performs internal
2649     * sizing to accommodate this many elements.
2650     * @throws IllegalArgumentException if the initial capacity of
2651     * elements is negative
2652     * @return the new set
2653     */
2654     public static <K> KeySetView<K,Boolean> newKeySet(int initialCapacity) {
2655     return new KeySetView<K,Boolean>(new ConcurrentHashMapV8<K,Boolean>(initialCapacity),
2656     Boolean.TRUE);
2657     }
2658    
2659     /**
2660 dl 1.14 * {@inheritDoc}
2661 dl 1.1 */
2662     public boolean isEmpty() {
2663 dl 1.2 return counter.sum() <= 0L; // ignore transient negative values
2664 dl 1.1 }
2665    
2666     /**
2667 dl 1.14 * {@inheritDoc}
2668 dl 1.1 */
2669     public int size() {
2670     long n = counter.sum();
2671 jsr166 1.15 return ((n < 0L) ? 0 :
2672     (n > (long)Integer.MAX_VALUE) ? Integer.MAX_VALUE :
2673 dl 1.14 (int)n);
2674 dl 1.1 }
2675    
2676 dl 1.52 /**
2677     * Returns the number of mappings. This method should be used
2678 dl 1.70 * instead of {@link #size} because a ConcurrentHashMapV8 may
2679 dl 1.52 * contain more mappings than can be represented as an int. The
2680     * value returned is a snapshot; the actual count may differ if
2681 dl 1.60 * there are ongoing concurrent insertions or removals.
2682 dl 1.52 *
2683     * @return the number of mappings
2684     */
2685     public long mappingCount() {
2686 dl 1.24 long n = counter.sum();
2687 dl 1.59 return (n < 0L) ? 0L : n; // ignore transient negative values
2688 dl 1.24 }
2689    
2690 dl 1.1 /**
2691     * Returns the value to which the specified key is mapped,
2692     * or {@code null} if this map contains no mapping for the key.
2693     *
2694     * <p>More formally, if this map contains a mapping from a key
2695     * {@code k} to a value {@code v} such that {@code key.equals(k)},
2696     * then this method returns {@code v}; otherwise it returns
2697     * {@code null}. (There can be at most one such mapping.)
2698     *
2699     * @throws NullPointerException if the specified key is null
2700     */
2701 dl 1.61 @SuppressWarnings("unchecked") public V get(Object key) {
2702 dl 1.1 if (key == null)
2703     throw new NullPointerException();
2704     return (V)internalGet(key);
2705     }
2706    
2707     /**
2708 dl 1.62 * Returns the value to which the specified key is mapped,
2709 jsr166 1.66 * or the given defaultValue if this map contains no mapping for the key.
2710 dl 1.62 *
2711     * @param key the key
2712     * @param defaultValue the value to return if this map contains
2713 jsr166 1.67 * no mapping for the given key
2714 dl 1.62 * @return the mapping for the key, if present; else the defaultValue
2715     * @throws NullPointerException if the specified key is null
2716     */
2717     @SuppressWarnings("unchecked") public V getValueOrDefault(Object key, V defaultValue) {
2718     if (key == null)
2719     throw new NullPointerException();
2720     V v = (V) internalGet(key);
2721     return v == null ? defaultValue : v;
2722     }
2723    
2724     /**
2725 dl 1.1 * Tests if the specified object is a key in this table.
2726     *
2727     * @param key possible key
2728     * @return {@code true} if and only if the specified object
2729     * is a key in this table, as determined by the
2730 jsr166 1.18 * {@code equals} method; {@code false} otherwise
2731 dl 1.1 * @throws NullPointerException if the specified key is null
2732     */
2733     public boolean containsKey(Object key) {
2734     if (key == null)
2735     throw new NullPointerException();
2736     return internalGet(key) != null;
2737     }
2738    
2739     /**
2740     * Returns {@code true} if this map maps one or more keys to the
2741 dl 1.14 * specified value. Note: This method may require a full traversal
2742     * of the map, and is much slower than method {@code containsKey}.
2743 dl 1.1 *
2744     * @param value value whose presence in this map is to be tested
2745     * @return {@code true} if this map maps one or more keys to the
2746     * specified value
2747     * @throws NullPointerException if the specified value is null
2748     */
2749     public boolean containsValue(Object value) {
2750     if (value == null)
2751     throw new NullPointerException();
2752 dl 1.14 Object v;
2753 dl 1.52 Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
2754 dl 1.41 while ((v = it.advance()) != null) {
2755     if (v == value || value.equals(v))
2756 dl 1.14 return true;
2757     }
2758     return false;
2759 dl 1.1 }
2760    
2761     /**
2762     * Legacy method testing if some key maps into the specified value
2763     * in this table. This method is identical in functionality to
2764     * {@link #containsValue}, and exists solely to ensure
2765     * full compatibility with class {@link java.util.Hashtable},
2766     * which supported this method prior to introduction of the
2767     * Java Collections framework.
2768     *
2769     * @param value a value to search for
2770     * @return {@code true} if and only if some key maps to the
2771     * {@code value} argument in this table as
2772     * determined by the {@code equals} method;
2773     * {@code false} otherwise
2774     * @throws NullPointerException if the specified value is null
2775     */
2776     public boolean contains(Object value) {
2777     return containsValue(value);
2778     }
2779    
2780     /**
2781     * Maps the specified key to the specified value in this table.
2782     * Neither the key nor the value can be null.
2783     *
2784     * <p> The value can be retrieved by calling the {@code get} method
2785     * with a key that is equal to the original key.
2786     *
2787     * @param key key with which the specified value is to be associated
2788     * @param value value to be associated with the specified key
2789     * @return the previous value associated with {@code key}, or
2790     * {@code null} if there was no mapping for {@code key}
2791     * @throws NullPointerException if the specified key or value is null
2792     */
2793 dl 1.61 @SuppressWarnings("unchecked") public V put(K key, V value) {
2794 dl 1.1 if (key == null || value == null)
2795     throw new NullPointerException();
2796 dl 1.27 return (V)internalPut(key, value);
2797 dl 1.1 }
2798    
2799     /**
2800     * {@inheritDoc}
2801     *
2802     * @return the previous value associated with the specified key,
2803     * or {@code null} if there was no mapping for the key
2804     * @throws NullPointerException if the specified key or value is null
2805     */
2806 dl 1.61 @SuppressWarnings("unchecked") public V putIfAbsent(K key, V value) {
2807 dl 1.1 if (key == null || value == null)
2808     throw new NullPointerException();
2809 dl 1.27 return (V)internalPutIfAbsent(key, value);
2810 dl 1.1 }
2811    
2812     /**
2813     * Copies all of the mappings from the specified map to this one.
2814     * These mappings replace any mappings that this map had for any of the
2815     * keys currently in the specified map.
2816     *
2817     * @param m mappings to be stored in this map
2818     */
2819     public void putAll(Map<? extends K, ? extends V> m) {
2820 dl 1.27 internalPutAll(m);
2821 dl 1.1 }
2822    
2823     /**
2824     * If the specified key is not already associated with a value,
2825 dl 1.41 * computes its value using the given mappingFunction and enters
2826     * it into the map unless null. This is equivalent to
2827 dl 1.27 * <pre> {@code
2828 jsr166 1.13 * if (map.containsKey(key))
2829     * return map.get(key);
2830 dl 1.52 * value = mappingFunction.apply(key);
2831 dl 1.41 * if (value != null)
2832     * map.put(key, value);
2833 jsr166 1.13 * return value;}</pre>
2834 dl 1.1 *
2835 dl 1.27 * except that the action is performed atomically. If the
2836 dl 1.41 * function returns {@code null} no mapping is recorded. If the
2837     * function itself throws an (unchecked) exception, the exception
2838     * is rethrown to its caller, and no mapping is recorded. Some
2839     * attempted update operations on this map by other threads may be
2840     * blocked while computation is in progress, so the computation
2841     * should be short and simple, and must not attempt to update any
2842     * other mappings of this Map. The most appropriate usage is to
2843     * construct a new object serving as an initial mapped value, or
2844     * memoized result, as in:
2845 dl 1.27 *
2846 jsr166 1.13 * <pre> {@code
2847 dl 1.52 * map.computeIfAbsent(key, new Fun<K, V>() {
2848 jsr166 1.13 * public V map(K k) { return new Value(f(k)); }});}</pre>
2849 dl 1.1 *
2850     * @param key key with which the specified value is to be associated
2851     * @param mappingFunction the function to compute a value
2852     * @return the current (existing or computed) value associated with
2853 jsr166 1.67 * the specified key, or null if the computed value is null
2854 dl 1.41 * @throws NullPointerException if the specified key or mappingFunction
2855     * is null
2856 dl 1.5 * @throws IllegalStateException if the computation detectably
2857     * attempts a recursive update to this map that would
2858 jsr166 1.18 * otherwise never complete
2859 dl 1.1 * @throws RuntimeException or Error if the mappingFunction does so,
2860 jsr166 1.18 * in which case the mapping is left unestablished
2861 dl 1.1 */
2862 dl 1.61 @SuppressWarnings("unchecked") public V computeIfAbsent
2863     (K key, Fun<? super K, ? extends V> mappingFunction) {
2864 dl 1.1 if (key == null || mappingFunction == null)
2865     throw new NullPointerException();
2866 dl 1.27 return (V)internalComputeIfAbsent(key, mappingFunction);
2867 dl 1.2 }
2868    
2869     /**
2870 dl 1.52 * If the given key is present, computes a new mapping value given a key and
2871     * its current mapped value. This is equivalent to
2872     * <pre> {@code
2873     * if (map.containsKey(key)) {
2874     * value = remappingFunction.apply(key, map.get(key));
2875     * if (value != null)
2876     * map.put(key, value);
2877     * else
2878     * map.remove(key);
2879     * }
2880     * }</pre>
2881     *
2882     * except that the action is performed atomically. If the
2883     * function returns {@code null}, the mapping is removed. If the
2884     * function itself throws an (unchecked) exception, the exception
2885     * is rethrown to its caller, and the current mapping is left
2886     * unchanged. Some attempted update operations on this map by
2887     * other threads may be blocked while computation is in progress,
2888     * so the computation should be short and simple, and must not
2889     * attempt to update any other mappings of this Map. For example,
2890     * to either create or append new messages to a value mapping:
2891     *
2892     * @param key key with which the specified value is to be associated
2893     * @param remappingFunction the function to compute a value
2894 jsr166 1.56 * @return the new value associated with the specified key, or null if none
2895 dl 1.52 * @throws NullPointerException if the specified key or remappingFunction
2896     * is null
2897     * @throws IllegalStateException if the computation detectably
2898     * attempts a recursive update to this map that would
2899     * otherwise never complete
2900     * @throws RuntimeException or Error if the remappingFunction does so,
2901     * in which case the mapping is unchanged
2902     */
2903 dl 1.61 @SuppressWarnings("unchecked") public V computeIfPresent
2904     (K key, BiFun<? super K, ? super V, ? extends V> remappingFunction) {
2905 dl 1.52 if (key == null || remappingFunction == null)
2906     throw new NullPointerException();
2907     return (V)internalCompute(key, true, remappingFunction);
2908     }
2909    
2910     /**
2911 dl 1.41 * Computes a new mapping value given a key and
2912 dl 1.27 * its current mapped value (or {@code null} if there is no current
2913     * mapping). This is equivalent to
2914 jsr166 1.13 * <pre> {@code
2915 dl 1.52 * value = remappingFunction.apply(key, map.get(key));
2916 dl 1.41 * if (value != null)
2917     * map.put(key, value);
2918     * else
2919     * map.remove(key);
2920 dl 1.27 * }</pre>
2921 dl 1.2 *
2922 dl 1.27 * except that the action is performed atomically. If the
2923 dl 1.41 * function returns {@code null}, the mapping is removed. If the
2924     * function itself throws an (unchecked) exception, the exception
2925     * is rethrown to its caller, and the current mapping is left
2926     * unchanged. Some attempted update operations on this map by
2927     * other threads may be blocked while computation is in progress,
2928     * so the computation should be short and simple, and must not
2929     * attempt to update any other mappings of this Map. For example,
2930     * to either create or append new messages to a value mapping:
2931 dl 1.27 *
2932     * <pre> {@code
2933     * Map<Key, String> map = ...;
2934     * final String msg = ...;
2935 dl 1.52 * map.compute(key, new BiFun<Key, String, String>() {
2936     * public String apply(Key k, String v) {
2937 dl 1.28 * return (v == null) ? msg : v + msg;});}}</pre>
2938 dl 1.2 *
2939     * @param key key with which the specified value is to be associated
2940 dl 1.27 * @param remappingFunction the function to compute a value
2941 jsr166 1.56 * @return the new value associated with the specified key, or null if none
2942 dl 1.27 * @throws NullPointerException if the specified key or remappingFunction
2943 dl 1.41 * is null
2944 dl 1.5 * @throws IllegalStateException if the computation detectably
2945     * attempts a recursive update to this map that would
2946 jsr166 1.18 * otherwise never complete
2947 dl 1.29 * @throws RuntimeException or Error if the remappingFunction does so,
2948 jsr166 1.18 * in which case the mapping is unchanged
2949 dl 1.2 */
2950 dl 1.61 @SuppressWarnings("unchecked") public V compute
2951     (K key, BiFun<? super K, ? super V, ? extends V> remappingFunction) {
2952 dl 1.27 if (key == null || remappingFunction == null)
2953 dl 1.2 throw new NullPointerException();
2954 dl 1.52 return (V)internalCompute(key, false, remappingFunction);
2955     }
2956    
2957     /**
2958     * If the specified key is not already associated
2959     * with a value, associate it with the given value.
2960     * Otherwise, replace the value with the results of
2961     * the given remapping function. This is equivalent to:
2962     * <pre> {@code
2963     * if (!map.containsKey(key))
2964     * map.put(value);
2965     * else {
2966     * newValue = remappingFunction.apply(map.get(key), value);
2967     * if (value != null)
2968     * map.put(key, value);
2969     * else
2970     * map.remove(key);
2971     * }
2972     * }</pre>
2973     * except that the action is performed atomically. If the
2974     * function returns {@code null}, the mapping is removed. If the
2975     * function itself throws an (unchecked) exception, the exception
2976     * is rethrown to its caller, and the current mapping is left
2977     * unchanged. Some attempted update operations on this map by
2978     * other threads may be blocked while computation is in progress,
2979     * so the computation should be short and simple, and must not
2980     * attempt to update any other mappings of this Map.
2981     */
2982 dl 1.61 @SuppressWarnings("unchecked") public V merge
2983     (K key, V value, BiFun<? super V, ? super V, ? extends V> remappingFunction) {
2984 dl 1.52 if (key == null || value == null || remappingFunction == null)
2985     throw new NullPointerException();
2986     return (V)internalMerge(key, value, remappingFunction);
2987 dl 1.1 }
2988    
2989     /**
2990     * Removes the key (and its corresponding value) from this map.
2991     * This method does nothing if the key is not in the map.
2992     *
2993     * @param key the key that needs to be removed
2994     * @return the previous value associated with {@code key}, or
2995     * {@code null} if there was no mapping for {@code key}
2996     * @throws NullPointerException if the specified key is null
2997     */
2998 dl 1.61 @SuppressWarnings("unchecked") public V remove(Object key) {
2999 dl 1.1 if (key == null)
3000     throw new NullPointerException();
3001 jsr166 1.3 return (V)internalReplace(key, null, null);
3002 dl 1.1 }
3003    
3004     /**
3005     * {@inheritDoc}
3006     *
3007     * @throws NullPointerException if the specified key is null
3008     */
3009     public boolean remove(Object key, Object value) {
3010     if (key == null)
3011     throw new NullPointerException();
3012     if (value == null)
3013     return false;
3014     return internalReplace(key, null, value) != null;
3015     }
3016    
3017     /**
3018     * {@inheritDoc}
3019     *
3020     * @throws NullPointerException if any of the arguments are null
3021     */
3022     public boolean replace(K key, V oldValue, V newValue) {
3023     if (key == null || oldValue == null || newValue == null)
3024     throw new NullPointerException();
3025 jsr166 1.3 return internalReplace(key, newValue, oldValue) != null;
3026 dl 1.1 }
3027    
3028     /**
3029     * {@inheritDoc}
3030     *
3031     * @return the previous value associated with the specified key,
3032     * or {@code null} if there was no mapping for the key
3033     * @throws NullPointerException if the specified key or value is null
3034     */
3035 dl 1.61 @SuppressWarnings("unchecked") public V replace(K key, V value) {
3036 dl 1.1 if (key == null || value == null)
3037     throw new NullPointerException();
3038 jsr166 1.3 return (V)internalReplace(key, value, null);
3039 dl 1.1 }
3040    
3041     /**
3042     * Removes all of the mappings from this map.
3043     */
3044     public void clear() {
3045     internalClear();
3046     }
3047    
3048     /**
3049     * Returns a {@link Set} view of the keys contained in this map.
3050     * The set is backed by the map, so changes to the map are
3051 dl 1.70 * reflected in the set, and vice-versa.
3052     *
3053     * @return the set view
3054     */
3055     public KeySetView<K,V> keySet() {
3056     KeySetView<K,V> ks = keySet;
3057     return (ks != null) ? ks : (keySet = new KeySetView<K,V>(this, null));
3058     }
3059    
3060     /**
3061     * Returns a {@link Set} view of the keys in this map, using the
3062     * given common mapped value for any additions (i.e., {@link
3063     * Collection#add} and {@link Collection#addAll}). This is of
3064     * course only appropriate if it is acceptable to use the same
3065     * value for all additions from this view.
3066 dl 1.1 *
3067 dl 1.70 * @param mappedValue the mapped value to use for any
3068     * additions.
3069     * @return the set view
3070     * @throws NullPointerException if the mappedValue is null
3071 dl 1.1 */
3072 dl 1.70 public KeySetView<K,V> keySet(V mappedValue) {
3073     if (mappedValue == null)
3074     throw new NullPointerException();
3075     return new KeySetView<K,V>(this, mappedValue);
3076 dl 1.1 }
3077    
3078     /**
3079     * Returns a {@link Collection} view of the values contained in this map.
3080     * The collection is backed by the map, so changes to the map are
3081     * reflected in the collection, and vice-versa. The collection
3082     * supports element removal, which removes the corresponding
3083     * mapping from this map, via the {@code Iterator.remove},
3084     * {@code Collection.remove}, {@code removeAll},
3085     * {@code retainAll}, and {@code clear} operations. It does not
3086     * support the {@code add} or {@code addAll} operations.
3087     *
3088     * <p>The view's {@code iterator} is a "weakly consistent" iterator
3089     * that will never throw {@link ConcurrentModificationException},
3090     * and guarantees to traverse elements as they existed upon
3091     * construction of the iterator, and may (but is not guaranteed to)
3092     * reflect any modifications subsequent to construction.
3093     */
3094     public Collection<V> values() {
3095 dl 1.14 Values<K,V> vs = values;
3096     return (vs != null) ? vs : (values = new Values<K,V>(this));
3097 dl 1.1 }
3098    
3099     /**
3100     * Returns a {@link Set} view of the mappings contained in this map.
3101     * The set is backed by the map, so changes to the map are
3102     * reflected in the set, and vice-versa. The set supports element
3103     * removal, which removes the corresponding mapping from the map,
3104     * via the {@code Iterator.remove}, {@code Set.remove},
3105     * {@code removeAll}, {@code retainAll}, and {@code clear}
3106     * operations. It does not support the {@code add} or
3107     * {@code addAll} operations.
3108     *
3109     * <p>The view's {@code iterator} is a "weakly consistent" iterator
3110     * that will never throw {@link ConcurrentModificationException},
3111     * and guarantees to traverse elements as they existed upon
3112     * construction of the iterator, and may (but is not guaranteed to)
3113     * reflect any modifications subsequent to construction.
3114     */
3115     public Set<Map.Entry<K,V>> entrySet() {
3116 dl 1.14 EntrySet<K,V> es = entrySet;
3117     return (es != null) ? es : (entrySet = new EntrySet<K,V>(this));
3118 dl 1.1 }
3119    
3120     /**
3121     * Returns an enumeration of the keys in this table.
3122     *
3123     * @return an enumeration of the keys in this table
3124     * @see #keySet()
3125     */
3126     public Enumeration<K> keys() {
3127 dl 1.14 return new KeyIterator<K,V>(this);
3128 dl 1.1 }
3129    
3130     /**
3131     * Returns an enumeration of the values in this table.
3132     *
3133     * @return an enumeration of the values in this table
3134     * @see #values()
3135     */
3136     public Enumeration<V> elements() {
3137 dl 1.14 return new ValueIterator<K,V>(this);
3138 dl 1.1 }
3139    
3140     /**
3141 jsr166 1.55 * Returns a partitionable iterator of the keys in this map.
3142 dl 1.41 *
3143 jsr166 1.55 * @return a partitionable iterator of the keys in this map
3144 dl 1.41 */
3145     public Spliterator<K> keySpliterator() {
3146     return new KeyIterator<K,V>(this);
3147     }
3148    
3149     /**
3150 jsr166 1.55 * Returns a partitionable iterator of the values in this map.
3151 dl 1.41 *
3152 jsr166 1.55 * @return a partitionable iterator of the values in this map
3153 dl 1.41 */
3154     public Spliterator<V> valueSpliterator() {
3155     return new ValueIterator<K,V>(this);
3156     }
3157    
3158     /**
3159 jsr166 1.55 * Returns a partitionable iterator of the entries in this map.
3160 dl 1.41 *
3161 jsr166 1.55 * @return a partitionable iterator of the entries in this map
3162 dl 1.41 */
3163     public Spliterator<Map.Entry<K,V>> entrySpliterator() {
3164     return new EntryIterator<K,V>(this);
3165     }
3166    
3167     /**
3168 dl 1.2 * Returns the hash code value for this {@link Map}, i.e.,
3169     * the sum of, for each key-value pair in the map,
3170     * {@code key.hashCode() ^ value.hashCode()}.
3171     *
3172     * @return the hash code value for this map
3173 dl 1.1 */
3174     public int hashCode() {
3175 dl 1.14 int h = 0;
3176 dl 1.52 Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
3177 dl 1.41 Object v;
3178     while ((v = it.advance()) != null) {
3179     h += it.nextKey.hashCode() ^ v.hashCode();
3180 dl 1.14 }
3181     return h;
3182 dl 1.1 }
3183    
3184     /**
3185 dl 1.2 * Returns a string representation of this map. The string
3186     * representation consists of a list of key-value mappings (in no
3187     * particular order) enclosed in braces ("{@code {}}"). Adjacent
3188     * mappings are separated by the characters {@code ", "} (comma
3189     * and space). Each key-value mapping is rendered as the key
3190     * followed by an equals sign ("{@code =}") followed by the
3191     * associated value.
3192     *
3193     * @return a string representation of this map
3194 dl 1.1 */
3195     public String toString() {
3196 dl 1.52 Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
3197 dl 1.14 StringBuilder sb = new StringBuilder();
3198     sb.append('{');
3199 dl 1.41 Object v;
3200     if ((v = it.advance()) != null) {
3201 dl 1.14 for (;;) {
3202 dl 1.41 Object k = it.nextKey;
3203 dl 1.14 sb.append(k == this ? "(this Map)" : k);
3204     sb.append('=');
3205     sb.append(v == this ? "(this Map)" : v);
3206 dl 1.41 if ((v = it.advance()) == null)
3207 dl 1.14 break;
3208     sb.append(',').append(' ');
3209     }
3210     }
3211     return sb.append('}').toString();
3212 dl 1.1 }
3213    
3214     /**
3215 dl 1.2 * Compares the specified object with this map for equality.
3216     * Returns {@code true} if the given object is a map with the same
3217     * mappings as this map. This operation may return misleading
3218     * results if either map is concurrently modified during execution
3219     * of this method.
3220     *
3221     * @param o object to be compared for equality with this map
3222     * @return {@code true} if the specified object is equal to this map
3223 dl 1.1 */
3224     public boolean equals(Object o) {
3225 dl 1.14 if (o != this) {
3226     if (!(o instanceof Map))
3227     return false;
3228     Map<?,?> m = (Map<?,?>) o;
3229 dl 1.52 Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
3230 dl 1.41 Object val;
3231     while ((val = it.advance()) != null) {
3232 dl 1.14 Object v = m.get(it.nextKey);
3233     if (v == null || (v != val && !v.equals(val)))
3234 dl 1.1 return false;
3235 dl 1.14 }
3236 dl 1.1 for (Map.Entry<?,?> e : m.entrySet()) {
3237 dl 1.14 Object mk, mv, v;
3238     if ((mk = e.getKey()) == null ||
3239     (mv = e.getValue()) == null ||
3240     (v = internalGet(mk)) == null ||
3241     (mv != v && !mv.equals(v)))
3242 dl 1.1 return false;
3243     }
3244 dl 1.14 }
3245     return true;
3246     }
3247    
3248     /* ----------------Iterators -------------- */
3249    
3250 dl 1.61 @SuppressWarnings("serial") static final class KeyIterator<K,V> extends Traverser<K,V,Object>
3251 dl 1.41 implements Spliterator<K>, Enumeration<K> {
3252     KeyIterator(ConcurrentHashMapV8<K, V> map) { super(map); }
3253 dl 1.61 KeyIterator(Traverser<K,V,Object> it) {
3254     super(it);
3255 dl 1.41 }
3256     public KeyIterator<K,V> split() {
3257 dl 1.70 if (nextKey != null)
3258 dl 1.41 throw new IllegalStateException();
3259 dl 1.61 return new KeyIterator<K,V>(this);
3260 dl 1.14 }
3261 dl 1.61 @SuppressWarnings("unchecked") public final K next() {
3262 dl 1.41 if (nextVal == null && advance() == null)
3263 dl 1.14 throw new NoSuchElementException();
3264     Object k = nextKey;
3265 dl 1.41 nextVal = null;
3266     return (K) k;
3267 dl 1.14 }
3268    
3269     public final K nextElement() { return next(); }
3270     }
3271    
3272 dl 1.61 @SuppressWarnings("serial") static final class ValueIterator<K,V> extends Traverser<K,V,Object>
3273 dl 1.41 implements Spliterator<V>, Enumeration<V> {
3274 dl 1.14 ValueIterator(ConcurrentHashMapV8<K, V> map) { super(map); }
3275 dl 1.61 ValueIterator(Traverser<K,V,Object> it) {
3276     super(it);
3277 dl 1.41 }
3278     public ValueIterator<K,V> split() {
3279 dl 1.70 if (nextKey != null)
3280 dl 1.41 throw new IllegalStateException();
3281 dl 1.61 return new ValueIterator<K,V>(this);
3282 dl 1.41 }
3283    
3284 dl 1.61 @SuppressWarnings("unchecked") public final V next() {
3285 dl 1.41 Object v;
3286     if ((v = nextVal) == null && (v = advance()) == null)
3287 dl 1.14 throw new NoSuchElementException();
3288 dl 1.41 nextVal = null;
3289     return (V) v;
3290 dl 1.14 }
3291    
3292     public final V nextElement() { return next(); }
3293     }
3294    
3295 dl 1.61 @SuppressWarnings("serial") static final class EntryIterator<K,V> extends Traverser<K,V,Object>
3296 dl 1.41 implements Spliterator<Map.Entry<K,V>> {
3297 dl 1.14 EntryIterator(ConcurrentHashMapV8<K, V> map) { super(map); }
3298 dl 1.61 EntryIterator(Traverser<K,V,Object> it) {
3299     super(it);
3300 dl 1.41 }
3301     public EntryIterator<K,V> split() {
3302 dl 1.70 if (nextKey != null)
3303 dl 1.41 throw new IllegalStateException();
3304 dl 1.61 return new EntryIterator<K,V>(this);
3305 dl 1.41 }
3306 dl 1.24
3307 dl 1.61 @SuppressWarnings("unchecked") public final Map.Entry<K,V> next() {
3308 dl 1.41 Object v;
3309     if ((v = nextVal) == null && (v = advance()) == null)
3310 dl 1.24 throw new NoSuchElementException();
3311     Object k = nextKey;
3312 dl 1.41 nextVal = null;
3313     return new MapEntry<K,V>((K)k, (V)v, map);
3314 dl 1.1 }
3315     }
3316    
3317     /**
3318 dl 1.41 * Exported Entry for iterators
3319 dl 1.1 */
3320 dl 1.41 static final class MapEntry<K,V> implements Map.Entry<K, V> {
3321 dl 1.14 final K key; // non-null
3322     V val; // non-null
3323 dl 1.41 final ConcurrentHashMapV8<K, V> map;
3324     MapEntry(K key, V val, ConcurrentHashMapV8<K, V> map) {
3325     this.key = key;
3326     this.val = val;
3327     this.map = map;
3328     }
3329 dl 1.14 public final K getKey() { return key; }
3330     public final V getValue() { return val; }
3331     public final int hashCode() { return key.hashCode() ^ val.hashCode(); }
3332     public final String toString(){ return key + "=" + val; }
3333    
3334     public final boolean equals(Object o) {
3335     Object k, v; Map.Entry<?,?> e;
3336     return ((o instanceof Map.Entry) &&
3337     (k = (e = (Map.Entry<?,?>)o).getKey()) != null &&
3338     (v = e.getValue()) != null &&
3339     (k == key || k.equals(key)) &&
3340     (v == val || v.equals(val)));
3341 dl 1.1 }
3342    
3343     /**
3344     * Sets our entry's value and writes through to the map. The
3345 jsr166 1.50 * value to return is somewhat arbitrary here. Since we do not
3346     * necessarily track asynchronous changes, the most recent
3347 dl 1.41 * "previous" value could be different from what we return (or
3348     * could even have been removed in which case the put will
3349     * re-establish). We do not and cannot guarantee more.
3350 dl 1.1 */
3351 dl 1.14 public final V setValue(V value) {
3352 dl 1.1 if (value == null) throw new NullPointerException();
3353 dl 1.14 V v = val;
3354     val = value;
3355     map.put(key, value);
3356 dl 1.1 return v;
3357     }
3358     }
3359    
3360 dl 1.14 /* ----------------Views -------------- */
3361 dl 1.1
3362 dl 1.24 /**
3363 dl 1.41 * Base class for views.
3364 dl 1.14 */
3365 dl 1.52 static abstract class CHMView<K, V> {
3366 dl 1.14 final ConcurrentHashMapV8<K, V> map;
3367 dl 1.52 CHMView(ConcurrentHashMapV8<K, V> map) { this.map = map; }
3368 dl 1.14 public final int size() { return map.size(); }
3369     public final boolean isEmpty() { return map.isEmpty(); }
3370     public final void clear() { map.clear(); }
3371 dl 1.24
3372     // implementations below rely on concrete classes supplying these
3373 dl 1.41 abstract public Iterator<?> iterator();
3374 dl 1.24 abstract public boolean contains(Object o);
3375     abstract public boolean remove(Object o);
3376    
3377     private static final String oomeMsg = "Required array size too large";
3378    
3379     public final Object[] toArray() {
3380 dl 1.52 long sz = map.mappingCount();
3381 dl 1.24 if (sz > (long)(MAX_ARRAY_SIZE))
3382     throw new OutOfMemoryError(oomeMsg);
3383     int n = (int)sz;
3384     Object[] r = new Object[n];
3385     int i = 0;
3386 dl 1.41 Iterator<?> it = iterator();
3387 dl 1.24 while (it.hasNext()) {
3388     if (i == n) {
3389     if (n >= MAX_ARRAY_SIZE)
3390     throw new OutOfMemoryError(oomeMsg);
3391     if (n >= MAX_ARRAY_SIZE - (MAX_ARRAY_SIZE >>> 1) - 1)
3392     n = MAX_ARRAY_SIZE;
3393     else
3394     n += (n >>> 1) + 1;
3395     r = Arrays.copyOf(r, n);
3396     }
3397     r[i++] = it.next();
3398     }
3399     return (i == n) ? r : Arrays.copyOf(r, i);
3400     }
3401    
3402 dl 1.61 @SuppressWarnings("unchecked") public final <T> T[] toArray(T[] a) {
3403 dl 1.52 long sz = map.mappingCount();
3404 dl 1.24 if (sz > (long)(MAX_ARRAY_SIZE))
3405     throw new OutOfMemoryError(oomeMsg);
3406     int m = (int)sz;
3407     T[] r = (a.length >= m) ? a :
3408     (T[])java.lang.reflect.Array
3409     .newInstance(a.getClass().getComponentType(), m);
3410     int n = r.length;
3411     int i = 0;
3412 dl 1.41 Iterator<?> it = iterator();
3413 dl 1.24 while (it.hasNext()) {
3414     if (i == n) {
3415     if (n >= MAX_ARRAY_SIZE)
3416     throw new OutOfMemoryError(oomeMsg);
3417     if (n >= MAX_ARRAY_SIZE - (MAX_ARRAY_SIZE >>> 1) - 1)
3418     n = MAX_ARRAY_SIZE;
3419     else
3420     n += (n >>> 1) + 1;
3421     r = Arrays.copyOf(r, n);
3422     }
3423     r[i++] = (T)it.next();
3424     }
3425     if (a == r && i < n) {
3426     r[i] = null; // null-terminate
3427     return r;
3428     }
3429     return (i == n) ? r : Arrays.copyOf(r, i);
3430     }
3431    
3432     public final int hashCode() {
3433     int h = 0;
3434 dl 1.41 for (Iterator<?> it = iterator(); it.hasNext();)
3435 dl 1.24 h += it.next().hashCode();
3436     return h;
3437     }
3438    
3439     public final String toString() {
3440     StringBuilder sb = new StringBuilder();
3441     sb.append('[');
3442 dl 1.41 Iterator<?> it = iterator();
3443 dl 1.24 if (it.hasNext()) {
3444     for (;;) {
3445     Object e = it.next();
3446     sb.append(e == this ? "(this Collection)" : e);
3447     if (!it.hasNext())
3448     break;
3449     sb.append(',').append(' ');
3450     }
3451     }
3452     return sb.append(']').toString();
3453     }
3454    
3455     public final boolean containsAll(Collection<?> c) {
3456     if (c != this) {
3457     for (Iterator<?> it = c.iterator(); it.hasNext();) {
3458     Object e = it.next();
3459     if (e == null || !contains(e))
3460     return false;
3461     }
3462     }
3463     return true;
3464     }
3465    
3466 jsr166 1.32 public final boolean removeAll(Collection<?> c) {
3467 dl 1.24 boolean modified = false;
3468 dl 1.41 for (Iterator<?> it = iterator(); it.hasNext();) {
3469 dl 1.24 if (c.contains(it.next())) {
3470     it.remove();
3471     modified = true;
3472     }
3473     }
3474     return modified;
3475     }
3476    
3477     public final boolean retainAll(Collection<?> c) {
3478     boolean modified = false;
3479 dl 1.41 for (Iterator<?> it = iterator(); it.hasNext();) {
3480 dl 1.24 if (!c.contains(it.next())) {
3481     it.remove();
3482     modified = true;
3483     }
3484     }
3485     return modified;
3486     }
3487    
3488     }
3489    
3490 dl 1.52 static final class Values<K,V> extends CHMView<K,V>
3491 jsr166 1.34 implements Collection<V> {
3492 dl 1.24 Values(ConcurrentHashMapV8<K, V> map) { super(map); }
3493     public final boolean contains(Object o) { return map.containsValue(o); }
3494     public final boolean remove(Object o) {
3495     if (o != null) {
3496     Iterator<V> it = new ValueIterator<K,V>(map);
3497     while (it.hasNext()) {
3498     if (o.equals(it.next())) {
3499     it.remove();
3500     return true;
3501     }
3502     }
3503     }
3504     return false;
3505     }
3506 dl 1.14 public final Iterator<V> iterator() {
3507     return new ValueIterator<K,V>(map);
3508 dl 1.1 }
3509 dl 1.24 public final boolean add(V e) {
3510     throw new UnsupportedOperationException();
3511     }
3512     public final boolean addAll(Collection<? extends V> c) {
3513     throw new UnsupportedOperationException();
3514     }
3515 dl 1.52
3516 dl 1.1 }
3517    
3518 dl 1.52 static final class EntrySet<K,V> extends CHMView<K,V>
3519 dl 1.24 implements Set<Map.Entry<K,V>> {
3520     EntrySet(ConcurrentHashMapV8<K, V> map) { super(map); }
3521 dl 1.14 public final boolean contains(Object o) {
3522     Object k, v, r; Map.Entry<?,?> e;
3523     return ((o instanceof Map.Entry) &&
3524     (k = (e = (Map.Entry<?,?>)o).getKey()) != null &&
3525     (r = map.get(k)) != null &&
3526     (v = e.getValue()) != null &&
3527     (v == r || v.equals(r)));
3528 dl 1.1 }
3529 dl 1.14 public final boolean remove(Object o) {
3530     Object k, v; Map.Entry<?,?> e;
3531     return ((o instanceof Map.Entry) &&
3532     (k = (e = (Map.Entry<?,?>)o).getKey()) != null &&
3533     (v = e.getValue()) != null &&
3534     map.remove(k, v));
3535 dl 1.1 }
3536 dl 1.24 public final Iterator<Map.Entry<K,V>> iterator() {
3537     return new EntryIterator<K,V>(map);
3538     }
3539     public final boolean add(Entry<K,V> e) {
3540     throw new UnsupportedOperationException();
3541     }
3542     public final boolean addAll(Collection<? extends Entry<K,V>> c) {
3543     throw new UnsupportedOperationException();
3544     }
3545     public boolean equals(Object o) {
3546     Set<?> c;
3547     return ((o instanceof Set) &&
3548     ((c = (Set<?>)o) == this ||
3549     (containsAll(c) && c.containsAll(this))));
3550     }
3551 dl 1.1 }
3552    
3553     /* ---------------- Serialization Support -------------- */
3554    
3555     /**
3556 dl 1.14 * Stripped-down version of helper class used in previous version,
3557     * declared for the sake of serialization compatibility
3558 dl 1.1 */
3559 dl 1.14 static class Segment<K,V> implements Serializable {
3560 dl 1.1 private static final long serialVersionUID = 2249069246763182397L;
3561     final float loadFactor;
3562     Segment(float lf) { this.loadFactor = lf; }
3563     }
3564    
3565     /**
3566     * Saves the state of the {@code ConcurrentHashMapV8} instance to a
3567     * stream (i.e., serializes it).
3568     * @param s the stream
3569     * @serialData
3570     * the key (Object) and value (Object)
3571     * for each key-value mapping, followed by a null pair.
3572     * The key-value mappings are emitted in no particular order.
3573     */
3574 dl 1.61 @SuppressWarnings("unchecked") private void writeObject(java.io.ObjectOutputStream s)
3575 dl 1.52 throws java.io.IOException {
3576 dl 1.1 if (segments == null) { // for serialization compatibility
3577     segments = (Segment<K,V>[])
3578     new Segment<?,?>[DEFAULT_CONCURRENCY_LEVEL];
3579     for (int i = 0; i < segments.length; ++i)
3580 dl 1.16 segments[i] = new Segment<K,V>(LOAD_FACTOR);
3581 dl 1.1 }
3582     s.defaultWriteObject();
3583 dl 1.52 Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
3584 dl 1.41 Object v;
3585     while ((v = it.advance()) != null) {
3586 dl 1.14 s.writeObject(it.nextKey);
3587 dl 1.41 s.writeObject(v);
3588 dl 1.14 }
3589 dl 1.1 s.writeObject(null);
3590     s.writeObject(null);
3591     segments = null; // throw away
3592     }
3593    
3594     /**
3595 jsr166 1.9 * Reconstitutes the instance from a stream (that is, deserializes it).
3596 dl 1.1 * @param s the stream
3597     */
3598 dl 1.61 @SuppressWarnings("unchecked") private void readObject(java.io.ObjectInputStream s)
3599 dl 1.52 throws java.io.IOException, ClassNotFoundException {
3600 dl 1.1 s.defaultReadObject();
3601     this.segments = null; // unneeded
3602 jsr166 1.21 // initialize transient final field
3603 dl 1.14 UNSAFE.putObjectVolatile(this, counterOffset, new LongAdder());
3604    
3605     // Create all nodes, then place in table once size is known
3606     long size = 0L;
3607     Node p = null;
3608 dl 1.1 for (;;) {
3609 dl 1.14 K k = (K) s.readObject();
3610     V v = (V) s.readObject();
3611     if (k != null && v != null) {
3612 dl 1.38 int h = spread(k.hashCode());
3613     p = new Node(h, k, v, p);
3614 dl 1.14 ++size;
3615     }
3616     else
3617 dl 1.1 break;
3618 dl 1.14 }
3619     if (p != null) {
3620     boolean init = false;
3621 dl 1.24 int n;
3622     if (size >= (long)(MAXIMUM_CAPACITY >>> 1))
3623     n = MAXIMUM_CAPACITY;
3624     else {
3625     int sz = (int)size;
3626     n = tableSizeFor(sz + (sz >>> 1) + 1);
3627     }
3628     int sc = sizeCtl;
3629 dl 1.38 boolean collide = false;
3630 dl 1.24 if (n > sc &&
3631     UNSAFE.compareAndSwapInt(this, sizeCtlOffset, sc, -1)) {
3632 dl 1.14 try {
3633     if (table == null) {
3634     init = true;
3635     Node[] tab = new Node[n];
3636     int mask = n - 1;
3637     while (p != null) {
3638     int j = p.hash & mask;
3639     Node next = p.next;
3640 dl 1.38 Node q = p.next = tabAt(tab, j);
3641 dl 1.14 setTabAt(tab, j, p);
3642 dl 1.38 if (!collide && q != null && q.hash == p.hash)
3643     collide = true;
3644 dl 1.14 p = next;
3645     }
3646     table = tab;
3647     counter.add(size);
3648 dl 1.29 sc = n - (n >>> 2);
3649 dl 1.14 }
3650     } finally {
3651 dl 1.24 sizeCtl = sc;
3652 dl 1.14 }
3653 dl 1.38 if (collide) { // rescan and convert to TreeBins
3654     Node[] tab = table;
3655     for (int i = 0; i < tab.length; ++i) {
3656     int c = 0;
3657     for (Node e = tabAt(tab, i); e != null; e = e.next) {
3658     if (++c > TREE_THRESHOLD &&
3659     (e.key instanceof Comparable)) {
3660     replaceWithTreeBin(tab, i, e.key);
3661     break;
3662     }
3663     }
3664     }
3665     }
3666 dl 1.14 }
3667     if (!init) { // Can only happen if unsafely published.
3668     while (p != null) {
3669 dl 1.27 internalPut(p.key, p.val);
3670 dl 1.14 p = p.next;
3671     }
3672     }
3673 dl 1.1 }
3674     }
3675    
3676    
3677 dl 1.52 // -------------------------------------------------------
3678    
3679     // Sams
3680     /** Interface describing a void action of one argument */
3681     public interface Action<A> { void apply(A a); }
3682     /** Interface describing a void action of two arguments */
3683     public interface BiAction<A,B> { void apply(A a, B b); }
3684     /** Interface describing a function of one argument */
3685     public interface Fun<A,T> { T apply(A a); }
3686     /** Interface describing a function of two arguments */
3687     public interface BiFun<A,B,T> { T apply(A a, B b); }
3688     /** Interface describing a function of no arguments */
3689     public interface Generator<T> { T apply(); }
3690     /** Interface describing a function mapping its argument to a double */
3691     public interface ObjectToDouble<A> { double apply(A a); }
3692     /** Interface describing a function mapping its argument to a long */
3693     public interface ObjectToLong<A> { long apply(A a); }
3694     /** Interface describing a function mapping its argument to an int */
3695     public interface ObjectToInt<A> {int apply(A a); }
3696     /** Interface describing a function mapping two arguments to a double */
3697     public interface ObjectByObjectToDouble<A,B> { double apply(A a, B b); }
3698     /** Interface describing a function mapping two arguments to a long */
3699     public interface ObjectByObjectToLong<A,B> { long apply(A a, B b); }
3700     /** Interface describing a function mapping two arguments to an int */
3701     public interface ObjectByObjectToInt<A,B> {int apply(A a, B b); }
3702     /** Interface describing a function mapping a double to a double */
3703     public interface DoubleToDouble { double apply(double a); }
3704     /** Interface describing a function mapping a long to a long */
3705     public interface LongToLong { long apply(long a); }
3706     /** Interface describing a function mapping an int to an int */
3707     public interface IntToInt { int apply(int a); }
3708     /** Interface describing a function mapping two doubles to a double */
3709     public interface DoubleByDoubleToDouble { double apply(double a, double b); }
3710     /** Interface describing a function mapping two longs to a long */
3711     public interface LongByLongToLong { long apply(long a, long b); }
3712     /** Interface describing a function mapping two ints to an int */
3713     public interface IntByIntToInt { int apply(int a, int b); }
3714    
3715    
3716     // -------------------------------------------------------
3717    
3718     /**
3719 dl 1.70 * Performs the given action for each (key, value).
3720 dl 1.52 *
3721 dl 1.70 * @param action the action
3722 dl 1.52 */
3723 dl 1.70 public void forEach(BiAction<K,V> action) {
3724     ForkJoinTasks.forEach
3725     (this, action).invoke();
3726 dl 1.52 }
3727    
3728     /**
3729 dl 1.70 * Performs the given action for each non-null transformation
3730     * of each (key, value).
3731     *
3732     * @param transformer a function returning the transformation
3733     * for an element, or null of there is no transformation (in
3734     * which case the action is not applied).
3735     * @param action the action
3736 dl 1.52 */
3737 dl 1.70 public <U> void forEach(BiFun<? super K, ? super V, ? extends U> transformer,
3738     Action<U> action) {
3739     ForkJoinTasks.forEach
3740     (this, transformer, action).invoke();
3741     }
3742    
3743     /**
3744     * Returns a non-null result from applying the given search
3745     * function on each (key, value), or null if none. Upon
3746     * success, further element processing is suppressed and the
3747     * results of any other parallel invocations of the search
3748     * function are ignored.
3749     *
3750     * @param searchFunction a function returning a non-null
3751     * result on success, else null
3752     * @return a non-null result from applying the given search
3753     * function on each (key, value), or null if none
3754     */
3755     public <U> U search(BiFun<? super K, ? super V, ? extends U> searchFunction) {
3756     return ForkJoinTasks.search
3757     (this, searchFunction).invoke();
3758     }
3759    
3760     /**
3761     * Returns the result of accumulating the given transformation
3762     * of all (key, value) pairs using the given reducer to
3763     * combine values, or null if none.
3764     *
3765     * @param transformer a function returning the transformation
3766     * for an element, or null of there is no transformation (in
3767     * which case it is not combined).
3768     * @param reducer a commutative associative combining function
3769     * @return the result of accumulating the given transformation
3770     * of all (key, value) pairs
3771     */
3772     public <U> U reduce(BiFun<? super K, ? super V, ? extends U> transformer,
3773     BiFun<? super U, ? super U, ? extends U> reducer) {
3774     return ForkJoinTasks.reduce
3775     (this, transformer, reducer).invoke();
3776     }
3777 dl 1.52
3778 dl 1.70 /**
3779     * Returns the result of accumulating the given transformation
3780     * of all (key, value) pairs using the given reducer to
3781     * combine values, and the given basis as an identity value.
3782     *
3783     * @param transformer a function returning the transformation
3784     * for an element
3785     * @param basis the identity (initial default value) for the reduction
3786     * @param reducer a commutative associative combining function
3787     * @return the result of accumulating the given transformation
3788     * of all (key, value) pairs
3789     */
3790     public double reduceToDouble(ObjectByObjectToDouble<? super K, ? super V> transformer,
3791     double basis,
3792     DoubleByDoubleToDouble reducer) {
3793     return ForkJoinTasks.reduceToDouble
3794     (this, transformer, basis, reducer).invoke();
3795     }
3796    
3797     /**
3798     * Returns the result of accumulating the given transformation
3799     * of all (key, value) pairs using the given reducer to
3800     * combine values, and the given basis as an identity value.
3801     *
3802     * @param transformer a function returning the transformation
3803     * for an element
3804     * @param basis the identity (initial default value) for the reduction
3805     * @param reducer a commutative associative combining function
3806     * @return the result of accumulating the given transformation
3807     * of all (key, value) pairs
3808     */
3809     public long reduceToLong(ObjectByObjectToLong<? super K, ? super V> transformer,
3810     long basis,
3811     LongByLongToLong reducer) {
3812     return ForkJoinTasks.reduceToLong
3813     (this, transformer, basis, reducer).invoke();
3814     }
3815    
3816     /**
3817     * Returns the result of accumulating the given transformation
3818     * of all (key, value) pairs using the given reducer to
3819     * combine values, and the given basis as an identity value.
3820     *
3821     * @param transformer a function returning the transformation
3822     * for an element
3823     * @param basis the identity (initial default value) for the reduction
3824     * @param reducer a commutative associative combining function
3825     * @return the result of accumulating the given transformation
3826     * of all (key, value) pairs
3827     */
3828     public int reduceToInt(ObjectByObjectToInt<? super K, ? super V> transformer,
3829     int basis,
3830     IntByIntToInt reducer) {
3831     return ForkJoinTasks.reduceToInt
3832     (this, transformer, basis, reducer).invoke();
3833     }
3834 dl 1.52
3835 dl 1.70 /**
3836     * Performs the given action for each key.
3837     *
3838     * @param action the action
3839     */
3840     public void forEachKey(Action<K> action) {
3841     ForkJoinTasks.forEachKey
3842     (this, action).invoke();
3843     }
3844 dl 1.52
3845 dl 1.70 /**
3846     * Performs the given action for each non-null transformation
3847     * of each key.
3848     *
3849     * @param transformer a function returning the transformation
3850     * for an element, or null of there is no transformation (in
3851     * which case the action is not applied).
3852     * @param action the action
3853     */
3854     public <U> void forEachKey(Fun<? super K, ? extends U> transformer,
3855     Action<U> action) {
3856     ForkJoinTasks.forEachKey
3857     (this, transformer, action).invoke();
3858     }
3859 dl 1.52
3860 dl 1.70 /**
3861 dl 1.71 * Returns a non-null result from applying the given search
3862     * function on each key, or null if none. Upon success,
3863     * further element processing is suppressed and the results of
3864     * any other parallel invocations of the search function are
3865     * ignored.
3866     *
3867     * @param searchFunction a function returning a non-null
3868     * result on success, else null
3869     * @return a non-null result from applying the given search
3870     * function on each key, or null if none
3871     */
3872     public <U> U searchKeys(Fun<? super K, ? extends U> searchFunction) {
3873     return ForkJoinTasks.searchKeys
3874     (this, searchFunction).invoke();
3875     }
3876    
3877     /**
3878 dl 1.70 * Returns the result of accumulating all keys using the given
3879     * reducer to combine values, or null if none.
3880     *
3881     * @param reducer a commutative associative combining function
3882     * @return the result of accumulating all keys using the given
3883     * reducer to combine values, or null if none
3884     */
3885     public K reduceKeys(BiFun<? super K, ? super K, ? extends K> reducer) {
3886     return ForkJoinTasks.reduceKeys
3887     (this, reducer).invoke();
3888     }
3889 dl 1.1
3890 dl 1.70 /**
3891     * Returns the result of accumulating the given transformation
3892     * of all keys using the given reducer to combine values, or
3893     * null if none.
3894     *
3895     * @param transformer a function returning the transformation
3896     * for an element, or null of there is no transformation (in
3897     * which case it is not combined).
3898     * @param reducer a commutative associative combining function
3899     * @return the result of accumulating the given transformation
3900     * of all keys
3901     */
3902     public <U> U reduceKeys(Fun<? super K, ? extends U> transformer,
3903 dl 1.52 BiFun<? super U, ? super U, ? extends U> reducer) {
3904 dl 1.70 return ForkJoinTasks.reduceKeys
3905     (this, transformer, reducer).invoke();
3906     }
3907 dl 1.1
3908 dl 1.70 /**
3909     * Returns the result of accumulating the given transformation
3910     * of all keys using the given reducer to combine values, and
3911     * the given basis as an identity value.
3912     *
3913     * @param transformer a function returning the transformation
3914     * for an element
3915     * @param basis the identity (initial default value) for the reduction
3916     * @param reducer a commutative associative combining function
3917     * @return the result of accumulating the given transformation
3918     * of all keys
3919     */
3920     public double reduceKeysToDouble(ObjectToDouble<? super K> transformer,
3921 dl 1.52 double basis,
3922     DoubleByDoubleToDouble reducer) {
3923 dl 1.70 return ForkJoinTasks.reduceKeysToDouble
3924     (this, transformer, basis, reducer).invoke();
3925     }
3926 dl 1.52
3927 dl 1.70 /**
3928     * Returns the result of accumulating the given transformation
3929     * of all keys using the given reducer to combine values, and
3930     * the given basis as an identity value.
3931     *
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 keys
3938     */
3939     public long reduceKeysToLong(ObjectToLong<? super K> transformer,
3940 dl 1.52 long basis,
3941     LongByLongToLong reducer) {
3942 dl 1.70 return ForkJoinTasks.reduceKeysToLong
3943     (this, transformer, basis, reducer).invoke();
3944     }
3945 dl 1.52
3946 dl 1.70 /**
3947     * Returns the result of accumulating the given transformation
3948     * of all keys using the given reducer to combine values, and
3949     * the given basis as an identity value.
3950     *
3951     * @param transformer a function returning the transformation
3952     * for an element
3953     * @param basis the identity (initial default value) for the reduction
3954     * @param reducer a commutative associative combining function
3955     * @return the result of accumulating the given transformation
3956     * of all keys
3957     */
3958     public int reduceKeysToInt(ObjectToInt<? super K> transformer,
3959 dl 1.52 int basis,
3960     IntByIntToInt reducer) {
3961 dl 1.70 return ForkJoinTasks.reduceKeysToInt
3962     (this, transformer, basis, reducer).invoke();
3963     }
3964 dl 1.52
3965 dl 1.70 /**
3966     * Performs the given action for each value.
3967     *
3968     * @param action the action
3969     */
3970     public void forEachValue(Action<V> action) {
3971     ForkJoinTasks.forEachValue
3972     (this, action).invoke();
3973     }
3974 dl 1.52
3975 dl 1.70 /**
3976     * Performs the given action for each non-null transformation
3977     * of each value.
3978     *
3979     * @param transformer a function returning the transformation
3980     * for an element, or null of there is no transformation (in
3981     * which case the action is not applied).
3982     */
3983     public <U> void forEachValue(Fun<? super V, ? extends U> transformer,
3984     Action<U> action) {
3985     ForkJoinTasks.forEachValue
3986     (this, transformer, action).invoke();
3987     }
3988 dl 1.52
3989 dl 1.70 /**
3990     * Returns a non-null result from applying the given search
3991     * function on each value, or null if none. Upon success,
3992     * further element processing is suppressed and the results of
3993     * any other parallel invocations of the search function are
3994     * ignored.
3995     *
3996     * @param searchFunction a function returning a non-null
3997     * result on success, else null
3998     * @return a non-null result from applying the given search
3999     * function on each value, or null if none
4000     *
4001     */
4002     public <U> U searchValues(Fun<? super V, ? extends U> searchFunction) {
4003     return ForkJoinTasks.searchValues
4004     (this, searchFunction).invoke();
4005     }
4006 dl 1.52
4007 dl 1.70 /**
4008     * Returns the result of accumulating all values using the
4009     * given reducer to combine values, or null if none.
4010     *
4011     * @param reducer a commutative associative combining function
4012     * @return the result of accumulating all values
4013     */
4014     public V reduceValues(BiFun<? super V, ? super V, ? extends V> reducer) {
4015     return ForkJoinTasks.reduceValues
4016     (this, reducer).invoke();
4017     }
4018 dl 1.52
4019 dl 1.70 /**
4020     * Returns the result of accumulating the given transformation
4021     * of all values using the given reducer to combine values, or
4022     * null if none.
4023     *
4024     * @param transformer a function returning the transformation
4025     * for an element, or null of there is no transformation (in
4026     * which case it is not combined).
4027     * @param reducer a commutative associative combining function
4028     * @return the result of accumulating the given transformation
4029     * of all values
4030     */
4031     public <U> U reduceValues(Fun<? super V, ? extends U> transformer,
4032     BiFun<? super U, ? super U, ? extends U> reducer) {
4033     return ForkJoinTasks.reduceValues
4034     (this, transformer, reducer).invoke();
4035     }
4036 dl 1.52
4037 dl 1.70 /**
4038     * Returns the result of accumulating the given transformation
4039     * of all values using the given reducer to combine values,
4040     * and the given basis as an identity value.
4041     *
4042     * @param transformer a function returning the transformation
4043     * for an element
4044     * @param basis the identity (initial default value) for the reduction
4045     * @param reducer a commutative associative combining function
4046     * @return the result of accumulating the given transformation
4047     * of all values
4048     */
4049     public double reduceValuesToDouble(ObjectToDouble<? super V> transformer,
4050     double basis,
4051     DoubleByDoubleToDouble reducer) {
4052     return ForkJoinTasks.reduceValuesToDouble
4053     (this, transformer, basis, reducer).invoke();
4054     }
4055 dl 1.52
4056 dl 1.70 /**
4057     * Returns the result of accumulating the given transformation
4058     * of all values using the given reducer to combine values,
4059     * and the given basis as an identity value.
4060     *
4061     * @param transformer a function returning the transformation
4062     * for an element
4063     * @param basis the identity (initial default value) for the reduction
4064     * @param reducer a commutative associative combining function
4065     * @return the result of accumulating the given transformation
4066     * of all values
4067     */
4068     public long reduceValuesToLong(ObjectToLong<? super V> transformer,
4069     long basis,
4070     LongByLongToLong reducer) {
4071     return ForkJoinTasks.reduceValuesToLong
4072     (this, transformer, basis, reducer).invoke();
4073     }
4074 dl 1.52
4075 dl 1.70 /**
4076     * Returns the result of accumulating the given transformation
4077     * of all values using the given reducer to combine values,
4078     * and the given basis as an identity value.
4079     *
4080     * @param transformer a function returning the transformation
4081     * for an element
4082     * @param basis the identity (initial default value) for the reduction
4083     * @param reducer a commutative associative combining function
4084     * @return the result of accumulating the given transformation
4085     * of all values
4086     */
4087     public int reduceValuesToInt(ObjectToInt<? super V> transformer,
4088     int basis,
4089     IntByIntToInt reducer) {
4090     return ForkJoinTasks.reduceValuesToInt
4091     (this, transformer, basis, reducer).invoke();
4092     }
4093 dl 1.52
4094 dl 1.70 /**
4095     * Performs the given action for each entry.
4096     *
4097     * @param action the action
4098     */
4099     public void forEachEntry(Action<Map.Entry<K,V>> action) {
4100     ForkJoinTasks.forEachEntry
4101     (this, action).invoke();
4102     }
4103 dl 1.52
4104 dl 1.70 /**
4105     * Performs the given action for each non-null transformation
4106     * of each entry.
4107     *
4108     * @param transformer a function returning the transformation
4109     * for an element, or null of there is no transformation (in
4110     * which case the action is not applied).
4111     * @param action the action
4112     */
4113     public <U> void forEachEntry(Fun<Map.Entry<K,V>, ? extends U> transformer,
4114     Action<U> action) {
4115     ForkJoinTasks.forEachEntry
4116     (this, transformer, action).invoke();
4117     }
4118 dl 1.52
4119 dl 1.70 /**
4120     * Returns a non-null result from applying the given search
4121     * function on each entry, or null if none. Upon success,
4122     * further element processing is suppressed and the results of
4123     * any other parallel invocations of the search function are
4124     * ignored.
4125     *
4126     * @param searchFunction a function returning a non-null
4127     * result on success, else null
4128     * @return a non-null result from applying the given search
4129     * function on each entry, or null if none
4130     */
4131     public <U> U searchEntries(Fun<Map.Entry<K,V>, ? extends U> searchFunction) {
4132     return ForkJoinTasks.searchEntries
4133     (this, searchFunction).invoke();
4134     }
4135 dl 1.52
4136 dl 1.70 /**
4137     * Returns the result of accumulating all entries using the
4138     * given reducer to combine values, or null if none.
4139     *
4140     * @param reducer a commutative associative combining function
4141     * @return the result of accumulating all entries
4142     */
4143     public Map.Entry<K,V> reduceEntries(BiFun<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer) {
4144     return ForkJoinTasks.reduceEntries
4145     (this, reducer).invoke();
4146     }
4147 dl 1.52
4148 dl 1.70 /**
4149     * Returns the result of accumulating the given transformation
4150     * of all entries using the given reducer to combine values,
4151     * or null if none.
4152     *
4153     * @param transformer a function returning the transformation
4154     * for an element, or null of there is no transformation (in
4155     * which case it is not combined).
4156     * @param reducer a commutative associative combining function
4157     * @return the result of accumulating the given transformation
4158     * of all entries
4159     */
4160     public <U> U reduceEntries(Fun<Map.Entry<K,V>, ? extends U> transformer,
4161     BiFun<? super U, ? super U, ? extends U> reducer) {
4162     return ForkJoinTasks.reduceEntries
4163     (this, transformer, reducer).invoke();
4164     }
4165 dl 1.52
4166 dl 1.70 /**
4167     * Returns the result of accumulating the given transformation
4168     * of all entries using the given reducer to combine values,
4169     * and the given basis as an identity value.
4170     *
4171     * @param transformer a function returning the transformation
4172     * for an element
4173     * @param basis the identity (initial default value) for the reduction
4174     * @param reducer a commutative associative combining function
4175     * @return the result of accumulating the given transformation
4176     * of all entries
4177     */
4178     public double reduceEntriesToDouble(ObjectToDouble<Map.Entry<K,V>> transformer,
4179     double basis,
4180     DoubleByDoubleToDouble reducer) {
4181     return ForkJoinTasks.reduceEntriesToDouble
4182     (this, transformer, basis, reducer).invoke();
4183     }
4184 dl 1.52
4185 dl 1.70 /**
4186     * Returns the result of accumulating the given transformation
4187     * of all entries using the given reducer to combine values,
4188     * and the given basis as an identity value.
4189     *
4190     * @param transformer a function returning the transformation
4191     * for an element
4192     * @param basis the identity (initial default value) for the reduction
4193     * @param reducer a commutative associative combining function
4194     * @return the result of accumulating the given transformation
4195     * of all entries
4196     */
4197     public long reduceEntriesToLong(ObjectToLong<Map.Entry<K,V>> transformer,
4198     long basis,
4199     LongByLongToLong reducer) {
4200     return ForkJoinTasks.reduceEntriesToLong
4201     (this, transformer, basis, reducer).invoke();
4202     }
4203 dl 1.52
4204 dl 1.70 /**
4205     * Returns the result of accumulating the given transformation
4206     * of all entries using the given reducer to combine values,
4207     * and the given basis as an identity value.
4208     *
4209     * @param transformer a function returning the transformation
4210     * for an element
4211     * @param basis the identity (initial default value) for the reduction
4212     * @param reducer a commutative associative combining function
4213     * @return the result of accumulating the given transformation
4214     * of all entries
4215     */
4216     public int reduceEntriesToInt(ObjectToInt<Map.Entry<K,V>> transformer,
4217     int basis,
4218     IntByIntToInt reducer) {
4219     return ForkJoinTasks.reduceEntriesToInt
4220     (this, transformer, basis, reducer).invoke();
4221 dl 1.52 }
4222    
4223     // ---------------------------------------------------------------------
4224    
4225     /**
4226     * Predefined tasks for performing bulk parallel operations on
4227 dl 1.70 * ConcurrentHashMapV8s. These tasks follow the forms and rules used
4228     * for bulk operations. Each method has the same name, but returns
4229     * a task rather than invoking it. These methods may be useful in
4230     * custom applications such as submitting a task without waiting
4231     * for completion, using a custom pool, or combining with other
4232     * tasks.
4233 dl 1.52 */
4234     public static class ForkJoinTasks {
4235     private ForkJoinTasks() {}
4236    
4237     /**
4238     * Returns a task that when invoked, performs the given
4239     * action for each (key, value)
4240     *
4241     * @param map the map
4242     * @param action the action
4243     * @return the task
4244     */
4245 jsr166 1.53 public static <K,V> ForkJoinTask<Void> forEach
4246 dl 1.52 (ConcurrentHashMapV8<K,V> map,
4247     BiAction<K,V> action) {
4248     if (action == null) throw new NullPointerException();
4249 dl 1.70 return new ForEachMappingTask<K,V>(map, null, -1, null, action);
4250 dl 1.52 }
4251    
4252     /**
4253     * Returns a task that when invoked, performs the given
4254     * action for each non-null transformation of each (key, value)
4255     *
4256     * @param map the map
4257     * @param transformer a function returning the transformation
4258 jsr166 1.68 * for an element, or null if there is no transformation (in
4259 jsr166 1.67 * which case the action is not applied)
4260 dl 1.52 * @param action the action
4261     * @return the task
4262     */
4263 jsr166 1.53 public static <K,V,U> ForkJoinTask<Void> forEach
4264 dl 1.52 (ConcurrentHashMapV8<K,V> map,
4265     BiFun<? super K, ? super V, ? extends U> transformer,
4266     Action<U> action) {
4267     if (transformer == null || action == null)
4268     throw new NullPointerException();
4269     return new ForEachTransformedMappingTask<K,V,U>
4270 dl 1.70 (map, null, -1, null, transformer, action);
4271 dl 1.52 }
4272    
4273     /**
4274 dl 1.59 * Returns a task that when invoked, returns a non-null result
4275     * from applying the given search function on each (key,
4276     * value), or null if none. Upon success, further element
4277     * processing is suppressed and the results of any other
4278     * parallel invocations of the search function are ignored.
4279 dl 1.52 *
4280     * @param map the map
4281     * @param searchFunction a function returning a non-null
4282     * result on success, else null
4283     * @return the task
4284     */
4285     public static <K,V,U> ForkJoinTask<U> search
4286     (ConcurrentHashMapV8<K,V> map,
4287     BiFun<? super K, ? super V, ? extends U> searchFunction) {
4288     if (searchFunction == null) throw new NullPointerException();
4289     return new SearchMappingsTask<K,V,U>
4290 dl 1.70 (map, null, -1, null, searchFunction,
4291 dl 1.52 new AtomicReference<U>());
4292     }
4293    
4294     /**
4295     * Returns a task that when invoked, returns the result of
4296     * accumulating the given transformation of all (key, value) pairs
4297     * using the given reducer to combine values, or null if none.
4298     *
4299     * @param map the map
4300     * @param transformer a function returning the transformation
4301 jsr166 1.68 * for an element, or null if there is no transformation (in
4302 dl 1.52 * which case it is not combined).
4303     * @param reducer a commutative associative combining function
4304     * @return the task
4305     */
4306     public static <K,V,U> ForkJoinTask<U> reduce
4307     (ConcurrentHashMapV8<K,V> map,
4308     BiFun<? super K, ? super V, ? extends U> transformer,
4309     BiFun<? super U, ? super U, ? extends U> reducer) {
4310     if (transformer == null || reducer == null)
4311     throw new NullPointerException();
4312     return new MapReduceMappingsTask<K,V,U>
4313 dl 1.63 (map, null, -1, null, transformer, reducer);
4314 dl 1.52 }
4315    
4316     /**
4317     * Returns a task that when invoked, returns the result of
4318     * accumulating the given transformation of all (key, value) pairs
4319     * using the given reducer to combine values, and the given
4320     * basis as an identity value.
4321     *
4322     * @param map the map
4323     * @param transformer a function returning the transformation
4324     * for an element
4325     * @param basis the identity (initial default value) for the reduction
4326     * @param reducer a commutative associative combining function
4327     * @return the task
4328     */
4329     public static <K,V> ForkJoinTask<Double> reduceToDouble
4330     (ConcurrentHashMapV8<K,V> map,
4331     ObjectByObjectToDouble<? super K, ? super V> transformer,
4332     double basis,
4333     DoubleByDoubleToDouble reducer) {
4334     if (transformer == null || reducer == null)
4335     throw new NullPointerException();
4336     return new MapReduceMappingsToDoubleTask<K,V>
4337 dl 1.63 (map, null, -1, null, transformer, basis, reducer);
4338 dl 1.52 }
4339    
4340     /**
4341     * Returns a task that when invoked, returns the result of
4342     * accumulating the given transformation of all (key, value) pairs
4343     * using the given reducer to combine values, and the given
4344     * basis as an identity value.
4345     *
4346     * @param map the map
4347     * @param transformer a function returning the transformation
4348     * for an element
4349     * @param basis the identity (initial default value) for the reduction
4350     * @param reducer a commutative associative combining function
4351     * @return the task
4352     */
4353     public static <K,V> ForkJoinTask<Long> reduceToLong
4354     (ConcurrentHashMapV8<K,V> map,
4355     ObjectByObjectToLong<? super K, ? super V> transformer,
4356     long basis,
4357     LongByLongToLong reducer) {
4358     if (transformer == null || reducer == null)
4359     throw new NullPointerException();
4360     return new MapReduceMappingsToLongTask<K,V>
4361 dl 1.63 (map, null, -1, null, transformer, basis, reducer);
4362 dl 1.52 }
4363    
4364     /**
4365     * Returns a task that when invoked, returns the result of
4366     * accumulating the given transformation of all (key, value) pairs
4367     * using the given reducer to combine values, and the given
4368     * basis as an identity value.
4369     *
4370     * @param transformer a function returning the transformation
4371     * for an element
4372     * @param basis the identity (initial default value) for the reduction
4373     * @param reducer a commutative associative combining function
4374     * @return the task
4375     */
4376     public static <K,V> ForkJoinTask<Integer> reduceToInt
4377     (ConcurrentHashMapV8<K,V> map,
4378     ObjectByObjectToInt<? super K, ? super V> transformer,
4379     int basis,
4380     IntByIntToInt reducer) {
4381     if (transformer == null || reducer == null)
4382     throw new NullPointerException();
4383     return new MapReduceMappingsToIntTask<K,V>
4384 dl 1.63 (map, null, -1, null, transformer, basis, reducer);
4385 dl 1.52 }
4386    
4387     /**
4388     * Returns a task that when invoked, performs the given action
4389 jsr166 1.56 * for each key.
4390 dl 1.52 *
4391     * @param map the map
4392     * @param action the action
4393     * @return the task
4394     */
4395 jsr166 1.53 public static <K,V> ForkJoinTask<Void> forEachKey
4396 dl 1.52 (ConcurrentHashMapV8<K,V> map,
4397     Action<K> action) {
4398     if (action == null) throw new NullPointerException();
4399 dl 1.70 return new ForEachKeyTask<K,V>(map, null, -1, null, action);
4400 dl 1.52 }
4401    
4402     /**
4403     * Returns a task that when invoked, performs the given action
4404 jsr166 1.56 * for each non-null transformation of each key.
4405 dl 1.52 *
4406     * @param map the map
4407     * @param transformer a function returning the transformation
4408 jsr166 1.68 * for an element, or null if there is no transformation (in
4409 jsr166 1.67 * which case the action is not applied)
4410 dl 1.52 * @param action the action
4411     * @return the task
4412     */
4413 jsr166 1.53 public static <K,V,U> ForkJoinTask<Void> forEachKey
4414 dl 1.52 (ConcurrentHashMapV8<K,V> map,
4415     Fun<? super K, ? extends U> transformer,
4416     Action<U> action) {
4417     if (transformer == null || action == null)
4418     throw new NullPointerException();
4419     return new ForEachTransformedKeyTask<K,V,U>
4420 dl 1.70 (map, null, -1, null, transformer, action);
4421 dl 1.52 }
4422    
4423     /**
4424     * Returns a task that when invoked, returns a non-null result
4425     * from applying the given search function on each key, or
4426 dl 1.59 * null if none. Upon success, further element processing is
4427     * suppressed and the results of any other parallel
4428     * invocations of the search function are ignored.
4429 dl 1.52 *
4430     * @param map the map
4431     * @param searchFunction a function returning a non-null
4432     * result on success, else null
4433     * @return the task
4434     */
4435     public static <K,V,U> ForkJoinTask<U> searchKeys
4436     (ConcurrentHashMapV8<K,V> map,
4437     Fun<? super K, ? extends U> searchFunction) {
4438     if (searchFunction == null) throw new NullPointerException();
4439     return new SearchKeysTask<K,V,U>
4440 dl 1.70 (map, null, -1, null, searchFunction,
4441 dl 1.52 new AtomicReference<U>());
4442     }
4443    
4444     /**
4445     * Returns a task that when invoked, returns the result of
4446     * accumulating all keys using the given reducer to combine
4447     * values, or null if none.
4448     *
4449     * @param map the map
4450     * @param reducer a commutative associative combining function
4451     * @return the task
4452     */
4453     public static <K,V> ForkJoinTask<K> reduceKeys
4454     (ConcurrentHashMapV8<K,V> map,
4455     BiFun<? super K, ? super K, ? extends K> reducer) {
4456     if (reducer == null) throw new NullPointerException();
4457     return new ReduceKeysTask<K,V>
4458 dl 1.63 (map, null, -1, null, reducer);
4459 dl 1.52 }
4460 jsr166 1.58
4461 dl 1.52 /**
4462     * Returns a task that when invoked, returns the result of
4463     * accumulating the given transformation of all keys using the given
4464     * reducer to combine values, or null if none.
4465     *
4466     * @param map the map
4467     * @param transformer a function returning the transformation
4468 jsr166 1.68 * for an element, or null if there is no transformation (in
4469 dl 1.52 * which case it is not combined).
4470     * @param reducer a commutative associative combining function
4471     * @return the task
4472     */
4473     public static <K,V,U> ForkJoinTask<U> reduceKeys
4474     (ConcurrentHashMapV8<K,V> map,
4475     Fun<? super K, ? extends U> transformer,
4476     BiFun<? super U, ? super U, ? extends U> reducer) {
4477     if (transformer == null || reducer == null)
4478     throw new NullPointerException();
4479     return new MapReduceKeysTask<K,V,U>
4480 dl 1.63 (map, null, -1, null, transformer, reducer);
4481 dl 1.52 }
4482    
4483     /**
4484     * Returns a task that when invoked, returns the result of
4485     * accumulating the given transformation of all keys using the given
4486     * reducer to combine values, and the given basis as an
4487     * identity value.
4488     *
4489     * @param map the map
4490     * @param transformer a function returning the transformation
4491     * for an element
4492     * @param basis the identity (initial default value) for the reduction
4493     * @param reducer a commutative associative combining function
4494     * @return the task
4495     */
4496     public static <K,V> ForkJoinTask<Double> reduceKeysToDouble
4497     (ConcurrentHashMapV8<K,V> map,
4498     ObjectToDouble<? super K> transformer,
4499     double basis,
4500     DoubleByDoubleToDouble reducer) {
4501     if (transformer == null || reducer == null)
4502     throw new NullPointerException();
4503     return new MapReduceKeysToDoubleTask<K,V>
4504 dl 1.63 (map, null, -1, null, transformer, basis, reducer);
4505 dl 1.52 }
4506    
4507     /**
4508     * Returns a task that when invoked, returns the result of
4509     * accumulating the given transformation of all keys using the given
4510     * reducer to combine values, and the given basis as an
4511     * identity value.
4512     *
4513     * @param map the map
4514     * @param transformer a function returning the transformation
4515     * for an element
4516     * @param basis the identity (initial default value) for the reduction
4517     * @param reducer a commutative associative combining function
4518     * @return the task
4519     */
4520     public static <K,V> ForkJoinTask<Long> reduceKeysToLong
4521     (ConcurrentHashMapV8<K,V> map,
4522     ObjectToLong<? super K> transformer,
4523     long basis,
4524     LongByLongToLong reducer) {
4525     if (transformer == null || reducer == null)
4526     throw new NullPointerException();
4527     return new MapReduceKeysToLongTask<K,V>
4528 dl 1.63 (map, null, -1, null, transformer, basis, reducer);
4529 dl 1.52 }
4530    
4531     /**
4532     * Returns a task that when invoked, returns the result of
4533     * accumulating the given transformation of all keys using the given
4534     * reducer to combine values, and the given basis as an
4535     * identity value.
4536     *
4537     * @param map the map
4538     * @param transformer a function returning the transformation
4539     * for an element
4540     * @param basis the identity (initial default value) for the reduction
4541     * @param reducer a commutative associative combining function
4542     * @return the task
4543     */
4544     public static <K,V> ForkJoinTask<Integer> reduceKeysToInt
4545     (ConcurrentHashMapV8<K,V> map,
4546     ObjectToInt<? super K> transformer,
4547     int basis,
4548     IntByIntToInt reducer) {
4549     if (transformer == null || reducer == null)
4550     throw new NullPointerException();
4551     return new MapReduceKeysToIntTask<K,V>
4552 dl 1.63 (map, null, -1, null, transformer, basis, reducer);
4553 dl 1.52 }
4554    
4555     /**
4556     * Returns a task that when invoked, performs the given action
4557 jsr166 1.56 * for each value.
4558 dl 1.52 *
4559     * @param map the map
4560     * @param action the action
4561     */
4562 jsr166 1.53 public static <K,V> ForkJoinTask<Void> forEachValue
4563 dl 1.52 (ConcurrentHashMapV8<K,V> map,
4564     Action<V> action) {
4565     if (action == null) throw new NullPointerException();
4566 dl 1.70 return new ForEachValueTask<K,V>(map, null, -1, null, action);
4567 dl 1.52 }
4568    
4569     /**
4570     * Returns a task that when invoked, performs the given action
4571 jsr166 1.56 * for each non-null transformation of each value.
4572 dl 1.52 *
4573     * @param map the map
4574     * @param transformer a function returning the transformation
4575 jsr166 1.68 * for an element, or null if there is no transformation (in
4576 jsr166 1.67 * which case the action is not applied)
4577 dl 1.52 * @param action the action
4578     */
4579 jsr166 1.53 public static <K,V,U> ForkJoinTask<Void> forEachValue
4580 dl 1.52 (ConcurrentHashMapV8<K,V> map,
4581     Fun<? super V, ? extends U> transformer,
4582     Action<U> action) {
4583     if (transformer == null || action == null)
4584     throw new NullPointerException();
4585     return new ForEachTransformedValueTask<K,V,U>
4586 dl 1.70 (map, null, -1, null, transformer, action);
4587 dl 1.52 }
4588    
4589     /**
4590     * Returns a task that when invoked, returns a non-null result
4591     * from applying the given search function on each value, or
4592 dl 1.59 * null if none. Upon success, further element processing is
4593     * suppressed and the results of any other parallel
4594     * invocations of the search function are ignored.
4595 dl 1.52 *
4596     * @param map the map
4597     * @param searchFunction a function returning a non-null
4598     * result on success, else null
4599     * @return the task
4600     */
4601     public static <K,V,U> ForkJoinTask<U> searchValues
4602     (ConcurrentHashMapV8<K,V> map,
4603     Fun<? super V, ? extends U> searchFunction) {
4604     if (searchFunction == null) throw new NullPointerException();
4605     return new SearchValuesTask<K,V,U>
4606 dl 1.70 (map, null, -1, null, searchFunction,
4607 dl 1.52 new AtomicReference<U>());
4608     }
4609    
4610     /**
4611     * Returns a task that when invoked, returns the result of
4612     * accumulating all values using the given reducer to combine
4613     * values, or null if none.
4614     *
4615     * @param map the map
4616     * @param reducer a commutative associative combining function
4617     * @return the task
4618     */
4619     public static <K,V> ForkJoinTask<V> reduceValues
4620     (ConcurrentHashMapV8<K,V> map,
4621     BiFun<? super V, ? super V, ? extends V> reducer) {
4622     if (reducer == null) throw new NullPointerException();
4623     return new ReduceValuesTask<K,V>
4624 dl 1.63 (map, null, -1, null, reducer);
4625 dl 1.52 }
4626    
4627     /**
4628     * Returns a task that when invoked, returns the result of
4629     * accumulating the given transformation of all values using the
4630     * given reducer to combine values, or null if none.
4631     *
4632     * @param map the map
4633     * @param transformer a function returning the transformation
4634 jsr166 1.68 * for an element, or null if there is no transformation (in
4635 dl 1.52 * which case it is not combined).
4636     * @param reducer a commutative associative combining function
4637     * @return the task
4638     */
4639     public static <K,V,U> ForkJoinTask<U> reduceValues
4640     (ConcurrentHashMapV8<K,V> map,
4641     Fun<? super V, ? extends U> transformer,
4642     BiFun<? super U, ? super U, ? extends U> reducer) {
4643     if (transformer == null || reducer == null)
4644     throw new NullPointerException();
4645     return new MapReduceValuesTask<K,V,U>
4646 dl 1.63 (map, null, -1, null, transformer, reducer);
4647 dl 1.52 }
4648    
4649     /**
4650     * Returns a task that when invoked, returns the result of
4651     * accumulating the given transformation of all values using the
4652     * given reducer to combine values, and the given basis as an
4653     * identity value.
4654     *
4655     * @param map the map
4656     * @param transformer a function returning the transformation
4657     * for an element
4658     * @param basis the identity (initial default value) for the reduction
4659     * @param reducer a commutative associative combining function
4660     * @return the task
4661     */
4662     public static <K,V> ForkJoinTask<Double> reduceValuesToDouble
4663     (ConcurrentHashMapV8<K,V> map,
4664     ObjectToDouble<? super V> transformer,
4665     double basis,
4666     DoubleByDoubleToDouble reducer) {
4667     if (transformer == null || reducer == null)
4668     throw new NullPointerException();
4669     return new MapReduceValuesToDoubleTask<K,V>
4670 dl 1.63 (map, null, -1, null, transformer, basis, reducer);
4671 dl 1.52 }
4672    
4673     /**
4674     * Returns a task that when invoked, returns the result of
4675     * accumulating the given transformation of all values using the
4676     * given reducer to combine values, and the given basis as an
4677     * identity value.
4678     *
4679     * @param map the map
4680     * @param transformer a function returning the transformation
4681     * for an element
4682     * @param basis the identity (initial default value) for the reduction
4683     * @param reducer a commutative associative combining function
4684     * @return the task
4685     */
4686     public static <K,V> ForkJoinTask<Long> reduceValuesToLong
4687     (ConcurrentHashMapV8<K,V> map,
4688     ObjectToLong<? super V> transformer,
4689     long basis,
4690     LongByLongToLong reducer) {
4691     if (transformer == null || reducer == null)
4692     throw new NullPointerException();
4693     return new MapReduceValuesToLongTask<K,V>
4694 dl 1.63 (map, null, -1, null, transformer, basis, reducer);
4695 dl 1.52 }
4696    
4697     /**
4698     * Returns a task that when invoked, returns the result of
4699     * accumulating the given transformation of all values using the
4700     * given reducer to combine values, and the given basis as an
4701     * identity value.
4702     *
4703     * @param map the map
4704     * @param transformer a function returning the transformation
4705     * for an element
4706     * @param basis the identity (initial default value) for the reduction
4707     * @param reducer a commutative associative combining function
4708     * @return the task
4709     */
4710     public static <K,V> ForkJoinTask<Integer> reduceValuesToInt
4711     (ConcurrentHashMapV8<K,V> map,
4712     ObjectToInt<? super V> transformer,
4713     int basis,
4714     IntByIntToInt reducer) {
4715     if (transformer == null || reducer == null)
4716     throw new NullPointerException();
4717     return new MapReduceValuesToIntTask<K,V>
4718 dl 1.63 (map, null, -1, null, transformer, basis, reducer);
4719 dl 1.52 }
4720    
4721     /**
4722     * Returns a task that when invoked, perform the given action
4723 jsr166 1.56 * for each entry.
4724 dl 1.52 *
4725     * @param map the map
4726     * @param action the action
4727     */
4728 jsr166 1.53 public static <K,V> ForkJoinTask<Void> forEachEntry
4729 dl 1.52 (ConcurrentHashMapV8<K,V> map,
4730     Action<Map.Entry<K,V>> action) {
4731     if (action == null) throw new NullPointerException();
4732 dl 1.70 return new ForEachEntryTask<K,V>(map, null, -1, null, action);
4733 dl 1.52 }
4734    
4735     /**
4736     * Returns a task that when invoked, perform the given action
4737 jsr166 1.56 * for each non-null transformation of each entry.
4738 dl 1.52 *
4739     * @param map the map
4740     * @param transformer a function returning the transformation
4741 jsr166 1.68 * for an element, or null if there is no transformation (in
4742 jsr166 1.67 * which case the action is not applied)
4743 dl 1.52 * @param action the action
4744     */
4745 jsr166 1.53 public static <K,V,U> ForkJoinTask<Void> forEachEntry
4746 dl 1.52 (ConcurrentHashMapV8<K,V> map,
4747     Fun<Map.Entry<K,V>, ? extends U> transformer,
4748     Action<U> action) {
4749     if (transformer == null || action == null)
4750     throw new NullPointerException();
4751     return new ForEachTransformedEntryTask<K,V,U>
4752 dl 1.70 (map, null, -1, null, transformer, action);
4753 dl 1.52 }
4754    
4755     /**
4756     * Returns a task that when invoked, returns a non-null result
4757     * from applying the given search function on each entry, or
4758 dl 1.59 * null if none. Upon success, further element processing is
4759     * suppressed and the results of any other parallel
4760     * invocations of the search function are ignored.
4761 dl 1.52 *
4762     * @param map the map
4763     * @param searchFunction a function returning a non-null
4764     * result on success, else null
4765     * @return the task
4766     */
4767     public static <K,V,U> ForkJoinTask<U> searchEntries
4768     (ConcurrentHashMapV8<K,V> map,
4769     Fun<Map.Entry<K,V>, ? extends U> searchFunction) {
4770     if (searchFunction == null) throw new NullPointerException();
4771     return new SearchEntriesTask<K,V,U>
4772 dl 1.70 (map, null, -1, null, searchFunction,
4773 dl 1.52 new AtomicReference<U>());
4774     }
4775    
4776     /**
4777     * Returns a task that when invoked, returns the result of
4778     * accumulating all entries using the given reducer to combine
4779     * values, or null if none.
4780     *
4781     * @param map the map
4782     * @param reducer a commutative associative combining function
4783     * @return the task
4784     */
4785     public static <K,V> ForkJoinTask<Map.Entry<K,V>> reduceEntries
4786     (ConcurrentHashMapV8<K,V> map,
4787     BiFun<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer) {
4788     if (reducer == null) throw new NullPointerException();
4789     return new ReduceEntriesTask<K,V>
4790 dl 1.63 (map, null, -1, null, reducer);
4791 dl 1.52 }
4792    
4793     /**
4794     * Returns a task that when invoked, returns the result of
4795     * accumulating the given transformation of all entries using the
4796     * given reducer to combine values, or null if none.
4797     *
4798     * @param map the map
4799     * @param transformer a function returning the transformation
4800 jsr166 1.68 * for an element, or null if there is no transformation (in
4801 dl 1.52 * which case it is not combined).
4802     * @param reducer a commutative associative combining function
4803     * @return the task
4804     */
4805     public static <K,V,U> ForkJoinTask<U> reduceEntries
4806     (ConcurrentHashMapV8<K,V> map,
4807     Fun<Map.Entry<K,V>, ? extends U> transformer,
4808     BiFun<? super U, ? super U, ? extends U> reducer) {
4809     if (transformer == null || reducer == null)
4810     throw new NullPointerException();
4811     return new MapReduceEntriesTask<K,V,U>
4812 dl 1.63 (map, null, -1, null, transformer, reducer);
4813 dl 1.52 }
4814    
4815     /**
4816     * Returns a task that when invoked, returns the result of
4817     * accumulating the given transformation of all entries using the
4818     * given reducer to combine values, and the given basis as an
4819     * identity value.
4820     *
4821     * @param map the map
4822     * @param transformer a function returning the transformation
4823     * for an element
4824     * @param basis the identity (initial default value) for the reduction
4825     * @param reducer a commutative associative combining function
4826     * @return the task
4827     */
4828     public static <K,V> ForkJoinTask<Double> reduceEntriesToDouble
4829     (ConcurrentHashMapV8<K,V> map,
4830     ObjectToDouble<Map.Entry<K,V>> transformer,
4831     double basis,
4832     DoubleByDoubleToDouble reducer) {
4833     if (transformer == null || reducer == null)
4834     throw new NullPointerException();
4835     return new MapReduceEntriesToDoubleTask<K,V>
4836 dl 1.63 (map, null, -1, null, transformer, basis, reducer);
4837 dl 1.52 }
4838    
4839     /**
4840     * Returns a task that when invoked, returns the result of
4841     * accumulating the given transformation of all entries using the
4842     * given reducer to combine values, and the given basis as an
4843     * identity value.
4844     *
4845     * @param map the map
4846     * @param transformer a function returning the transformation
4847     * for an element
4848     * @param basis the identity (initial default value) for the reduction
4849     * @param reducer a commutative associative combining function
4850     * @return the task
4851     */
4852     public static <K,V> ForkJoinTask<Long> reduceEntriesToLong
4853     (ConcurrentHashMapV8<K,V> map,
4854     ObjectToLong<Map.Entry<K,V>> transformer,
4855     long basis,
4856     LongByLongToLong reducer) {
4857     if (transformer == null || reducer == null)
4858     throw new NullPointerException();
4859     return new MapReduceEntriesToLongTask<K,V>
4860 dl 1.63 (map, null, -1, null, transformer, basis, reducer);
4861 dl 1.52 }
4862    
4863     /**
4864     * Returns a task that when invoked, returns the result of
4865     * accumulating the given transformation of all entries using the
4866     * given reducer to combine values, and the given basis as an
4867     * identity value.
4868     *
4869     * @param map the map
4870     * @param transformer a function returning the transformation
4871     * for an element
4872     * @param basis the identity (initial default value) for the reduction
4873     * @param reducer a commutative associative combining function
4874     * @return the task
4875     */
4876     public static <K,V> ForkJoinTask<Integer> reduceEntriesToInt
4877     (ConcurrentHashMapV8<K,V> map,
4878     ObjectToInt<Map.Entry<K,V>> transformer,
4879     int basis,
4880     IntByIntToInt reducer) {
4881     if (transformer == null || reducer == null)
4882     throw new NullPointerException();
4883     return new MapReduceEntriesToIntTask<K,V>
4884 dl 1.63 (map, null, -1, null, transformer, basis, reducer);
4885 dl 1.52 }
4886     }
4887    
4888     // -------------------------------------------------------
4889    
4890     /**
4891     * Base for FJ tasks for bulk operations. This adds a variant of
4892 jsr166 1.55 * CountedCompleters and some split and merge bookkeeping to
4893 dl 1.52 * iterator functionality. The forEach and reduce methods are
4894     * similar to those illustrated in CountedCompleter documentation,
4895     * except that bottom-up reduction completions perform them within
4896     * their compute methods. The search methods are like forEach
4897     * except they continually poll for success and exit early. Also,
4898     * exceptions are handled in a simpler manner, by just trying to
4899     * complete root task exceptionally.
4900     */
4901 dl 1.61 @SuppressWarnings("serial") static abstract class BulkTask<K,V,R> extends Traverser<K,V,R> {
4902 dl 1.52 final BulkTask<K,V,?> parent; // completion target
4903 dl 1.63 int batch; // split control; -1 for unknown
4904 dl 1.52 int pending; // completion control
4905    
4906 jsr166 1.64 BulkTask(ConcurrentHashMapV8<K,V> map, BulkTask<K,V,?> parent,
4907 dl 1.63 int batch) {
4908 dl 1.52 super(map);
4909     this.parent = parent;
4910     this.batch = batch;
4911 dl 1.63 if (parent != null && map != null) { // split parent
4912     Node[] t;
4913     if ((t = parent.tab) == null &&
4914     (t = parent.tab = map.table) != null)
4915     parent.baseLimit = parent.baseSize = t.length;
4916     this.tab = t;
4917     this.baseSize = parent.baseSize;
4918     int hi = this.baseLimit = parent.baseLimit;
4919     parent.baseLimit = this.index = this.baseIndex =
4920     (hi + parent.baseIndex + 1) >>> 1;
4921     }
4922 dl 1.52 }
4923    
4924     /**
4925 dl 1.61 * Forces root task to complete.
4926     * @param ex if null, complete normally, else exceptionally
4927     * @return false to simplify use
4928 dl 1.52 */
4929 dl 1.61 final boolean tryCompleteComputation(Throwable ex) {
4930 dl 1.52 for (BulkTask<K,V,?> a = this;;) {
4931     BulkTask<K,V,?> p = a.parent;
4932     if (p == null) {
4933 dl 1.61 if (ex != null)
4934     a.completeExceptionally(ex);
4935     else
4936     a.quietlyComplete();
4937     return false;
4938 dl 1.52 }
4939     a = p;
4940     }
4941     }
4942    
4943 dl 1.61 /**
4944     * Version of tryCompleteComputation for function screening checks
4945     */
4946     final boolean abortOnNullFunction() {
4947     return tryCompleteComputation(new Error("Unexpected null function"));
4948 dl 1.52 }
4949    
4950     // utilities
4951    
4952     /** CompareAndSet pending count */
4953     final boolean casPending(int cmp, int val) {
4954     return U.compareAndSwapInt(this, PENDING, cmp, val);
4955     }
4956    
4957     /**
4958 jsr166 1.56 * Returns approx exp2 of the number of times (minus one) to
4959 dl 1.52 * split task by two before executing leaf action. This value
4960     * is faster to compute and more convenient to use as a guide
4961     * to splitting than is the depth, since it is used while
4962     * dividing by two anyway.
4963     */
4964     final int batch() {
4965 dl 1.70 ConcurrentHashMapV8<K, V> m; int b; Node[] t; ForkJoinPool pool;
4966 dl 1.63 if ((b = batch) < 0 && (m = map) != null) { // force initialization
4967     if ((t = tab) == null && (t = tab = m.table) != null)
4968     baseLimit = baseSize = t.length;
4969     if (t != null) {
4970     long n = m.counter.sum();
4971 jsr166 1.72 int par = ((pool = getPool()) == null) ?
4972 dl 1.70 ForkJoinPool.getCommonPoolParallelism() :
4973     pool.getParallelism();
4974     int sp = par << 3; // slack of 8
4975 dl 1.63 b = batch = (n <= 0L) ? 0 : (n < (long)sp) ? (int)n : sp;
4976     }
4977 dl 1.52 }
4978     return b;
4979     }
4980    
4981     /**
4982 jsr166 1.56 * Returns exportable snapshot entry.
4983 dl 1.52 */
4984     static <K,V> AbstractMap.SimpleEntry<K,V> entryFor(K k, V v) {
4985 dl 1.59 return new AbstractMap.SimpleEntry<K,V>(k, v);
4986 dl 1.52 }
4987    
4988     // Unsafe mechanics
4989     private static final sun.misc.Unsafe U;
4990     private static final long PENDING;
4991     static {
4992     try {
4993 dl 1.59 U = getUnsafe();
4994 dl 1.52 PENDING = U.objectFieldOffset
4995     (BulkTask.class.getDeclaredField("pending"));
4996     } catch (Exception e) {
4997     throw new Error(e);
4998     }
4999     }
5000     }
5001    
5002 dl 1.71 /**
5003     * Base class for non-reductive actions
5004     */
5005     @SuppressWarnings("serial") static abstract class BulkAction<K,V,R> extends BulkTask<K,V,R> {
5006     BulkAction<K,V,?> nextTask;
5007     BulkAction(ConcurrentHashMapV8<K,V> map, BulkTask<K,V,?> parent,
5008     int batch, BulkAction<K,V,?> nextTask) {
5009     super(map, parent, batch);
5010     this.nextTask = nextTask;
5011     }
5012    
5013     /**
5014     * Try to complete task and upward parents. Upon hitting
5015     * non-completed parent, if a non-FJ task, try to help out the
5016     * computation.
5017     */
5018     final void tryComplete(BulkAction<K,V,?> subtasks) {
5019     BulkTask<K,V,?> a = this, s = a;
5020     for (int c;;) {
5021     if ((c = a.pending) == 0) {
5022     if ((a = (s = a).parent) == null) {
5023     s.quietlyComplete();
5024     break;
5025     }
5026     }
5027     else if (a.casPending(c, c - 1)) {
5028     if (subtasks != null && !inForkJoinPool()) {
5029     while ((s = a.parent) != null)
5030     a = s;
5031     while (!a.isDone()) {
5032     BulkAction<K,V,?> next = subtasks.nextTask;
5033     if (subtasks.tryUnfork())
5034     subtasks.exec();
5035     if ((subtasks = next) == null)
5036     break;
5037     }
5038     }
5039     break;
5040     }
5041     }
5042     }
5043    
5044     }
5045    
5046 dl 1.52 /*
5047     * Task classes. Coded in a regular but ugly format/style to
5048     * simplify checks that each variant differs in the right way from
5049     * others.
5050     */
5051    
5052 dl 1.61 @SuppressWarnings("serial") static final class ForEachKeyTask<K,V>
5053 dl 1.71 extends BulkAction<K,V,Void> {
5054 dl 1.52 final Action<K> action;
5055     ForEachKeyTask
5056 dl 1.63 (ConcurrentHashMapV8<K,V> m, BulkTask<K,V,?> p, int b,
5057 dl 1.71 ForEachKeyTask<K,V> nextTask,
5058 dl 1.52 Action<K> action) {
5059 dl 1.71 super(m, p, b, nextTask);
5060 dl 1.52 this.action = action;
5061     }
5062 dl 1.61 @SuppressWarnings("unchecked") public final boolean exec() {
5063 dl 1.52 final Action<K> action = this.action;
5064     if (action == null)
5065 dl 1.61 return abortOnNullFunction();
5066 dl 1.71 ForEachKeyTask<K,V> subtasks = null;
5067 dl 1.61 try {
5068     int b = batch(), c;
5069     while (b > 1 && baseIndex != baseLimit) {
5070     do {} while (!casPending(c = pending, c+1));
5071 dl 1.71 (subtasks = new ForEachKeyTask<K,V>
5072     (map, this, b >>>= 1, subtasks, action)).fork();
5073 dl 1.61 }
5074     while (advance() != null)
5075     action.apply((K)nextKey);
5076     } catch (Throwable ex) {
5077     return tryCompleteComputation(ex);
5078 dl 1.52 }
5079 dl 1.71 tryComplete(subtasks);
5080 dl 1.61 return false;
5081 dl 1.52 }
5082     }
5083    
5084 dl 1.61 @SuppressWarnings("serial") static final class ForEachValueTask<K,V>
5085 dl 1.71 extends BulkAction<K,V,Void> {
5086 dl 1.52 final Action<V> action;
5087     ForEachValueTask
5088 dl 1.63 (ConcurrentHashMapV8<K,V> m, BulkTask<K,V,?> p, int b,
5089 dl 1.71 ForEachValueTask<K,V> nextTask,
5090 dl 1.52 Action<V> action) {
5091 dl 1.71 super(m, p, b, nextTask);
5092 dl 1.52 this.action = action;
5093     }
5094 dl 1.61 @SuppressWarnings("unchecked") public final boolean exec() {
5095 dl 1.52 final Action<V> action = this.action;
5096     if (action == null)
5097 dl 1.61 return abortOnNullFunction();
5098 dl 1.71 ForEachValueTask<K,V> subtasks = null;
5099 dl 1.61 try {
5100     int b = batch(), c;
5101     while (b > 1 && baseIndex != baseLimit) {
5102     do {} while (!casPending(c = pending, c+1));
5103 dl 1.71 (subtasks = new ForEachValueTask<K,V>
5104     (map, this, b >>>= 1, subtasks, action)).fork();
5105 dl 1.61 }
5106     Object v;
5107     while ((v = advance()) != null)
5108     action.apply((V)v);
5109     } catch (Throwable ex) {
5110     return tryCompleteComputation(ex);
5111 dl 1.52 }
5112 dl 1.71 tryComplete(subtasks);
5113 dl 1.61 return false;
5114 dl 1.52 }
5115     }
5116    
5117 dl 1.61 @SuppressWarnings("serial") static final class ForEachEntryTask<K,V>
5118 dl 1.71 extends BulkAction<K,V,Void> {
5119 dl 1.52 final Action<Entry<K,V>> action;
5120     ForEachEntryTask
5121 dl 1.63 (ConcurrentHashMapV8<K,V> m, BulkTask<K,V,?> p, int b,
5122 dl 1.71 ForEachEntryTask<K,V> nextTask,
5123 dl 1.52 Action<Entry<K,V>> action) {
5124 dl 1.71 super(m, p, b, nextTask);
5125 dl 1.52 this.action = action;
5126     }
5127 dl 1.61 @SuppressWarnings("unchecked") public final boolean exec() {
5128 dl 1.52 final Action<Entry<K,V>> action = this.action;
5129     if (action == null)
5130 dl 1.61 return abortOnNullFunction();
5131 dl 1.71 ForEachEntryTask<K,V> subtasks = null;
5132 dl 1.61 try {
5133     int b = batch(), c;
5134     while (b > 1 && baseIndex != baseLimit) {
5135     do {} while (!casPending(c = pending, c+1));
5136 dl 1.71 (subtasks = new ForEachEntryTask<K,V>
5137     (map, this, b >>>= 1, subtasks, action)).fork();
5138 dl 1.61 }
5139     Object v;
5140     while ((v = advance()) != null)
5141     action.apply(entryFor((K)nextKey, (V)v));
5142     } catch (Throwable ex) {
5143     return tryCompleteComputation(ex);
5144 dl 1.52 }
5145 dl 1.71 tryComplete(subtasks);
5146 dl 1.61 return false;
5147 dl 1.52 }
5148     }
5149    
5150 dl 1.61 @SuppressWarnings("serial") static final class ForEachMappingTask<K,V>
5151 dl 1.71 extends BulkAction<K,V,Void> {
5152 dl 1.52 final BiAction<K,V> action;
5153     ForEachMappingTask
5154 dl 1.63 (ConcurrentHashMapV8<K,V> m, BulkTask<K,V,?> p, int b,
5155 dl 1.71 ForEachMappingTask<K,V> nextTask,
5156 dl 1.52 BiAction<K,V> action) {
5157 dl 1.71 super(m, p, b, nextTask);
5158 dl 1.52 this.action = action;
5159     }
5160 dl 1.61 @SuppressWarnings("unchecked") public final boolean exec() {
5161 dl 1.52 final BiAction<K,V> action = this.action;
5162     if (action == null)
5163 dl 1.61 return abortOnNullFunction();
5164 dl 1.71 ForEachMappingTask<K,V> subtasks = null;
5165 dl 1.61 try {
5166     int b = batch(), c;
5167     while (b > 1 && baseIndex != baseLimit) {
5168     do {} while (!casPending(c = pending, c+1));
5169 dl 1.71 (subtasks = new ForEachMappingTask<K,V>
5170     (map, this, b >>>= 1, subtasks, action)).fork();
5171 dl 1.61 }
5172     Object v;
5173     while ((v = advance()) != null)
5174     action.apply((K)nextKey, (V)v);
5175     } catch (Throwable ex) {
5176     return tryCompleteComputation(ex);
5177 dl 1.52 }
5178 dl 1.71 tryComplete(subtasks);
5179 dl 1.61 return false;
5180 dl 1.52 }
5181     }
5182    
5183 dl 1.61 @SuppressWarnings("serial") static final class ForEachTransformedKeyTask<K,V,U>
5184 dl 1.71 extends BulkAction<K,V,Void> {
5185 dl 1.52 final Fun<? super K, ? extends U> transformer;
5186     final Action<U> action;
5187     ForEachTransformedKeyTask
5188 dl 1.63 (ConcurrentHashMapV8<K,V> m, BulkTask<K,V,?> p, int b,
5189 dl 1.71 ForEachTransformedKeyTask<K,V,U> nextTask,
5190 dl 1.52 Fun<? super K, ? extends U> transformer,
5191     Action<U> action) {
5192 dl 1.71 super(m, p, b, nextTask);
5193 dl 1.52 this.transformer = transformer;
5194     this.action = action;
5195    
5196     }
5197 dl 1.61 @SuppressWarnings("unchecked") public final boolean exec() {
5198 dl 1.52 final Fun<? super K, ? extends U> transformer =
5199     this.transformer;
5200     final Action<U> action = this.action;
5201     if (transformer == null || action == null)
5202 dl 1.61 return abortOnNullFunction();
5203 dl 1.71 ForEachTransformedKeyTask<K,V,U> subtasks = null;
5204 dl 1.61 try {
5205     int b = batch(), c;
5206     while (b > 1 && baseIndex != baseLimit) {
5207     do {} while (!casPending(c = pending, c+1));
5208 dl 1.71 (subtasks = new ForEachTransformedKeyTask<K,V,U>
5209     (map, this, b >>>= 1, subtasks, transformer, action)).fork();
5210 dl 1.61 }
5211     U u;
5212     while (advance() != null) {
5213     if ((u = transformer.apply((K)nextKey)) != null)
5214     action.apply(u);
5215     }
5216     } catch (Throwable ex) {
5217     return tryCompleteComputation(ex);
5218 dl 1.52 }
5219 dl 1.71 tryComplete(subtasks);
5220 dl 1.61 return false;
5221 dl 1.52 }
5222     }
5223    
5224 dl 1.61 @SuppressWarnings("serial") static final class ForEachTransformedValueTask<K,V,U>
5225 dl 1.71 extends BulkAction<K,V,Void> {
5226 dl 1.52 final Fun<? super V, ? extends U> transformer;
5227     final Action<U> action;
5228     ForEachTransformedValueTask
5229 dl 1.63 (ConcurrentHashMapV8<K,V> m, BulkTask<K,V,?> p, int b,
5230 dl 1.71 ForEachTransformedValueTask<K,V,U> nextTask,
5231 dl 1.52 Fun<? super V, ? extends U> transformer,
5232     Action<U> action) {
5233 dl 1.71 super(m, p, b, nextTask);
5234 dl 1.52 this.transformer = transformer;
5235     this.action = action;
5236    
5237     }
5238 dl 1.61 @SuppressWarnings("unchecked") public final boolean exec() {
5239 dl 1.52 final Fun<? super V, ? extends U> transformer =
5240     this.transformer;
5241     final Action<U> action = this.action;
5242     if (transformer == null || action == null)
5243 dl 1.61 return abortOnNullFunction();
5244 dl 1.71 ForEachTransformedValueTask<K,V,U> subtasks = null;
5245 dl 1.61 try {
5246     int b = batch(), c;
5247     while (b > 1 && baseIndex != baseLimit) {
5248     do {} while (!casPending(c = pending, c+1));
5249 dl 1.71 (subtasks = new ForEachTransformedValueTask<K,V,U>
5250     (map, this, b >>>= 1, subtasks, transformer, action)).fork();
5251 dl 1.61 }
5252     Object v; U u;
5253     while ((v = advance()) != null) {
5254     if ((u = transformer.apply((V)v)) != null)
5255     action.apply(u);
5256     }
5257     } catch (Throwable ex) {
5258     return tryCompleteComputation(ex);
5259 dl 1.52 }
5260 dl 1.71 tryComplete(subtasks);
5261 dl 1.61 return false;
5262 dl 1.52 }
5263     }
5264    
5265 dl 1.61 @SuppressWarnings("serial") static final class ForEachTransformedEntryTask<K,V,U>
5266 dl 1.71 extends BulkAction<K,V,Void> {
5267 dl 1.52 final Fun<Map.Entry<K,V>, ? extends U> transformer;
5268     final Action<U> action;
5269     ForEachTransformedEntryTask
5270 dl 1.63 (ConcurrentHashMapV8<K,V> m, BulkTask<K,V,?> p, int b,
5271 dl 1.71 ForEachTransformedEntryTask<K,V,U> nextTask,
5272 dl 1.52 Fun<Map.Entry<K,V>, ? extends U> transformer,
5273     Action<U> action) {
5274 dl 1.71 super(m, p, b, nextTask);
5275 dl 1.52 this.transformer = transformer;
5276     this.action = action;
5277    
5278     }
5279 dl 1.61 @SuppressWarnings("unchecked") public final boolean exec() {
5280 dl 1.52 final Fun<Map.Entry<K,V>, ? extends U> transformer =
5281     this.transformer;
5282     final Action<U> action = this.action;
5283     if (transformer == null || action == null)
5284 dl 1.61 return abortOnNullFunction();
5285 dl 1.71 ForEachTransformedEntryTask<K,V,U> subtasks = null;
5286 dl 1.61 try {
5287     int b = batch(), c;
5288     while (b > 1 && baseIndex != baseLimit) {
5289     do {} while (!casPending(c = pending, c+1));
5290 dl 1.71 (subtasks = new ForEachTransformedEntryTask<K,V,U>
5291     (map, this, b >>>= 1, subtasks, transformer, action)).fork();
5292 dl 1.61 }
5293     Object v; U u;
5294     while ((v = advance()) != null) {
5295     if ((u = transformer.apply(entryFor((K)nextKey, (V)v))) != null)
5296     action.apply(u);
5297     }
5298     } catch (Throwable ex) {
5299     return tryCompleteComputation(ex);
5300 dl 1.52 }
5301 dl 1.71 tryComplete(subtasks);
5302 dl 1.61 return false;
5303 dl 1.52 }
5304     }
5305    
5306 dl 1.61 @SuppressWarnings("serial") static final class ForEachTransformedMappingTask<K,V,U>
5307 dl 1.71 extends BulkAction<K,V,Void> {
5308 dl 1.52 final BiFun<? super K, ? super V, ? extends U> transformer;
5309     final Action<U> action;
5310     ForEachTransformedMappingTask
5311 dl 1.63 (ConcurrentHashMapV8<K,V> m, BulkTask<K,V,?> p, int b,
5312 dl 1.71 ForEachTransformedMappingTask<K,V,U> nextTask,
5313 dl 1.52 BiFun<? super K, ? super V, ? extends U> transformer,
5314     Action<U> action) {
5315 dl 1.71 super(m, p, b, nextTask);
5316 dl 1.52 this.transformer = transformer;
5317     this.action = action;
5318    
5319     }
5320 dl 1.61 @SuppressWarnings("unchecked") public final boolean exec() {
5321 dl 1.52 final BiFun<? super K, ? super V, ? extends U> transformer =
5322     this.transformer;
5323     final Action<U> action = this.action;
5324     if (transformer == null || action == null)
5325 dl 1.61 return abortOnNullFunction();
5326 dl 1.71 ForEachTransformedMappingTask<K,V,U> subtasks = null;
5327 dl 1.61 try {
5328     int b = batch(), c;
5329     while (b > 1 && baseIndex != baseLimit) {
5330     do {} while (!casPending(c = pending, c+1));
5331 dl 1.71 (subtasks = new ForEachTransformedMappingTask<K,V,U>
5332     (map, this, b >>>= 1, subtasks, transformer, action)).fork();
5333 dl 1.61 }
5334     Object v; U u;
5335     while ((v = advance()) != null) {
5336     if ((u = transformer.apply((K)nextKey, (V)v)) != null)
5337     action.apply(u);
5338     }
5339     } catch (Throwable ex) {
5340     return tryCompleteComputation(ex);
5341 dl 1.52 }
5342 dl 1.71 tryComplete(subtasks);
5343 dl 1.61 return false;
5344 dl 1.52 }
5345     }
5346    
5347 dl 1.61 @SuppressWarnings("serial") static final class SearchKeysTask<K,V,U>
5348 dl 1.71 extends BulkAction<K,V,U> {
5349 dl 1.52 final Fun<? super K, ? extends U> searchFunction;
5350     final AtomicReference<U> result;
5351     SearchKeysTask
5352 dl 1.63 (ConcurrentHashMapV8<K,V> m, BulkTask<K,V,?> p, int b,
5353 dl 1.71 SearchKeysTask<K,V,U> nextTask,
5354 dl 1.52 Fun<? super K, ? extends U> searchFunction,
5355     AtomicReference<U> result) {
5356 dl 1.71 super(m, p, b, nextTask);
5357 dl 1.52 this.searchFunction = searchFunction; this.result = result;
5358     }
5359 dl 1.61 @SuppressWarnings("unchecked") public final boolean exec() {
5360 dl 1.52 AtomicReference<U> result = this.result;
5361     final Fun<? super K, ? extends U> searchFunction =
5362     this.searchFunction;
5363     if (searchFunction == null || result == null)
5364 dl 1.61 return abortOnNullFunction();
5365 dl 1.71 SearchKeysTask<K,V,U> subtasks = null;
5366 dl 1.61 try {
5367     int b = batch(), c;
5368     while (b > 1 && baseIndex != baseLimit && result.get() == null) {
5369     do {} while (!casPending(c = pending, c+1));
5370 dl 1.71 (subtasks = new SearchKeysTask<K,V,U>
5371     (map, this, b >>>= 1, subtasks, searchFunction, result)).fork();
5372 dl 1.61 }
5373     U u;
5374     while (result.get() == null && advance() != null) {
5375     if ((u = searchFunction.apply((K)nextKey)) != null) {
5376     if (result.compareAndSet(null, u))
5377     tryCompleteComputation(null);
5378     break;
5379 dl 1.59 }
5380 dl 1.52 }
5381 dl 1.61 } catch (Throwable ex) {
5382     return tryCompleteComputation(ex);
5383 dl 1.52 }
5384 dl 1.71 tryComplete(subtasks);
5385 dl 1.61 return false;
5386 dl 1.52 }
5387     public final U getRawResult() { return result.get(); }
5388     }
5389    
5390 dl 1.61 @SuppressWarnings("serial") static final class SearchValuesTask<K,V,U>
5391 dl 1.71 extends BulkAction<K,V,U> {
5392 dl 1.52 final Fun<? super V, ? extends U> searchFunction;
5393     final AtomicReference<U> result;
5394     SearchValuesTask
5395 dl 1.63 (ConcurrentHashMapV8<K,V> m, BulkTask<K,V,?> p, int b,
5396 dl 1.71 SearchValuesTask<K,V,U> nextTask,
5397 dl 1.52 Fun<? super V, ? extends U> searchFunction,
5398     AtomicReference<U> result) {
5399 dl 1.71 super(m, p, b, nextTask);
5400 dl 1.52 this.searchFunction = searchFunction; this.result = result;
5401     }
5402 dl 1.61 @SuppressWarnings("unchecked") public final boolean exec() {
5403 dl 1.52 AtomicReference<U> result = this.result;
5404     final Fun<? super V, ? extends U> searchFunction =
5405     this.searchFunction;
5406     if (searchFunction == null || result == null)
5407 dl 1.61 return abortOnNullFunction();
5408 dl 1.71 SearchValuesTask<K,V,U> subtasks = null;
5409 dl 1.61 try {
5410     int b = batch(), c;
5411     while (b > 1 && baseIndex != baseLimit && result.get() == null) {
5412     do {} while (!casPending(c = pending, c+1));
5413 dl 1.71 (subtasks = new SearchValuesTask<K,V,U>
5414     (map, this, b >>>= 1, subtasks, searchFunction, result)).fork();
5415 dl 1.61 }
5416     Object v; U u;
5417     while (result.get() == null && (v = advance()) != null) {
5418     if ((u = searchFunction.apply((V)v)) != null) {
5419     if (result.compareAndSet(null, u))
5420     tryCompleteComputation(null);
5421     break;
5422 dl 1.59 }
5423 dl 1.52 }
5424 dl 1.61 } catch (Throwable ex) {
5425     return tryCompleteComputation(ex);
5426 dl 1.52 }
5427 dl 1.71 tryComplete(subtasks);
5428 dl 1.61 return false;
5429 dl 1.52 }
5430     public final U getRawResult() { return result.get(); }
5431     }
5432    
5433 dl 1.61 @SuppressWarnings("serial") static final class SearchEntriesTask<K,V,U>
5434 dl 1.71 extends BulkAction<K,V,U> {
5435 dl 1.52 final Fun<Entry<K,V>, ? extends U> searchFunction;
5436     final AtomicReference<U> result;
5437     SearchEntriesTask
5438 dl 1.63 (ConcurrentHashMapV8<K,V> m, BulkTask<K,V,?> p, int b,
5439 dl 1.71 SearchEntriesTask<K,V,U> nextTask,
5440 dl 1.52 Fun<Entry<K,V>, ? extends U> searchFunction,
5441     AtomicReference<U> result) {
5442 dl 1.71 super(m, p, b, nextTask);
5443 dl 1.52 this.searchFunction = searchFunction; this.result = result;
5444     }
5445 dl 1.61 @SuppressWarnings("unchecked") public final boolean exec() {
5446 dl 1.52 AtomicReference<U> result = this.result;
5447     final Fun<Entry<K,V>, ? extends U> searchFunction =
5448     this.searchFunction;
5449     if (searchFunction == null || result == null)
5450 dl 1.61 return abortOnNullFunction();
5451 dl 1.71 SearchEntriesTask<K,V,U> subtasks = null;
5452 dl 1.61 try {
5453     int b = batch(), c;
5454     while (b > 1 && baseIndex != baseLimit && result.get() == null) {
5455     do {} while (!casPending(c = pending, c+1));
5456 dl 1.71 (subtasks = new SearchEntriesTask<K,V,U>
5457     (map, this, b >>>= 1, subtasks, searchFunction, result)).fork();
5458 dl 1.61 }
5459     Object v; U u;
5460     while (result.get() == null && (v = advance()) != null) {
5461     if ((u = searchFunction.apply(entryFor((K)nextKey, (V)v))) != null) {
5462     if (result.compareAndSet(null, u))
5463     tryCompleteComputation(null);
5464     break;
5465 dl 1.59 }
5466 dl 1.52 }
5467 dl 1.61 } catch (Throwable ex) {
5468     return tryCompleteComputation(ex);
5469 dl 1.52 }
5470 dl 1.71 tryComplete(subtasks);
5471 dl 1.61 return false;
5472 dl 1.52 }
5473     public final U getRawResult() { return result.get(); }
5474     }
5475    
5476 dl 1.61 @SuppressWarnings("serial") static final class SearchMappingsTask<K,V,U>
5477 dl 1.71 extends BulkAction<K,V,U> {
5478 dl 1.52 final BiFun<? super K, ? super V, ? extends U> searchFunction;
5479     final AtomicReference<U> result;
5480     SearchMappingsTask
5481 dl 1.63 (ConcurrentHashMapV8<K,V> m, BulkTask<K,V,?> p, int b,
5482 dl 1.71 SearchMappingsTask<K,V,U> nextTask,
5483 dl 1.52 BiFun<? super K, ? super V, ? extends U> searchFunction,
5484     AtomicReference<U> result) {
5485 dl 1.71 super(m, p, b, nextTask);
5486 dl 1.52 this.searchFunction = searchFunction; this.result = result;
5487     }
5488 dl 1.61 @SuppressWarnings("unchecked") public final boolean exec() {
5489 dl 1.52 AtomicReference<U> result = this.result;
5490     final BiFun<? super K, ? super V, ? extends U> searchFunction =
5491     this.searchFunction;
5492     if (searchFunction == null || result == null)
5493 dl 1.61 return abortOnNullFunction();
5494 dl 1.71 SearchMappingsTask<K,V,U> subtasks = null;
5495 dl 1.61 try {
5496     int b = batch(), c;
5497     while (b > 1 && baseIndex != baseLimit && result.get() == null) {
5498     do {} while (!casPending(c = pending, c+1));
5499 dl 1.71 (subtasks = new SearchMappingsTask<K,V,U>
5500     (map, this, b >>>= 1, subtasks, searchFunction, result)).fork();
5501 dl 1.61 }
5502     Object v; U u;
5503     while (result.get() == null && (v = advance()) != null) {
5504     if ((u = searchFunction.apply((K)nextKey, (V)v)) != null) {
5505     if (result.compareAndSet(null, u))
5506     tryCompleteComputation(null);
5507     break;
5508 dl 1.59 }
5509 dl 1.52 }
5510 dl 1.61 } catch (Throwable ex) {
5511     return tryCompleteComputation(ex);
5512 dl 1.52 }
5513 dl 1.71 tryComplete(subtasks);
5514 dl 1.61 return false;
5515 dl 1.52 }
5516     public final U getRawResult() { return result.get(); }
5517     }
5518    
5519 dl 1.61 @SuppressWarnings("serial") static final class ReduceKeysTask<K,V>
5520 dl 1.52 extends BulkTask<K,V,K> {
5521     final BiFun<? super K, ? super K, ? extends K> reducer;
5522     K result;
5523 dl 1.61 ReduceKeysTask<K,V> rights, nextRight;
5524 dl 1.52 ReduceKeysTask
5525 dl 1.63 (ConcurrentHashMapV8<K,V> m, BulkTask<K,V,?> p, int b,
5526 dl 1.61 ReduceKeysTask<K,V> nextRight,
5527 dl 1.52 BiFun<? super K, ? super K, ? extends K> reducer) {
5528 dl 1.63 super(m, p, b); this.nextRight = nextRight;
5529 dl 1.52 this.reducer = reducer;
5530     }
5531 dl 1.61 @SuppressWarnings("unchecked") public final boolean exec() {
5532 dl 1.52 final BiFun<? super K, ? super K, ? extends K> reducer =
5533     this.reducer;
5534     if (reducer == null)
5535 dl 1.61 return abortOnNullFunction();
5536     try {
5537     for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
5538     do {} while (!casPending(c = pending, c+1));
5539     (rights = new ReduceKeysTask<K,V>
5540 dl 1.63 (map, this, b >>>= 1, rights, reducer)).fork();
5541 dl 1.61 }
5542     K r = null;
5543     while (advance() != null) {
5544     K u = (K)nextKey;
5545     r = (r == null) ? u : reducer.apply(r, u);
5546 dl 1.52 }
5547 dl 1.61 result = r;
5548     for (ReduceKeysTask<K,V> t = this, s;;) {
5549     int c; BulkTask<K,V,?> par; K tr, sr;
5550     if ((c = t.pending) == 0) {
5551     for (s = t.rights; s != null; s = t.rights = s.nextRight) {
5552     if ((sr = s.result) != null)
5553 jsr166 1.65 t.result = ((tr = t.result) == null) ? sr : reducer.apply(tr, sr);
5554 dl 1.61 }
5555     if ((par = t.parent) == null ||
5556     !(par instanceof ReduceKeysTask)) {
5557     t.quietlyComplete();
5558     break;
5559     }
5560     t = (ReduceKeysTask<K,V>)par;
5561     }
5562     else if (t.casPending(c, c - 1))
5563     break;
5564 dl 1.52 }
5565 dl 1.61 } catch (Throwable ex) {
5566     return tryCompleteComputation(ex);
5567 dl 1.52 }
5568 dl 1.71 ReduceKeysTask<K,V> s = rights;
5569     if (s != null && !inForkJoinPool()) {
5570     do {
5571     if (s.tryUnfork())
5572     s.exec();
5573     } while ((s = s.nextRight) != null);
5574     }
5575 dl 1.61 return false;
5576 dl 1.52 }
5577     public final K getRawResult() { return result; }
5578     }
5579    
5580 dl 1.61 @SuppressWarnings("serial") static final class ReduceValuesTask<K,V>
5581 dl 1.52 extends BulkTask<K,V,V> {
5582     final BiFun<? super V, ? super V, ? extends V> reducer;
5583     V result;
5584 dl 1.61 ReduceValuesTask<K,V> rights, nextRight;
5585 dl 1.52 ReduceValuesTask
5586 dl 1.63 (ConcurrentHashMapV8<K,V> m, BulkTask<K,V,?> p, int b,
5587 dl 1.61 ReduceValuesTask<K,V> nextRight,
5588 dl 1.52 BiFun<? super V, ? super V, ? extends V> reducer) {
5589 dl 1.63 super(m, p, b); this.nextRight = nextRight;
5590 dl 1.52 this.reducer = reducer;
5591     }
5592 dl 1.61 @SuppressWarnings("unchecked") public final boolean exec() {
5593 dl 1.52 final BiFun<? super V, ? super V, ? extends V> reducer =
5594     this.reducer;
5595     if (reducer == null)
5596 dl 1.61 return abortOnNullFunction();
5597     try {
5598     for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
5599     do {} while (!casPending(c = pending, c+1));
5600     (rights = new ReduceValuesTask<K,V>
5601 dl 1.63 (map, this, b >>>= 1, rights, reducer)).fork();
5602 dl 1.61 }
5603     V r = null;
5604     Object v;
5605     while ((v = advance()) != null) {
5606     V u = (V)v;
5607     r = (r == null) ? u : reducer.apply(r, u);
5608 dl 1.52 }
5609 dl 1.61 result = r;
5610     for (ReduceValuesTask<K,V> t = this, s;;) {
5611     int c; BulkTask<K,V,?> par; V tr, sr;
5612     if ((c = t.pending) == 0) {
5613     for (s = t.rights; s != null; s = t.rights = s.nextRight) {
5614     if ((sr = s.result) != null)
5615 jsr166 1.65 t.result = ((tr = t.result) == null) ? sr : reducer.apply(tr, sr);
5616 dl 1.61 }
5617     if ((par = t.parent) == null ||
5618     !(par instanceof ReduceValuesTask)) {
5619     t.quietlyComplete();
5620     break;
5621     }
5622     t = (ReduceValuesTask<K,V>)par;
5623     }
5624     else if (t.casPending(c, c - 1))
5625     break;
5626 dl 1.52 }
5627 dl 1.61 } catch (Throwable ex) {
5628     return tryCompleteComputation(ex);
5629 dl 1.52 }
5630 dl 1.71 ReduceValuesTask<K,V> s = rights;
5631     if (s != null && !inForkJoinPool()) {
5632     do {
5633     if (s.tryUnfork())
5634     s.exec();
5635     } while ((s = s.nextRight) != null);
5636     }
5637 dl 1.61 return false;
5638 dl 1.52 }
5639     public final V getRawResult() { return result; }
5640     }
5641    
5642 dl 1.61 @SuppressWarnings("serial") static final class ReduceEntriesTask<K,V>
5643 dl 1.52 extends BulkTask<K,V,Map.Entry<K,V>> {
5644     final BiFun<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer;
5645     Map.Entry<K,V> result;
5646 dl 1.61 ReduceEntriesTask<K,V> rights, nextRight;
5647 dl 1.52 ReduceEntriesTask
5648 dl 1.63 (ConcurrentHashMapV8<K,V> m, BulkTask<K,V,?> p, int b,
5649     ReduceEntriesTask<K,V> nextRight,
5650 dl 1.52 BiFun<Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer) {
5651 dl 1.63 super(m, p, b); this.nextRight = nextRight;
5652 dl 1.52 this.reducer = reducer;
5653     }
5654 dl 1.61 @SuppressWarnings("unchecked") public final boolean exec() {
5655 dl 1.52 final BiFun<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer =
5656     this.reducer;
5657     if (reducer == null)
5658 dl 1.61 return abortOnNullFunction();
5659     try {
5660     for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
5661     do {} while (!casPending(c = pending, c+1));
5662     (rights = new ReduceEntriesTask<K,V>
5663 dl 1.63 (map, this, b >>>= 1, rights, reducer)).fork();
5664 dl 1.61 }
5665     Map.Entry<K,V> r = null;
5666     Object v;
5667     while ((v = advance()) != null) {
5668     Map.Entry<K,V> u = entryFor((K)nextKey, (V)v);
5669     r = (r == null) ? u : reducer.apply(r, u);
5670 dl 1.52 }
5671 dl 1.61 result = r;
5672     for (ReduceEntriesTask<K,V> t = this, s;;) {
5673     int c; BulkTask<K,V,?> par; Map.Entry<K,V> tr, sr;
5674     if ((c = t.pending) == 0) {
5675     for (s = t.rights; s != null; s = t.rights = s.nextRight) {
5676     if ((sr = s.result) != null)
5677 jsr166 1.65 t.result = ((tr = t.result) == null) ? sr : reducer.apply(tr, sr);
5678 dl 1.61 }
5679     if ((par = t.parent) == null ||
5680     !(par instanceof ReduceEntriesTask)) {
5681     t.quietlyComplete();
5682     break;
5683     }
5684     t = (ReduceEntriesTask<K,V>)par;
5685     }
5686     else if (t.casPending(c, c - 1))
5687     break;
5688 dl 1.52 }
5689 dl 1.61 } catch (Throwable ex) {
5690     return tryCompleteComputation(ex);
5691 dl 1.52 }
5692 dl 1.71 ReduceEntriesTask<K,V> s = rights;
5693     if (s != null && !inForkJoinPool()) {
5694     do {
5695     if (s.tryUnfork())
5696     s.exec();
5697     } while ((s = s.nextRight) != null);
5698     }
5699 dl 1.61 return false;
5700 dl 1.52 }
5701     public final Map.Entry<K,V> getRawResult() { return result; }
5702     }
5703    
5704 dl 1.61 @SuppressWarnings("serial") static final class MapReduceKeysTask<K,V,U>
5705 dl 1.52 extends BulkTask<K,V,U> {
5706     final Fun<? super K, ? extends U> transformer;
5707     final BiFun<? super U, ? super U, ? extends U> reducer;
5708     U result;
5709 dl 1.61 MapReduceKeysTask<K,V,U> rights, nextRight;
5710 dl 1.52 MapReduceKeysTask
5711 dl 1.63 (ConcurrentHashMapV8<K,V> m, BulkTask<K,V,?> p, int b,
5712 dl 1.61 MapReduceKeysTask<K,V,U> nextRight,
5713 dl 1.52 Fun<? super K, ? extends U> transformer,
5714     BiFun<? super U, ? super U, ? extends U> reducer) {
5715 dl 1.63 super(m, p, b); this.nextRight = nextRight;
5716 dl 1.52 this.transformer = transformer;
5717     this.reducer = reducer;
5718     }
5719 dl 1.61 @SuppressWarnings("unchecked") public final boolean exec() {
5720 dl 1.52 final Fun<? super K, ? extends U> transformer =
5721     this.transformer;
5722     final BiFun<? super U, ? super U, ? extends U> reducer =
5723     this.reducer;
5724     if (transformer == null || reducer == null)
5725 dl 1.61 return abortOnNullFunction();
5726     try {
5727     for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
5728     do {} while (!casPending(c = pending, c+1));
5729     (rights = new MapReduceKeysTask<K,V,U>
5730 dl 1.63 (map, this, b >>>= 1, rights, transformer, reducer)).fork();
5731 dl 1.61 }
5732     U r = null, u;
5733     while (advance() != null) {
5734     if ((u = transformer.apply((K)nextKey)) != null)
5735     r = (r == null) ? u : reducer.apply(r, u);
5736 dl 1.52 }
5737 dl 1.61 result = r;
5738     for (MapReduceKeysTask<K,V,U> t = this, s;;) {
5739     int c; BulkTask<K,V,?> par; U tr, sr;
5740     if ((c = t.pending) == 0) {
5741     for (s = t.rights; s != null; s = t.rights = s.nextRight) {
5742     if ((sr = s.result) != null)
5743 jsr166 1.65 t.result = ((tr = t.result) == null) ? sr : reducer.apply(tr, sr);
5744 dl 1.61 }
5745     if ((par = t.parent) == null ||
5746     !(par instanceof MapReduceKeysTask)) {
5747     t.quietlyComplete();
5748     break;
5749     }
5750     t = (MapReduceKeysTask<K,V,U>)par;
5751     }
5752     else if (t.casPending(c, c - 1))
5753     break;
5754 dl 1.52 }
5755 dl 1.61 } catch (Throwable ex) {
5756     return tryCompleteComputation(ex);
5757 dl 1.52 }
5758 dl 1.71 MapReduceKeysTask<K,V,U> s = rights;
5759     if (s != null && !inForkJoinPool()) {
5760     do {
5761     if (s.tryUnfork())
5762     s.exec();
5763     } while ((s = s.nextRight) != null);
5764     }
5765 dl 1.61 return false;
5766 dl 1.52 }
5767     public final U getRawResult() { return result; }
5768     }
5769    
5770 dl 1.61 @SuppressWarnings("serial") static final class MapReduceValuesTask<K,V,U>
5771 dl 1.52 extends BulkTask<K,V,U> {
5772     final Fun<? super V, ? extends U> transformer;
5773     final BiFun<? super U, ? super U, ? extends U> reducer;
5774     U result;
5775 dl 1.61 MapReduceValuesTask<K,V,U> rights, nextRight;
5776 dl 1.52 MapReduceValuesTask
5777 dl 1.63 (ConcurrentHashMapV8<K,V> m, BulkTask<K,V,?> p, int b,
5778 dl 1.61 MapReduceValuesTask<K,V,U> nextRight,
5779 dl 1.52 Fun<? super V, ? extends U> transformer,
5780     BiFun<? super U, ? super U, ? extends U> reducer) {
5781 dl 1.63 super(m, p, b); this.nextRight = nextRight;
5782 dl 1.52 this.transformer = transformer;
5783     this.reducer = reducer;
5784     }
5785 dl 1.61 @SuppressWarnings("unchecked") public final boolean exec() {
5786 dl 1.52 final Fun<? super V, ? extends U> transformer =
5787     this.transformer;
5788     final BiFun<? super U, ? super U, ? extends U> reducer =
5789     this.reducer;
5790     if (transformer == null || reducer == null)
5791 dl 1.61 return abortOnNullFunction();
5792     try {
5793     for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
5794     do {} while (!casPending(c = pending, c+1));
5795     (rights = new MapReduceValuesTask<K,V,U>
5796 dl 1.63 (map, this, b >>>= 1, rights, transformer, reducer)).fork();
5797 dl 1.61 }
5798     U r = null, u;
5799     Object v;
5800     while ((v = advance()) != null) {
5801     if ((u = transformer.apply((V)v)) != null)
5802     r = (r == null) ? u : reducer.apply(r, u);
5803 dl 1.52 }
5804 dl 1.61 result = r;
5805     for (MapReduceValuesTask<K,V,U> t = this, s;;) {
5806     int c; BulkTask<K,V,?> par; U tr, sr;
5807     if ((c = t.pending) == 0) {
5808     for (s = t.rights; s != null; s = t.rights = s.nextRight) {
5809     if ((sr = s.result) != null)
5810 jsr166 1.65 t.result = ((tr = t.result) == null) ? sr : reducer.apply(tr, sr);
5811 dl 1.61 }
5812     if ((par = t.parent) == null ||
5813     !(par instanceof MapReduceValuesTask)) {
5814     t.quietlyComplete();
5815     break;
5816     }
5817     t = (MapReduceValuesTask<K,V,U>)par;
5818     }
5819     else if (t.casPending(c, c - 1))
5820     break;
5821 dl 1.52 }
5822 dl 1.61 } catch (Throwable ex) {
5823     return tryCompleteComputation(ex);
5824 dl 1.52 }
5825 dl 1.71 MapReduceValuesTask<K,V,U> s = rights;
5826     if (s != null && !inForkJoinPool()) {
5827     do {
5828     if (s.tryUnfork())
5829     s.exec();
5830     } while ((s = s.nextRight) != null);
5831     }
5832 dl 1.61 return false;
5833 dl 1.52 }
5834     public final U getRawResult() { return result; }
5835     }
5836    
5837 dl 1.61 @SuppressWarnings("serial") static final class MapReduceEntriesTask<K,V,U>
5838 dl 1.52 extends BulkTask<K,V,U> {
5839     final Fun<Map.Entry<K,V>, ? extends U> transformer;
5840     final BiFun<? super U, ? super U, ? extends U> reducer;
5841     U result;
5842 dl 1.61 MapReduceEntriesTask<K,V,U> rights, nextRight;
5843 dl 1.52 MapReduceEntriesTask
5844 dl 1.63 (ConcurrentHashMapV8<K,V> m, BulkTask<K,V,?> p, int b,
5845 dl 1.61 MapReduceEntriesTask<K,V,U> nextRight,
5846 dl 1.52 Fun<Map.Entry<K,V>, ? extends U> transformer,
5847     BiFun<? super U, ? super U, ? extends U> reducer) {
5848 dl 1.63 super(m, p, b); this.nextRight = nextRight;
5849 dl 1.52 this.transformer = transformer;
5850     this.reducer = reducer;
5851     }
5852 dl 1.61 @SuppressWarnings("unchecked") public final boolean exec() {
5853 dl 1.52 final Fun<Map.Entry<K,V>, ? extends U> transformer =
5854     this.transformer;
5855     final BiFun<? super U, ? super U, ? extends U> reducer =
5856     this.reducer;
5857     if (transformer == null || reducer == null)
5858 dl 1.61 return abortOnNullFunction();
5859     try {
5860     for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
5861     do {} while (!casPending(c = pending, c+1));
5862     (rights = new MapReduceEntriesTask<K,V,U>
5863 dl 1.63 (map, this, b >>>= 1, rights, transformer, reducer)).fork();
5864 dl 1.61 }
5865     U r = null, u;
5866     Object v;
5867     while ((v = advance()) != null) {
5868     if ((u = transformer.apply(entryFor((K)nextKey, (V)v))) != null)
5869     r = (r == null) ? u : reducer.apply(r, u);
5870 dl 1.52 }
5871 dl 1.61 result = r;
5872     for (MapReduceEntriesTask<K,V,U> t = this, s;;) {
5873     int c; BulkTask<K,V,?> par; U tr, sr;
5874     if ((c = t.pending) == 0) {
5875     for (s = t.rights; s != null; s = t.rights = s.nextRight) {
5876     if ((sr = s.result) != null)
5877 jsr166 1.65 t.result = ((tr = t.result) == null) ? sr : reducer.apply(tr, sr);
5878 dl 1.61 }
5879     if ((par = t.parent) == null ||
5880     !(par instanceof MapReduceEntriesTask)) {
5881     t.quietlyComplete();
5882     break;
5883     }
5884     t = (MapReduceEntriesTask<K,V,U>)par;
5885     }
5886     else if (t.casPending(c, c - 1))
5887     break;
5888 dl 1.52 }
5889 dl 1.61 } catch (Throwable ex) {
5890     return tryCompleteComputation(ex);
5891 dl 1.52 }
5892 dl 1.71 MapReduceEntriesTask<K,V,U> s = rights;
5893     if (s != null && !inForkJoinPool()) {
5894     do {
5895     if (s.tryUnfork())
5896     s.exec();
5897     } while ((s = s.nextRight) != null);
5898     }
5899 dl 1.61 return false;
5900 dl 1.52 }
5901     public final U getRawResult() { return result; }
5902     }
5903    
5904 dl 1.61 @SuppressWarnings("serial") static final class MapReduceMappingsTask<K,V,U>
5905 dl 1.52 extends BulkTask<K,V,U> {
5906     final BiFun<? super K, ? super V, ? extends U> transformer;
5907     final BiFun<? super U, ? super U, ? extends U> reducer;
5908     U result;
5909 dl 1.61 MapReduceMappingsTask<K,V,U> rights, nextRight;
5910 dl 1.52 MapReduceMappingsTask
5911 dl 1.63 (ConcurrentHashMapV8<K,V> m, BulkTask<K,V,?> p, int b,
5912 dl 1.61 MapReduceMappingsTask<K,V,U> nextRight,
5913 dl 1.52 BiFun<? super K, ? super V, ? extends U> transformer,
5914     BiFun<? super U, ? super U, ? extends U> reducer) {
5915 dl 1.63 super(m, p, b); this.nextRight = nextRight;
5916 dl 1.52 this.transformer = transformer;
5917     this.reducer = reducer;
5918     }
5919 dl 1.61 @SuppressWarnings("unchecked") public final boolean exec() {
5920 dl 1.52 final BiFun<? super K, ? super V, ? extends U> transformer =
5921     this.transformer;
5922     final BiFun<? super U, ? super U, ? extends U> reducer =
5923     this.reducer;
5924     if (transformer == null || reducer == null)
5925 dl 1.61 return abortOnNullFunction();
5926     try {
5927     for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
5928     do {} while (!casPending(c = pending, c+1));
5929     (rights = new MapReduceMappingsTask<K,V,U>
5930 dl 1.63 (map, this, b >>>= 1, rights, transformer, reducer)).fork();
5931 dl 1.61 }
5932     U r = null, u;
5933     Object v;
5934     while ((v = advance()) != null) {
5935     if ((u = transformer.apply((K)nextKey, (V)v)) != null)
5936     r = (r == null) ? u : reducer.apply(r, u);
5937 dl 1.52 }
5938 dl 1.61 result = r;
5939     for (MapReduceMappingsTask<K,V,U> t = this, s;;) {
5940     int c; BulkTask<K,V,?> par; U tr, sr;
5941     if ((c = t.pending) == 0) {
5942     for (s = t.rights; s != null; s = t.rights = s.nextRight) {
5943     if ((sr = s.result) != null)
5944 jsr166 1.65 t.result = ((tr = t.result) == null) ? sr : reducer.apply(tr, sr);
5945 dl 1.61 }
5946     if ((par = t.parent) == null ||
5947     !(par instanceof MapReduceMappingsTask)) {
5948     t.quietlyComplete();
5949     break;
5950     }
5951     t = (MapReduceMappingsTask<K,V,U>)par;
5952     }
5953     else if (t.casPending(c, c - 1))
5954     break;
5955 dl 1.52 }
5956 dl 1.61 } catch (Throwable ex) {
5957     return tryCompleteComputation(ex);
5958 dl 1.52 }
5959 dl 1.71 MapReduceMappingsTask<K,V,U> s = rights;
5960     if (s != null && !inForkJoinPool()) {
5961     do {
5962     if (s.tryUnfork())
5963     s.exec();
5964     } while ((s = s.nextRight) != null);
5965     }
5966 dl 1.61 return false;
5967 dl 1.52 }
5968     public final U getRawResult() { return result; }
5969     }
5970    
5971 dl 1.61 @SuppressWarnings("serial") static final class MapReduceKeysToDoubleTask<K,V>
5972 dl 1.52 extends BulkTask<K,V,Double> {
5973     final ObjectToDouble<? super K> transformer;
5974     final DoubleByDoubleToDouble reducer;
5975     final double basis;
5976     double result;
5977 dl 1.61 MapReduceKeysToDoubleTask<K,V> rights, nextRight;
5978 dl 1.52 MapReduceKeysToDoubleTask
5979 dl 1.63 (ConcurrentHashMapV8<K,V> m, BulkTask<K,V,?> p, int b,
5980 dl 1.61 MapReduceKeysToDoubleTask<K,V> nextRight,
5981 dl 1.52 ObjectToDouble<? super K> transformer,
5982     double basis,
5983     DoubleByDoubleToDouble reducer) {
5984 dl 1.63 super(m, p, b); this.nextRight = nextRight;
5985 dl 1.52 this.transformer = transformer;
5986     this.basis = basis; this.reducer = reducer;
5987     }
5988 dl 1.61 @SuppressWarnings("unchecked") public final boolean exec() {
5989 dl 1.52 final ObjectToDouble<? super K> transformer =
5990     this.transformer;
5991     final DoubleByDoubleToDouble reducer = this.reducer;
5992     if (transformer == null || reducer == null)
5993 dl 1.61 return abortOnNullFunction();
5994     try {
5995     final double id = this.basis;
5996     for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
5997     do {} while (!casPending(c = pending, c+1));
5998     (rights = new MapReduceKeysToDoubleTask<K,V>
5999 dl 1.63 (map, this, b >>>= 1, rights, transformer, id, reducer)).fork();
6000 dl 1.61 }
6001     double r = id;
6002     while (advance() != null)
6003     r = reducer.apply(r, transformer.apply((K)nextKey));
6004     result = r;
6005     for (MapReduceKeysToDoubleTask<K,V> t = this, s;;) {
6006     int c; BulkTask<K,V,?> par;
6007     if ((c = t.pending) == 0) {
6008     for (s = t.rights; s != null; s = t.rights = s.nextRight) {
6009     t.result = reducer.apply(t.result, s.result);
6010     }
6011     if ((par = t.parent) == null ||
6012     !(par instanceof MapReduceKeysToDoubleTask)) {
6013     t.quietlyComplete();
6014     break;
6015     }
6016     t = (MapReduceKeysToDoubleTask<K,V>)par;
6017     }
6018     else if (t.casPending(c, c - 1))
6019     break;
6020 dl 1.52 }
6021 dl 1.61 } catch (Throwable ex) {
6022     return tryCompleteComputation(ex);
6023 dl 1.52 }
6024 dl 1.71 MapReduceKeysToDoubleTask<K,V> s = rights;
6025     if (s != null && !inForkJoinPool()) {
6026     do {
6027     if (s.tryUnfork())
6028     s.exec();
6029     } while ((s = s.nextRight) != null);
6030     }
6031 dl 1.61 return false;
6032 dl 1.52 }
6033     public final Double getRawResult() { return result; }
6034     }
6035    
6036 dl 1.61 @SuppressWarnings("serial") static final class MapReduceValuesToDoubleTask<K,V>
6037 dl 1.52 extends BulkTask<K,V,Double> {
6038     final ObjectToDouble<? super V> transformer;
6039     final DoubleByDoubleToDouble reducer;
6040     final double basis;
6041     double result;
6042 dl 1.61 MapReduceValuesToDoubleTask<K,V> rights, nextRight;
6043 dl 1.52 MapReduceValuesToDoubleTask
6044 dl 1.63 (ConcurrentHashMapV8<K,V> m, BulkTask<K,V,?> p, int b,
6045 dl 1.61 MapReduceValuesToDoubleTask<K,V> nextRight,
6046 dl 1.52 ObjectToDouble<? super V> transformer,
6047     double basis,
6048     DoubleByDoubleToDouble reducer) {
6049 dl 1.63 super(m, p, b); this.nextRight = nextRight;
6050 dl 1.52 this.transformer = transformer;
6051     this.basis = basis; this.reducer = reducer;
6052     }
6053 dl 1.61 @SuppressWarnings("unchecked") public final boolean exec() {
6054 dl 1.52 final ObjectToDouble<? super V> transformer =
6055     this.transformer;
6056     final DoubleByDoubleToDouble reducer = this.reducer;
6057     if (transformer == null || reducer == null)
6058 dl 1.61 return abortOnNullFunction();
6059     try {
6060     final double id = this.basis;
6061     for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
6062     do {} while (!casPending(c = pending, c+1));
6063     (rights = new MapReduceValuesToDoubleTask<K,V>
6064 dl 1.63 (map, this, b >>>= 1, rights, transformer, id, reducer)).fork();
6065 dl 1.61 }
6066     double r = id;
6067     Object v;
6068     while ((v = advance()) != null)
6069     r = reducer.apply(r, transformer.apply((V)v));
6070     result = r;
6071     for (MapReduceValuesToDoubleTask<K,V> t = this, s;;) {
6072     int c; BulkTask<K,V,?> par;
6073     if ((c = t.pending) == 0) {
6074     for (s = t.rights; s != null; s = t.rights = s.nextRight) {
6075     t.result = reducer.apply(t.result, s.result);
6076     }
6077     if ((par = t.parent) == null ||
6078     !(par instanceof MapReduceValuesToDoubleTask)) {
6079     t.quietlyComplete();
6080     break;
6081     }
6082     t = (MapReduceValuesToDoubleTask<K,V>)par;
6083     }
6084     else if (t.casPending(c, c - 1))
6085     break;
6086 dl 1.52 }
6087 dl 1.61 } catch (Throwable ex) {
6088     return tryCompleteComputation(ex);
6089 dl 1.52 }
6090 dl 1.71 MapReduceValuesToDoubleTask<K,V> s = rights;
6091     if (s != null && !inForkJoinPool()) {
6092     do {
6093     if (s.tryUnfork())
6094     s.exec();
6095     } while ((s = s.nextRight) != null);
6096     }
6097 dl 1.61 return false;
6098 dl 1.52 }
6099     public final Double getRawResult() { return result; }
6100     }
6101    
6102 dl 1.61 @SuppressWarnings("serial") static final class MapReduceEntriesToDoubleTask<K,V>
6103 dl 1.52 extends BulkTask<K,V,Double> {
6104     final ObjectToDouble<Map.Entry<K,V>> transformer;
6105     final DoubleByDoubleToDouble reducer;
6106     final double basis;
6107     double result;
6108 dl 1.61 MapReduceEntriesToDoubleTask<K,V> rights, nextRight;
6109 dl 1.52 MapReduceEntriesToDoubleTask
6110 dl 1.63 (ConcurrentHashMapV8<K,V> m, BulkTask<K,V,?> p, int b,
6111 dl 1.61 MapReduceEntriesToDoubleTask<K,V> nextRight,
6112 dl 1.52 ObjectToDouble<Map.Entry<K,V>> transformer,
6113     double basis,
6114     DoubleByDoubleToDouble reducer) {
6115 dl 1.63 super(m, p, b); this.nextRight = nextRight;
6116 dl 1.52 this.transformer = transformer;
6117     this.basis = basis; this.reducer = reducer;
6118     }
6119 dl 1.61 @SuppressWarnings("unchecked") public final boolean exec() {
6120 dl 1.52 final ObjectToDouble<Map.Entry<K,V>> transformer =
6121     this.transformer;
6122     final DoubleByDoubleToDouble reducer = this.reducer;
6123     if (transformer == null || reducer == null)
6124 dl 1.61 return abortOnNullFunction();
6125     try {
6126     final double id = this.basis;
6127     for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
6128     do {} while (!casPending(c = pending, c+1));
6129     (rights = new MapReduceEntriesToDoubleTask<K,V>
6130 dl 1.63 (map, this, b >>>= 1, rights, transformer, id, reducer)).fork();
6131 dl 1.61 }
6132     double r = id;
6133     Object v;
6134     while ((v = advance()) != null)
6135     r = reducer.apply(r, transformer.apply(entryFor((K)nextKey, (V)v)));
6136     result = r;
6137     for (MapReduceEntriesToDoubleTask<K,V> t = this, s;;) {
6138     int c; BulkTask<K,V,?> par;
6139     if ((c = t.pending) == 0) {
6140     for (s = t.rights; s != null; s = t.rights = s.nextRight) {
6141     t.result = reducer.apply(t.result, s.result);
6142     }
6143     if ((par = t.parent) == null ||
6144     !(par instanceof MapReduceEntriesToDoubleTask)) {
6145     t.quietlyComplete();
6146     break;
6147     }
6148     t = (MapReduceEntriesToDoubleTask<K,V>)par;
6149     }
6150     else if (t.casPending(c, c - 1))
6151     break;
6152 dl 1.52 }
6153 dl 1.61 } catch (Throwable ex) {
6154     return tryCompleteComputation(ex);
6155 dl 1.52 }
6156 dl 1.71 MapReduceEntriesToDoubleTask<K,V> s = rights;
6157     if (s != null && !inForkJoinPool()) {
6158     do {
6159     if (s.tryUnfork())
6160     s.exec();
6161     } while ((s = s.nextRight) != null);
6162     }
6163 dl 1.61 return false;
6164 dl 1.52 }
6165     public final Double getRawResult() { return result; }
6166     }
6167    
6168 dl 1.61 @SuppressWarnings("serial") static final class MapReduceMappingsToDoubleTask<K,V>
6169 dl 1.52 extends BulkTask<K,V,Double> {
6170     final ObjectByObjectToDouble<? super K, ? super V> transformer;
6171     final DoubleByDoubleToDouble reducer;
6172     final double basis;
6173     double result;
6174 dl 1.61 MapReduceMappingsToDoubleTask<K,V> rights, nextRight;
6175 dl 1.52 MapReduceMappingsToDoubleTask
6176 dl 1.63 (ConcurrentHashMapV8<K,V> m, BulkTask<K,V,?> p, int b,
6177 dl 1.61 MapReduceMappingsToDoubleTask<K,V> nextRight,
6178 dl 1.52 ObjectByObjectToDouble<? super K, ? super V> transformer,
6179     double basis,
6180     DoubleByDoubleToDouble reducer) {
6181 dl 1.63 super(m, p, b); this.nextRight = nextRight;
6182 dl 1.52 this.transformer = transformer;
6183     this.basis = basis; this.reducer = reducer;
6184     }
6185 dl 1.61 @SuppressWarnings("unchecked") public final boolean exec() {
6186 dl 1.52 final ObjectByObjectToDouble<? super K, ? super V> transformer =
6187     this.transformer;
6188     final DoubleByDoubleToDouble reducer = this.reducer;
6189     if (transformer == null || reducer == null)
6190 dl 1.61 return abortOnNullFunction();
6191     try {
6192     final double id = this.basis;
6193     for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
6194     do {} while (!casPending(c = pending, c+1));
6195     (rights = new MapReduceMappingsToDoubleTask<K,V>
6196 dl 1.63 (map, this, b >>>= 1, rights, transformer, id, reducer)).fork();
6197 dl 1.61 }
6198     double r = id;
6199     Object v;
6200     while ((v = advance()) != null)
6201     r = reducer.apply(r, transformer.apply((K)nextKey, (V)v));
6202     result = r;
6203     for (MapReduceMappingsToDoubleTask<K,V> t = this, s;;) {
6204     int c; BulkTask<K,V,?> par;
6205     if ((c = t.pending) == 0) {
6206     for (s = t.rights; s != null; s = t.rights = s.nextRight) {
6207     t.result = reducer.apply(t.result, s.result);
6208     }
6209     if ((par = t.parent) == null ||
6210     !(par instanceof MapReduceMappingsToDoubleTask)) {
6211     t.quietlyComplete();
6212     break;
6213     }
6214     t = (MapReduceMappingsToDoubleTask<K,V>)par;
6215     }
6216     else if (t.casPending(c, c - 1))
6217     break;
6218 dl 1.52 }
6219 dl 1.61 } catch (Throwable ex) {
6220     return tryCompleteComputation(ex);
6221 dl 1.52 }
6222 dl 1.71 MapReduceMappingsToDoubleTask<K,V> s = rights;
6223     if (s != null && !inForkJoinPool()) {
6224     do {
6225     if (s.tryUnfork())
6226     s.exec();
6227     } while ((s = s.nextRight) != null);
6228     }
6229 dl 1.61 return false;
6230 dl 1.52 }
6231     public final Double getRawResult() { return result; }
6232     }
6233    
6234 dl 1.61 @SuppressWarnings("serial") static final class MapReduceKeysToLongTask<K,V>
6235 dl 1.52 extends BulkTask<K,V,Long> {
6236     final ObjectToLong<? super K> transformer;
6237     final LongByLongToLong reducer;
6238     final long basis;
6239     long result;
6240 dl 1.61 MapReduceKeysToLongTask<K,V> rights, nextRight;
6241 dl 1.52 MapReduceKeysToLongTask
6242 dl 1.63 (ConcurrentHashMapV8<K,V> m, BulkTask<K,V,?> p, int b,
6243 dl 1.61 MapReduceKeysToLongTask<K,V> nextRight,
6244 dl 1.52 ObjectToLong<? super K> transformer,
6245     long basis,
6246     LongByLongToLong reducer) {
6247 dl 1.63 super(m, p, b); this.nextRight = nextRight;
6248 dl 1.52 this.transformer = transformer;
6249     this.basis = basis; this.reducer = reducer;
6250     }
6251 dl 1.61 @SuppressWarnings("unchecked") public final boolean exec() {
6252 dl 1.52 final ObjectToLong<? super K> transformer =
6253     this.transformer;
6254     final LongByLongToLong reducer = this.reducer;
6255     if (transformer == null || reducer == null)
6256 dl 1.61 return abortOnNullFunction();
6257     try {
6258     final long id = this.basis;
6259     for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
6260     do {} while (!casPending(c = pending, c+1));
6261     (rights = new MapReduceKeysToLongTask<K,V>
6262 dl 1.63 (map, this, b >>>= 1, rights, transformer, id, reducer)).fork();
6263 dl 1.61 }
6264     long r = id;
6265     while (advance() != null)
6266     r = reducer.apply(r, transformer.apply((K)nextKey));
6267     result = r;
6268     for (MapReduceKeysToLongTask<K,V> t = this, s;;) {
6269     int c; BulkTask<K,V,?> par;
6270     if ((c = t.pending) == 0) {
6271     for (s = t.rights; s != null; s = t.rights = s.nextRight) {
6272     t.result = reducer.apply(t.result, s.result);
6273     }
6274     if ((par = t.parent) == null ||
6275     !(par instanceof MapReduceKeysToLongTask)) {
6276     t.quietlyComplete();
6277     break;
6278     }
6279     t = (MapReduceKeysToLongTask<K,V>)par;
6280     }
6281     else if (t.casPending(c, c - 1))
6282     break;
6283 dl 1.52 }
6284 dl 1.61 } catch (Throwable ex) {
6285     return tryCompleteComputation(ex);
6286 dl 1.52 }
6287 dl 1.71 MapReduceKeysToLongTask<K,V> s = rights;
6288     if (s != null && !inForkJoinPool()) {
6289     do {
6290     if (s.tryUnfork())
6291     s.exec();
6292     } while ((s = s.nextRight) != null);
6293     }
6294 dl 1.61 return false;
6295 dl 1.52 }
6296     public final Long getRawResult() { return result; }
6297     }
6298    
6299 dl 1.61 @SuppressWarnings("serial") static final class MapReduceValuesToLongTask<K,V>
6300 dl 1.52 extends BulkTask<K,V,Long> {
6301     final ObjectToLong<? super V> transformer;
6302     final LongByLongToLong reducer;
6303     final long basis;
6304     long result;
6305 dl 1.61 MapReduceValuesToLongTask<K,V> rights, nextRight;
6306 dl 1.52 MapReduceValuesToLongTask
6307 dl 1.63 (ConcurrentHashMapV8<K,V> m, BulkTask<K,V,?> p, int b,
6308 dl 1.61 MapReduceValuesToLongTask<K,V> nextRight,
6309 dl 1.52 ObjectToLong<? super V> transformer,
6310     long basis,
6311     LongByLongToLong reducer) {
6312 dl 1.63 super(m, p, b); this.nextRight = nextRight;
6313 dl 1.52 this.transformer = transformer;
6314     this.basis = basis; this.reducer = reducer;
6315     }
6316 dl 1.61 @SuppressWarnings("unchecked") public final boolean exec() {
6317 dl 1.52 final ObjectToLong<? super V> transformer =
6318     this.transformer;
6319     final LongByLongToLong reducer = this.reducer;
6320     if (transformer == null || reducer == null)
6321 dl 1.61 return abortOnNullFunction();
6322     try {
6323     final long id = this.basis;
6324     for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
6325     do {} while (!casPending(c = pending, c+1));
6326     (rights = new MapReduceValuesToLongTask<K,V>
6327 dl 1.63 (map, this, b >>>= 1, rights, transformer, id, reducer)).fork();
6328 dl 1.61 }
6329     long r = id;
6330     Object v;
6331     while ((v = advance()) != null)
6332     r = reducer.apply(r, transformer.apply((V)v));
6333     result = r;
6334     for (MapReduceValuesToLongTask<K,V> t = this, s;;) {
6335     int c; BulkTask<K,V,?> par;
6336     if ((c = t.pending) == 0) {
6337     for (s = t.rights; s != null; s = t.rights = s.nextRight) {
6338     t.result = reducer.apply(t.result, s.result);
6339     }
6340     if ((par = t.parent) == null ||
6341     !(par instanceof MapReduceValuesToLongTask)) {
6342     t.quietlyComplete();
6343     break;
6344     }
6345     t = (MapReduceValuesToLongTask<K,V>)par;
6346     }
6347     else if (t.casPending(c, c - 1))
6348     break;
6349 dl 1.52 }
6350 dl 1.61 } catch (Throwable ex) {
6351     return tryCompleteComputation(ex);
6352 dl 1.52 }
6353 dl 1.71 MapReduceValuesToLongTask<K,V> s = rights;
6354     if (s != null && !inForkJoinPool()) {
6355     do {
6356     if (s.tryUnfork())
6357     s.exec();
6358     } while ((s = s.nextRight) != null);
6359     }
6360 dl 1.61 return false;
6361 dl 1.52 }
6362     public final Long getRawResult() { return result; }
6363     }
6364    
6365 dl 1.61 @SuppressWarnings("serial") static final class MapReduceEntriesToLongTask<K,V>
6366 dl 1.52 extends BulkTask<K,V,Long> {
6367     final ObjectToLong<Map.Entry<K,V>> transformer;
6368     final LongByLongToLong reducer;
6369     final long basis;
6370     long result;
6371 dl 1.61 MapReduceEntriesToLongTask<K,V> rights, nextRight;
6372 dl 1.52 MapReduceEntriesToLongTask
6373 dl 1.63 (ConcurrentHashMapV8<K,V> m, BulkTask<K,V,?> p, int b,
6374 dl 1.61 MapReduceEntriesToLongTask<K,V> nextRight,
6375 dl 1.52 ObjectToLong<Map.Entry<K,V>> transformer,
6376     long basis,
6377     LongByLongToLong reducer) {
6378 dl 1.63 super(m, p, b); this.nextRight = nextRight;
6379 dl 1.52 this.transformer = transformer;
6380     this.basis = basis; this.reducer = reducer;
6381     }
6382 dl 1.61 @SuppressWarnings("unchecked") public final boolean exec() {
6383 dl 1.52 final ObjectToLong<Map.Entry<K,V>> transformer =
6384     this.transformer;
6385     final LongByLongToLong reducer = this.reducer;
6386     if (transformer == null || reducer == null)
6387 dl 1.61 return abortOnNullFunction();
6388     try {
6389     final long id = this.basis;
6390     for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
6391     do {} while (!casPending(c = pending, c+1));
6392     (rights = new MapReduceEntriesToLongTask<K,V>
6393 dl 1.63 (map, this, b >>>= 1, rights, transformer, id, reducer)).fork();
6394 dl 1.61 }
6395     long r = id;
6396     Object v;
6397     while ((v = advance()) != null)
6398     r = reducer.apply(r, transformer.apply(entryFor((K)nextKey, (V)v)));
6399     result = r;
6400     for (MapReduceEntriesToLongTask<K,V> t = this, s;;) {
6401     int c; BulkTask<K,V,?> par;
6402     if ((c = t.pending) == 0) {
6403     for (s = t.rights; s != null; s = t.rights = s.nextRight) {
6404     t.result = reducer.apply(t.result, s.result);
6405     }
6406     if ((par = t.parent) == null ||
6407     !(par instanceof MapReduceEntriesToLongTask)) {
6408     t.quietlyComplete();
6409     break;
6410     }
6411     t = (MapReduceEntriesToLongTask<K,V>)par;
6412     }
6413     else if (t.casPending(c, c - 1))
6414     break;
6415 dl 1.52 }
6416 dl 1.61 } catch (Throwable ex) {
6417     return tryCompleteComputation(ex);
6418 dl 1.52 }
6419 dl 1.71 MapReduceEntriesToLongTask<K,V> s = rights;
6420     if (s != null && !inForkJoinPool()) {
6421     do {
6422     if (s.tryUnfork())
6423     s.exec();
6424     } while ((s = s.nextRight) != null);
6425     }
6426 dl 1.61 return false;
6427 dl 1.52 }
6428     public final Long getRawResult() { return result; }
6429     }
6430    
6431 dl 1.61 @SuppressWarnings("serial") static final class MapReduceMappingsToLongTask<K,V>
6432 dl 1.52 extends BulkTask<K,V,Long> {
6433     final ObjectByObjectToLong<? super K, ? super V> transformer;
6434     final LongByLongToLong reducer;
6435     final long basis;
6436     long result;
6437 dl 1.61 MapReduceMappingsToLongTask<K,V> rights, nextRight;
6438 dl 1.52 MapReduceMappingsToLongTask
6439 dl 1.63 (ConcurrentHashMapV8<K,V> m, BulkTask<K,V,?> p, int b,
6440 dl 1.61 MapReduceMappingsToLongTask<K,V> nextRight,
6441 dl 1.52 ObjectByObjectToLong<? super K, ? super V> transformer,
6442     long basis,
6443     LongByLongToLong reducer) {
6444 dl 1.63 super(m, p, b); this.nextRight = nextRight;
6445 dl 1.52 this.transformer = transformer;
6446     this.basis = basis; this.reducer = reducer;
6447     }
6448 dl 1.61 @SuppressWarnings("unchecked") public final boolean exec() {
6449 dl 1.52 final ObjectByObjectToLong<? super K, ? super V> transformer =
6450     this.transformer;
6451     final LongByLongToLong reducer = this.reducer;
6452     if (transformer == null || reducer == null)
6453 dl 1.61 return abortOnNullFunction();
6454     try {
6455     final long id = this.basis;
6456     for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
6457     do {} while (!casPending(c = pending, c+1));
6458     (rights = new MapReduceMappingsToLongTask<K,V>
6459 dl 1.63 (map, this, b >>>= 1, rights, transformer, id, reducer)).fork();
6460 dl 1.61 }
6461     long r = id;
6462     Object v;
6463     while ((v = advance()) != null)
6464     r = reducer.apply(r, transformer.apply((K)nextKey, (V)v));
6465     result = r;
6466     for (MapReduceMappingsToLongTask<K,V> t = this, s;;) {
6467     int c; BulkTask<K,V,?> par;
6468     if ((c = t.pending) == 0) {
6469     for (s = t.rights; s != null; s = t.rights = s.nextRight) {
6470     t.result = reducer.apply(t.result, s.result);
6471     }
6472     if ((par = t.parent) == null ||
6473     !(par instanceof MapReduceMappingsToLongTask)) {
6474     t.quietlyComplete();
6475     break;
6476     }
6477     t = (MapReduceMappingsToLongTask<K,V>)par;
6478     }
6479     else if (t.casPending(c, c - 1))
6480     break;
6481 dl 1.52 }
6482 dl 1.61 } catch (Throwable ex) {
6483     return tryCompleteComputation(ex);
6484 dl 1.52 }
6485 dl 1.71 MapReduceMappingsToLongTask<K,V> s = rights;
6486     if (s != null && !inForkJoinPool()) {
6487     do {
6488     if (s.tryUnfork())
6489     s.exec();
6490     } while ((s = s.nextRight) != null);
6491     }
6492 dl 1.61 return false;
6493 dl 1.52 }
6494     public final Long getRawResult() { return result; }
6495     }
6496    
6497 dl 1.61 @SuppressWarnings("serial") static final class MapReduceKeysToIntTask<K,V>
6498 dl 1.52 extends BulkTask<K,V,Integer> {
6499     final ObjectToInt<? super K> transformer;
6500     final IntByIntToInt reducer;
6501     final int basis;
6502     int result;
6503 dl 1.61 MapReduceKeysToIntTask<K,V> rights, nextRight;
6504 dl 1.52 MapReduceKeysToIntTask
6505 dl 1.63 (ConcurrentHashMapV8<K,V> m, BulkTask<K,V,?> p, int b,
6506 dl 1.61 MapReduceKeysToIntTask<K,V> nextRight,
6507 dl 1.52 ObjectToInt<? super K> transformer,
6508     int basis,
6509     IntByIntToInt reducer) {
6510 dl 1.63 super(m, p, b); this.nextRight = nextRight;
6511 dl 1.52 this.transformer = transformer;
6512     this.basis = basis; this.reducer = reducer;
6513     }
6514 dl 1.61 @SuppressWarnings("unchecked") public final boolean exec() {
6515 dl 1.52 final ObjectToInt<? super K> transformer =
6516     this.transformer;
6517     final IntByIntToInt reducer = this.reducer;
6518     if (transformer == null || reducer == null)
6519 dl 1.61 return abortOnNullFunction();
6520     try {
6521     final int id = this.basis;
6522     for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
6523     do {} while (!casPending(c = pending, c+1));
6524     (rights = new MapReduceKeysToIntTask<K,V>
6525 dl 1.63 (map, this, b >>>= 1, rights, transformer, id, reducer)).fork();
6526 dl 1.61 }
6527     int r = id;
6528     while (advance() != null)
6529     r = reducer.apply(r, transformer.apply((K)nextKey));
6530     result = r;
6531     for (MapReduceKeysToIntTask<K,V> t = this, s;;) {
6532     int c; BulkTask<K,V,?> par;
6533     if ((c = t.pending) == 0) {
6534     for (s = t.rights; s != null; s = t.rights = s.nextRight) {
6535     t.result = reducer.apply(t.result, s.result);
6536     }
6537     if ((par = t.parent) == null ||
6538     !(par instanceof MapReduceKeysToIntTask)) {
6539     t.quietlyComplete();
6540     break;
6541     }
6542     t = (MapReduceKeysToIntTask<K,V>)par;
6543     }
6544     else if (t.casPending(c, c - 1))
6545     break;
6546 dl 1.52 }
6547 dl 1.61 } catch (Throwable ex) {
6548     return tryCompleteComputation(ex);
6549 dl 1.52 }
6550 dl 1.71 MapReduceKeysToIntTask<K,V> s = rights;
6551     if (s != null && !inForkJoinPool()) {
6552     do {
6553     if (s.tryUnfork())
6554     s.exec();
6555     } while ((s = s.nextRight) != null);
6556     }
6557 dl 1.61 return false;
6558 dl 1.52 }
6559     public final Integer getRawResult() { return result; }
6560     }
6561    
6562 dl 1.61 @SuppressWarnings("serial") static final class MapReduceValuesToIntTask<K,V>
6563 dl 1.52 extends BulkTask<K,V,Integer> {
6564     final ObjectToInt<? super V> transformer;
6565     final IntByIntToInt reducer;
6566     final int basis;
6567     int result;
6568 dl 1.61 MapReduceValuesToIntTask<K,V> rights, nextRight;
6569 dl 1.52 MapReduceValuesToIntTask
6570 dl 1.63 (ConcurrentHashMapV8<K,V> m, BulkTask<K,V,?> p, int b,
6571 dl 1.61 MapReduceValuesToIntTask<K,V> nextRight,
6572 dl 1.52 ObjectToInt<? super V> transformer,
6573     int basis,
6574     IntByIntToInt reducer) {
6575 dl 1.63 super(m, p, b); this.nextRight = nextRight;
6576 dl 1.52 this.transformer = transformer;
6577     this.basis = basis; this.reducer = reducer;
6578     }
6579 dl 1.61 @SuppressWarnings("unchecked") public final boolean exec() {
6580 dl 1.52 final ObjectToInt<? super V> transformer =
6581     this.transformer;
6582     final IntByIntToInt reducer = this.reducer;
6583     if (transformer == null || reducer == null)
6584 dl 1.61 return abortOnNullFunction();
6585     try {
6586     final int id = this.basis;
6587     for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
6588     do {} while (!casPending(c = pending, c+1));
6589     (rights = new MapReduceValuesToIntTask<K,V>
6590 dl 1.63 (map, this, b >>>= 1, rights, transformer, id, reducer)).fork();
6591 dl 1.61 }
6592     int r = id;
6593     Object v;
6594     while ((v = advance()) != null)
6595     r = reducer.apply(r, transformer.apply((V)v));
6596     result = r;
6597     for (MapReduceValuesToIntTask<K,V> t = this, s;;) {
6598     int c; BulkTask<K,V,?> par;
6599     if ((c = t.pending) == 0) {
6600     for (s = t.rights; s != null; s = t.rights = s.nextRight) {
6601     t.result = reducer.apply(t.result, s.result);
6602     }
6603     if ((par = t.parent) == null ||
6604     !(par instanceof MapReduceValuesToIntTask)) {
6605     t.quietlyComplete();
6606     break;
6607     }
6608     t = (MapReduceValuesToIntTask<K,V>)par;
6609     }
6610     else if (t.casPending(c, c - 1))
6611     break;
6612 dl 1.52 }
6613 dl 1.61 } catch (Throwable ex) {
6614     return tryCompleteComputation(ex);
6615 dl 1.52 }
6616 dl 1.71 MapReduceValuesToIntTask<K,V> s = rights;
6617     if (s != null && !inForkJoinPool()) {
6618     do {
6619     if (s.tryUnfork())
6620     s.exec();
6621     } while ((s = s.nextRight) != null);
6622     }
6623 dl 1.61 return false;
6624 dl 1.52 }
6625     public final Integer getRawResult() { return result; }
6626     }
6627    
6628 dl 1.61 @SuppressWarnings("serial") static final class MapReduceEntriesToIntTask<K,V>
6629 dl 1.52 extends BulkTask<K,V,Integer> {
6630     final ObjectToInt<Map.Entry<K,V>> transformer;
6631     final IntByIntToInt reducer;
6632     final int basis;
6633     int result;
6634 dl 1.61 MapReduceEntriesToIntTask<K,V> rights, nextRight;
6635 dl 1.52 MapReduceEntriesToIntTask
6636 dl 1.63 (ConcurrentHashMapV8<K,V> m, BulkTask<K,V,?> p, int b,
6637 dl 1.61 MapReduceEntriesToIntTask<K,V> nextRight,
6638 dl 1.52 ObjectToInt<Map.Entry<K,V>> transformer,
6639     int basis,
6640     IntByIntToInt reducer) {
6641 dl 1.63 super(m, p, b); this.nextRight = nextRight;
6642 dl 1.52 this.transformer = transformer;
6643     this.basis = basis; this.reducer = reducer;
6644     }
6645 dl 1.61 @SuppressWarnings("unchecked") public final boolean exec() {
6646 dl 1.52 final ObjectToInt<Map.Entry<K,V>> transformer =
6647     this.transformer;
6648     final IntByIntToInt reducer = this.reducer;
6649     if (transformer == null || reducer == null)
6650 dl 1.61 return abortOnNullFunction();
6651     try {
6652     final int id = this.basis;
6653     for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
6654     do {} while (!casPending(c = pending, c+1));
6655     (rights = new MapReduceEntriesToIntTask<K,V>
6656 dl 1.63 (map, this, b >>>= 1, rights, transformer, id, reducer)).fork();
6657 dl 1.61 }
6658     int r = id;
6659     Object v;
6660     while ((v = advance()) != null)
6661     r = reducer.apply(r, transformer.apply(entryFor((K)nextKey, (V)v)));
6662     result = r;
6663     for (MapReduceEntriesToIntTask<K,V> t = this, s;;) {
6664     int c; BulkTask<K,V,?> par;
6665     if ((c = t.pending) == 0) {
6666     for (s = t.rights; s != null; s = t.rights = s.nextRight) {
6667     t.result = reducer.apply(t.result, s.result);
6668     }
6669     if ((par = t.parent) == null ||
6670     !(par instanceof MapReduceEntriesToIntTask)) {
6671     t.quietlyComplete();
6672     break;
6673     }
6674     t = (MapReduceEntriesToIntTask<K,V>)par;
6675     }
6676     else if (t.casPending(c, c - 1))
6677     break;
6678 dl 1.52 }
6679 dl 1.61 } catch (Throwable ex) {
6680     return tryCompleteComputation(ex);
6681 dl 1.52 }
6682 dl 1.71 MapReduceEntriesToIntTask<K,V> s = rights;
6683     if (s != null && !inForkJoinPool()) {
6684     do {
6685     if (s.tryUnfork())
6686     s.exec();
6687     } while ((s = s.nextRight) != null);
6688     }
6689 dl 1.61 return false;
6690 dl 1.52 }
6691     public final Integer getRawResult() { return result; }
6692     }
6693    
6694 dl 1.61 @SuppressWarnings("serial") static final class MapReduceMappingsToIntTask<K,V>
6695 dl 1.52 extends BulkTask<K,V,Integer> {
6696     final ObjectByObjectToInt<? super K, ? super V> transformer;
6697     final IntByIntToInt reducer;
6698     final int basis;
6699     int result;
6700 dl 1.61 MapReduceMappingsToIntTask<K,V> rights, nextRight;
6701 dl 1.52 MapReduceMappingsToIntTask
6702 dl 1.63 (ConcurrentHashMapV8<K,V> m, BulkTask<K,V,?> p, int b,
6703     MapReduceMappingsToIntTask<K,V> rights,
6704 dl 1.52 ObjectByObjectToInt<? super K, ? super V> transformer,
6705     int basis,
6706     IntByIntToInt reducer) {
6707 dl 1.63 super(m, p, b); this.nextRight = nextRight;
6708 dl 1.52 this.transformer = transformer;
6709     this.basis = basis; this.reducer = reducer;
6710     }
6711 dl 1.61 @SuppressWarnings("unchecked") public final boolean exec() {
6712 dl 1.52 final ObjectByObjectToInt<? super K, ? super V> transformer =
6713     this.transformer;
6714     final IntByIntToInt reducer = this.reducer;
6715     if (transformer == null || reducer == null)
6716 dl 1.61 return abortOnNullFunction();
6717     try {
6718     final int id = this.basis;
6719     for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
6720     do {} while (!casPending(c = pending, c+1));
6721     (rights = new MapReduceMappingsToIntTask<K,V>
6722 dl 1.63 (map, this, b >>>= 1, rights, transformer, id, reducer)).fork();
6723 dl 1.61 }
6724     int r = id;
6725     Object v;
6726     while ((v = advance()) != null)
6727     r = reducer.apply(r, transformer.apply((K)nextKey, (V)v));
6728     result = r;
6729     for (MapReduceMappingsToIntTask<K,V> t = this, s;;) {
6730     int c; BulkTask<K,V,?> par;
6731     if ((c = t.pending) == 0) {
6732     for (s = t.rights; s != null; s = t.rights = s.nextRight) {
6733     t.result = reducer.apply(t.result, s.result);
6734     }
6735     if ((par = t.parent) == null ||
6736     !(par instanceof MapReduceMappingsToIntTask)) {
6737     t.quietlyComplete();
6738     break;
6739     }
6740     t = (MapReduceMappingsToIntTask<K,V>)par;
6741     }
6742     else if (t.casPending(c, c - 1))
6743     break;
6744 dl 1.52 }
6745 dl 1.61 } catch (Throwable ex) {
6746     return tryCompleteComputation(ex);
6747 dl 1.52 }
6748 dl 1.71 MapReduceMappingsToIntTask<K,V> s = rights;
6749     if (s != null && !inForkJoinPool()) {
6750     do {
6751     if (s.tryUnfork())
6752     s.exec();
6753     } while ((s = s.nextRight) != null);
6754     }
6755 dl 1.61 return false;
6756 dl 1.52 }
6757     public final Integer getRawResult() { return result; }
6758     }
6759    
6760 dl 1.71
6761 dl 1.52 // Unsafe mechanics
6762     private static final sun.misc.Unsafe UNSAFE;
6763     private static final long counterOffset;
6764     private static final long sizeCtlOffset;
6765     private static final long ABASE;
6766     private static final int ASHIFT;
6767    
6768     static {
6769     int ss;
6770     try {
6771     UNSAFE = getUnsafe();
6772     Class<?> k = ConcurrentHashMapV8.class;
6773     counterOffset = UNSAFE.objectFieldOffset
6774     (k.getDeclaredField("counter"));
6775     sizeCtlOffset = UNSAFE.objectFieldOffset
6776     (k.getDeclaredField("sizeCtl"));
6777     Class<?> sc = Node[].class;
6778     ABASE = UNSAFE.arrayBaseOffset(sc);
6779     ss = UNSAFE.arrayIndexScale(sc);
6780     } catch (Exception e) {
6781     throw new Error(e);
6782     }
6783     if ((ss & (ss-1)) != 0)
6784     throw new Error("data type scale not a power of two");
6785     ASHIFT = 31 - Integer.numberOfLeadingZeros(ss);
6786     }
6787    
6788     /**
6789     * Returns a sun.misc.Unsafe. Suitable for use in a 3rd party package.
6790     * Replace with a simple call to Unsafe.getUnsafe when integrating
6791     * into a jdk.
6792     *
6793     * @return a sun.misc.Unsafe
6794     */
6795     private static sun.misc.Unsafe getUnsafe() {
6796     try {
6797     return sun.misc.Unsafe.getUnsafe();
6798     } catch (SecurityException se) {
6799     try {
6800     return java.security.AccessController.doPrivileged
6801     (new java.security
6802     .PrivilegedExceptionAction<sun.misc.Unsafe>() {
6803     public sun.misc.Unsafe run() throws Exception {
6804     java.lang.reflect.Field f = sun.misc
6805     .Unsafe.class.getDeclaredField("theUnsafe");
6806     f.setAccessible(true);
6807     return (sun.misc.Unsafe) f.get(null);
6808     }});
6809     } catch (java.security.PrivilegedActionException e) {
6810     throw new RuntimeException("Could not initialize intrinsics",
6811     e.getCause());
6812     }
6813     }
6814     }
6815 dl 1.1 }