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.81 by dl, Sat Dec 8 14:10:38 2012 UTC vs.
Revision 1.120 by dl, Sun Dec 1 20:55:50 2013 UTC

# Line 6 | Line 6
6  
7   package jsr166e;
8  
9 < import java.util.Comparator;
10 < import java.util.Arrays;
11 < import java.util.Map;
12 < import java.util.Set;
13 < import java.util.Collection;
14 < import java.util.AbstractMap;
15 < import java.util.AbstractSet;
16 < import java.util.AbstractCollection;
17 < import java.util.Hashtable;
18 < import java.util.HashMap;
19 < import java.util.Iterator;
20 < import java.util.Enumeration;
21 < import java.util.ConcurrentModificationException;
22 < import java.util.NoSuchElementException;
23 < import java.util.concurrent.ConcurrentMap;
24 < import java.util.concurrent.ThreadLocalRandom;
25 < import java.util.concurrent.locks.LockSupport;
26 < import java.util.concurrent.locks.AbstractQueuedSynchronizer;
27 < import java.util.concurrent.atomic.AtomicReference;
9 > import jsr166e.ForkJoinPool;
10  
11 + import java.io.ObjectStreamField;
12   import java.io.Serializable;
13 <
14 < import java.util.Comparator;
13 > import java.lang.reflect.ParameterizedType;
14 > import java.lang.reflect.Type;
15 > import java.util.AbstractMap;
16   import java.util.Arrays;
33 import java.util.Map;
34 import java.util.Set;
17   import java.util.Collection;
18 < import java.util.AbstractMap;
19 < import java.util.AbstractSet;
20 < import java.util.AbstractCollection;
39 < import java.util.Hashtable;
18 > import java.util.Comparator;
19 > import java.util.ConcurrentModificationException;
20 > import java.util.Enumeration;
21   import java.util.HashMap;
22 + import java.util.Hashtable;
23   import java.util.Iterator;
24 < import java.util.Enumeration;
43 < import java.util.ConcurrentModificationException;
24 > import java.util.Map;
25   import java.util.NoSuchElementException;
26 + import java.util.Set;
27   import java.util.concurrent.ConcurrentMap;
46 import java.util.concurrent.ThreadLocalRandom;
47 import java.util.concurrent.locks.LockSupport;
48 import java.util.concurrent.locks.AbstractQueuedSynchronizer;
28   import java.util.concurrent.atomic.AtomicReference;
29 <
30 < import java.io.Serializable;
29 > import java.util.concurrent.atomic.AtomicInteger;
30 > import java.util.concurrent.locks.LockSupport;
31 > import java.util.concurrent.locks.ReentrantLock;
32  
33   /**
34   * A hash table supporting full concurrency of retrievals and
# Line 102 | Line 82 | import java.io.Serializable;
82   * expected {@code concurrencyLevel} as an additional hint for
83   * internal sizing.  Note that using many keys with exactly the same
84   * {@code hashCode()} is a sure way to slow down performance of any
85 < * hash table.
85 > * hash table. To ameliorate impact, when keys are {@link Comparable},
86 > * this class may use comparison order among keys to help break ties.
87   *
88   * <p>A {@link Set} projection of a ConcurrentHashMapV8 may be created
89   * (using {@link #newKeySet()} or {@link #newKeySet(int)}), or viewed
# Line 110 | Line 91 | import java.io.Serializable;
91   * mapped values are (perhaps transiently) not used or all take the
92   * same mapping value.
93   *
113 * <p>A ConcurrentHashMapV8 can be used as scalable frequency map (a
114 * form of histogram or multiset) by using {@link LongAdder} values
115 * and initializing via {@link #computeIfAbsent}. For example, to add
116 * a count to a {@code ConcurrentHashMapV8<String,LongAdder> freqs}, you
117 * can use {@code freqs.computeIfAbsent(k -> new
118 * LongAdder()).increment();}
119 *
94   * <p>This class and its views and iterators implement all of the
95   * <em>optional</em> methods of the {@link Map} and {@link Iterator}
96   * interfaces.
# Line 124 | Line 98 | import java.io.Serializable;
98   * <p>Like {@link Hashtable} but unlike {@link HashMap}, this class
99   * does <em>not</em> allow {@code null} to be used as a key or value.
100   *
101 < * <p>ConcurrentHashMapV8s support parallel operations using the {@link
102 < * ForkJoinPool#commonPool}. (Tasks that may be used in other contexts
103 < * are available in class {@link ForkJoinTasks}). These operations are
104 < * designed to be safely, and often sensibly, applied even with maps
105 < * that are being concurrently updated by other threads; for example,
106 < * when computing a snapshot summary of the values in a shared
107 < * registry.  There are three kinds of operation, each with four
108 < * forms, accepting functions with Keys, Values, Entries, and (Key,
109 < * Value) arguments and/or return values. (The first three forms are
110 < * also available via the {@link #keySet()}, {@link #values()} and
111 < * {@link #entrySet()} views). Because the elements of a
112 < * ConcurrentHashMapV8 are not ordered in any particular way, and may be
113 < * processed in different orders in different parallel executions, the
114 < * correctness of supplied functions should not depend on any
115 < * ordering, or on any other objects or values that may transiently
142 < * change while computation is in progress; and except for forEach
143 < * actions, should ideally be side-effect-free.
101 > * <p>ConcurrentHashMapV8s support a set of sequential and parallel bulk
102 > * operations that are designed
103 > * to be safely, and often sensibly, applied even with maps that are
104 > * being concurrently updated by other threads; for example, when
105 > * computing a snapshot summary of the values in a shared registry.
106 > * There are three kinds of operation, each with four forms, accepting
107 > * functions with Keys, Values, Entries, and (Key, Value) arguments
108 > * and/or return values. Because the elements of a ConcurrentHashMapV8
109 > * are not ordered in any particular way, and may be processed in
110 > * different orders in different parallel executions, the correctness
111 > * of supplied functions should not depend on any ordering, or on any
112 > * other objects or values that may transiently change while
113 > * computation is in progress; and except for forEach actions, should
114 > * ideally be side-effect-free. Bulk operations on {@link java.util.Map.Entry}
115 > * objects do not support method {@code setValue}.
116   *
117   * <ul>
118   * <li> forEach: Perform a given action on each element.
# Line 167 | Line 139 | import java.io.Serializable;
139   * <li> Reductions to scalar doubles, longs, and ints, using a
140   * given basis value.</li>
141   *
170 * </li>
142   * </ul>
143 + * </li>
144   * </ul>
145   *
146 + * <p>These bulk operations accept a {@code parallelismThreshold}
147 + * argument. Methods proceed sequentially if the current map size is
148 + * estimated to be less than the given threshold. Using a value of
149 + * {@code Long.MAX_VALUE} suppresses all parallelism.  Using a value
150 + * of {@code 1} results in maximal parallelism by partitioning into
151 + * enough subtasks to fully utilize the {@link
152 + * ForkJoinPool#commonPool()} that is used for all parallel
153 + * computations. Normally, you would initially choose one of these
154 + * extreme values, and then measure performance of using in-between
155 + * values that trade off overhead versus throughput.
156 + *
157   * <p>The concurrency properties of bulk operations follow
158   * from those of ConcurrentHashMapV8: Any non-null result returned
159   * from {@code get(key)} and related access methods bears a
# Line 213 | Line 196 | import java.io.Serializable;
196   * exceptions, or would have done so if the first exception had
197   * not occurred.
198   *
199 < * <p>Parallel speedups for bulk operations compared to sequential
200 < * processing are common but not guaranteed.  Operations involving
201 < * brief functions on small maps may execute more slowly than
202 < * sequential loops if the underlying work to parallelize the
203 < * computation is more expensive than the computation itself.
204 < * Similarly, parallelization may not lead to much actual parallelism
205 < * if all processors are busy performing unrelated tasks.
199 > * <p>Speedups for parallel compared to sequential forms are common
200 > * but not guaranteed.  Parallel operations involving brief functions
201 > * on small maps may execute more slowly than sequential forms if the
202 > * underlying work to parallelize the computation is more expensive
203 > * than the computation itself.  Similarly, parallelization may not
204 > * lead to much actual parallelism if all processors are busy
205 > * performing unrelated tasks.
206   *
207   * <p>All arguments to all task methods must be non-null.
208   *
# Line 236 | Line 219 | import java.io.Serializable;
219   * @param <K> the type of keys maintained by this map
220   * @param <V> the type of mapped values
221   */
222 < public class ConcurrentHashMapV8<K, V>
223 <    implements ConcurrentMap<K, V>, Serializable {
222 > public class ConcurrentHashMapV8<K,V> extends AbstractMap<K,V>
223 >    implements ConcurrentMap<K,V>, Serializable {
224      private static final long serialVersionUID = 7249069246763182397L;
225  
226      /**
227 <     * A partitionable iterator. A Spliterator can be traversed
228 <     * directly, but can also be partitioned (before traversal) by
229 <     * creating another Spliterator that covers a non-overlapping
247 <     * portion of the elements, and so may be amenable to parallel
248 <     * execution.
249 <     *
250 <     * <p>This interface exports a subset of expected JDK8
251 <     * functionality.
252 <     *
253 <     * <p>Sample usage: Here is one (of the several) ways to compute
254 <     * the sum of the values held in a map using the ForkJoin
255 <     * framework. As illustrated here, Spliterators are well suited to
256 <     * designs in which a task repeatedly splits off half its work
257 <     * into forked subtasks until small enough to process directly,
258 <     * and then joins these subtasks. Variants of this style can also
259 <     * be used in completion-based designs.
260 <     *
261 <     * <pre>
262 <     * {@code ConcurrentHashMapV8<String, Long> m = ...
263 <     * // split as if have 8 * parallelism, for load balance
264 <     * int n = m.size();
265 <     * int p = aForkJoinPool.getParallelism() * 8;
266 <     * int split = (n < p)? n : p;
267 <     * long sum = aForkJoinPool.invoke(new SumValues(m.valueSpliterator(), split, null));
268 <     * // ...
269 <     * static class SumValues extends RecursiveTask<Long> {
270 <     *   final Spliterator<Long> s;
271 <     *   final int split;             // split while > 1
272 <     *   final SumValues nextJoin;    // records forked subtasks to join
273 <     *   SumValues(Spliterator<Long> s, int depth, SumValues nextJoin) {
274 <     *     this.s = s; this.depth = depth; this.nextJoin = nextJoin;
275 <     *   }
276 <     *   public Long compute() {
277 <     *     long sum = 0;
278 <     *     SumValues subtasks = null; // fork subtasks
279 <     *     for (int s = split >>> 1; s > 0; s >>>= 1)
280 <     *       (subtasks = new SumValues(s.split(), s, subtasks)).fork();
281 <     *     while (s.hasNext())        // directly process remaining elements
282 <     *       sum += s.next();
283 <     *     for (SumValues t = subtasks; t != null; t = t.nextJoin)
284 <     *       sum += t.join();         // collect subtask results
285 <     *     return sum;
286 <     *   }
287 <     * }
288 <     * }</pre>
227 >     * An object for traversing and partitioning elements of a source.
228 >     * This interface provides a subset of the functionality of JDK8
229 >     * java.util.Spliterator.
230       */
231 <    public static interface Spliterator<T> extends Iterator<T> {
231 >    public static interface ConcurrentHashMapSpliterator<T> {
232          /**
233 <         * Returns a Spliterator covering approximately half of the
234 <         * elements, guaranteed not to overlap with those subsequently
235 <         * returned by this Spliterator.  After invoking this method,
236 <         * the current Spliterator will <em>not</em> produce any of
296 <         * the elements of the returned Spliterator, but the two
297 <         * Spliterators together will produce all of the elements that
298 <         * would have been produced by this Spliterator had this
299 <         * method not been called. The exact number of elements
300 <         * produced by the returned Spliterator is not guaranteed, and
301 <         * may be zero (i.e., with {@code hasNext()} reporting {@code
302 <         * false}) if this Spliterator cannot be further split.
303 <         *
304 <         * @return a Spliterator covering approximately half of the
305 <         * elements
306 <         * @throws IllegalStateException if this Spliterator has
307 <         * already commenced traversing elements
233 >         * If possible, returns a new spliterator covering
234 >         * approximately one half of the elements, which will not be
235 >         * covered by this spliterator. Returns null if cannot be
236 >         * split.
237           */
238 <        Spliterator<T> split();
238 >        ConcurrentHashMapSpliterator<T> trySplit();
239 >        /**
240 >         * Returns an estimate of the number of elements covered by
241 >         * this Spliterator.
242 >         */
243 >        long estimateSize();
244 >
245 >        /** Applies the action to each untraversed element */
246 >        void forEachRemaining(Action<? super T> action);
247 >        /** If an element remains, applies the action and returns true. */
248 >        boolean tryAdvance(Action<? super T> action);
249      }
250  
251 +    // Sams
252 +    /** Interface describing a void action of one argument */
253 +    public interface Action<A> { void apply(A a); }
254 +    /** Interface describing a void action of two arguments */
255 +    public interface BiAction<A,B> { void apply(A a, B b); }
256 +    /** Interface describing a function of one argument */
257 +    public interface Fun<A,T> { T apply(A a); }
258 +    /** Interface describing a function of two arguments */
259 +    public interface BiFun<A,B,T> { T apply(A a, B b); }
260 +    /** Interface describing a function mapping its argument to a double */
261 +    public interface ObjectToDouble<A> { double apply(A a); }
262 +    /** Interface describing a function mapping its argument to a long */
263 +    public interface ObjectToLong<A> { long apply(A a); }
264 +    /** Interface describing a function mapping its argument to an int */
265 +    public interface ObjectToInt<A> {int apply(A a); }
266 +    /** Interface describing a function mapping two arguments to a double */
267 +    public interface ObjectByObjectToDouble<A,B> { double apply(A a, B b); }
268 +    /** Interface describing a function mapping two arguments to a long */
269 +    public interface ObjectByObjectToLong<A,B> { long apply(A a, B b); }
270 +    /** Interface describing a function mapping two arguments to an int */
271 +    public interface ObjectByObjectToInt<A,B> {int apply(A a, B b); }
272 +    /** Interface describing a function mapping two doubles to a double */
273 +    public interface DoubleByDoubleToDouble { double apply(double a, double b); }
274 +    /** Interface describing a function mapping two longs to a long */
275 +    public interface LongByLongToLong { long apply(long a, long b); }
276 +    /** Interface describing a function mapping two ints to an int */
277 +    public interface IntByIntToInt { int apply(int a, int b); }
278 +
279  
280      /*
281       * Overview:
# Line 320 | Line 287 | public class ConcurrentHashMapV8<K, V>
287       * the same or better than java.util.HashMap, and to support high
288       * initial insertion rates on an empty table by many threads.
289       *
290 <     * Each key-value mapping is held in a Node.  Because Node fields
291 <     * can contain special values, they are defined using plain Object
292 <     * types. Similarly in turn, all internal methods that use them
293 <     * work off Object types. And similarly, so do the internal
294 <     * methods of auxiliary iterator and view classes.  All public
295 <     * generic typed methods relay in/out of these internal methods,
296 <     * supplying null-checks and casts as needed. This also allows
297 <     * many of the public methods to be factored into a smaller number
298 <     * of internal methods (although sadly not so for the five
299 <     * variants of put-related operations). The validation-based
300 <     * approach explained below leads to a lot of code sprawl because
301 <     * retry-control precludes factoring into smaller methods.
290 >     * This map usually acts as a binned (bucketed) hash table.  Each
291 >     * key-value mapping is held in a Node.  Most nodes are instances
292 >     * of the basic Node class with hash, key, value, and next
293 >     * fields. However, various subclasses exist: TreeNodes are
294 >     * arranged in balanced trees, not lists.  TreeBins hold the roots
295 >     * of sets of TreeNodes. ForwardingNodes are placed at the heads
296 >     * of bins during resizing. ReservationNodes are used as
297 >     * placeholders while establishing values in computeIfAbsent and
298 >     * related methods.  The types TreeBin, ForwardingNode, and
299 >     * ReservationNode do not hold normal user keys, values, or
300 >     * hashes, and are readily distinguishable during search etc
301 >     * because they have negative hash fields and null key and value
302 >     * fields. (These special nodes are either uncommon or transient,
303 >     * so the impact of carrying around some unused fields is
304 >     * insignificant.)
305       *
306       * The table is lazily initialized to a power-of-two size upon the
307       * first insertion.  Each bin in the table normally contains a
# Line 339 | Line 309 | public class ConcurrentHashMapV8<K, V>
309       * Table accesses require volatile/atomic reads, writes, and
310       * CASes.  Because there is no other way to arrange this without
311       * adding further indirections, we use intrinsics
312 <     * (sun.misc.Unsafe) operations.  The lists of nodes within bins
313 <     * are always accurately traversable under volatile reads, so long
314 <     * as lookups check hash code and non-nullness of value before
315 <     * checking key equality.
316 <     *
317 <     * We use the top two bits of Node hash fields for control
348 <     * purposes -- they are available anyway because of addressing
349 <     * constraints.  As explained further below, these top bits are
350 <     * used as follows:
351 <     *  00 - Normal
352 <     *  01 - Locked
353 <     *  11 - Locked and may have a thread waiting for lock
354 <     *  10 - Node is a forwarding node
355 <     *
356 <     * The lower 30 bits of each Node's hash field contain a
357 <     * transformation of the key's hash code, except for forwarding
358 <     * nodes, for which the lower bits are zero (and so always have
359 <     * hash field == MOVED).
312 >     * (sun.misc.Unsafe) operations.
313 >     *
314 >     * We use the top (sign) bit of Node hash fields for control
315 >     * purposes -- it is available anyway because of addressing
316 >     * constraints.  Nodes with negative hash fields are specially
317 >     * handled or ignored in map methods.
318       *
319       * Insertion (via put or its variants) of the first node in an
320       * empty bin is performed by just CASing it to the bin.  This is
# Line 365 | Line 323 | public class ConcurrentHashMapV8<K, V>
323       * delete, and replace) require locks.  We do not want to waste
324       * the space required to associate a distinct lock object with
325       * each bin, so instead use the first node of a bin list itself as
326 <     * a lock. Blocking support for these locks relies on the builtin
327 <     * "synchronized" monitors.  However, we also need a tryLock
370 <     * construction, so we overlay these by using bits of the Node
371 <     * hash field for lock control (see above), and so normally use
372 <     * builtin monitors only for blocking and signalling using
373 <     * wait/notifyAll constructions. See Node.tryAwaitLock.
326 >     * a lock. Locking support for these locks relies on builtin
327 >     * "synchronized" monitors.
328       *
329       * Using the first node of a list as a lock does not by itself
330       * suffice though: When a node is locked, any update must first
331       * validate that it is still the first node after locking it, and
332       * retry if not. Because new nodes are always appended to lists,
333       * once a node is first in a bin, it remains first until deleted
334 <     * or the bin becomes invalidated (upon resizing).  However,
381 <     * operations that only conditionally update may inspect nodes
382 <     * until the point of update. This is a converse of sorts to the
383 <     * lazy locking technique described by Herlihy & Shavit.
334 >     * or the bin becomes invalidated (upon resizing).
335       *
336       * The main disadvantage of per-bin locks is that other update
337       * operations on other nodes in a bin list protected by the same
# Line 413 | Line 364 | public class ConcurrentHashMapV8<K, V>
364       * sometimes deviate significantly from uniform randomness.  This
365       * includes the case when N > (1<<30), so some keys MUST collide.
366       * Similarly for dumb or hostile usages in which multiple keys are
367 <     * designed to have identical hash codes. Also, although we guard
368 <     * against the worst effects of this (see method spread), sets of
369 <     * hashes may differ only in bits that do not impact their bin
370 <     * index for a given power-of-two mask.  So we use a secondary
371 <     * strategy that applies when the number of nodes in a bin exceeds
372 <     * a threshold, and at least one of the keys implements
422 <     * Comparable.  These TreeBins use a balanced tree to hold nodes
423 <     * (a specialized form of red-black trees), bounding search time
424 <     * to O(log N).  Each search step in a TreeBin is around twice as
367 >     * designed to have identical hash codes or ones that differs only
368 >     * in masked-out high bits. So we use a secondary strategy that
369 >     * applies when the number of nodes in a bin exceeds a
370 >     * threshold. These TreeBins use a balanced tree to hold nodes (a
371 >     * specialized form of red-black trees), bounding search time to
372 >     * O(log N).  Each search step in a TreeBin is at least twice as
373       * slow as in a regular list, but given that N cannot exceed
374       * (1<<64) (before running out of addresses) this bounds search
375       * steps, lock hold times, etc, to reasonable constants (roughly
# Line 432 | Line 380 | public class ConcurrentHashMapV8<K, V>
380       * iterators in the same way.
381       *
382       * The table is resized when occupancy exceeds a percentage
383 <     * threshold (nominally, 0.75, but see below).  Only a single
384 <     * thread performs the resize (using field "sizeCtl", to arrange
385 <     * exclusion), but the table otherwise remains usable for reads
386 <     * and updates. Resizing proceeds by transferring bins, one by
387 <     * one, from the table to the next table.  Because we are using
388 <     * power-of-two expansion, the elements from each bin must either
389 <     * stay at same index, or move with a power of two offset. We
390 <     * eliminate unnecessary node creation by catching cases where old
391 <     * nodes can be reused because their next fields won't change.  On
392 <     * average, only about one-sixth of them need cloning when a table
393 <     * doubles. The nodes they replace will be garbage collectable as
394 <     * soon as they are no longer referenced by any reader thread that
395 <     * may be in the midst of concurrently traversing table.  Upon
396 <     * transfer, the old table bin contains only a special forwarding
397 <     * node (with hash field "MOVED") that contains the next table as
398 <     * its key. On encountering a forwarding node, access and update
399 <     * operations restart, using the new table.
400 <     *
401 <     * Each bin transfer requires its bin lock. However, unlike other
402 <     * cases, a transfer can skip a bin if it fails to acquire its
403 <     * lock, and revisit it later (unless it is a TreeBin). Method
404 <     * rebuild maintains a buffer of TRANSFER_BUFFER_SIZE bins that
405 <     * have been skipped because of failure to acquire a lock, and
406 <     * blocks only if none are available (i.e., only very rarely).
407 <     * The transfer operation must also ensure that all accessible
408 <     * bins in both the old and new table are usable by any traversal.
409 <     * When there are no lock acquisition failures, this is arranged
410 <     * simply by proceeding from the last bin (table.length - 1) up
411 <     * towards the first.  Upon seeing a forwarding node, traversals
412 <     * (see class Iter) arrange to move to the new table
413 <     * without revisiting nodes.  However, when any node is skipped
414 <     * during a transfer, all earlier table bins may have become
415 <     * visible, so are initialized with a reverse-forwarding node back
416 <     * to the old table until the new ones are established. (This
417 <     * sometimes requires transiently locking a forwarding node, which
418 <     * is possible under the above encoding.) These more expensive
419 <     * mechanics trigger only when necessary.
383 >     * threshold (nominally, 0.75, but see below).  Any thread
384 >     * noticing an overfull bin may assist in resizing after the
385 >     * initiating thread allocates and sets up the replacement array.
386 >     * However, rather than stalling, these other threads may proceed
387 >     * with insertions etc.  The use of TreeBins shields us from the
388 >     * worst case effects of overfilling while resizes are in
389 >     * progress.  Resizing proceeds by transferring bins, one by one,
390 >     * from the table to the next table. However, threads claim small
391 >     * blocks of indices to transfer (via field transferIndex) before
392 >     * doing so, reducing contention.  A generation stamp in field
393 >     * sizeCtl ensures that resizings do not overlap. Because we are
394 >     * using power-of-two expansion, the elements from each bin must
395 >     * either stay at same index, or move with a power of two
396 >     * offset. We eliminate unnecessary node creation by catching
397 >     * cases where old nodes can be reused because their next fields
398 >     * won't change.  On average, only about one-sixth of them need
399 >     * cloning when a table doubles. The nodes they replace will be
400 >     * garbage collectable as soon as they are no longer referenced by
401 >     * any reader thread that may be in the midst of concurrently
402 >     * traversing table.  Upon transfer, the old table bin contains
403 >     * only a special forwarding node (with hash field "MOVED") that
404 >     * contains the next table as its key. On encountering a
405 >     * forwarding node, access and update operations restart, using
406 >     * the new table.
407 >     *
408 >     * Each bin transfer requires its bin lock, which can stall
409 >     * waiting for locks while resizing. However, because other
410 >     * threads can join in and help resize rather than contend for
411 >     * locks, average aggregate waits become shorter as resizing
412 >     * progresses.  The transfer operation must also ensure that all
413 >     * accessible bins in both the old and new table are usable by any
414 >     * traversal.  This is arranged in part by proceeding from the
415 >     * last bin (table.length - 1) up towards the first.  Upon seeing
416 >     * a forwarding node, traversals (see class Traverser) arrange to
417 >     * move to the new table without revisiting nodes.  To ensure that
418 >     * no intervening nodes are skipped even when moved out of order,
419 >     * a stack (see class TableStack) is created on first encounter of
420 >     * a forwarding node during a traversal, to maintain its place if
421 >     * later processing the current table. The need for these
422 >     * save/restore mechanics is relatively rare, but when one
423 >     * forwarding node is encountered, typically many more will be.
424 >     * So Traversers use a simple caching scheme to avoid creating so
425 >     * many new TableStack nodes. (Thanks to Peter Levart for
426 >     * suggesting use of a stack here.)
427       *
428       * The traversal scheme also applies to partial traversals of
429       * ranges of bins (via an alternate Traverser constructor)
# Line 483 | Line 438 | public class ConcurrentHashMapV8<K, V>
438       * These cases attempt to override the initial capacity settings,
439       * but harmlessly fail to take effect in cases of races.
440       *
441 <     * The element count is maintained using a LongAdder, which avoids
442 <     * contention on updates but can encounter cache thrashing if read
443 <     * too frequently during concurrent access. To avoid reading so
444 <     * often, resizing is attempted either when a bin lock is
445 <     * contended, or upon adding to a bin already holding two or more
446 <     * nodes (checked before adding in the xIfAbsent methods, after
447 <     * adding in others). Under uniform hash distributions, the
448 <     * probability of this occurring at threshold is around 13%,
449 <     * meaning that only about 1 in 8 puts check threshold (and after
450 <     * resizing, many fewer do so). But this approximation has high
451 <     * variance for small table sizes, so we check on any collision
452 <     * for sizes <= 64. The bulk putAll operation further reduces
453 <     * contention by only committing count updates upon these size
454 <     * checks.
441 >     * The element count is maintained using a specialization of
442 >     * LongAdder. We need to incorporate a specialization rather than
443 >     * just use a LongAdder in order to access implicit
444 >     * contention-sensing that leads to creation of multiple
445 >     * CounterCells.  The counter mechanics avoid contention on
446 >     * updates but can encounter cache thrashing if read too
447 >     * frequently during concurrent access. To avoid reading so often,
448 >     * resizing under contention is attempted only upon adding to a
449 >     * bin already holding two or more nodes. Under uniform hash
450 >     * distributions, the probability of this occurring at threshold
451 >     * is around 13%, meaning that only about 1 in 8 puts check
452 >     * threshold (and after resizing, many fewer do so).
453 >     *
454 >     * TreeBins use a special form of comparison for search and
455 >     * related operations (which is the main reason we cannot use
456 >     * existing collections such as TreeMaps). TreeBins contain
457 >     * Comparable elements, but may contain others, as well as
458 >     * elements that are Comparable but not necessarily Comparable for
459 >     * the same T, so we cannot invoke compareTo among them. To handle
460 >     * this, the tree is ordered primarily by hash value, then by
461 >     * Comparable.compareTo order if applicable.  On lookup at a node,
462 >     * if elements are not comparable or compare as 0 then both left
463 >     * and right children may need to be searched in the case of tied
464 >     * hash values. (This corresponds to the full list search that
465 >     * would be necessary if all elements were non-Comparable and had
466 >     * tied hashes.) On insertion, to keep a total ordering (or as
467 >     * close as is required here) across rebalancings, we compare
468 >     * classes and identityHashCodes as tie-breakers. The red-black
469 >     * balancing code is updated from pre-jdk-collections
470 >     * (http://gee.cs.oswego.edu/dl/classes/collections/RBCell.java)
471 >     * based in turn on Cormen, Leiserson, and Rivest "Introduction to
472 >     * Algorithms" (CLR).
473 >     *
474 >     * TreeBins also require an additional locking mechanism.  While
475 >     * list traversal is always possible by readers even during
476 >     * updates, tree traversal is not, mainly because of tree-rotations
477 >     * that may change the root node and/or its linkages.  TreeBins
478 >     * include a simple read-write lock mechanism parasitic on the
479 >     * main bin-synchronization strategy: Structural adjustments
480 >     * associated with an insertion or removal are already bin-locked
481 >     * (and so cannot conflict with other writers) but must wait for
482 >     * ongoing readers to finish. Since there can be only one such
483 >     * waiter, we use a simple scheme using a single "waiter" field to
484 >     * block writers.  However, readers need never block.  If the root
485 >     * lock is held, they proceed along the slow traversal path (via
486 >     * next-pointers) until the lock becomes available or the list is
487 >     * exhausted, whichever comes first. These cases are not fast, but
488 >     * maximize aggregate expected throughput.
489       *
490       * Maintaining API and serialization compatibility with previous
491       * versions of this class introduces several oddities. Mainly: We
# Line 506 | Line 495 | public class ConcurrentHashMapV8<K, V>
495       * time that we can guarantee to honor it.) We also declare an
496       * unused "Segment" class that is instantiated in minimal form
497       * only when serializing.
498 +     *
499 +     * Also, solely for compatibility with previous versions of this
500 +     * class, it extends AbstractMap, even though all of its methods
501 +     * are overridden, so it is just useless baggage.
502 +     *
503 +     * This file is organized to make things a little easier to follow
504 +     * while reading than they might otherwise: First the main static
505 +     * declarations and utilities, then fields, then main public
506 +     * methods (with a few factorings of multiple public methods into
507 +     * internal ones), then sizing methods, trees, traversers, and
508 +     * bulk operations.
509       */
510  
511      /* ---------------- Constants -------------- */
# Line 547 | Line 547 | public class ConcurrentHashMapV8<K, V>
547      private static final float LOAD_FACTOR = 0.75f;
548  
549      /**
550 <     * The buffer size for skipped bins during transfers. The
551 <     * value is arbitrary but should be large enough to avoid
552 <     * most locking stalls during resizes.
550 >     * The bin count threshold for using a tree rather than list for a
551 >     * bin.  Bins are converted to trees when adding an element to a
552 >     * bin with at least this many nodes. The value must be greater
553 >     * than 2, and should be at least 8 to mesh with assumptions in
554 >     * tree removal about conversion back to plain bins upon
555 >     * shrinkage.
556       */
557 <    private static final int TRANSFER_BUFFER_SIZE = 32;
557 >    static final int TREEIFY_THRESHOLD = 8;
558  
559      /**
560 <     * The bin count threshold for using a tree rather than list for a
561 <     * bin.  The value reflects the approximate break-even point for
562 <     * using tree-based operations.
560 >     * The bin count threshold for untreeifying a (split) bin during a
561 >     * resize operation. Should be less than TREEIFY_THRESHOLD, and at
562 >     * most 6 to mesh with shrinkage detection under removal.
563       */
564 <    private static final int TREE_THRESHOLD = 8;
564 >    static final int UNTREEIFY_THRESHOLD = 6;
565  
566 <    /*
567 <     * Encodings for special uses of Node hash fields. See above for
568 <     * explanation.
566 >    /**
567 >     * The smallest table capacity for which bins may be treeified.
568 >     * (Otherwise the table is resized if too many nodes in a bin.)
569 >     * The value should be at least 4 * TREEIFY_THRESHOLD to avoid
570 >     * conflicts between resizing and treeification thresholds.
571       */
572 <    static final int MOVED     = 0x80000000; // hash field for forwarding nodes
568 <    static final int LOCKED    = 0x40000000; // set/tested only as a bit
569 <    static final int WAITING   = 0xc0000000; // both bits set/tested together
570 <    static final int HASH_BITS = 0x3fffffff; // usable bits of normal node hash
571 <
572 <    /* ---------------- Fields -------------- */
572 >    static final int MIN_TREEIFY_CAPACITY = 64;
573  
574      /**
575 <     * The array of bins. Lazily initialized upon first insertion.
576 <     * Size is always a power of two. Accessed directly by iterators.
575 >     * Minimum number of rebinnings per transfer step. Ranges are
576 >     * subdivided to allow multiple resizer threads.  This value
577 >     * serves as a lower bound to avoid resizers encountering
578 >     * excessive memory contention.  The value should be at least
579 >     * DEFAULT_CAPACITY.
580       */
581 <    transient volatile Node[] table;
581 >    private static final int MIN_TRANSFER_STRIDE = 16;
582  
583      /**
584 <     * The counter maintaining number of elements.
584 >     * The number of bits used for generation stamp in sizeCtl.
585 >     * Must be at least 6 for 32bit arrays.
586       */
587 <    private transient final LongAdder counter;
587 >    private static int RESIZE_STAMP_BITS = 16;
588  
589      /**
590 <     * Table initialization and resizing control.  When negative, the
591 <     * table is being initialized or resized. Otherwise, when table is
588 <     * null, holds the initial table size to use upon creation, or 0
589 <     * for default. After initialization, holds the next element count
590 <     * value upon which to resize the table.
590 >     * The maximum number of threads that can help resize.
591 >     * Must fit in 32 - RESIZE_STAMP_BITS bits.
592       */
593 <    private transient volatile int sizeCtl;
593 >    private static final int MAX_RESIZERS = (1 << (32 - RESIZE_STAMP_BITS)) - 1;
594  
595 <    // views
596 <    private transient KeySetView<K,V> keySet;
597 <    private transient ValuesView<K,V> values;
598 <    private transient EntrySetView<K,V> entrySet;
598 <
599 <    /** For serialization compatibility. Null unless serialized; see below */
600 <    private Segment<K,V>[] segments;
601 <
602 <    /* ---------------- Table element access -------------- */
595 >    /**
596 >     * The bit shift for recording size stamp in sizeCtl.
597 >     */
598 >    private static final int RESIZE_STAMP_SHIFT = 32 - RESIZE_STAMP_BITS;
599  
600      /*
601 <     * Volatile access methods are used for table elements as well as
606 <     * elements of in-progress next table while resizing.  Uses are
607 <     * null checked by callers, and implicitly bounds-checked, relying
608 <     * on the invariants that tab arrays have non-zero size, and all
609 <     * indices are masked with (tab.length - 1) which is never
610 <     * negative and always less than length. Note that, to be correct
611 <     * wrt arbitrary concurrency errors by users, bounds checks must
612 <     * operate on local variables, which accounts for some odd-looking
613 <     * inline assignments below.
601 >     * Encodings for Node hash fields. See above for explanation.
602       */
603 <
604 <    static final Node tabAt(Node[] tab, int i) { // used by Iter
605 <        return (Node)UNSAFE.getObjectVolatile(tab, ((long)i<<ASHIFT)+ABASE);
606 <    }
607 <
608 <    private static final boolean casTabAt(Node[] tab, int i, Node c, Node v) {
609 <        return UNSAFE.compareAndSwapObject(tab, ((long)i<<ASHIFT)+ABASE, c, v);
610 <    }
611 <
612 <    private static final void setTabAt(Node[] tab, int i, Node v) {
613 <        UNSAFE.putObjectVolatile(tab, ((long)i<<ASHIFT)+ABASE, v);
614 <    }
603 >    static final int MOVED     = -1; // hash for forwarding nodes
604 >    static final int TREEBIN   = -2; // hash for roots of trees
605 >    static final int RESERVED  = -3; // hash for transient reservations
606 >    static final int HASH_BITS = 0x7fffffff; // usable bits of normal node hash
607 >
608 >    /** Number of CPUS, to place bounds on some sizings */
609 >    static final int NCPU = Runtime.getRuntime().availableProcessors();
610 >
611 >    /** For serialization compatibility. */
612 >    private static final ObjectStreamField[] serialPersistentFields = {
613 >        new ObjectStreamField("segments", Segment[].class),
614 >        new ObjectStreamField("segmentMask", Integer.TYPE),
615 >        new ObjectStreamField("segmentShift", Integer.TYPE)
616 >    };
617  
618      /* ---------------- Nodes -------------- */
619  
620      /**
621 <     * Key-value entry. Note that this is never exported out as a
622 <     * user-visible Map.Entry (see MapEntry below). Nodes with a hash
623 <     * field of MOVED are special, and do not contain user keys or
624 <     * values.  Otherwise, keys are never null, and null val fields
625 <     * indicate that a node is in the process of being deleted or
626 <     * created. For purposes of read-only access, a key may be read
627 <     * before a val, but can only be used after checking val to be
628 <     * non-null.
629 <     */
630 <    static class Node {
631 <        volatile int hash;
632 <        final Object key;
643 <        volatile Object val;
644 <        volatile Node next;
621 >     * Key-value entry.  This class is never exported out as a
622 >     * user-mutable Map.Entry (i.e., one supporting setValue; see
623 >     * MapEntry below), but can be used for read-only traversals used
624 >     * in bulk tasks.  Subclasses of Node with a negative hash field
625 >     * are special, and contain null keys and values (but are never
626 >     * exported).  Otherwise, keys and vals are never null.
627 >     */
628 >    static class Node<K,V> implements Map.Entry<K,V> {
629 >        final int hash;
630 >        final K key;
631 >        volatile V val;
632 >        volatile Node<K,V> next;
633  
634 <        Node(int hash, Object key, Object val, Node next) {
634 >        Node(int hash, K key, V val, Node<K,V> next) {
635              this.hash = hash;
636              this.key = key;
637              this.val = val;
638              this.next = next;
639          }
640  
641 <        /** CompareAndSet the hash field */
642 <        final boolean casHash(int cmp, int val) {
643 <            return UNSAFE.compareAndSwapInt(this, hashOffset, cmp, val);
644 <        }
645 <
646 <        /** The number of spins before blocking for a lock */
659 <        static final int MAX_SPINS =
660 <            Runtime.getRuntime().availableProcessors() > 1 ? 64 : 1;
661 <
662 <        /**
663 <         * Spins a while if LOCKED bit set and this node is the first
664 <         * of its bin, and then sets WAITING bits on hash field and
665 <         * blocks (once) if they are still set.  It is OK for this
666 <         * method to return even if lock is not available upon exit,
667 <         * which enables these simple single-wait mechanics.
668 <         *
669 <         * The corresponding signalling operation is performed within
670 <         * callers: Upon detecting that WAITING has been set when
671 <         * unlocking lock (via a failed CAS from non-waiting LOCKED
672 <         * state), unlockers acquire the sync lock and perform a
673 <         * notifyAll.
674 <         *
675 <         * The initial sanity check on tab and bounds is not currently
676 <         * necessary in the only usages of this method, but enables
677 <         * use in other future contexts.
678 <         */
679 <        final void tryAwaitLock(Node[] tab, int i) {
680 <            if (tab != null && i >= 0 && i < tab.length) { // sanity check
681 <                int r = ThreadLocalRandom.current().nextInt(); // randomize spins
682 <                int spins = MAX_SPINS, h;
683 <                while (tabAt(tab, i) == this && ((h = hash) & LOCKED) != 0) {
684 <                    if (spins >= 0) {
685 <                        r ^= r << 1; r ^= r >>> 3; r ^= r << 10; // xorshift
686 <                        if (r >= 0 && --spins == 0)
687 <                            Thread.yield();  // yield before block
688 <                    }
689 <                    else if (casHash(h, h | WAITING)) {
690 <                        synchronized (this) {
691 <                            if (tabAt(tab, i) == this &&
692 <                                (hash & WAITING) == WAITING) {
693 <                                try {
694 <                                    wait();
695 <                                } catch (InterruptedException ie) {
696 <                                    try {
697 <                                        Thread.currentThread().interrupt();
698 <                                    } catch (SecurityException ignore) {
699 <                                    }
700 <                                }
701 <                            }
702 <                            else
703 <                                notifyAll(); // possibly won race vs signaller
704 <                        }
705 <                        break;
706 <                    }
707 <                }
708 <            }
709 <        }
710 <
711 <        // Unsafe mechanics for casHash
712 <        private static final sun.misc.Unsafe UNSAFE;
713 <        private static final long hashOffset;
714 <
715 <        static {
716 <            try {
717 <                UNSAFE = getUnsafe();
718 <                Class<?> k = Node.class;
719 <                hashOffset = UNSAFE.objectFieldOffset
720 <                    (k.getDeclaredField("hash"));
721 <            } catch (Exception e) {
722 <                throw new Error(e);
723 <            }
724 <        }
725 <    }
726 <
727 <    /* ---------------- TreeBins -------------- */
728 <
729 <    /**
730 <     * Nodes for use in TreeBins
731 <     */
732 <    static final class TreeNode extends Node {
733 <        TreeNode parent;  // red-black tree links
734 <        TreeNode left;
735 <        TreeNode right;
736 <        TreeNode prev;    // needed to unlink next upon deletion
737 <        boolean red;
738 <
739 <        TreeNode(int hash, Object key, Object val, Node next, TreeNode parent) {
740 <            super(hash, key, val, next);
741 <            this.parent = parent;
742 <        }
743 <    }
744 <
745 <    /**
746 <     * A specialized form of red-black tree for use in bins
747 <     * whose size exceeds a threshold.
748 <     *
749 <     * TreeBins use a special form of comparison for search and
750 <     * related operations (which is the main reason we cannot use
751 <     * existing collections such as TreeMaps). TreeBins contain
752 <     * Comparable elements, but may contain others, as well as
753 <     * elements that are Comparable but not necessarily Comparable<T>
754 <     * for the same T, so we cannot invoke compareTo among them. To
755 <     * handle this, the tree is ordered primarily by hash value, then
756 <     * by getClass().getName() order, and then by Comparator order
757 <     * among elements of the same class.  On lookup at a node, if
758 <     * elements are not comparable or compare as 0, both left and
759 <     * right children may need to be searched in the case of tied hash
760 <     * values. (This corresponds to the full list search that would be
761 <     * necessary if all elements were non-Comparable and had tied
762 <     * hashes.)  The red-black balancing code is updated from
763 <     * pre-jdk-collections
764 <     * (http://gee.cs.oswego.edu/dl/classes/collections/RBCell.java)
765 <     * based in turn on Cormen, Leiserson, and Rivest "Introduction to
766 <     * Algorithms" (CLR).
767 <     *
768 <     * TreeBins also maintain a separate locking discipline than
769 <     * regular bins. Because they are forwarded via special MOVED
770 <     * nodes at bin heads (which can never change once established),
771 <     * we cannot use those nodes as locks. Instead, TreeBin
772 <     * extends AbstractQueuedSynchronizer to support a simple form of
773 <     * read-write lock. For update operations and table validation,
774 <     * the exclusive form of lock behaves in the same way as bin-head
775 <     * locks. However, lookups use shared read-lock mechanics to allow
776 <     * multiple readers in the absence of writers.  Additionally,
777 <     * these lookups do not ever block: While the lock is not
778 <     * available, they proceed along the slow traversal path (via
779 <     * next-pointers) until the lock becomes available or the list is
780 <     * exhausted, whichever comes first. (These cases are not fast,
781 <     * but maximize aggregate expected throughput.)  The AQS mechanics
782 <     * for doing this are straightforward.  The lock state is held as
783 <     * AQS getState().  Read counts are negative; the write count (1)
784 <     * is positive.  There are no signalling preferences among readers
785 <     * and writers. Since we don't need to export full Lock API, we
786 <     * just override the minimal AQS methods and use them directly.
787 <     */
788 <    static final class TreeBin extends AbstractQueuedSynchronizer {
789 <        private static final long serialVersionUID = 2249069246763182397L;
790 <        transient TreeNode root;  // root of tree
791 <        transient TreeNode first; // head of next-pointer list
792 <
793 <        /* AQS overrides */
794 <        public final boolean isHeldExclusively() { return getState() > 0; }
795 <        public final boolean tryAcquire(int ignore) {
796 <            if (compareAndSetState(0, 1)) {
797 <                setExclusiveOwnerThread(Thread.currentThread());
798 <                return true;
799 <            }
800 <            return false;
801 <        }
802 <        public final boolean tryRelease(int ignore) {
803 <            setExclusiveOwnerThread(null);
804 <            setState(0);
805 <            return true;
806 <        }
807 <        public final int tryAcquireShared(int ignore) {
808 <            for (int c;;) {
809 <                if ((c = getState()) > 0)
810 <                    return -1;
811 <                if (compareAndSetState(c, c -1))
812 <                    return 1;
813 <            }
814 <        }
815 <        public final boolean tryReleaseShared(int ignore) {
816 <            int c;
817 <            do {} while (!compareAndSetState(c = getState(), c + 1));
818 <            return c == -1;
819 <        }
820 <
821 <        /** From CLR */
822 <        private void rotateLeft(TreeNode p) {
823 <            if (p != null) {
824 <                TreeNode r = p.right, pp, rl;
825 <                if ((rl = p.right = r.left) != null)
826 <                    rl.parent = p;
827 <                if ((pp = r.parent = p.parent) == null)
828 <                    root = r;
829 <                else if (pp.left == p)
830 <                    pp.left = r;
831 <                else
832 <                    pp.right = r;
833 <                r.left = p;
834 <                p.parent = r;
835 <            }
836 <        }
837 <
838 <        /** From CLR */
839 <        private void rotateRight(TreeNode p) {
840 <            if (p != null) {
841 <                TreeNode l = p.left, pp, lr;
842 <                if ((lr = p.left = l.right) != null)
843 <                    lr.parent = p;
844 <                if ((pp = l.parent = p.parent) == null)
845 <                    root = l;
846 <                else if (pp.right == p)
847 <                    pp.right = l;
848 <                else
849 <                    pp.left = l;
850 <                l.right = p;
851 <                p.parent = l;
852 <            }
853 <        }
854 <
855 <        /**
856 <         * Returns the TreeNode (or null if not found) for the given key
857 <         * starting at given root.
858 <         */
859 <        @SuppressWarnings("unchecked") final TreeNode getTreeNode
860 <            (int h, Object k, TreeNode p) {
861 <            Class<?> c = k.getClass();
862 <            while (p != null) {
863 <                int dir, ph;  Object pk; Class<?> pc;
864 <                if ((ph = p.hash) == h) {
865 <                    if ((pk = p.key) == k || k.equals(pk))
866 <                        return p;
867 <                    if (c != (pc = pk.getClass()) ||
868 <                        !(k instanceof Comparable) ||
869 <                        (dir = ((Comparable)k).compareTo((Comparable)pk)) == 0) {
870 <                        if ((dir = (c == pc) ? 0 :
871 <                             c.getName().compareTo(pc.getName())) == 0) {
872 <                            TreeNode r = null, pl, pr; // check both sides
873 <                            if ((pr = p.right) != null && h >= pr.hash &&
874 <                                (r = getTreeNode(h, k, pr)) != null)
875 <                                return r;
876 <                            else if ((pl = p.left) != null && h <= pl.hash)
877 <                                dir = -1;
878 <                            else // nothing there
879 <                                return null;
880 <                        }
881 <                    }
882 <                }
883 <                else
884 <                    dir = (h < ph) ? -1 : 1;
885 <                p = (dir > 0) ? p.right : p.left;
886 <            }
887 <            return null;
641 >        public final K getKey()       { return key; }
642 >        public final V getValue()     { return val; }
643 >        public final int hashCode()   { return key.hashCode() ^ val.hashCode(); }
644 >        public final String toString(){ return key + "=" + val; }
645 >        public final V setValue(V value) {
646 >            throw new UnsupportedOperationException();
647          }
648  
649 <        /**
650 <         * Wrapper for getTreeNode used by CHM.get. Tries to obtain
651 <         * read-lock to call getTreeNode, but during failure to get
652 <         * lock, searches along next links.
653 <         */
654 <        final Object getValue(int h, Object k) {
655 <            Node r = null;
897 <            int c = getState(); // Must read lock state first
898 <            for (Node e = first; e != null; e = e.next) {
899 <                if (c <= 0 && compareAndSetState(c, c - 1)) {
900 <                    try {
901 <                        r = getTreeNode(h, k, root);
902 <                    } finally {
903 <                        releaseShared(0);
904 <                    }
905 <                    break;
906 <                }
907 <                else if ((e.hash & HASH_BITS) == h && k.equals(e.key)) {
908 <                    r = e;
909 <                    break;
910 <                }
911 <                else
912 <                    c = getState();
913 <            }
914 <            return r == null ? null : r.val;
649 >        public final boolean equals(Object o) {
650 >            Object k, v, u; Map.Entry<?,?> e;
651 >            return ((o instanceof Map.Entry) &&
652 >                    (k = (e = (Map.Entry<?,?>)o).getKey()) != null &&
653 >                    (v = e.getValue()) != null &&
654 >                    (k == key || k.equals(key)) &&
655 >                    (v == (u = val) || v.equals(u)));
656          }
657  
658          /**
659 <         * Finds or adds a node.
919 <         * @return null if added
659 >         * Virtualized support for map.get(); overridden in subclasses.
660           */
661 <        @SuppressWarnings("unchecked") final TreeNode putTreeNode
662 <            (int h, Object k, Object v) {
663 <            Class<?> c = k.getClass();
664 <            TreeNode pp = root, p = null;
665 <            int dir = 0;
666 <            while (pp != null) { // find existing node or leaf to insert at
667 <                int ph;  Object pk; Class<?> pc;
668 <                p = pp;
669 <                if ((ph = p.hash) == h) {
930 <                    if ((pk = p.key) == k || k.equals(pk))
931 <                        return p;
932 <                    if (c != (pc = pk.getClass()) ||
933 <                        !(k instanceof Comparable) ||
934 <                        (dir = ((Comparable)k).compareTo((Comparable)pk)) == 0) {
935 <                        TreeNode s = null, r = null, pr;
936 <                        if ((dir = (c == pc) ? 0 :
937 <                             c.getName().compareTo(pc.getName())) == 0) {
938 <                            if ((pr = p.right) != null && h >= pr.hash &&
939 <                                (r = getTreeNode(h, k, pr)) != null)
940 <                                return r;
941 <                            else // continue left
942 <                                dir = -1;
943 <                        }
944 <                        else if ((pr = p.right) != null && h >= pr.hash)
945 <                            s = pr;
946 <                        if (s != null && (r = getTreeNode(h, k, s)) != null)
947 <                            return r;
948 <                    }
949 <                }
950 <                else
951 <                    dir = (h < ph) ? -1 : 1;
952 <                pp = (dir > 0) ? p.right : p.left;
953 <            }
954 <
955 <            TreeNode f = first;
956 <            TreeNode x = first = new TreeNode(h, k, v, f, p);
957 <            if (p == null)
958 <                root = x;
959 <            else { // attach and rebalance; adapted from CLR
960 <                TreeNode xp, xpp;
961 <                if (f != null)
962 <                    f.prev = x;
963 <                if (dir <= 0)
964 <                    p.left = x;
965 <                else
966 <                    p.right = x;
967 <                x.red = true;
968 <                while (x != null && (xp = x.parent) != null && xp.red &&
969 <                       (xpp = xp.parent) != null) {
970 <                    TreeNode xppl = xpp.left;
971 <                    if (xp == xppl) {
972 <                        TreeNode y = xpp.right;
973 <                        if (y != null && y.red) {
974 <                            y.red = false;
975 <                            xp.red = false;
976 <                            xpp.red = true;
977 <                            x = xpp;
978 <                        }
979 <                        else {
980 <                            if (x == xp.right) {
981 <                                rotateLeft(x = xp);
982 <                                xpp = (xp = x.parent) == null ? null : xp.parent;
983 <                            }
984 <                            if (xp != null) {
985 <                                xp.red = false;
986 <                                if (xpp != null) {
987 <                                    xpp.red = true;
988 <                                    rotateRight(xpp);
989 <                                }
990 <                            }
991 <                        }
992 <                    }
993 <                    else {
994 <                        TreeNode y = xppl;
995 <                        if (y != null && y.red) {
996 <                            y.red = false;
997 <                            xp.red = false;
998 <                            xpp.red = true;
999 <                            x = xpp;
1000 <                        }
1001 <                        else {
1002 <                            if (x == xp.left) {
1003 <                                rotateRight(x = xp);
1004 <                                xpp = (xp = x.parent) == null ? null : xp.parent;
1005 <                            }
1006 <                            if (xp != null) {
1007 <                                xp.red = false;
1008 <                                if (xpp != null) {
1009 <                                    xpp.red = true;
1010 <                                    rotateLeft(xpp);
1011 <                                }
1012 <                            }
1013 <                        }
1014 <                    }
1015 <                }
1016 <                TreeNode r = root;
1017 <                if (r != null && r.red)
1018 <                    r.red = false;
661 >        Node<K,V> find(int h, Object k) {
662 >            Node<K,V> e = this;
663 >            if (k != null) {
664 >                do {
665 >                    K ek;
666 >                    if (e.hash == h &&
667 >                        ((ek = e.key) == k || (ek != null && k.equals(ek))))
668 >                        return e;
669 >                } while ((e = e.next) != null);
670              }
671              return null;
672          }
1022
1023        /**
1024         * Removes the given node, that must be present before this
1025         * call.  This is messier than typical red-black deletion code
1026         * because we cannot swap the contents of an interior node
1027         * with a leaf successor that is pinned by "next" pointers
1028         * that are accessible independently of lock. So instead we
1029         * swap the tree linkages.
1030         */
1031        final void deleteTreeNode(TreeNode p) {
1032            TreeNode next = (TreeNode)p.next; // unlink traversal pointers
1033            TreeNode pred = p.prev;
1034            if (pred == null)
1035                first = next;
1036            else
1037                pred.next = next;
1038            if (next != null)
1039                next.prev = pred;
1040            TreeNode replacement;
1041            TreeNode pl = p.left;
1042            TreeNode pr = p.right;
1043            if (pl != null && pr != null) {
1044                TreeNode s = pr, sl;
1045                while ((sl = s.left) != null) // find successor
1046                    s = sl;
1047                boolean c = s.red; s.red = p.red; p.red = c; // swap colors
1048                TreeNode sr = s.right;
1049                TreeNode pp = p.parent;
1050                if (s == pr) { // p was s's direct parent
1051                    p.parent = s;
1052                    s.right = p;
1053                }
1054                else {
1055                    TreeNode sp = s.parent;
1056                    if ((p.parent = sp) != null) {
1057                        if (s == sp.left)
1058                            sp.left = p;
1059                        else
1060                            sp.right = p;
1061                    }
1062                    if ((s.right = pr) != null)
1063                        pr.parent = s;
1064                }
1065                p.left = null;
1066                if ((p.right = sr) != null)
1067                    sr.parent = p;
1068                if ((s.left = pl) != null)
1069                    pl.parent = s;
1070                if ((s.parent = pp) == null)
1071                    root = s;
1072                else if (p == pp.left)
1073                    pp.left = s;
1074                else
1075                    pp.right = s;
1076                replacement = sr;
1077            }
1078            else
1079                replacement = (pl != null) ? pl : pr;
1080            TreeNode pp = p.parent;
1081            if (replacement == null) {
1082                if (pp == null) {
1083                    root = null;
1084                    return;
1085                }
1086                replacement = p;
1087            }
1088            else {
1089                replacement.parent = pp;
1090                if (pp == null)
1091                    root = replacement;
1092                else if (p == pp.left)
1093                    pp.left = replacement;
1094                else
1095                    pp.right = replacement;
1096                p.left = p.right = p.parent = null;
1097            }
1098            if (!p.red) { // rebalance, from CLR
1099                TreeNode x = replacement;
1100                while (x != null) {
1101                    TreeNode xp, xpl;
1102                    if (x.red || (xp = x.parent) == null) {
1103                        x.red = false;
1104                        break;
1105                    }
1106                    if (x == (xpl = xp.left)) {
1107                        TreeNode sib = xp.right;
1108                        if (sib != null && sib.red) {
1109                            sib.red = false;
1110                            xp.red = true;
1111                            rotateLeft(xp);
1112                            sib = (xp = x.parent) == null ? null : xp.right;
1113                        }
1114                        if (sib == null)
1115                            x = xp;
1116                        else {
1117                            TreeNode sl = sib.left, sr = sib.right;
1118                            if ((sr == null || !sr.red) &&
1119                                (sl == null || !sl.red)) {
1120                                sib.red = true;
1121                                x = xp;
1122                            }
1123                            else {
1124                                if (sr == null || !sr.red) {
1125                                    if (sl != null)
1126                                        sl.red = false;
1127                                    sib.red = true;
1128                                    rotateRight(sib);
1129                                    sib = (xp = x.parent) == null ? null : xp.right;
1130                                }
1131                                if (sib != null) {
1132                                    sib.red = (xp == null) ? false : xp.red;
1133                                    if ((sr = sib.right) != null)
1134                                        sr.red = false;
1135                                }
1136                                if (xp != null) {
1137                                    xp.red = false;
1138                                    rotateLeft(xp);
1139                                }
1140                                x = root;
1141                            }
1142                        }
1143                    }
1144                    else { // symmetric
1145                        TreeNode sib = xpl;
1146                        if (sib != null && sib.red) {
1147                            sib.red = false;
1148                            xp.red = true;
1149                            rotateRight(xp);
1150                            sib = (xp = x.parent) == null ? null : xp.left;
1151                        }
1152                        if (sib == null)
1153                            x = xp;
1154                        else {
1155                            TreeNode sl = sib.left, sr = sib.right;
1156                            if ((sl == null || !sl.red) &&
1157                                (sr == null || !sr.red)) {
1158                                sib.red = true;
1159                                x = xp;
1160                            }
1161                            else {
1162                                if (sl == null || !sl.red) {
1163                                    if (sr != null)
1164                                        sr.red = false;
1165                                    sib.red = true;
1166                                    rotateLeft(sib);
1167                                    sib = (xp = x.parent) == null ? null : xp.left;
1168                                }
1169                                if (sib != null) {
1170                                    sib.red = (xp == null) ? false : xp.red;
1171                                    if ((sl = sib.left) != null)
1172                                        sl.red = false;
1173                                }
1174                                if (xp != null) {
1175                                    xp.red = false;
1176                                    rotateRight(xp);
1177                                }
1178                                x = root;
1179                            }
1180                        }
1181                    }
1182                }
1183            }
1184            if (p == replacement && (pp = p.parent) != null) {
1185                if (p == pp.left) // detach pointers
1186                    pp.left = null;
1187                else if (p == pp.right)
1188                    pp.right = null;
1189                p.parent = null;
1190            }
1191        }
673      }
674  
675 <    /* ---------------- Collision reduction methods -------------- */
675 >    /* ---------------- Static utilities -------------- */
676  
677      /**
678 <     * Spreads higher bits to lower, and also forces top 2 bits to 0.
679 <     * Because the table uses power-of-two masking, sets of hashes
680 <     * that vary only in bits above the current mask will always
681 <     * collide. (Among known examples are sets of Float keys holding
682 <     * consecutive whole numbers in small tables.)  To counter this,
683 <     * we apply a transform that spreads the impact of higher bits
678 >     * Spreads (XORs) higher bits of hash to lower and also forces top
679 >     * bit to 0. Because the table uses power-of-two masking, sets of
680 >     * hashes that vary only in bits above the current mask will
681 >     * always collide. (Among known examples are sets of Float keys
682 >     * holding consecutive whole numbers in small tables.)  So we
683 >     * apply a transform that spreads the impact of higher bits
684       * downward. There is a tradeoff between speed, utility, and
685       * quality of bit-spreading. Because many common sets of hashes
686 <     * are already reasonably distributed across bits (so don't benefit
687 <     * from spreading), and because we use trees to handle large sets
688 <     * of collisions in bins, we don't need excessively high quality.
689 <     */
690 <    private static final int spread(int h) {
691 <        h ^= (h >>> 18) ^ (h >>> 12);
1211 <        return (h ^ (h >>> 10)) & HASH_BITS;
1212 <    }
1213 <
1214 <    /**
1215 <     * Replaces a list bin with a tree bin. Call only when locked.
1216 <     * Fails to replace if the given key is non-comparable or table
1217 <     * is, or needs, resizing.
686 >     * are already reasonably distributed (so don't benefit from
687 >     * spreading), and because we use trees to handle large sets of
688 >     * collisions in bins, we just XOR some shifted bits in the
689 >     * cheapest possible way to reduce systematic lossage, as well as
690 >     * to incorporate impact of the highest bits that would otherwise
691 >     * never be used in index calculations because of table bounds.
692       */
693 <    private final void replaceWithTreeBin(Node[] tab, int index, Object key) {
694 <        if ((key instanceof Comparable) &&
1221 <            (tab.length >= MAXIMUM_CAPACITY || counter.sum() < (long)sizeCtl)) {
1222 <            TreeBin t = new TreeBin();
1223 <            for (Node e = tabAt(tab, index); e != null; e = e.next)
1224 <                t.putTreeNode(e.hash & HASH_BITS, e.key, e.val);
1225 <            setTabAt(tab, index, new Node(MOVED, t, null, null));
1226 <        }
1227 <    }
1228 <
1229 <    /* ---------------- Internal access and update methods -------------- */
1230 <
1231 <    /** Implementation for get and containsKey */
1232 <    private final Object internalGet(Object k) {
1233 <        int h = spread(k.hashCode());
1234 <        retry: for (Node[] tab = table; tab != null;) {
1235 <            Node e, p; Object ek, ev; int eh;      // locals to read fields once
1236 <            for (e = tabAt(tab, (tab.length - 1) & h); e != null; e = e.next) {
1237 <                if ((eh = e.hash) == MOVED) {
1238 <                    if ((ek = e.key) instanceof TreeBin)  // search TreeBin
1239 <                        return ((TreeBin)ek).getValue(h, k);
1240 <                    else {                        // restart with new table
1241 <                        tab = (Node[])ek;
1242 <                        continue retry;
1243 <                    }
1244 <                }
1245 <                else if ((eh & HASH_BITS) == h && (ev = e.val) != null &&
1246 <                         ((ek = e.key) == k || k.equals(ek)))
1247 <                    return ev;
1248 <            }
1249 <            break;
1250 <        }
1251 <        return null;
693 >    static final int spread(int h) {
694 >        return (h ^ (h >>> 16)) & HASH_BITS;
695      }
696  
697      /**
1255     * Implementation for the four public remove/replace methods:
1256     * Replaces node value with v, conditional upon match of cv if
1257     * non-null.  If resulting value is null, delete.
1258     */
1259    private final Object internalReplace(Object k, Object v, Object cv) {
1260        int h = spread(k.hashCode());
1261        Object oldVal = null;
1262        for (Node[] tab = table;;) {
1263            Node f; int i, fh; Object fk;
1264            if (tab == null ||
1265                (f = tabAt(tab, i = (tab.length - 1) & h)) == null)
1266                break;
1267            else if ((fh = f.hash) == MOVED) {
1268                if ((fk = f.key) instanceof TreeBin) {
1269                    TreeBin t = (TreeBin)fk;
1270                    boolean validated = false;
1271                    boolean deleted = false;
1272                    t.acquire(0);
1273                    try {
1274                        if (tabAt(tab, i) == f) {
1275                            validated = true;
1276                            TreeNode p = t.getTreeNode(h, k, t.root);
1277                            if (p != null) {
1278                                Object pv = p.val;
1279                                if (cv == null || cv == pv || cv.equals(pv)) {
1280                                    oldVal = pv;
1281                                    if ((p.val = v) == null) {
1282                                        deleted = true;
1283                                        t.deleteTreeNode(p);
1284                                    }
1285                                }
1286                            }
1287                        }
1288                    } finally {
1289                        t.release(0);
1290                    }
1291                    if (validated) {
1292                        if (deleted)
1293                            counter.add(-1L);
1294                        break;
1295                    }
1296                }
1297                else
1298                    tab = (Node[])fk;
1299            }
1300            else if ((fh & HASH_BITS) != h && f.next == null) // precheck
1301                break;                          // rules out possible existence
1302            else if ((fh & LOCKED) != 0) {
1303                checkForResize();               // try resizing if can't get lock
1304                f.tryAwaitLock(tab, i);
1305            }
1306            else if (f.casHash(fh, fh | LOCKED)) {
1307                boolean validated = false;
1308                boolean deleted = false;
1309                try {
1310                    if (tabAt(tab, i) == f) {
1311                        validated = true;
1312                        for (Node e = f, pred = null;;) {
1313                            Object ek, ev;
1314                            if ((e.hash & HASH_BITS) == h &&
1315                                ((ev = e.val) != null) &&
1316                                ((ek = e.key) == k || k.equals(ek))) {
1317                                if (cv == null || cv == ev || cv.equals(ev)) {
1318                                    oldVal = ev;
1319                                    if ((e.val = v) == null) {
1320                                        deleted = true;
1321                                        Node en = e.next;
1322                                        if (pred != null)
1323                                            pred.next = en;
1324                                        else
1325                                            setTabAt(tab, i, en);
1326                                    }
1327                                }
1328                                break;
1329                            }
1330                            pred = e;
1331                            if ((e = e.next) == null)
1332                                break;
1333                        }
1334                    }
1335                } finally {
1336                    if (!f.casHash(fh | LOCKED, fh)) {
1337                        f.hash = fh;
1338                        synchronized (f) { f.notifyAll(); };
1339                    }
1340                }
1341                if (validated) {
1342                    if (deleted)
1343                        counter.add(-1L);
1344                    break;
1345                }
1346            }
1347        }
1348        return oldVal;
1349    }
1350
1351    /*
1352     * Internal versions of the six insertion methods, each a
1353     * little more complicated than the last. All have
1354     * the same basic structure as the first (internalPut):
1355     *  1. If table uninitialized, create
1356     *  2. If bin empty, try to CAS new node
1357     *  3. If bin stale, use new table
1358     *  4. if bin converted to TreeBin, validate and relay to TreeBin methods
1359     *  5. Lock and validate; if valid, scan and add or update
1360     *
1361     * The others interweave other checks and/or alternative actions:
1362     *  * Plain put checks for and performs resize after insertion.
1363     *  * putIfAbsent prescans for mapping without lock (and fails to add
1364     *    if present), which also makes pre-emptive resize checks worthwhile.
1365     *  * computeIfAbsent extends form used in putIfAbsent with additional
1366     *    mechanics to deal with, calls, potential exceptions and null
1367     *    returns from function call.
1368     *  * compute uses the same function-call mechanics, but without
1369     *    the prescans
1370     *  * merge acts as putIfAbsent in the absent case, but invokes the
1371     *    update function if present
1372     *  * putAll attempts to pre-allocate enough table space
1373     *    and more lazily performs count updates and checks.
1374     *
1375     * Someday when details settle down a bit more, it might be worth
1376     * some factoring to reduce sprawl.
1377     */
1378
1379    /** Implementation for put */
1380    private final Object internalPut(Object k, Object v) {
1381        int h = spread(k.hashCode());
1382        int count = 0;
1383        for (Node[] tab = table;;) {
1384            int i; Node f; int fh; Object fk;
1385            if (tab == null)
1386                tab = initTable();
1387            else if ((f = tabAt(tab, i = (tab.length - 1) & h)) == null) {
1388                if (casTabAt(tab, i, null, new Node(h, k, v, null)))
1389                    break;                   // no lock when adding to empty bin
1390            }
1391            else if ((fh = f.hash) == MOVED) {
1392                if ((fk = f.key) instanceof TreeBin) {
1393                    TreeBin t = (TreeBin)fk;
1394                    Object oldVal = null;
1395                    t.acquire(0);
1396                    try {
1397                        if (tabAt(tab, i) == f) {
1398                            count = 2;
1399                            TreeNode p = t.putTreeNode(h, k, v);
1400                            if (p != null) {
1401                                oldVal = p.val;
1402                                p.val = v;
1403                            }
1404                        }
1405                    } finally {
1406                        t.release(0);
1407                    }
1408                    if (count != 0) {
1409                        if (oldVal != null)
1410                            return oldVal;
1411                        break;
1412                    }
1413                }
1414                else
1415                    tab = (Node[])fk;
1416            }
1417            else if ((fh & LOCKED) != 0) {
1418                checkForResize();
1419                f.tryAwaitLock(tab, i);
1420            }
1421            else if (f.casHash(fh, fh | LOCKED)) {
1422                Object oldVal = null;
1423                try {                        // needed in case equals() throws
1424                    if (tabAt(tab, i) == f) {
1425                        count = 1;
1426                        for (Node e = f;; ++count) {
1427                            Object ek, ev;
1428                            if ((e.hash & HASH_BITS) == h &&
1429                                (ev = e.val) != null &&
1430                                ((ek = e.key) == k || k.equals(ek))) {
1431                                oldVal = ev;
1432                                e.val = v;
1433                                break;
1434                            }
1435                            Node last = e;
1436                            if ((e = e.next) == null) {
1437                                last.next = new Node(h, k, v, null);
1438                                if (count >= TREE_THRESHOLD)
1439                                    replaceWithTreeBin(tab, i, k);
1440                                break;
1441                            }
1442                        }
1443                    }
1444                } finally {                  // unlock and signal if needed
1445                    if (!f.casHash(fh | LOCKED, fh)) {
1446                        f.hash = fh;
1447                        synchronized (f) { f.notifyAll(); };
1448                    }
1449                }
1450                if (count != 0) {
1451                    if (oldVal != null)
1452                        return oldVal;
1453                    if (tab.length <= 64)
1454                        count = 2;
1455                    break;
1456                }
1457            }
1458        }
1459        counter.add(1L);
1460        if (count > 1)
1461            checkForResize();
1462        return null;
1463    }
1464
1465    /** Implementation for putIfAbsent */
1466    private final Object internalPutIfAbsent(Object k, Object v) {
1467        int h = spread(k.hashCode());
1468        int count = 0;
1469        for (Node[] tab = table;;) {
1470            int i; Node f; int fh; Object fk, fv;
1471            if (tab == null)
1472                tab = initTable();
1473            else if ((f = tabAt(tab, i = (tab.length - 1) & h)) == null) {
1474                if (casTabAt(tab, i, null, new Node(h, k, v, null)))
1475                    break;
1476            }
1477            else if ((fh = f.hash) == MOVED) {
1478                if ((fk = f.key) instanceof TreeBin) {
1479                    TreeBin t = (TreeBin)fk;
1480                    Object oldVal = null;
1481                    t.acquire(0);
1482                    try {
1483                        if (tabAt(tab, i) == f) {
1484                            count = 2;
1485                            TreeNode p = t.putTreeNode(h, k, v);
1486                            if (p != null)
1487                                oldVal = p.val;
1488                        }
1489                    } finally {
1490                        t.release(0);
1491                    }
1492                    if (count != 0) {
1493                        if (oldVal != null)
1494                            return oldVal;
1495                        break;
1496                    }
1497                }
1498                else
1499                    tab = (Node[])fk;
1500            }
1501            else if ((fh & HASH_BITS) == h && (fv = f.val) != null &&
1502                     ((fk = f.key) == k || k.equals(fk)))
1503                return fv;
1504            else {
1505                Node g = f.next;
1506                if (g != null) { // at least 2 nodes -- search and maybe resize
1507                    for (Node e = g;;) {
1508                        Object ek, ev;
1509                        if ((e.hash & HASH_BITS) == h && (ev = e.val) != null &&
1510                            ((ek = e.key) == k || k.equals(ek)))
1511                            return ev;
1512                        if ((e = e.next) == null) {
1513                            checkForResize();
1514                            break;
1515                        }
1516                    }
1517                }
1518                if (((fh = f.hash) & LOCKED) != 0) {
1519                    checkForResize();
1520                    f.tryAwaitLock(tab, i);
1521                }
1522                else if (tabAt(tab, i) == f && f.casHash(fh, fh | LOCKED)) {
1523                    Object oldVal = null;
1524                    try {
1525                        if (tabAt(tab, i) == f) {
1526                            count = 1;
1527                            for (Node e = f;; ++count) {
1528                                Object ek, ev;
1529                                if ((e.hash & HASH_BITS) == h &&
1530                                    (ev = e.val) != null &&
1531                                    ((ek = e.key) == k || k.equals(ek))) {
1532                                    oldVal = ev;
1533                                    break;
1534                                }
1535                                Node last = e;
1536                                if ((e = e.next) == null) {
1537                                    last.next = new Node(h, k, v, null);
1538                                    if (count >= TREE_THRESHOLD)
1539                                        replaceWithTreeBin(tab, i, k);
1540                                    break;
1541                                }
1542                            }
1543                        }
1544                    } finally {
1545                        if (!f.casHash(fh | LOCKED, fh)) {
1546                            f.hash = fh;
1547                            synchronized (f) { f.notifyAll(); };
1548                        }
1549                    }
1550                    if (count != 0) {
1551                        if (oldVal != null)
1552                            return oldVal;
1553                        if (tab.length <= 64)
1554                            count = 2;
1555                        break;
1556                    }
1557                }
1558            }
1559        }
1560        counter.add(1L);
1561        if (count > 1)
1562            checkForResize();
1563        return null;
1564    }
1565
1566    /** Implementation for computeIfAbsent */
1567    private final Object internalComputeIfAbsent(K k,
1568                                                 Fun<? super K, ?> mf) {
1569        int h = spread(k.hashCode());
1570        Object val = null;
1571        int count = 0;
1572        for (Node[] tab = table;;) {
1573            Node f; int i, fh; Object fk, fv;
1574            if (tab == null)
1575                tab = initTable();
1576            else if ((f = tabAt(tab, i = (tab.length - 1) & h)) == null) {
1577                Node node = new Node(fh = h | LOCKED, k, null, null);
1578                if (casTabAt(tab, i, null, node)) {
1579                    count = 1;
1580                    try {
1581                        if ((val = mf.apply(k)) != null)
1582                            node.val = val;
1583                    } finally {
1584                        if (val == null)
1585                            setTabAt(tab, i, null);
1586                        if (!node.casHash(fh, h)) {
1587                            node.hash = h;
1588                            synchronized (node) { node.notifyAll(); };
1589                        }
1590                    }
1591                }
1592                if (count != 0)
1593                    break;
1594            }
1595            else if ((fh = f.hash) == MOVED) {
1596                if ((fk = f.key) instanceof TreeBin) {
1597                    TreeBin t = (TreeBin)fk;
1598                    boolean added = false;
1599                    t.acquire(0);
1600                    try {
1601                        if (tabAt(tab, i) == f) {
1602                            count = 1;
1603                            TreeNode p = t.getTreeNode(h, k, t.root);
1604                            if (p != null)
1605                                val = p.val;
1606                            else if ((val = mf.apply(k)) != null) {
1607                                added = true;
1608                                count = 2;
1609                                t.putTreeNode(h, k, val);
1610                            }
1611                        }
1612                    } finally {
1613                        t.release(0);
1614                    }
1615                    if (count != 0) {
1616                        if (!added)
1617                            return val;
1618                        break;
1619                    }
1620                }
1621                else
1622                    tab = (Node[])fk;
1623            }
1624            else if ((fh & HASH_BITS) == h && (fv = f.val) != null &&
1625                     ((fk = f.key) == k || k.equals(fk)))
1626                return fv;
1627            else {
1628                Node g = f.next;
1629                if (g != null) {
1630                    for (Node e = g;;) {
1631                        Object ek, ev;
1632                        if ((e.hash & HASH_BITS) == h && (ev = e.val) != null &&
1633                            ((ek = e.key) == k || k.equals(ek)))
1634                            return ev;
1635                        if ((e = e.next) == null) {
1636                            checkForResize();
1637                            break;
1638                        }
1639                    }
1640                }
1641                if (((fh = f.hash) & LOCKED) != 0) {
1642                    checkForResize();
1643                    f.tryAwaitLock(tab, i);
1644                }
1645                else if (tabAt(tab, i) == f && f.casHash(fh, fh | LOCKED)) {
1646                    boolean added = false;
1647                    try {
1648                        if (tabAt(tab, i) == f) {
1649                            count = 1;
1650                            for (Node e = f;; ++count) {
1651                                Object ek, ev;
1652                                if ((e.hash & HASH_BITS) == h &&
1653                                    (ev = e.val) != null &&
1654                                    ((ek = e.key) == k || k.equals(ek))) {
1655                                    val = ev;
1656                                    break;
1657                                }
1658                                Node last = e;
1659                                if ((e = e.next) == null) {
1660                                    if ((val = mf.apply(k)) != null) {
1661                                        added = true;
1662                                        last.next = new Node(h, k, val, null);
1663                                        if (count >= TREE_THRESHOLD)
1664                                            replaceWithTreeBin(tab, i, k);
1665                                    }
1666                                    break;
1667                                }
1668                            }
1669                        }
1670                    } finally {
1671                        if (!f.casHash(fh | LOCKED, fh)) {
1672                            f.hash = fh;
1673                            synchronized (f) { f.notifyAll(); };
1674                        }
1675                    }
1676                    if (count != 0) {
1677                        if (!added)
1678                            return val;
1679                        if (tab.length <= 64)
1680                            count = 2;
1681                        break;
1682                    }
1683                }
1684            }
1685        }
1686        if (val != null) {
1687            counter.add(1L);
1688            if (count > 1)
1689                checkForResize();
1690        }
1691        return val;
1692    }
1693
1694    /** Implementation for compute */
1695    @SuppressWarnings("unchecked") private final Object internalCompute
1696        (K k, boolean onlyIfPresent, BiFun<? super K, ? super V, ? extends V> mf) {
1697        int h = spread(k.hashCode());
1698        Object val = null;
1699        int delta = 0;
1700        int count = 0;
1701        for (Node[] tab = table;;) {
1702            Node f; int i, fh; Object fk;
1703            if (tab == null)
1704                tab = initTable();
1705            else if ((f = tabAt(tab, i = (tab.length - 1) & h)) == null) {
1706                if (onlyIfPresent)
1707                    break;
1708                Node node = new Node(fh = h | LOCKED, k, null, null);
1709                if (casTabAt(tab, i, null, node)) {
1710                    try {
1711                        count = 1;
1712                        if ((val = mf.apply(k, null)) != null) {
1713                            node.val = val;
1714                            delta = 1;
1715                        }
1716                    } finally {
1717                        if (delta == 0)
1718                            setTabAt(tab, i, null);
1719                        if (!node.casHash(fh, h)) {
1720                            node.hash = h;
1721                            synchronized (node) { node.notifyAll(); };
1722                        }
1723                    }
1724                }
1725                if (count != 0)
1726                    break;
1727            }
1728            else if ((fh = f.hash) == MOVED) {
1729                if ((fk = f.key) instanceof TreeBin) {
1730                    TreeBin t = (TreeBin)fk;
1731                    t.acquire(0);
1732                    try {
1733                        if (tabAt(tab, i) == f) {
1734                            count = 1;
1735                            TreeNode p = t.getTreeNode(h, k, t.root);
1736                            Object pv = (p == null) ? null : p.val;
1737                            if ((val = mf.apply(k, (V)pv)) != null) {
1738                                if (p != null)
1739                                    p.val = val;
1740                                else {
1741                                    count = 2;
1742                                    delta = 1;
1743                                    t.putTreeNode(h, k, val);
1744                                }
1745                            }
1746                            else if (p != null) {
1747                                delta = -1;
1748                                t.deleteTreeNode(p);
1749                            }
1750                        }
1751                    } finally {
1752                        t.release(0);
1753                    }
1754                    if (count != 0)
1755                        break;
1756                }
1757                else
1758                    tab = (Node[])fk;
1759            }
1760            else if ((fh & LOCKED) != 0) {
1761                checkForResize();
1762                f.tryAwaitLock(tab, i);
1763            }
1764            else if (f.casHash(fh, fh | LOCKED)) {
1765                try {
1766                    if (tabAt(tab, i) == f) {
1767                        count = 1;
1768                        for (Node e = f, pred = null;; ++count) {
1769                            Object ek, ev;
1770                            if ((e.hash & HASH_BITS) == h &&
1771                                (ev = e.val) != null &&
1772                                ((ek = e.key) == k || k.equals(ek))) {
1773                                val = mf.apply(k, (V)ev);
1774                                if (val != null)
1775                                    e.val = val;
1776                                else {
1777                                    delta = -1;
1778                                    Node en = e.next;
1779                                    if (pred != null)
1780                                        pred.next = en;
1781                                    else
1782                                        setTabAt(tab, i, en);
1783                                }
1784                                break;
1785                            }
1786                            pred = e;
1787                            if ((e = e.next) == null) {
1788                                if (!onlyIfPresent && (val = mf.apply(k, null)) != null) {
1789                                    pred.next = new Node(h, k, val, null);
1790                                    delta = 1;
1791                                    if (count >= TREE_THRESHOLD)
1792                                        replaceWithTreeBin(tab, i, k);
1793                                }
1794                                break;
1795                            }
1796                        }
1797                    }
1798                } finally {
1799                    if (!f.casHash(fh | LOCKED, fh)) {
1800                        f.hash = fh;
1801                        synchronized (f) { f.notifyAll(); };
1802                    }
1803                }
1804                if (count != 0) {
1805                    if (tab.length <= 64)
1806                        count = 2;
1807                    break;
1808                }
1809            }
1810        }
1811        if (delta != 0) {
1812            counter.add((long)delta);
1813            if (count > 1)
1814                checkForResize();
1815        }
1816        return val;
1817    }
1818
1819    /** Implementation for merge */
1820    @SuppressWarnings("unchecked") private final Object internalMerge
1821        (K k, V v, BiFun<? super V, ? super V, ? extends V> mf) {
1822        int h = spread(k.hashCode());
1823        Object val = null;
1824        int delta = 0;
1825        int count = 0;
1826        for (Node[] tab = table;;) {
1827            int i; Node f; int fh; Object fk, fv;
1828            if (tab == null)
1829                tab = initTable();
1830            else if ((f = tabAt(tab, i = (tab.length - 1) & h)) == null) {
1831                if (casTabAt(tab, i, null, new Node(h, k, v, null))) {
1832                    delta = 1;
1833                    val = v;
1834                    break;
1835                }
1836            }
1837            else if ((fh = f.hash) == MOVED) {
1838                if ((fk = f.key) instanceof TreeBin) {
1839                    TreeBin t = (TreeBin)fk;
1840                    t.acquire(0);
1841                    try {
1842                        if (tabAt(tab, i) == f) {
1843                            count = 1;
1844                            TreeNode p = t.getTreeNode(h, k, t.root);
1845                            val = (p == null) ? v : mf.apply((V)p.val, v);
1846                            if (val != null) {
1847                                if (p != null)
1848                                    p.val = val;
1849                                else {
1850                                    count = 2;
1851                                    delta = 1;
1852                                    t.putTreeNode(h, k, val);
1853                                }
1854                            }
1855                            else if (p != null) {
1856                                delta = -1;
1857                                t.deleteTreeNode(p);
1858                            }
1859                        }
1860                    } finally {
1861                        t.release(0);
1862                    }
1863                    if (count != 0)
1864                        break;
1865                }
1866                else
1867                    tab = (Node[])fk;
1868            }
1869            else if ((fh & LOCKED) != 0) {
1870                checkForResize();
1871                f.tryAwaitLock(tab, i);
1872            }
1873            else if (f.casHash(fh, fh | LOCKED)) {
1874                try {
1875                    if (tabAt(tab, i) == f) {
1876                        count = 1;
1877                        for (Node e = f, pred = null;; ++count) {
1878                            Object ek, ev;
1879                            if ((e.hash & HASH_BITS) == h &&
1880                                (ev = e.val) != null &&
1881                                ((ek = e.key) == k || k.equals(ek))) {
1882                                val = mf.apply(v, (V)ev);
1883                                if (val != null)
1884                                    e.val = val;
1885                                else {
1886                                    delta = -1;
1887                                    Node en = e.next;
1888                                    if (pred != null)
1889                                        pred.next = en;
1890                                    else
1891                                        setTabAt(tab, i, en);
1892                                }
1893                                break;
1894                            }
1895                            pred = e;
1896                            if ((e = e.next) == null) {
1897                                val = v;
1898                                pred.next = new Node(h, k, val, null);
1899                                delta = 1;
1900                                if (count >= TREE_THRESHOLD)
1901                                    replaceWithTreeBin(tab, i, k);
1902                                break;
1903                            }
1904                        }
1905                    }
1906                } finally {
1907                    if (!f.casHash(fh | LOCKED, fh)) {
1908                        f.hash = fh;
1909                        synchronized (f) { f.notifyAll(); };
1910                    }
1911                }
1912                if (count != 0) {
1913                    if (tab.length <= 64)
1914                        count = 2;
1915                    break;
1916                }
1917            }
1918        }
1919        if (delta != 0) {
1920            counter.add((long)delta);
1921            if (count > 1)
1922                checkForResize();
1923        }
1924        return val;
1925    }
1926
1927    /** Implementation for putAll */
1928    private final void internalPutAll(Map<?, ?> m) {
1929        tryPresize(m.size());
1930        long delta = 0L;     // number of uncommitted additions
1931        boolean npe = false; // to throw exception on exit for nulls
1932        try {                // to clean up counts on other exceptions
1933            for (Map.Entry<?, ?> entry : m.entrySet()) {
1934                Object k, v;
1935                if (entry == null || (k = entry.getKey()) == null ||
1936                    (v = entry.getValue()) == null) {
1937                    npe = true;
1938                    break;
1939                }
1940                int h = spread(k.hashCode());
1941                for (Node[] tab = table;;) {
1942                    int i; Node f; int fh; Object fk;
1943                    if (tab == null)
1944                        tab = initTable();
1945                    else if ((f = tabAt(tab, i = (tab.length - 1) & h)) == null){
1946                        if (casTabAt(tab, i, null, new Node(h, k, v, null))) {
1947                            ++delta;
1948                            break;
1949                        }
1950                    }
1951                    else if ((fh = f.hash) == MOVED) {
1952                        if ((fk = f.key) instanceof TreeBin) {
1953                            TreeBin t = (TreeBin)fk;
1954                            boolean validated = false;
1955                            t.acquire(0);
1956                            try {
1957                                if (tabAt(tab, i) == f) {
1958                                    validated = true;
1959                                    TreeNode p = t.getTreeNode(h, k, t.root);
1960                                    if (p != null)
1961                                        p.val = v;
1962                                    else {
1963                                        t.putTreeNode(h, k, v);
1964                                        ++delta;
1965                                    }
1966                                }
1967                            } finally {
1968                                t.release(0);
1969                            }
1970                            if (validated)
1971                                break;
1972                        }
1973                        else
1974                            tab = (Node[])fk;
1975                    }
1976                    else if ((fh & LOCKED) != 0) {
1977                        counter.add(delta);
1978                        delta = 0L;
1979                        checkForResize();
1980                        f.tryAwaitLock(tab, i);
1981                    }
1982                    else if (f.casHash(fh, fh | LOCKED)) {
1983                        int count = 0;
1984                        try {
1985                            if (tabAt(tab, i) == f) {
1986                                count = 1;
1987                                for (Node e = f;; ++count) {
1988                                    Object ek, ev;
1989                                    if ((e.hash & HASH_BITS) == h &&
1990                                        (ev = e.val) != null &&
1991                                        ((ek = e.key) == k || k.equals(ek))) {
1992                                        e.val = v;
1993                                        break;
1994                                    }
1995                                    Node last = e;
1996                                    if ((e = e.next) == null) {
1997                                        ++delta;
1998                                        last.next = new Node(h, k, v, null);
1999                                        if (count >= TREE_THRESHOLD)
2000                                            replaceWithTreeBin(tab, i, k);
2001                                        break;
2002                                    }
2003                                }
2004                            }
2005                        } finally {
2006                            if (!f.casHash(fh | LOCKED, fh)) {
2007                                f.hash = fh;
2008                                synchronized (f) { f.notifyAll(); };
2009                            }
2010                        }
2011                        if (count != 0) {
2012                            if (count > 1) {
2013                                counter.add(delta);
2014                                delta = 0L;
2015                                checkForResize();
2016                            }
2017                            break;
2018                        }
2019                    }
2020                }
2021            }
2022        } finally {
2023            if (delta != 0)
2024                counter.add(delta);
2025        }
2026        if (npe)
2027            throw new NullPointerException();
2028    }
2029
2030    /* ---------------- Table Initialization and Resizing -------------- */
2031
2032    /**
698       * Returns a power of two table size for the given desired capacity.
699       * See Hackers Delight, sec 3.2
700       */
# Line 2044 | Line 709 | public class ConcurrentHashMapV8<K, V>
709      }
710  
711      /**
712 <     * Initializes table, using the size recorded in sizeCtl.
712 >     * Returns x's Class if it is of the form "class C implements
713 >     * Comparable<C>", else null.
714       */
715 <    private final Node[] initTable() {
716 <        Node[] tab; int sc;
717 <        while ((tab = table) == null) {
718 <            if ((sc = sizeCtl) < 0)
719 <                Thread.yield(); // lost initialization race; just spin
720 <            else if (UNSAFE.compareAndSwapInt(this, sizeCtlOffset, sc, -1)) {
721 <                try {
722 <                    if ((tab = table) == null) {
723 <                        int n = (sc > 0) ? sc : DEFAULT_CAPACITY;
724 <                        tab = table = new Node[n];
725 <                        sc = n - (n >>> 2);
726 <                    }
727 <                } finally {
2062 <                    sizeCtl = sc;
2063 <                }
2064 <                break;
2065 <            }
2066 <        }
2067 <        return tab;
2068 <    }
2069 <
2070 <    /**
2071 <     * If table is too small and not already resizing, creates next
2072 <     * table and transfers bins.  Rechecks occupancy after a transfer
2073 <     * to see if another resize is already needed because resizings
2074 <     * are lagging additions.
2075 <     */
2076 <    private final void checkForResize() {
2077 <        Node[] tab; int n, sc;
2078 <        while ((tab = table) != null &&
2079 <               (n = tab.length) < MAXIMUM_CAPACITY &&
2080 <               (sc = sizeCtl) >= 0 && counter.sum() >= (long)sc &&
2081 <               UNSAFE.compareAndSwapInt(this, sizeCtlOffset, sc, -1)) {
2082 <            try {
2083 <                if (tab == table) {
2084 <                    table = rebuild(tab);
2085 <                    sc = (n << 1) - (n >>> 1);
715 >    static Class<?> comparableClassFor(Object x) {
716 >        if (x instanceof Comparable) {
717 >            Class<?> c; Type[] ts, as; Type t; ParameterizedType p;
718 >            if ((c = x.getClass()) == String.class) // bypass checks
719 >                return c;
720 >            if ((ts = c.getGenericInterfaces()) != null) {
721 >                for (int i = 0; i < ts.length; ++i) {
722 >                    if (((t = ts[i]) instanceof ParameterizedType) &&
723 >                        ((p = (ParameterizedType)t).getRawType() ==
724 >                         Comparable.class) &&
725 >                        (as = p.getActualTypeArguments()) != null &&
726 >                        as.length == 1 && as[0] == c) // type arg is c
727 >                        return c;
728                  }
2087            } finally {
2088                sizeCtl = sc;
729              }
730          }
731 +        return null;
732      }
733  
734      /**
735 <     * Tries to presize table to accommodate the given number of elements.
736 <     *
2096 <     * @param size number of elements (doesn't need to be perfectly accurate)
735 >     * Returns k.compareTo(x) if x matches kc (k's screened comparable
736 >     * class), else 0.
737       */
738 <    private final void tryPresize(int size) {
739 <        int c = (size >= (MAXIMUM_CAPACITY >>> 1)) ? MAXIMUM_CAPACITY :
740 <            tableSizeFor(size + (size >>> 1) + 1);
741 <        int sc;
2102 <        while ((sc = sizeCtl) >= 0) {
2103 <            Node[] tab = table; int n;
2104 <            if (tab == null || (n = tab.length) == 0) {
2105 <                n = (sc > c) ? sc : c;
2106 <                if (UNSAFE.compareAndSwapInt(this, sizeCtlOffset, sc, -1)) {
2107 <                    try {
2108 <                        if (table == tab) {
2109 <                            table = new Node[n];
2110 <                            sc = n - (n >>> 2);
2111 <                        }
2112 <                    } finally {
2113 <                        sizeCtl = sc;
2114 <                    }
2115 <                }
2116 <            }
2117 <            else if (c <= sc || n >= MAXIMUM_CAPACITY)
2118 <                break;
2119 <            else if (UNSAFE.compareAndSwapInt(this, sizeCtlOffset, sc, -1)) {
2120 <                try {
2121 <                    if (table == tab) {
2122 <                        table = rebuild(tab);
2123 <                        sc = (n << 1) - (n >>> 1);
2124 <                    }
2125 <                } finally {
2126 <                    sizeCtl = sc;
2127 <                }
2128 <            }
2129 <        }
738 >    @SuppressWarnings({"rawtypes","unchecked"}) // for cast to Comparable
739 >    static int compareComparables(Class<?> kc, Object k, Object x) {
740 >        return (x == null || x.getClass() != kc ? 0 :
741 >                ((Comparable)k).compareTo(x));
742      }
743  
744 +    /* ---------------- Table element access -------------- */
745 +
746      /*
747 <     * Moves and/or copies the nodes in each bin to new table. See
748 <     * above for explanation.
749 <     *
750 <     * @return the new table
751 <     */
752 <    private static final Node[] rebuild(Node[] tab) {
753 <        int n = tab.length;
754 <        Node[] nextTab = new Node[n << 1];
755 <        Node fwd = new Node(MOVED, nextTab, null, null);
756 <        int[] buffer = null;       // holds bins to revisit; null until needed
757 <        Node rev = null;           // reverse forwarder; null until needed
758 <        int nbuffered = 0;         // the number of bins in buffer list
759 <        int bufferIndex = 0;       // buffer index of current buffered bin
760 <        int bin = n - 1;           // current non-buffered bin or -1 if none
761 <
762 <        for (int i = bin;;) {      // start upwards sweep
763 <            int fh; Node f;
764 <            if ((f = tabAt(tab, i)) == null) {
765 <                if (bin >= 0) {    // Unbuffered; no lock needed (or available)
766 <                    if (!casTabAt(tab, i, f, fwd))
767 <                        continue;
768 <                }
769 <                else {             // transiently use a locked forwarding node
2156 <                    Node g = new Node(MOVED|LOCKED, nextTab, null, null);
2157 <                    if (!casTabAt(tab, i, f, g))
2158 <                        continue;
2159 <                    setTabAt(nextTab, i, null);
2160 <                    setTabAt(nextTab, i + n, null);
2161 <                    setTabAt(tab, i, fwd);
2162 <                    if (!g.casHash(MOVED|LOCKED, MOVED)) {
2163 <                        g.hash = MOVED;
2164 <                        synchronized (g) { g.notifyAll(); }
2165 <                    }
2166 <                }
2167 <            }
2168 <            else if ((fh = f.hash) == MOVED) {
2169 <                Object fk = f.key;
2170 <                if (fk instanceof TreeBin) {
2171 <                    TreeBin t = (TreeBin)fk;
2172 <                    boolean validated = false;
2173 <                    t.acquire(0);
2174 <                    try {
2175 <                        if (tabAt(tab, i) == f) {
2176 <                            validated = true;
2177 <                            splitTreeBin(nextTab, i, t);
2178 <                            setTabAt(tab, i, fwd);
2179 <                        }
2180 <                    } finally {
2181 <                        t.release(0);
2182 <                    }
2183 <                    if (!validated)
2184 <                        continue;
2185 <                }
2186 <            }
2187 <            else if ((fh & LOCKED) == 0 && f.casHash(fh, fh|LOCKED)) {
2188 <                boolean validated = false;
2189 <                try {              // split to lo and hi lists; copying as needed
2190 <                    if (tabAt(tab, i) == f) {
2191 <                        validated = true;
2192 <                        splitBin(nextTab, i, f);
2193 <                        setTabAt(tab, i, fwd);
2194 <                    }
2195 <                } finally {
2196 <                    if (!f.casHash(fh | LOCKED, fh)) {
2197 <                        f.hash = fh;
2198 <                        synchronized (f) { f.notifyAll(); };
2199 <                    }
2200 <                }
2201 <                if (!validated)
2202 <                    continue;
2203 <            }
2204 <            else {
2205 <                if (buffer == null) // initialize buffer for revisits
2206 <                    buffer = new int[TRANSFER_BUFFER_SIZE];
2207 <                if (bin < 0 && bufferIndex > 0) {
2208 <                    int j = buffer[--bufferIndex];
2209 <                    buffer[bufferIndex] = i;
2210 <                    i = j;         // swap with another bin
2211 <                    continue;
2212 <                }
2213 <                if (bin < 0 || nbuffered >= TRANSFER_BUFFER_SIZE) {
2214 <                    f.tryAwaitLock(tab, i);
2215 <                    continue;      // no other options -- block
2216 <                }
2217 <                if (rev == null)   // initialize reverse-forwarder
2218 <                    rev = new Node(MOVED, tab, null, null);
2219 <                if (tabAt(tab, i) != f || (f.hash & LOCKED) == 0)
2220 <                    continue;      // recheck before adding to list
2221 <                buffer[nbuffered++] = i;
2222 <                setTabAt(nextTab, i, rev);     // install place-holders
2223 <                setTabAt(nextTab, i + n, rev);
2224 <            }
2225 <
2226 <            if (bin > 0)
2227 <                i = --bin;
2228 <            else if (buffer != null && nbuffered > 0) {
2229 <                bin = -1;
2230 <                i = buffer[bufferIndex = --nbuffered];
2231 <            }
2232 <            else
2233 <                return nextTab;
2234 <        }
747 >     * Volatile access methods are used for table elements as well as
748 >     * elements of in-progress next table while resizing.  All uses of
749 >     * the tab arguments must be null checked by callers.  All callers
750 >     * also paranoically precheck that tab's length is not zero (or an
751 >     * equivalent check), thus ensuring that any index argument taking
752 >     * the form of a hash value anded with (length - 1) is a valid
753 >     * index.  Note that, to be correct wrt arbitrary concurrency
754 >     * errors by users, these checks must operate on local variables,
755 >     * which accounts for some odd-looking inline assignments below.
756 >     * Note that calls to setTabAt always occur within locked regions,
757 >     * and so in principle require only release ordering, not
758 >     * full volatile semantics, but are currently coded as volatile
759 >     * writes to be conservative.
760 >     */
761 >
762 >    @SuppressWarnings("unchecked")
763 >    static final <K,V> Node<K,V> tabAt(Node<K,V>[] tab, int i) {
764 >        return (Node<K,V>)U.getObjectVolatile(tab, ((long)i << ASHIFT) + ABASE);
765 >    }
766 >
767 >    static final <K,V> boolean casTabAt(Node<K,V>[] tab, int i,
768 >                                        Node<K,V> c, Node<K,V> v) {
769 >        return U.compareAndSwapObject(tab, ((long)i << ASHIFT) + ABASE, c, v);
770      }
771  
772 <    /**
773 <     * Splits a normal bin with list headed by e into lo and hi parts;
2239 <     * installs in given table.
2240 <     */
2241 <    private static void splitBin(Node[] nextTab, int i, Node e) {
2242 <        int bit = nextTab.length >>> 1; // bit to split on
2243 <        int runBit = e.hash & bit;
2244 <        Node lastRun = e, lo = null, hi = null;
2245 <        for (Node p = e.next; p != null; p = p.next) {
2246 <            int b = p.hash & bit;
2247 <            if (b != runBit) {
2248 <                runBit = b;
2249 <                lastRun = p;
2250 <            }
2251 <        }
2252 <        if (runBit == 0)
2253 <            lo = lastRun;
2254 <        else
2255 <            hi = lastRun;
2256 <        for (Node p = e; p != lastRun; p = p.next) {
2257 <            int ph = p.hash & HASH_BITS;
2258 <            Object pk = p.key, pv = p.val;
2259 <            if ((ph & bit) == 0)
2260 <                lo = new Node(ph, pk, pv, lo);
2261 <            else
2262 <                hi = new Node(ph, pk, pv, hi);
2263 <        }
2264 <        setTabAt(nextTab, i, lo);
2265 <        setTabAt(nextTab, i + bit, hi);
772 >    static final <K,V> void setTabAt(Node<K,V>[] tab, int i, Node<K,V> v) {
773 >        U.putObjectVolatile(tab, ((long)i << ASHIFT) + ABASE, v);
774      }
775  
776 +    /* ---------------- Fields -------------- */
777 +
778      /**
779 <     * Splits a tree bin into lo and hi parts; installs in given table.
779 >     * The array of bins. Lazily initialized upon first insertion.
780 >     * Size is always a power of two. Accessed directly by iterators.
781       */
782 <    private static void splitTreeBin(Node[] nextTab, int i, TreeBin t) {
2272 <        int bit = nextTab.length >>> 1;
2273 <        TreeBin lt = new TreeBin();
2274 <        TreeBin ht = new TreeBin();
2275 <        int lc = 0, hc = 0;
2276 <        for (Node e = t.first; e != null; e = e.next) {
2277 <            int h = e.hash & HASH_BITS;
2278 <            Object k = e.key, v = e.val;
2279 <            if ((h & bit) == 0) {
2280 <                ++lc;
2281 <                lt.putTreeNode(h, k, v);
2282 <            }
2283 <            else {
2284 <                ++hc;
2285 <                ht.putTreeNode(h, k, v);
2286 <            }
2287 <        }
2288 <        Node ln, hn; // throw away trees if too small
2289 <        if (lc <= (TREE_THRESHOLD >>> 1)) {
2290 <            ln = null;
2291 <            for (Node p = lt.first; p != null; p = p.next)
2292 <                ln = new Node(p.hash, p.key, p.val, ln);
2293 <        }
2294 <        else
2295 <            ln = new Node(MOVED, lt, null, null);
2296 <        setTabAt(nextTab, i, ln);
2297 <        if (hc <= (TREE_THRESHOLD >>> 1)) {
2298 <            hn = null;
2299 <            for (Node p = ht.first; p != null; p = p.next)
2300 <                hn = new Node(p.hash, p.key, p.val, hn);
2301 <        }
2302 <        else
2303 <            hn = new Node(MOVED, ht, null, null);
2304 <        setTabAt(nextTab, i + bit, hn);
2305 <    }
782 >    transient volatile Node<K,V>[] table;
783  
784      /**
785 <     * Implementation for clear. Steps through each bin, removing all
2309 <     * nodes.
785 >     * The next table to use; non-null only while resizing.
786       */
787 <    private final void internalClear() {
2312 <        long delta = 0L; // negative number of deletions
2313 <        int i = 0;
2314 <        Node[] tab = table;
2315 <        while (tab != null && i < tab.length) {
2316 <            int fh; Object fk;
2317 <            Node f = tabAt(tab, i);
2318 <            if (f == null)
2319 <                ++i;
2320 <            else if ((fh = f.hash) == MOVED) {
2321 <                if ((fk = f.key) instanceof TreeBin) {
2322 <                    TreeBin t = (TreeBin)fk;
2323 <                    t.acquire(0);
2324 <                    try {
2325 <                        if (tabAt(tab, i) == f) {
2326 <                            for (Node p = t.first; p != null; p = p.next) {
2327 <                                if (p.val != null) { // (currently always true)
2328 <                                    p.val = null;
2329 <                                    --delta;
2330 <                                }
2331 <                            }
2332 <                            t.first = null;
2333 <                            t.root = null;
2334 <                            ++i;
2335 <                        }
2336 <                    } finally {
2337 <                        t.release(0);
2338 <                    }
2339 <                }
2340 <                else
2341 <                    tab = (Node[])fk;
2342 <            }
2343 <            else if ((fh & LOCKED) != 0) {
2344 <                counter.add(delta); // opportunistically update count
2345 <                delta = 0L;
2346 <                f.tryAwaitLock(tab, i);
2347 <            }
2348 <            else if (f.casHash(fh, fh | LOCKED)) {
2349 <                try {
2350 <                    if (tabAt(tab, i) == f) {
2351 <                        for (Node e = f; e != null; e = e.next) {
2352 <                            if (e.val != null) {  // (currently always true)
2353 <                                e.val = null;
2354 <                                --delta;
2355 <                            }
2356 <                        }
2357 <                        setTabAt(tab, i, null);
2358 <                        ++i;
2359 <                    }
2360 <                } finally {
2361 <                    if (!f.casHash(fh | LOCKED, fh)) {
2362 <                        f.hash = fh;
2363 <                        synchronized (f) { f.notifyAll(); };
2364 <                    }
2365 <                }
2366 <            }
2367 <        }
2368 <        if (delta != 0)
2369 <            counter.add(delta);
2370 <    }
2371 <
2372 <    /* ----------------Table Traversal -------------- */
787 >    private transient volatile Node<K,V>[] nextTable;
788  
789      /**
790 <     * Encapsulates traversal for methods such as containsValue; also
791 <     * serves as a base class for other iterators and bulk tasks.
792 <     *
793 <     * At each step, the iterator snapshots the key ("nextKey") and
794 <     * value ("nextVal") of a valid node (i.e., one that, at point of
2380 <     * snapshot, has a non-null user value). Because val fields can
2381 <     * change (including to null, indicating deletion), field nextVal
2382 <     * might not be accurate at point of use, but still maintains the
2383 <     * weak consistency property of holding a value that was once
2384 <     * valid. To support iterator.remove, the nextKey field is not
2385 <     * updated (nulled out) when the iterator cannot advance.
2386 <     *
2387 <     * Internal traversals directly access these fields, as in:
2388 <     * {@code while (it.advance() != null) { process(it.nextKey); }}
2389 <     *
2390 <     * Exported iterators must track whether the iterator has advanced
2391 <     * (in hasNext vs next) (by setting/checking/nulling field
2392 <     * nextVal), and then extract key, value, or key-value pairs as
2393 <     * return values of next().
2394 <     *
2395 <     * The iterator visits once each still-valid node that was
2396 <     * reachable upon iterator construction. It might miss some that
2397 <     * were added to a bin after the bin was visited, which is OK wrt
2398 <     * consistency guarantees. Maintaining this property in the face
2399 <     * of possible ongoing resizes requires a fair amount of
2400 <     * bookkeeping state that is difficult to optimize away amidst
2401 <     * volatile accesses.  Even so, traversal maintains reasonable
2402 <     * throughput.
2403 <     *
2404 <     * Normally, iteration proceeds bin-by-bin traversing lists.
2405 <     * However, if the table has been resized, then all future steps
2406 <     * must traverse both the bin at the current index as well as at
2407 <     * (index + baseSize); and so on for further resizings. To
2408 <     * paranoically cope with potential sharing by users of iterators
2409 <     * across threads, iteration terminates if a bounds checks fails
2410 <     * for a table read.
2411 <     *
2412 <     * This class extends CountedCompleter to streamline parallel
2413 <     * iteration in bulk operations. This adds only a few fields of
2414 <     * space overhead, which is small enough in cases where it is not
2415 <     * needed to not worry about it.  Because CountedCompleter is
2416 <     * Serializable, but iterators need not be, we need to add warning
2417 <     * suppressions.
2418 <     */
2419 <    @SuppressWarnings("serial") static class Traverser<K,V,R> extends CountedCompleter<R> {
2420 <        final ConcurrentHashMapV8<K, V> map;
2421 <        Node next;           // the next entry to use
2422 <        Object nextKey;      // cached key field of next
2423 <        Object nextVal;      // cached val field of next
2424 <        Node[] tab;          // current table; updated if resized
2425 <        int index;           // index of bin to use next
2426 <        int baseIndex;       // current index of initial table
2427 <        int baseLimit;       // index bound for initial table
2428 <        int baseSize;        // initial table size
2429 <        int batch;           // split control
2430 <
2431 <        /** Creates iterator for all entries in the table. */
2432 <        Traverser(ConcurrentHashMapV8<K, V> map) {
2433 <            this.map = map;
2434 <        }
2435 <
2436 <        /** Creates iterator for split() methods and task constructors */
2437 <        Traverser(ConcurrentHashMapV8<K,V> map, Traverser<K,V,?> it, int batch) {
2438 <            super(it);
2439 <            this.batch = batch;
2440 <            if ((this.map = map) != null && it != null) { // split parent
2441 <                Node[] t;
2442 <                if ((t = it.tab) == null &&
2443 <                    (t = it.tab = map.table) != null)
2444 <                    it.baseLimit = it.baseSize = t.length;
2445 <                this.tab = t;
2446 <                this.baseSize = it.baseSize;
2447 <                int hi = this.baseLimit = it.baseLimit;
2448 <                it.baseLimit = this.index = this.baseIndex =
2449 <                    (hi + it.baseIndex + 1) >>> 1;
2450 <            }
2451 <        }
2452 <
2453 <        /**
2454 <         * Advances next; returns nextVal or null if terminated.
2455 <         * See above for explanation.
2456 <         */
2457 <        final Object advance() {
2458 <            Node e = next;
2459 <            Object ev = null;
2460 <            outer: do {
2461 <                if (e != null)                  // advance past used/skipped node
2462 <                    e = e.next;
2463 <                while (e == null) {             // get to next non-null bin
2464 <                    ConcurrentHashMapV8<K, V> m;
2465 <                    Node[] t; int b, i, n; Object ek; // checks must use locals
2466 <                    if ((t = tab) != null)
2467 <                        n = t.length;
2468 <                    else if ((m = map) != null && (t = tab = m.table) != null)
2469 <                        n = baseLimit = baseSize = t.length;
2470 <                    else
2471 <                        break outer;
2472 <                    if ((b = baseIndex) >= baseLimit ||
2473 <                        (i = index) < 0 || i >= n)
2474 <                        break outer;
2475 <                    if ((e = tabAt(t, i)) != null && e.hash == MOVED) {
2476 <                        if ((ek = e.key) instanceof TreeBin)
2477 <                            e = ((TreeBin)ek).first;
2478 <                        else {
2479 <                            tab = (Node[])ek;
2480 <                            continue;           // restarts due to null val
2481 <                        }
2482 <                    }                           // visit upper slots if present
2483 <                    index = (i += baseSize) < n ? i : (baseIndex = b + 1);
2484 <                }
2485 <                nextKey = e.key;
2486 <            } while ((ev = e.val) == null);    // skip deleted or special nodes
2487 <            next = e;
2488 <            return nextVal = ev;
2489 <        }
790 >     * Base counter value, used mainly when there is no contention,
791 >     * but also as a fallback during table initialization
792 >     * races. Updated via CAS.
793 >     */
794 >    private transient volatile long baseCount;
795  
796 <        public final void remove() {
797 <            Object k = nextKey;
798 <            if (k == null && (advance() == null || (k = nextKey) == null))
799 <                throw new IllegalStateException();
800 <            map.internalReplace(k, null, null);
801 <        }
796 >    /**
797 >     * Table initialization and resizing control.  When negative, the
798 >     * table is being initialized or resized: -1 for initialization,
799 >     * else -(1 + the number of active resizing threads).  Otherwise,
800 >     * when table is null, holds the initial table size to use upon
801 >     * creation, or 0 for default. After initialization, holds the
802 >     * next element count value upon which to resize the table.
803 >     */
804 >    private transient volatile int sizeCtl;
805  
806 <        public final boolean hasNext() {
807 <            return nextVal != null || advance() != null;
808 <        }
806 >    /**
807 >     * The next table index (plus one) to split while resizing.
808 >     */
809 >    private transient volatile int transferIndex;
810  
811 <        public final boolean hasMoreElements() { return hasNext(); }
811 >    /**
812 >     * Spinlock (locked via CAS) used when resizing and/or creating CounterCells.
813 >     */
814 >    private transient volatile int cellsBusy;
815  
816 <        public void compute() { } // default no-op CountedCompleter body
816 >    /**
817 >     * Table of counter cells. When non-null, size is a power of 2.
818 >     */
819 >    private transient volatile CounterCell[] counterCells;
820  
821 <        /**
822 <         * Returns a batch value > 0 if this task should (and must) be
823 <         * split, if so, adding to pending count, and in any case
824 <         * updating batch value. The initial batch value is approx
2510 <         * exp2 of the number of times (minus one) to split task by
2511 <         * two before executing leaf action. This value is faster to
2512 <         * compute and more convenient to use as a guide to splitting
2513 <         * than is the depth, since it is used while dividing by two
2514 <         * anyway.
2515 <         */
2516 <        final int preSplit() {
2517 <            ConcurrentHashMapV8<K, V> m; int b; Node[] t;  ForkJoinPool pool;
2518 <            if ((b = batch) < 0 && (m = map) != null) { // force initialization
2519 <                if ((t = tab) == null && (t = tab = m.table) != null)
2520 <                    baseLimit = baseSize = t.length;
2521 <                if (t != null) {
2522 <                    long n = m.counter.sum();
2523 <                    int par = ((pool = getPool()) == null) ?
2524 <                        ForkJoinPool.getCommonPoolParallelism() :
2525 <                        pool.getParallelism();
2526 <                    int sp = par << 3; // slack of 8
2527 <                    b = (n <= 0L) ? 0 : (n < (long)sp) ? (int)n : sp;
2528 <                }
2529 <            }
2530 <            b = (b <= 1 || baseIndex == baseLimit) ? 0 : (b >>> 1);
2531 <            if ((batch = b) > 0)
2532 <                addToPendingCount(1);
2533 <            return b;
2534 <        }
821 >    // views
822 >    private transient KeySetView<K,V> keySet;
823 >    private transient ValuesView<K,V> values;
824 >    private transient EntrySetView<K,V> entrySet;
825  
2536    }
826  
827      /* ---------------- Public operations -------------- */
828  
# Line 2541 | Line 830 | public class ConcurrentHashMapV8<K, V>
830       * Creates a new, empty map with the default initial table size (16).
831       */
832      public ConcurrentHashMapV8() {
2544        this.counter = new LongAdder();
833      }
834  
835      /**
# Line 2560 | Line 848 | public class ConcurrentHashMapV8<K, V>
848          int cap = ((initialCapacity >= (MAXIMUM_CAPACITY >>> 1)) ?
849                     MAXIMUM_CAPACITY :
850                     tableSizeFor(initialCapacity + (initialCapacity >>> 1) + 1));
2563        this.counter = new LongAdder();
851          this.sizeCtl = cap;
852      }
853  
# Line 2570 | Line 857 | public class ConcurrentHashMapV8<K, V>
857       * @param m the map
858       */
859      public ConcurrentHashMapV8(Map<? extends K, ? extends V> m) {
2573        this.counter = new LongAdder();
860          this.sizeCtl = DEFAULT_CAPACITY;
861 <        internalPutAll(m);
861 >        putAll(m);
862      }
863  
864      /**
# Line 2613 | Line 899 | public class ConcurrentHashMapV8<K, V>
899       * nonpositive
900       */
901      public ConcurrentHashMapV8(int initialCapacity,
902 <                               float loadFactor, int concurrencyLevel) {
902 >                             float loadFactor, int concurrencyLevel) {
903          if (!(loadFactor > 0.0f) || initialCapacity < 0 || concurrencyLevel <= 0)
904              throw new IllegalArgumentException();
905          if (initialCapacity < concurrencyLevel)   // Use at least as many bins
# Line 2621 | Line 907 | public class ConcurrentHashMapV8<K, V>
907          long size = (long)(1.0 + (long)initialCapacity / loadFactor);
908          int cap = (size >= (long)MAXIMUM_CAPACITY) ?
909              MAXIMUM_CAPACITY : tableSizeFor((int)size);
2624        this.counter = new LongAdder();
910          this.sizeCtl = cap;
911      }
912  
913 <    /**
2629 <     * Creates a new {@link Set} backed by a ConcurrentHashMapV8
2630 <     * from the given type to {@code Boolean.TRUE}.
2631 <     *
2632 <     * @return the new set
2633 <     */
2634 <    public static <K> KeySetView<K,Boolean> newKeySet() {
2635 <        return new KeySetView<K,Boolean>(new ConcurrentHashMapV8<K,Boolean>(),
2636 <                                      Boolean.TRUE);
2637 <    }
2638 <
2639 <    /**
2640 <     * Creates a new {@link Set} backed by a ConcurrentHashMapV8
2641 <     * from the given type to {@code Boolean.TRUE}.
2642 <     *
2643 <     * @param initialCapacity The implementation performs internal
2644 <     * sizing to accommodate this many elements.
2645 <     * @throws IllegalArgumentException if the initial capacity of
2646 <     * elements is negative
2647 <     * @return the new set
2648 <     */
2649 <    public static <K> KeySetView<K,Boolean> newKeySet(int initialCapacity) {
2650 <        return new KeySetView<K,Boolean>(new ConcurrentHashMapV8<K,Boolean>(initialCapacity),
2651 <                                      Boolean.TRUE);
2652 <    }
2653 <
2654 <    /**
2655 <     * {@inheritDoc}
2656 <     */
2657 <    public boolean isEmpty() {
2658 <        return counter.sum() <= 0L; // ignore transient negative values
2659 <    }
913 >    // Original (since JDK1.2) Map methods
914  
915      /**
916       * {@inheritDoc}
917       */
918      public int size() {
919 <        long n = counter.sum();
919 >        long n = sumCount();
920          return ((n < 0L) ? 0 :
921                  (n > (long)Integer.MAX_VALUE) ? Integer.MAX_VALUE :
922                  (int)n);
923      }
924  
925      /**
926 <     * Returns the number of mappings. This method should be used
2673 <     * instead of {@link #size} because a ConcurrentHashMapV8 may
2674 <     * contain more mappings than can be represented as an int. The
2675 <     * value returned is an estimate; the actual count may differ if
2676 <     * there are concurrent insertions or removals.
2677 <     *
2678 <     * @return the number of mappings
926 >     * {@inheritDoc}
927       */
928 <    public long mappingCount() {
929 <        long n = counter.sum();
2682 <        return (n < 0L) ? 0L : n; // ignore transient negative values
928 >    public boolean isEmpty() {
929 >        return sumCount() <= 0L; // ignore transient negative values
930      }
931  
932      /**
# Line 2693 | Line 940 | public class ConcurrentHashMapV8<K, V>
940       *
941       * @throws NullPointerException if the specified key is null
942       */
943 <    @SuppressWarnings("unchecked") public V get(Object key) {
944 <        if (key == null)
945 <            throw new NullPointerException();
946 <        return (V)internalGet(key);
947 <    }
948 <
949 <    /**
950 <     * Returns the value to which the specified key is mapped,
951 <     * or the given defaultValue if this map contains no mapping for the key.
952 <     *
953 <     * @param key the key
954 <     * @param defaultValue the value to return if this map contains
955 <     * no mapping for the given key
956 <     * @return the mapping for the key, if present; else the defaultValue
957 <     * @throws NullPointerException if the specified key is null
958 <     */
959 <    @SuppressWarnings("unchecked") public V getValueOrDefault(Object key, V defaultValue) {
960 <        if (key == null)
2714 <            throw new NullPointerException();
2715 <        V v = (V) internalGet(key);
2716 <        return v == null ? defaultValue : v;
943 >    public V get(Object key) {
944 >        Node<K,V>[] tab; Node<K,V> e, p; int n, eh; K ek;
945 >        int h = spread(key.hashCode());
946 >        if ((tab = table) != null && (n = tab.length) > 0 &&
947 >            (e = tabAt(tab, (n - 1) & h)) != null) {
948 >            if ((eh = e.hash) == h) {
949 >                if ((ek = e.key) == key || (ek != null && key.equals(ek)))
950 >                    return e.val;
951 >            }
952 >            else if (eh < 0)
953 >                return (p = e.find(h, key)) != null ? p.val : null;
954 >            while ((e = e.next) != null) {
955 >                if (e.hash == h &&
956 >                    ((ek = e.key) == key || (ek != null && key.equals(ek))))
957 >                    return e.val;
958 >            }
959 >        }
960 >        return null;
961      }
962  
963      /**
964       * Tests if the specified object is a key in this table.
965       *
966 <     * @param  key   possible key
966 >     * @param  key possible key
967       * @return {@code true} if and only if the specified object
968       *         is a key in this table, as determined by the
969       *         {@code equals} method; {@code false} otherwise
970       * @throws NullPointerException if the specified key is null
971       */
972      public boolean containsKey(Object key) {
973 <        if (key == null)
2730 <            throw new NullPointerException();
2731 <        return internalGet(key) != null;
973 >        return get(key) != null;
974      }
975  
976      /**
# Line 2744 | Line 986 | public class ConcurrentHashMapV8<K, V>
986      public boolean containsValue(Object value) {
987          if (value == null)
988              throw new NullPointerException();
989 <        Object v;
990 <        Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
991 <        while ((v = it.advance()) != null) {
992 <            if (v == value || value.equals(v))
993 <                return true;
989 >        Node<K,V>[] t;
990 >        if ((t = table) != null) {
991 >            Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length);
992 >            for (Node<K,V> p; (p = it.advance()) != null; ) {
993 >                V v;
994 >                if ((v = p.val) == value || (v != null && value.equals(v)))
995 >                    return true;
996 >            }
997          }
998          return false;
999      }
1000  
1001      /**
2757     * Legacy method testing if some key maps into the specified value
2758     * in this table.  This method is identical in functionality to
2759     * {@link #containsValue}, and exists solely to ensure
2760     * full compatibility with class {@link java.util.Hashtable},
2761     * which supported this method prior to introduction of the
2762     * Java Collections framework.
2763     *
2764     * @param  value a value to search for
2765     * @return {@code true} if and only if some key maps to the
2766     *         {@code value} argument in this table as
2767     *         determined by the {@code equals} method;
2768     *         {@code false} otherwise
2769     * @throws NullPointerException if the specified value is null
2770     */
2771    public boolean contains(Object value) {
2772        return containsValue(value);
2773    }
2774
2775    /**
1002       * Maps the specified key to the specified value in this table.
1003       * Neither the key nor the value can be null.
1004       *
# Line 2785 | Line 1011 | public class ConcurrentHashMapV8<K, V>
1011       *         {@code null} if there was no mapping for {@code key}
1012       * @throws NullPointerException if the specified key or value is null
1013       */
1014 <    @SuppressWarnings("unchecked") public V put(K key, V value) {
1015 <        if (key == null || value == null)
1014 >    public V put(K key, V value) {
1015 >        return putVal(key, value, false);
1016 >    }
1017 >
1018 >    /** Implementation for put and putIfAbsent */
1019 >    final V putVal(K key, V value, boolean onlyIfAbsent) {
1020 >        if (key == null || value == null) throw new NullPointerException();
1021 >        int hash = spread(key.hashCode());
1022 >        int binCount = 0;
1023 >        for (Node<K,V>[] tab = table;;) {
1024 >            Node<K,V> f; int n, i, fh;
1025 >            if (tab == null || (n = tab.length) == 0)
1026 >                tab = initTable();
1027 >            else if ((f = tabAt(tab, i = (n - 1) & hash)) == null) {
1028 >                if (casTabAt(tab, i, null,
1029 >                             new Node<K,V>(hash, key, value, null)))
1030 >                    break;                   // no lock when adding to empty bin
1031 >            }
1032 >            else if ((fh = f.hash) == MOVED)
1033 >                tab = helpTransfer(tab, f);
1034 >            else {
1035 >                V oldVal = null;
1036 >                synchronized (f) {
1037 >                    if (tabAt(tab, i) == f) {
1038 >                        if (fh >= 0) {
1039 >                            binCount = 1;
1040 >                            for (Node<K,V> e = f;; ++binCount) {
1041 >                                K ek;
1042 >                                if (e.hash == hash &&
1043 >                                    ((ek = e.key) == key ||
1044 >                                     (ek != null && key.equals(ek)))) {
1045 >                                    oldVal = e.val;
1046 >                                    if (!onlyIfAbsent)
1047 >                                        e.val = value;
1048 >                                    break;
1049 >                                }
1050 >                                Node<K,V> pred = e;
1051 >                                if ((e = e.next) == null) {
1052 >                                    pred.next = new Node<K,V>(hash, key,
1053 >                                                              value, null);
1054 >                                    break;
1055 >                                }
1056 >                            }
1057 >                        }
1058 >                        else if (f instanceof TreeBin) {
1059 >                            Node<K,V> p;
1060 >                            binCount = 2;
1061 >                            if ((p = ((TreeBin<K,V>)f).putTreeVal(hash, key,
1062 >                                                           value)) != null) {
1063 >                                oldVal = p.val;
1064 >                                if (!onlyIfAbsent)
1065 >                                    p.val = value;
1066 >                            }
1067 >                        }
1068 >                    }
1069 >                }
1070 >                if (binCount != 0) {
1071 >                    if (binCount >= TREEIFY_THRESHOLD)
1072 >                        treeifyBin(tab, i);
1073 >                    if (oldVal != null)
1074 >                        return oldVal;
1075 >                    break;
1076 >                }
1077 >            }
1078 >        }
1079 >        addCount(1L, binCount);
1080 >        return null;
1081 >    }
1082 >
1083 >    /**
1084 >     * Copies all of the mappings from the specified map to this one.
1085 >     * These mappings replace any mappings that this map had for any of the
1086 >     * keys currently in the specified map.
1087 >     *
1088 >     * @param m mappings to be stored in this map
1089 >     */
1090 >    public void putAll(Map<? extends K, ? extends V> m) {
1091 >        tryPresize(m.size());
1092 >        for (Map.Entry<? extends K, ? extends V> e : m.entrySet())
1093 >            putVal(e.getKey(), e.getValue(), false);
1094 >    }
1095 >
1096 >    /**
1097 >     * Removes the key (and its corresponding value) from this map.
1098 >     * This method does nothing if the key is not in the map.
1099 >     *
1100 >     * @param  key the key that needs to be removed
1101 >     * @return the previous value associated with {@code key}, or
1102 >     *         {@code null} if there was no mapping for {@code key}
1103 >     * @throws NullPointerException if the specified key is null
1104 >     */
1105 >    public V remove(Object key) {
1106 >        return replaceNode(key, null, null);
1107 >    }
1108 >
1109 >    /**
1110 >     * Implementation for the four public remove/replace methods:
1111 >     * Replaces node value with v, conditional upon match of cv if
1112 >     * non-null.  If resulting value is null, delete.
1113 >     */
1114 >    final V replaceNode(Object key, V value, Object cv) {
1115 >        int hash = spread(key.hashCode());
1116 >        for (Node<K,V>[] tab = table;;) {
1117 >            Node<K,V> f; int n, i, fh;
1118 >            if (tab == null || (n = tab.length) == 0 ||
1119 >                (f = tabAt(tab, i = (n - 1) & hash)) == null)
1120 >                break;
1121 >            else if ((fh = f.hash) == MOVED)
1122 >                tab = helpTransfer(tab, f);
1123 >            else {
1124 >                V oldVal = null;
1125 >                boolean validated = false;
1126 >                synchronized (f) {
1127 >                    if (tabAt(tab, i) == f) {
1128 >                        if (fh >= 0) {
1129 >                            validated = true;
1130 >                            for (Node<K,V> e = f, pred = null;;) {
1131 >                                K ek;
1132 >                                if (e.hash == hash &&
1133 >                                    ((ek = e.key) == key ||
1134 >                                     (ek != null && key.equals(ek)))) {
1135 >                                    V ev = e.val;
1136 >                                    if (cv == null || cv == ev ||
1137 >                                        (ev != null && cv.equals(ev))) {
1138 >                                        oldVal = ev;
1139 >                                        if (value != null)
1140 >                                            e.val = value;
1141 >                                        else if (pred != null)
1142 >                                            pred.next = e.next;
1143 >                                        else
1144 >                                            setTabAt(tab, i, e.next);
1145 >                                    }
1146 >                                    break;
1147 >                                }
1148 >                                pred = e;
1149 >                                if ((e = e.next) == null)
1150 >                                    break;
1151 >                            }
1152 >                        }
1153 >                        else if (f instanceof TreeBin) {
1154 >                            validated = true;
1155 >                            TreeBin<K,V> t = (TreeBin<K,V>)f;
1156 >                            TreeNode<K,V> r, p;
1157 >                            if ((r = t.root) != null &&
1158 >                                (p = r.findTreeNode(hash, key, null)) != null) {
1159 >                                V pv = p.val;
1160 >                                if (cv == null || cv == pv ||
1161 >                                    (pv != null && cv.equals(pv))) {
1162 >                                    oldVal = pv;
1163 >                                    if (value != null)
1164 >                                        p.val = value;
1165 >                                    else if (t.removeTreeNode(p))
1166 >                                        setTabAt(tab, i, untreeify(t.first));
1167 >                                }
1168 >                            }
1169 >                        }
1170 >                    }
1171 >                }
1172 >                if (validated) {
1173 >                    if (oldVal != null) {
1174 >                        if (value == null)
1175 >                            addCount(-1L, -1);
1176 >                        return oldVal;
1177 >                    }
1178 >                    break;
1179 >                }
1180 >            }
1181 >        }
1182 >        return null;
1183 >    }
1184 >
1185 >    /**
1186 >     * Removes all of the mappings from this map.
1187 >     */
1188 >    public void clear() {
1189 >        long delta = 0L; // negative number of deletions
1190 >        int i = 0;
1191 >        Node<K,V>[] tab = table;
1192 >        while (tab != null && i < tab.length) {
1193 >            int fh;
1194 >            Node<K,V> f = tabAt(tab, i);
1195 >            if (f == null)
1196 >                ++i;
1197 >            else if ((fh = f.hash) == MOVED) {
1198 >                tab = helpTransfer(tab, f);
1199 >                i = 0; // restart
1200 >            }
1201 >            else {
1202 >                synchronized (f) {
1203 >                    if (tabAt(tab, i) == f) {
1204 >                        Node<K,V> p = (fh >= 0 ? f :
1205 >                                       (f instanceof TreeBin) ?
1206 >                                       ((TreeBin<K,V>)f).first : null);
1207 >                        while (p != null) {
1208 >                            --delta;
1209 >                            p = p.next;
1210 >                        }
1211 >                        setTabAt(tab, i++, null);
1212 >                    }
1213 >                }
1214 >            }
1215 >        }
1216 >        if (delta != 0L)
1217 >            addCount(delta, -1);
1218 >    }
1219 >
1220 >    /**
1221 >     * Returns a {@link Set} view of the keys contained in this map.
1222 >     * The set is backed by the map, so changes to the map are
1223 >     * reflected in the set, and vice-versa. The set supports element
1224 >     * removal, which removes the corresponding mapping from this map,
1225 >     * via the {@code Iterator.remove}, {@code Set.remove},
1226 >     * {@code removeAll}, {@code retainAll}, and {@code clear}
1227 >     * operations.  It does not support the {@code add} or
1228 >     * {@code addAll} operations.
1229 >     *
1230 >     * <p>The view's {@code iterator} is a "weakly consistent" iterator
1231 >     * that will never throw {@link ConcurrentModificationException},
1232 >     * and guarantees to traverse elements as they existed upon
1233 >     * construction of the iterator, and may (but is not guaranteed to)
1234 >     * reflect any modifications subsequent to construction.
1235 >     *
1236 >     * @return the set view
1237 >     */
1238 >    public KeySetView<K,V> keySet() {
1239 >        KeySetView<K,V> ks;
1240 >        return (ks = keySet) != null ? ks : (keySet = new KeySetView<K,V>(this, null));
1241 >    }
1242 >
1243 >    /**
1244 >     * Returns a {@link Collection} view of the values contained in this map.
1245 >     * The collection is backed by the map, so changes to the map are
1246 >     * reflected in the collection, and vice-versa.  The collection
1247 >     * supports element removal, which removes the corresponding
1248 >     * mapping from this map, via the {@code Iterator.remove},
1249 >     * {@code Collection.remove}, {@code removeAll},
1250 >     * {@code retainAll}, and {@code clear} operations.  It does not
1251 >     * support the {@code add} or {@code addAll} operations.
1252 >     *
1253 >     * <p>The view's {@code iterator} is a "weakly consistent" iterator
1254 >     * that will never throw {@link ConcurrentModificationException},
1255 >     * and guarantees to traverse elements as they existed upon
1256 >     * construction of the iterator, and may (but is not guaranteed to)
1257 >     * reflect any modifications subsequent to construction.
1258 >     *
1259 >     * @return the collection view
1260 >     */
1261 >    public Collection<V> values() {
1262 >        ValuesView<K,V> vs;
1263 >        return (vs = values) != null ? vs : (values = new ValuesView<K,V>(this));
1264 >    }
1265 >
1266 >    /**
1267 >     * Returns a {@link Set} view of the mappings contained in this map.
1268 >     * The set is backed by the map, so changes to the map are
1269 >     * reflected in the set, and vice-versa.  The set supports element
1270 >     * removal, which removes the corresponding mapping from the map,
1271 >     * via the {@code Iterator.remove}, {@code Set.remove},
1272 >     * {@code removeAll}, {@code retainAll}, and {@code clear}
1273 >     * operations.
1274 >     *
1275 >     * <p>The view's {@code iterator} is a "weakly consistent" iterator
1276 >     * that will never throw {@link ConcurrentModificationException},
1277 >     * and guarantees to traverse elements as they existed upon
1278 >     * construction of the iterator, and may (but is not guaranteed to)
1279 >     * reflect any modifications subsequent to construction.
1280 >     *
1281 >     * @return the set view
1282 >     */
1283 >    public Set<Map.Entry<K,V>> entrySet() {
1284 >        EntrySetView<K,V> es;
1285 >        return (es = entrySet) != null ? es : (entrySet = new EntrySetView<K,V>(this));
1286 >    }
1287 >
1288 >    /**
1289 >     * Returns the hash code value for this {@link Map}, i.e.,
1290 >     * the sum of, for each key-value pair in the map,
1291 >     * {@code key.hashCode() ^ value.hashCode()}.
1292 >     *
1293 >     * @return the hash code value for this map
1294 >     */
1295 >    public int hashCode() {
1296 >        int h = 0;
1297 >        Node<K,V>[] t;
1298 >        if ((t = table) != null) {
1299 >            Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length);
1300 >            for (Node<K,V> p; (p = it.advance()) != null; )
1301 >                h += p.key.hashCode() ^ p.val.hashCode();
1302 >        }
1303 >        return h;
1304 >    }
1305 >
1306 >    /**
1307 >     * Returns a string representation of this map.  The string
1308 >     * representation consists of a list of key-value mappings (in no
1309 >     * particular order) enclosed in braces ("{@code {}}").  Adjacent
1310 >     * mappings are separated by the characters {@code ", "} (comma
1311 >     * and space).  Each key-value mapping is rendered as the key
1312 >     * followed by an equals sign ("{@code =}") followed by the
1313 >     * associated value.
1314 >     *
1315 >     * @return a string representation of this map
1316 >     */
1317 >    public String toString() {
1318 >        Node<K,V>[] t;
1319 >        int f = (t = table) == null ? 0 : t.length;
1320 >        Traverser<K,V> it = new Traverser<K,V>(t, f, 0, f);
1321 >        StringBuilder sb = new StringBuilder();
1322 >        sb.append('{');
1323 >        Node<K,V> p;
1324 >        if ((p = it.advance()) != null) {
1325 >            for (;;) {
1326 >                K k = p.key;
1327 >                V v = p.val;
1328 >                sb.append(k == this ? "(this Map)" : k);
1329 >                sb.append('=');
1330 >                sb.append(v == this ? "(this Map)" : v);
1331 >                if ((p = it.advance()) == null)
1332 >                    break;
1333 >                sb.append(',').append(' ');
1334 >            }
1335 >        }
1336 >        return sb.append('}').toString();
1337 >    }
1338 >
1339 >    /**
1340 >     * Compares the specified object with this map for equality.
1341 >     * Returns {@code true} if the given object is a map with the same
1342 >     * mappings as this map.  This operation may return misleading
1343 >     * results if either map is concurrently modified during execution
1344 >     * of this method.
1345 >     *
1346 >     * @param o object to be compared for equality with this map
1347 >     * @return {@code true} if the specified object is equal to this map
1348 >     */
1349 >    public boolean equals(Object o) {
1350 >        if (o != this) {
1351 >            if (!(o instanceof Map))
1352 >                return false;
1353 >            Map<?,?> m = (Map<?,?>) o;
1354 >            Node<K,V>[] t;
1355 >            int f = (t = table) == null ? 0 : t.length;
1356 >            Traverser<K,V> it = new Traverser<K,V>(t, f, 0, f);
1357 >            for (Node<K,V> p; (p = it.advance()) != null; ) {
1358 >                V val = p.val;
1359 >                Object v = m.get(p.key);
1360 >                if (v == null || (v != val && !v.equals(val)))
1361 >                    return false;
1362 >            }
1363 >            for (Map.Entry<?,?> e : m.entrySet()) {
1364 >                Object mk, mv, v;
1365 >                if ((mk = e.getKey()) == null ||
1366 >                    (mv = e.getValue()) == null ||
1367 >                    (v = get(mk)) == null ||
1368 >                    (mv != v && !mv.equals(v)))
1369 >                    return false;
1370 >            }
1371 >        }
1372 >        return true;
1373 >    }
1374 >
1375 >    /**
1376 >     * Stripped-down version of helper class used in previous version,
1377 >     * declared for the sake of serialization compatibility
1378 >     */
1379 >    static class Segment<K,V> extends ReentrantLock implements Serializable {
1380 >        private static final long serialVersionUID = 2249069246763182397L;
1381 >        final float loadFactor;
1382 >        Segment(float lf) { this.loadFactor = lf; }
1383 >    }
1384 >
1385 >    /**
1386 >     * Saves the state of the {@code ConcurrentHashMapV8} instance to a
1387 >     * stream (i.e., serializes it).
1388 >     * @param s the stream
1389 >     * @throws java.io.IOException if an I/O error occurs
1390 >     * @serialData
1391 >     * the key (Object) and value (Object)
1392 >     * for each key-value mapping, followed by a null pair.
1393 >     * The key-value mappings are emitted in no particular order.
1394 >     */
1395 >    private void writeObject(java.io.ObjectOutputStream s)
1396 >        throws java.io.IOException {
1397 >        // For serialization compatibility
1398 >        // Emulate segment calculation from previous version of this class
1399 >        int sshift = 0;
1400 >        int ssize = 1;
1401 >        while (ssize < DEFAULT_CONCURRENCY_LEVEL) {
1402 >            ++sshift;
1403 >            ssize <<= 1;
1404 >        }
1405 >        int segmentShift = 32 - sshift;
1406 >        int segmentMask = ssize - 1;
1407 >        @SuppressWarnings("unchecked") Segment<K,V>[] segments = (Segment<K,V>[])
1408 >            new Segment<?,?>[DEFAULT_CONCURRENCY_LEVEL];
1409 >        for (int i = 0; i < segments.length; ++i)
1410 >            segments[i] = new Segment<K,V>(LOAD_FACTOR);
1411 >        s.putFields().put("segments", segments);
1412 >        s.putFields().put("segmentShift", segmentShift);
1413 >        s.putFields().put("segmentMask", segmentMask);
1414 >        s.writeFields();
1415 >
1416 >        Node<K,V>[] t;
1417 >        if ((t = table) != null) {
1418 >            Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length);
1419 >            for (Node<K,V> p; (p = it.advance()) != null; ) {
1420 >                s.writeObject(p.key);
1421 >                s.writeObject(p.val);
1422 >            }
1423 >        }
1424 >        s.writeObject(null);
1425 >        s.writeObject(null);
1426 >        segments = null; // throw away
1427 >    }
1428 >
1429 >    /**
1430 >     * Reconstitutes the instance from a stream (that is, deserializes it).
1431 >     * @param s the stream
1432 >     * @throws ClassNotFoundException if the class of a serialized object
1433 >     *         could not be found
1434 >     * @throws java.io.IOException if an I/O error occurs
1435 >     */
1436 >    private void readObject(java.io.ObjectInputStream s)
1437 >        throws java.io.IOException, ClassNotFoundException {
1438 >        /*
1439 >         * To improve performance in typical cases, we create nodes
1440 >         * while reading, then place in table once size is known.
1441 >         * However, we must also validate uniqueness and deal with
1442 >         * overpopulated bins while doing so, which requires
1443 >         * specialized versions of putVal mechanics.
1444 >         */
1445 >        sizeCtl = -1; // force exclusion for table construction
1446 >        s.defaultReadObject();
1447 >        long size = 0L;
1448 >        Node<K,V> p = null;
1449 >        for (;;) {
1450 >            @SuppressWarnings("unchecked") K k = (K) s.readObject();
1451 >            @SuppressWarnings("unchecked") V v = (V) s.readObject();
1452 >            if (k != null && v != null) {
1453 >                p = new Node<K,V>(spread(k.hashCode()), k, v, p);
1454 >                ++size;
1455 >            }
1456 >            else
1457 >                break;
1458 >        }
1459 >        if (size == 0L)
1460 >            sizeCtl = 0;
1461 >        else {
1462 >            int n;
1463 >            if (size >= (long)(MAXIMUM_CAPACITY >>> 1))
1464 >                n = MAXIMUM_CAPACITY;
1465 >            else {
1466 >                int sz = (int)size;
1467 >                n = tableSizeFor(sz + (sz >>> 1) + 1);
1468 >            }
1469 >            @SuppressWarnings("unchecked")
1470 >                Node<K,V>[] tab = (Node<K,V>[])new Node<?,?>[n];
1471 >            int mask = n - 1;
1472 >            long added = 0L;
1473 >            while (p != null) {
1474 >                boolean insertAtFront;
1475 >                Node<K,V> next = p.next, first;
1476 >                int h = p.hash, j = h & mask;
1477 >                if ((first = tabAt(tab, j)) == null)
1478 >                    insertAtFront = true;
1479 >                else {
1480 >                    K k = p.key;
1481 >                    if (first.hash < 0) {
1482 >                        TreeBin<K,V> t = (TreeBin<K,V>)first;
1483 >                        if (t.putTreeVal(h, k, p.val) == null)
1484 >                            ++added;
1485 >                        insertAtFront = false;
1486 >                    }
1487 >                    else {
1488 >                        int binCount = 0;
1489 >                        insertAtFront = true;
1490 >                        Node<K,V> q; K qk;
1491 >                        for (q = first; q != null; q = q.next) {
1492 >                            if (q.hash == h &&
1493 >                                ((qk = q.key) == k ||
1494 >                                 (qk != null && k.equals(qk)))) {
1495 >                                insertAtFront = false;
1496 >                                break;
1497 >                            }
1498 >                            ++binCount;
1499 >                        }
1500 >                        if (insertAtFront && binCount >= TREEIFY_THRESHOLD) {
1501 >                            insertAtFront = false;
1502 >                            ++added;
1503 >                            p.next = first;
1504 >                            TreeNode<K,V> hd = null, tl = null;
1505 >                            for (q = p; q != null; q = q.next) {
1506 >                                TreeNode<K,V> t = new TreeNode<K,V>
1507 >                                    (q.hash, q.key, q.val, null, null);
1508 >                                if ((t.prev = tl) == null)
1509 >                                    hd = t;
1510 >                                else
1511 >                                    tl.next = t;
1512 >                                tl = t;
1513 >                            }
1514 >                            setTabAt(tab, j, new TreeBin<K,V>(hd));
1515 >                        }
1516 >                    }
1517 >                }
1518 >                if (insertAtFront) {
1519 >                    ++added;
1520 >                    p.next = first;
1521 >                    setTabAt(tab, j, p);
1522 >                }
1523 >                p = next;
1524 >            }
1525 >            table = tab;
1526 >            sizeCtl = n - (n >>> 2);
1527 >            baseCount = added;
1528 >        }
1529 >    }
1530 >
1531 >    // ConcurrentMap methods
1532 >
1533 >    /**
1534 >     * {@inheritDoc}
1535 >     *
1536 >     * @return the previous value associated with the specified key,
1537 >     *         or {@code null} if there was no mapping for the key
1538 >     * @throws NullPointerException if the specified key or value is null
1539 >     */
1540 >    public V putIfAbsent(K key, V value) {
1541 >        return putVal(key, value, true);
1542 >    }
1543 >
1544 >    /**
1545 >     * {@inheritDoc}
1546 >     *
1547 >     * @throws NullPointerException if the specified key is null
1548 >     */
1549 >    public boolean remove(Object key, Object value) {
1550 >        if (key == null)
1551              throw new NullPointerException();
1552 <        return (V)internalPut(key, value);
1552 >        return value != null && replaceNode(key, null, value) != null;
1553 >    }
1554 >
1555 >    /**
1556 >     * {@inheritDoc}
1557 >     *
1558 >     * @throws NullPointerException if any of the arguments are null
1559 >     */
1560 >    public boolean replace(K key, V oldValue, V newValue) {
1561 >        if (key == null || oldValue == null || newValue == null)
1562 >            throw new NullPointerException();
1563 >        return replaceNode(key, newValue, oldValue) != null;
1564      }
1565  
1566      /**
# Line 2798 | Line 1570 | public class ConcurrentHashMapV8<K, V>
1570       *         or {@code null} if there was no mapping for the key
1571       * @throws NullPointerException if the specified key or value is null
1572       */
1573 <    @SuppressWarnings("unchecked") public V putIfAbsent(K key, V value) {
1573 >    public V replace(K key, V value) {
1574          if (key == null || value == null)
1575              throw new NullPointerException();
1576 <        return (V)internalPutIfAbsent(key, value);
1576 >        return replaceNode(key, value, null);
1577      }
1578  
1579 +    // Overrides of JDK8+ Map extension method defaults
1580 +
1581      /**
1582 <     * Copies all of the mappings from the specified map to this one.
1583 <     * These mappings replace any mappings that this map had for any of the
1584 <     * keys currently in the specified map.
1582 >     * Returns the value to which the specified key is mapped, or the
1583 >     * given default value if this map contains no mapping for the
1584 >     * key.
1585       *
1586 <     * @param m mappings to be stored in this map
1586 >     * @param key the key whose associated value is to be returned
1587 >     * @param defaultValue the value to return if this map contains
1588 >     * no mapping for the given key
1589 >     * @return the mapping for the key, if present; else the default value
1590 >     * @throws NullPointerException if the specified key is null
1591       */
1592 <    public void putAll(Map<? extends K, ? extends V> m) {
1593 <        internalPutAll(m);
1592 >    public V getOrDefault(Object key, V defaultValue) {
1593 >        V v;
1594 >        return (v = get(key)) == null ? defaultValue : v;
1595 >    }
1596 >
1597 >    public void forEach(BiAction<? super K, ? super V> action) {
1598 >        if (action == null) throw new NullPointerException();
1599 >        Node<K,V>[] t;
1600 >        if ((t = table) != null) {
1601 >            Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length);
1602 >            for (Node<K,V> p; (p = it.advance()) != null; ) {
1603 >                action.apply(p.key, p.val);
1604 >            }
1605 >        }
1606 >    }
1607 >
1608 >    public void replaceAll(BiFun<? super K, ? super V, ? extends V> function) {
1609 >        if (function == null) throw new NullPointerException();
1610 >        Node<K,V>[] t;
1611 >        if ((t = table) != null) {
1612 >            Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length);
1613 >            for (Node<K,V> p; (p = it.advance()) != null; ) {
1614 >                V oldValue = p.val;
1615 >                for (K key = p.key;;) {
1616 >                    V newValue = function.apply(key, oldValue);
1617 >                    if (newValue == null)
1618 >                        throw new NullPointerException();
1619 >                    if (replaceNode(key, newValue, oldValue) != null ||
1620 >                        (oldValue = get(key)) == null)
1621 >                        break;
1622 >                }
1623 >            }
1624 >        }
1625      }
1626  
1627      /**
1628       * If the specified key is not already associated with a value,
1629 <     * computes its value using the given mappingFunction and enters
1630 <     * it into the map unless null.  This is equivalent to
1631 <     * <pre> {@code
1632 <     * if (map.containsKey(key))
1633 <     *   return map.get(key);
1634 <     * value = mappingFunction.apply(key);
1635 <     * if (value != null)
2827 <     *   map.put(key, value);
2828 <     * return value;}</pre>
2829 <     *
2830 <     * except that the action is performed atomically.  If the
2831 <     * function returns {@code null} no mapping is recorded. If the
2832 <     * function itself throws an (unchecked) exception, the exception
2833 <     * is rethrown to its caller, and no mapping is recorded.  Some
2834 <     * attempted update operations on this map by other threads may be
2835 <     * blocked while computation is in progress, so the computation
2836 <     * should be short and simple, and must not attempt to update any
2837 <     * other mappings of this Map. The most appropriate usage is to
2838 <     * construct a new object serving as an initial mapped value, or
2839 <     * memoized result, as in:
2840 <     *
2841 <     *  <pre> {@code
2842 <     * map.computeIfAbsent(key, new Fun<K, V>() {
2843 <     *   public V map(K k) { return new Value(f(k)); }});}</pre>
1629 >     * attempts to compute its value using the given mapping function
1630 >     * and enters it into this map unless {@code null}.  The entire
1631 >     * method invocation is performed atomically, so the function is
1632 >     * applied at most once per key.  Some attempted update operations
1633 >     * on this map by other threads may be blocked while computation
1634 >     * is in progress, so the computation should be short and simple,
1635 >     * and must not attempt to update any other mappings of this map.
1636       *
1637       * @param key key with which the specified value is to be associated
1638       * @param mappingFunction the function to compute a value
# Line 2854 | Line 1646 | public class ConcurrentHashMapV8<K, V>
1646       * @throws RuntimeException or Error if the mappingFunction does so,
1647       *         in which case the mapping is left unestablished
1648       */
1649 <    @SuppressWarnings("unchecked") public V computeIfAbsent
2858 <        (K key, Fun<? super K, ? extends V> mappingFunction) {
1649 >    public V computeIfAbsent(K key, Fun<? super K, ? extends V> mappingFunction) {
1650          if (key == null || mappingFunction == null)
1651              throw new NullPointerException();
1652 <        return (V)internalComputeIfAbsent(key, mappingFunction);
1652 >        int h = spread(key.hashCode());
1653 >        V val = null;
1654 >        int binCount = 0;
1655 >        for (Node<K,V>[] tab = table;;) {
1656 >            Node<K,V> f; int n, i, fh;
1657 >            if (tab == null || (n = tab.length) == 0)
1658 >                tab = initTable();
1659 >            else if ((f = tabAt(tab, i = (n - 1) & h)) == null) {
1660 >                Node<K,V> r = new ReservationNode<K,V>();
1661 >                synchronized (r) {
1662 >                    if (casTabAt(tab, i, null, r)) {
1663 >                        binCount = 1;
1664 >                        Node<K,V> node = null;
1665 >                        try {
1666 >                            if ((val = mappingFunction.apply(key)) != null)
1667 >                                node = new Node<K,V>(h, key, val, null);
1668 >                        } finally {
1669 >                            setTabAt(tab, i, node);
1670 >                        }
1671 >                    }
1672 >                }
1673 >                if (binCount != 0)
1674 >                    break;
1675 >            }
1676 >            else if ((fh = f.hash) == MOVED)
1677 >                tab = helpTransfer(tab, f);
1678 >            else {
1679 >                boolean added = false;
1680 >                synchronized (f) {
1681 >                    if (tabAt(tab, i) == f) {
1682 >                        if (fh >= 0) {
1683 >                            binCount = 1;
1684 >                            for (Node<K,V> e = f;; ++binCount) {
1685 >                                K ek; V ev;
1686 >                                if (e.hash == h &&
1687 >                                    ((ek = e.key) == key ||
1688 >                                     (ek != null && key.equals(ek)))) {
1689 >                                    val = e.val;
1690 >                                    break;
1691 >                                }
1692 >                                Node<K,V> pred = e;
1693 >                                if ((e = e.next) == null) {
1694 >                                    if ((val = mappingFunction.apply(key)) != null) {
1695 >                                        added = true;
1696 >                                        pred.next = new Node<K,V>(h, key, val, null);
1697 >                                    }
1698 >                                    break;
1699 >                                }
1700 >                            }
1701 >                        }
1702 >                        else if (f instanceof TreeBin) {
1703 >                            binCount = 2;
1704 >                            TreeBin<K,V> t = (TreeBin<K,V>)f;
1705 >                            TreeNode<K,V> r, p;
1706 >                            if ((r = t.root) != null &&
1707 >                                (p = r.findTreeNode(h, key, null)) != null)
1708 >                                val = p.val;
1709 >                            else if ((val = mappingFunction.apply(key)) != null) {
1710 >                                added = true;
1711 >                                t.putTreeVal(h, key, val);
1712 >                            }
1713 >                        }
1714 >                    }
1715 >                }
1716 >                if (binCount != 0) {
1717 >                    if (binCount >= TREEIFY_THRESHOLD)
1718 >                        treeifyBin(tab, i);
1719 >                    if (!added)
1720 >                        return val;
1721 >                    break;
1722 >                }
1723 >            }
1724 >        }
1725 >        if (val != null)
1726 >            addCount(1L, binCount);
1727 >        return val;
1728      }
1729  
1730      /**
1731 <     * If the given key is present, computes a new mapping value given a key and
1732 <     * its current mapped value. This is equivalent to
1733 <     *  <pre> {@code
1734 <     *   if (map.containsKey(key)) {
1735 <     *     value = remappingFunction.apply(key, map.get(key));
1736 <     *     if (value != null)
1737 <     *       map.put(key, value);
2872 <     *     else
2873 <     *       map.remove(key);
2874 <     *   }
2875 <     * }</pre>
2876 <     *
2877 <     * except that the action is performed atomically.  If the
2878 <     * function returns {@code null}, the mapping is removed.  If the
2879 <     * function itself throws an (unchecked) exception, the exception
2880 <     * is rethrown to its caller, and the current mapping is left
2881 <     * unchanged.  Some attempted update operations on this map by
2882 <     * other threads may be blocked while computation is in progress,
2883 <     * so the computation should be short and simple, and must not
2884 <     * attempt to update any other mappings of this Map. For example,
2885 <     * to either create or append new messages to a value mapping:
1731 >     * If the value for the specified key is present, attempts to
1732 >     * compute a new mapping given the key and its current mapped
1733 >     * value.  The entire method invocation is performed atomically.
1734 >     * Some attempted update operations on this map by other threads
1735 >     * may be blocked while computation is in progress, so the
1736 >     * computation should be short and simple, and must not attempt to
1737 >     * update any other mappings of this map.
1738       *
1739 <     * @param key key with which the specified value is to be associated
1739 >     * @param key key with which a value may be associated
1740       * @param remappingFunction the function to compute a value
1741       * @return the new value associated with the specified key, or null if none
1742       * @throws NullPointerException if the specified key or remappingFunction
# Line 2895 | Line 1747 | public class ConcurrentHashMapV8<K, V>
1747       * @throws RuntimeException or Error if the remappingFunction does so,
1748       *         in which case the mapping is unchanged
1749       */
1750 <    @SuppressWarnings("unchecked") public V computeIfPresent
2899 <        (K key, BiFun<? super K, ? super V, ? extends V> remappingFunction) {
1750 >    public V computeIfPresent(K key, BiFun<? super K, ? super V, ? extends V> remappingFunction) {
1751          if (key == null || remappingFunction == null)
1752              throw new NullPointerException();
1753 <        return (V)internalCompute(key, true, remappingFunction);
1753 >        int h = spread(key.hashCode());
1754 >        V val = null;
1755 >        int delta = 0;
1756 >        int binCount = 0;
1757 >        for (Node<K,V>[] tab = table;;) {
1758 >            Node<K,V> f; int n, i, fh;
1759 >            if (tab == null || (n = tab.length) == 0)
1760 >                tab = initTable();
1761 >            else if ((f = tabAt(tab, i = (n - 1) & h)) == null)
1762 >                break;
1763 >            else if ((fh = f.hash) == MOVED)
1764 >                tab = helpTransfer(tab, f);
1765 >            else {
1766 >                synchronized (f) {
1767 >                    if (tabAt(tab, i) == f) {
1768 >                        if (fh >= 0) {
1769 >                            binCount = 1;
1770 >                            for (Node<K,V> e = f, pred = null;; ++binCount) {
1771 >                                K ek;
1772 >                                if (e.hash == h &&
1773 >                                    ((ek = e.key) == key ||
1774 >                                     (ek != null && key.equals(ek)))) {
1775 >                                    val = remappingFunction.apply(key, e.val);
1776 >                                    if (val != null)
1777 >                                        e.val = val;
1778 >                                    else {
1779 >                                        delta = -1;
1780 >                                        Node<K,V> en = e.next;
1781 >                                        if (pred != null)
1782 >                                            pred.next = en;
1783 >                                        else
1784 >                                            setTabAt(tab, i, en);
1785 >                                    }
1786 >                                    break;
1787 >                                }
1788 >                                pred = e;
1789 >                                if ((e = e.next) == null)
1790 >                                    break;
1791 >                            }
1792 >                        }
1793 >                        else if (f instanceof TreeBin) {
1794 >                            binCount = 2;
1795 >                            TreeBin<K,V> t = (TreeBin<K,V>)f;
1796 >                            TreeNode<K,V> r, p;
1797 >                            if ((r = t.root) != null &&
1798 >                                (p = r.findTreeNode(h, key, null)) != null) {
1799 >                                val = remappingFunction.apply(key, p.val);
1800 >                                if (val != null)
1801 >                                    p.val = val;
1802 >                                else {
1803 >                                    delta = -1;
1804 >                                    if (t.removeTreeNode(p))
1805 >                                        setTabAt(tab, i, untreeify(t.first));
1806 >                                }
1807 >                            }
1808 >                        }
1809 >                    }
1810 >                }
1811 >                if (binCount != 0)
1812 >                    break;
1813 >            }
1814 >        }
1815 >        if (delta != 0)
1816 >            addCount((long)delta, binCount);
1817 >        return val;
1818      }
1819  
1820      /**
1821 <     * Computes a new mapping value given a key and
1822 <     * its current mapped value (or {@code null} if there is no current
1823 <     * mapping). This is equivalent to
1824 <     *  <pre> {@code
1825 <     *   value = remappingFunction.apply(key, map.get(key));
1826 <     *   if (value != null)
1827 <     *     map.put(key, value);
2913 <     *   else
2914 <     *     map.remove(key);
2915 <     * }</pre>
2916 <     *
2917 <     * except that the action is performed atomically.  If the
2918 <     * function returns {@code null}, the mapping is removed.  If the
2919 <     * function itself throws an (unchecked) exception, the exception
2920 <     * is rethrown to its caller, and the current mapping is left
2921 <     * unchanged.  Some attempted update operations on this map by
2922 <     * other threads may be blocked while computation is in progress,
2923 <     * so the computation should be short and simple, and must not
2924 <     * attempt to update any other mappings of this Map. For example,
2925 <     * to either create or append new messages to a value mapping:
2926 <     *
2927 <     * <pre> {@code
2928 <     * Map<Key, String> map = ...;
2929 <     * final String msg = ...;
2930 <     * map.compute(key, new BiFun<Key, String, String>() {
2931 <     *   public String apply(Key k, String v) {
2932 <     *    return (v == null) ? msg : v + msg;});}}</pre>
1821 >     * Attempts to compute a mapping for the specified key and its
1822 >     * current mapped value (or {@code null} if there is no current
1823 >     * mapping). The entire method invocation is performed atomically.
1824 >     * Some attempted update operations on this map by other threads
1825 >     * may be blocked while computation is in progress, so the
1826 >     * computation should be short and simple, and must not attempt to
1827 >     * update any other mappings of this Map.
1828       *
1829       * @param key key with which the specified value is to be associated
1830       * @param remappingFunction the function to compute a value
# Line 2942 | Line 1837 | public class ConcurrentHashMapV8<K, V>
1837       * @throws RuntimeException or Error if the remappingFunction does so,
1838       *         in which case the mapping is unchanged
1839       */
1840 <    @SuppressWarnings("unchecked") public V compute
1841 <        (K key, BiFun<? super K, ? super V, ? extends V> remappingFunction) {
1840 >    public V compute(K key,
1841 >                     BiFun<? super K, ? super V, ? extends V> remappingFunction) {
1842          if (key == null || remappingFunction == null)
1843              throw new NullPointerException();
1844 <        return (V)internalCompute(key, false, remappingFunction);
1844 >        int h = spread(key.hashCode());
1845 >        V val = null;
1846 >        int delta = 0;
1847 >        int binCount = 0;
1848 >        for (Node<K,V>[] tab = table;;) {
1849 >            Node<K,V> f; int n, i, fh;
1850 >            if (tab == null || (n = tab.length) == 0)
1851 >                tab = initTable();
1852 >            else if ((f = tabAt(tab, i = (n - 1) & h)) == null) {
1853 >                Node<K,V> r = new ReservationNode<K,V>();
1854 >                synchronized (r) {
1855 >                    if (casTabAt(tab, i, null, r)) {
1856 >                        binCount = 1;
1857 >                        Node<K,V> node = null;
1858 >                        try {
1859 >                            if ((val = remappingFunction.apply(key, null)) != null) {
1860 >                                delta = 1;
1861 >                                node = new Node<K,V>(h, key, val, null);
1862 >                            }
1863 >                        } finally {
1864 >                            setTabAt(tab, i, node);
1865 >                        }
1866 >                    }
1867 >                }
1868 >                if (binCount != 0)
1869 >                    break;
1870 >            }
1871 >            else if ((fh = f.hash) == MOVED)
1872 >                tab = helpTransfer(tab, f);
1873 >            else {
1874 >                synchronized (f) {
1875 >                    if (tabAt(tab, i) == f) {
1876 >                        if (fh >= 0) {
1877 >                            binCount = 1;
1878 >                            for (Node<K,V> e = f, pred = null;; ++binCount) {
1879 >                                K ek;
1880 >                                if (e.hash == h &&
1881 >                                    ((ek = e.key) == key ||
1882 >                                     (ek != null && key.equals(ek)))) {
1883 >                                    val = remappingFunction.apply(key, e.val);
1884 >                                    if (val != null)
1885 >                                        e.val = val;
1886 >                                    else {
1887 >                                        delta = -1;
1888 >                                        Node<K,V> en = e.next;
1889 >                                        if (pred != null)
1890 >                                            pred.next = en;
1891 >                                        else
1892 >                                            setTabAt(tab, i, en);
1893 >                                    }
1894 >                                    break;
1895 >                                }
1896 >                                pred = e;
1897 >                                if ((e = e.next) == null) {
1898 >                                    val = remappingFunction.apply(key, null);
1899 >                                    if (val != null) {
1900 >                                        delta = 1;
1901 >                                        pred.next =
1902 >                                            new Node<K,V>(h, key, val, null);
1903 >                                    }
1904 >                                    break;
1905 >                                }
1906 >                            }
1907 >                        }
1908 >                        else if (f instanceof TreeBin) {
1909 >                            binCount = 1;
1910 >                            TreeBin<K,V> t = (TreeBin<K,V>)f;
1911 >                            TreeNode<K,V> r, p;
1912 >                            if ((r = t.root) != null)
1913 >                                p = r.findTreeNode(h, key, null);
1914 >                            else
1915 >                                p = null;
1916 >                            V pv = (p == null) ? null : p.val;
1917 >                            val = remappingFunction.apply(key, pv);
1918 >                            if (val != null) {
1919 >                                if (p != null)
1920 >                                    p.val = val;
1921 >                                else {
1922 >                                    delta = 1;
1923 >                                    t.putTreeVal(h, key, val);
1924 >                                }
1925 >                            }
1926 >                            else if (p != null) {
1927 >                                delta = -1;
1928 >                                if (t.removeTreeNode(p))
1929 >                                    setTabAt(tab, i, untreeify(t.first));
1930 >                            }
1931 >                        }
1932 >                    }
1933 >                }
1934 >                if (binCount != 0) {
1935 >                    if (binCount >= TREEIFY_THRESHOLD)
1936 >                        treeifyBin(tab, i);
1937 >                    break;
1938 >                }
1939 >            }
1940 >        }
1941 >        if (delta != 0)
1942 >            addCount((long)delta, binCount);
1943 >        return val;
1944      }
1945  
1946      /**
1947 <     * If the specified key is not already associated
1948 <     * with a value, associate it with the given value.
1949 <     * Otherwise, replace the value with the results of
1950 <     * the given remapping function. This is equivalent to:
1951 <     *  <pre> {@code
1952 <     *   if (!map.containsKey(key))
1953 <     *     map.put(value);
1954 <     *   else {
1955 <     *     newValue = remappingFunction.apply(map.get(key), value);
1956 <     *     if (value != null)
1957 <     *       map.put(key, value);
1958 <     *     else
1959 <     *       map.remove(key);
1960 <     *   }
1961 <     * }</pre>
1962 <     * except that the action is performed atomically.  If the
1963 <     * function returns {@code null}, the mapping is removed.  If the
1964 <     * function itself throws an (unchecked) exception, the exception
2971 <     * is rethrown to its caller, and the current mapping is left
2972 <     * unchanged.  Some attempted update operations on this map by
2973 <     * other threads may be blocked while computation is in progress,
2974 <     * so the computation should be short and simple, and must not
2975 <     * attempt to update any other mappings of this Map.
1947 >     * If the specified key is not already associated with a
1948 >     * (non-null) value, associates it with the given value.
1949 >     * Otherwise, replaces the value with the results of the given
1950 >     * remapping function, or removes if {@code null}. The entire
1951 >     * method invocation is performed atomically.  Some attempted
1952 >     * update operations on this map by other threads may be blocked
1953 >     * while computation is in progress, so the computation should be
1954 >     * short and simple, and must not attempt to update any other
1955 >     * mappings of this Map.
1956 >     *
1957 >     * @param key key with which the specified value is to be associated
1958 >     * @param value the value to use if absent
1959 >     * @param remappingFunction the function to recompute a value if present
1960 >     * @return the new value associated with the specified key, or null if none
1961 >     * @throws NullPointerException if the specified key or the
1962 >     *         remappingFunction is null
1963 >     * @throws RuntimeException or Error if the remappingFunction does so,
1964 >     *         in which case the mapping is unchanged
1965       */
1966 <    @SuppressWarnings("unchecked") public V merge
2978 <        (K key, V value, BiFun<? super V, ? super V, ? extends V> remappingFunction) {
1966 >    public V merge(K key, V value, BiFun<? super V, ? super V, ? extends V> remappingFunction) {
1967          if (key == null || value == null || remappingFunction == null)
1968              throw new NullPointerException();
1969 <        return (V)internalMerge(key, value, remappingFunction);
1969 >        int h = spread(key.hashCode());
1970 >        V val = null;
1971 >        int delta = 0;
1972 >        int binCount = 0;
1973 >        for (Node<K,V>[] tab = table;;) {
1974 >            Node<K,V> f; int n, i, fh;
1975 >            if (tab == null || (n = tab.length) == 0)
1976 >                tab = initTable();
1977 >            else if ((f = tabAt(tab, i = (n - 1) & h)) == null) {
1978 >                if (casTabAt(tab, i, null, new Node<K,V>(h, key, value, null))) {
1979 >                    delta = 1;
1980 >                    val = value;
1981 >                    break;
1982 >                }
1983 >            }
1984 >            else if ((fh = f.hash) == MOVED)
1985 >                tab = helpTransfer(tab, f);
1986 >            else {
1987 >                synchronized (f) {
1988 >                    if (tabAt(tab, i) == f) {
1989 >                        if (fh >= 0) {
1990 >                            binCount = 1;
1991 >                            for (Node<K,V> e = f, pred = null;; ++binCount) {
1992 >                                K ek;
1993 >                                if (e.hash == h &&
1994 >                                    ((ek = e.key) == key ||
1995 >                                     (ek != null && key.equals(ek)))) {
1996 >                                    val = remappingFunction.apply(e.val, value);
1997 >                                    if (val != null)
1998 >                                        e.val = val;
1999 >                                    else {
2000 >                                        delta = -1;
2001 >                                        Node<K,V> en = e.next;
2002 >                                        if (pred != null)
2003 >                                            pred.next = en;
2004 >                                        else
2005 >                                            setTabAt(tab, i, en);
2006 >                                    }
2007 >                                    break;
2008 >                                }
2009 >                                pred = e;
2010 >                                if ((e = e.next) == null) {
2011 >                                    delta = 1;
2012 >                                    val = value;
2013 >                                    pred.next =
2014 >                                        new Node<K,V>(h, key, val, null);
2015 >                                    break;
2016 >                                }
2017 >                            }
2018 >                        }
2019 >                        else if (f instanceof TreeBin) {
2020 >                            binCount = 2;
2021 >                            TreeBin<K,V> t = (TreeBin<K,V>)f;
2022 >                            TreeNode<K,V> r = t.root;
2023 >                            TreeNode<K,V> p = (r == null) ? null :
2024 >                                r.findTreeNode(h, key, null);
2025 >                            val = (p == null) ? value :
2026 >                                remappingFunction.apply(p.val, value);
2027 >                            if (val != null) {
2028 >                                if (p != null)
2029 >                                    p.val = val;
2030 >                                else {
2031 >                                    delta = 1;
2032 >                                    t.putTreeVal(h, key, val);
2033 >                                }
2034 >                            }
2035 >                            else if (p != null) {
2036 >                                delta = -1;
2037 >                                if (t.removeTreeNode(p))
2038 >                                    setTabAt(tab, i, untreeify(t.first));
2039 >                            }
2040 >                        }
2041 >                    }
2042 >                }
2043 >                if (binCount != 0) {
2044 >                    if (binCount >= TREEIFY_THRESHOLD)
2045 >                        treeifyBin(tab, i);
2046 >                    break;
2047 >                }
2048 >            }
2049 >        }
2050 >        if (delta != 0)
2051 >            addCount((long)delta, binCount);
2052 >        return val;
2053      }
2054  
2055 +    // Hashtable legacy methods
2056 +
2057      /**
2058 <     * Removes the key (and its corresponding value) from this map.
2059 <     * This method does nothing if the key is not in the map.
2058 >     * Legacy method testing if some key maps into the specified value
2059 >     * in this table.  This method is identical in functionality to
2060 >     * {@link #containsValue(Object)}, and exists solely to ensure
2061 >     * full compatibility with class {@link java.util.Hashtable},
2062 >     * which supported this method prior to introduction of the
2063 >     * Java Collections framework.
2064       *
2065 <     * @param  key the key that needs to be removed
2066 <     * @return the previous value associated with {@code key}, or
2067 <     *         {@code null} if there was no mapping for {@code key}
2068 <     * @throws NullPointerException if the specified key is null
2065 >     * @param  value a value to search for
2066 >     * @return {@code true} if and only if some key maps to the
2067 >     *         {@code value} argument in this table as
2068 >     *         determined by the {@code equals} method;
2069 >     *         {@code false} otherwise
2070 >     * @throws NullPointerException if the specified value is null
2071       */
2072 <    @SuppressWarnings("unchecked") public V remove(Object key) {
2073 <        if (key == null)
2995 <            throw new NullPointerException();
2996 <        return (V)internalReplace(key, null, null);
2072 >    @Deprecated public boolean contains(Object value) {
2073 >        return containsValue(value);
2074      }
2075  
2076      /**
2077 <     * {@inheritDoc}
2077 >     * Returns an enumeration of the keys in this table.
2078       *
2079 <     * @throws NullPointerException if the specified key is null
2079 >     * @return an enumeration of the keys in this table
2080 >     * @see #keySet()
2081       */
2082 <    public boolean remove(Object key, Object value) {
2083 <        if (key == null)
2084 <            throw new NullPointerException();
2085 <        if (value == null)
3008 <            return false;
3009 <        return internalReplace(key, null, value) != null;
2082 >    public Enumeration<K> keys() {
2083 >        Node<K,V>[] t;
2084 >        int f = (t = table) == null ? 0 : t.length;
2085 >        return new KeyIterator<K,V>(t, f, 0, f, this);
2086      }
2087  
2088      /**
2089 <     * {@inheritDoc}
2089 >     * Returns an enumeration of the values in this table.
2090       *
2091 <     * @throws NullPointerException if any of the arguments are null
2091 >     * @return an enumeration of the values in this table
2092 >     * @see #values()
2093       */
2094 <    public boolean replace(K key, V oldValue, V newValue) {
2095 <        if (key == null || oldValue == null || newValue == null)
2096 <            throw new NullPointerException();
2097 <        return internalReplace(key, newValue, oldValue) != null;
2094 >    public Enumeration<V> elements() {
2095 >        Node<K,V>[] t;
2096 >        int f = (t = table) == null ? 0 : t.length;
2097 >        return new ValueIterator<K,V>(t, f, 0, f, this);
2098      }
2099  
2100 +    // ConcurrentHashMapV8-only methods
2101 +
2102      /**
2103 <     * {@inheritDoc}
2103 >     * Returns the number of mappings. This method should be used
2104 >     * instead of {@link #size} because a ConcurrentHashMapV8 may
2105 >     * contain more mappings than can be represented as an int. The
2106 >     * value returned is an estimate; the actual count may differ if
2107 >     * there are concurrent insertions or removals.
2108       *
2109 <     * @return the previous value associated with the specified key,
2110 <     *         or {@code null} if there was no mapping for the key
3028 <     * @throws NullPointerException if the specified key or value is null
2109 >     * @return the number of mappings
2110 >     * @since 1.8
2111       */
2112 <    @SuppressWarnings("unchecked") public V replace(K key, V value) {
2113 <        if (key == null || value == null)
2114 <            throw new NullPointerException();
3033 <        return (V)internalReplace(key, value, null);
2112 >    public long mappingCount() {
2113 >        long n = sumCount();
2114 >        return (n < 0L) ? 0L : n; // ignore transient negative values
2115      }
2116  
2117      /**
2118 <     * Removes all of the mappings from this map.
2118 >     * Creates a new {@link Set} backed by a ConcurrentHashMapV8
2119 >     * from the given type to {@code Boolean.TRUE}.
2120 >     *
2121 >     * @return the new set
2122 >     * @since 1.8
2123       */
2124 <    public void clear() {
2125 <        internalClear();
2124 >    public static <K> KeySetView<K,Boolean> newKeySet() {
2125 >        return new KeySetView<K,Boolean>
2126 >            (new ConcurrentHashMapV8<K,Boolean>(), Boolean.TRUE);
2127      }
2128  
2129      /**
2130 <     * Returns a {@link Set} view of the keys contained in this map.
2131 <     * The set is backed by the map, so changes to the map are
3046 <     * reflected in the set, and vice-versa.
2130 >     * Creates a new {@link Set} backed by a ConcurrentHashMapV8
2131 >     * from the given type to {@code Boolean.TRUE}.
2132       *
2133 <     * @return the set view
2133 >     * @param initialCapacity The implementation performs internal
2134 >     * sizing to accommodate this many elements.
2135 >     * @return the new set
2136 >     * @throws IllegalArgumentException if the initial capacity of
2137 >     * elements is negative
2138 >     * @since 1.8
2139       */
2140 <    public KeySetView<K,V> keySet() {
2141 <        KeySetView<K,V> ks = keySet;
2142 <        return (ks != null) ? ks : (keySet = new KeySetView<K,V>(this, null));
2140 >    public static <K> KeySetView<K,Boolean> newKeySet(int initialCapacity) {
2141 >        return new KeySetView<K,Boolean>
2142 >            (new ConcurrentHashMapV8<K,Boolean>(initialCapacity), Boolean.TRUE);
2143      }
2144  
2145      /**
2146       * Returns a {@link Set} view of the keys in this map, using the
2147       * given common mapped value for any additions (i.e., {@link
2148 <     * Collection#add} and {@link Collection#addAll}). This is of
2149 <     * course only appropriate if it is acceptable to use the same
2150 <     * value for all additions from this view.
2148 >     * Collection#add} and {@link Collection#addAll(Collection)}).
2149 >     * This is of course only appropriate if it is acceptable to use
2150 >     * the same value for all additions from this view.
2151       *
2152 <     * @param mappedValue the mapped value to use for any
3063 <     * additions.
2152 >     * @param mappedValue the mapped value to use for any additions
2153       * @return the set view
2154       * @throws NullPointerException if the mappedValue is null
2155       */
# Line 3070 | Line 2159 | public class ConcurrentHashMapV8<K, V>
2159          return new KeySetView<K,V>(this, mappedValue);
2160      }
2161  
2162 +    /* ---------------- Special Nodes -------------- */
2163 +
2164      /**
2165 <     * Returns a {@link Collection} view of the values contained in this map.
3075 <     * The collection is backed by the map, so changes to the map are
3076 <     * reflected in the collection, and vice-versa.
2165 >     * A node inserted at head of bins during transfer operations.
2166       */
2167 <    public ValuesView<K,V> values() {
2168 <        ValuesView<K,V> vs = values;
2169 <        return (vs != null) ? vs : (values = new ValuesView<K,V>(this));
2167 >    static final class ForwardingNode<K,V> extends Node<K,V> {
2168 >        final Node<K,V>[] nextTable;
2169 >        ForwardingNode(Node<K,V>[] tab) {
2170 >            super(MOVED, null, null, null);
2171 >            this.nextTable = tab;
2172 >        }
2173 >
2174 >        Node<K,V> find(int h, Object k) {
2175 >            // loop to avoid arbitrarily deep recursion on forwarding nodes
2176 >            outer: for (Node<K,V>[] tab = nextTable;;) {
2177 >                Node<K,V> e; int n;
2178 >                if (k == null || tab == null || (n = tab.length) == 0 ||
2179 >                    (e = tabAt(tab, (n - 1) & h)) == null)
2180 >                    return null;
2181 >                for (;;) {
2182 >                    int eh; K ek;
2183 >                    if ((eh = e.hash) == h &&
2184 >                        ((ek = e.key) == k || (ek != null && k.equals(ek))))
2185 >                        return e;
2186 >                    if (eh < 0) {
2187 >                        if (e instanceof ForwardingNode) {
2188 >                            tab = ((ForwardingNode<K,V>)e).nextTable;
2189 >                            continue outer;
2190 >                        }
2191 >                        else
2192 >                            return e.find(h, k);
2193 >                    }
2194 >                    if ((e = e.next) == null)
2195 >                        return null;
2196 >                }
2197 >            }
2198 >        }
2199      }
2200  
2201      /**
2202 <     * Returns a {@link Set} view of the mappings contained in this map.
3085 <     * The set is backed by the map, so changes to the map are
3086 <     * reflected in the set, and vice-versa.  The set supports element
3087 <     * removal, which removes the corresponding mapping from the map,
3088 <     * via the {@code Iterator.remove}, {@code Set.remove},
3089 <     * {@code removeAll}, {@code retainAll}, and {@code clear}
3090 <     * operations.  It does not support the {@code add} or
3091 <     * {@code addAll} operations.
3092 <     *
3093 <     * <p>The view's {@code iterator} is a "weakly consistent" iterator
3094 <     * that will never throw {@link ConcurrentModificationException},
3095 <     * and guarantees to traverse elements as they existed upon
3096 <     * construction of the iterator, and may (but is not guaranteed to)
3097 <     * reflect any modifications subsequent to construction.
2202 >     * A place-holder node used in computeIfAbsent and compute
2203       */
2204 <    public Set<Map.Entry<K,V>> entrySet() {
2205 <        EntrySetView<K,V> es = entrySet;
2206 <        return (es != null) ? es : (entrySet = new EntrySetView<K,V>(this));
2204 >    static final class ReservationNode<K,V> extends Node<K,V> {
2205 >        ReservationNode() {
2206 >            super(RESERVED, null, null, null);
2207 >        }
2208 >
2209 >        Node<K,V> find(int h, Object k) {
2210 >            return null;
2211 >        }
2212      }
2213  
2214 +    /* ---------------- Table Initialization and Resizing -------------- */
2215 +
2216      /**
2217 <     * Returns an enumeration of the keys in this table.
2218 <     *
3107 <     * @return an enumeration of the keys in this table
3108 <     * @see #keySet()
2217 >     * Returns the stamp bits for resizing a table of size n.
2218 >     * Must be negative when shifted left by RESIZE_STAMP_SHIFT.
2219       */
2220 <    public Enumeration<K> keys() {
2221 <        return new KeyIterator<K,V>(this);
2220 >    static final int resizeStamp(int n) {
2221 >        return Integer.numberOfLeadingZeros(n) | (1 << (RESIZE_STAMP_BITS - 1));
2222      }
2223  
2224      /**
2225 <     * Returns an enumeration of the values in this table.
3116 <     *
3117 <     * @return an enumeration of the values in this table
3118 <     * @see #values()
2225 >     * Initializes table, using the size recorded in sizeCtl.
2226       */
2227 <    public Enumeration<V> elements() {
2228 <        return new ValueIterator<K,V>(this);
2227 >    private final Node<K,V>[] initTable() {
2228 >        Node<K,V>[] tab; int sc;
2229 >        while ((tab = table) == null || tab.length == 0) {
2230 >            if ((sc = sizeCtl) < 0)
2231 >                Thread.yield(); // lost initialization race; just spin
2232 >            else if (U.compareAndSwapInt(this, SIZECTL, sc, -1)) {
2233 >                try {
2234 >                    if ((tab = table) == null || tab.length == 0) {
2235 >                        int n = (sc > 0) ? sc : DEFAULT_CAPACITY;
2236 >                        @SuppressWarnings("unchecked")
2237 >                        Node<K,V>[] nt = (Node<K,V>[])new Node<?,?>[n];
2238 >                        table = tab = nt;
2239 >                        sc = n - (n >>> 2);
2240 >                    }
2241 >                } finally {
2242 >                    sizeCtl = sc;
2243 >                }
2244 >                break;
2245 >            }
2246 >        }
2247 >        return tab;
2248      }
2249  
2250      /**
2251 <     * Returns a partitionable iterator of the keys in this map.
2252 <     *
2253 <     * @return a partitionable iterator of the keys in this map
2251 >     * Adds to count, and if table is too small and not already
2252 >     * resizing, initiates transfer. If already resizing, helps
2253 >     * perform transfer if work is available.  Rechecks occupancy
2254 >     * after a transfer to see if another resize is already needed
2255 >     * because resizings are lagging additions.
2256 >     *
2257 >     * @param x the count to add
2258 >     * @param check if <0, don't check resize, if <= 1 only check if uncontended
2259 >     */
2260 >    private final void addCount(long x, int check) {
2261 >        CounterCell[] as; long b, s;
2262 >        if ((as = counterCells) != null ||
2263 >            !U.compareAndSwapLong(this, BASECOUNT, b = baseCount, s = b + x)) {
2264 >            CounterHashCode hc; CounterCell a; long v; int m;
2265 >            boolean uncontended = true;
2266 >            if ((hc = threadCounterHashCode.get()) == null ||
2267 >                as == null || (m = as.length - 1) < 0 ||
2268 >                (a = as[m & hc.code]) == null ||
2269 >                !(uncontended =
2270 >                  U.compareAndSwapLong(a, CELLVALUE, v = a.value, v + x))) {
2271 >                fullAddCount(x, hc, uncontended);
2272 >                return;
2273 >            }
2274 >            if (check <= 1)
2275 >                return;
2276 >            s = sumCount();
2277 >        }
2278 >        if (check >= 0) {
2279 >            Node<K,V>[] tab, nt; int n, sc;
2280 >            while (s >= (long)(sc = sizeCtl) && (tab = table) != null &&
2281 >                   (n = tab.length) < MAXIMUM_CAPACITY) {
2282 >                int rs = resizeStamp(n);
2283 >                if (sc < 0) {
2284 >                    if ((sc >>> RESIZE_STAMP_SHIFT) != rs || sc == rs + 1 ||
2285 >                        sc == rs + MAX_RESIZERS || (nt = nextTable) == null ||
2286 >                        transferIndex <= 0)
2287 >                        break;
2288 >                    if (U.compareAndSwapInt(this, SIZECTL, sc, sc + 1))
2289 >                        transfer(tab, nt);
2290 >                }
2291 >                else if (U.compareAndSwapInt(this, SIZECTL, sc,
2292 >                                             (rs << RESIZE_STAMP_SHIFT) + 2))
2293 >                    transfer(tab, null);
2294 >                s = sumCount();
2295 >            }
2296 >        }
2297 >    }
2298 >
2299 >    /**
2300 >     * Helps transfer if a resize is in progress.
2301       */
2302 <    public Spliterator<K> keySpliterator() {
2303 <        return new KeyIterator<K,V>(this);
2302 >    final Node<K,V>[] helpTransfer(Node<K,V>[] tab, Node<K,V> f) {
2303 >        Node<K,V>[] nextTab; int sc;
2304 >        if (tab != null && (f instanceof ForwardingNode) &&
2305 >            (nextTab = ((ForwardingNode<K,V>)f).nextTable) != null) {
2306 >            int rs = resizeStamp(tab.length);
2307 >            while (nextTab == nextTable && table == tab &&
2308 >                   (sc = sizeCtl) < 0) {
2309 >                if ((sc >>> RESIZE_STAMP_SHIFT) != rs || sc == rs + 1 ||
2310 >                    sc == rs + MAX_RESIZERS || transferIndex <= 0)
2311 >                    break;
2312 >                if (U.compareAndSwapInt(this, SIZECTL, sc, sc + 1)) {
2313 >                    transfer(tab, nextTab);
2314 >                    break;
2315 >                }
2316 >            }
2317 >            return nextTab;
2318 >        }
2319 >        return table;
2320      }
2321  
2322      /**
2323 <     * Returns a partitionable iterator of the values in this map.
2323 >     * Tries to presize table to accommodate the given number of elements.
2324       *
2325 <     * @return a partitionable iterator of the values in this map
2325 >     * @param size number of elements (doesn't need to be perfectly accurate)
2326       */
2327 <    public Spliterator<V> valueSpliterator() {
2328 <        return new ValueIterator<K,V>(this);
2327 >    private final void tryPresize(int size) {
2328 >        int c = (size >= (MAXIMUM_CAPACITY >>> 1)) ? MAXIMUM_CAPACITY :
2329 >            tableSizeFor(size + (size >>> 1) + 1);
2330 >        int sc;
2331 >        while ((sc = sizeCtl) >= 0) {
2332 >            Node<K,V>[] tab = table; int n;
2333 >            if (tab == null || (n = tab.length) == 0) {
2334 >                n = (sc > c) ? sc : c;
2335 >                if (U.compareAndSwapInt(this, SIZECTL, sc, -1)) {
2336 >                    try {
2337 >                        if (table == tab) {
2338 >                            @SuppressWarnings("unchecked")
2339 >                            Node<K,V>[] nt = (Node<K,V>[])new Node<?,?>[n];
2340 >                            table = nt;
2341 >                            sc = n - (n >>> 2);
2342 >                        }
2343 >                    } finally {
2344 >                        sizeCtl = sc;
2345 >                    }
2346 >                }
2347 >            }
2348 >            else if (c <= sc || n >= MAXIMUM_CAPACITY)
2349 >                break;
2350 >            else if (tab == table) {
2351 >                int rs = resizeStamp(n);
2352 >                if (sc < 0) {
2353 >                    Node<K,V>[] nt;
2354 >                    if ((sc >>> RESIZE_STAMP_SHIFT) != rs || sc == rs + 1 ||
2355 >                        sc == rs + MAX_RESIZERS || (nt = nextTable) == null ||
2356 >                        transferIndex <= 0)
2357 >                        break;
2358 >                    if (U.compareAndSwapInt(this, SIZECTL, sc, sc + 1))
2359 >                        transfer(tab, nt);
2360 >                }
2361 >                else if (U.compareAndSwapInt(this, SIZECTL, sc,
2362 >                                             (rs << RESIZE_STAMP_SHIFT) + 2))
2363 >                    transfer(tab, null);
2364 >            }
2365 >        }
2366      }
2367  
2368      /**
2369 <     * Returns a partitionable iterator of the entries in this map.
2370 <     *
2371 <     * @return a partitionable iterator of the entries in this map
2369 >     * Moves and/or copies the nodes in each bin to new table. See
2370 >     * above for explanation.
2371 >     */
2372 >    private final void transfer(Node<K,V>[] tab, Node<K,V>[] nextTab) {
2373 >        int n = tab.length, stride;
2374 >        if ((stride = (NCPU > 1) ? (n >>> 3) / NCPU : n) < MIN_TRANSFER_STRIDE)
2375 >            stride = MIN_TRANSFER_STRIDE; // subdivide range
2376 >        if (nextTab == null) {            // initiating
2377 >            try {
2378 >                @SuppressWarnings("unchecked")
2379 >                Node<K,V>[] nt = (Node<K,V>[])new Node<?,?>[n << 1];
2380 >                nextTab = nt;
2381 >            } catch (Throwable ex) {      // try to cope with OOME
2382 >                sizeCtl = Integer.MAX_VALUE;
2383 >                return;
2384 >            }
2385 >            nextTable = nextTab;
2386 >            transferIndex = n;
2387 >        }
2388 >        int nextn = nextTab.length;
2389 >        ForwardingNode<K,V> fwd = new ForwardingNode<K,V>(nextTab);
2390 >        boolean advance = true;
2391 >        boolean finishing = false; // to ensure sweep before committing nextTab
2392 >        for (int i = 0, bound = 0;;) {
2393 >            Node<K,V> f; int fh;
2394 >            while (advance) {
2395 >                int nextIndex, nextBound;
2396 >                if (--i >= bound || finishing)
2397 >                    advance = false;
2398 >                else if ((nextIndex = transferIndex) <= 0) {
2399 >                    i = -1;
2400 >                    advance = false;
2401 >                }
2402 >                else if (U.compareAndSwapInt
2403 >                         (this, TRANSFERINDEX, nextIndex,
2404 >                          nextBound = (nextIndex > stride ?
2405 >                                       nextIndex - stride : 0))) {
2406 >                    bound = nextBound;
2407 >                    i = nextIndex - 1;
2408 >                    advance = false;
2409 >                }
2410 >            }
2411 >            if (i < 0 || i >= n || i + n >= nextn) {
2412 >                int sc;
2413 >                if (finishing) {
2414 >                    nextTable = null;
2415 >                    table = nextTab;
2416 >                    sizeCtl = (n << 1) - (n >>> 1);
2417 >                    return;
2418 >                }
2419 >                if (U.compareAndSwapInt(this, SIZECTL, sc = sizeCtl, sc - 1)) {
2420 >                    if ((sc - 2) != resizeStamp(n) << RESIZE_STAMP_SHIFT)
2421 >                        return;
2422 >                    finishing = advance = true;
2423 >                    i = n; // recheck before commit
2424 >                }
2425 >            }
2426 >            else if ((f = tabAt(tab, i)) == null)
2427 >                advance = casTabAt(tab, i, null, fwd);
2428 >            else if ((fh = f.hash) == MOVED)
2429 >                advance = true; // already processed
2430 >            else {
2431 >                synchronized (f) {
2432 >                    if (tabAt(tab, i) == f) {
2433 >                        Node<K,V> ln, hn;
2434 >                        if (fh >= 0) {
2435 >                            int runBit = fh & n;
2436 >                            Node<K,V> lastRun = f;
2437 >                            for (Node<K,V> p = f.next; p != null; p = p.next) {
2438 >                                int b = p.hash & n;
2439 >                                if (b != runBit) {
2440 >                                    runBit = b;
2441 >                                    lastRun = p;
2442 >                                }
2443 >                            }
2444 >                            if (runBit == 0) {
2445 >                                ln = lastRun;
2446 >                                hn = null;
2447 >                            }
2448 >                            else {
2449 >                                hn = lastRun;
2450 >                                ln = null;
2451 >                            }
2452 >                            for (Node<K,V> p = f; p != lastRun; p = p.next) {
2453 >                                int ph = p.hash; K pk = p.key; V pv = p.val;
2454 >                                if ((ph & n) == 0)
2455 >                                    ln = new Node<K,V>(ph, pk, pv, ln);
2456 >                                else
2457 >                                    hn = new Node<K,V>(ph, pk, pv, hn);
2458 >                            }
2459 >                            setTabAt(nextTab, i, ln);
2460 >                            setTabAt(nextTab, i + n, hn);
2461 >                            setTabAt(tab, i, fwd);
2462 >                            advance = true;
2463 >                        }
2464 >                        else if (f instanceof TreeBin) {
2465 >                            TreeBin<K,V> t = (TreeBin<K,V>)f;
2466 >                            TreeNode<K,V> lo = null, loTail = null;
2467 >                            TreeNode<K,V> hi = null, hiTail = null;
2468 >                            int lc = 0, hc = 0;
2469 >                            for (Node<K,V> e = t.first; e != null; e = e.next) {
2470 >                                int h = e.hash;
2471 >                                TreeNode<K,V> p = new TreeNode<K,V>
2472 >                                    (h, e.key, e.val, null, null);
2473 >                                if ((h & n) == 0) {
2474 >                                    if ((p.prev = loTail) == null)
2475 >                                        lo = p;
2476 >                                    else
2477 >                                        loTail.next = p;
2478 >                                    loTail = p;
2479 >                                    ++lc;
2480 >                                }
2481 >                                else {
2482 >                                    if ((p.prev = hiTail) == null)
2483 >                                        hi = p;
2484 >                                    else
2485 >                                        hiTail.next = p;
2486 >                                    hiTail = p;
2487 >                                    ++hc;
2488 >                                }
2489 >                            }
2490 >                            ln = (lc <= UNTREEIFY_THRESHOLD) ? untreeify(lo) :
2491 >                                (hc != 0) ? new TreeBin<K,V>(lo) : t;
2492 >                            hn = (hc <= UNTREEIFY_THRESHOLD) ? untreeify(hi) :
2493 >                                (lc != 0) ? new TreeBin<K,V>(hi) : t;
2494 >                            setTabAt(nextTab, i, ln);
2495 >                            setTabAt(nextTab, i + n, hn);
2496 >                            setTabAt(tab, i, fwd);
2497 >                            advance = true;
2498 >                        }
2499 >                    }
2500 >                }
2501 >            }
2502 >        }
2503 >    }
2504 >
2505 >    /* ---------------- Conversion from/to TreeBins -------------- */
2506 >
2507 >    /**
2508 >     * Replaces all linked nodes in bin at given index unless table is
2509 >     * too small, in which case resizes instead.
2510       */
2511 <    public Spliterator<Map.Entry<K,V>> entrySpliterator() {
2512 <        return new EntryIterator<K,V>(this);
2511 >    private final void treeifyBin(Node<K,V>[] tab, int index) {
2512 >        Node<K,V> b; int n, sc;
2513 >        if (tab != null) {
2514 >            if ((n = tab.length) < MIN_TREEIFY_CAPACITY)
2515 >                tryPresize(n << 1);
2516 >            else if ((b = tabAt(tab, index)) != null && b.hash >= 0) {
2517 >                synchronized (b) {
2518 >                    if (tabAt(tab, index) == b) {
2519 >                        TreeNode<K,V> hd = null, tl = null;
2520 >                        for (Node<K,V> e = b; e != null; e = e.next) {
2521 >                            TreeNode<K,V> p =
2522 >                                new TreeNode<K,V>(e.hash, e.key, e.val,
2523 >                                                  null, null);
2524 >                            if ((p.prev = tl) == null)
2525 >                                hd = p;
2526 >                            else
2527 >                                tl.next = p;
2528 >                            tl = p;
2529 >                        }
2530 >                        setTabAt(tab, index, new TreeBin<K,V>(hd));
2531 >                    }
2532 >                }
2533 >            }
2534 >        }
2535      }
2536  
2537      /**
2538 <     * Returns the hash code value for this {@link Map}, i.e.,
3153 <     * the sum of, for each key-value pair in the map,
3154 <     * {@code key.hashCode() ^ value.hashCode()}.
3155 <     *
3156 <     * @return the hash code value for this map
2538 >     * Returns a list on non-TreeNodes replacing those in given list.
2539       */
2540 <    public int hashCode() {
2541 <        int h = 0;
2542 <        Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
2543 <        Object v;
2544 <        while ((v = it.advance()) != null) {
2545 <            h += it.nextKey.hashCode() ^ v.hashCode();
2540 >    static <K,V> Node<K,V> untreeify(Node<K,V> b) {
2541 >        Node<K,V> hd = null, tl = null;
2542 >        for (Node<K,V> q = b; q != null; q = q.next) {
2543 >            Node<K,V> p = new Node<K,V>(q.hash, q.key, q.val, null);
2544 >            if (tl == null)
2545 >                hd = p;
2546 >            else
2547 >                tl.next = p;
2548 >            tl = p;
2549          }
2550 <        return h;
2550 >        return hd;
2551      }
2552  
2553 +    /* ---------------- TreeNodes -------------- */
2554 +
2555      /**
2556 <     * Returns a string representation of this map.  The string
3170 <     * representation consists of a list of key-value mappings (in no
3171 <     * particular order) enclosed in braces ("{@code {}}").  Adjacent
3172 <     * mappings are separated by the characters {@code ", "} (comma
3173 <     * and space).  Each key-value mapping is rendered as the key
3174 <     * followed by an equals sign ("{@code =}") followed by the
3175 <     * associated value.
3176 <     *
3177 <     * @return a string representation of this map
2556 >     * Nodes for use in TreeBins
2557       */
2558 <    public String toString() {
2559 <        Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
2560 <        StringBuilder sb = new StringBuilder();
2561 <        sb.append('{');
2562 <        Object v;
2563 <        if ((v = it.advance()) != null) {
2564 <            for (;;) {
2565 <                Object k = it.nextKey;
2566 <                sb.append(k == this ? "(this Map)" : k);
2567 <                sb.append('=');
2568 <                sb.append(v == this ? "(this Map)" : v);
2569 <                if ((v = it.advance()) == null)
2558 >    static final class TreeNode<K,V> extends Node<K,V> {
2559 >        TreeNode<K,V> parent;  // red-black tree links
2560 >        TreeNode<K,V> left;
2561 >        TreeNode<K,V> right;
2562 >        TreeNode<K,V> prev;    // needed to unlink next upon deletion
2563 >        boolean red;
2564 >
2565 >        TreeNode(int hash, K key, V val, Node<K,V> next,
2566 >                 TreeNode<K,V> parent) {
2567 >            super(hash, key, val, next);
2568 >            this.parent = parent;
2569 >        }
2570 >
2571 >        Node<K,V> find(int h, Object k) {
2572 >            return findTreeNode(h, k, null);
2573 >        }
2574 >
2575 >        /**
2576 >         * Returns the TreeNode (or null if not found) for the given key
2577 >         * starting at given root.
2578 >         */
2579 >        final TreeNode<K,V> findTreeNode(int h, Object k, Class<?> kc) {
2580 >            if (k != null) {
2581 >                TreeNode<K,V> p = this;
2582 >                do  {
2583 >                    int ph, dir; K pk; TreeNode<K,V> q;
2584 >                    TreeNode<K,V> pl = p.left, pr = p.right;
2585 >                    if ((ph = p.hash) > h)
2586 >                        p = pl;
2587 >                    else if (ph < h)
2588 >                        p = pr;
2589 >                    else if ((pk = p.key) == k || (pk != null && k.equals(pk)))
2590 >                        return p;
2591 >                    else if (pl == null)
2592 >                        p = pr;
2593 >                    else if (pr == null)
2594 >                        p = pl;
2595 >                    else if ((kc != null ||
2596 >                              (kc = comparableClassFor(k)) != null) &&
2597 >                             (dir = compareComparables(kc, k, pk)) != 0)
2598 >                        p = (dir < 0) ? pl : pr;
2599 >                    else if ((q = pr.findTreeNode(h, k, kc)) != null)
2600 >                        return q;
2601 >                    else
2602 >                        p = pl;
2603 >                } while (p != null);
2604 >            }
2605 >            return null;
2606 >        }
2607 >    }
2608 >
2609 >    /* ---------------- TreeBins -------------- */
2610 >
2611 >    /**
2612 >     * TreeNodes used at the heads of bins. TreeBins do not hold user
2613 >     * keys or values, but instead point to list of TreeNodes and
2614 >     * their root. They also maintain a parasitic read-write lock
2615 >     * forcing writers (who hold bin lock) to wait for readers (who do
2616 >     * not) to complete before tree restructuring operations.
2617 >     */
2618 >    static final class TreeBin<K,V> extends Node<K,V> {
2619 >        TreeNode<K,V> root;
2620 >        volatile TreeNode<K,V> first;
2621 >        volatile Thread waiter;
2622 >        volatile int lockState;
2623 >        // values for lockState
2624 >        static final int WRITER = 1; // set while holding write lock
2625 >        static final int WAITER = 2; // set when waiting for write lock
2626 >        static final int READER = 4; // increment value for setting read lock
2627 >
2628 >        /**
2629 >         * Tie-breaking utility for ordering insertions when equal
2630 >         * hashCodes and non-comparable. We don't require a total
2631 >         * order, just a consistent insertion rule to maintain
2632 >         * equivalence across rebalancings. Tie-breaking further than
2633 >         * necessary simplifies testing a bit.
2634 >         */
2635 >        static int tieBreakOrder(Object a, Object b) {
2636 >            int d;
2637 >            if (a == null || b == null ||
2638 >                (d = a.getClass().getName().
2639 >                 compareTo(b.getClass().getName())) == 0)
2640 >                d = (System.identityHashCode(a) <= System.identityHashCode(b) ?
2641 >                     -1 : 1);
2642 >            return d;
2643 >        }
2644 >
2645 >        /**
2646 >         * Creates bin with initial set of nodes headed by b.
2647 >         */
2648 >        TreeBin(TreeNode<K,V> b) {
2649 >            super(TREEBIN, null, null, null);
2650 >            this.first = b;
2651 >            TreeNode<K,V> r = null;
2652 >            for (TreeNode<K,V> x = b, next; x != null; x = next) {
2653 >                next = (TreeNode<K,V>)x.next;
2654 >                x.left = x.right = null;
2655 >                if (r == null) {
2656 >                    x.parent = null;
2657 >                    x.red = false;
2658 >                    r = x;
2659 >                }
2660 >                else {
2661 >                    K k = x.key;
2662 >                    int h = x.hash;
2663 >                    Class<?> kc = null;
2664 >                    for (TreeNode<K,V> p = r;;) {
2665 >                        int dir, ph;
2666 >                        K pk = p.key;
2667 >                        if ((ph = p.hash) > h)
2668 >                            dir = -1;
2669 >                        else if (ph < h)
2670 >                            dir = 1;
2671 >                        else if ((kc == null &&
2672 >                                  (kc = comparableClassFor(k)) == null) ||
2673 >                                 (dir = compareComparables(kc, k, pk)) == 0)
2674 >                            dir = tieBreakOrder(k, pk);
2675 >                            TreeNode<K,V> xp = p;
2676 >                        if ((p = (dir <= 0) ? p.left : p.right) == null) {
2677 >                            x.parent = xp;
2678 >                            if (dir <= 0)
2679 >                                xp.left = x;
2680 >                            else
2681 >                                xp.right = x;
2682 >                            r = balanceInsertion(r, x);
2683 >                            break;
2684 >                        }
2685 >                    }
2686 >                }
2687 >            }
2688 >            this.root = r;
2689 >            assert checkInvariants(root);
2690 >        }
2691 >
2692 >        /**
2693 >         * Acquires write lock for tree restructuring.
2694 >         */
2695 >        private final void lockRoot() {
2696 >            if (!U.compareAndSwapInt(this, LOCKSTATE, 0, WRITER))
2697 >                contendedLock(); // offload to separate method
2698 >        }
2699 >
2700 >        /**
2701 >         * Releases write lock for tree restructuring.
2702 >         */
2703 >        private final void unlockRoot() {
2704 >            lockState = 0;
2705 >        }
2706 >
2707 >        /**
2708 >         * Possibly blocks awaiting root lock.
2709 >         */
2710 >        private final void contendedLock() {
2711 >            boolean waiting = false;
2712 >            for (int s;;) {
2713 >                if (((s = lockState) & ~WAITER) == 0) {
2714 >                    if (U.compareAndSwapInt(this, LOCKSTATE, s, WRITER)) {
2715 >                        if (waiting)
2716 >                            waiter = null;
2717 >                        return;
2718 >                    }
2719 >                }
2720 >                else if ((s & WAITER) == 0) {
2721 >                    if (U.compareAndSwapInt(this, LOCKSTATE, s, s | WAITER)) {
2722 >                        waiting = true;
2723 >                        waiter = Thread.currentThread();
2724 >                    }
2725 >                }
2726 >                else if (waiting)
2727 >                    LockSupport.park(this);
2728 >            }
2729 >        }
2730 >
2731 >        /**
2732 >         * Returns matching node or null if none. Tries to search
2733 >         * using tree comparisons from root, but continues linear
2734 >         * search when lock not available.
2735 >         */
2736 >        final Node<K,V> find(int h, Object k) {
2737 >            if (k != null) {
2738 >                for (Node<K,V> e = first; e != null; ) {
2739 >                    int s; K ek;
2740 >                    if (((s = lockState) & (WAITER|WRITER)) != 0) {
2741 >                        if (e.hash == h &&
2742 >                            ((ek = e.key) == k || (ek != null && k.equals(ek))))
2743 >                            return e;
2744 >                        e = e.next;
2745 >                    }
2746 >                    else if (U.compareAndSwapInt(this, LOCKSTATE, s,
2747 >                                                 s + READER)) {
2748 >                        TreeNode<K,V> r, p;
2749 >                        try {
2750 >                            p = ((r = root) == null ? null :
2751 >                                 r.findTreeNode(h, k, null));
2752 >                        } finally {
2753 >                            Thread w;
2754 >                            int ls;
2755 >                            do {} while (!U.compareAndSwapInt
2756 >                                         (this, LOCKSTATE,
2757 >                                          ls = lockState, ls - READER));
2758 >                            if (ls == (READER|WAITER) && (w = waiter) != null)
2759 >                                LockSupport.unpark(w);
2760 >                        }
2761 >                        return p;
2762 >                    }
2763 >                }
2764 >            }
2765 >            return null;
2766 >        }
2767 >
2768 >        /**
2769 >         * Finds or adds a node.
2770 >         * @return null if added
2771 >         */
2772 >        final TreeNode<K,V> putTreeVal(int h, K k, V v) {
2773 >            Class<?> kc = null;
2774 >            boolean searched = false;
2775 >            for (TreeNode<K,V> p = root;;) {
2776 >                int dir, ph; K pk;
2777 >                if (p == null) {
2778 >                    first = root = new TreeNode<K,V>(h, k, v, null, null);
2779                      break;
2780 <                sb.append(',').append(' ');
2780 >                }
2781 >                else if ((ph = p.hash) > h)
2782 >                    dir = -1;
2783 >                else if (ph < h)
2784 >                    dir = 1;
2785 >                else if ((pk = p.key) == k || (pk != null && k.equals(pk)))
2786 >                    return p;
2787 >                else if ((kc == null &&
2788 >                          (kc = comparableClassFor(k)) == null) ||
2789 >                         (dir = compareComparables(kc, k, pk)) == 0) {
2790 >                    if (!searched) {
2791 >                        TreeNode<K,V> q, ch;
2792 >                        searched = true;
2793 >                        if (((ch = p.left) != null &&
2794 >                             (q = ch.findTreeNode(h, k, kc)) != null) ||
2795 >                            ((ch = p.right) != null &&
2796 >                             (q = ch.findTreeNode(h, k, kc)) != null))
2797 >                            return q;
2798 >                    }
2799 >                    dir = tieBreakOrder(k, pk);
2800 >                }
2801 >
2802 >                TreeNode<K,V> xp = p;
2803 >                if ((p = (dir <= 0) ? p.left : p.right) == null) {
2804 >                    TreeNode<K,V> x, f = first;
2805 >                    first = x = new TreeNode<K,V>(h, k, v, f, xp);
2806 >                    if (f != null)
2807 >                        f.prev = x;
2808 >                    if (dir <= 0)
2809 >                        xp.left = x;
2810 >                    else
2811 >                        xp.right = x;
2812 >                    if (!xp.red)
2813 >                        x.red = true;
2814 >                    else {
2815 >                        lockRoot();
2816 >                        try {
2817 >                            root = balanceInsertion(root, x);
2818 >                        } finally {
2819 >                            unlockRoot();
2820 >                        }
2821 >                    }
2822 >                    break;
2823 >                }
2824 >            }
2825 >            assert checkInvariants(root);
2826 >            return null;
2827 >        }
2828 >
2829 >        /**
2830 >         * Removes the given node, that must be present before this
2831 >         * call.  This is messier than typical red-black deletion code
2832 >         * because we cannot swap the contents of an interior node
2833 >         * with a leaf successor that is pinned by "next" pointers
2834 >         * that are accessible independently of lock. So instead we
2835 >         * swap the tree linkages.
2836 >         *
2837 >         * @return true if now too small, so should be untreeified
2838 >         */
2839 >        final boolean removeTreeNode(TreeNode<K,V> p) {
2840 >            TreeNode<K,V> next = (TreeNode<K,V>)p.next;
2841 >            TreeNode<K,V> pred = p.prev;  // unlink traversal pointers
2842 >            TreeNode<K,V> r, rl;
2843 >            if (pred == null)
2844 >                first = next;
2845 >            else
2846 >                pred.next = next;
2847 >            if (next != null)
2848 >                next.prev = pred;
2849 >            if (first == null) {
2850 >                root = null;
2851 >                return true;
2852 >            }
2853 >            if ((r = root) == null || r.right == null || // too small
2854 >                (rl = r.left) == null || rl.left == null)
2855 >                return true;
2856 >            lockRoot();
2857 >            try {
2858 >                TreeNode<K,V> replacement;
2859 >                TreeNode<K,V> pl = p.left;
2860 >                TreeNode<K,V> pr = p.right;
2861 >                if (pl != null && pr != null) {
2862 >                    TreeNode<K,V> s = pr, sl;
2863 >                    while ((sl = s.left) != null) // find successor
2864 >                        s = sl;
2865 >                    boolean c = s.red; s.red = p.red; p.red = c; // swap colors
2866 >                    TreeNode<K,V> sr = s.right;
2867 >                    TreeNode<K,V> pp = p.parent;
2868 >                    if (s == pr) { // p was s's direct parent
2869 >                        p.parent = s;
2870 >                        s.right = p;
2871 >                    }
2872 >                    else {
2873 >                        TreeNode<K,V> sp = s.parent;
2874 >                        if ((p.parent = sp) != null) {
2875 >                            if (s == sp.left)
2876 >                                sp.left = p;
2877 >                            else
2878 >                                sp.right = p;
2879 >                        }
2880 >                        if ((s.right = pr) != null)
2881 >                            pr.parent = s;
2882 >                    }
2883 >                    p.left = null;
2884 >                    if ((p.right = sr) != null)
2885 >                        sr.parent = p;
2886 >                    if ((s.left = pl) != null)
2887 >                        pl.parent = s;
2888 >                    if ((s.parent = pp) == null)
2889 >                        r = s;
2890 >                    else if (p == pp.left)
2891 >                        pp.left = s;
2892 >                    else
2893 >                        pp.right = s;
2894 >                    if (sr != null)
2895 >                        replacement = sr;
2896 >                    else
2897 >                        replacement = p;
2898 >                }
2899 >                else if (pl != null)
2900 >                    replacement = pl;
2901 >                else if (pr != null)
2902 >                    replacement = pr;
2903 >                else
2904 >                    replacement = p;
2905 >                if (replacement != p) {
2906 >                    TreeNode<K,V> pp = replacement.parent = p.parent;
2907 >                    if (pp == null)
2908 >                        r = replacement;
2909 >                    else if (p == pp.left)
2910 >                        pp.left = replacement;
2911 >                    else
2912 >                        pp.right = replacement;
2913 >                    p.left = p.right = p.parent = null;
2914 >                }
2915 >
2916 >                root = (p.red) ? r : balanceDeletion(r, replacement);
2917 >
2918 >                if (p == replacement) {  // detach pointers
2919 >                    TreeNode<K,V> pp;
2920 >                    if ((pp = p.parent) != null) {
2921 >                        if (p == pp.left)
2922 >                            pp.left = null;
2923 >                        else if (p == pp.right)
2924 >                            pp.right = null;
2925 >                        p.parent = null;
2926 >                    }
2927 >                }
2928 >            } finally {
2929 >                unlockRoot();
2930 >            }
2931 >            assert checkInvariants(root);
2932 >            return false;
2933 >        }
2934 >
2935 >        /* ------------------------------------------------------------ */
2936 >        // Red-black tree methods, all adapted from CLR
2937 >
2938 >        static <K,V> TreeNode<K,V> rotateLeft(TreeNode<K,V> root,
2939 >                                              TreeNode<K,V> p) {
2940 >            TreeNode<K,V> r, pp, rl;
2941 >            if (p != null && (r = p.right) != null) {
2942 >                if ((rl = p.right = r.left) != null)
2943 >                    rl.parent = p;
2944 >                if ((pp = r.parent = p.parent) == null)
2945 >                    (root = r).red = false;
2946 >                else if (pp.left == p)
2947 >                    pp.left = r;
2948 >                else
2949 >                    pp.right = r;
2950 >                r.left = p;
2951 >                p.parent = r;
2952 >            }
2953 >            return root;
2954 >        }
2955 >
2956 >        static <K,V> TreeNode<K,V> rotateRight(TreeNode<K,V> root,
2957 >                                               TreeNode<K,V> p) {
2958 >            TreeNode<K,V> l, pp, lr;
2959 >            if (p != null && (l = p.left) != null) {
2960 >                if ((lr = p.left = l.right) != null)
2961 >                    lr.parent = p;
2962 >                if ((pp = l.parent = p.parent) == null)
2963 >                    (root = l).red = false;
2964 >                else if (pp.right == p)
2965 >                    pp.right = l;
2966 >                else
2967 >                    pp.left = l;
2968 >                l.right = p;
2969 >                p.parent = l;
2970 >            }
2971 >            return root;
2972 >        }
2973 >
2974 >        static <K,V> TreeNode<K,V> balanceInsertion(TreeNode<K,V> root,
2975 >                                                    TreeNode<K,V> x) {
2976 >            x.red = true;
2977 >            for (TreeNode<K,V> xp, xpp, xppl, xppr;;) {
2978 >                if ((xp = x.parent) == null) {
2979 >                    x.red = false;
2980 >                    return x;
2981 >                }
2982 >                else if (!xp.red || (xpp = xp.parent) == null)
2983 >                    return root;
2984 >                if (xp == (xppl = xpp.left)) {
2985 >                    if ((xppr = xpp.right) != null && xppr.red) {
2986 >                        xppr.red = false;
2987 >                        xp.red = false;
2988 >                        xpp.red = true;
2989 >                        x = xpp;
2990 >                    }
2991 >                    else {
2992 >                        if (x == xp.right) {
2993 >                            root = rotateLeft(root, x = xp);
2994 >                            xpp = (xp = x.parent) == null ? null : xp.parent;
2995 >                        }
2996 >                        if (xp != null) {
2997 >                            xp.red = false;
2998 >                            if (xpp != null) {
2999 >                                xpp.red = true;
3000 >                                root = rotateRight(root, xpp);
3001 >                            }
3002 >                        }
3003 >                    }
3004 >                }
3005 >                else {
3006 >                    if (xppl != null && xppl.red) {
3007 >                        xppl.red = false;
3008 >                        xp.red = false;
3009 >                        xpp.red = true;
3010 >                        x = xpp;
3011 >                    }
3012 >                    else {
3013 >                        if (x == xp.left) {
3014 >                            root = rotateRight(root, x = xp);
3015 >                            xpp = (xp = x.parent) == null ? null : xp.parent;
3016 >                        }
3017 >                        if (xp != null) {
3018 >                            xp.red = false;
3019 >                            if (xpp != null) {
3020 >                                xpp.red = true;
3021 >                                root = rotateLeft(root, xpp);
3022 >                            }
3023 >                        }
3024 >                    }
3025 >                }
3026 >            }
3027 >        }
3028 >
3029 >        static <K,V> TreeNode<K,V> balanceDeletion(TreeNode<K,V> root,
3030 >                                                   TreeNode<K,V> x) {
3031 >            for (TreeNode<K,V> xp, xpl, xpr;;)  {
3032 >                if (x == null || x == root)
3033 >                    return root;
3034 >                else if ((xp = x.parent) == null) {
3035 >                    x.red = false;
3036 >                    return x;
3037 >                }
3038 >                else if (x.red) {
3039 >                    x.red = false;
3040 >                    return root;
3041 >                }
3042 >                else if ((xpl = xp.left) == x) {
3043 >                    if ((xpr = xp.right) != null && xpr.red) {
3044 >                        xpr.red = false;
3045 >                        xp.red = true;
3046 >                        root = rotateLeft(root, xp);
3047 >                        xpr = (xp = x.parent) == null ? null : xp.right;
3048 >                    }
3049 >                    if (xpr == null)
3050 >                        x = xp;
3051 >                    else {
3052 >                        TreeNode<K,V> sl = xpr.left, sr = xpr.right;
3053 >                        if ((sr == null || !sr.red) &&
3054 >                            (sl == null || !sl.red)) {
3055 >                            xpr.red = true;
3056 >                            x = xp;
3057 >                        }
3058 >                        else {
3059 >                            if (sr == null || !sr.red) {
3060 >                                if (sl != null)
3061 >                                    sl.red = false;
3062 >                                xpr.red = true;
3063 >                                root = rotateRight(root, xpr);
3064 >                                xpr = (xp = x.parent) == null ?
3065 >                                    null : xp.right;
3066 >                            }
3067 >                            if (xpr != null) {
3068 >                                xpr.red = (xp == null) ? false : xp.red;
3069 >                                if ((sr = xpr.right) != null)
3070 >                                    sr.red = false;
3071 >                            }
3072 >                            if (xp != null) {
3073 >                                xp.red = false;
3074 >                                root = rotateLeft(root, xp);
3075 >                            }
3076 >                            x = root;
3077 >                        }
3078 >                    }
3079 >                }
3080 >                else { // symmetric
3081 >                    if (xpl != null && xpl.red) {
3082 >                        xpl.red = false;
3083 >                        xp.red = true;
3084 >                        root = rotateRight(root, xp);
3085 >                        xpl = (xp = x.parent) == null ? null : xp.left;
3086 >                    }
3087 >                    if (xpl == null)
3088 >                        x = xp;
3089 >                    else {
3090 >                        TreeNode<K,V> sl = xpl.left, sr = xpl.right;
3091 >                        if ((sl == null || !sl.red) &&
3092 >                            (sr == null || !sr.red)) {
3093 >                            xpl.red = true;
3094 >                            x = xp;
3095 >                        }
3096 >                        else {
3097 >                            if (sl == null || !sl.red) {
3098 >                                if (sr != null)
3099 >                                    sr.red = false;
3100 >                                xpl.red = true;
3101 >                                root = rotateLeft(root, xpl);
3102 >                                xpl = (xp = x.parent) == null ?
3103 >                                    null : xp.left;
3104 >                            }
3105 >                            if (xpl != null) {
3106 >                                xpl.red = (xp == null) ? false : xp.red;
3107 >                                if ((sl = xpl.left) != null)
3108 >                                    sl.red = false;
3109 >                            }
3110 >                            if (xp != null) {
3111 >                                xp.red = false;
3112 >                                root = rotateRight(root, xp);
3113 >                            }
3114 >                            x = root;
3115 >                        }
3116 >                    }
3117 >                }
3118 >            }
3119 >        }
3120 >
3121 >        /**
3122 >         * Recursive invariant check
3123 >         */
3124 >        static <K,V> boolean checkInvariants(TreeNode<K,V> t) {
3125 >            TreeNode<K,V> tp = t.parent, tl = t.left, tr = t.right,
3126 >                tb = t.prev, tn = (TreeNode<K,V>)t.next;
3127 >            if (tb != null && tb.next != t)
3128 >                return false;
3129 >            if (tn != null && tn.prev != t)
3130 >                return false;
3131 >            if (tp != null && t != tp.left && t != tp.right)
3132 >                return false;
3133 >            if (tl != null && (tl.parent != t || tl.hash > t.hash))
3134 >                return false;
3135 >            if (tr != null && (tr.parent != t || tr.hash < t.hash))
3136 >                return false;
3137 >            if (t.red && tl != null && tl.red && tr != null && tr.red)
3138 >                return false;
3139 >            if (tl != null && !checkInvariants(tl))
3140 >                return false;
3141 >            if (tr != null && !checkInvariants(tr))
3142 >                return false;
3143 >            return true;
3144 >        }
3145 >
3146 >        private static final sun.misc.Unsafe U;
3147 >        private static final long LOCKSTATE;
3148 >        static {
3149 >            try {
3150 >                U = getUnsafe();
3151 >                Class<?> k = TreeBin.class;
3152 >                LOCKSTATE = U.objectFieldOffset
3153 >                    (k.getDeclaredField("lockState"));
3154 >            } catch (Exception e) {
3155 >                throw new Error(e);
3156              }
3157          }
3195        return sb.append('}').toString();
3158      }
3159  
3160 +    /* ----------------Table Traversal -------------- */
3161 +
3162      /**
3163 <     * Compares the specified object with this map for equality.
3164 <     * Returns {@code true} if the given object is a map with the same
3165 <     * mappings as this map.  This operation may return misleading
3166 <     * results if either map is concurrently modified during execution
3167 <     * of this method.
3163 >     * Records the table, its length, and current traversal index for a
3164 >     * traverser that must process a region of a forwarded table before
3165 >     * proceeding with current table.
3166 >     */
3167 >    static final class TableStack<K,V> {
3168 >        int length;
3169 >        int index;
3170 >        Node<K,V>[] tab;
3171 >        TableStack<K,V> next;
3172 >    }
3173 >
3174 >    /**
3175 >     * Encapsulates traversal for methods such as containsValue; also
3176 >     * serves as a base class for other iterators and spliterators.
3177       *
3178 <     * @param o object to be compared for equality with this map
3179 <     * @return {@code true} if the specified object is equal to this map
3178 >     * Method advance visits once each still-valid node that was
3179 >     * reachable upon iterator construction. It might miss some that
3180 >     * were added to a bin after the bin was visited, which is OK wrt
3181 >     * consistency guarantees. Maintaining this property in the face
3182 >     * of possible ongoing resizes requires a fair amount of
3183 >     * bookkeeping state that is difficult to optimize away amidst
3184 >     * volatile accesses.  Even so, traversal maintains reasonable
3185 >     * throughput.
3186 >     *
3187 >     * Normally, iteration proceeds bin-by-bin traversing lists.
3188 >     * However, if the table has been resized, then all future steps
3189 >     * must traverse both the bin at the current index as well as at
3190 >     * (index + baseSize); and so on for further resizings. To
3191 >     * paranoically cope with potential sharing by users of iterators
3192 >     * across threads, iteration terminates if a bounds checks fails
3193 >     * for a table read.
3194       */
3195 <    public boolean equals(Object o) {
3196 <        if (o != this) {
3197 <            if (!(o instanceof Map))
3198 <                return false;
3199 <            Map<?,?> m = (Map<?,?>) o;
3200 <            Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
3201 <            Object val;
3202 <            while ((val = it.advance()) != null) {
3203 <                Object v = m.get(it.nextKey);
3204 <                if (v == null || (v != val && !v.equals(val)))
3205 <                    return false;
3195 >    static class Traverser<K,V> {
3196 >        Node<K,V>[] tab;        // current table; updated if resized
3197 >        Node<K,V> next;         // the next entry to use
3198 >        TableStack<K,V> stack, spare; // to save/restore on ForwardingNodes
3199 >        int index;              // index of bin to use next
3200 >        int baseIndex;          // current index of initial table
3201 >        int baseLimit;          // index bound for initial table
3202 >        final int baseSize;     // initial table size
3203 >
3204 >        Traverser(Node<K,V>[] tab, int size, int index, int limit) {
3205 >            this.tab = tab;
3206 >            this.baseSize = size;
3207 >            this.baseIndex = this.index = index;
3208 >            this.baseLimit = limit;
3209 >            this.next = null;
3210 >        }
3211 >
3212 >        /**
3213 >         * Advances if possible, returning next valid node, or null if none.
3214 >         */
3215 >        final Node<K,V> advance() {
3216 >            Node<K,V> e;
3217 >            if ((e = next) != null)
3218 >                e = e.next;
3219 >            for (;;) {
3220 >                Node<K,V>[] t; int i, n;  // must use locals in checks
3221 >                if (e != null)
3222 >                    return next = e;
3223 >                if (baseIndex >= baseLimit || (t = tab) == null ||
3224 >                    (n = t.length) <= (i = index) || i < 0)
3225 >                    return next = null;
3226 >                if ((e = tabAt(t, i)) != null && e.hash < 0) {
3227 >                    if (e instanceof ForwardingNode) {
3228 >                        tab = ((ForwardingNode<K,V>)e).nextTable;
3229 >                        e = null;
3230 >                        pushState(t, i, n);
3231 >                        continue;
3232 >                    }
3233 >                    else if (e instanceof TreeBin)
3234 >                        e = ((TreeBin<K,V>)e).first;
3235 >                    else
3236 >                        e = null;
3237 >                }
3238 >                if (stack != null)
3239 >                    recoverState(n);
3240 >                else if ((index = i + baseSize) >= n)
3241 >                    index = ++baseIndex; // visit upper slots if present
3242              }
3243 <            for (Map.Entry<?,?> e : m.entrySet()) {
3244 <                Object mk, mv, v;
3245 <                if ((mk = e.getKey()) == null ||
3246 <                    (mv = e.getValue()) == null ||
3247 <                    (v = internalGet(mk)) == null ||
3248 <                    (mv != v && !mv.equals(v)))
3249 <                    return false;
3243 >        }
3244 >
3245 >        /**
3246 >         * Saves traversal state upon encountering a forwarding node.
3247 >         */
3248 >        private void pushState(Node<K,V>[] t, int i, int n) {
3249 >            TableStack<K,V> s = spare;  // reuse if possible
3250 >            if (s != null)
3251 >                spare = s.next;
3252 >            else
3253 >                s = new TableStack<K,V>();
3254 >            s.tab = t;
3255 >            s.length = n;
3256 >            s.index = i;
3257 >            s.next = stack;
3258 >            stack = s;
3259 >        }
3260 >
3261 >        /**
3262 >         * Possibly pops traversal state.
3263 >         *
3264 >         * @param n length of current table
3265 >         */
3266 >        private void recoverState(int n) {
3267 >            TableStack<K,V> s; int len;
3268 >            while ((s = stack) != null && (index += (len = s.length)) >= n) {
3269 >                n = len;
3270 >                index = s.index;
3271 >                tab = s.tab;
3272 >                s.tab = null;
3273 >                TableStack<K,V> next = s.next;
3274 >                s.next = spare; // save for reuse
3275 >                stack = next;
3276 >                spare = s;
3277              }
3278 +            if (s == null && (index += baseSize) >= n)
3279 +                index = ++baseIndex;
3280          }
3229        return true;
3281      }
3282  
3283 <    /* ----------------Iterators -------------- */
3284 <
3285 <    @SuppressWarnings("serial") static final class KeyIterator<K,V> extends Traverser<K,V,Object>
3286 <        implements Spliterator<K>, Enumeration<K> {
3287 <        KeyIterator(ConcurrentHashMapV8<K, V> map) { super(map); }
3288 <        KeyIterator(ConcurrentHashMapV8<K, V> map, Traverser<K,V,Object> it) {
3289 <            super(map, it, -1);
3283 >    /**
3284 >     * Base of key, value, and entry Iterators. Adds fields to
3285 >     * Traverser to support iterator.remove.
3286 >     */
3287 >    static class BaseIterator<K,V> extends Traverser<K,V> {
3288 >        final ConcurrentHashMapV8<K,V> map;
3289 >        Node<K,V> lastReturned;
3290 >        BaseIterator(Node<K,V>[] tab, int size, int index, int limit,
3291 >                    ConcurrentHashMapV8<K,V> map) {
3292 >            super(tab, size, index, limit);
3293 >            this.map = map;
3294 >            advance();
3295          }
3296 <        public KeyIterator<K,V> split() {
3297 <            if (nextKey != null)
3296 >
3297 >        public final boolean hasNext() { return next != null; }
3298 >        public final boolean hasMoreElements() { return next != null; }
3299 >
3300 >        public final void remove() {
3301 >            Node<K,V> p;
3302 >            if ((p = lastReturned) == null)
3303                  throw new IllegalStateException();
3304 <            return new KeyIterator<K,V>(map, this);
3304 >            lastReturned = null;
3305 >            map.replaceNode(p.key, null, null);
3306          }
3307 <        @SuppressWarnings("unchecked") public final K next() {
3308 <            if (nextVal == null && advance() == null)
3307 >    }
3308 >
3309 >    static final class KeyIterator<K,V> extends BaseIterator<K,V>
3310 >        implements Iterator<K>, Enumeration<K> {
3311 >        KeyIterator(Node<K,V>[] tab, int index, int size, int limit,
3312 >                    ConcurrentHashMapV8<K,V> map) {
3313 >            super(tab, index, size, limit, map);
3314 >        }
3315 >
3316 >        public final K next() {
3317 >            Node<K,V> p;
3318 >            if ((p = next) == null)
3319                  throw new NoSuchElementException();
3320 <            Object k = nextKey;
3321 <            nextVal = null;
3322 <            return (K) k;
3320 >            K k = p.key;
3321 >            lastReturned = p;
3322 >            advance();
3323 >            return k;
3324          }
3325  
3326          public final K nextElement() { return next(); }
3327      }
3328  
3329 <    @SuppressWarnings("serial") static final class ValueIterator<K,V> extends Traverser<K,V,Object>
3330 <        implements Spliterator<V>, Enumeration<V> {
3331 <        ValueIterator(ConcurrentHashMapV8<K, V> map) { super(map); }
3332 <        ValueIterator(ConcurrentHashMapV8<K, V> map, Traverser<K,V,Object> it) {
3333 <            super(map, it, -1);
3261 <        }
3262 <        public ValueIterator<K,V> split() {
3263 <            if (nextKey != null)
3264 <                throw new IllegalStateException();
3265 <            return new ValueIterator<K,V>(map, this);
3329 >    static final class ValueIterator<K,V> extends BaseIterator<K,V>
3330 >        implements Iterator<V>, Enumeration<V> {
3331 >        ValueIterator(Node<K,V>[] tab, int index, int size, int limit,
3332 >                      ConcurrentHashMapV8<K,V> map) {
3333 >            super(tab, index, size, limit, map);
3334          }
3335  
3336 <        @SuppressWarnings("unchecked") public final V next() {
3337 <            Object v;
3338 <            if ((v = nextVal) == null && (v = advance()) == null)
3336 >        public final V next() {
3337 >            Node<K,V> p;
3338 >            if ((p = next) == null)
3339                  throw new NoSuchElementException();
3340 <            nextVal = null;
3341 <            return (V) v;
3340 >            V v = p.val;
3341 >            lastReturned = p;
3342 >            advance();
3343 >            return v;
3344          }
3345  
3346          public final V nextElement() { return next(); }
3347      }
3348  
3349 <    @SuppressWarnings("serial") static final class EntryIterator<K,V> extends Traverser<K,V,Object>
3350 <        implements Spliterator<Map.Entry<K,V>> {
3351 <        EntryIterator(ConcurrentHashMapV8<K, V> map) { super(map); }
3352 <        EntryIterator(ConcurrentHashMapV8<K, V> map, Traverser<K,V,Object> it) {
3353 <            super(map, it, -1);
3284 <        }
3285 <        public EntryIterator<K,V> split() {
3286 <            if (nextKey != null)
3287 <                throw new IllegalStateException();
3288 <            return new EntryIterator<K,V>(map, this);
3349 >    static final class EntryIterator<K,V> extends BaseIterator<K,V>
3350 >        implements Iterator<Map.Entry<K,V>> {
3351 >        EntryIterator(Node<K,V>[] tab, int index, int size, int limit,
3352 >                      ConcurrentHashMapV8<K,V> map) {
3353 >            super(tab, index, size, limit, map);
3354          }
3355  
3356 <        @SuppressWarnings("unchecked") public final Map.Entry<K,V> next() {
3357 <            Object v;
3358 <            if ((v = nextVal) == null && (v = advance()) == null)
3356 >        public final Map.Entry<K,V> next() {
3357 >            Node<K,V> p;
3358 >            if ((p = next) == null)
3359                  throw new NoSuchElementException();
3360 <            Object k = nextKey;
3361 <            nextVal = null;
3362 <            return new MapEntry<K,V>((K)k, (V)v, map);
3360 >            K k = p.key;
3361 >            V v = p.val;
3362 >            lastReturned = p;
3363 >            advance();
3364 >            return new MapEntry<K,V>(k, v, map);
3365          }
3366      }
3367  
3368      /**
3369 <     * Exported Entry for iterators
3369 >     * Exported Entry for EntryIterator
3370       */
3371 <    static final class MapEntry<K,V> implements Map.Entry<K, V> {
3371 >    static final class MapEntry<K,V> implements Map.Entry<K,V> {
3372          final K key; // non-null
3373          V val;       // non-null
3374 <        final ConcurrentHashMapV8<K, V> map;
3375 <        MapEntry(K key, V val, ConcurrentHashMapV8<K, V> map) {
3374 >        final ConcurrentHashMapV8<K,V> map;
3375 >        MapEntry(K key, V val, ConcurrentHashMapV8<K,V> map) {
3376              this.key = key;
3377              this.val = val;
3378              this.map = map;
3379          }
3380 <        public final K getKey()       { return key; }
3381 <        public final V getValue()     { return val; }
3382 <        public final int hashCode()   { return key.hashCode() ^ val.hashCode(); }
3383 <        public final String toString(){ return key + "=" + val; }
3380 >        public K getKey()        { return key; }
3381 >        public V getValue()      { return val; }
3382 >        public int hashCode()    { return key.hashCode() ^ val.hashCode(); }
3383 >        public String toString() { return key + "=" + val; }
3384  
3385 <        public final boolean equals(Object o) {
3385 >        public boolean equals(Object o) {
3386              Object k, v; Map.Entry<?,?> e;
3387              return ((o instanceof Map.Entry) &&
3388                      (k = (e = (Map.Entry<?,?>)o).getKey()) != null &&
# Line 3329 | Line 3396 | public class ConcurrentHashMapV8<K, V>
3396           * value to return is somewhat arbitrary here. Since we do not
3397           * necessarily track asynchronous changes, the most recent
3398           * "previous" value could be different from what we return (or
3399 <         * could even have been removed in which case the put will
3399 >         * could even have been removed, in which case the put will
3400           * re-establish). We do not and cannot guarantee more.
3401           */
3402 <        public final V setValue(V value) {
3402 >        public V setValue(V value) {
3403              if (value == null) throw new NullPointerException();
3404              V v = val;
3405              val = value;
# Line 3341 | Line 3408 | public class ConcurrentHashMapV8<K, V>
3408          }
3409      }
3410  
3411 <    /**
3412 <     * Returns exportable snapshot entry for the given key and value
3413 <     * when write-through can't or shouldn't be used.
3414 <     */
3415 <    static <K,V> AbstractMap.SimpleEntry<K,V> entryFor(K k, V v) {
3416 <        return new AbstractMap.SimpleEntry<K,V>(k, v);
3417 <    }
3411 >    static final class KeySpliterator<K,V> extends Traverser<K,V>
3412 >        implements ConcurrentHashMapSpliterator<K> {
3413 >        long est;               // size estimate
3414 >        KeySpliterator(Node<K,V>[] tab, int size, int index, int limit,
3415 >                       long est) {
3416 >            super(tab, size, index, limit);
3417 >            this.est = est;
3418 >        }
3419 >
3420 >        public ConcurrentHashMapSpliterator<K> trySplit() {
3421 >            int i, f, h;
3422 >            return (h = ((i = baseIndex) + (f = baseLimit)) >>> 1) <= i ? null :
3423 >                new KeySpliterator<K,V>(tab, baseSize, baseLimit = h,
3424 >                                        f, est >>>= 1);
3425 >        }
3426  
3427 <    /* ---------------- Serialization Support -------------- */
3427 >        public void forEachRemaining(Action<? super K> action) {
3428 >            if (action == null) throw new NullPointerException();
3429 >            for (Node<K,V> p; (p = advance()) != null;)
3430 >                action.apply(p.key);
3431 >        }
3432 >
3433 >        public boolean tryAdvance(Action<? super K> action) {
3434 >            if (action == null) throw new NullPointerException();
3435 >            Node<K,V> p;
3436 >            if ((p = advance()) == null)
3437 >                return false;
3438 >            action.apply(p.key);
3439 >            return true;
3440 >        }
3441 >
3442 >        public long estimateSize() { return est; }
3443  
3354    /**
3355     * Stripped-down version of helper class used in previous version,
3356     * declared for the sake of serialization compatibility
3357     */
3358    static class Segment<K,V> implements Serializable {
3359        private static final long serialVersionUID = 2249069246763182397L;
3360        final float loadFactor;
3361        Segment(float lf) { this.loadFactor = lf; }
3444      }
3445  
3446 <    /**
3447 <     * Saves the state of the {@code ConcurrentHashMapV8} instance to a
3448 <     * stream (i.e., serializes it).
3449 <     * @param s the stream
3450 <     * @serialData
3451 <     * the key (Object) and value (Object)
3452 <     * for each key-value mapping, followed by a null pair.
3371 <     * The key-value mappings are emitted in no particular order.
3372 <     */
3373 <    @SuppressWarnings("unchecked") private void writeObject(java.io.ObjectOutputStream s)
3374 <        throws java.io.IOException {
3375 <        if (segments == null) { // for serialization compatibility
3376 <            segments = (Segment<K,V>[])
3377 <                new Segment<?,?>[DEFAULT_CONCURRENCY_LEVEL];
3378 <            for (int i = 0; i < segments.length; ++i)
3379 <                segments[i] = new Segment<K,V>(LOAD_FACTOR);
3380 <        }
3381 <        s.defaultWriteObject();
3382 <        Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
3383 <        Object v;
3384 <        while ((v = it.advance()) != null) {
3385 <            s.writeObject(it.nextKey);
3386 <            s.writeObject(v);
3446 >    static final class ValueSpliterator<K,V> extends Traverser<K,V>
3447 >        implements ConcurrentHashMapSpliterator<V> {
3448 >        long est;               // size estimate
3449 >        ValueSpliterator(Node<K,V>[] tab, int size, int index, int limit,
3450 >                         long est) {
3451 >            super(tab, size, index, limit);
3452 >            this.est = est;
3453          }
3388        s.writeObject(null);
3389        s.writeObject(null);
3390        segments = null; // throw away
3391    }
3454  
3455 <    /**
3456 <     * Reconstitutes the instance from a stream (that is, deserializes it).
3457 <     * @param s the stream
3458 <     */
3459 <    @SuppressWarnings("unchecked") private void readObject(java.io.ObjectInputStream s)
3460 <        throws java.io.IOException, ClassNotFoundException {
3399 <        s.defaultReadObject();
3400 <        this.segments = null; // unneeded
3401 <        // initialize transient final field
3402 <        UNSAFE.putObjectVolatile(this, counterOffset, new LongAdder());
3455 >        public ConcurrentHashMapSpliterator<V> trySplit() {
3456 >            int i, f, h;
3457 >            return (h = ((i = baseIndex) + (f = baseLimit)) >>> 1) <= i ? null :
3458 >                new ValueSpliterator<K,V>(tab, baseSize, baseLimit = h,
3459 >                                          f, est >>>= 1);
3460 >        }
3461  
3462 <        // Create all nodes, then place in table once size is known
3463 <        long size = 0L;
3464 <        Node p = null;
3465 <        for (;;) {
3408 <            K k = (K) s.readObject();
3409 <            V v = (V) s.readObject();
3410 <            if (k != null && v != null) {
3411 <                int h = spread(k.hashCode());
3412 <                p = new Node(h, k, v, p);
3413 <                ++size;
3414 <            }
3415 <            else
3416 <                break;
3462 >        public void forEachRemaining(Action<? super V> action) {
3463 >            if (action == null) throw new NullPointerException();
3464 >            for (Node<K,V> p; (p = advance()) != null;)
3465 >                action.apply(p.val);
3466          }
3467 <        if (p != null) {
3468 <            boolean init = false;
3469 <            int n;
3470 <            if (size >= (long)(MAXIMUM_CAPACITY >>> 1))
3471 <                n = MAXIMUM_CAPACITY;
3472 <            else {
3473 <                int sz = (int)size;
3474 <                n = tableSizeFor(sz + (sz >>> 1) + 1);
3426 <            }
3427 <            int sc = sizeCtl;
3428 <            boolean collide = false;
3429 <            if (n > sc &&
3430 <                UNSAFE.compareAndSwapInt(this, sizeCtlOffset, sc, -1)) {
3431 <                try {
3432 <                    if (table == null) {
3433 <                        init = true;
3434 <                        Node[] tab = new Node[n];
3435 <                        int mask = n - 1;
3436 <                        while (p != null) {
3437 <                            int j = p.hash & mask;
3438 <                            Node next = p.next;
3439 <                            Node q = p.next = tabAt(tab, j);
3440 <                            setTabAt(tab, j, p);
3441 <                            if (!collide && q != null && q.hash == p.hash)
3442 <                                collide = true;
3443 <                            p = next;
3444 <                        }
3445 <                        table = tab;
3446 <                        counter.add(size);
3447 <                        sc = n - (n >>> 2);
3448 <                    }
3449 <                } finally {
3450 <                    sizeCtl = sc;
3451 <                }
3452 <                if (collide) { // rescan and convert to TreeBins
3453 <                    Node[] tab = table;
3454 <                    for (int i = 0; i < tab.length; ++i) {
3455 <                        int c = 0;
3456 <                        for (Node e = tabAt(tab, i); e != null; e = e.next) {
3457 <                            if (++c > TREE_THRESHOLD &&
3458 <                                (e.key instanceof Comparable)) {
3459 <                                replaceWithTreeBin(tab, i, e.key);
3460 <                                break;
3461 <                            }
3462 <                        }
3463 <                    }
3464 <                }
3465 <            }
3466 <            if (!init) { // Can only happen if unsafely published.
3467 <                while (p != null) {
3468 <                    internalPut(p.key, p.val);
3469 <                    p = p.next;
3470 <                }
3471 <            }
3467 >
3468 >        public boolean tryAdvance(Action<? super V> action) {
3469 >            if (action == null) throw new NullPointerException();
3470 >            Node<K,V> p;
3471 >            if ((p = advance()) == null)
3472 >                return false;
3473 >            action.apply(p.val);
3474 >            return true;
3475          }
3476 +
3477 +        public long estimateSize() { return est; }
3478 +
3479      }
3480  
3481 +    static final class EntrySpliterator<K,V> extends Traverser<K,V>
3482 +        implements ConcurrentHashMapSpliterator<Map.Entry<K,V>> {
3483 +        final ConcurrentHashMapV8<K,V> map; // To export MapEntry
3484 +        long est;               // size estimate
3485 +        EntrySpliterator(Node<K,V>[] tab, int size, int index, int limit,
3486 +                         long est, ConcurrentHashMapV8<K,V> map) {
3487 +            super(tab, size, index, limit);
3488 +            this.map = map;
3489 +            this.est = est;
3490 +        }
3491  
3492 <    // -------------------------------------------------------
3492 >        public ConcurrentHashMapSpliterator<Map.Entry<K,V>> trySplit() {
3493 >            int i, f, h;
3494 >            return (h = ((i = baseIndex) + (f = baseLimit)) >>> 1) <= i ? null :
3495 >                new EntrySpliterator<K,V>(tab, baseSize, baseLimit = h,
3496 >                                          f, est >>>= 1, map);
3497 >        }
3498  
3499 <    // Sams
3500 <    /** Interface describing a void action of one argument */
3501 <    public interface Action<A> { void apply(A a); }
3502 <    /** Interface describing a void action of two arguments */
3503 <    public interface BiAction<A,B> { void apply(A a, B b); }
3483 <    /** Interface describing a function of one argument */
3484 <    public interface Fun<A,T> { T apply(A a); }
3485 <    /** Interface describing a function of two arguments */
3486 <    public interface BiFun<A,B,T> { T apply(A a, B b); }
3487 <    /** Interface describing a function of no arguments */
3488 <    public interface Generator<T> { T apply(); }
3489 <    /** Interface describing a function mapping its argument to a double */
3490 <    public interface ObjectToDouble<A> { double apply(A a); }
3491 <    /** Interface describing a function mapping its argument to a long */
3492 <    public interface ObjectToLong<A> { long apply(A a); }
3493 <    /** Interface describing a function mapping its argument to an int */
3494 <    public interface ObjectToInt<A> {int apply(A a); }
3495 <    /** Interface describing a function mapping two arguments to a double */
3496 <    public interface ObjectByObjectToDouble<A,B> { double apply(A a, B b); }
3497 <    /** Interface describing a function mapping two arguments to a long */
3498 <    public interface ObjectByObjectToLong<A,B> { long apply(A a, B b); }
3499 <    /** Interface describing a function mapping two arguments to an int */
3500 <    public interface ObjectByObjectToInt<A,B> {int apply(A a, B b); }
3501 <    /** Interface describing a function mapping a double to a double */
3502 <    public interface DoubleToDouble { double apply(double a); }
3503 <    /** Interface describing a function mapping a long to a long */
3504 <    public interface LongToLong { long apply(long a); }
3505 <    /** Interface describing a function mapping an int to an int */
3506 <    public interface IntToInt { int apply(int a); }
3507 <    /** Interface describing a function mapping two doubles to a double */
3508 <    public interface DoubleByDoubleToDouble { double apply(double a, double b); }
3509 <    /** Interface describing a function mapping two longs to a long */
3510 <    public interface LongByLongToLong { long apply(long a, long b); }
3511 <    /** Interface describing a function mapping two ints to an int */
3512 <    public interface IntByIntToInt { int apply(int a, int b); }
3499 >        public void forEachRemaining(Action<? super Map.Entry<K,V>> action) {
3500 >            if (action == null) throw new NullPointerException();
3501 >            for (Node<K,V> p; (p = advance()) != null; )
3502 >                action.apply(new MapEntry<K,V>(p.key, p.val, map));
3503 >        }
3504  
3505 +        public boolean tryAdvance(Action<? super Map.Entry<K,V>> action) {
3506 +            if (action == null) throw new NullPointerException();
3507 +            Node<K,V> p;
3508 +            if ((p = advance()) == null)
3509 +                return false;
3510 +            action.apply(new MapEntry<K,V>(p.key, p.val, map));
3511 +            return true;
3512 +        }
3513  
3514 <    // -------------------------------------------------------
3514 >        public long estimateSize() { return est; }
3515 >
3516 >    }
3517 >
3518 >    // Parallel bulk operations
3519 >
3520 >    /**
3521 >     * Computes initial batch value for bulk tasks. The returned value
3522 >     * is approximately exp2 of the number of times (minus one) to
3523 >     * split task by two before executing leaf action. This value is
3524 >     * faster to compute and more convenient to use as a guide to
3525 >     * splitting than is the depth, since it is used while dividing by
3526 >     * two anyway.
3527 >     */
3528 >    final int batchFor(long b) {
3529 >        long n;
3530 >        if (b == Long.MAX_VALUE || (n = sumCount()) <= 1L || n < b)
3531 >            return 0;
3532 >        int sp = ForkJoinPool.getCommonPoolParallelism() << 2; // slack of 4
3533 >        return (b <= 0L || (n /= b) >= sp) ? sp : (int)n;
3534 >    }
3535  
3536      /**
3537       * Performs the given action for each (key, value).
3538       *
3539 +     * @param parallelismThreshold the (estimated) number of elements
3540 +     * needed for this operation to be executed in parallel
3541       * @param action the action
3542 +     * @since 1.8
3543       */
3544 <    public void forEach(BiAction<K,V> action) {
3545 <        ForkJoinTasks.forEach
3546 <            (this, action).invoke();
3544 >    public void forEach(long parallelismThreshold,
3545 >                        BiAction<? super K,? super V> action) {
3546 >        if (action == null) throw new NullPointerException();
3547 >        new ForEachMappingTask<K,V>
3548 >            (null, batchFor(parallelismThreshold), 0, 0, table,
3549 >             action).invoke();
3550      }
3551  
3552      /**
3553       * Performs the given action for each non-null transformation
3554       * of each (key, value).
3555       *
3556 +     * @param parallelismThreshold the (estimated) number of elements
3557 +     * needed for this operation to be executed in parallel
3558       * @param transformer a function returning the transformation
3559 <     * for an element, or null of there is no transformation (in
3560 <     * which case the action is not applied).
3559 >     * for an element, or null if there is no transformation (in
3560 >     * which case the action is not applied)
3561       * @param action the action
3562 +     * @since 1.8
3563       */
3564 <    public <U> void forEach(BiFun<? super K, ? super V, ? extends U> transformer,
3565 <                            Action<U> action) {
3566 <        ForkJoinTasks.forEach
3567 <            (this, transformer, action).invoke();
3564 >    public <U> void forEach(long parallelismThreshold,
3565 >                            BiFun<? super K, ? super V, ? extends U> transformer,
3566 >                            Action<? super U> action) {
3567 >        if (transformer == null || action == null)
3568 >            throw new NullPointerException();
3569 >        new ForEachTransformedMappingTask<K,V,U>
3570 >            (null, batchFor(parallelismThreshold), 0, 0, table,
3571 >             transformer, action).invoke();
3572      }
3573  
3574      /**
# Line 3546 | Line 3578 | public class ConcurrentHashMapV8<K, V>
3578       * results of any other parallel invocations of the search
3579       * function are ignored.
3580       *
3581 +     * @param parallelismThreshold the (estimated) number of elements
3582 +     * needed for this operation to be executed in parallel
3583       * @param searchFunction a function returning a non-null
3584       * result on success, else null
3585       * @return a non-null result from applying the given search
3586       * function on each (key, value), or null if none
3587 +     * @since 1.8
3588       */
3589 <    public <U> U search(BiFun<? super K, ? super V, ? extends U> searchFunction) {
3590 <        return ForkJoinTasks.search
3591 <            (this, searchFunction).invoke();
3589 >    public <U> U search(long parallelismThreshold,
3590 >                        BiFun<? super K, ? super V, ? extends U> searchFunction) {
3591 >        if (searchFunction == null) throw new NullPointerException();
3592 >        return new SearchMappingsTask<K,V,U>
3593 >            (null, batchFor(parallelismThreshold), 0, 0, table,
3594 >             searchFunction, new AtomicReference<U>()).invoke();
3595      }
3596  
3597      /**
# Line 3561 | Line 3599 | public class ConcurrentHashMapV8<K, V>
3599       * of all (key, value) pairs using the given reducer to
3600       * combine values, or null if none.
3601       *
3602 +     * @param parallelismThreshold the (estimated) number of elements
3603 +     * needed for this operation to be executed in parallel
3604       * @param transformer a function returning the transformation
3605 <     * for an element, or null of there is no transformation (in
3606 <     * which case it is not combined).
3605 >     * for an element, or null if there is no transformation (in
3606 >     * which case it is not combined)
3607       * @param reducer a commutative associative combining function
3608       * @return the result of accumulating the given transformation
3609       * of all (key, value) pairs
3610 +     * @since 1.8
3611       */
3612 <    public <U> U reduce(BiFun<? super K, ? super V, ? extends U> transformer,
3612 >    public <U> U reduce(long parallelismThreshold,
3613 >                        BiFun<? super K, ? super V, ? extends U> transformer,
3614                          BiFun<? super U, ? super U, ? extends U> reducer) {
3615 <        return ForkJoinTasks.reduce
3616 <            (this, transformer, reducer).invoke();
3615 >        if (transformer == null || reducer == null)
3616 >            throw new NullPointerException();
3617 >        return new MapReduceMappingsTask<K,V,U>
3618 >            (null, batchFor(parallelismThreshold), 0, 0, table,
3619 >             null, transformer, reducer).invoke();
3620      }
3621  
3622      /**
# Line 3579 | Line 3624 | public class ConcurrentHashMapV8<K, V>
3624       * of all (key, value) pairs using the given reducer to
3625       * combine values, and the given basis as an identity value.
3626       *
3627 +     * @param parallelismThreshold the (estimated) number of elements
3628 +     * needed for this operation to be executed in parallel
3629       * @param transformer a function returning the transformation
3630       * for an element
3631       * @param basis the identity (initial default value) for the reduction
3632       * @param reducer a commutative associative combining function
3633       * @return the result of accumulating the given transformation
3634       * of all (key, value) pairs
3635 +     * @since 1.8
3636       */
3637 <    public double reduceToDouble(ObjectByObjectToDouble<? super K, ? super V> transformer,
3637 >    public double reduceToDouble(long parallelismThreshold,
3638 >                                 ObjectByObjectToDouble<? super K, ? super V> transformer,
3639                                   double basis,
3640                                   DoubleByDoubleToDouble reducer) {
3641 <        return ForkJoinTasks.reduceToDouble
3642 <            (this, transformer, basis, reducer).invoke();
3641 >        if (transformer == null || reducer == null)
3642 >            throw new NullPointerException();
3643 >        return new MapReduceMappingsToDoubleTask<K,V>
3644 >            (null, batchFor(parallelismThreshold), 0, 0, table,
3645 >             null, transformer, basis, reducer).invoke();
3646      }
3647  
3648      /**
# Line 3598 | Line 3650 | public class ConcurrentHashMapV8<K, V>
3650       * of all (key, value) pairs using the given reducer to
3651       * combine values, and the given basis as an identity value.
3652       *
3653 +     * @param parallelismThreshold the (estimated) number of elements
3654 +     * needed for this operation to be executed in parallel
3655       * @param transformer a function returning the transformation
3656       * for an element
3657       * @param basis the identity (initial default value) for the reduction
3658       * @param reducer a commutative associative combining function
3659       * @return the result of accumulating the given transformation
3660       * of all (key, value) pairs
3661 +     * @since 1.8
3662       */
3663 <    public long reduceToLong(ObjectByObjectToLong<? super K, ? super V> transformer,
3663 >    public long reduceToLong(long parallelismThreshold,
3664 >                             ObjectByObjectToLong<? super K, ? super V> transformer,
3665                               long basis,
3666                               LongByLongToLong reducer) {
3667 <        return ForkJoinTasks.reduceToLong
3668 <            (this, transformer, basis, reducer).invoke();
3667 >        if (transformer == null || reducer == null)
3668 >            throw new NullPointerException();
3669 >        return new MapReduceMappingsToLongTask<K,V>
3670 >            (null, batchFor(parallelismThreshold), 0, 0, table,
3671 >             null, transformer, basis, reducer).invoke();
3672      }
3673  
3674      /**
# Line 3617 | Line 3676 | public class ConcurrentHashMapV8<K, V>
3676       * of all (key, value) pairs using the given reducer to
3677       * combine values, and the given basis as an identity value.
3678       *
3679 +     * @param parallelismThreshold the (estimated) number of elements
3680 +     * needed for this operation to be executed in parallel
3681       * @param transformer a function returning the transformation
3682       * for an element
3683       * @param basis the identity (initial default value) for the reduction
3684       * @param reducer a commutative associative combining function
3685       * @return the result of accumulating the given transformation
3686       * of all (key, value) pairs
3687 +     * @since 1.8
3688       */
3689 <    public int reduceToInt(ObjectByObjectToInt<? super K, ? super V> transformer,
3689 >    public int reduceToInt(long parallelismThreshold,
3690 >                           ObjectByObjectToInt<? super K, ? super V> transformer,
3691                             int basis,
3692                             IntByIntToInt reducer) {
3693 <        return ForkJoinTasks.reduceToInt
3694 <            (this, transformer, basis, reducer).invoke();
3693 >        if (transformer == null || reducer == null)
3694 >            throw new NullPointerException();
3695 >        return new MapReduceMappingsToIntTask<K,V>
3696 >            (null, batchFor(parallelismThreshold), 0, 0, table,
3697 >             null, transformer, basis, reducer).invoke();
3698      }
3699  
3700      /**
3701       * Performs the given action for each key.
3702       *
3703 +     * @param parallelismThreshold the (estimated) number of elements
3704 +     * needed for this operation to be executed in parallel
3705       * @param action the action
3706 +     * @since 1.8
3707       */
3708 <    public void forEachKey(Action<K> action) {
3709 <        ForkJoinTasks.forEachKey
3710 <            (this, action).invoke();
3708 >    public void forEachKey(long parallelismThreshold,
3709 >                           Action<? super K> action) {
3710 >        if (action == null) throw new NullPointerException();
3711 >        new ForEachKeyTask<K,V>
3712 >            (null, batchFor(parallelismThreshold), 0, 0, table,
3713 >             action).invoke();
3714      }
3715  
3716      /**
3717       * Performs the given action for each non-null transformation
3718       * of each key.
3719       *
3720 +     * @param parallelismThreshold the (estimated) number of elements
3721 +     * needed for this operation to be executed in parallel
3722       * @param transformer a function returning the transformation
3723 <     * for an element, or null of there is no transformation (in
3724 <     * which case the action is not applied).
3723 >     * for an element, or null if there is no transformation (in
3724 >     * which case the action is not applied)
3725       * @param action the action
3726 +     * @since 1.8
3727       */
3728 <    public <U> void forEachKey(Fun<? super K, ? extends U> transformer,
3729 <                               Action<U> action) {
3730 <        ForkJoinTasks.forEachKey
3731 <            (this, transformer, action).invoke();
3728 >    public <U> void forEachKey(long parallelismThreshold,
3729 >                               Fun<? super K, ? extends U> transformer,
3730 >                               Action<? super U> action) {
3731 >        if (transformer == null || action == null)
3732 >            throw new NullPointerException();
3733 >        new ForEachTransformedKeyTask<K,V,U>
3734 >            (null, batchFor(parallelismThreshold), 0, 0, table,
3735 >             transformer, action).invoke();
3736      }
3737  
3738      /**
# Line 3663 | Line 3742 | public class ConcurrentHashMapV8<K, V>
3742       * any other parallel invocations of the search function are
3743       * ignored.
3744       *
3745 +     * @param parallelismThreshold the (estimated) number of elements
3746 +     * needed for this operation to be executed in parallel
3747       * @param searchFunction a function returning a non-null
3748       * result on success, else null
3749       * @return a non-null result from applying the given search
3750       * function on each key, or null if none
3751 +     * @since 1.8
3752       */
3753 <    public <U> U searchKeys(Fun<? super K, ? extends U> searchFunction) {
3754 <        return ForkJoinTasks.searchKeys
3755 <            (this, searchFunction).invoke();
3753 >    public <U> U searchKeys(long parallelismThreshold,
3754 >                            Fun<? super K, ? extends U> searchFunction) {
3755 >        if (searchFunction == null) throw new NullPointerException();
3756 >        return new SearchKeysTask<K,V,U>
3757 >            (null, batchFor(parallelismThreshold), 0, 0, table,
3758 >             searchFunction, new AtomicReference<U>()).invoke();
3759      }
3760  
3761      /**
3762       * Returns the result of accumulating all keys using the given
3763       * reducer to combine values, or null if none.
3764       *
3765 +     * @param parallelismThreshold the (estimated) number of elements
3766 +     * needed for this operation to be executed in parallel
3767       * @param reducer a commutative associative combining function
3768       * @return the result of accumulating all keys using the given
3769       * reducer to combine values, or null if none
3770 +     * @since 1.8
3771       */
3772 <    public K reduceKeys(BiFun<? super K, ? super K, ? extends K> reducer) {
3773 <        return ForkJoinTasks.reduceKeys
3774 <            (this, reducer).invoke();
3772 >    public K reduceKeys(long parallelismThreshold,
3773 >                        BiFun<? super K, ? super K, ? extends K> reducer) {
3774 >        if (reducer == null) throw new NullPointerException();
3775 >        return new ReduceKeysTask<K,V>
3776 >            (null, batchFor(parallelismThreshold), 0, 0, table,
3777 >             null, reducer).invoke();
3778      }
3779  
3780      /**
# Line 3691 | Line 3782 | public class ConcurrentHashMapV8<K, V>
3782       * of all keys using the given reducer to combine values, or
3783       * null if none.
3784       *
3785 +     * @param parallelismThreshold the (estimated) number of elements
3786 +     * needed for this operation to be executed in parallel
3787       * @param transformer a function returning the transformation
3788 <     * for an element, or null of there is no transformation (in
3789 <     * which case it is not combined).
3788 >     * for an element, or null if there is no transformation (in
3789 >     * which case it is not combined)
3790       * @param reducer a commutative associative combining function
3791       * @return the result of accumulating the given transformation
3792       * of all keys
3793 +     * @since 1.8
3794       */
3795 <    public <U> U reduceKeys(Fun<? super K, ? extends U> transformer,
3796 <                            BiFun<? super U, ? super U, ? extends U> reducer) {
3797 <        return ForkJoinTasks.reduceKeys
3798 <            (this, transformer, reducer).invoke();
3795 >    public <U> U reduceKeys(long parallelismThreshold,
3796 >                            Fun<? super K, ? extends U> transformer,
3797 >         BiFun<? super U, ? super U, ? extends U> reducer) {
3798 >        if (transformer == null || reducer == null)
3799 >            throw new NullPointerException();
3800 >        return new MapReduceKeysTask<K,V,U>
3801 >            (null, batchFor(parallelismThreshold), 0, 0, table,
3802 >             null, transformer, reducer).invoke();
3803      }
3804  
3805      /**
# Line 3709 | Line 3807 | public class ConcurrentHashMapV8<K, V>
3807       * of all keys using the given reducer to combine values, and
3808       * the given basis as an identity value.
3809       *
3810 +     * @param parallelismThreshold the (estimated) number of elements
3811 +     * needed for this operation to be executed in parallel
3812       * @param transformer a function returning the transformation
3813       * for an element
3814       * @param basis the identity (initial default value) for the reduction
3815       * @param reducer a commutative associative combining function
3816 <     * @return  the result of accumulating the given transformation
3816 >     * @return the result of accumulating the given transformation
3817       * of all keys
3818 +     * @since 1.8
3819       */
3820 <    public double reduceKeysToDouble(ObjectToDouble<? super K> transformer,
3820 >    public double reduceKeysToDouble(long parallelismThreshold,
3821 >                                     ObjectToDouble<? super K> transformer,
3822                                       double basis,
3823                                       DoubleByDoubleToDouble reducer) {
3824 <        return ForkJoinTasks.reduceKeysToDouble
3825 <            (this, transformer, basis, reducer).invoke();
3824 >        if (transformer == null || reducer == null)
3825 >            throw new NullPointerException();
3826 >        return new MapReduceKeysToDoubleTask<K,V>
3827 >            (null, batchFor(parallelismThreshold), 0, 0, table,
3828 >             null, transformer, basis, reducer).invoke();
3829      }
3830  
3831      /**
# Line 3728 | Line 3833 | public class ConcurrentHashMapV8<K, V>
3833       * of all keys using the given reducer to combine values, and
3834       * the given basis as an identity value.
3835       *
3836 +     * @param parallelismThreshold the (estimated) number of elements
3837 +     * needed for this operation to be executed in parallel
3838       * @param transformer a function returning the transformation
3839       * for an element
3840       * @param basis the identity (initial default value) for the reduction
3841       * @param reducer a commutative associative combining function
3842       * @return the result of accumulating the given transformation
3843       * of all keys
3844 +     * @since 1.8
3845       */
3846 <    public long reduceKeysToLong(ObjectToLong<? super K> transformer,
3846 >    public long reduceKeysToLong(long parallelismThreshold,
3847 >                                 ObjectToLong<? super K> transformer,
3848                                   long basis,
3849                                   LongByLongToLong reducer) {
3850 <        return ForkJoinTasks.reduceKeysToLong
3851 <            (this, transformer, basis, reducer).invoke();
3850 >        if (transformer == null || reducer == null)
3851 >            throw new NullPointerException();
3852 >        return new MapReduceKeysToLongTask<K,V>
3853 >            (null, batchFor(parallelismThreshold), 0, 0, table,
3854 >             null, transformer, basis, reducer).invoke();
3855      }
3856  
3857      /**
# Line 3747 | Line 3859 | public class ConcurrentHashMapV8<K, V>
3859       * of all keys using the given reducer to combine values, and
3860       * the given basis as an identity value.
3861       *
3862 +     * @param parallelismThreshold the (estimated) number of elements
3863 +     * needed for this operation to be executed in parallel
3864       * @param transformer a function returning the transformation
3865       * for an element
3866       * @param basis the identity (initial default value) for the reduction
3867       * @param reducer a commutative associative combining function
3868       * @return the result of accumulating the given transformation
3869       * of all keys
3870 +     * @since 1.8
3871       */
3872 <    public int reduceKeysToInt(ObjectToInt<? super K> transformer,
3872 >    public int reduceKeysToInt(long parallelismThreshold,
3873 >                               ObjectToInt<? super K> transformer,
3874                                 int basis,
3875                                 IntByIntToInt reducer) {
3876 <        return ForkJoinTasks.reduceKeysToInt
3877 <            (this, transformer, basis, reducer).invoke();
3876 >        if (transformer == null || reducer == null)
3877 >            throw new NullPointerException();
3878 >        return new MapReduceKeysToIntTask<K,V>
3879 >            (null, batchFor(parallelismThreshold), 0, 0, table,
3880 >             null, transformer, basis, reducer).invoke();
3881      }
3882  
3883      /**
3884       * Performs the given action for each value.
3885       *
3886 +     * @param parallelismThreshold the (estimated) number of elements
3887 +     * needed for this operation to be executed in parallel
3888       * @param action the action
3889 +     * @since 1.8
3890       */
3891 <    public void forEachValue(Action<V> action) {
3892 <        ForkJoinTasks.forEachValue
3893 <            (this, action).invoke();
3891 >    public void forEachValue(long parallelismThreshold,
3892 >                             Action<? super V> action) {
3893 >        if (action == null)
3894 >            throw new NullPointerException();
3895 >        new ForEachValueTask<K,V>
3896 >            (null, batchFor(parallelismThreshold), 0, 0, table,
3897 >             action).invoke();
3898      }
3899  
3900      /**
3901       * Performs the given action for each non-null transformation
3902       * of each value.
3903       *
3904 +     * @param parallelismThreshold the (estimated) number of elements
3905 +     * needed for this operation to be executed in parallel
3906       * @param transformer a function returning the transformation
3907 <     * for an element, or null of there is no transformation (in
3908 <     * which case the action is not applied).
3907 >     * for an element, or null if there is no transformation (in
3908 >     * which case the action is not applied)
3909 >     * @param action the action
3910 >     * @since 1.8
3911       */
3912 <    public <U> void forEachValue(Fun<? super V, ? extends U> transformer,
3913 <                                 Action<U> action) {
3914 <        ForkJoinTasks.forEachValue
3915 <            (this, transformer, action).invoke();
3912 >    public <U> void forEachValue(long parallelismThreshold,
3913 >                                 Fun<? super V, ? extends U> transformer,
3914 >                                 Action<? super U> action) {
3915 >        if (transformer == null || action == null)
3916 >            throw new NullPointerException();
3917 >        new ForEachTransformedValueTask<K,V,U>
3918 >            (null, batchFor(parallelismThreshold), 0, 0, table,
3919 >             transformer, action).invoke();
3920      }
3921  
3922      /**
# Line 3792 | Line 3926 | public class ConcurrentHashMapV8<K, V>
3926       * any other parallel invocations of the search function are
3927       * ignored.
3928       *
3929 +     * @param parallelismThreshold the (estimated) number of elements
3930 +     * needed for this operation to be executed in parallel
3931       * @param searchFunction a function returning a non-null
3932       * result on success, else null
3933       * @return a non-null result from applying the given search
3934       * function on each value, or null if none
3935 <     *
3935 >     * @since 1.8
3936       */
3937 <    public <U> U searchValues(Fun<? super V, ? extends U> searchFunction) {
3938 <        return ForkJoinTasks.searchValues
3939 <            (this, searchFunction).invoke();
3937 >    public <U> U searchValues(long parallelismThreshold,
3938 >                              Fun<? super V, ? extends U> searchFunction) {
3939 >        if (searchFunction == null) throw new NullPointerException();
3940 >        return new SearchValuesTask<K,V,U>
3941 >            (null, batchFor(parallelismThreshold), 0, 0, table,
3942 >             searchFunction, new AtomicReference<U>()).invoke();
3943      }
3944  
3945      /**
3946       * Returns the result of accumulating all values using the
3947       * given reducer to combine values, or null if none.
3948       *
3949 +     * @param parallelismThreshold the (estimated) number of elements
3950 +     * needed for this operation to be executed in parallel
3951       * @param reducer a commutative associative combining function
3952 <     * @return  the result of accumulating all values
3952 >     * @return the result of accumulating all values
3953 >     * @since 1.8
3954       */
3955 <    public V reduceValues(BiFun<? super V, ? super V, ? extends V> reducer) {
3956 <        return ForkJoinTasks.reduceValues
3957 <            (this, reducer).invoke();
3955 >    public V reduceValues(long parallelismThreshold,
3956 >                          BiFun<? super V, ? super V, ? extends V> reducer) {
3957 >        if (reducer == null) throw new NullPointerException();
3958 >        return new ReduceValuesTask<K,V>
3959 >            (null, batchFor(parallelismThreshold), 0, 0, table,
3960 >             null, reducer).invoke();
3961      }
3962  
3963      /**
# Line 3820 | Line 3965 | public class ConcurrentHashMapV8<K, V>
3965       * of all values using the given reducer to combine values, or
3966       * null if none.
3967       *
3968 +     * @param parallelismThreshold the (estimated) number of elements
3969 +     * needed for this operation to be executed in parallel
3970       * @param transformer a function returning the transformation
3971 <     * for an element, or null of there is no transformation (in
3972 <     * which case it is not combined).
3971 >     * for an element, or null if there is no transformation (in
3972 >     * which case it is not combined)
3973       * @param reducer a commutative associative combining function
3974       * @return the result of accumulating the given transformation
3975       * of all values
3976 +     * @since 1.8
3977       */
3978 <    public <U> U reduceValues(Fun<? super V, ? extends U> transformer,
3978 >    public <U> U reduceValues(long parallelismThreshold,
3979 >                              Fun<? super V, ? extends U> transformer,
3980                                BiFun<? super U, ? super U, ? extends U> reducer) {
3981 <        return ForkJoinTasks.reduceValues
3982 <            (this, transformer, reducer).invoke();
3981 >        if (transformer == null || reducer == null)
3982 >            throw new NullPointerException();
3983 >        return new MapReduceValuesTask<K,V,U>
3984 >            (null, batchFor(parallelismThreshold), 0, 0, table,
3985 >             null, transformer, reducer).invoke();
3986      }
3987  
3988      /**
# Line 3838 | Line 3990 | public class ConcurrentHashMapV8<K, V>
3990       * of all values using the given reducer to combine values,
3991       * and the given basis as an identity value.
3992       *
3993 +     * @param parallelismThreshold the (estimated) number of elements
3994 +     * needed for this operation to be executed in parallel
3995       * @param transformer a function returning the transformation
3996       * for an element
3997       * @param basis the identity (initial default value) for the reduction
3998       * @param reducer a commutative associative combining function
3999       * @return the result of accumulating the given transformation
4000       * of all values
4001 +     * @since 1.8
4002       */
4003 <    public double reduceValuesToDouble(ObjectToDouble<? super V> transformer,
4003 >    public double reduceValuesToDouble(long parallelismThreshold,
4004 >                                       ObjectToDouble<? super V> transformer,
4005                                         double basis,
4006                                         DoubleByDoubleToDouble reducer) {
4007 <        return ForkJoinTasks.reduceValuesToDouble
4008 <            (this, transformer, basis, reducer).invoke();
4007 >        if (transformer == null || reducer == null)
4008 >            throw new NullPointerException();
4009 >        return new MapReduceValuesToDoubleTask<K,V>
4010 >            (null, batchFor(parallelismThreshold), 0, 0, table,
4011 >             null, transformer, basis, reducer).invoke();
4012      }
4013  
4014      /**
# Line 3857 | Line 4016 | public class ConcurrentHashMapV8<K, V>
4016       * of all values using the given reducer to combine values,
4017       * and the given basis as an identity value.
4018       *
4019 +     * @param parallelismThreshold the (estimated) number of elements
4020 +     * needed for this operation to be executed in parallel
4021       * @param transformer a function returning the transformation
4022       * for an element
4023       * @param basis the identity (initial default value) for the reduction
4024       * @param reducer a commutative associative combining function
4025       * @return the result of accumulating the given transformation
4026       * of all values
4027 +     * @since 1.8
4028       */
4029 <    public long reduceValuesToLong(ObjectToLong<? super V> transformer,
4029 >    public long reduceValuesToLong(long parallelismThreshold,
4030 >                                   ObjectToLong<? super V> transformer,
4031                                     long basis,
4032                                     LongByLongToLong reducer) {
4033 <        return ForkJoinTasks.reduceValuesToLong
4034 <            (this, transformer, basis, reducer).invoke();
4033 >        if (transformer == null || reducer == null)
4034 >            throw new NullPointerException();
4035 >        return new MapReduceValuesToLongTask<K,V>
4036 >            (null, batchFor(parallelismThreshold), 0, 0, table,
4037 >             null, transformer, basis, reducer).invoke();
4038      }
4039  
4040      /**
# Line 3876 | Line 4042 | public class ConcurrentHashMapV8<K, V>
4042       * of all values using the given reducer to combine values,
4043       * and the given basis as an identity value.
4044       *
4045 +     * @param parallelismThreshold the (estimated) number of elements
4046 +     * needed for this operation to be executed in parallel
4047       * @param transformer a function returning the transformation
4048       * for an element
4049       * @param basis the identity (initial default value) for the reduction
4050       * @param reducer a commutative associative combining function
4051       * @return the result of accumulating the given transformation
4052       * of all values
4053 +     * @since 1.8
4054       */
4055 <    public int reduceValuesToInt(ObjectToInt<? super V> transformer,
4055 >    public int reduceValuesToInt(long parallelismThreshold,
4056 >                                 ObjectToInt<? super V> transformer,
4057                                   int basis,
4058                                   IntByIntToInt reducer) {
4059 <        return ForkJoinTasks.reduceValuesToInt
4060 <            (this, transformer, basis, reducer).invoke();
4059 >        if (transformer == null || reducer == null)
4060 >            throw new NullPointerException();
4061 >        return new MapReduceValuesToIntTask<K,V>
4062 >            (null, batchFor(parallelismThreshold), 0, 0, table,
4063 >             null, transformer, basis, reducer).invoke();
4064      }
4065  
4066      /**
4067       * Performs the given action for each entry.
4068       *
4069 +     * @param parallelismThreshold the (estimated) number of elements
4070 +     * needed for this operation to be executed in parallel
4071       * @param action the action
4072 +     * @since 1.8
4073       */
4074 <    public void forEachEntry(Action<Map.Entry<K,V>> action) {
4075 <        ForkJoinTasks.forEachEntry
4076 <            (this, action).invoke();
4074 >    public void forEachEntry(long parallelismThreshold,
4075 >                             Action<? super Map.Entry<K,V>> action) {
4076 >        if (action == null) throw new NullPointerException();
4077 >        new ForEachEntryTask<K,V>(null, batchFor(parallelismThreshold), 0, 0, table,
4078 >                                  action).invoke();
4079      }
4080  
4081      /**
4082       * Performs the given action for each non-null transformation
4083       * of each entry.
4084       *
4085 +     * @param parallelismThreshold the (estimated) number of elements
4086 +     * needed for this operation to be executed in parallel
4087       * @param transformer a function returning the transformation
4088 <     * for an element, or null of there is no transformation (in
4089 <     * which case the action is not applied).
4088 >     * for an element, or null if there is no transformation (in
4089 >     * which case the action is not applied)
4090       * @param action the action
4091 +     * @since 1.8
4092       */
4093 <    public <U> void forEachEntry(Fun<Map.Entry<K,V>, ? extends U> transformer,
4094 <                                 Action<U> action) {
4095 <        ForkJoinTasks.forEachEntry
4096 <            (this, transformer, action).invoke();
4093 >    public <U> void forEachEntry(long parallelismThreshold,
4094 >                                 Fun<Map.Entry<K,V>, ? extends U> transformer,
4095 >                                 Action<? super U> action) {
4096 >        if (transformer == null || action == null)
4097 >            throw new NullPointerException();
4098 >        new ForEachTransformedEntryTask<K,V,U>
4099 >            (null, batchFor(parallelismThreshold), 0, 0, table,
4100 >             transformer, action).invoke();
4101      }
4102  
4103      /**
# Line 3922 | Line 4107 | public class ConcurrentHashMapV8<K, V>
4107       * any other parallel invocations of the search function are
4108       * ignored.
4109       *
4110 +     * @param parallelismThreshold the (estimated) number of elements
4111 +     * needed for this operation to be executed in parallel
4112       * @param searchFunction a function returning a non-null
4113       * result on success, else null
4114       * @return a non-null result from applying the given search
4115       * function on each entry, or null if none
4116 +     * @since 1.8
4117       */
4118 <    public <U> U searchEntries(Fun<Map.Entry<K,V>, ? extends U> searchFunction) {
4119 <        return ForkJoinTasks.searchEntries
4120 <            (this, searchFunction).invoke();
4118 >    public <U> U searchEntries(long parallelismThreshold,
4119 >                               Fun<Map.Entry<K,V>, ? extends U> searchFunction) {
4120 >        if (searchFunction == null) throw new NullPointerException();
4121 >        return new SearchEntriesTask<K,V,U>
4122 >            (null, batchFor(parallelismThreshold), 0, 0, table,
4123 >             searchFunction, new AtomicReference<U>()).invoke();
4124      }
4125  
4126      /**
4127       * Returns the result of accumulating all entries using the
4128       * given reducer to combine values, or null if none.
4129       *
4130 +     * @param parallelismThreshold the (estimated) number of elements
4131 +     * needed for this operation to be executed in parallel
4132       * @param reducer a commutative associative combining function
4133       * @return the result of accumulating all entries
4134 +     * @since 1.8
4135       */
4136 <    public Map.Entry<K,V> reduceEntries(BiFun<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer) {
4137 <        return ForkJoinTasks.reduceEntries
4138 <            (this, reducer).invoke();
4136 >    public Map.Entry<K,V> reduceEntries(long parallelismThreshold,
4137 >                                        BiFun<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer) {
4138 >        if (reducer == null) throw new NullPointerException();
4139 >        return new ReduceEntriesTask<K,V>
4140 >            (null, batchFor(parallelismThreshold), 0, 0, table,
4141 >             null, reducer).invoke();
4142      }
4143  
4144      /**
# Line 3949 | Line 4146 | public class ConcurrentHashMapV8<K, V>
4146       * of all entries using the given reducer to combine values,
4147       * or null if none.
4148       *
4149 +     * @param parallelismThreshold the (estimated) number of elements
4150 +     * needed for this operation to be executed in parallel
4151       * @param transformer a function returning the transformation
4152 <     * for an element, or null of there is no transformation (in
4153 <     * which case it is not combined).
4152 >     * for an element, or null if there is no transformation (in
4153 >     * which case it is not combined)
4154       * @param reducer a commutative associative combining function
4155       * @return the result of accumulating the given transformation
4156       * of all entries
4157 +     * @since 1.8
4158       */
4159 <    public <U> U reduceEntries(Fun<Map.Entry<K,V>, ? extends U> transformer,
4159 >    public <U> U reduceEntries(long parallelismThreshold,
4160 >                               Fun<Map.Entry<K,V>, ? extends U> transformer,
4161                                 BiFun<? super U, ? super U, ? extends U> reducer) {
4162 <        return ForkJoinTasks.reduceEntries
4163 <            (this, transformer, reducer).invoke();
4162 >        if (transformer == null || reducer == null)
4163 >            throw new NullPointerException();
4164 >        return new MapReduceEntriesTask<K,V,U>
4165 >            (null, batchFor(parallelismThreshold), 0, 0, table,
4166 >             null, transformer, reducer).invoke();
4167      }
4168  
4169      /**
# Line 3967 | Line 4171 | public class ConcurrentHashMapV8<K, V>
4171       * of all entries using the given reducer to combine values,
4172       * and the given basis as an identity value.
4173       *
4174 +     * @param parallelismThreshold the (estimated) number of elements
4175 +     * needed for this operation to be executed in parallel
4176       * @param transformer a function returning the transformation
4177       * for an element
4178       * @param basis the identity (initial default value) for the reduction
4179       * @param reducer a commutative associative combining function
4180       * @return the result of accumulating the given transformation
4181       * of all entries
4182 +     * @since 1.8
4183       */
4184 <    public double reduceEntriesToDouble(ObjectToDouble<Map.Entry<K,V>> transformer,
4184 >    public double reduceEntriesToDouble(long parallelismThreshold,
4185 >                                        ObjectToDouble<Map.Entry<K,V>> transformer,
4186                                          double basis,
4187                                          DoubleByDoubleToDouble reducer) {
4188 <        return ForkJoinTasks.reduceEntriesToDouble
4189 <            (this, transformer, basis, reducer).invoke();
4188 >        if (transformer == null || reducer == null)
4189 >            throw new NullPointerException();
4190 >        return new MapReduceEntriesToDoubleTask<K,V>
4191 >            (null, batchFor(parallelismThreshold), 0, 0, table,
4192 >             null, transformer, basis, reducer).invoke();
4193      }
4194  
4195      /**
# Line 3986 | Line 4197 | public class ConcurrentHashMapV8<K, V>
4197       * of all entries using the given reducer to combine values,
4198       * and the given basis as an identity value.
4199       *
4200 +     * @param parallelismThreshold the (estimated) number of elements
4201 +     * needed for this operation to be executed in parallel
4202       * @param transformer a function returning the transformation
4203       * for an element
4204       * @param basis the identity (initial default value) for the reduction
4205       * @param reducer a commutative associative combining function
4206 <     * @return  the result of accumulating the given transformation
4206 >     * @return the result of accumulating the given transformation
4207       * of all entries
4208 +     * @since 1.8
4209       */
4210 <    public long reduceEntriesToLong(ObjectToLong<Map.Entry<K,V>> transformer,
4210 >    public long reduceEntriesToLong(long parallelismThreshold,
4211 >                                    ObjectToLong<Map.Entry<K,V>> transformer,
4212                                      long basis,
4213                                      LongByLongToLong reducer) {
4214 <        return ForkJoinTasks.reduceEntriesToLong
4215 <            (this, transformer, basis, reducer).invoke();
4214 >        if (transformer == null || reducer == null)
4215 >            throw new NullPointerException();
4216 >        return new MapReduceEntriesToLongTask<K,V>
4217 >            (null, batchFor(parallelismThreshold), 0, 0, table,
4218 >             null, transformer, basis, reducer).invoke();
4219      }
4220  
4221      /**
# Line 4005 | Line 4223 | public class ConcurrentHashMapV8<K, V>
4223       * of all entries using the given reducer to combine values,
4224       * and the given basis as an identity value.
4225       *
4226 +     * @param parallelismThreshold the (estimated) number of elements
4227 +     * needed for this operation to be executed in parallel
4228       * @param transformer a function returning the transformation
4229       * for an element
4230       * @param basis the identity (initial default value) for the reduction
4231       * @param reducer a commutative associative combining function
4232       * @return the result of accumulating the given transformation
4233       * of all entries
4234 +     * @since 1.8
4235       */
4236 <    public int reduceEntriesToInt(ObjectToInt<Map.Entry<K,V>> transformer,
4236 >    public int reduceEntriesToInt(long parallelismThreshold,
4237 >                                  ObjectToInt<Map.Entry<K,V>> transformer,
4238                                    int basis,
4239                                    IntByIntToInt reducer) {
4240 <        return ForkJoinTasks.reduceEntriesToInt
4241 <            (this, transformer, basis, reducer).invoke();
4240 >        if (transformer == null || reducer == null)
4241 >            throw new NullPointerException();
4242 >        return new MapReduceEntriesToIntTask<K,V>
4243 >            (null, batchFor(parallelismThreshold), 0, 0, table,
4244 >             null, transformer, basis, reducer).invoke();
4245      }
4246  
4247 +
4248      /* ----------------Views -------------- */
4249  
4250      /**
4251       * Base class for views.
4252       */
4253 <    static abstract class CHMView<K, V> {
4254 <        final ConcurrentHashMapV8<K, V> map;
4255 <        CHMView(ConcurrentHashMapV8<K, V> map)  { this.map = map; }
4253 >    abstract static class CollectionView<K,V,E>
4254 >        implements Collection<E>, java.io.Serializable {
4255 >        private static final long serialVersionUID = 7249069246763182397L;
4256 >        final ConcurrentHashMapV8<K,V> map;
4257 >        CollectionView(ConcurrentHashMapV8<K,V> map)  { this.map = map; }
4258  
4259          /**
4260           * Returns the map backing this view.
# Line 4035 | Line 4263 | public class ConcurrentHashMapV8<K, V>
4263           */
4264          public ConcurrentHashMapV8<K,V> getMap() { return map; }
4265  
4266 <        public final int size()                 { return map.size(); }
4267 <        public final boolean isEmpty()          { return map.isEmpty(); }
4268 <        public final void clear()               { map.clear(); }
4266 >        /**
4267 >         * Removes all of the elements from this view, by removing all
4268 >         * the mappings from the map backing this view.
4269 >         */
4270 >        public final void clear()      { map.clear(); }
4271 >        public final int size()        { return map.size(); }
4272 >        public final boolean isEmpty() { return map.isEmpty(); }
4273  
4274          // implementations below rely on concrete classes supplying these
4275 <        abstract public Iterator<?> iterator();
4276 <        abstract public boolean contains(Object o);
4277 <        abstract public boolean remove(Object o);
4275 >        // abstract methods
4276 >        /**
4277 >         * Returns a "weakly consistent" iterator that will never
4278 >         * throw {@link ConcurrentModificationException}, and
4279 >         * guarantees to traverse elements as they existed upon
4280 >         * construction of the iterator, and may (but is not
4281 >         * guaranteed to) reflect any modifications subsequent to
4282 >         * construction.
4283 >         */
4284 >        public abstract Iterator<E> iterator();
4285 >        public abstract boolean contains(Object o);
4286 >        public abstract boolean remove(Object o);
4287  
4288          private static final String oomeMsg = "Required array size too large";
4289  
4290          public final Object[] toArray() {
4291              long sz = map.mappingCount();
4292 <            if (sz > (long)(MAX_ARRAY_SIZE))
4292 >            if (sz > MAX_ARRAY_SIZE)
4293                  throw new OutOfMemoryError(oomeMsg);
4294              int n = (int)sz;
4295              Object[] r = new Object[n];
4296              int i = 0;
4297 <            Iterator<?> it = iterator();
4057 <            while (it.hasNext()) {
4297 >            for (E e : this) {
4298                  if (i == n) {
4299                      if (n >= MAX_ARRAY_SIZE)
4300                          throw new OutOfMemoryError(oomeMsg);
# Line 4064 | Line 4304 | public class ConcurrentHashMapV8<K, V>
4304                          n += (n >>> 1) + 1;
4305                      r = Arrays.copyOf(r, n);
4306                  }
4307 <                r[i++] = it.next();
4307 >                r[i++] = e;
4308              }
4309              return (i == n) ? r : Arrays.copyOf(r, i);
4310          }
4311  
4312 <        @SuppressWarnings("unchecked") public final <T> T[] toArray(T[] a) {
4312 >        @SuppressWarnings("unchecked")
4313 >        public final <T> T[] toArray(T[] a) {
4314              long sz = map.mappingCount();
4315 <            if (sz > (long)(MAX_ARRAY_SIZE))
4315 >            if (sz > MAX_ARRAY_SIZE)
4316                  throw new OutOfMemoryError(oomeMsg);
4317              int m = (int)sz;
4318              T[] r = (a.length >= m) ? a :
# Line 4079 | Line 4320 | public class ConcurrentHashMapV8<K, V>
4320                  .newInstance(a.getClass().getComponentType(), m);
4321              int n = r.length;
4322              int i = 0;
4323 <            Iterator<?> it = iterator();
4083 <            while (it.hasNext()) {
4323 >            for (E e : this) {
4324                  if (i == n) {
4325                      if (n >= MAX_ARRAY_SIZE)
4326                          throw new OutOfMemoryError(oomeMsg);
# Line 4090 | Line 4330 | public class ConcurrentHashMapV8<K, V>
4330                          n += (n >>> 1) + 1;
4331                      r = Arrays.copyOf(r, n);
4332                  }
4333 <                r[i++] = (T)it.next();
4333 >                r[i++] = (T)e;
4334              }
4335              if (a == r && i < n) {
4336                  r[i] = null; // null-terminate
# Line 4099 | Line 4339 | public class ConcurrentHashMapV8<K, V>
4339              return (i == n) ? r : Arrays.copyOf(r, i);
4340          }
4341  
4342 <        public final int hashCode() {
4343 <            int h = 0;
4344 <            for (Iterator<?> it = iterator(); it.hasNext();)
4345 <                h += it.next().hashCode();
4346 <            return h;
4347 <        }
4348 <
4342 >        /**
4343 >         * Returns a string representation of this collection.
4344 >         * The string representation consists of the string representations
4345 >         * of the collection's elements in the order they are returned by
4346 >         * its iterator, enclosed in square brackets ({@code "[]"}).
4347 >         * Adjacent elements are separated by the characters {@code ", "}
4348 >         * (comma and space).  Elements are converted to strings as by
4349 >         * {@link String#valueOf(Object)}.
4350 >         *
4351 >         * @return a string representation of this collection
4352 >         */
4353          public final String toString() {
4354              StringBuilder sb = new StringBuilder();
4355              sb.append('[');
4356 <            Iterator<?> it = iterator();
4356 >            Iterator<E> it = iterator();
4357              if (it.hasNext()) {
4358                  for (;;) {
4359                      Object e = it.next();
# Line 4124 | Line 4368 | public class ConcurrentHashMapV8<K, V>
4368  
4369          public final boolean containsAll(Collection<?> c) {
4370              if (c != this) {
4371 <                for (Iterator<?> it = c.iterator(); it.hasNext();) {
4128 <                    Object e = it.next();
4371 >                for (Object e : c) {
4372                      if (e == null || !contains(e))
4373                          return false;
4374                  }
# Line 4135 | Line 4378 | public class ConcurrentHashMapV8<K, V>
4378  
4379          public final boolean removeAll(Collection<?> c) {
4380              boolean modified = false;
4381 <            for (Iterator<?> it = iterator(); it.hasNext();) {
4381 >            for (Iterator<E> it = iterator(); it.hasNext();) {
4382                  if (c.contains(it.next())) {
4383                      it.remove();
4384                      modified = true;
# Line 4146 | Line 4389 | public class ConcurrentHashMapV8<K, V>
4389  
4390          public final boolean retainAll(Collection<?> c) {
4391              boolean modified = false;
4392 <            for (Iterator<?> it = iterator(); it.hasNext();) {
4392 >            for (Iterator<E> it = iterator(); it.hasNext();) {
4393                  if (!c.contains(it.next())) {
4394                      it.remove();
4395                      modified = true;
# Line 4160 | Line 4403 | public class ConcurrentHashMapV8<K, V>
4403      /**
4404       * A view of a ConcurrentHashMapV8 as a {@link Set} of keys, in
4405       * which additions may optionally be enabled by mapping to a
4406 <     * common value.  This class cannot be directly instantiated. See
4407 <     * {@link #keySet}, {@link #keySet(Object)}, {@link #newKeySet()},
4408 <     * {@link #newKeySet(int)}.
4406 >     * common value.  This class cannot be directly instantiated.
4407 >     * See {@link #keySet() keySet()},
4408 >     * {@link #keySet(Object) keySet(V)},
4409 >     * {@link #newKeySet() newKeySet()},
4410 >     * {@link #newKeySet(int) newKeySet(int)}.
4411 >     *
4412 >     * @since 1.8
4413       */
4414 <    public static class KeySetView<K,V> extends CHMView<K,V> implements Set<K>, java.io.Serializable {
4414 >    public static class KeySetView<K,V> extends CollectionView<K,V,K>
4415 >        implements Set<K>, java.io.Serializable {
4416          private static final long serialVersionUID = 7249069246763182397L;
4417          private final V value;
4418 <        KeySetView(ConcurrentHashMapV8<K, V> map, V value) {  // non-public
4418 >        KeySetView(ConcurrentHashMapV8<K,V> map, V value) {  // non-public
4419              super(map);
4420              this.value = value;
4421          }
# Line 4177 | Line 4425 | public class ConcurrentHashMapV8<K, V>
4425           * or {@code null} if additions are not supported.
4426           *
4427           * @return the default mapped value for additions, or {@code null}
4428 <         * if not supported.
4428 >         * if not supported
4429           */
4430          public V getMappedValue() { return value; }
4431  
4432 <        // implement Set API
4433 <
4432 >        /**
4433 >         * {@inheritDoc}
4434 >         * @throws NullPointerException if the specified key is null
4435 >         */
4436          public boolean contains(Object o) { return map.containsKey(o); }
4187        public boolean remove(Object o)   { return map.remove(o) != null; }
4437  
4438          /**
4439 <         * Returns a "weakly consistent" iterator that will never
4440 <         * throw {@link ConcurrentModificationException}, and
4441 <         * guarantees to traverse elements as they existed upon
4193 <         * construction of the iterator, and may (but is not
4194 <         * guaranteed to) reflect any modifications subsequent to
4195 <         * construction.
4439 >         * Removes the key from this map view, by removing the key (and its
4440 >         * corresponding value) from the backing map.  This method does
4441 >         * nothing if the key is not in the map.
4442           *
4443 <         * @return an iterator over the keys of this map
4443 >         * @param  o the key to be removed from the backing map
4444 >         * @return {@code true} if the backing map contained the specified key
4445 >         * @throws NullPointerException if the specified key is null
4446 >         */
4447 >        public boolean remove(Object o) { return map.remove(o) != null; }
4448 >
4449 >        /**
4450 >         * @return an iterator over the keys of the backing map
4451 >         */
4452 >        public Iterator<K> iterator() {
4453 >            Node<K,V>[] t;
4454 >            ConcurrentHashMapV8<K,V> m = map;
4455 >            int f = (t = m.table) == null ? 0 : t.length;
4456 >            return new KeyIterator<K,V>(t, f, 0, f, m);
4457 >        }
4458 >
4459 >        /**
4460 >         * Adds the specified key to this set view by mapping the key to
4461 >         * the default mapped value in the backing map, if defined.
4462 >         *
4463 >         * @param e key to be added
4464 >         * @return {@code true} if this set changed as a result of the call
4465 >         * @throws NullPointerException if the specified key is null
4466 >         * @throws UnsupportedOperationException if no default mapped value
4467 >         * for additions was provided
4468           */
4199        public Iterator<K> iterator()     { return new KeyIterator<K,V>(map); }
4469          public boolean add(K e) {
4470              V v;
4471              if ((v = value) == null)
4472                  throw new UnsupportedOperationException();
4473 <            if (e == null)
4205 <                throw new NullPointerException();
4206 <            return map.internalPutIfAbsent(e, v) == null;
4473 >            return map.putVal(e, v, true) == null;
4474          }
4475 +
4476 +        /**
4477 +         * Adds all of the elements in the specified collection to this set,
4478 +         * as if by calling {@link #add} on each one.
4479 +         *
4480 +         * @param c the elements to be inserted into this set
4481 +         * @return {@code true} if this set changed as a result of the call
4482 +         * @throws NullPointerException if the collection or any of its
4483 +         * elements are {@code null}
4484 +         * @throws UnsupportedOperationException if no default mapped value
4485 +         * for additions was provided
4486 +         */
4487          public boolean addAll(Collection<? extends K> c) {
4488              boolean added = false;
4489              V v;
4490              if ((v = value) == null)
4491                  throw new UnsupportedOperationException();
4492              for (K e : c) {
4493 <                if (e == null)
4215 <                    throw new NullPointerException();
4216 <                if (map.internalPutIfAbsent(e, v) == null)
4493 >                if (map.putVal(e, v, true) == null)
4494                      added = true;
4495              }
4496              return added;
4497          }
4498 +
4499 +        public int hashCode() {
4500 +            int h = 0;
4501 +            for (K e : this)
4502 +                h += e.hashCode();
4503 +            return h;
4504 +        }
4505 +
4506          public boolean equals(Object o) {
4507              Set<?> c;
4508              return ((o instanceof Set) &&
# Line 4225 | Line 4510 | public class ConcurrentHashMapV8<K, V>
4510                       (containsAll(c) && c.containsAll(this))));
4511          }
4512  
4513 <        /**
4514 <         * Performs the given action for each key.
4515 <         *
4516 <         * @param action the action
4517 <         */
4518 <        public void forEach(Action<K> action) {
4234 <            ForkJoinTasks.forEachKey
4235 <                (map, action).invoke();
4513 >        public ConcurrentHashMapSpliterator<K> spliterator() {
4514 >            Node<K,V>[] t;
4515 >            ConcurrentHashMapV8<K,V> m = map;
4516 >            long n = m.sumCount();
4517 >            int f = (t = m.table) == null ? 0 : t.length;
4518 >            return new KeySpliterator<K,V>(t, f, 0, f, n < 0L ? 0L : n);
4519          }
4520  
4521 <        /**
4522 <         * Performs the given action for each non-null transformation
4523 <         * of each key.
4524 <         *
4525 <         * @param transformer a function returning the transformation
4526 <         * for an element, or null of there is no transformation (in
4527 <         * which case the action is not applied).
4528 <         * @param action the action
4246 <         */
4247 <        public <U> void forEach(Fun<? super K, ? extends U> transformer,
4248 <                                Action<U> action) {
4249 <            ForkJoinTasks.forEachKey
4250 <                (map, transformer, action).invoke();
4251 <        }
4252 <
4253 <        /**
4254 <         * Returns a non-null result from applying the given search
4255 <         * function on each key, or null if none. Upon success,
4256 <         * further element processing is suppressed and the results of
4257 <         * any other parallel invocations of the search function are
4258 <         * ignored.
4259 <         *
4260 <         * @param searchFunction a function returning a non-null
4261 <         * result on success, else null
4262 <         * @return a non-null result from applying the given search
4263 <         * function on each key, or null if none
4264 <         */
4265 <        public <U> U search(Fun<? super K, ? extends U> searchFunction) {
4266 <            return ForkJoinTasks.searchKeys
4267 <                (map, searchFunction).invoke();
4268 <        }
4269 <
4270 <        /**
4271 <         * Returns the result of accumulating all keys using the given
4272 <         * reducer to combine values, or null if none.
4273 <         *
4274 <         * @param reducer a commutative associative combining function
4275 <         * @return the result of accumulating all keys using the given
4276 <         * reducer to combine values, or null if none
4277 <         */
4278 <        public K reduce(BiFun<? super K, ? super K, ? extends K> reducer) {
4279 <            return ForkJoinTasks.reduceKeys
4280 <                (map, reducer).invoke();
4281 <        }
4282 <
4283 <        /**
4284 <         * Returns the result of accumulating the given transformation
4285 <         * of all keys using the given reducer to combine values, and
4286 <         * the given basis as an identity value.
4287 <         *
4288 <         * @param transformer a function returning the transformation
4289 <         * for an element
4290 <         * @param basis the identity (initial default value) for the reduction
4291 <         * @param reducer a commutative associative combining function
4292 <         * @return  the result of accumulating the given transformation
4293 <         * of all keys
4294 <         */
4295 <        public double reduceToDouble(ObjectToDouble<? super K> transformer,
4296 <                                     double basis,
4297 <                                     DoubleByDoubleToDouble reducer) {
4298 <            return ForkJoinTasks.reduceKeysToDouble
4299 <                (map, transformer, basis, reducer).invoke();
4300 <        }
4301 <
4302 <
4303 <        /**
4304 <         * Returns the result of accumulating the given transformation
4305 <         * of all keys using the given reducer to combine values, and
4306 <         * the given basis as an identity value.
4307 <         *
4308 <         * @param transformer a function returning the transformation
4309 <         * for an element
4310 <         * @param basis the identity (initial default value) for the reduction
4311 <         * @param reducer a commutative associative combining function
4312 <         * @return the result of accumulating the given transformation
4313 <         * of all keys
4314 <         */
4315 <        public long reduceToLong(ObjectToLong<? super K> transformer,
4316 <                                 long basis,
4317 <                                 LongByLongToLong reducer) {
4318 <            return ForkJoinTasks.reduceKeysToLong
4319 <                (map, transformer, basis, reducer).invoke();
4320 <        }
4321 <
4322 <        /**
4323 <         * Returns the result of accumulating the given transformation
4324 <         * of all keys using the given reducer to combine values, and
4325 <         * the given basis as an identity value.
4326 <         *
4327 <         * @param transformer a function returning the transformation
4328 <         * for an element
4329 <         * @param basis the identity (initial default value) for the reduction
4330 <         * @param reducer a commutative associative combining function
4331 <         * @return the result of accumulating the given transformation
4332 <         * of all keys
4333 <         */
4334 <        public int reduceToInt(ObjectToInt<? super K> transformer,
4335 <                               int basis,
4336 <                               IntByIntToInt reducer) {
4337 <            return ForkJoinTasks.reduceKeysToInt
4338 <                (map, transformer, basis, reducer).invoke();
4521 >        public void forEach(Action<? super K> action) {
4522 >            if (action == null) throw new NullPointerException();
4523 >            Node<K,V>[] t;
4524 >            if ((t = map.table) != null) {
4525 >                Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length);
4526 >                for (Node<K,V> p; (p = it.advance()) != null; )
4527 >                    action.apply(p.key);
4528 >            }
4529          }
4340
4530      }
4531  
4532      /**
4533       * A view of a ConcurrentHashMapV8 as a {@link Collection} of
4534       * values, in which additions are disabled. This class cannot be
4535 <     * directly instantiated. See {@link #values},
4347 <     *
4348 <     * <p>The view's {@code iterator} is a "weakly consistent" iterator
4349 <     * that will never throw {@link ConcurrentModificationException},
4350 <     * and guarantees to traverse elements as they existed upon
4351 <     * construction of the iterator, and may (but is not guaranteed to)
4352 <     * reflect any modifications subsequent to construction.
4535 >     * directly instantiated. See {@link #values()}.
4536       */
4537 <    public static final class ValuesView<K,V> extends CHMView<K,V>
4538 <        implements Collection<V> {
4539 <        ValuesView(ConcurrentHashMapV8<K, V> map)   { super(map); }
4540 <        public final boolean contains(Object o) { return map.containsValue(o); }
4537 >    static final class ValuesView<K,V> extends CollectionView<K,V,V>
4538 >        implements Collection<V>, java.io.Serializable {
4539 >        private static final long serialVersionUID = 2249069246763182397L;
4540 >        ValuesView(ConcurrentHashMapV8<K,V> map) { super(map); }
4541 >        public final boolean contains(Object o) {
4542 >            return map.containsValue(o);
4543 >        }
4544 >
4545          public final boolean remove(Object o) {
4546              if (o != null) {
4547 <                Iterator<V> it = new ValueIterator<K,V>(map);
4361 <                while (it.hasNext()) {
4547 >                for (Iterator<V> it = iterator(); it.hasNext();) {
4548                      if (o.equals(it.next())) {
4549                          it.remove();
4550                          return true;
# Line 4368 | Line 4554 | public class ConcurrentHashMapV8<K, V>
4554              return false;
4555          }
4556  
4371        /**
4372         * Returns a "weakly consistent" iterator that will never
4373         * throw {@link ConcurrentModificationException}, and
4374         * guarantees to traverse elements as they existed upon
4375         * construction of the iterator, and may (but is not
4376         * guaranteed to) reflect any modifications subsequent to
4377         * construction.
4378         *
4379         * @return an iterator over the values of this map
4380         */
4557          public final Iterator<V> iterator() {
4558 <            return new ValueIterator<K,V>(map);
4558 >            ConcurrentHashMapV8<K,V> m = map;
4559 >            Node<K,V>[] t;
4560 >            int f = (t = m.table) == null ? 0 : t.length;
4561 >            return new ValueIterator<K,V>(t, f, 0, f, m);
4562          }
4563 +
4564          public final boolean add(V e) {
4565              throw new UnsupportedOperationException();
4566          }
# Line 4388 | Line 4568 | public class ConcurrentHashMapV8<K, V>
4568              throw new UnsupportedOperationException();
4569          }
4570  
4571 <        /**
4572 <         * Performs the given action for each value.
4573 <         *
4574 <         * @param action the action
4575 <         */
4576 <        public void forEach(Action<V> action) {
4397 <            ForkJoinTasks.forEachValue
4398 <                (map, action).invoke();
4399 <        }
4400 <
4401 <        /**
4402 <         * Performs the given action for each non-null transformation
4403 <         * of each value.
4404 <         *
4405 <         * @param transformer a function returning the transformation
4406 <         * for an element, or null of there is no transformation (in
4407 <         * which case the action is not applied).
4408 <         */
4409 <        public <U> void forEach(Fun<? super V, ? extends U> transformer,
4410 <                                     Action<U> action) {
4411 <            ForkJoinTasks.forEachValue
4412 <                (map, transformer, action).invoke();
4413 <        }
4414 <
4415 <        /**
4416 <         * Returns a non-null result from applying the given search
4417 <         * function on each value, or null if none.  Upon success,
4418 <         * further element processing is suppressed and the results of
4419 <         * any other parallel invocations of the search function are
4420 <         * ignored.
4421 <         *
4422 <         * @param searchFunction a function returning a non-null
4423 <         * result on success, else null
4424 <         * @return a non-null result from applying the given search
4425 <         * function on each value, or null if none
4426 <         *
4427 <         */
4428 <        public <U> U search(Fun<? super V, ? extends U> searchFunction) {
4429 <            return ForkJoinTasks.searchValues
4430 <                (map, searchFunction).invoke();
4431 <        }
4432 <
4433 <        /**
4434 <         * Returns the result of accumulating all values using the
4435 <         * given reducer to combine values, or null if none.
4436 <         *
4437 <         * @param reducer a commutative associative combining function
4438 <         * @return  the result of accumulating all values
4439 <         */
4440 <        public V reduce(BiFun<? super V, ? super V, ? extends V> reducer) {
4441 <            return ForkJoinTasks.reduceValues
4442 <                (map, reducer).invoke();
4443 <        }
4444 <
4445 <        /**
4446 <         * Returns the result of accumulating the given transformation
4447 <         * of all values using the given reducer to combine values, or
4448 <         * null if none.
4449 <         *
4450 <         * @param transformer a function returning the transformation
4451 <         * for an element, or null of there is no transformation (in
4452 <         * which case it is not combined).
4453 <         * @param reducer a commutative associative combining function
4454 <         * @return the result of accumulating the given transformation
4455 <         * of all values
4456 <         */
4457 <        public <U> U reduce(Fun<? super V, ? extends U> transformer,
4458 <                            BiFun<? super U, ? super U, ? extends U> reducer) {
4459 <            return ForkJoinTasks.reduceValues
4460 <                (map, transformer, reducer).invoke();
4571 >        public ConcurrentHashMapSpliterator<V> spliterator() {
4572 >            Node<K,V>[] t;
4573 >            ConcurrentHashMapV8<K,V> m = map;
4574 >            long n = m.sumCount();
4575 >            int f = (t = m.table) == null ? 0 : t.length;
4576 >            return new ValueSpliterator<K,V>(t, f, 0, f, n < 0L ? 0L : n);
4577          }
4578  
4579 <        /**
4580 <         * Returns the result of accumulating the given transformation
4581 <         * of all values using the given reducer to combine values,
4582 <         * and the given basis as an identity value.
4583 <         *
4584 <         * @param transformer a function returning the transformation
4585 <         * for an element
4586 <         * @param basis the identity (initial default value) for the reduction
4471 <         * @param reducer a commutative associative combining function
4472 <         * @return the result of accumulating the given transformation
4473 <         * of all values
4474 <         */
4475 <        public double reduceToDouble(ObjectToDouble<? super V> transformer,
4476 <                                     double basis,
4477 <                                     DoubleByDoubleToDouble reducer) {
4478 <            return ForkJoinTasks.reduceValuesToDouble
4479 <                (map, transformer, basis, reducer).invoke();
4480 <        }
4481 <
4482 <        /**
4483 <         * Returns the result of accumulating the given transformation
4484 <         * of all values using the given reducer to combine values,
4485 <         * and the given basis as an identity value.
4486 <         *
4487 <         * @param transformer a function returning the transformation
4488 <         * for an element
4489 <         * @param basis the identity (initial default value) for the reduction
4490 <         * @param reducer a commutative associative combining function
4491 <         * @return the result of accumulating the given transformation
4492 <         * of all values
4493 <         */
4494 <        public long reduceToLong(ObjectToLong<? super V> transformer,
4495 <                                 long basis,
4496 <                                 LongByLongToLong reducer) {
4497 <            return ForkJoinTasks.reduceValuesToLong
4498 <                (map, transformer, basis, reducer).invoke();
4499 <        }
4500 <
4501 <        /**
4502 <         * Returns the result of accumulating the given transformation
4503 <         * of all values using the given reducer to combine values,
4504 <         * and the given basis as an identity value.
4505 <         *
4506 <         * @param transformer a function returning the transformation
4507 <         * for an element
4508 <         * @param basis the identity (initial default value) for the reduction
4509 <         * @param reducer a commutative associative combining function
4510 <         * @return the result of accumulating the given transformation
4511 <         * of all values
4512 <         */
4513 <        public int reduceToInt(ObjectToInt<? super V> transformer,
4514 <                               int basis,
4515 <                               IntByIntToInt reducer) {
4516 <            return ForkJoinTasks.reduceValuesToInt
4517 <                (map, transformer, basis, reducer).invoke();
4579 >        public void forEach(Action<? super V> action) {
4580 >            if (action == null) throw new NullPointerException();
4581 >            Node<K,V>[] t;
4582 >            if ((t = map.table) != null) {
4583 >                Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length);
4584 >                for (Node<K,V> p; (p = it.advance()) != null; )
4585 >                    action.apply(p.val);
4586 >            }
4587          }
4519
4588      }
4589  
4590      /**
4591       * A view of a ConcurrentHashMapV8 as a {@link Set} of (key, value)
4592       * entries.  This class cannot be directly instantiated. See
4593 <     * {@link #entrySet}.
4593 >     * {@link #entrySet()}.
4594       */
4595 <    public static final class EntrySetView<K,V> extends CHMView<K,V>
4596 <        implements Set<Map.Entry<K,V>> {
4597 <        EntrySetView(ConcurrentHashMapV8<K, V> map) { super(map); }
4598 <        public final boolean contains(Object o) {
4595 >    static final class EntrySetView<K,V> extends CollectionView<K,V,Map.Entry<K,V>>
4596 >        implements Set<Map.Entry<K,V>>, java.io.Serializable {
4597 >        private static final long serialVersionUID = 2249069246763182397L;
4598 >        EntrySetView(ConcurrentHashMapV8<K,V> map) { super(map); }
4599 >
4600 >        public boolean contains(Object o) {
4601              Object k, v, r; Map.Entry<?,?> e;
4602              return ((o instanceof Map.Entry) &&
4603                      (k = (e = (Map.Entry<?,?>)o).getKey()) != null &&
# Line 4535 | Line 4605 | public class ConcurrentHashMapV8<K, V>
4605                      (v = e.getValue()) != null &&
4606                      (v == r || v.equals(r)));
4607          }
4608 <        public final boolean remove(Object o) {
4608 >
4609 >        public boolean remove(Object o) {
4610              Object k, v; Map.Entry<?,?> e;
4611              return ((o instanceof Map.Entry) &&
4612                      (k = (e = (Map.Entry<?,?>)o).getKey()) != null &&
# Line 4544 | Line 4615 | public class ConcurrentHashMapV8<K, V>
4615          }
4616  
4617          /**
4618 <         * Returns a "weakly consistent" iterator that will never
4548 <         * throw {@link ConcurrentModificationException}, and
4549 <         * guarantees to traverse elements as they existed upon
4550 <         * construction of the iterator, and may (but is not
4551 <         * guaranteed to) reflect any modifications subsequent to
4552 <         * construction.
4553 <         *
4554 <         * @return an iterator over the entries of this map
4618 >         * @return an iterator over the entries of the backing map
4619           */
4620 <        public final Iterator<Map.Entry<K,V>> iterator() {
4621 <            return new EntryIterator<K,V>(map);
4620 >        public Iterator<Map.Entry<K,V>> iterator() {
4621 >            ConcurrentHashMapV8<K,V> m = map;
4622 >            Node<K,V>[] t;
4623 >            int f = (t = m.table) == null ? 0 : t.length;
4624 >            return new EntryIterator<K,V>(t, f, 0, f, m);
4625          }
4626  
4627 <        public final boolean add(Entry<K,V> e) {
4628 <            K key = e.getKey();
4562 <            V value = e.getValue();
4563 <            if (key == null || value == null)
4564 <                throw new NullPointerException();
4565 <            return map.internalPut(key, value) == null;
4627 >        public boolean add(Entry<K,V> e) {
4628 >            return map.putVal(e.getKey(), e.getValue(), false) == null;
4629          }
4630 <        public final boolean addAll(Collection<? extends Entry<K,V>> c) {
4630 >
4631 >        public boolean addAll(Collection<? extends Entry<K,V>> c) {
4632              boolean added = false;
4633              for (Entry<K,V> e : c) {
4634                  if (add(e))
# Line 4572 | Line 4636 | public class ConcurrentHashMapV8<K, V>
4636              }
4637              return added;
4638          }
4639 <        public boolean equals(Object o) {
4639 >
4640 >        public final int hashCode() {
4641 >            int h = 0;
4642 >            Node<K,V>[] t;
4643 >            if ((t = map.table) != null) {
4644 >                Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length);
4645 >                for (Node<K,V> p; (p = it.advance()) != null; ) {
4646 >                    h += p.hashCode();
4647 >                }
4648 >            }
4649 >            return h;
4650 >        }
4651 >
4652 >        public final boolean equals(Object o) {
4653              Set<?> c;
4654              return ((o instanceof Set) &&
4655                      ((c = (Set<?>)o) == this ||
4656                       (containsAll(c) && c.containsAll(this))));
4657          }
4658  
4659 <        /**
4660 <         * Performs the given action for each entry.
4661 <         *
4662 <         * @param action the action
4663 <         */
4664 <        public void forEach(Action<Map.Entry<K,V>> action) {
4588 <            ForkJoinTasks.forEachEntry
4589 <                (map, action).invoke();
4590 <        }
4591 <
4592 <        /**
4593 <         * Performs the given action for each non-null transformation
4594 <         * of each entry.
4595 <         *
4596 <         * @param transformer a function returning the transformation
4597 <         * for an element, or null of there is no transformation (in
4598 <         * which case the action is not applied).
4599 <         * @param action the action
4600 <         */
4601 <        public <U> void forEach(Fun<Map.Entry<K,V>, ? extends U> transformer,
4602 <                                Action<U> action) {
4603 <            ForkJoinTasks.forEachEntry
4604 <                (map, transformer, action).invoke();
4605 <        }
4606 <
4607 <        /**
4608 <         * Returns a non-null result from applying the given search
4609 <         * function on each entry, or null if none.  Upon success,
4610 <         * further element processing is suppressed and the results of
4611 <         * any other parallel invocations of the search function are
4612 <         * ignored.
4613 <         *
4614 <         * @param searchFunction a function returning a non-null
4615 <         * result on success, else null
4616 <         * @return a non-null result from applying the given search
4617 <         * function on each entry, or null if none
4618 <         */
4619 <        public <U> U search(Fun<Map.Entry<K,V>, ? extends U> searchFunction) {
4620 <            return ForkJoinTasks.searchEntries
4621 <                (map, searchFunction).invoke();
4622 <        }
4623 <
4624 <        /**
4625 <         * Returns the result of accumulating all entries using the
4626 <         * given reducer to combine values, or null if none.
4627 <         *
4628 <         * @param reducer a commutative associative combining function
4629 <         * @return the result of accumulating all entries
4630 <         */
4631 <        public Map.Entry<K,V> reduce(BiFun<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer) {
4632 <            return ForkJoinTasks.reduceEntries
4633 <                (map, reducer).invoke();
4634 <        }
4635 <
4636 <        /**
4637 <         * Returns the result of accumulating the given transformation
4638 <         * of all entries using the given reducer to combine values,
4639 <         * or null if none.
4640 <         *
4641 <         * @param transformer a function returning the transformation
4642 <         * for an element, or null of there is no transformation (in
4643 <         * which case it is not combined).
4644 <         * @param reducer a commutative associative combining function
4645 <         * @return the result of accumulating the given transformation
4646 <         * of all entries
4647 <         */
4648 <        public <U> U reduce(Fun<Map.Entry<K,V>, ? extends U> transformer,
4649 <                            BiFun<? super U, ? super U, ? extends U> reducer) {
4650 <            return ForkJoinTasks.reduceEntries
4651 <                (map, transformer, reducer).invoke();
4652 <        }
4653 <
4654 <        /**
4655 <         * Returns the result of accumulating the given transformation
4656 <         * of all entries using the given reducer to combine values,
4657 <         * and the given basis as an identity value.
4658 <         *
4659 <         * @param transformer a function returning the transformation
4660 <         * for an element
4661 <         * @param basis the identity (initial default value) for the reduction
4662 <         * @param reducer a commutative associative combining function
4663 <         * @return the result of accumulating the given transformation
4664 <         * of all entries
4665 <         */
4666 <        public double reduceToDouble(ObjectToDouble<Map.Entry<K,V>> transformer,
4667 <                                     double basis,
4668 <                                     DoubleByDoubleToDouble reducer) {
4669 <            return ForkJoinTasks.reduceEntriesToDouble
4670 <                (map, transformer, basis, reducer).invoke();
4671 <        }
4672 <
4673 <        /**
4674 <         * Returns the result of accumulating the given transformation
4675 <         * of all entries using the given reducer to combine values,
4676 <         * and the given basis as an identity value.
4677 <         *
4678 <         * @param transformer a function returning the transformation
4679 <         * for an element
4680 <         * @param basis the identity (initial default value) for the reduction
4681 <         * @param reducer a commutative associative combining function
4682 <         * @return  the result of accumulating the given transformation
4683 <         * of all entries
4684 <         */
4685 <        public long reduceToLong(ObjectToLong<Map.Entry<K,V>> transformer,
4686 <                                 long basis,
4687 <                                 LongByLongToLong reducer) {
4688 <            return ForkJoinTasks.reduceEntriesToLong
4689 <                (map, transformer, basis, reducer).invoke();
4659 >        public ConcurrentHashMapSpliterator<Map.Entry<K,V>> spliterator() {
4660 >            Node<K,V>[] t;
4661 >            ConcurrentHashMapV8<K,V> m = map;
4662 >            long n = m.sumCount();
4663 >            int f = (t = m.table) == null ? 0 : t.length;
4664 >            return new EntrySpliterator<K,V>(t, f, 0, f, n < 0L ? 0L : n, m);
4665          }
4666  
4667 <        /**
4668 <         * Returns the result of accumulating the given transformation
4669 <         * of all entries using the given reducer to combine values,
4670 <         * and the given basis as an identity value.
4671 <         *
4672 <         * @param transformer a function returning the transformation
4673 <         * for an element
4674 <         * @param basis the identity (initial default value) for the reduction
4700 <         * @param reducer a commutative associative combining function
4701 <         * @return the result of accumulating the given transformation
4702 <         * of all entries
4703 <         */
4704 <        public int reduceToInt(ObjectToInt<Map.Entry<K,V>> transformer,
4705 <                               int basis,
4706 <                               IntByIntToInt reducer) {
4707 <            return ForkJoinTasks.reduceEntriesToInt
4708 <                (map, transformer, basis, reducer).invoke();
4667 >        public void forEach(Action<? super Map.Entry<K,V>> action) {
4668 >            if (action == null) throw new NullPointerException();
4669 >            Node<K,V>[] t;
4670 >            if ((t = map.table) != null) {
4671 >                Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length);
4672 >                for (Node<K,V> p; (p = it.advance()) != null; )
4673 >                    action.apply(new MapEntry<K,V>(p.key, p.val, map));
4674 >            }
4675          }
4676  
4677      }
4678  
4679 <    // ---------------------------------------------------------------------
4679 >    // -------------------------------------------------------
4680  
4681      /**
4682 <     * Predefined tasks for performing bulk parallel operations on
4683 <     * ConcurrentHashMapV8s. These tasks follow the forms and rules used
4718 <     * for bulk operations. Each method has the same name, but returns
4719 <     * a task rather than invoking it. These methods may be useful in
4720 <     * custom applications such as submitting a task without waiting
4721 <     * for completion, using a custom pool, or combining with other
4722 <     * tasks.
4682 >     * Base class for bulk tasks. Repeats some fields and code from
4683 >     * class Traverser, because we need to subclass CountedCompleter.
4684       */
4685 <    public static class ForkJoinTasks {
4686 <        private ForkJoinTasks() {}
4687 <
4688 <        /**
4689 <         * Returns a task that when invoked, performs the given
4690 <         * action for each (key, value)
4691 <         *
4692 <         * @param map the map
4693 <         * @param action the action
4694 <         * @return the task
4695 <         */
4696 <        public static <K,V> ForkJoinTask<Void> forEach
4697 <            (ConcurrentHashMapV8<K,V> map,
4698 <             BiAction<K,V> action) {
4699 <            if (action == null) throw new NullPointerException();
4700 <            return new ForEachMappingTask<K,V>(map, null, -1, action);
4701 <        }
4702 <
4703 <        /**
4704 <         * Returns a task that when invoked, performs the given
4705 <         * action for each non-null transformation of each (key, value)
4745 <         *
4746 <         * @param map the map
4747 <         * @param transformer a function returning the transformation
4748 <         * for an element, or null if there is no transformation (in
4749 <         * which case the action is not applied)
4750 <         * @param action the action
4751 <         * @return the task
4752 <         */
4753 <        public static <K,V,U> ForkJoinTask<Void> forEach
4754 <            (ConcurrentHashMapV8<K,V> map,
4755 <             BiFun<? super K, ? super V, ? extends U> transformer,
4756 <             Action<U> action) {
4757 <            if (transformer == null || action == null)
4758 <                throw new NullPointerException();
4759 <            return new ForEachTransformedMappingTask<K,V,U>
4760 <                (map, null, -1, transformer, action);
4761 <        }
4762 <
4763 <        /**
4764 <         * Returns a task that when invoked, returns a non-null result
4765 <         * from applying the given search function on each (key,
4766 <         * value), or null if none. Upon success, further element
4767 <         * processing is suppressed and the results of any other
4768 <         * parallel invocations of the search function are ignored.
4769 <         *
4770 <         * @param map the map
4771 <         * @param searchFunction a function returning a non-null
4772 <         * result on success, else null
4773 <         * @return the task
4774 <         */
4775 <        public static <K,V,U> ForkJoinTask<U> search
4776 <            (ConcurrentHashMapV8<K,V> map,
4777 <             BiFun<? super K, ? super V, ? extends U> searchFunction) {
4778 <            if (searchFunction == null) throw new NullPointerException();
4779 <            return new SearchMappingsTask<K,V,U>
4780 <                (map, null, -1, searchFunction,
4781 <                 new AtomicReference<U>());
4782 <        }
4783 <
4784 <        /**
4785 <         * Returns a task that when invoked, returns the result of
4786 <         * accumulating the given transformation of all (key, value) pairs
4787 <         * using the given reducer to combine values, or null if none.
4788 <         *
4789 <         * @param map the map
4790 <         * @param transformer a function returning the transformation
4791 <         * for an element, or null if there is no transformation (in
4792 <         * which case it is not combined).
4793 <         * @param reducer a commutative associative combining function
4794 <         * @return the task
4795 <         */
4796 <        public static <K,V,U> ForkJoinTask<U> reduce
4797 <            (ConcurrentHashMapV8<K,V> map,
4798 <             BiFun<? super K, ? super V, ? extends U> transformer,
4799 <             BiFun<? super U, ? super U, ? extends U> reducer) {
4800 <            if (transformer == null || reducer == null)
4801 <                throw new NullPointerException();
4802 <            return new MapReduceMappingsTask<K,V,U>
4803 <                (map, null, -1, null, transformer, reducer);
4804 <        }
4805 <
4806 <        /**
4807 <         * Returns a task that when invoked, returns the result of
4808 <         * accumulating the given transformation of all (key, value) pairs
4809 <         * using the given reducer to combine values, and the given
4810 <         * basis as an identity value.
4811 <         *
4812 <         * @param map the map
4813 <         * @param transformer a function returning the transformation
4814 <         * for an element
4815 <         * @param basis the identity (initial default value) for the reduction
4816 <         * @param reducer a commutative associative combining function
4817 <         * @return the task
4818 <         */
4819 <        public static <K,V> ForkJoinTask<Double> reduceToDouble
4820 <            (ConcurrentHashMapV8<K,V> map,
4821 <             ObjectByObjectToDouble<? super K, ? super V> transformer,
4822 <             double basis,
4823 <             DoubleByDoubleToDouble reducer) {
4824 <            if (transformer == null || reducer == null)
4825 <                throw new NullPointerException();
4826 <            return new MapReduceMappingsToDoubleTask<K,V>
4827 <                (map, null, -1, null, transformer, basis, reducer);
4828 <        }
4829 <
4830 <        /**
4831 <         * Returns a task that when invoked, returns the result of
4832 <         * accumulating the given transformation of all (key, value) pairs
4833 <         * using the given reducer to combine values, and the given
4834 <         * basis as an identity value.
4835 <         *
4836 <         * @param map the map
4837 <         * @param transformer a function returning the transformation
4838 <         * for an element
4839 <         * @param basis the identity (initial default value) for the reduction
4840 <         * @param reducer a commutative associative combining function
4841 <         * @return the task
4842 <         */
4843 <        public static <K,V> ForkJoinTask<Long> reduceToLong
4844 <            (ConcurrentHashMapV8<K,V> map,
4845 <             ObjectByObjectToLong<? super K, ? super V> transformer,
4846 <             long basis,
4847 <             LongByLongToLong reducer) {
4848 <            if (transformer == null || reducer == null)
4849 <                throw new NullPointerException();
4850 <            return new MapReduceMappingsToLongTask<K,V>
4851 <                (map, null, -1, null, transformer, basis, reducer);
4852 <        }
4853 <
4854 <        /**
4855 <         * Returns a task that when invoked, returns the result of
4856 <         * accumulating the given transformation of all (key, value) pairs
4857 <         * using the given reducer to combine values, and the given
4858 <         * basis as an identity value.
4859 <         *
4860 <         * @param transformer a function returning the transformation
4861 <         * for an element
4862 <         * @param basis the identity (initial default value) for the reduction
4863 <         * @param reducer a commutative associative combining function
4864 <         * @return the task
4865 <         */
4866 <        public static <K,V> ForkJoinTask<Integer> reduceToInt
4867 <            (ConcurrentHashMapV8<K,V> map,
4868 <             ObjectByObjectToInt<? super K, ? super V> transformer,
4869 <             int basis,
4870 <             IntByIntToInt reducer) {
4871 <            if (transformer == null || reducer == null)
4872 <                throw new NullPointerException();
4873 <            return new MapReduceMappingsToIntTask<K,V>
4874 <                (map, null, -1, null, transformer, basis, reducer);
4875 <        }
4876 <
4877 <        /**
4878 <         * Returns a task that when invoked, performs the given action
4879 <         * for each key.
4880 <         *
4881 <         * @param map the map
4882 <         * @param action the action
4883 <         * @return the task
4884 <         */
4885 <        public static <K,V> ForkJoinTask<Void> forEachKey
4886 <            (ConcurrentHashMapV8<K,V> map,
4887 <             Action<K> action) {
4888 <            if (action == null) throw new NullPointerException();
4889 <            return new ForEachKeyTask<K,V>(map, null, -1, action);
4890 <        }
4891 <
4892 <        /**
4893 <         * Returns a task that when invoked, performs the given action
4894 <         * for each non-null transformation of each key.
4895 <         *
4896 <         * @param map the map
4897 <         * @param transformer a function returning the transformation
4898 <         * for an element, or null if there is no transformation (in
4899 <         * which case the action is not applied)
4900 <         * @param action the action
4901 <         * @return the task
4902 <         */
4903 <        public static <K,V,U> ForkJoinTask<Void> forEachKey
4904 <            (ConcurrentHashMapV8<K,V> map,
4905 <             Fun<? super K, ? extends U> transformer,
4906 <             Action<U> action) {
4907 <            if (transformer == null || action == null)
4908 <                throw new NullPointerException();
4909 <            return new ForEachTransformedKeyTask<K,V,U>
4910 <                (map, null, -1, transformer, action);
4911 <        }
4912 <
4913 <        /**
4914 <         * Returns a task that when invoked, returns a non-null result
4915 <         * from applying the given search function on each key, or
4916 <         * null if none.  Upon success, further element processing is
4917 <         * suppressed and the results of any other parallel
4918 <         * invocations of the search function are ignored.
4919 <         *
4920 <         * @param map the map
4921 <         * @param searchFunction a function returning a non-null
4922 <         * result on success, else null
4923 <         * @return the task
4924 <         */
4925 <        public static <K,V,U> ForkJoinTask<U> searchKeys
4926 <            (ConcurrentHashMapV8<K,V> map,
4927 <             Fun<? super K, ? extends U> searchFunction) {
4928 <            if (searchFunction == null) throw new NullPointerException();
4929 <            return new SearchKeysTask<K,V,U>
4930 <                (map, null, -1, searchFunction,
4931 <                 new AtomicReference<U>());
4932 <        }
4933 <
4934 <        /**
4935 <         * Returns a task that when invoked, returns the result of
4936 <         * accumulating all keys using the given reducer to combine
4937 <         * values, or null if none.
4938 <         *
4939 <         * @param map the map
4940 <         * @param reducer a commutative associative combining function
4941 <         * @return the task
4942 <         */
4943 <        public static <K,V> ForkJoinTask<K> reduceKeys
4944 <            (ConcurrentHashMapV8<K,V> map,
4945 <             BiFun<? super K, ? super K, ? extends K> reducer) {
4946 <            if (reducer == null) throw new NullPointerException();
4947 <            return new ReduceKeysTask<K,V>
4948 <                (map, null, -1, null, reducer);
4949 <        }
4950 <
4951 <        /**
4952 <         * Returns a task that when invoked, returns the result of
4953 <         * accumulating the given transformation of all keys using the given
4954 <         * reducer to combine values, or null if none.
4955 <         *
4956 <         * @param map the map
4957 <         * @param transformer a function returning the transformation
4958 <         * for an element, or null if there is no transformation (in
4959 <         * which case it is not combined).
4960 <         * @param reducer a commutative associative combining function
4961 <         * @return the task
4962 <         */
4963 <        public static <K,V,U> ForkJoinTask<U> reduceKeys
4964 <            (ConcurrentHashMapV8<K,V> map,
4965 <             Fun<? super K, ? extends U> transformer,
4966 <             BiFun<? super U, ? super U, ? extends U> reducer) {
4967 <            if (transformer == null || reducer == null)
4968 <                throw new NullPointerException();
4969 <            return new MapReduceKeysTask<K,V,U>
4970 <                (map, null, -1, null, transformer, reducer);
4971 <        }
4972 <
4973 <        /**
4974 <         * Returns a task that when invoked, returns the result of
4975 <         * accumulating the given transformation of all keys using the given
4976 <         * reducer to combine values, and the given basis as an
4977 <         * identity value.
4978 <         *
4979 <         * @param map the map
4980 <         * @param transformer a function returning the transformation
4981 <         * for an element
4982 <         * @param basis the identity (initial default value) for the reduction
4983 <         * @param reducer a commutative associative combining function
4984 <         * @return the task
4985 <         */
4986 <        public static <K,V> ForkJoinTask<Double> reduceKeysToDouble
4987 <            (ConcurrentHashMapV8<K,V> map,
4988 <             ObjectToDouble<? super K> transformer,
4989 <             double basis,
4990 <             DoubleByDoubleToDouble reducer) {
4991 <            if (transformer == null || reducer == null)
4992 <                throw new NullPointerException();
4993 <            return new MapReduceKeysToDoubleTask<K,V>
4994 <                (map, null, -1, null, transformer, basis, reducer);
4995 <        }
4996 <
4997 <        /**
4998 <         * Returns a task that when invoked, returns the result of
4999 <         * accumulating the given transformation of all keys using the given
5000 <         * reducer to combine values, and the given basis as an
5001 <         * identity value.
5002 <         *
5003 <         * @param map the map
5004 <         * @param transformer a function returning the transformation
5005 <         * for an element
5006 <         * @param basis the identity (initial default value) for the reduction
5007 <         * @param reducer a commutative associative combining function
5008 <         * @return the task
5009 <         */
5010 <        public static <K,V> ForkJoinTask<Long> reduceKeysToLong
5011 <            (ConcurrentHashMapV8<K,V> map,
5012 <             ObjectToLong<? super K> transformer,
5013 <             long basis,
5014 <             LongByLongToLong reducer) {
5015 <            if (transformer == null || reducer == null)
5016 <                throw new NullPointerException();
5017 <            return new MapReduceKeysToLongTask<K,V>
5018 <                (map, null, -1, null, transformer, basis, reducer);
5019 <        }
5020 <
5021 <        /**
5022 <         * Returns a task that when invoked, returns the result of
5023 <         * accumulating the given transformation of all keys using the given
5024 <         * reducer to combine values, and the given basis as an
5025 <         * identity value.
5026 <         *
5027 <         * @param map the map
5028 <         * @param transformer a function returning the transformation
5029 <         * for an element
5030 <         * @param basis the identity (initial default value) for the reduction
5031 <         * @param reducer a commutative associative combining function
5032 <         * @return the task
5033 <         */
5034 <        public static <K,V> ForkJoinTask<Integer> reduceKeysToInt
5035 <            (ConcurrentHashMapV8<K,V> map,
5036 <             ObjectToInt<? super K> transformer,
5037 <             int basis,
5038 <             IntByIntToInt reducer) {
5039 <            if (transformer == null || reducer == null)
5040 <                throw new NullPointerException();
5041 <            return new MapReduceKeysToIntTask<K,V>
5042 <                (map, null, -1, null, transformer, basis, reducer);
5043 <        }
5044 <
5045 <        /**
5046 <         * Returns a task that when invoked, performs the given action
5047 <         * for each value.
5048 <         *
5049 <         * @param map the map
5050 <         * @param action the action
5051 <         */
5052 <        public static <K,V> ForkJoinTask<Void> forEachValue
5053 <            (ConcurrentHashMapV8<K,V> map,
5054 <             Action<V> action) {
5055 <            if (action == null) throw new NullPointerException();
5056 <            return new ForEachValueTask<K,V>(map, null, -1, action);
5057 <        }
5058 <
5059 <        /**
5060 <         * Returns a task that when invoked, performs the given action
5061 <         * for each non-null transformation of each value.
5062 <         *
5063 <         * @param map the map
5064 <         * @param transformer a function returning the transformation
5065 <         * for an element, or null if there is no transformation (in
5066 <         * which case the action is not applied)
5067 <         * @param action the action
5068 <         */
5069 <        public static <K,V,U> ForkJoinTask<Void> forEachValue
5070 <            (ConcurrentHashMapV8<K,V> map,
5071 <             Fun<? super V, ? extends U> transformer,
5072 <             Action<U> action) {
5073 <            if (transformer == null || action == null)
5074 <                throw new NullPointerException();
5075 <            return new ForEachTransformedValueTask<K,V,U>
5076 <                (map, null, -1, transformer, action);
5077 <        }
5078 <
5079 <        /**
5080 <         * Returns a task that when invoked, returns a non-null result
5081 <         * from applying the given search function on each value, or
5082 <         * null if none.  Upon success, further element processing is
5083 <         * suppressed and the results of any other parallel
5084 <         * invocations of the search function are ignored.
5085 <         *
5086 <         * @param map the map
5087 <         * @param searchFunction a function returning a non-null
5088 <         * result on success, else null
5089 <         * @return the task
5090 <         */
5091 <        public static <K,V,U> ForkJoinTask<U> searchValues
5092 <            (ConcurrentHashMapV8<K,V> map,
5093 <             Fun<? super V, ? extends U> searchFunction) {
5094 <            if (searchFunction == null) throw new NullPointerException();
5095 <            return new SearchValuesTask<K,V,U>
5096 <                (map, null, -1, searchFunction,
5097 <                 new AtomicReference<U>());
5098 <        }
5099 <
5100 <        /**
5101 <         * Returns a task that when invoked, returns the result of
5102 <         * accumulating all values using the given reducer to combine
5103 <         * values, or null if none.
5104 <         *
5105 <         * @param map the map
5106 <         * @param reducer a commutative associative combining function
5107 <         * @return the task
5108 <         */
5109 <        public static <K,V> ForkJoinTask<V> reduceValues
5110 <            (ConcurrentHashMapV8<K,V> map,
5111 <             BiFun<? super V, ? super V, ? extends V> reducer) {
5112 <            if (reducer == null) throw new NullPointerException();
5113 <            return new ReduceValuesTask<K,V>
5114 <                (map, null, -1, null, reducer);
5115 <        }
5116 <
5117 <        /**
5118 <         * Returns a task that when invoked, returns the result of
5119 <         * accumulating the given transformation of all values using the
5120 <         * given reducer to combine values, or null if none.
5121 <         *
5122 <         * @param map the map
5123 <         * @param transformer a function returning the transformation
5124 <         * for an element, or null if there is no transformation (in
5125 <         * which case it is not combined).
5126 <         * @param reducer a commutative associative combining function
5127 <         * @return the task
5128 <         */
5129 <        public static <K,V,U> ForkJoinTask<U> reduceValues
5130 <            (ConcurrentHashMapV8<K,V> map,
5131 <             Fun<? super V, ? extends U> transformer,
5132 <             BiFun<? super U, ? super U, ? extends U> reducer) {
5133 <            if (transformer == null || reducer == null)
5134 <                throw new NullPointerException();
5135 <            return new MapReduceValuesTask<K,V,U>
5136 <                (map, null, -1, null, transformer, reducer);
5137 <        }
5138 <
5139 <        /**
5140 <         * Returns a task that when invoked, returns the result of
5141 <         * accumulating the given transformation of all values using the
5142 <         * given reducer to combine values, and the given basis as an
5143 <         * identity value.
5144 <         *
5145 <         * @param map the map
5146 <         * @param transformer a function returning the transformation
5147 <         * for an element
5148 <         * @param basis the identity (initial default value) for the reduction
5149 <         * @param reducer a commutative associative combining function
5150 <         * @return the task
5151 <         */
5152 <        public static <K,V> ForkJoinTask<Double> reduceValuesToDouble
5153 <            (ConcurrentHashMapV8<K,V> map,
5154 <             ObjectToDouble<? super V> transformer,
5155 <             double basis,
5156 <             DoubleByDoubleToDouble reducer) {
5157 <            if (transformer == null || reducer == null)
5158 <                throw new NullPointerException();
5159 <            return new MapReduceValuesToDoubleTask<K,V>
5160 <                (map, null, -1, null, transformer, basis, reducer);
5161 <        }
5162 <
5163 <        /**
5164 <         * Returns a task that when invoked, returns the result of
5165 <         * accumulating the given transformation of all values using the
5166 <         * given reducer to combine values, and the given basis as an
5167 <         * identity value.
5168 <         *
5169 <         * @param map the map
5170 <         * @param transformer a function returning the transformation
5171 <         * for an element
5172 <         * @param basis the identity (initial default value) for the reduction
5173 <         * @param reducer a commutative associative combining function
5174 <         * @return the task
5175 <         */
5176 <        public static <K,V> ForkJoinTask<Long> reduceValuesToLong
5177 <            (ConcurrentHashMapV8<K,V> map,
5178 <             ObjectToLong<? super V> transformer,
5179 <             long basis,
5180 <             LongByLongToLong reducer) {
5181 <            if (transformer == null || reducer == null)
5182 <                throw new NullPointerException();
5183 <            return new MapReduceValuesToLongTask<K,V>
5184 <                (map, null, -1, null, transformer, basis, reducer);
5185 <        }
5186 <
5187 <        /**
5188 <         * Returns a task that when invoked, returns the result of
5189 <         * accumulating the given transformation of all values using the
5190 <         * given reducer to combine values, and the given basis as an
5191 <         * identity value.
5192 <         *
5193 <         * @param map the map
5194 <         * @param transformer a function returning the transformation
5195 <         * for an element
5196 <         * @param basis the identity (initial default value) for the reduction
5197 <         * @param reducer a commutative associative combining function
5198 <         * @return the task
5199 <         */
5200 <        public static <K,V> ForkJoinTask<Integer> reduceValuesToInt
5201 <            (ConcurrentHashMapV8<K,V> map,
5202 <             ObjectToInt<? super V> transformer,
5203 <             int basis,
5204 <             IntByIntToInt reducer) {
5205 <            if (transformer == null || reducer == null)
5206 <                throw new NullPointerException();
5207 <            return new MapReduceValuesToIntTask<K,V>
5208 <                (map, null, -1, null, transformer, basis, reducer);
5209 <        }
5210 <
5211 <        /**
5212 <         * Returns a task that when invoked, perform the given action
5213 <         * for each entry.
5214 <         *
5215 <         * @param map the map
5216 <         * @param action the action
5217 <         */
5218 <        public static <K,V> ForkJoinTask<Void> forEachEntry
5219 <            (ConcurrentHashMapV8<K,V> map,
5220 <             Action<Map.Entry<K,V>> action) {
5221 <            if (action == null) throw new NullPointerException();
5222 <            return new ForEachEntryTask<K,V>(map, null, -1, action);
5223 <        }
5224 <
5225 <        /**
5226 <         * Returns a task that when invoked, perform the given action
5227 <         * for each non-null transformation of each entry.
5228 <         *
5229 <         * @param map the map
5230 <         * @param transformer a function returning the transformation
5231 <         * for an element, or null if there is no transformation (in
5232 <         * which case the action is not applied)
5233 <         * @param action the action
5234 <         */
5235 <        public static <K,V,U> ForkJoinTask<Void> forEachEntry
5236 <            (ConcurrentHashMapV8<K,V> map,
5237 <             Fun<Map.Entry<K,V>, ? extends U> transformer,
5238 <             Action<U> action) {
5239 <            if (transformer == null || action == null)
5240 <                throw new NullPointerException();
5241 <            return new ForEachTransformedEntryTask<K,V,U>
5242 <                (map, null, -1, transformer, action);
5243 <        }
5244 <
5245 <        /**
5246 <         * Returns a task that when invoked, returns a non-null result
5247 <         * from applying the given search function on each entry, or
5248 <         * null if none.  Upon success, further element processing is
5249 <         * suppressed and the results of any other parallel
5250 <         * invocations of the search function are ignored.
5251 <         *
5252 <         * @param map the map
5253 <         * @param searchFunction a function returning a non-null
5254 <         * result on success, else null
5255 <         * @return the task
5256 <         */
5257 <        public static <K,V,U> ForkJoinTask<U> searchEntries
5258 <            (ConcurrentHashMapV8<K,V> map,
5259 <             Fun<Map.Entry<K,V>, ? extends U> searchFunction) {
5260 <            if (searchFunction == null) throw new NullPointerException();
5261 <            return new SearchEntriesTask<K,V,U>
5262 <                (map, null, -1, searchFunction,
5263 <                 new AtomicReference<U>());
5264 <        }
5265 <
5266 <        /**
5267 <         * Returns a task that when invoked, returns the result of
5268 <         * accumulating all entries using the given reducer to combine
5269 <         * values, or null if none.
5270 <         *
5271 <         * @param map the map
5272 <         * @param reducer a commutative associative combining function
5273 <         * @return the task
5274 <         */
5275 <        public static <K,V> ForkJoinTask<Map.Entry<K,V>> reduceEntries
5276 <            (ConcurrentHashMapV8<K,V> map,
5277 <             BiFun<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer) {
5278 <            if (reducer == null) throw new NullPointerException();
5279 <            return new ReduceEntriesTask<K,V>
5280 <                (map, null, -1, null, reducer);
5281 <        }
5282 <
5283 <        /**
5284 <         * Returns a task that when invoked, returns the result of
5285 <         * accumulating the given transformation of all entries using the
5286 <         * given reducer to combine values, or null if none.
5287 <         *
5288 <         * @param map the map
5289 <         * @param transformer a function returning the transformation
5290 <         * for an element, or null if there is no transformation (in
5291 <         * which case it is not combined).
5292 <         * @param reducer a commutative associative combining function
5293 <         * @return the task
5294 <         */
5295 <        public static <K,V,U> ForkJoinTask<U> reduceEntries
5296 <            (ConcurrentHashMapV8<K,V> map,
5297 <             Fun<Map.Entry<K,V>, ? extends U> transformer,
5298 <             BiFun<? super U, ? super U, ? extends U> reducer) {
5299 <            if (transformer == null || reducer == null)
5300 <                throw new NullPointerException();
5301 <            return new MapReduceEntriesTask<K,V,U>
5302 <                (map, null, -1, null, transformer, reducer);
5303 <        }
5304 <
5305 <        /**
5306 <         * Returns a task that when invoked, returns the result of
5307 <         * accumulating the given transformation of all entries using the
5308 <         * given reducer to combine values, and the given basis as an
5309 <         * identity value.
5310 <         *
5311 <         * @param map the map
5312 <         * @param transformer a function returning the transformation
5313 <         * for an element
5314 <         * @param basis the identity (initial default value) for the reduction
5315 <         * @param reducer a commutative associative combining function
5316 <         * @return the task
5317 <         */
5318 <        public static <K,V> ForkJoinTask<Double> reduceEntriesToDouble
5319 <            (ConcurrentHashMapV8<K,V> map,
5320 <             ObjectToDouble<Map.Entry<K,V>> transformer,
5321 <             double basis,
5322 <             DoubleByDoubleToDouble reducer) {
5323 <            if (transformer == null || reducer == null)
5324 <                throw new NullPointerException();
5325 <            return new MapReduceEntriesToDoubleTask<K,V>
5326 <                (map, null, -1, null, transformer, basis, reducer);
5327 <        }
5328 <
5329 <        /**
5330 <         * Returns a task that when invoked, returns the result of
5331 <         * accumulating the given transformation of all entries using the
5332 <         * given reducer to combine values, and the given basis as an
5333 <         * identity value.
5334 <         *
5335 <         * @param map the map
5336 <         * @param transformer a function returning the transformation
5337 <         * for an element
5338 <         * @param basis the identity (initial default value) for the reduction
5339 <         * @param reducer a commutative associative combining function
5340 <         * @return the task
5341 <         */
5342 <        public static <K,V> ForkJoinTask<Long> reduceEntriesToLong
5343 <            (ConcurrentHashMapV8<K,V> map,
5344 <             ObjectToLong<Map.Entry<K,V>> transformer,
5345 <             long basis,
5346 <             LongByLongToLong reducer) {
5347 <            if (transformer == null || reducer == null)
5348 <                throw new NullPointerException();
5349 <            return new MapReduceEntriesToLongTask<K,V>
5350 <                (map, null, -1, null, transformer, basis, reducer);
4685 >    abstract static class BulkTask<K,V,R> extends CountedCompleter<R> {
4686 >        Node<K,V>[] tab;        // same as Traverser
4687 >        Node<K,V> next;
4688 >        int index;
4689 >        int baseIndex;
4690 >        int baseLimit;
4691 >        final int baseSize;
4692 >        int batch;              // split control
4693 >
4694 >        BulkTask(BulkTask<K,V,?> par, int b, int i, int f, Node<K,V>[] t) {
4695 >            super(par);
4696 >            this.batch = b;
4697 >            this.index = this.baseIndex = i;
4698 >            if ((this.tab = t) == null)
4699 >                this.baseSize = this.baseLimit = 0;
4700 >            else if (par == null)
4701 >                this.baseSize = this.baseLimit = t.length;
4702 >            else {
4703 >                this.baseLimit = f;
4704 >                this.baseSize = par.baseSize;
4705 >            }
4706          }
4707  
4708          /**
4709 <         * Returns a task that when invoked, returns the result of
5355 <         * accumulating the given transformation of all entries using the
5356 <         * given reducer to combine values, and the given basis as an
5357 <         * identity value.
5358 <         *
5359 <         * @param map the map
5360 <         * @param transformer a function returning the transformation
5361 <         * for an element
5362 <         * @param basis the identity (initial default value) for the reduction
5363 <         * @param reducer a commutative associative combining function
5364 <         * @return the task
4709 >         * Same as Traverser version
4710           */
4711 <        public static <K,V> ForkJoinTask<Integer> reduceEntriesToInt
4712 <            (ConcurrentHashMapV8<K,V> map,
4713 <             ObjectToInt<Map.Entry<K,V>> transformer,
4714 <             int basis,
4715 <             IntByIntToInt reducer) {
4716 <            if (transformer == null || reducer == null)
4717 <                throw new NullPointerException();
4718 <            return new MapReduceEntriesToIntTask<K,V>
4719 <                (map, null, -1, null, transformer, basis, reducer);
4711 >        final Node<K,V> advance() {
4712 >            Node<K,V> e;
4713 >            if ((e = next) != null)
4714 >                e = e.next;
4715 >            for (;;) {
4716 >                Node<K,V>[] t; int i, n; K ek;  // must use locals in checks
4717 >                if (e != null)
4718 >                    return next = e;
4719 >                if (baseIndex >= baseLimit || (t = tab) == null ||
4720 >                    (n = t.length) <= (i = index) || i < 0)
4721 >                    return next = null;
4722 >                if ((e = tabAt(t, index)) != null && e.hash < 0) {
4723 >                    if (e instanceof ForwardingNode) {
4724 >                        tab = ((ForwardingNode<K,V>)e).nextTable;
4725 >                        e = null;
4726 >                        continue;
4727 >                    }
4728 >                    else if (e instanceof TreeBin)
4729 >                        e = ((TreeBin<K,V>)e).first;
4730 >                    else
4731 >                        e = null;
4732 >                }
4733 >                if ((index += baseSize) >= n)
4734 >                    index = ++baseIndex;    // visit upper slots if present
4735 >            }
4736          }
4737      }
4738  
5378    // -------------------------------------------------------
5379
4739      /*
4740       * Task classes. Coded in a regular but ugly format/style to
4741       * simplify checks that each variant differs in the right way from
4742 <     * others.
4743 <     */
4744 <
4745 <    @SuppressWarnings("serial") static final class ForEachKeyTask<K,V>
4746 <        extends Traverser<K,V,Void> {
4747 <        final Action<K> action;
4742 >     * others. The null screenings exist because compilers cannot tell
4743 >     * that we've already null-checked task arguments, so we force
4744 >     * simplest hoisted bypass to help avoid convoluted traps.
4745 >     */
4746 >    @SuppressWarnings("serial")
4747 >    static final class ForEachKeyTask<K,V>
4748 >        extends BulkTask<K,V,Void> {
4749 >        final Action<? super K> action;
4750          ForEachKeyTask
4751 <            (ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b,
4752 <             Action<K> action) {
4753 <            super(m, p, b);
4751 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
4752 >             Action<? super K> action) {
4753 >            super(p, b, i, f, t);
4754              this.action = action;
4755          }
4756 <        @SuppressWarnings("unchecked") public final void compute() {
4757 <            final Action<K> action;
4758 <            if ((action = this.action) == null)
4759 <                throw new NullPointerException();
4760 <            for (int b; (b = preSplit()) > 0;)
4761 <                new ForEachKeyTask<K,V>(map, this, b, action).fork();
4762 <            while (advance() != null)
4763 <                action.apply((K)nextKey);
4764 <            propagateCompletion();
4756 >        public final void compute() {
4757 >            final Action<? super K> action;
4758 >            if ((action = this.action) != null) {
4759 >                for (int i = baseIndex, f, h; batch > 0 &&
4760 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
4761 >                    addToPendingCount(1);
4762 >                    new ForEachKeyTask<K,V>
4763 >                        (this, batch >>>= 1, baseLimit = h, f, tab,
4764 >                         action).fork();
4765 >                }
4766 >                for (Node<K,V> p; (p = advance()) != null;)
4767 >                    action.apply(p.key);
4768 >                propagateCompletion();
4769 >            }
4770          }
4771      }
4772  
4773 <    @SuppressWarnings("serial") static final class ForEachValueTask<K,V>
4774 <        extends Traverser<K,V,Void> {
4775 <        final Action<V> action;
4773 >    @SuppressWarnings("serial")
4774 >    static final class ForEachValueTask<K,V>
4775 >        extends BulkTask<K,V,Void> {
4776 >        final Action<? super V> action;
4777          ForEachValueTask
4778 <            (ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b,
4779 <             Action<V> action) {
4780 <            super(m, p, b);
4778 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
4779 >             Action<? super V> action) {
4780 >            super(p, b, i, f, t);
4781              this.action = action;
4782          }
4783 <        @SuppressWarnings("unchecked") public final void compute() {
4784 <            final Action<V> action;
4785 <            if ((action = this.action) == null)
4786 <                throw new NullPointerException();
4787 <            for (int b; (b = preSplit()) > 0;)
4788 <                new ForEachValueTask<K,V>(map, this, b, action).fork();
4789 <            Object v;
4790 <            while ((v = advance()) != null)
4791 <                action.apply((V)v);
4792 <            propagateCompletion();
4783 >        public final void compute() {
4784 >            final Action<? super V> action;
4785 >            if ((action = this.action) != null) {
4786 >                for (int i = baseIndex, f, h; batch > 0 &&
4787 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
4788 >                    addToPendingCount(1);
4789 >                    new ForEachValueTask<K,V>
4790 >                        (this, batch >>>= 1, baseLimit = h, f, tab,
4791 >                         action).fork();
4792 >                }
4793 >                for (Node<K,V> p; (p = advance()) != null;)
4794 >                    action.apply(p.val);
4795 >                propagateCompletion();
4796 >            }
4797          }
4798      }
4799  
4800 <    @SuppressWarnings("serial") static final class ForEachEntryTask<K,V>
4801 <        extends Traverser<K,V,Void> {
4802 <        final Action<Entry<K,V>> action;
4800 >    @SuppressWarnings("serial")
4801 >    static final class ForEachEntryTask<K,V>
4802 >        extends BulkTask<K,V,Void> {
4803 >        final Action<? super Entry<K,V>> action;
4804          ForEachEntryTask
4805 <            (ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b,
4806 <             Action<Entry<K,V>> action) {
4807 <            super(m, p, b);
4805 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
4806 >             Action<? super Entry<K,V>> action) {
4807 >            super(p, b, i, f, t);
4808              this.action = action;
4809          }
4810 <        @SuppressWarnings("unchecked") public final void compute() {
4811 <            final Action<Entry<K,V>> action;
4812 <            if ((action = this.action) == null)
4813 <                throw new NullPointerException();
4814 <            for (int b; (b = preSplit()) > 0;)
4815 <                new ForEachEntryTask<K,V>(map, this, b, action).fork();
4816 <            Object v;
4817 <            while ((v = advance()) != null)
4818 <                action.apply(entryFor((K)nextKey, (V)v));
4819 <            propagateCompletion();
4810 >        public final void compute() {
4811 >            final Action<? super Entry<K,V>> action;
4812 >            if ((action = this.action) != null) {
4813 >                for (int i = baseIndex, f, h; batch > 0 &&
4814 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
4815 >                    addToPendingCount(1);
4816 >                    new ForEachEntryTask<K,V>
4817 >                        (this, batch >>>= 1, baseLimit = h, f, tab,
4818 >                         action).fork();
4819 >                }
4820 >                for (Node<K,V> p; (p = advance()) != null; )
4821 >                    action.apply(p);
4822 >                propagateCompletion();
4823 >            }
4824          }
4825      }
4826  
4827 <    @SuppressWarnings("serial") static final class ForEachMappingTask<K,V>
4828 <        extends Traverser<K,V,Void> {
4829 <        final BiAction<K,V> action;
4827 >    @SuppressWarnings("serial")
4828 >    static final class ForEachMappingTask<K,V>
4829 >        extends BulkTask<K,V,Void> {
4830 >        final BiAction<? super K, ? super V> action;
4831          ForEachMappingTask
4832 <            (ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b,
4833 <             BiAction<K,V> action) {
4834 <            super(m, p, b);
4832 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
4833 >             BiAction<? super K,? super V> action) {
4834 >            super(p, b, i, f, t);
4835              this.action = action;
4836          }
4837 <        @SuppressWarnings("unchecked") public final void compute() {
4838 <            final BiAction<K,V> action;
4839 <            if ((action = this.action) == null)
4840 <                throw new NullPointerException();
4841 <            for (int b; (b = preSplit()) > 0;)
4842 <                new ForEachMappingTask<K,V>(map, this, b, action).fork();
4843 <            Object v;
4844 <            while ((v = advance()) != null)
4845 <                action.apply((K)nextKey, (V)v);
4846 <            propagateCompletion();
4837 >        public final void compute() {
4838 >            final BiAction<? super K, ? super V> action;
4839 >            if ((action = this.action) != null) {
4840 >                for (int i = baseIndex, f, h; batch > 0 &&
4841 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
4842 >                    addToPendingCount(1);
4843 >                    new ForEachMappingTask<K,V>
4844 >                        (this, batch >>>= 1, baseLimit = h, f, tab,
4845 >                         action).fork();
4846 >                }
4847 >                for (Node<K,V> p; (p = advance()) != null; )
4848 >                    action.apply(p.key, p.val);
4849 >                propagateCompletion();
4850 >            }
4851          }
4852      }
4853  
4854 <    @SuppressWarnings("serial") static final class ForEachTransformedKeyTask<K,V,U>
4855 <        extends Traverser<K,V,Void> {
4854 >    @SuppressWarnings("serial")
4855 >    static final class ForEachTransformedKeyTask<K,V,U>
4856 >        extends BulkTask<K,V,Void> {
4857          final Fun<? super K, ? extends U> transformer;
4858 <        final Action<U> action;
4858 >        final Action<? super U> action;
4859          ForEachTransformedKeyTask
4860 <            (ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b,
4861 <             Fun<? super K, ? extends U> transformer, Action<U> action) {
4862 <            super(m, p, b);
4860 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
4861 >             Fun<? super K, ? extends U> transformer, Action<? super U> action) {
4862 >            super(p, b, i, f, t);
4863              this.transformer = transformer; this.action = action;
4864          }
4865 <        @SuppressWarnings("unchecked") public final void compute() {
4865 >        public final void compute() {
4866              final Fun<? super K, ? extends U> transformer;
4867 <            final Action<U> action;
4868 <            if ((transformer = this.transformer) == null ||
4869 <                (action = this.action) == null)
4870 <                throw new NullPointerException();
4871 <            for (int b; (b = preSplit()) > 0;)
4872 <                new ForEachTransformedKeyTask<K,V,U>
4873 <                     (map, this, b, transformer, action).fork();
4874 <            U u;
4875 <            while (advance() != null) {
4876 <                if ((u = transformer.apply((K)nextKey)) != null)
4877 <                    action.apply(u);
4867 >            final Action<? super U> action;
4868 >            if ((transformer = this.transformer) != null &&
4869 >                (action = this.action) != null) {
4870 >                for (int i = baseIndex, f, h; batch > 0 &&
4871 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
4872 >                    addToPendingCount(1);
4873 >                    new ForEachTransformedKeyTask<K,V,U>
4874 >                        (this, batch >>>= 1, baseLimit = h, f, tab,
4875 >                         transformer, action).fork();
4876 >                }
4877 >                for (Node<K,V> p; (p = advance()) != null; ) {
4878 >                    U u;
4879 >                    if ((u = transformer.apply(p.key)) != null)
4880 >                        action.apply(u);
4881 >                }
4882 >                propagateCompletion();
4883              }
5497            propagateCompletion();
4884          }
4885      }
4886  
4887 <    @SuppressWarnings("serial") static final class ForEachTransformedValueTask<K,V,U>
4888 <        extends Traverser<K,V,Void> {
4887 >    @SuppressWarnings("serial")
4888 >    static final class ForEachTransformedValueTask<K,V,U>
4889 >        extends BulkTask<K,V,Void> {
4890          final Fun<? super V, ? extends U> transformer;
4891 <        final Action<U> action;
4891 >        final Action<? super U> action;
4892          ForEachTransformedValueTask
4893 <            (ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b,
4894 <             Fun<? super V, ? extends U> transformer, Action<U> action) {
4895 <            super(m, p, b);
4893 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
4894 >             Fun<? super V, ? extends U> transformer, Action<? super U> action) {
4895 >            super(p, b, i, f, t);
4896              this.transformer = transformer; this.action = action;
4897          }
4898 <        @SuppressWarnings("unchecked") public final void compute() {
4898 >        public final void compute() {
4899              final Fun<? super V, ? extends U> transformer;
4900 <            final Action<U> action;
4901 <            if ((transformer = this.transformer) == null ||
4902 <                (action = this.action) == null)
4903 <                throw new NullPointerException();
4904 <            for (int b; (b = preSplit()) > 0;)
4905 <                new ForEachTransformedValueTask<K,V,U>
4906 <                    (map, this, b, transformer, action).fork();
4907 <            Object v; U u;
4908 <            while ((v = advance()) != null) {
4909 <                if ((u = transformer.apply((V)v)) != null)
4910 <                    action.apply(u);
4900 >            final Action<? super U> action;
4901 >            if ((transformer = this.transformer) != null &&
4902 >                (action = this.action) != null) {
4903 >                for (int i = baseIndex, f, h; batch > 0 &&
4904 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
4905 >                    addToPendingCount(1);
4906 >                    new ForEachTransformedValueTask<K,V,U>
4907 >                        (this, batch >>>= 1, baseLimit = h, f, tab,
4908 >                         transformer, action).fork();
4909 >                }
4910 >                for (Node<K,V> p; (p = advance()) != null; ) {
4911 >                    U u;
4912 >                    if ((u = transformer.apply(p.val)) != null)
4913 >                        action.apply(u);
4914 >                }
4915 >                propagateCompletion();
4916              }
5525            propagateCompletion();
4917          }
4918      }
4919  
4920 <    @SuppressWarnings("serial") static final class ForEachTransformedEntryTask<K,V,U>
4921 <        extends Traverser<K,V,Void> {
4920 >    @SuppressWarnings("serial")
4921 >    static final class ForEachTransformedEntryTask<K,V,U>
4922 >        extends BulkTask<K,V,Void> {
4923          final Fun<Map.Entry<K,V>, ? extends U> transformer;
4924 <        final Action<U> action;
4924 >        final Action<? super U> action;
4925          ForEachTransformedEntryTask
4926 <            (ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b,
4927 <             Fun<Map.Entry<K,V>, ? extends U> transformer, Action<U> action) {
4928 <            super(m, p, b);
4926 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
4927 >             Fun<Map.Entry<K,V>, ? extends U> transformer, Action<? super U> action) {
4928 >            super(p, b, i, f, t);
4929              this.transformer = transformer; this.action = action;
4930          }
4931 <        @SuppressWarnings("unchecked") public final void compute() {
4931 >        public final void compute() {
4932              final Fun<Map.Entry<K,V>, ? extends U> transformer;
4933 <            final Action<U> action;
4934 <            if ((transformer = this.transformer) == null ||
4935 <                (action = this.action) == null)
4936 <                throw new NullPointerException();
4937 <            for (int b; (b = preSplit()) > 0;)
4938 <                new ForEachTransformedEntryTask<K,V,U>
4939 <                    (map, this, b, transformer, action).fork();
4940 <            Object v; U u;
4941 <            while ((v = advance()) != null) {
4942 <                if ((u = transformer.apply(entryFor((K)nextKey, (V)v))) != null)
4943 <                    action.apply(u);
4933 >            final Action<? super U> action;
4934 >            if ((transformer = this.transformer) != null &&
4935 >                (action = this.action) != null) {
4936 >                for (int i = baseIndex, f, h; batch > 0 &&
4937 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
4938 >                    addToPendingCount(1);
4939 >                    new ForEachTransformedEntryTask<K,V,U>
4940 >                        (this, batch >>>= 1, baseLimit = h, f, tab,
4941 >                         transformer, action).fork();
4942 >                }
4943 >                for (Node<K,V> p; (p = advance()) != null; ) {
4944 >                    U u;
4945 >                    if ((u = transformer.apply(p)) != null)
4946 >                        action.apply(u);
4947 >                }
4948 >                propagateCompletion();
4949              }
5553            propagateCompletion();
4950          }
4951      }
4952  
4953 <    @SuppressWarnings("serial") static final class ForEachTransformedMappingTask<K,V,U>
4954 <        extends Traverser<K,V,Void> {
4953 >    @SuppressWarnings("serial")
4954 >    static final class ForEachTransformedMappingTask<K,V,U>
4955 >        extends BulkTask<K,V,Void> {
4956          final BiFun<? super K, ? super V, ? extends U> transformer;
4957 <        final Action<U> action;
4957 >        final Action<? super U> action;
4958          ForEachTransformedMappingTask
4959 <            (ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b,
4959 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
4960               BiFun<? super K, ? super V, ? extends U> transformer,
4961 <             Action<U> action) {
4962 <            super(m, p, b);
4961 >             Action<? super U> action) {
4962 >            super(p, b, i, f, t);
4963              this.transformer = transformer; this.action = action;
4964          }
4965 <        @SuppressWarnings("unchecked") public final void compute() {
4965 >        public final void compute() {
4966              final BiFun<? super K, ? super V, ? extends U> transformer;
4967 <            final Action<U> action;
4968 <            if ((transformer = this.transformer) == null ||
4969 <                (action = this.action) == null)
4970 <                throw new NullPointerException();
4971 <            for (int b; (b = preSplit()) > 0;)
4972 <                new ForEachTransformedMappingTask<K,V,U>
4973 <                    (map, this, b, transformer, action).fork();
4974 <            Object v; U u;
4975 <            while ((v = advance()) != null) {
4976 <                if ((u = transformer.apply((K)nextKey, (V)v)) != null)
4977 <                    action.apply(u);
4967 >            final Action<? super U> action;
4968 >            if ((transformer = this.transformer) != null &&
4969 >                (action = this.action) != null) {
4970 >                for (int i = baseIndex, f, h; batch > 0 &&
4971 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
4972 >                    addToPendingCount(1);
4973 >                    new ForEachTransformedMappingTask<K,V,U>
4974 >                        (this, batch >>>= 1, baseLimit = h, f, tab,
4975 >                         transformer, action).fork();
4976 >                }
4977 >                for (Node<K,V> p; (p = advance()) != null; ) {
4978 >                    U u;
4979 >                    if ((u = transformer.apply(p.key, p.val)) != null)
4980 >                        action.apply(u);
4981 >                }
4982 >                propagateCompletion();
4983              }
5582            propagateCompletion();
4984          }
4985      }
4986  
4987 <    @SuppressWarnings("serial") static final class SearchKeysTask<K,V,U>
4988 <        extends Traverser<K,V,U> {
4987 >    @SuppressWarnings("serial")
4988 >    static final class SearchKeysTask<K,V,U>
4989 >        extends BulkTask<K,V,U> {
4990          final Fun<? super K, ? extends U> searchFunction;
4991          final AtomicReference<U> result;
4992          SearchKeysTask
4993 <            (ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b,
4993 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
4994               Fun<? super K, ? extends U> searchFunction,
4995               AtomicReference<U> result) {
4996 <            super(m, p, b);
4996 >            super(p, b, i, f, t);
4997              this.searchFunction = searchFunction; this.result = result;
4998          }
4999          public final U getRawResult() { return result.get(); }
5000 <        @SuppressWarnings("unchecked") public final void compute() {
5000 >        public final void compute() {
5001              final Fun<? super K, ? extends U> searchFunction;
5002              final AtomicReference<U> result;
5003 <            if ((searchFunction = this.searchFunction) == null ||
5004 <                (result = this.result) == null)
5005 <                throw new NullPointerException();
5006 <            for (int b;;) {
5007 <                if (result.get() != null)
5008 <                    return;
5009 <                if ((b = preSplit()) <= 0)
5010 <                    break;
5011 <                new SearchKeysTask<K,V,U>
5012 <                    (map, this, b, searchFunction, result).fork();
5013 <            }
5014 <            while (result.get() == null) {
5015 <                U u;
5016 <                if (advance() == null) {
5017 <                    propagateCompletion();
5018 <                    break;
5019 <                }
5020 <                if ((u = searchFunction.apply((K)nextKey)) != null) {
5021 <                    if (result.compareAndSet(null, u))
5022 <                        quietlyCompleteRoot();
5023 <                    break;
5003 >            if ((searchFunction = this.searchFunction) != null &&
5004 >                (result = this.result) != null) {
5005 >                for (int i = baseIndex, f, h; batch > 0 &&
5006 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5007 >                    if (result.get() != null)
5008 >                        return;
5009 >                    addToPendingCount(1);
5010 >                    new SearchKeysTask<K,V,U>
5011 >                        (this, batch >>>= 1, baseLimit = h, f, tab,
5012 >                         searchFunction, result).fork();
5013 >                }
5014 >                while (result.get() == null) {
5015 >                    U u;
5016 >                    Node<K,V> p;
5017 >                    if ((p = advance()) == null) {
5018 >                        propagateCompletion();
5019 >                        break;
5020 >                    }
5021 >                    if ((u = searchFunction.apply(p.key)) != null) {
5022 >                        if (result.compareAndSet(null, u))
5023 >                            quietlyCompleteRoot();
5024 >                        break;
5025 >                    }
5026                  }
5027              }
5028          }
5029      }
5030  
5031 <    @SuppressWarnings("serial") static final class SearchValuesTask<K,V,U>
5032 <        extends Traverser<K,V,U> {
5031 >    @SuppressWarnings("serial")
5032 >    static final class SearchValuesTask<K,V,U>
5033 >        extends BulkTask<K,V,U> {
5034          final Fun<? super V, ? extends U> searchFunction;
5035          final AtomicReference<U> result;
5036          SearchValuesTask
5037 <            (ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b,
5037 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5038               Fun<? super V, ? extends U> searchFunction,
5039               AtomicReference<U> result) {
5040 <            super(m, p, b);
5040 >            super(p, b, i, f, t);
5041              this.searchFunction = searchFunction; this.result = result;
5042          }
5043          public final U getRawResult() { return result.get(); }
5044 <        @SuppressWarnings("unchecked") public final void compute() {
5044 >        public final void compute() {
5045              final Fun<? super V, ? extends U> searchFunction;
5046              final AtomicReference<U> result;
5047 <            if ((searchFunction = this.searchFunction) == null ||
5048 <                (result = this.result) == null)
5049 <                throw new NullPointerException();
5050 <            for (int b;;) {
5051 <                if (result.get() != null)
5052 <                    return;
5053 <                if ((b = preSplit()) <= 0)
5054 <                    break;
5055 <                new SearchValuesTask<K,V,U>
5056 <                    (map, this, b, searchFunction, result).fork();
5057 <            }
5058 <            while (result.get() == null) {
5059 <                Object v; U u;
5060 <                if ((v = advance()) == null) {
5061 <                    propagateCompletion();
5062 <                    break;
5063 <                }
5064 <                if ((u = searchFunction.apply((V)v)) != null) {
5065 <                    if (result.compareAndSet(null, u))
5066 <                        quietlyCompleteRoot();
5067 <                    break;
5047 >            if ((searchFunction = this.searchFunction) != null &&
5048 >                (result = this.result) != null) {
5049 >                for (int i = baseIndex, f, h; batch > 0 &&
5050 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5051 >                    if (result.get() != null)
5052 >                        return;
5053 >                    addToPendingCount(1);
5054 >                    new SearchValuesTask<K,V,U>
5055 >                        (this, batch >>>= 1, baseLimit = h, f, tab,
5056 >                         searchFunction, result).fork();
5057 >                }
5058 >                while (result.get() == null) {
5059 >                    U u;
5060 >                    Node<K,V> p;
5061 >                    if ((p = advance()) == null) {
5062 >                        propagateCompletion();
5063 >                        break;
5064 >                    }
5065 >                    if ((u = searchFunction.apply(p.val)) != null) {
5066 >                        if (result.compareAndSet(null, u))
5067 >                            quietlyCompleteRoot();
5068 >                        break;
5069 >                    }
5070                  }
5071              }
5072          }
5073      }
5074  
5075 <    @SuppressWarnings("serial") static final class SearchEntriesTask<K,V,U>
5076 <        extends Traverser<K,V,U> {
5075 >    @SuppressWarnings("serial")
5076 >    static final class SearchEntriesTask<K,V,U>
5077 >        extends BulkTask<K,V,U> {
5078          final Fun<Entry<K,V>, ? extends U> searchFunction;
5079          final AtomicReference<U> result;
5080          SearchEntriesTask
5081 <            (ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b,
5081 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5082               Fun<Entry<K,V>, ? extends U> searchFunction,
5083               AtomicReference<U> result) {
5084 <            super(m, p, b);
5084 >            super(p, b, i, f, t);
5085              this.searchFunction = searchFunction; this.result = result;
5086          }
5087          public final U getRawResult() { return result.get(); }
5088 <        @SuppressWarnings("unchecked") public final void compute() {
5088 >        public final void compute() {
5089              final Fun<Entry<K,V>, ? extends U> searchFunction;
5090              final AtomicReference<U> result;
5091 <            if ((searchFunction = this.searchFunction) == null ||
5092 <                (result = this.result) == null)
5093 <                throw new NullPointerException();
5094 <            for (int b;;) {
5095 <                if (result.get() != null)
5096 <                    return;
5097 <                if ((b = preSplit()) <= 0)
5098 <                    break;
5099 <                new SearchEntriesTask<K,V,U>
5100 <                    (map, this, b, searchFunction, result).fork();
5101 <            }
5102 <            while (result.get() == null) {
5103 <                Object v; U u;
5104 <                if ((v = advance()) == null) {
5105 <                    propagateCompletion();
5106 <                    break;
5107 <                }
5108 <                if ((u = searchFunction.apply(entryFor((K)nextKey, (V)v))) != null) {
5109 <                    if (result.compareAndSet(null, u))
5110 <                        quietlyCompleteRoot();
5111 <                    return;
5091 >            if ((searchFunction = this.searchFunction) != null &&
5092 >                (result = this.result) != null) {
5093 >                for (int i = baseIndex, f, h; batch > 0 &&
5094 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5095 >                    if (result.get() != null)
5096 >                        return;
5097 >                    addToPendingCount(1);
5098 >                    new SearchEntriesTask<K,V,U>
5099 >                        (this, batch >>>= 1, baseLimit = h, f, tab,
5100 >                         searchFunction, result).fork();
5101 >                }
5102 >                while (result.get() == null) {
5103 >                    U u;
5104 >                    Node<K,V> p;
5105 >                    if ((p = advance()) == null) {
5106 >                        propagateCompletion();
5107 >                        break;
5108 >                    }
5109 >                    if ((u = searchFunction.apply(p)) != null) {
5110 >                        if (result.compareAndSet(null, u))
5111 >                            quietlyCompleteRoot();
5112 >                        return;
5113 >                    }
5114                  }
5115              }
5116          }
5117      }
5118  
5119 <    @SuppressWarnings("serial") static final class SearchMappingsTask<K,V,U>
5120 <        extends Traverser<K,V,U> {
5119 >    @SuppressWarnings("serial")
5120 >    static final class SearchMappingsTask<K,V,U>
5121 >        extends BulkTask<K,V,U> {
5122          final BiFun<? super K, ? super V, ? extends U> searchFunction;
5123          final AtomicReference<U> result;
5124          SearchMappingsTask
5125 <            (ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b,
5125 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5126               BiFun<? super K, ? super V, ? extends U> searchFunction,
5127               AtomicReference<U> result) {
5128 <            super(m, p, b);
5128 >            super(p, b, i, f, t);
5129              this.searchFunction = searchFunction; this.result = result;
5130          }
5131          public final U getRawResult() { return result.get(); }
5132 <        @SuppressWarnings("unchecked") public final void compute() {
5132 >        public final void compute() {
5133              final BiFun<? super K, ? super V, ? extends U> searchFunction;
5134              final AtomicReference<U> result;
5135 <            if ((searchFunction = this.searchFunction) == null ||
5136 <                (result = this.result) == null)
5137 <                throw new NullPointerException();
5138 <            for (int b;;) {
5139 <                if (result.get() != null)
5140 <                    return;
5141 <                if ((b = preSplit()) <= 0)
5142 <                    break;
5143 <                new SearchMappingsTask<K,V,U>
5144 <                    (map, this, b, searchFunction, result).fork();
5145 <            }
5146 <            while (result.get() == null) {
5147 <                Object v; U u;
5148 <                if ((v = advance()) == null) {
5149 <                    propagateCompletion();
5150 <                    break;
5151 <                }
5152 <                if ((u = searchFunction.apply((K)nextKey, (V)v)) != null) {
5153 <                    if (result.compareAndSet(null, u))
5154 <                        quietlyCompleteRoot();
5155 <                    break;
5135 >            if ((searchFunction = this.searchFunction) != null &&
5136 >                (result = this.result) != null) {
5137 >                for (int i = baseIndex, f, h; batch > 0 &&
5138 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5139 >                    if (result.get() != null)
5140 >                        return;
5141 >                    addToPendingCount(1);
5142 >                    new SearchMappingsTask<K,V,U>
5143 >                        (this, batch >>>= 1, baseLimit = h, f, tab,
5144 >                         searchFunction, result).fork();
5145 >                }
5146 >                while (result.get() == null) {
5147 >                    U u;
5148 >                    Node<K,V> p;
5149 >                    if ((p = advance()) == null) {
5150 >                        propagateCompletion();
5151 >                        break;
5152 >                    }
5153 >                    if ((u = searchFunction.apply(p.key, p.val)) != null) {
5154 >                        if (result.compareAndSet(null, u))
5155 >                            quietlyCompleteRoot();
5156 >                        break;
5157 >                    }
5158                  }
5159              }
5160          }
5161      }
5162  
5163 <    @SuppressWarnings("serial") static final class ReduceKeysTask<K,V>
5164 <        extends Traverser<K,V,K> {
5163 >    @SuppressWarnings("serial")
5164 >    static final class ReduceKeysTask<K,V>
5165 >        extends BulkTask<K,V,K> {
5166          final BiFun<? super K, ? super K, ? extends K> reducer;
5167          K result;
5168          ReduceKeysTask<K,V> rights, nextRight;
5169          ReduceKeysTask
5170 <            (ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b,
5170 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5171               ReduceKeysTask<K,V> nextRight,
5172               BiFun<? super K, ? super K, ? extends K> reducer) {
5173 <            super(m, p, b); this.nextRight = nextRight;
5173 >            super(p, b, i, f, t); this.nextRight = nextRight;
5174              this.reducer = reducer;
5175          }
5176          public final K getRawResult() { return result; }
5177 <        @SuppressWarnings("unchecked") public final void compute() {
5178 <            final BiFun<? super K, ? super K, ? extends K> reducer =
5179 <                this.reducer;
5180 <            if (reducer == null)
5181 <                throw new NullPointerException();
5182 <            for (int b; (b = preSplit()) > 0;)
5183 <                (rights = new ReduceKeysTask<K,V>
5184 <                 (map, this, b, rights, reducer)).fork();
5185 <            K r = null;
5186 <            while (advance() != null) {
5187 <                K u = (K)nextKey;
5188 <                r = (r == null) ? u : reducer.apply(r, u);
5189 <            }
5190 <            result = r;
5191 <            CountedCompleter<?> c;
5192 <            for (c = firstComplete(); c != null; c = c.nextComplete()) {
5193 <                ReduceKeysTask<K,V>
5194 <                    t = (ReduceKeysTask<K,V>)c,
5195 <                    s = t.rights;
5196 <                while (s != null) {
5197 <                    K tr, sr;
5198 <                    if ((sr = s.result) != null)
5199 <                        t.result = (((tr = t.result) == null) ? sr :
5200 <                                    reducer.apply(tr, sr));
5201 <                    s = t.rights = s.nextRight;
5177 >        public final void compute() {
5178 >            final BiFun<? super K, ? super K, ? extends K> reducer;
5179 >            if ((reducer = this.reducer) != null) {
5180 >                for (int i = baseIndex, f, h; batch > 0 &&
5181 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5182 >                    addToPendingCount(1);
5183 >                    (rights = new ReduceKeysTask<K,V>
5184 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
5185 >                      rights, reducer)).fork();
5186 >                }
5187 >                K r = null;
5188 >                for (Node<K,V> p; (p = advance()) != null; ) {
5189 >                    K u = p.key;
5190 >                    r = (r == null) ? u : u == null ? r : reducer.apply(r, u);
5191 >                }
5192 >                result = r;
5193 >                CountedCompleter<?> c;
5194 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5195 >                    @SuppressWarnings("unchecked") ReduceKeysTask<K,V>
5196 >                        t = (ReduceKeysTask<K,V>)c,
5197 >                        s = t.rights;
5198 >                    while (s != null) {
5199 >                        K tr, sr;
5200 >                        if ((sr = s.result) != null)
5201 >                            t.result = (((tr = t.result) == null) ? sr :
5202 >                                        reducer.apply(tr, sr));
5203 >                        s = t.rights = s.nextRight;
5204 >                    }
5205                  }
5206              }
5207          }
5208      }
5209  
5210 <    @SuppressWarnings("serial") static final class ReduceValuesTask<K,V>
5211 <        extends Traverser<K,V,V> {
5210 >    @SuppressWarnings("serial")
5211 >    static final class ReduceValuesTask<K,V>
5212 >        extends BulkTask<K,V,V> {
5213          final BiFun<? super V, ? super V, ? extends V> reducer;
5214          V result;
5215          ReduceValuesTask<K,V> rights, nextRight;
5216          ReduceValuesTask
5217 <            (ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b,
5217 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5218               ReduceValuesTask<K,V> nextRight,
5219               BiFun<? super V, ? super V, ? extends V> reducer) {
5220 <            super(m, p, b); this.nextRight = nextRight;
5220 >            super(p, b, i, f, t); this.nextRight = nextRight;
5221              this.reducer = reducer;
5222          }
5223          public final V getRawResult() { return result; }
5224 <        @SuppressWarnings("unchecked") public final void compute() {
5225 <            final BiFun<? super V, ? super V, ? extends V> reducer =
5226 <                this.reducer;
5227 <            if (reducer == null)
5228 <                throw new NullPointerException();
5229 <            for (int b; (b = preSplit()) > 0;)
5230 <                (rights = new ReduceValuesTask<K,V>
5231 <                 (map, this, b, rights, reducer)).fork();
5232 <            V r = null;
5233 <            Object v;
5234 <            while ((v = advance()) != null) {
5235 <                V u = (V)v;
5236 <                r = (r == null) ? u : reducer.apply(r, u);
5237 <            }
5238 <            result = r;
5239 <            CountedCompleter<?> c;
5240 <            for (c = firstComplete(); c != null; c = c.nextComplete()) {
5241 <                ReduceValuesTask<K,V>
5242 <                    t = (ReduceValuesTask<K,V>)c,
5243 <                    s = t.rights;
5244 <                while (s != null) {
5245 <                    V tr, sr;
5246 <                    if ((sr = s.result) != null)
5247 <                        t.result = (((tr = t.result) == null) ? sr :
5248 <                                    reducer.apply(tr, sr));
5249 <                    s = t.rights = s.nextRight;
5224 >        public final void compute() {
5225 >            final BiFun<? super V, ? super V, ? extends V> reducer;
5226 >            if ((reducer = this.reducer) != null) {
5227 >                for (int i = baseIndex, f, h; batch > 0 &&
5228 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5229 >                    addToPendingCount(1);
5230 >                    (rights = new ReduceValuesTask<K,V>
5231 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
5232 >                      rights, reducer)).fork();
5233 >                }
5234 >                V r = null;
5235 >                for (Node<K,V> p; (p = advance()) != null; ) {
5236 >                    V v = p.val;
5237 >                    r = (r == null) ? v : reducer.apply(r, v);
5238 >                }
5239 >                result = r;
5240 >                CountedCompleter<?> c;
5241 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5242 >                    @SuppressWarnings("unchecked") ReduceValuesTask<K,V>
5243 >                        t = (ReduceValuesTask<K,V>)c,
5244 >                        s = t.rights;
5245 >                    while (s != null) {
5246 >                        V tr, sr;
5247 >                        if ((sr = s.result) != null)
5248 >                            t.result = (((tr = t.result) == null) ? sr :
5249 >                                        reducer.apply(tr, sr));
5250 >                        s = t.rights = s.nextRight;
5251 >                    }
5252                  }
5253              }
5254          }
5255      }
5256  
5257 <    @SuppressWarnings("serial") static final class ReduceEntriesTask<K,V>
5258 <        extends Traverser<K,V,Map.Entry<K,V>> {
5257 >    @SuppressWarnings("serial")
5258 >    static final class ReduceEntriesTask<K,V>
5259 >        extends BulkTask<K,V,Map.Entry<K,V>> {
5260          final BiFun<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer;
5261          Map.Entry<K,V> result;
5262          ReduceEntriesTask<K,V> rights, nextRight;
5263          ReduceEntriesTask
5264 <            (ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b,
5264 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5265               ReduceEntriesTask<K,V> nextRight,
5266               BiFun<Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer) {
5267 <            super(m, p, b); this.nextRight = nextRight;
5267 >            super(p, b, i, f, t); this.nextRight = nextRight;
5268              this.reducer = reducer;
5269          }
5270          public final Map.Entry<K,V> getRawResult() { return result; }
5271 <        @SuppressWarnings("unchecked") public final void compute() {
5272 <            final BiFun<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer =
5273 <                this.reducer;
5274 <            if (reducer == null)
5275 <                throw new NullPointerException();
5276 <            for (int b; (b = preSplit()) > 0;)
5277 <                (rights = new ReduceEntriesTask<K,V>
5278 <                 (map, this, b, rights, reducer)).fork();
5279 <            Map.Entry<K,V> r = null;
5280 <            Object v;
5281 <            while ((v = advance()) != null) {
5282 <                Map.Entry<K,V> u = entryFor((K)nextKey, (V)v);
5283 <                r = (r == null) ? u : reducer.apply(r, u);
5284 <            }
5285 <            result = r;
5286 <            CountedCompleter<?> c;
5287 <            for (c = firstComplete(); c != null; c = c.nextComplete()) {
5288 <                ReduceEntriesTask<K,V>
5289 <                    t = (ReduceEntriesTask<K,V>)c,
5290 <                    s = t.rights;
5291 <                while (s != null) {
5292 <                    Map.Entry<K,V> tr, sr;
5293 <                    if ((sr = s.result) != null)
5294 <                        t.result = (((tr = t.result) == null) ? sr :
5295 <                                    reducer.apply(tr, sr));
5296 <                    s = t.rights = s.nextRight;
5271 >        public final void compute() {
5272 >            final BiFun<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer;
5273 >            if ((reducer = this.reducer) != null) {
5274 >                for (int i = baseIndex, f, h; batch > 0 &&
5275 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5276 >                    addToPendingCount(1);
5277 >                    (rights = new ReduceEntriesTask<K,V>
5278 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
5279 >                      rights, reducer)).fork();
5280 >                }
5281 >                Map.Entry<K,V> r = null;
5282 >                for (Node<K,V> p; (p = advance()) != null; )
5283 >                    r = (r == null) ? p : reducer.apply(r, p);
5284 >                result = r;
5285 >                CountedCompleter<?> c;
5286 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5287 >                    @SuppressWarnings("unchecked") ReduceEntriesTask<K,V>
5288 >                        t = (ReduceEntriesTask<K,V>)c,
5289 >                        s = t.rights;
5290 >                    while (s != null) {
5291 >                        Map.Entry<K,V> tr, sr;
5292 >                        if ((sr = s.result) != null)
5293 >                            t.result = (((tr = t.result) == null) ? sr :
5294 >                                        reducer.apply(tr, sr));
5295 >                        s = t.rights = s.nextRight;
5296 >                    }
5297                  }
5298              }
5299          }
5300      }
5301  
5302 <    @SuppressWarnings("serial") static final class MapReduceKeysTask<K,V,U>
5303 <        extends Traverser<K,V,U> {
5302 >    @SuppressWarnings("serial")
5303 >    static final class MapReduceKeysTask<K,V,U>
5304 >        extends BulkTask<K,V,U> {
5305          final Fun<? super K, ? extends U> transformer;
5306          final BiFun<? super U, ? super U, ? extends U> reducer;
5307          U result;
5308          MapReduceKeysTask<K,V,U> rights, nextRight;
5309          MapReduceKeysTask
5310 <            (ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b,
5310 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5311               MapReduceKeysTask<K,V,U> nextRight,
5312               Fun<? super K, ? extends U> transformer,
5313               BiFun<? super U, ? super U, ? extends U> reducer) {
5314 <            super(m, p, b); this.nextRight = nextRight;
5314 >            super(p, b, i, f, t); this.nextRight = nextRight;
5315              this.transformer = transformer;
5316              this.reducer = reducer;
5317          }
5318          public final U getRawResult() { return result; }
5319 <        @SuppressWarnings("unchecked") public final void compute() {
5320 <            final Fun<? super K, ? extends U> transformer =
5321 <                this.transformer;
5322 <            final BiFun<? super U, ? super U, ? extends U> reducer =
5323 <                this.reducer;
5324 <            if (transformer == null || reducer == null)
5325 <                throw new NullPointerException();
5326 <            for (int b; (b = preSplit()) > 0;)
5327 <                (rights = new MapReduceKeysTask<K,V,U>
5328 <                 (map, this, b, rights, transformer, reducer)).fork();
5329 <            U r = null, u;
5330 <            while (advance() != null) {
5331 <                if ((u = transformer.apply((K)nextKey)) != null)
5332 <                    r = (r == null) ? u : reducer.apply(r, u);
5333 <            }
5334 <            result = r;
5335 <            CountedCompleter<?> c;
5336 <            for (c = firstComplete(); c != null; c = c.nextComplete()) {
5337 <                MapReduceKeysTask<K,V,U>
5338 <                    t = (MapReduceKeysTask<K,V,U>)c,
5339 <                    s = t.rights;
5340 <                while (s != null) {
5341 <                    U tr, sr;
5342 <                    if ((sr = s.result) != null)
5343 <                        t.result = (((tr = t.result) == null) ? sr :
5344 <                                    reducer.apply(tr, sr));
5345 <                    s = t.rights = s.nextRight;
5319 >        public final void compute() {
5320 >            final Fun<? super K, ? extends U> transformer;
5321 >            final BiFun<? super U, ? super U, ? extends U> reducer;
5322 >            if ((transformer = this.transformer) != null &&
5323 >                (reducer = this.reducer) != null) {
5324 >                for (int i = baseIndex, f, h; batch > 0 &&
5325 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5326 >                    addToPendingCount(1);
5327 >                    (rights = new MapReduceKeysTask<K,V,U>
5328 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
5329 >                      rights, transformer, reducer)).fork();
5330 >                }
5331 >                U r = null;
5332 >                for (Node<K,V> p; (p = advance()) != null; ) {
5333 >                    U u;
5334 >                    if ((u = transformer.apply(p.key)) != null)
5335 >                        r = (r == null) ? u : reducer.apply(r, u);
5336 >                }
5337 >                result = r;
5338 >                CountedCompleter<?> c;
5339 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5340 >                    @SuppressWarnings("unchecked") MapReduceKeysTask<K,V,U>
5341 >                        t = (MapReduceKeysTask<K,V,U>)c,
5342 >                        s = t.rights;
5343 >                    while (s != null) {
5344 >                        U tr, sr;
5345 >                        if ((sr = s.result) != null)
5346 >                            t.result = (((tr = t.result) == null) ? sr :
5347 >                                        reducer.apply(tr, sr));
5348 >                        s = t.rights = s.nextRight;
5349 >                    }
5350                  }
5351              }
5352          }
5353      }
5354  
5355 <    @SuppressWarnings("serial") static final class MapReduceValuesTask<K,V,U>
5356 <        extends Traverser<K,V,U> {
5355 >    @SuppressWarnings("serial")
5356 >    static final class MapReduceValuesTask<K,V,U>
5357 >        extends BulkTask<K,V,U> {
5358          final Fun<? super V, ? extends U> transformer;
5359          final BiFun<? super U, ? super U, ? extends U> reducer;
5360          U result;
5361          MapReduceValuesTask<K,V,U> rights, nextRight;
5362          MapReduceValuesTask
5363 <            (ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b,
5363 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5364               MapReduceValuesTask<K,V,U> nextRight,
5365               Fun<? super V, ? extends U> transformer,
5366               BiFun<? super U, ? super U, ? extends U> reducer) {
5367 <            super(m, p, b); this.nextRight = nextRight;
5367 >            super(p, b, i, f, t); this.nextRight = nextRight;
5368              this.transformer = transformer;
5369              this.reducer = reducer;
5370          }
5371          public final U getRawResult() { return result; }
5372 <        @SuppressWarnings("unchecked") public final void compute() {
5373 <            final Fun<? super V, ? extends U> transformer =
5374 <                this.transformer;
5375 <            final BiFun<? super U, ? super U, ? extends U> reducer =
5376 <                this.reducer;
5377 <            if (transformer == null || reducer == null)
5378 <                throw new NullPointerException();
5379 <            for (int b; (b = preSplit()) > 0;)
5380 <                (rights = new MapReduceValuesTask<K,V,U>
5381 <                 (map, this, b, rights, transformer, reducer)).fork();
5382 <            U r = null, u;
5383 <            Object v;
5384 <            while ((v = advance()) != null) {
5385 <                if ((u = transformer.apply((V)v)) != null)
5386 <                    r = (r == null) ? u : reducer.apply(r, u);
5387 <            }
5388 <            result = r;
5389 <            CountedCompleter<?> c;
5390 <            for (c = firstComplete(); c != null; c = c.nextComplete()) {
5391 <                MapReduceValuesTask<K,V,U>
5392 <                    t = (MapReduceValuesTask<K,V,U>)c,
5393 <                    s = t.rights;
5394 <                while (s != null) {
5395 <                    U tr, sr;
5396 <                    if ((sr = s.result) != null)
5397 <                        t.result = (((tr = t.result) == null) ? sr :
5398 <                                    reducer.apply(tr, sr));
5399 <                    s = t.rights = s.nextRight;
5372 >        public final void compute() {
5373 >            final Fun<? super V, ? extends U> transformer;
5374 >            final BiFun<? super U, ? super U, ? extends U> reducer;
5375 >            if ((transformer = this.transformer) != null &&
5376 >                (reducer = this.reducer) != null) {
5377 >                for (int i = baseIndex, f, h; batch > 0 &&
5378 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5379 >                    addToPendingCount(1);
5380 >                    (rights = new MapReduceValuesTask<K,V,U>
5381 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
5382 >                      rights, transformer, reducer)).fork();
5383 >                }
5384 >                U r = null;
5385 >                for (Node<K,V> p; (p = advance()) != null; ) {
5386 >                    U u;
5387 >                    if ((u = transformer.apply(p.val)) != null)
5388 >                        r = (r == null) ? u : reducer.apply(r, u);
5389 >                }
5390 >                result = r;
5391 >                CountedCompleter<?> c;
5392 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5393 >                    @SuppressWarnings("unchecked") MapReduceValuesTask<K,V,U>
5394 >                        t = (MapReduceValuesTask<K,V,U>)c,
5395 >                        s = t.rights;
5396 >                    while (s != null) {
5397 >                        U tr, sr;
5398 >                        if ((sr = s.result) != null)
5399 >                            t.result = (((tr = t.result) == null) ? sr :
5400 >                                        reducer.apply(tr, sr));
5401 >                        s = t.rights = s.nextRight;
5402 >                    }
5403                  }
5404              }
5405          }
5406      }
5407  
5408 <    @SuppressWarnings("serial") static final class MapReduceEntriesTask<K,V,U>
5409 <        extends Traverser<K,V,U> {
5408 >    @SuppressWarnings("serial")
5409 >    static final class MapReduceEntriesTask<K,V,U>
5410 >        extends BulkTask<K,V,U> {
5411          final Fun<Map.Entry<K,V>, ? extends U> transformer;
5412          final BiFun<? super U, ? super U, ? extends U> reducer;
5413          U result;
5414          MapReduceEntriesTask<K,V,U> rights, nextRight;
5415          MapReduceEntriesTask
5416 <            (ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b,
5416 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5417               MapReduceEntriesTask<K,V,U> nextRight,
5418               Fun<Map.Entry<K,V>, ? extends U> transformer,
5419               BiFun<? super U, ? super U, ? extends U> reducer) {
5420 <            super(m, p, b); this.nextRight = nextRight;
5420 >            super(p, b, i, f, t); this.nextRight = nextRight;
5421              this.transformer = transformer;
5422              this.reducer = reducer;
5423          }
5424          public final U getRawResult() { return result; }
5425 <        @SuppressWarnings("unchecked") public final void compute() {
5426 <            final Fun<Map.Entry<K,V>, ? extends U> transformer =
5427 <                this.transformer;
5428 <            final BiFun<? super U, ? super U, ? extends U> reducer =
5429 <                this.reducer;
5430 <            if (transformer == null || reducer == null)
5431 <                throw new NullPointerException();
5432 <            for (int b; (b = preSplit()) > 0;)
5433 <                (rights = new MapReduceEntriesTask<K,V,U>
5434 <                 (map, this, b, rights, transformer, reducer)).fork();
5435 <            U r = null, u;
5436 <            Object v;
5437 <            while ((v = advance()) != null) {
5438 <                if ((u = transformer.apply(entryFor((K)nextKey, (V)v))) != null)
5439 <                    r = (r == null) ? u : reducer.apply(r, u);
5440 <            }
5441 <            result = r;
5442 <            CountedCompleter<?> c;
5443 <            for (c = firstComplete(); c != null; c = c.nextComplete()) {
5444 <                MapReduceEntriesTask<K,V,U>
5445 <                    t = (MapReduceEntriesTask<K,V,U>)c,
5446 <                    s = t.rights;
5447 <                while (s != null) {
5448 <                    U tr, sr;
5449 <                    if ((sr = s.result) != null)
5450 <                        t.result = (((tr = t.result) == null) ? sr :
5451 <                                    reducer.apply(tr, sr));
5452 <                    s = t.rights = s.nextRight;
5425 >        public final void compute() {
5426 >            final Fun<Map.Entry<K,V>, ? extends U> transformer;
5427 >            final BiFun<? super U, ? super U, ? extends U> reducer;
5428 >            if ((transformer = this.transformer) != null &&
5429 >                (reducer = this.reducer) != null) {
5430 >                for (int i = baseIndex, f, h; batch > 0 &&
5431 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5432 >                    addToPendingCount(1);
5433 >                    (rights = new MapReduceEntriesTask<K,V,U>
5434 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
5435 >                      rights, transformer, reducer)).fork();
5436 >                }
5437 >                U r = null;
5438 >                for (Node<K,V> p; (p = advance()) != null; ) {
5439 >                    U u;
5440 >                    if ((u = transformer.apply(p)) != null)
5441 >                        r = (r == null) ? u : reducer.apply(r, u);
5442 >                }
5443 >                result = r;
5444 >                CountedCompleter<?> c;
5445 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5446 >                    @SuppressWarnings("unchecked") MapReduceEntriesTask<K,V,U>
5447 >                        t = (MapReduceEntriesTask<K,V,U>)c,
5448 >                        s = t.rights;
5449 >                    while (s != null) {
5450 >                        U tr, sr;
5451 >                        if ((sr = s.result) != null)
5452 >                            t.result = (((tr = t.result) == null) ? sr :
5453 >                                        reducer.apply(tr, sr));
5454 >                        s = t.rights = s.nextRight;
5455 >                    }
5456                  }
5457              }
5458          }
5459      }
5460  
5461 <    @SuppressWarnings("serial") static final class MapReduceMappingsTask<K,V,U>
5462 <        extends Traverser<K,V,U> {
5461 >    @SuppressWarnings("serial")
5462 >    static final class MapReduceMappingsTask<K,V,U>
5463 >        extends BulkTask<K,V,U> {
5464          final BiFun<? super K, ? super V, ? extends U> transformer;
5465          final BiFun<? super U, ? super U, ? extends U> reducer;
5466          U result;
5467          MapReduceMappingsTask<K,V,U> rights, nextRight;
5468          MapReduceMappingsTask
5469 <            (ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b,
5469 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5470               MapReduceMappingsTask<K,V,U> nextRight,
5471               BiFun<? super K, ? super V, ? extends U> transformer,
5472               BiFun<? super U, ? super U, ? extends U> reducer) {
5473 <            super(m, p, b); this.nextRight = nextRight;
5473 >            super(p, b, i, f, t); this.nextRight = nextRight;
5474              this.transformer = transformer;
5475              this.reducer = reducer;
5476          }
5477          public final U getRawResult() { return result; }
5478 <        @SuppressWarnings("unchecked") public final void compute() {
5479 <            final BiFun<? super K, ? super V, ? extends U> transformer =
5480 <                this.transformer;
5481 <            final BiFun<? super U, ? super U, ? extends U> reducer =
5482 <                this.reducer;
5483 <            if (transformer == null || reducer == null)
5484 <                throw new NullPointerException();
5485 <            for (int b; (b = preSplit()) > 0;)
5486 <                (rights = new MapReduceMappingsTask<K,V,U>
5487 <                 (map, this, b, rights, transformer, reducer)).fork();
5488 <            U r = null, u;
5489 <            Object v;
5490 <            while ((v = advance()) != null) {
5491 <                if ((u = transformer.apply((K)nextKey, (V)v)) != null)
5492 <                    r = (r == null) ? u : reducer.apply(r, u);
5493 <            }
5494 <            result = r;
5495 <            CountedCompleter<?> c;
5496 <            for (c = firstComplete(); c != null; c = c.nextComplete()) {
5497 <                MapReduceMappingsTask<K,V,U>
5498 <                    t = (MapReduceMappingsTask<K,V,U>)c,
5499 <                    s = t.rights;
5500 <                while (s != null) {
5501 <                    U tr, sr;
5502 <                    if ((sr = s.result) != null)
5503 <                        t.result = (((tr = t.result) == null) ? sr :
5504 <                                    reducer.apply(tr, sr));
5505 <                    s = t.rights = s.nextRight;
5478 >        public final void compute() {
5479 >            final BiFun<? super K, ? super V, ? extends U> transformer;
5480 >            final BiFun<? super U, ? super U, ? extends U> reducer;
5481 >            if ((transformer = this.transformer) != null &&
5482 >                (reducer = this.reducer) != null) {
5483 >                for (int i = baseIndex, f, h; batch > 0 &&
5484 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5485 >                    addToPendingCount(1);
5486 >                    (rights = new MapReduceMappingsTask<K,V,U>
5487 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
5488 >                      rights, transformer, reducer)).fork();
5489 >                }
5490 >                U r = null;
5491 >                for (Node<K,V> p; (p = advance()) != null; ) {
5492 >                    U u;
5493 >                    if ((u = transformer.apply(p.key, p.val)) != null)
5494 >                        r = (r == null) ? u : reducer.apply(r, u);
5495 >                }
5496 >                result = r;
5497 >                CountedCompleter<?> c;
5498 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5499 >                    @SuppressWarnings("unchecked") MapReduceMappingsTask<K,V,U>
5500 >                        t = (MapReduceMappingsTask<K,V,U>)c,
5501 >                        s = t.rights;
5502 >                    while (s != null) {
5503 >                        U tr, sr;
5504 >                        if ((sr = s.result) != null)
5505 >                            t.result = (((tr = t.result) == null) ? sr :
5506 >                                        reducer.apply(tr, sr));
5507 >                        s = t.rights = s.nextRight;
5508 >                    }
5509                  }
5510              }
5511          }
5512      }
5513  
5514 <    @SuppressWarnings("serial") static final class MapReduceKeysToDoubleTask<K,V>
5515 <        extends Traverser<K,V,Double> {
5514 >    @SuppressWarnings("serial")
5515 >    static final class MapReduceKeysToDoubleTask<K,V>
5516 >        extends BulkTask<K,V,Double> {
5517          final ObjectToDouble<? super K> transformer;
5518          final DoubleByDoubleToDouble reducer;
5519          final double basis;
5520          double result;
5521          MapReduceKeysToDoubleTask<K,V> rights, nextRight;
5522          MapReduceKeysToDoubleTask
5523 <            (ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b,
5523 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5524               MapReduceKeysToDoubleTask<K,V> nextRight,
5525               ObjectToDouble<? super K> transformer,
5526               double basis,
5527               DoubleByDoubleToDouble reducer) {
5528 <            super(m, p, b); this.nextRight = nextRight;
5528 >            super(p, b, i, f, t); this.nextRight = nextRight;
5529              this.transformer = transformer;
5530              this.basis = basis; this.reducer = reducer;
5531          }
5532          public final Double getRawResult() { return result; }
5533 <        @SuppressWarnings("unchecked") public final void compute() {
5534 <            final ObjectToDouble<? super K> transformer =
5535 <                this.transformer;
5536 <            final DoubleByDoubleToDouble reducer = this.reducer;
5537 <            if (transformer == null || reducer == null)
5538 <                throw new NullPointerException();
5539 <            double r = this.basis;
5540 <            for (int b; (b = preSplit()) > 0;)
5541 <                (rights = new MapReduceKeysToDoubleTask<K,V>
5542 <                 (map, this, b, rights, transformer, r, reducer)).fork();
5543 <            while (advance() != null)
5544 <                r = reducer.apply(r, transformer.apply((K)nextKey));
5545 <            result = r;
5546 <            CountedCompleter<?> c;
5547 <            for (c = firstComplete(); c != null; c = c.nextComplete()) {
5548 <                MapReduceKeysToDoubleTask<K,V>
5549 <                    t = (MapReduceKeysToDoubleTask<K,V>)c,
5550 <                    s = t.rights;
5551 <                while (s != null) {
5552 <                    t.result = reducer.apply(t.result, s.result);
5553 <                    s = t.rights = s.nextRight;
5533 >        public final void compute() {
5534 >            final ObjectToDouble<? super K> transformer;
5535 >            final DoubleByDoubleToDouble reducer;
5536 >            if ((transformer = this.transformer) != null &&
5537 >                (reducer = this.reducer) != null) {
5538 >                double r = this.basis;
5539 >                for (int i = baseIndex, f, h; batch > 0 &&
5540 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5541 >                    addToPendingCount(1);
5542 >                    (rights = new MapReduceKeysToDoubleTask<K,V>
5543 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
5544 >                      rights, transformer, r, reducer)).fork();
5545 >                }
5546 >                for (Node<K,V> p; (p = advance()) != null; )
5547 >                    r = reducer.apply(r, transformer.apply(p.key));
5548 >                result = r;
5549 >                CountedCompleter<?> c;
5550 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5551 >                    @SuppressWarnings("unchecked") MapReduceKeysToDoubleTask<K,V>
5552 >                        t = (MapReduceKeysToDoubleTask<K,V>)c,
5553 >                        s = t.rights;
5554 >                    while (s != null) {
5555 >                        t.result = reducer.apply(t.result, s.result);
5556 >                        s = t.rights = s.nextRight;
5557 >                    }
5558                  }
5559              }
5560          }
5561      }
5562  
5563 <    @SuppressWarnings("serial") static final class MapReduceValuesToDoubleTask<K,V>
5564 <        extends Traverser<K,V,Double> {
5563 >    @SuppressWarnings("serial")
5564 >    static final class MapReduceValuesToDoubleTask<K,V>
5565 >        extends BulkTask<K,V,Double> {
5566          final ObjectToDouble<? super V> transformer;
5567          final DoubleByDoubleToDouble reducer;
5568          final double basis;
5569          double result;
5570          MapReduceValuesToDoubleTask<K,V> rights, nextRight;
5571          MapReduceValuesToDoubleTask
5572 <            (ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b,
5572 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5573               MapReduceValuesToDoubleTask<K,V> nextRight,
5574               ObjectToDouble<? super V> transformer,
5575               double basis,
5576               DoubleByDoubleToDouble reducer) {
5577 <            super(m, p, b); this.nextRight = nextRight;
5577 >            super(p, b, i, f, t); this.nextRight = nextRight;
5578              this.transformer = transformer;
5579              this.basis = basis; this.reducer = reducer;
5580          }
5581          public final Double getRawResult() { return result; }
5582 <        @SuppressWarnings("unchecked") public final void compute() {
5583 <            final ObjectToDouble<? super V> transformer =
5584 <                this.transformer;
5585 <            final DoubleByDoubleToDouble reducer = this.reducer;
5586 <            if (transformer == null || reducer == null)
5587 <                throw new NullPointerException();
5588 <            double r = this.basis;
5589 <            for (int b; (b = preSplit()) > 0;)
5590 <                (rights = new MapReduceValuesToDoubleTask<K,V>
5591 <                 (map, this, b, rights, transformer, r, reducer)).fork();
5592 <            Object v;
5593 <            while ((v = advance()) != null)
5594 <                r = reducer.apply(r, transformer.apply((V)v));
5595 <            result = r;
5596 <            CountedCompleter<?> c;
5597 <            for (c = firstComplete(); c != null; c = c.nextComplete()) {
5598 <                MapReduceValuesToDoubleTask<K,V>
5599 <                    t = (MapReduceValuesToDoubleTask<K,V>)c,
5600 <                    s = t.rights;
5601 <                while (s != null) {
5602 <                    t.result = reducer.apply(t.result, s.result);
5603 <                    s = t.rights = s.nextRight;
5582 >        public final void compute() {
5583 >            final ObjectToDouble<? super V> transformer;
5584 >            final DoubleByDoubleToDouble reducer;
5585 >            if ((transformer = this.transformer) != null &&
5586 >                (reducer = this.reducer) != null) {
5587 >                double r = this.basis;
5588 >                for (int i = baseIndex, f, h; batch > 0 &&
5589 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5590 >                    addToPendingCount(1);
5591 >                    (rights = new MapReduceValuesToDoubleTask<K,V>
5592 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
5593 >                      rights, transformer, r, reducer)).fork();
5594 >                }
5595 >                for (Node<K,V> p; (p = advance()) != null; )
5596 >                    r = reducer.apply(r, transformer.apply(p.val));
5597 >                result = r;
5598 >                CountedCompleter<?> c;
5599 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5600 >                    @SuppressWarnings("unchecked") MapReduceValuesToDoubleTask<K,V>
5601 >                        t = (MapReduceValuesToDoubleTask<K,V>)c,
5602 >                        s = t.rights;
5603 >                    while (s != null) {
5604 >                        t.result = reducer.apply(t.result, s.result);
5605 >                        s = t.rights = s.nextRight;
5606 >                    }
5607                  }
5608              }
5609          }
5610      }
5611  
5612 <    @SuppressWarnings("serial") static final class MapReduceEntriesToDoubleTask<K,V>
5613 <        extends Traverser<K,V,Double> {
5612 >    @SuppressWarnings("serial")
5613 >    static final class MapReduceEntriesToDoubleTask<K,V>
5614 >        extends BulkTask<K,V,Double> {
5615          final ObjectToDouble<Map.Entry<K,V>> transformer;
5616          final DoubleByDoubleToDouble reducer;
5617          final double basis;
5618          double result;
5619          MapReduceEntriesToDoubleTask<K,V> rights, nextRight;
5620          MapReduceEntriesToDoubleTask
5621 <            (ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b,
5621 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5622               MapReduceEntriesToDoubleTask<K,V> nextRight,
5623               ObjectToDouble<Map.Entry<K,V>> transformer,
5624               double basis,
5625               DoubleByDoubleToDouble reducer) {
5626 <            super(m, p, b); this.nextRight = nextRight;
5626 >            super(p, b, i, f, t); this.nextRight = nextRight;
5627              this.transformer = transformer;
5628              this.basis = basis; this.reducer = reducer;
5629          }
5630          public final Double getRawResult() { return result; }
5631 <        @SuppressWarnings("unchecked") public final void compute() {
5632 <            final ObjectToDouble<Map.Entry<K,V>> transformer =
5633 <                this.transformer;
5634 <            final DoubleByDoubleToDouble reducer = this.reducer;
5635 <            if (transformer == null || reducer == null)
5636 <                throw new NullPointerException();
5637 <            double r = this.basis;
5638 <            for (int b; (b = preSplit()) > 0;)
5639 <                (rights = new MapReduceEntriesToDoubleTask<K,V>
5640 <                 (map, this, b, rights, transformer, r, reducer)).fork();
5641 <            Object v;
5642 <            while ((v = advance()) != null)
5643 <                r = reducer.apply(r, transformer.apply(entryFor((K)nextKey, (V)v)));
5644 <            result = r;
5645 <            CountedCompleter<?> c;
5646 <            for (c = firstComplete(); c != null; c = c.nextComplete()) {
5647 <                MapReduceEntriesToDoubleTask<K,V>
5648 <                    t = (MapReduceEntriesToDoubleTask<K,V>)c,
5649 <                    s = t.rights;
5650 <                while (s != null) {
5651 <                    t.result = reducer.apply(t.result, s.result);
5652 <                    s = t.rights = s.nextRight;
5631 >        public final void compute() {
5632 >            final ObjectToDouble<Map.Entry<K,V>> transformer;
5633 >            final DoubleByDoubleToDouble reducer;
5634 >            if ((transformer = this.transformer) != null &&
5635 >                (reducer = this.reducer) != null) {
5636 >                double r = this.basis;
5637 >                for (int i = baseIndex, f, h; batch > 0 &&
5638 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5639 >                    addToPendingCount(1);
5640 >                    (rights = new MapReduceEntriesToDoubleTask<K,V>
5641 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
5642 >                      rights, transformer, r, reducer)).fork();
5643 >                }
5644 >                for (Node<K,V> p; (p = advance()) != null; )
5645 >                    r = reducer.apply(r, transformer.apply(p));
5646 >                result = r;
5647 >                CountedCompleter<?> c;
5648 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5649 >                    @SuppressWarnings("unchecked") MapReduceEntriesToDoubleTask<K,V>
5650 >                        t = (MapReduceEntriesToDoubleTask<K,V>)c,
5651 >                        s = t.rights;
5652 >                    while (s != null) {
5653 >                        t.result = reducer.apply(t.result, s.result);
5654 >                        s = t.rights = s.nextRight;
5655 >                    }
5656                  }
5657              }
5658          }
5659      }
5660  
5661 <    @SuppressWarnings("serial") static final class MapReduceMappingsToDoubleTask<K,V>
5662 <        extends Traverser<K,V,Double> {
5661 >    @SuppressWarnings("serial")
5662 >    static final class MapReduceMappingsToDoubleTask<K,V>
5663 >        extends BulkTask<K,V,Double> {
5664          final ObjectByObjectToDouble<? super K, ? super V> transformer;
5665          final DoubleByDoubleToDouble reducer;
5666          final double basis;
5667          double result;
5668          MapReduceMappingsToDoubleTask<K,V> rights, nextRight;
5669          MapReduceMappingsToDoubleTask
5670 <            (ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b,
5670 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5671               MapReduceMappingsToDoubleTask<K,V> nextRight,
5672               ObjectByObjectToDouble<? super K, ? super V> transformer,
5673               double basis,
5674               DoubleByDoubleToDouble reducer) {
5675 <            super(m, p, b); this.nextRight = nextRight;
5675 >            super(p, b, i, f, t); this.nextRight = nextRight;
5676              this.transformer = transformer;
5677              this.basis = basis; this.reducer = reducer;
5678          }
5679          public final Double getRawResult() { return result; }
5680 <        @SuppressWarnings("unchecked") public final void compute() {
5681 <            final ObjectByObjectToDouble<? super K, ? super V> transformer =
5682 <                this.transformer;
5683 <            final DoubleByDoubleToDouble reducer = this.reducer;
5684 <            if (transformer == null || reducer == null)
5685 <                throw new NullPointerException();
5686 <            double r = this.basis;
5687 <            for (int b; (b = preSplit()) > 0;)
5688 <                (rights = new MapReduceMappingsToDoubleTask<K,V>
5689 <                 (map, this, b, rights, transformer, r, reducer)).fork();
5690 <            Object v;
5691 <            while ((v = advance()) != null)
5692 <                r = reducer.apply(r, transformer.apply((K)nextKey, (V)v));
5693 <            result = r;
5694 <            CountedCompleter<?> c;
5695 <            for (c = firstComplete(); c != null; c = c.nextComplete()) {
5696 <                MapReduceMappingsToDoubleTask<K,V>
5697 <                    t = (MapReduceMappingsToDoubleTask<K,V>)c,
5698 <                    s = t.rights;
5699 <                while (s != null) {
5700 <                    t.result = reducer.apply(t.result, s.result);
5701 <                    s = t.rights = s.nextRight;
5680 >        public final void compute() {
5681 >            final ObjectByObjectToDouble<? super K, ? super V> transformer;
5682 >            final DoubleByDoubleToDouble reducer;
5683 >            if ((transformer = this.transformer) != null &&
5684 >                (reducer = this.reducer) != null) {
5685 >                double r = this.basis;
5686 >                for (int i = baseIndex, f, h; batch > 0 &&
5687 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5688 >                    addToPendingCount(1);
5689 >                    (rights = new MapReduceMappingsToDoubleTask<K,V>
5690 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
5691 >                      rights, transformer, r, reducer)).fork();
5692 >                }
5693 >                for (Node<K,V> p; (p = advance()) != null; )
5694 >                    r = reducer.apply(r, transformer.apply(p.key, p.val));
5695 >                result = r;
5696 >                CountedCompleter<?> c;
5697 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5698 >                    @SuppressWarnings("unchecked") MapReduceMappingsToDoubleTask<K,V>
5699 >                        t = (MapReduceMappingsToDoubleTask<K,V>)c,
5700 >                        s = t.rights;
5701 >                    while (s != null) {
5702 >                        t.result = reducer.apply(t.result, s.result);
5703 >                        s = t.rights = s.nextRight;
5704 >                    }
5705                  }
5706              }
5707          }
5708      }
5709  
5710 <    @SuppressWarnings("serial") static final class MapReduceKeysToLongTask<K,V>
5711 <        extends Traverser<K,V,Long> {
5710 >    @SuppressWarnings("serial")
5711 >    static final class MapReduceKeysToLongTask<K,V>
5712 >        extends BulkTask<K,V,Long> {
5713          final ObjectToLong<? super K> transformer;
5714          final LongByLongToLong reducer;
5715          final long basis;
5716          long result;
5717          MapReduceKeysToLongTask<K,V> rights, nextRight;
5718          MapReduceKeysToLongTask
5719 <            (ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b,
5719 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5720               MapReduceKeysToLongTask<K,V> nextRight,
5721               ObjectToLong<? super K> transformer,
5722               long basis,
5723               LongByLongToLong reducer) {
5724 <            super(m, p, b); this.nextRight = nextRight;
5724 >            super(p, b, i, f, t); this.nextRight = nextRight;
5725              this.transformer = transformer;
5726              this.basis = basis; this.reducer = reducer;
5727          }
5728          public final Long getRawResult() { return result; }
5729 <        @SuppressWarnings("unchecked") public final void compute() {
5730 <            final ObjectToLong<? super K> transformer =
5731 <                this.transformer;
5732 <            final LongByLongToLong reducer = this.reducer;
5733 <            if (transformer == null || reducer == null)
5734 <                throw new NullPointerException();
5735 <            long r = this.basis;
5736 <            for (int b; (b = preSplit()) > 0;)
5737 <                (rights = new MapReduceKeysToLongTask<K,V>
5738 <                 (map, this, b, rights, transformer, r, reducer)).fork();
5739 <            while (advance() != null)
5740 <                r = reducer.apply(r, transformer.apply((K)nextKey));
5741 <            result = r;
5742 <            CountedCompleter<?> c;
5743 <            for (c = firstComplete(); c != null; c = c.nextComplete()) {
5744 <                MapReduceKeysToLongTask<K,V>
5745 <                    t = (MapReduceKeysToLongTask<K,V>)c,
5746 <                    s = t.rights;
5747 <                while (s != null) {
5748 <                    t.result = reducer.apply(t.result, s.result);
5749 <                    s = t.rights = s.nextRight;
5729 >        public final void compute() {
5730 >            final ObjectToLong<? super K> transformer;
5731 >            final LongByLongToLong reducer;
5732 >            if ((transformer = this.transformer) != null &&
5733 >                (reducer = this.reducer) != null) {
5734 >                long r = this.basis;
5735 >                for (int i = baseIndex, f, h; batch > 0 &&
5736 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5737 >                    addToPendingCount(1);
5738 >                    (rights = new MapReduceKeysToLongTask<K,V>
5739 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
5740 >                      rights, transformer, r, reducer)).fork();
5741 >                }
5742 >                for (Node<K,V> p; (p = advance()) != null; )
5743 >                    r = reducer.apply(r, transformer.apply(p.key));
5744 >                result = r;
5745 >                CountedCompleter<?> c;
5746 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5747 >                    @SuppressWarnings("unchecked") MapReduceKeysToLongTask<K,V>
5748 >                        t = (MapReduceKeysToLongTask<K,V>)c,
5749 >                        s = t.rights;
5750 >                    while (s != null) {
5751 >                        t.result = reducer.apply(t.result, s.result);
5752 >                        s = t.rights = s.nextRight;
5753 >                    }
5754                  }
5755              }
5756          }
5757      }
5758  
5759 <    @SuppressWarnings("serial") static final class MapReduceValuesToLongTask<K,V>
5760 <        extends Traverser<K,V,Long> {
5759 >    @SuppressWarnings("serial")
5760 >    static final class MapReduceValuesToLongTask<K,V>
5761 >        extends BulkTask<K,V,Long> {
5762          final ObjectToLong<? super V> transformer;
5763          final LongByLongToLong reducer;
5764          final long basis;
5765          long result;
5766          MapReduceValuesToLongTask<K,V> rights, nextRight;
5767          MapReduceValuesToLongTask
5768 <            (ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b,
5768 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5769               MapReduceValuesToLongTask<K,V> nextRight,
5770               ObjectToLong<? super V> transformer,
5771               long basis,
5772               LongByLongToLong reducer) {
5773 <            super(m, p, b); this.nextRight = nextRight;
5773 >            super(p, b, i, f, t); this.nextRight = nextRight;
5774              this.transformer = transformer;
5775              this.basis = basis; this.reducer = reducer;
5776          }
5777          public final Long getRawResult() { return result; }
5778 <        @SuppressWarnings("unchecked") public final void compute() {
5779 <            final ObjectToLong<? super V> transformer =
5780 <                this.transformer;
5781 <            final LongByLongToLong reducer = this.reducer;
5782 <            if (transformer == null || reducer == null)
5783 <                throw new NullPointerException();
5784 <            long r = this.basis;
5785 <            for (int b; (b = preSplit()) > 0;)
5786 <                (rights = new MapReduceValuesToLongTask<K,V>
5787 <                 (map, this, b, rights, transformer, r, reducer)).fork();
5788 <            Object v;
5789 <            while ((v = advance()) != null)
5790 <                r = reducer.apply(r, transformer.apply((V)v));
5791 <            result = r;
5792 <            CountedCompleter<?> c;
5793 <            for (c = firstComplete(); c != null; c = c.nextComplete()) {
5794 <                MapReduceValuesToLongTask<K,V>
5795 <                    t = (MapReduceValuesToLongTask<K,V>)c,
5796 <                    s = t.rights;
5797 <                while (s != null) {
5798 <                    t.result = reducer.apply(t.result, s.result);
5799 <                    s = t.rights = s.nextRight;
5778 >        public final void compute() {
5779 >            final ObjectToLong<? super V> transformer;
5780 >            final LongByLongToLong reducer;
5781 >            if ((transformer = this.transformer) != null &&
5782 >                (reducer = this.reducer) != null) {
5783 >                long r = this.basis;
5784 >                for (int i = baseIndex, f, h; batch > 0 &&
5785 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5786 >                    addToPendingCount(1);
5787 >                    (rights = new MapReduceValuesToLongTask<K,V>
5788 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
5789 >                      rights, transformer, r, reducer)).fork();
5790 >                }
5791 >                for (Node<K,V> p; (p = advance()) != null; )
5792 >                    r = reducer.apply(r, transformer.apply(p.val));
5793 >                result = r;
5794 >                CountedCompleter<?> c;
5795 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5796 >                    @SuppressWarnings("unchecked") MapReduceValuesToLongTask<K,V>
5797 >                        t = (MapReduceValuesToLongTask<K,V>)c,
5798 >                        s = t.rights;
5799 >                    while (s != null) {
5800 >                        t.result = reducer.apply(t.result, s.result);
5801 >                        s = t.rights = s.nextRight;
5802 >                    }
5803                  }
5804              }
5805          }
5806      }
5807  
5808 <    @SuppressWarnings("serial") static final class MapReduceEntriesToLongTask<K,V>
5809 <        extends Traverser<K,V,Long> {
5808 >    @SuppressWarnings("serial")
5809 >    static final class MapReduceEntriesToLongTask<K,V>
5810 >        extends BulkTask<K,V,Long> {
5811          final ObjectToLong<Map.Entry<K,V>> transformer;
5812          final LongByLongToLong reducer;
5813          final long basis;
5814          long result;
5815          MapReduceEntriesToLongTask<K,V> rights, nextRight;
5816          MapReduceEntriesToLongTask
5817 <            (ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b,
5817 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5818               MapReduceEntriesToLongTask<K,V> nextRight,
5819               ObjectToLong<Map.Entry<K,V>> transformer,
5820               long basis,
5821               LongByLongToLong reducer) {
5822 <            super(m, p, b); this.nextRight = nextRight;
5822 >            super(p, b, i, f, t); this.nextRight = nextRight;
5823              this.transformer = transformer;
5824              this.basis = basis; this.reducer = reducer;
5825          }
5826          public final Long getRawResult() { return result; }
5827 <        @SuppressWarnings("unchecked") public final void compute() {
5828 <            final ObjectToLong<Map.Entry<K,V>> transformer =
5829 <                this.transformer;
5830 <            final LongByLongToLong reducer = this.reducer;
5831 <            if (transformer == null || reducer == null)
5832 <                throw new NullPointerException();
5833 <            long r = this.basis;
5834 <            for (int b; (b = preSplit()) > 0;)
5835 <                (rights = new MapReduceEntriesToLongTask<K,V>
5836 <                 (map, this, b, rights, transformer, r, reducer)).fork();
5837 <            Object v;
5838 <            while ((v = advance()) != null)
5839 <                r = reducer.apply(r, transformer.apply(entryFor((K)nextKey, (V)v)));
5840 <            result = r;
5841 <            CountedCompleter<?> c;
5842 <            for (c = firstComplete(); c != null; c = c.nextComplete()) {
5843 <                MapReduceEntriesToLongTask<K,V>
5844 <                    t = (MapReduceEntriesToLongTask<K,V>)c,
5845 <                    s = t.rights;
5846 <                while (s != null) {
5847 <                    t.result = reducer.apply(t.result, s.result);
5848 <                    s = t.rights = s.nextRight;
5827 >        public final void compute() {
5828 >            final ObjectToLong<Map.Entry<K,V>> transformer;
5829 >            final LongByLongToLong reducer;
5830 >            if ((transformer = this.transformer) != null &&
5831 >                (reducer = this.reducer) != null) {
5832 >                long r = this.basis;
5833 >                for (int i = baseIndex, f, h; batch > 0 &&
5834 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5835 >                    addToPendingCount(1);
5836 >                    (rights = new MapReduceEntriesToLongTask<K,V>
5837 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
5838 >                      rights, transformer, r, reducer)).fork();
5839 >                }
5840 >                for (Node<K,V> p; (p = advance()) != null; )
5841 >                    r = reducer.apply(r, transformer.apply(p));
5842 >                result = r;
5843 >                CountedCompleter<?> c;
5844 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5845 >                    @SuppressWarnings("unchecked") MapReduceEntriesToLongTask<K,V>
5846 >                        t = (MapReduceEntriesToLongTask<K,V>)c,
5847 >                        s = t.rights;
5848 >                    while (s != null) {
5849 >                        t.result = reducer.apply(t.result, s.result);
5850 >                        s = t.rights = s.nextRight;
5851 >                    }
5852                  }
5853              }
5854          }
5855      }
5856  
5857 <    @SuppressWarnings("serial") static final class MapReduceMappingsToLongTask<K,V>
5858 <        extends Traverser<K,V,Long> {
5857 >    @SuppressWarnings("serial")
5858 >    static final class MapReduceMappingsToLongTask<K,V>
5859 >        extends BulkTask<K,V,Long> {
5860          final ObjectByObjectToLong<? super K, ? super V> transformer;
5861          final LongByLongToLong reducer;
5862          final long basis;
5863          long result;
5864          MapReduceMappingsToLongTask<K,V> rights, nextRight;
5865          MapReduceMappingsToLongTask
5866 <            (ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b,
5866 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5867               MapReduceMappingsToLongTask<K,V> nextRight,
5868               ObjectByObjectToLong<? super K, ? super V> transformer,
5869               long basis,
5870               LongByLongToLong reducer) {
5871 <            super(m, p, b); this.nextRight = nextRight;
5871 >            super(p, b, i, f, t); this.nextRight = nextRight;
5872              this.transformer = transformer;
5873              this.basis = basis; this.reducer = reducer;
5874          }
5875          public final Long getRawResult() { return result; }
5876 <        @SuppressWarnings("unchecked") public final void compute() {
5877 <            final ObjectByObjectToLong<? super K, ? super V> transformer =
5878 <                this.transformer;
5879 <            final LongByLongToLong reducer = this.reducer;
5880 <            if (transformer == null || reducer == null)
5881 <                throw new NullPointerException();
5882 <            long r = this.basis;
5883 <            for (int b; (b = preSplit()) > 0;)
5884 <                (rights = new MapReduceMappingsToLongTask<K,V>
5885 <                 (map, this, b, rights, transformer, r, reducer)).fork();
5886 <            Object v;
5887 <            while ((v = advance()) != null)
5888 <                r = reducer.apply(r, transformer.apply((K)nextKey, (V)v));
5889 <            result = r;
5890 <            CountedCompleter<?> c;
5891 <            for (c = firstComplete(); c != null; c = c.nextComplete()) {
5892 <                MapReduceMappingsToLongTask<K,V>
5893 <                    t = (MapReduceMappingsToLongTask<K,V>)c,
5894 <                    s = t.rights;
5895 <                while (s != null) {
5896 <                    t.result = reducer.apply(t.result, s.result);
5897 <                    s = t.rights = s.nextRight;
5876 >        public final void compute() {
5877 >            final ObjectByObjectToLong<? super K, ? super V> transformer;
5878 >            final LongByLongToLong reducer;
5879 >            if ((transformer = this.transformer) != null &&
5880 >                (reducer = this.reducer) != null) {
5881 >                long r = this.basis;
5882 >                for (int i = baseIndex, f, h; batch > 0 &&
5883 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5884 >                    addToPendingCount(1);
5885 >                    (rights = new MapReduceMappingsToLongTask<K,V>
5886 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
5887 >                      rights, transformer, r, reducer)).fork();
5888 >                }
5889 >                for (Node<K,V> p; (p = advance()) != null; )
5890 >                    r = reducer.apply(r, transformer.apply(p.key, p.val));
5891 >                result = r;
5892 >                CountedCompleter<?> c;
5893 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5894 >                    @SuppressWarnings("unchecked") MapReduceMappingsToLongTask<K,V>
5895 >                        t = (MapReduceMappingsToLongTask<K,V>)c,
5896 >                        s = t.rights;
5897 >                    while (s != null) {
5898 >                        t.result = reducer.apply(t.result, s.result);
5899 >                        s = t.rights = s.nextRight;
5900 >                    }
5901                  }
5902              }
5903          }
5904      }
5905  
5906 <    @SuppressWarnings("serial") static final class MapReduceKeysToIntTask<K,V>
5907 <        extends Traverser<K,V,Integer> {
5906 >    @SuppressWarnings("serial")
5907 >    static final class MapReduceKeysToIntTask<K,V>
5908 >        extends BulkTask<K,V,Integer> {
5909          final ObjectToInt<? super K> transformer;
5910          final IntByIntToInt reducer;
5911          final int basis;
5912          int result;
5913          MapReduceKeysToIntTask<K,V> rights, nextRight;
5914          MapReduceKeysToIntTask
5915 <            (ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b,
5915 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5916               MapReduceKeysToIntTask<K,V> nextRight,
5917               ObjectToInt<? super K> transformer,
5918               int basis,
5919               IntByIntToInt reducer) {
5920 <            super(m, p, b); this.nextRight = nextRight;
5920 >            super(p, b, i, f, t); this.nextRight = nextRight;
5921              this.transformer = transformer;
5922              this.basis = basis; this.reducer = reducer;
5923          }
5924          public final Integer getRawResult() { return result; }
5925 <        @SuppressWarnings("unchecked") public final void compute() {
5926 <            final ObjectToInt<? super K> transformer =
5927 <                this.transformer;
5928 <            final IntByIntToInt reducer = this.reducer;
5929 <            if (transformer == null || reducer == null)
5930 <                throw new NullPointerException();
5931 <            int r = this.basis;
5932 <            for (int b; (b = preSplit()) > 0;)
5933 <                (rights = new MapReduceKeysToIntTask<K,V>
5934 <                 (map, this, b, rights, transformer, r, reducer)).fork();
5935 <            while (advance() != null)
5936 <                r = reducer.apply(r, transformer.apply((K)nextKey));
5937 <            result = r;
5938 <            CountedCompleter<?> c;
5939 <            for (c = firstComplete(); c != null; c = c.nextComplete()) {
5940 <                MapReduceKeysToIntTask<K,V>
5941 <                    t = (MapReduceKeysToIntTask<K,V>)c,
5942 <                    s = t.rights;
5943 <                while (s != null) {
5944 <                    t.result = reducer.apply(t.result, s.result);
5945 <                    s = t.rights = s.nextRight;
5925 >        public final void compute() {
5926 >            final ObjectToInt<? super K> transformer;
5927 >            final IntByIntToInt reducer;
5928 >            if ((transformer = this.transformer) != null &&
5929 >                (reducer = this.reducer) != null) {
5930 >                int r = this.basis;
5931 >                for (int i = baseIndex, f, h; batch > 0 &&
5932 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5933 >                    addToPendingCount(1);
5934 >                    (rights = new MapReduceKeysToIntTask<K,V>
5935 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
5936 >                      rights, transformer, r, reducer)).fork();
5937 >                }
5938 >                for (Node<K,V> p; (p = advance()) != null; )
5939 >                    r = reducer.apply(r, transformer.apply(p.key));
5940 >                result = r;
5941 >                CountedCompleter<?> c;
5942 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5943 >                    @SuppressWarnings("unchecked") MapReduceKeysToIntTask<K,V>
5944 >                        t = (MapReduceKeysToIntTask<K,V>)c,
5945 >                        s = t.rights;
5946 >                    while (s != null) {
5947 >                        t.result = reducer.apply(t.result, s.result);
5948 >                        s = t.rights = s.nextRight;
5949 >                    }
5950                  }
5951              }
5952          }
5953      }
5954  
5955 <    @SuppressWarnings("serial") static final class MapReduceValuesToIntTask<K,V>
5956 <        extends Traverser<K,V,Integer> {
5955 >    @SuppressWarnings("serial")
5956 >    static final class MapReduceValuesToIntTask<K,V>
5957 >        extends BulkTask<K,V,Integer> {
5958          final ObjectToInt<? super V> transformer;
5959          final IntByIntToInt reducer;
5960          final int basis;
5961          int result;
5962          MapReduceValuesToIntTask<K,V> rights, nextRight;
5963          MapReduceValuesToIntTask
5964 <            (ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b,
5964 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5965               MapReduceValuesToIntTask<K,V> nextRight,
5966               ObjectToInt<? super V> transformer,
5967               int basis,
5968               IntByIntToInt reducer) {
5969 <            super(m, p, b); this.nextRight = nextRight;
5969 >            super(p, b, i, f, t); this.nextRight = nextRight;
5970              this.transformer = transformer;
5971              this.basis = basis; this.reducer = reducer;
5972          }
5973          public final Integer getRawResult() { return result; }
5974 <        @SuppressWarnings("unchecked") public final void compute() {
5975 <            final ObjectToInt<? super V> transformer =
5976 <                this.transformer;
5977 <            final IntByIntToInt reducer = this.reducer;
5978 <            if (transformer == null || reducer == null)
5979 <                throw new NullPointerException();
5980 <            int r = this.basis;
5981 <            for (int b; (b = preSplit()) > 0;)
5982 <                (rights = new MapReduceValuesToIntTask<K,V>
5983 <                 (map, this, b, rights, transformer, r, reducer)).fork();
5984 <            Object v;
5985 <            while ((v = advance()) != null)
5986 <                r = reducer.apply(r, transformer.apply((V)v));
5987 <            result = r;
5988 <            CountedCompleter<?> c;
5989 <            for (c = firstComplete(); c != null; c = c.nextComplete()) {
5990 <                MapReduceValuesToIntTask<K,V>
5991 <                    t = (MapReduceValuesToIntTask<K,V>)c,
5992 <                    s = t.rights;
5993 <                while (s != null) {
5994 <                    t.result = reducer.apply(t.result, s.result);
5995 <                    s = t.rights = s.nextRight;
5974 >        public final void compute() {
5975 >            final ObjectToInt<? super V> transformer;
5976 >            final IntByIntToInt reducer;
5977 >            if ((transformer = this.transformer) != null &&
5978 >                (reducer = this.reducer) != null) {
5979 >                int r = this.basis;
5980 >                for (int i = baseIndex, f, h; batch > 0 &&
5981 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5982 >                    addToPendingCount(1);
5983 >                    (rights = new MapReduceValuesToIntTask<K,V>
5984 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
5985 >                      rights, transformer, r, reducer)).fork();
5986 >                }
5987 >                for (Node<K,V> p; (p = advance()) != null; )
5988 >                    r = reducer.apply(r, transformer.apply(p.val));
5989 >                result = r;
5990 >                CountedCompleter<?> c;
5991 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5992 >                    @SuppressWarnings("unchecked") MapReduceValuesToIntTask<K,V>
5993 >                        t = (MapReduceValuesToIntTask<K,V>)c,
5994 >                        s = t.rights;
5995 >                    while (s != null) {
5996 >                        t.result = reducer.apply(t.result, s.result);
5997 >                        s = t.rights = s.nextRight;
5998 >                    }
5999                  }
6000              }
6001          }
6002      }
6003  
6004 <    @SuppressWarnings("serial") static final class MapReduceEntriesToIntTask<K,V>
6005 <        extends Traverser<K,V,Integer> {
6004 >    @SuppressWarnings("serial")
6005 >    static final class MapReduceEntriesToIntTask<K,V>
6006 >        extends BulkTask<K,V,Integer> {
6007          final ObjectToInt<Map.Entry<K,V>> transformer;
6008          final IntByIntToInt reducer;
6009          final int basis;
6010          int result;
6011          MapReduceEntriesToIntTask<K,V> rights, nextRight;
6012          MapReduceEntriesToIntTask
6013 <            (ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b,
6013 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
6014               MapReduceEntriesToIntTask<K,V> nextRight,
6015               ObjectToInt<Map.Entry<K,V>> transformer,
6016               int basis,
6017               IntByIntToInt reducer) {
6018 <            super(m, p, b); this.nextRight = nextRight;
6018 >            super(p, b, i, f, t); this.nextRight = nextRight;
6019              this.transformer = transformer;
6020              this.basis = basis; this.reducer = reducer;
6021          }
6022          public final Integer getRawResult() { return result; }
6023 <        @SuppressWarnings("unchecked") public final void compute() {
6024 <            final ObjectToInt<Map.Entry<K,V>> transformer =
6025 <                this.transformer;
6026 <            final IntByIntToInt reducer = this.reducer;
6027 <            if (transformer == null || reducer == null)
6028 <                throw new NullPointerException();
6029 <            int r = this.basis;
6030 <            for (int b; (b = preSplit()) > 0;)
6031 <                (rights = new MapReduceEntriesToIntTask<K,V>
6032 <                 (map, this, b, rights, transformer, r, reducer)).fork();
6033 <            Object v;
6034 <            while ((v = advance()) != null)
6035 <                r = reducer.apply(r, transformer.apply(entryFor((K)nextKey, (V)v)));
6036 <            result = r;
6037 <            CountedCompleter<?> c;
6038 <            for (c = firstComplete(); c != null; c = c.nextComplete()) {
6039 <                MapReduceEntriesToIntTask<K,V>
6040 <                    t = (MapReduceEntriesToIntTask<K,V>)c,
6041 <                    s = t.rights;
6042 <                while (s != null) {
6043 <                    t.result = reducer.apply(t.result, s.result);
6044 <                    s = t.rights = s.nextRight;
6023 >        public final void compute() {
6024 >            final ObjectToInt<Map.Entry<K,V>> transformer;
6025 >            final IntByIntToInt reducer;
6026 >            if ((transformer = this.transformer) != null &&
6027 >                (reducer = this.reducer) != null) {
6028 >                int r = this.basis;
6029 >                for (int i = baseIndex, f, h; batch > 0 &&
6030 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
6031 >                    addToPendingCount(1);
6032 >                    (rights = new MapReduceEntriesToIntTask<K,V>
6033 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
6034 >                      rights, transformer, r, reducer)).fork();
6035 >                }
6036 >                for (Node<K,V> p; (p = advance()) != null; )
6037 >                    r = reducer.apply(r, transformer.apply(p));
6038 >                result = r;
6039 >                CountedCompleter<?> c;
6040 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
6041 >                    @SuppressWarnings("unchecked") MapReduceEntriesToIntTask<K,V>
6042 >                        t = (MapReduceEntriesToIntTask<K,V>)c,
6043 >                        s = t.rights;
6044 >                    while (s != null) {
6045 >                        t.result = reducer.apply(t.result, s.result);
6046 >                        s = t.rights = s.nextRight;
6047 >                    }
6048                  }
6049              }
6050          }
6051      }
6052  
6053 <    @SuppressWarnings("serial") static final class MapReduceMappingsToIntTask<K,V>
6054 <        extends Traverser<K,V,Integer> {
6053 >    @SuppressWarnings("serial")
6054 >    static final class MapReduceMappingsToIntTask<K,V>
6055 >        extends BulkTask<K,V,Integer> {
6056          final ObjectByObjectToInt<? super K, ? super V> transformer;
6057          final IntByIntToInt reducer;
6058          final int basis;
6059          int result;
6060          MapReduceMappingsToIntTask<K,V> rights, nextRight;
6061          MapReduceMappingsToIntTask
6062 <            (ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b,
6062 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
6063               MapReduceMappingsToIntTask<K,V> nextRight,
6064               ObjectByObjectToInt<? super K, ? super V> transformer,
6065               int basis,
6066               IntByIntToInt reducer) {
6067 <            super(m, p, b); this.nextRight = nextRight;
6067 >            super(p, b, i, f, t); this.nextRight = nextRight;
6068              this.transformer = transformer;
6069              this.basis = basis; this.reducer = reducer;
6070          }
6071          public final Integer getRawResult() { return result; }
6072 <        @SuppressWarnings("unchecked") public final void compute() {
6073 <            final ObjectByObjectToInt<? super K, ? super V> transformer =
6074 <                this.transformer;
6075 <            final IntByIntToInt reducer = this.reducer;
6076 <            if (transformer == null || reducer == null)
6077 <                throw new NullPointerException();
6078 <            int r = this.basis;
6079 <            for (int b; (b = preSplit()) > 0;)
6080 <                (rights = new MapReduceMappingsToIntTask<K,V>
6081 <                 (map, this, b, rights, transformer, r, reducer)).fork();
6082 <            Object v;
6083 <            while ((v = advance()) != null)
6084 <                r = reducer.apply(r, transformer.apply((K)nextKey, (V)v));
6085 <            result = r;
6086 <            CountedCompleter<?> c;
6087 <            for (c = firstComplete(); c != null; c = c.nextComplete()) {
6088 <                MapReduceMappingsToIntTask<K,V>
6089 <                    t = (MapReduceMappingsToIntTask<K,V>)c,
6090 <                    s = t.rights;
6091 <                while (s != null) {
6092 <                    t.result = reducer.apply(t.result, s.result);
6093 <                    s = t.rights = s.nextRight;
6072 >        public final void compute() {
6073 >            final ObjectByObjectToInt<? super K, ? super V> transformer;
6074 >            final IntByIntToInt reducer;
6075 >            if ((transformer = this.transformer) != null &&
6076 >                (reducer = this.reducer) != null) {
6077 >                int r = this.basis;
6078 >                for (int i = baseIndex, f, h; batch > 0 &&
6079 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
6080 >                    addToPendingCount(1);
6081 >                    (rights = new MapReduceMappingsToIntTask<K,V>
6082 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
6083 >                      rights, transformer, r, reducer)).fork();
6084 >                }
6085 >                for (Node<K,V> p; (p = advance()) != null; )
6086 >                    r = reducer.apply(r, transformer.apply(p.key, p.val));
6087 >                result = r;
6088 >                CountedCompleter<?> c;
6089 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
6090 >                    @SuppressWarnings("unchecked") MapReduceMappingsToIntTask<K,V>
6091 >                        t = (MapReduceMappingsToIntTask<K,V>)c,
6092 >                        s = t.rights;
6093 >                    while (s != null) {
6094 >                        t.result = reducer.apply(t.result, s.result);
6095 >                        s = t.rights = s.nextRight;
6096 >                    }
6097 >                }
6098 >            }
6099 >        }
6100 >    }
6101 >
6102 >    /* ---------------- Counters -------------- */
6103 >
6104 >    // Adapted from LongAdder and Striped64.
6105 >    // See their internal docs for explanation.
6106 >
6107 >    // A padded cell for distributing counts
6108 >    static final class CounterCell {
6109 >        volatile long p0, p1, p2, p3, p4, p5, p6;
6110 >        volatile long value;
6111 >        volatile long q0, q1, q2, q3, q4, q5, q6;
6112 >        CounterCell(long x) { value = x; }
6113 >    }
6114 >
6115 >    /**
6116 >     * Holder for the thread-local hash code determining which
6117 >     * CounterCell to use. The code is initialized via the
6118 >     * counterHashCodeGenerator, but may be moved upon collisions.
6119 >     */
6120 >    static final class CounterHashCode {
6121 >        int code;
6122 >    }
6123 >
6124 >    /**
6125 >     * Generates initial value for per-thread CounterHashCodes.
6126 >     */
6127 >    static final AtomicInteger counterHashCodeGenerator = new AtomicInteger();
6128 >
6129 >    /**
6130 >     * Increment for counterHashCodeGenerator. See class ThreadLocal
6131 >     * for explanation.
6132 >     */
6133 >    static final int SEED_INCREMENT = 0x61c88647;
6134 >
6135 >    /**
6136 >     * Per-thread counter hash codes. Shared across all instances.
6137 >     */
6138 >    static final ThreadLocal<CounterHashCode> threadCounterHashCode =
6139 >        new ThreadLocal<CounterHashCode>();
6140 >
6141 >
6142 >    final long sumCount() {
6143 >        CounterCell[] as = counterCells; CounterCell a;
6144 >        long sum = baseCount;
6145 >        if (as != null) {
6146 >            for (int i = 0; i < as.length; ++i) {
6147 >                if ((a = as[i]) != null)
6148 >                    sum += a.value;
6149 >            }
6150 >        }
6151 >        return sum;
6152 >    }
6153 >
6154 >    // See LongAdder version for explanation
6155 >    private final void fullAddCount(long x, CounterHashCode hc,
6156 >                                    boolean wasUncontended) {
6157 >        int h;
6158 >        if (hc == null) {
6159 >            hc = new CounterHashCode();
6160 >            int s = counterHashCodeGenerator.addAndGet(SEED_INCREMENT);
6161 >            h = hc.code = (s == 0) ? 1 : s; // Avoid zero
6162 >            threadCounterHashCode.set(hc);
6163 >        }
6164 >        else
6165 >            h = hc.code;
6166 >        boolean collide = false;                // True if last slot nonempty
6167 >        for (;;) {
6168 >            CounterCell[] as; CounterCell a; int n; long v;
6169 >            if ((as = counterCells) != null && (n = as.length) > 0) {
6170 >                if ((a = as[(n - 1) & h]) == null) {
6171 >                    if (cellsBusy == 0) {            // Try to attach new Cell
6172 >                        CounterCell r = new CounterCell(x); // Optimistic create
6173 >                        if (cellsBusy == 0 &&
6174 >                            U.compareAndSwapInt(this, CELLSBUSY, 0, 1)) {
6175 >                            boolean created = false;
6176 >                            try {               // Recheck under lock
6177 >                                CounterCell[] rs; int m, j;
6178 >                                if ((rs = counterCells) != null &&
6179 >                                    (m = rs.length) > 0 &&
6180 >                                    rs[j = (m - 1) & h] == null) {
6181 >                                    rs[j] = r;
6182 >                                    created = true;
6183 >                                }
6184 >                            } finally {
6185 >                                cellsBusy = 0;
6186 >                            }
6187 >                            if (created)
6188 >                                break;
6189 >                            continue;           // Slot is now non-empty
6190 >                        }
6191 >                    }
6192 >                    collide = false;
6193 >                }
6194 >                else if (!wasUncontended)       // CAS already known to fail
6195 >                    wasUncontended = true;      // Continue after rehash
6196 >                else if (U.compareAndSwapLong(a, CELLVALUE, v = a.value, v + x))
6197 >                    break;
6198 >                else if (counterCells != as || n >= NCPU)
6199 >                    collide = false;            // At max size or stale
6200 >                else if (!collide)
6201 >                    collide = true;
6202 >                else if (cellsBusy == 0 &&
6203 >                         U.compareAndSwapInt(this, CELLSBUSY, 0, 1)) {
6204 >                    try {
6205 >                        if (counterCells == as) {// Expand table unless stale
6206 >                            CounterCell[] rs = new CounterCell[n << 1];
6207 >                            for (int i = 0; i < n; ++i)
6208 >                                rs[i] = as[i];
6209 >                            counterCells = rs;
6210 >                        }
6211 >                    } finally {
6212 >                        cellsBusy = 0;
6213 >                    }
6214 >                    collide = false;
6215 >                    continue;                   // Retry with expanded table
6216 >                }
6217 >                h ^= h << 13;                   // Rehash
6218 >                h ^= h >>> 17;
6219 >                h ^= h << 5;
6220 >            }
6221 >            else if (cellsBusy == 0 && counterCells == as &&
6222 >                     U.compareAndSwapInt(this, CELLSBUSY, 0, 1)) {
6223 >                boolean init = false;
6224 >                try {                           // Initialize table
6225 >                    if (counterCells == as) {
6226 >                        CounterCell[] rs = new CounterCell[2];
6227 >                        rs[h & 1] = new CounterCell(x);
6228 >                        counterCells = rs;
6229 >                        init = true;
6230 >                    }
6231 >                } finally {
6232 >                    cellsBusy = 0;
6233                  }
6234 +                if (init)
6235 +                    break;
6236              }
6237 +            else if (U.compareAndSwapLong(this, BASECOUNT, v = baseCount, v + x))
6238 +                break;                          // Fall back on using base
6239          }
6240 +        hc.code = h;                            // Record index for next time
6241      }
6242  
6243      // Unsafe mechanics
6244 <    private static final sun.misc.Unsafe UNSAFE;
6245 <    private static final long counterOffset;
6246 <    private static final long sizeCtlOffset;
6244 >    private static final sun.misc.Unsafe U;
6245 >    private static final long SIZECTL;
6246 >    private static final long TRANSFERINDEX;
6247 >    private static final long BASECOUNT;
6248 >    private static final long CELLSBUSY;
6249 >    private static final long CELLVALUE;
6250      private static final long ABASE;
6251      private static final int ASHIFT;
6252  
6253      static {
6621        int ss;
6254          try {
6255 <            UNSAFE = getUnsafe();
6255 >            U = getUnsafe();
6256              Class<?> k = ConcurrentHashMapV8.class;
6257 <            counterOffset = UNSAFE.objectFieldOffset
6626 <                (k.getDeclaredField("counter"));
6627 <            sizeCtlOffset = UNSAFE.objectFieldOffset
6257 >            SIZECTL = U.objectFieldOffset
6258                  (k.getDeclaredField("sizeCtl"));
6259 <            Class<?> sc = Node[].class;
6260 <            ABASE = UNSAFE.arrayBaseOffset(sc);
6261 <            ss = UNSAFE.arrayIndexScale(sc);
6259 >            TRANSFERINDEX = U.objectFieldOffset
6260 >                (k.getDeclaredField("transferIndex"));
6261 >            BASECOUNT = U.objectFieldOffset
6262 >                (k.getDeclaredField("baseCount"));
6263 >            CELLSBUSY = U.objectFieldOffset
6264 >                (k.getDeclaredField("cellsBusy"));
6265 >            Class<?> ck = CounterCell.class;
6266 >            CELLVALUE = U.objectFieldOffset
6267 >                (ck.getDeclaredField("value"));
6268 >            Class<?> ak = Node[].class;
6269 >            ABASE = U.arrayBaseOffset(ak);
6270 >            int scale = U.arrayIndexScale(ak);
6271 >            if ((scale & (scale - 1)) != 0)
6272 >                throw new Error("data type scale not a power of two");
6273 >            ASHIFT = 31 - Integer.numberOfLeadingZeros(scale);
6274          } catch (Exception e) {
6275              throw new Error(e);
6276          }
6635        if ((ss & (ss-1)) != 0)
6636            throw new Error("data type scale not a power of two");
6637        ASHIFT = 31 - Integer.numberOfLeadingZeros(ss);
6277      }
6278  
6279      /**
# Line 6647 | Line 6286 | public class ConcurrentHashMapV8<K, V>
6286      private static sun.misc.Unsafe getUnsafe() {
6287          try {
6288              return sun.misc.Unsafe.getUnsafe();
6289 <        } catch (SecurityException se) {
6290 <            try {
6291 <                return java.security.AccessController.doPrivileged
6292 <                    (new java.security
6293 <                     .PrivilegedExceptionAction<sun.misc.Unsafe>() {
6294 <                        public sun.misc.Unsafe run() throws Exception {
6295 <                            java.lang.reflect.Field f = sun.misc
6296 <                                .Unsafe.class.getDeclaredField("theUnsafe");
6297 <                            f.setAccessible(true);
6298 <                            return (sun.misc.Unsafe) f.get(null);
6299 <                        }});
6300 <            } catch (java.security.PrivilegedActionException e) {
6301 <                throw new RuntimeException("Could not initialize intrinsics",
6302 <                                           e.getCause());
6303 <            }
6289 >        } catch (SecurityException tryReflectionInstead) {}
6290 >        try {
6291 >            return java.security.AccessController.doPrivileged
6292 >            (new java.security.PrivilegedExceptionAction<sun.misc.Unsafe>() {
6293 >                public sun.misc.Unsafe run() throws Exception {
6294 >                    Class<sun.misc.Unsafe> k = sun.misc.Unsafe.class;
6295 >                    for (java.lang.reflect.Field f : k.getDeclaredFields()) {
6296 >                        f.setAccessible(true);
6297 >                        Object x = f.get(null);
6298 >                        if (k.isInstance(x))
6299 >                            return k.cast(x);
6300 >                    }
6301 >                    throw new NoSuchFieldError("the Unsafe");
6302 >                }});
6303 >        } catch (java.security.PrivilegedActionException e) {
6304 >            throw new RuntimeException("Could not initialize intrinsics",
6305 >                                       e.getCause());
6306          }
6307      }
6308   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines