ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/jsr166e/ConcurrentHashMapV8.java
(Generate patch)

Comparing jsr166/src/jsr166e/ConcurrentHashMapV8.java (file contents):
Revision 1.16 by dl, Fri Sep 9 13:02:01 2011 UTC vs.
Revision 1.59 by dl, Tue Aug 14 13:16:50 2012 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines