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.52 by dl, Mon Aug 13 15:52:33 2012 UTC vs.
Revision 1.88 by jsr166, Fri Jan 18 04:23:27 2013 UTC

# Line 5 | Line 5
5   */
6  
7   package jsr166e;
8 import jsr166e.LongAdder;
9 import jsr166e.ForkJoinPool;
10 import jsr166e.ForkJoinTask;
8  
9   import java.util.Comparator;
10   import java.util.Arrays;
# Line 24 | Line 21 | import java.util.Enumeration;
21   import java.util.ConcurrentModificationException;
22   import java.util.NoSuchElementException;
23   import java.util.concurrent.ConcurrentMap;
27 import java.util.concurrent.ThreadLocalRandom;
28 import java.util.concurrent.locks.LockSupport;
24   import java.util.concurrent.locks.AbstractQueuedSynchronizer;
25 + import java.util.concurrent.atomic.AtomicInteger;
26   import java.util.concurrent.atomic.AtomicReference;
31
27   import java.io.Serializable;
28  
29   /**
# Line 43 | Line 38 | import java.io.Serializable;
38   * interoperable with {@code Hashtable} in programs that rely on its
39   * thread safety but not on its synchronization details.
40   *
41 < * <p> Retrieval operations (including {@code get}) generally do not
41 > * <p>Retrieval operations (including {@code get}) generally do not
42   * block, so may overlap with update operations (including {@code put}
43   * and {@code remove}). Retrievals reflect the results of the most
44   * recently <em>completed</em> update operations holding upon their
45 < * onset.  For aggregate operations such as {@code putAll} and {@code
46 < * clear}, concurrent retrievals may reflect insertion or removal of
47 < * only some entries.  Similarly, Iterators and Enumerations return
48 < * elements reflecting the state of the hash table at some point at or
49 < * since the creation of the iterator/enumeration.  They do
50 < * <em>not</em> throw {@link ConcurrentModificationException}.
51 < * However, iterators are designed to be used by only one thread at a
52 < * time.  Bear in mind that the results of aggregate status methods
53 < * including {@code size}, {@code isEmpty}, and {@code containsValue}
54 < * are typically useful only when a map is not undergoing concurrent
55 < * updates in other threads.  Otherwise the results of these methods
56 < * reflect transient states that may be adequate for monitoring
57 < * or estimation purposes, but not for program control.
45 > * onset. (More formally, an update operation for a given key bears a
46 > * <em>happens-before</em> relation with any (non-null) retrieval for
47 > * that key reporting the updated value.)  For aggregate operations
48 > * such as {@code putAll} and {@code clear}, concurrent retrievals may
49 > * reflect insertion or removal of only some entries.  Similarly,
50 > * Iterators and Enumerations return elements reflecting the state of
51 > * the hash table at some point at or since the creation of the
52 > * iterator/enumeration.  They do <em>not</em> throw {@link
53 > * ConcurrentModificationException}.  However, iterators are designed
54 > * to be used by only one thread at a time.  Bear in mind that the
55 > * results of aggregate status methods including {@code size}, {@code
56 > * isEmpty}, and {@code containsValue} are typically useful only when
57 > * a map is not undergoing concurrent updates in other threads.
58 > * Otherwise the results of these methods reflect transient states
59 > * that may be adequate for monitoring or estimation purposes, but not
60 > * for program control.
61   *
62 < * <p> The table is dynamically expanded when there are too many
62 > * <p>The table is dynamically expanded when there are too many
63   * collisions (i.e., keys that have distinct hash codes but fall into
64   * the same slot modulo the table size), with the expected average
65   * effect of maintaining roughly two bins per mapping (corresponding
# Line 82 | Line 80 | import java.io.Serializable;
80   * {@code hashCode()} is a sure way to slow down performance of any
81   * hash table.
82   *
83 + * <p>A {@link Set} projection of a ConcurrentHashMapV8 may be created
84 + * (using {@link #newKeySet()} or {@link #newKeySet(int)}), or viewed
85 + * (using {@link #keySet(Object)} when only keys are of interest, and the
86 + * mapped values are (perhaps transiently) not used or all take the
87 + * same mapping value.
88 + *
89 + * <p>A ConcurrentHashMapV8 can be used as scalable frequency map (a
90 + * form of histogram or multiset) by using {@link LongAdder} values
91 + * and initializing via {@link #computeIfAbsent}. For example, to add
92 + * a count to a {@code ConcurrentHashMapV8<String,LongAdder> freqs}, you
93 + * can use {@code freqs.computeIfAbsent(k -> new
94 + * LongAdder()).increment();}
95 + *
96   * <p>This class and its views and iterators implement all of the
97   * <em>optional</em> methods of the {@link Map} and {@link Iterator}
98   * interfaces.
99   *
100 < * <p> Like {@link Hashtable} but unlike {@link HashMap}, this class
100 > * <p>Like {@link Hashtable} but unlike {@link HashMap}, this class
101   * does <em>not</em> allow {@code null} to be used as a key or value.
102   *
103 + * <p>ConcurrentHashMapV8s support sequential and parallel operations
104 + * bulk operations. (Parallel forms use the {@link
105 + * ForkJoinPool#commonPool()}). Tasks that may be used in other
106 + * contexts are available in class {@link ForkJoinTasks}. These
107 + * operations are designed to be safely, and often sensibly, applied
108 + * even with maps that are being concurrently updated by other
109 + * threads; for example, when computing a snapshot summary of the
110 + * values in a shared registry.  There are three kinds of operation,
111 + * each with four forms, accepting functions with Keys, Values,
112 + * Entries, and (Key, Value) arguments and/or return values. Because
113 + * the elements of a ConcurrentHashMapV8 are not ordered in any
114 + * particular way, and may be processed in different orders in
115 + * different parallel executions, the correctness of supplied
116 + * functions should not depend on any ordering, or on any other
117 + * objects or values that may transiently change while computation is
118 + * in progress; and except for forEach actions, should ideally be
119 + * side-effect-free.
120 + *
121 + * <ul>
122 + * <li> forEach: Perform a given action on each element.
123 + * A variant form applies a given transformation on each element
124 + * before performing the action.</li>
125 + *
126 + * <li> search: Return the first available non-null result of
127 + * applying a given function on each element; skipping further
128 + * search when a result is found.</li>
129 + *
130 + * <li> reduce: Accumulate each element.  The supplied reduction
131 + * function cannot rely on ordering (more formally, it should be
132 + * both associative and commutative).  There are five variants:
133 + *
134 + * <ul>
135 + *
136 + * <li> Plain reductions. (There is not a form of this method for
137 + * (key, value) function arguments since there is no corresponding
138 + * return type.)</li>
139 + *
140 + * <li> Mapped reductions that accumulate the results of a given
141 + * function applied to each element.</li>
142 + *
143 + * <li> Reductions to scalar doubles, longs, and ints, using a
144 + * given basis value.</li>
145 + *
146 + * </li>
147 + * </ul>
148 + * </ul>
149 + *
150 + * <p>The concurrency properties of bulk operations follow
151 + * from those of ConcurrentHashMapV8: Any non-null result returned
152 + * from {@code get(key)} and related access methods bears a
153 + * happens-before relation with the associated insertion or
154 + * update.  The result of any bulk operation reflects the
155 + * composition of these per-element relations (but is not
156 + * necessarily atomic with respect to the map as a whole unless it
157 + * is somehow known to be quiescent).  Conversely, because keys
158 + * and values in the map are never null, null serves as a reliable
159 + * atomic indicator of the current lack of any result.  To
160 + * maintain this property, null serves as an implicit basis for
161 + * all non-scalar reduction operations. For the double, long, and
162 + * int versions, the basis should be one that, when combined with
163 + * any other value, returns that other value (more formally, it
164 + * should be the identity element for the reduction). Most common
165 + * reductions have these properties; for example, computing a sum
166 + * with basis 0 or a minimum with basis MAX_VALUE.
167 + *
168 + * <p>Search and transformation functions provided as arguments
169 + * should similarly return null to indicate the lack of any result
170 + * (in which case it is not used). In the case of mapped
171 + * reductions, this also enables transformations to serve as
172 + * filters, returning null (or, in the case of primitive
173 + * specializations, the identity basis) if the element should not
174 + * be combined. You can create compound transformations and
175 + * filterings by composing them yourself under this "null means
176 + * there is nothing there now" rule before using them in search or
177 + * reduce operations.
178 + *
179 + * <p>Methods accepting and/or returning Entry arguments maintain
180 + * key-value associations. They may be useful for example when
181 + * finding the key for the greatest value. Note that "plain" Entry
182 + * arguments can be supplied using {@code new
183 + * AbstractMap.SimpleEntry(k,v)}.
184 + *
185 + * <p>Bulk operations may complete abruptly, throwing an
186 + * exception encountered in the application of a supplied
187 + * function. Bear in mind when handling such exceptions that other
188 + * concurrently executing functions could also have thrown
189 + * exceptions, or would have done so if the first exception had
190 + * not occurred.
191 + *
192 + * <p>Speedups for parallel compared to sequential forms are common
193 + * but not guaranteed.  Parallel operations involving brief functions
194 + * on small maps may execute more slowly than sequential forms if the
195 + * underlying work to parallelize the computation is more expensive
196 + * than the computation itself.  Similarly, parallelization may not
197 + * lead to much actual parallelism if all processors are busy
198 + * performing unrelated tasks.
199 + *
200 + * <p>All arguments to all task methods must be non-null.
201 + *
202 + * <p><em>jsr166e note: During transition, this class
203 + * uses nested functional interfaces with different names but the
204 + * same forms as those expected for JDK8.</em>
205 + *
206   * <p>This class is a member of the
207   * <a href="{@docRoot}/../technotes/guides/collections/index.html">
208   * Java Collections Framework</a>.
209   *
96 * <p><em>jsr166e note: This class is a candidate replacement for
97 * java.util.concurrent.ConcurrentHashMap.  During transition, this
98 * class declares and uses nested functional interfaces with different
99 * names but the same forms as those expected for JDK8.<em>
100 *
210   * @since 1.5
211   * @author Doug Lea
212   * @param <K> the type of keys maintained by this map
# Line 114 | Line 223 | public class ConcurrentHashMapV8<K, V>
223       * portion of the elements, and so may be amenable to parallel
224       * execution.
225       *
226 <     * <p> This interface exports a subset of expected JDK8
226 >     * <p>This interface exports a subset of expected JDK8
227       * functionality.
228       *
229       * <p>Sample usage: Here is one (of the several) ways to compute
# Line 186 | Line 295 | public class ConcurrentHashMapV8<K, V>
295       * the same or better than java.util.HashMap, and to support high
296       * initial insertion rates on an empty table by many threads.
297       *
298 <     * Each key-value mapping is held in a Node.  Because Node fields
299 <     * can contain special values, they are defined using plain Object
300 <     * types. Similarly in turn, all internal methods that use them
301 <     * work off Object types. And similarly, so do the internal
302 <     * methods of auxiliary iterator and view classes.  All public
303 <     * generic typed methods relay in/out of these internal methods,
304 <     * supplying null-checks and casts as needed. This also allows
305 <     * many of the public methods to be factored into a smaller number
306 <     * of internal methods (although sadly not so for the five
198 <     * variants of put-related operations). The validation-based
199 <     * approach explained below leads to a lot of code sprawl because
298 >     * Each key-value mapping is held in a Node.  Because Node key
299 >     * fields can contain special values, they are defined using plain
300 >     * Object types (not type "K"). This leads to a lot of explicit
301 >     * casting (and many explicit warning suppressions to tell
302 >     * compilers not to complain about it). It also allows some of the
303 >     * public methods to be factored into a smaller number of internal
304 >     * methods (although sadly not so for the five variants of
305 >     * put-related operations). The validation-based approach
306 >     * explained below leads to a lot of code sprawl because
307       * retry-control precludes factoring into smaller methods.
308       *
309       * The table is lazily initialized to a power-of-two size upon the
# Line 210 | Line 317 | public class ConcurrentHashMapV8<K, V>
317       * as lookups check hash code and non-nullness of value before
318       * checking key equality.
319       *
320 <     * We use the top two bits of Node hash fields for control
321 <     * purposes -- they are available anyway because of addressing
322 <     * constraints.  As explained further below, these top bits are
323 <     * used as follows:
324 <     *  00 - Normal
325 <     *  01 - Locked
219 <     *  11 - Locked and may have a thread waiting for lock
220 <     *  10 - Node is a forwarding node
221 <     *
222 <     * The lower 30 bits of each Node's hash field contain a
223 <     * transformation of the key's hash code, except for forwarding
224 <     * nodes, for which the lower bits are zero (and so always have
225 <     * hash field == MOVED).
320 >     * We use the top (sign) bit of Node hash fields for control
321 >     * purposes -- it is available anyway because of addressing
322 >     * constraints.  Nodes with negative hash fields are forwarding
323 >     * nodes to either TreeBins or resized tables.  The lower 31 bits
324 >     * of each normal Node's hash field contain a transformation of
325 >     * the key's hash code.
326       *
327       * Insertion (via put or its variants) of the first node in an
328       * empty bin is performed by just CASing it to the bin.  This is
# Line 231 | Line 331 | public class ConcurrentHashMapV8<K, V>
331       * delete, and replace) require locks.  We do not want to waste
332       * the space required to associate a distinct lock object with
333       * each bin, so instead use the first node of a bin list itself as
334 <     * a lock. Blocking support for these locks relies on the builtin
335 <     * "synchronized" monitors.  However, we also need a tryLock
236 <     * construction, so we overlay these by using bits of the Node
237 <     * hash field for lock control (see above), and so normally use
238 <     * builtin monitors only for blocking and signalling using
239 <     * wait/notifyAll constructions. See Node.tryAwaitLock.
334 >     * a lock. Locking support for these locks relies on builtin
335 >     * "synchronized" monitors.
336       *
337       * Using the first node of a list as a lock does not by itself
338       * suffice though: When a node is locked, any update must first
# Line 298 | Line 394 | public class ConcurrentHashMapV8<K, V>
394       * iterators in the same way.
395       *
396       * The table is resized when occupancy exceeds a percentage
397 <     * threshold (nominally, 0.75, but see below).  Only a single
398 <     * thread performs the resize (using field "sizeCtl", to arrange
399 <     * exclusion), but the table otherwise remains usable for reads
400 <     * and updates. Resizing proceeds by transferring bins, one by
401 <     * one, from the table to the next table.  Because we are using
402 <     * power-of-two expansion, the elements from each bin must either
403 <     * stay at same index, or move with a power of two offset. We
404 <     * eliminate unnecessary node creation by catching cases where old
405 <     * nodes can be reused because their next fields won't change.  On
406 <     * average, only about one-sixth of them need cloning when a table
407 <     * doubles. The nodes they replace will be garbage collectable as
408 <     * soon as they are no longer referenced by any reader thread that
409 <     * may be in the midst of concurrently traversing table.  Upon
410 <     * transfer, the old table bin contains only a special forwarding
411 <     * node (with hash field "MOVED") that contains the next table as
412 <     * its key. On encountering a forwarding node, access and update
413 <     * operations restart, using the new table.
414 <     *
415 <     * Each bin transfer requires its bin lock. However, unlike other
416 <     * cases, a transfer can skip a bin if it fails to acquire its
417 <     * lock, and revisit it later (unless it is a TreeBin). Method
418 <     * rebuild maintains a buffer of TRANSFER_BUFFER_SIZE bins that
419 <     * have been skipped because of failure to acquire a lock, and
420 <     * blocks only if none are available (i.e., only very rarely).
421 <     * The transfer operation must also ensure that all accessible
422 <     * bins in both the old and new table are usable by any traversal.
423 <     * When there are no lock acquisition failures, this is arranged
424 <     * simply by proceeding from the last bin (table.length - 1) up
425 <     * towards the first.  Upon seeing a forwarding node, traversals
426 <     * (see class Iter) arrange to move to the new table
427 <     * without revisiting nodes.  However, when any node is skipped
428 <     * during a transfer, all earlier table bins may have become
429 <     * visible, so are initialized with a reverse-forwarding node back
430 <     * to the old table until the new ones are established. (This
431 <     * sometimes requires transiently locking a forwarding node, which
432 <     * is possible under the above encoding.) These more expensive
433 <     * mechanics trigger only when necessary.
397 >     * threshold (nominally, 0.75, but see below).  Any thread
398 >     * noticing an overfull bin may assist in resizing after the
399 >     * initiating thread allocates and sets up the replacement
400 >     * array. However, rather than stalling, these other threads may
401 >     * proceed with insertions etc.  The use of TreeBins shields us
402 >     * from the worst case effects of overfilling while resizes are in
403 >     * progress.  Resizing proceeds by transferring bins, one by one,
404 >     * from the table to the next table. To enable concurrency, the
405 >     * next table must be (incrementally) prefilled with place-holders
406 >     * serving as reverse forwarders to the old table.  Because we are
407 >     * using power-of-two expansion, the elements from each bin must
408 >     * either stay at same index, or move with a power of two
409 >     * offset. We eliminate unnecessary node creation by catching
410 >     * cases where old nodes can be reused because their next fields
411 >     * won't change.  On average, only about one-sixth of them need
412 >     * cloning when a table doubles. The nodes they replace will be
413 >     * garbage collectable as soon as they are no longer referenced by
414 >     * any reader thread that may be in the midst of concurrently
415 >     * traversing table.  Upon transfer, the old table bin contains
416 >     * only a special forwarding node (with hash field "MOVED") that
417 >     * contains the next table as its key. On encountering a
418 >     * forwarding node, access and update operations restart, using
419 >     * the new table.
420 >     *
421 >     * Each bin transfer requires its bin lock, which can stall
422 >     * waiting for locks while resizing. However, because other
423 >     * threads can join in and help resize rather than contend for
424 >     * locks, average aggregate waits become shorter as resizing
425 >     * progresses.  The transfer operation must also ensure that all
426 >     * accessible bins in both the old and new table are usable by any
427 >     * traversal.  This is arranged by proceeding from the last bin
428 >     * (table.length - 1) up towards the first.  Upon seeing a
429 >     * forwarding node, traversals (see class Traverser) arrange to
430 >     * move to the new table without revisiting nodes.  However, to
431 >     * ensure that no intervening nodes are skipped, bin splitting can
432 >     * only begin after the associated reverse-forwarders are in
433 >     * place.
434       *
435       * The traversal scheme also applies to partial traversals of
436       * ranges of bins (via an alternate Traverser constructor)
# Line 349 | Line 445 | public class ConcurrentHashMapV8<K, V>
445       * These cases attempt to override the initial capacity settings,
446       * but harmlessly fail to take effect in cases of races.
447       *
448 <     * The element count is maintained using a LongAdder, which avoids
449 <     * contention on updates but can encounter cache thrashing if read
450 <     * too frequently during concurrent access. To avoid reading so
451 <     * often, resizing is attempted either when a bin lock is
452 <     * contended, or upon adding to a bin already holding two or more
453 <     * nodes (checked before adding in the xIfAbsent methods, after
454 <     * adding in others). Under uniform hash distributions, the
455 <     * probability of this occurring at threshold is around 13%,
456 <     * meaning that only about 1 in 8 puts check threshold (and after
457 <     * resizing, many fewer do so). But this approximation has high
458 <     * variance for small table sizes, so we check on any collision
459 <     * for sizes <= 64. The bulk putAll operation further reduces
460 <     * contention by only committing count updates upon these size
461 <     * checks.
448 >     * The element count is maintained using a specialization of
449 >     * LongAdder. We need to incorporate a specialization rather than
450 >     * just use a LongAdder in order to access implicit
451 >     * contention-sensing that leads to creation of multiple
452 >     * CounterCells.  The counter mechanics avoid contention on
453 >     * updates but can encounter cache thrashing if read too
454 >     * frequently during concurrent access. To avoid reading so often,
455 >     * resizing under contention is attempted only upon adding to a
456 >     * bin already holding two or more nodes. Under uniform hash
457 >     * distributions, the probability of this occurring at threshold
458 >     * is around 13%, meaning that only about 1 in 8 puts check
459 >     * threshold (and after resizing, many fewer do so). The bulk
460 >     * putAll operation further reduces contention by only committing
461 >     * count updates upon these size checks.
462       *
463       * Maintaining API and serialization compatibility with previous
464       * versions of this class introduces several oddities. Mainly: We
# Line 413 | Line 509 | public class ConcurrentHashMapV8<K, V>
509      private static final float LOAD_FACTOR = 0.75f;
510  
511      /**
416     * The buffer size for skipped bins during transfers. The
417     * value is arbitrary but should be large enough to avoid
418     * most locking stalls during resizes.
419     */
420    private static final int TRANSFER_BUFFER_SIZE = 32;
421
422    /**
512       * The bin count threshold for using a tree rather than list for a
513       * bin.  The value reflects the approximate break-even point for
514       * using tree-based operations.
515       */
516      private static final int TREE_THRESHOLD = 8;
517  
518 +    /**
519 +     * Minimum number of rebinnings per transfer step. Ranges are
520 +     * subdivided to allow multiple resizer threads.  This value
521 +     * serves as a lower bound to avoid resizers encountering
522 +     * excessive memory contention.  The value should be at least
523 +     * DEFAULT_CAPACITY.
524 +     */
525 +    private static final int MIN_TRANSFER_STRIDE = 16;
526 +
527      /*
528 <     * Encodings for special uses of Node hash fields. See above for
431 <     * explanation.
528 >     * Encodings for Node hash fields. See above for explanation.
529       */
530      static final int MOVED     = 0x80000000; // hash field for forwarding nodes
531 <    static final int LOCKED    = 0x40000000; // set/tested only as a bit
532 <    static final int WAITING   = 0xc0000000; // both bits set/tested together
533 <    static final int HASH_BITS = 0x3fffffff; // usable bits of normal node hash
531 >    static final int HASH_BITS = 0x7fffffff; // usable bits of normal node hash
532 >
533 >    /** Number of CPUS, to place bounds on some sizings */
534 >    static final int NCPU = Runtime.getRuntime().availableProcessors();
535 >
536 >    /* ---------------- Counters -------------- */
537 >
538 >    // Adapted from LongAdder and Striped64.
539 >    // See their internal docs for explanation.
540 >
541 >    // A padded cell for distributing counts
542 >    static final class CounterCell {
543 >        volatile long p0, p1, p2, p3, p4, p5, p6;
544 >        volatile long value;
545 >        volatile long q0, q1, q2, q3, q4, q5, q6;
546 >        CounterCell(long x) { value = x; }
547 >    }
548 >
549 >    /**
550 >     * Holder for the thread-local hash code determining which
551 >     * CounterCell to use. The code is initialized via the
552 >     * counterHashCodeGenerator, but may be moved upon collisions.
553 >     */
554 >    static final class CounterHashCode {
555 >        int code;
556 >    }
557 >
558 >    /**
559 >     * Generates initial value for per-thread CounterHashCodes
560 >     */
561 >    static final AtomicInteger counterHashCodeGenerator = new AtomicInteger();
562 >
563 >    /**
564 >     * Increment for counterHashCodeGenerator. See class ThreadLocal
565 >     * for explanation.
566 >     */
567 >    static final int SEED_INCREMENT = 0x61c88647;
568 >
569 >    /**
570 >     * Per-thread counter hash codes. Shared across all instances.
571 >     */
572 >    static final ThreadLocal<CounterHashCode> threadCounterHashCode =
573 >        new ThreadLocal<CounterHashCode>();
574  
575      /* ---------------- Fields -------------- */
576  
# Line 441 | Line 578 | public class ConcurrentHashMapV8<K, V>
578       * The array of bins. Lazily initialized upon first insertion.
579       * Size is always a power of two. Accessed directly by iterators.
580       */
581 <    transient volatile Node[] table;
581 >    transient volatile Node<V>[] table;
582  
583      /**
584 <     * The counter maintaining number of elements.
584 >     * The next table to use; non-null only while resizing.
585       */
586 <    private transient final LongAdder counter;
586 >    private transient volatile Node<V>[] nextTable;
587 >
588 >    /**
589 >     * Base counter value, used mainly when there is no contention,
590 >     * but also as a fallback during table initialization
591 >     * races. Updated via CAS.
592 >     */
593 >    private transient volatile long baseCount;
594  
595      /**
596       * Table initialization and resizing control.  When negative, the
597 <     * table is being initialized or resized. Otherwise, when table is
598 <     * null, holds the initial table size to use upon creation, or 0
599 <     * for default. After initialization, holds the next element count
600 <     * value upon which to resize the table.
597 >     * table is being initialized or resized: -1 for initialization,
598 >     * else -(1 + the number of active resizing threads).  Otherwise,
599 >     * when table is null, holds the initial table size to use upon
600 >     * creation, or 0 for default. After initialization, holds the
601 >     * next element count value upon which to resize the table.
602       */
603      private transient volatile int sizeCtl;
604  
605 +    /**
606 +     * The next table index (plus one) to split while resizing.
607 +     */
608 +    private transient volatile int transferIndex;
609 +
610 +    /**
611 +     * The least available table index to split while resizing.
612 +     */
613 +    private transient volatile int transferOrigin;
614 +
615 +    /**
616 +     * Spinlock (locked via CAS) used when resizing and/or creating Cells.
617 +     */
618 +    private transient volatile int counterBusy;
619 +
620 +    /**
621 +     * Table of counter cells. When non-null, size is a power of 2.
622 +     */
623 +    private transient volatile CounterCell[] counterCells;
624 +
625      // views
626 <    private transient KeySet<K,V> keySet;
627 <    private transient Values<K,V> values;
628 <    private transient EntrySet<K,V> entrySet;
626 >    private transient KeySetView<K,V> keySet;
627 >    private transient ValuesView<K,V> values;
628 >    private transient EntrySetView<K,V> entrySet;
629  
630      /** For serialization compatibility. Null unless serialized; see below */
631      private Segment<K,V>[] segments;
# Line 479 | Line 644 | public class ConcurrentHashMapV8<K, V>
644       * inline assignments below.
645       */
646  
647 <    static final Node tabAt(Node[] tab, int i) { // used by Iter
648 <        return (Node)UNSAFE.getObjectVolatile(tab, ((long)i<<ASHIFT)+ABASE);
647 >    @SuppressWarnings("unchecked") static final <V> Node<V> tabAt
648 >        (Node<V>[] tab, int i) { // used by Traverser
649 >        return (Node<V>)U.getObjectVolatile(tab, ((long)i << ASHIFT) + ABASE);
650      }
651  
652 <    private static final boolean casTabAt(Node[] tab, int i, Node c, Node v) {
653 <        return UNSAFE.compareAndSwapObject(tab, ((long)i<<ASHIFT)+ABASE, c, v);
652 >    private static final <V> boolean casTabAt
653 >        (Node<V>[] tab, int i, Node<V> c, Node<V> v) {
654 >        return U.compareAndSwapObject(tab, ((long)i << ASHIFT) + ABASE, c, v);
655      }
656  
657 <    private static final void setTabAt(Node[] tab, int i, Node v) {
658 <        UNSAFE.putObjectVolatile(tab, ((long)i<<ASHIFT)+ABASE, v);
657 >    private static final <V> void setTabAt
658 >        (Node<V>[] tab, int i, Node<V> v) {
659 >        U.putObjectVolatile(tab, ((long)i << ASHIFT) + ABASE, v);
660      }
661  
662      /* ---------------- Nodes -------------- */
# Line 503 | Line 671 | public class ConcurrentHashMapV8<K, V>
671       * before a val, but can only be used after checking val to be
672       * non-null.
673       */
674 <    static class Node {
675 <        volatile int hash;
674 >    static class Node<V> {
675 >        final int hash;
676          final Object key;
677 <        volatile Object val;
678 <        volatile Node next;
677 >        volatile V val;
678 >        volatile Node<V> next;
679  
680 <        Node(int hash, Object key, Object val, Node next) {
680 >        Node(int hash, Object key, V val, Node<V> next) {
681              this.hash = hash;
682              this.key = key;
683              this.val = val;
684              this.next = next;
685          }
518
519        /** CompareAndSet the hash field */
520        final boolean casHash(int cmp, int val) {
521            return UNSAFE.compareAndSwapInt(this, hashOffset, cmp, val);
522        }
523
524        /** The number of spins before blocking for a lock */
525        static final int MAX_SPINS =
526            Runtime.getRuntime().availableProcessors() > 1 ? 64 : 1;
527
528        /**
529         * Spins a while if LOCKED bit set and this node is the first
530         * of its bin, and then sets WAITING bits on hash field and
531         * blocks (once) if they are still set.  It is OK for this
532         * method to return even if lock is not available upon exit,
533         * which enables these simple single-wait mechanics.
534         *
535         * The corresponding signalling operation is performed within
536         * callers: Upon detecting that WAITING has been set when
537         * unlocking lock (via a failed CAS from non-waiting LOCKED
538         * state), unlockers acquire the sync lock and perform a
539         * notifyAll.
540         */
541        final void tryAwaitLock(Node[] tab, int i) {
542            if (tab != null && i >= 0 && i < tab.length) { // bounds check
543                int r = ThreadLocalRandom.current().nextInt(); // randomize spins
544                int spins = MAX_SPINS, h;
545                while (tabAt(tab, i) == this && ((h = hash) & LOCKED) != 0) {
546                    if (spins >= 0) {
547                        r ^= r << 1; r ^= r >>> 3; r ^= r << 10; // xorshift
548                        if (r >= 0 && --spins == 0)
549                            Thread.yield();  // yield before block
550                    }
551                    else if (casHash(h, h | WAITING)) {
552                        synchronized (this) {
553                            if (tabAt(tab, i) == this &&
554                                (hash & WAITING) == WAITING) {
555                                try {
556                                    wait();
557                                } catch (InterruptedException ie) {
558                                    Thread.currentThread().interrupt();
559                                }
560                            }
561                            else
562                                notifyAll(); // possibly won race vs signaller
563                        }
564                        break;
565                    }
566                }
567            }
568        }
569
570        // Unsafe mechanics for casHash
571        private static final sun.misc.Unsafe UNSAFE;
572        private static final long hashOffset;
573
574        static {
575            try {
576                UNSAFE = getUnsafe();
577                Class<?> k = Node.class;
578                hashOffset = UNSAFE.objectFieldOffset
579                    (k.getDeclaredField("hash"));
580            } catch (Exception e) {
581                throw new Error(e);
582            }
583        }
686      }
687  
688      /* ---------------- TreeBins -------------- */
# Line 588 | Line 690 | public class ConcurrentHashMapV8<K, V>
690      /**
691       * Nodes for use in TreeBins
692       */
693 <    static final class TreeNode extends Node {
694 <        TreeNode parent;  // red-black tree links
695 <        TreeNode left;
696 <        TreeNode right;
697 <        TreeNode prev;    // needed to unlink next upon deletion
693 >    static final class TreeNode<V> extends Node<V> {
694 >        TreeNode<V> parent;  // red-black tree links
695 >        TreeNode<V> left;
696 >        TreeNode<V> right;
697 >        TreeNode<V> prev;    // needed to unlink next upon deletion
698          boolean red;
699  
700 <        TreeNode(int hash, Object key, Object val, Node next, TreeNode parent) {
700 >        TreeNode(int hash, Object key, V val, Node<V> next, TreeNode<V> parent) {
701              super(hash, key, val, next);
702              this.parent = parent;
703          }
# Line 644 | Line 746 | public class ConcurrentHashMapV8<K, V>
746       * and writers. Since we don't need to export full Lock API, we
747       * just override the minimal AQS methods and use them directly.
748       */
749 <    static final class TreeBin extends AbstractQueuedSynchronizer {
749 >    static final class TreeBin<V> extends AbstractQueuedSynchronizer {
750          private static final long serialVersionUID = 2249069246763182397L;
751 <        transient TreeNode root;  // root of tree
752 <        transient TreeNode first; // head of next-pointer list
751 >        transient TreeNode<V> root;  // root of tree
752 >        transient TreeNode<V> first; // head of next-pointer list
753  
754          /* AQS overrides */
755          public final boolean isHeldExclusively() { return getState() > 0; }
# Line 678 | Line 780 | public class ConcurrentHashMapV8<K, V>
780          }
781  
782          /** From CLR */
783 <        private void rotateLeft(TreeNode p) {
783 >        private void rotateLeft(TreeNode<V> p) {
784              if (p != null) {
785 <                TreeNode r = p.right, pp, rl;
785 >                TreeNode<V> r = p.right, pp, rl;
786                  if ((rl = p.right = r.left) != null)
787                      rl.parent = p;
788                  if ((pp = r.parent = p.parent) == null)
# Line 695 | Line 797 | public class ConcurrentHashMapV8<K, V>
797          }
798  
799          /** From CLR */
800 <        private void rotateRight(TreeNode p) {
800 >        private void rotateRight(TreeNode<V> p) {
801              if (p != null) {
802 <                TreeNode l = p.left, pp, lr;
802 >                TreeNode<V> l = p.left, pp, lr;
803                  if ((lr = p.left = l.right) != null)
804                      lr.parent = p;
805                  if ((pp = l.parent = p.parent) == null)
# Line 712 | Line 814 | public class ConcurrentHashMapV8<K, V>
814          }
815  
816          /**
817 <         * Return the TreeNode (or null if not found) for the given key
817 >         * Returns the TreeNode (or null if not found) for the given key
818           * starting at given root.
819           */
820 <        @SuppressWarnings("unchecked") // suppress Comparable cast warning
821 <            final TreeNode getTreeNode(int h, Object k, TreeNode p) {
820 >        @SuppressWarnings("unchecked") final TreeNode<V> getTreeNode
821 >            (int h, Object k, TreeNode<V> p) {
822              Class<?> c = k.getClass();
823              while (p != null) {
824                  int dir, ph;  Object pk; Class<?> pc;
# Line 726 | Line 828 | public class ConcurrentHashMapV8<K, V>
828                      if (c != (pc = pk.getClass()) ||
829                          !(k instanceof Comparable) ||
830                          (dir = ((Comparable)k).compareTo((Comparable)pk)) == 0) {
831 <                        dir = (c == pc) ? 0 : c.getName().compareTo(pc.getName());
832 <                        TreeNode r = null, s = null, pl, pr;
833 <                        if (dir >= 0) {
834 <                            if ((pl = p.left) != null && h <= pl.hash)
835 <                                s = pl;
831 >                        if ((dir = (c == pc) ? 0 :
832 >                             c.getName().compareTo(pc.getName())) == 0) {
833 >                            TreeNode<V> r = null, pl, pr; // check both sides
834 >                            if ((pr = p.right) != null && h >= pr.hash &&
835 >                                (r = getTreeNode(h, k, pr)) != null)
836 >                                return r;
837 >                            else if ((pl = p.left) != null && h <= pl.hash)
838 >                                dir = -1;
839 >                            else // nothing there
840 >                                return null;
841                          }
735                        else if ((pr = p.right) != null && h >= pr.hash)
736                            s = pr;
737                        if (s != null && (r = getTreeNode(h, k, s)) != null)
738                            return r;
842                      }
843                  }
844                  else
# Line 750 | Line 853 | public class ConcurrentHashMapV8<K, V>
853           * read-lock to call getTreeNode, but during failure to get
854           * lock, searches along next links.
855           */
856 <        final Object getValue(int h, Object k) {
857 <            Node r = null;
856 >        final V getValue(int h, Object k) {
857 >            Node<V> r = null;
858              int c = getState(); // Must read lock state first
859 <            for (Node e = first; e != null; e = e.next) {
859 >            for (Node<V> e = first; e != null; e = e.next) {
860                  if (c <= 0 && compareAndSetState(c, c - 1)) {
861                      try {
862                          r = getTreeNode(h, k, root);
# Line 762 | Line 865 | public class ConcurrentHashMapV8<K, V>
865                      }
866                      break;
867                  }
868 <                else if ((e.hash & HASH_BITS) == h && k.equals(e.key)) {
868 >                else if (e.hash == h && k.equals(e.key)) {
869                      r = e;
870                      break;
871                  }
# Line 776 | Line 879 | public class ConcurrentHashMapV8<K, V>
879           * Finds or adds a node.
880           * @return null if added
881           */
882 <        @SuppressWarnings("unchecked") // suppress Comparable cast warning
883 <            final TreeNode putTreeNode(int h, Object k, Object v) {
882 >        @SuppressWarnings("unchecked") final TreeNode<V> putTreeNode
883 >            (int h, Object k, V v) {
884              Class<?> c = k.getClass();
885 <            TreeNode pp = root, p = null;
885 >            TreeNode<V> pp = root, p = null;
886              int dir = 0;
887              while (pp != null) { // find existing node or leaf to insert at
888                  int ph;  Object pk; Class<?> pc;
# Line 790 | Line 893 | public class ConcurrentHashMapV8<K, V>
893                      if (c != (pc = pk.getClass()) ||
894                          !(k instanceof Comparable) ||
895                          (dir = ((Comparable)k).compareTo((Comparable)pk)) == 0) {
896 <                        dir = (c == pc) ? 0 : c.getName().compareTo(pc.getName());
897 <                        TreeNode r = null, s = null, pl, pr;
898 <                        if (dir >= 0) {
899 <                            if ((pl = p.left) != null && h <= pl.hash)
900 <                                s = pl;
896 >                        TreeNode<V> s = null, r = null, pr;
897 >                        if ((dir = (c == pc) ? 0 :
898 >                             c.getName().compareTo(pc.getName())) == 0) {
899 >                            if ((pr = p.right) != null && h >= pr.hash &&
900 >                                (r = getTreeNode(h, k, pr)) != null)
901 >                                return r;
902 >                            else // continue left
903 >                                dir = -1;
904                          }
905                          else if ((pr = p.right) != null && h >= pr.hash)
906                              s = pr;
# Line 807 | Line 913 | public class ConcurrentHashMapV8<K, V>
913                  pp = (dir > 0) ? p.right : p.left;
914              }
915  
916 <            TreeNode f = first;
917 <            TreeNode x = first = new TreeNode(h, k, v, f, p);
916 >            TreeNode<V> f = first;
917 >            TreeNode<V> x = first = new TreeNode<V>(h, k, v, f, p);
918              if (p == null)
919                  root = x;
920              else { // attach and rebalance; adapted from CLR
921 <                TreeNode xp, xpp;
921 >                TreeNode<V> xp, xpp;
922                  if (f != null)
923                      f.prev = x;
924                  if (dir <= 0)
# Line 822 | Line 928 | public class ConcurrentHashMapV8<K, V>
928                  x.red = true;
929                  while (x != null && (xp = x.parent) != null && xp.red &&
930                         (xpp = xp.parent) != null) {
931 <                    TreeNode xppl = xpp.left;
931 >                    TreeNode<V> xppl = xpp.left;
932                      if (xp == xppl) {
933 <                        TreeNode y = xpp.right;
933 >                        TreeNode<V> y = xpp.right;
934                          if (y != null && y.red) {
935                              y.red = false;
936                              xp.red = false;
# Line 846 | Line 952 | public class ConcurrentHashMapV8<K, V>
952                          }
953                      }
954                      else {
955 <                        TreeNode y = xppl;
955 >                        TreeNode<V> y = xppl;
956                          if (y != null && y.red) {
957                              y.red = false;
958                              xp.red = false;
# Line 868 | Line 974 | public class ConcurrentHashMapV8<K, V>
974                          }
975                      }
976                  }
977 <                TreeNode r = root;
977 >                TreeNode<V> r = root;
978                  if (r != null && r.red)
979                      r.red = false;
980              }
# Line 883 | Line 989 | public class ConcurrentHashMapV8<K, V>
989           * that are accessible independently of lock. So instead we
990           * swap the tree linkages.
991           */
992 <        final void deleteTreeNode(TreeNode p) {
993 <            TreeNode next = (TreeNode)p.next; // unlink traversal pointers
994 <            TreeNode pred = p.prev;
992 >        final void deleteTreeNode(TreeNode<V> p) {
993 >            TreeNode<V> next = (TreeNode<V>)p.next; // unlink traversal pointers
994 >            TreeNode<V> pred = p.prev;
995              if (pred == null)
996                  first = next;
997              else
998                  pred.next = next;
999              if (next != null)
1000                  next.prev = pred;
1001 <            TreeNode replacement;
1002 <            TreeNode pl = p.left;
1003 <            TreeNode pr = p.right;
1001 >            TreeNode<V> replacement;
1002 >            TreeNode<V> pl = p.left;
1003 >            TreeNode<V> pr = p.right;
1004              if (pl != null && pr != null) {
1005 <                TreeNode s = pr, sl;
1005 >                TreeNode<V> s = pr, sl;
1006                  while ((sl = s.left) != null) // find successor
1007                      s = sl;
1008                  boolean c = s.red; s.red = p.red; p.red = c; // swap colors
1009 <                TreeNode sr = s.right;
1010 <                TreeNode pp = p.parent;
1009 >                TreeNode<V> sr = s.right;
1010 >                TreeNode<V> pp = p.parent;
1011                  if (s == pr) { // p was s's direct parent
1012                      p.parent = s;
1013                      s.right = p;
1014                  }
1015                  else {
1016 <                    TreeNode sp = s.parent;
1016 >                    TreeNode<V> sp = s.parent;
1017                      if ((p.parent = sp) != null) {
1018                          if (s == sp.left)
1019                              sp.left = p;
# Line 932 | Line 1038 | public class ConcurrentHashMapV8<K, V>
1038              }
1039              else
1040                  replacement = (pl != null) ? pl : pr;
1041 <            TreeNode pp = p.parent;
1041 >            TreeNode<V> pp = p.parent;
1042              if (replacement == null) {
1043                  if (pp == null) {
1044                      root = null;
# Line 951 | Line 1057 | public class ConcurrentHashMapV8<K, V>
1057                  p.left = p.right = p.parent = null;
1058              }
1059              if (!p.red) { // rebalance, from CLR
1060 <                TreeNode x = replacement;
1060 >                TreeNode<V> x = replacement;
1061                  while (x != null) {
1062 <                    TreeNode xp, xpl;
1062 >                    TreeNode<V> xp, xpl;
1063                      if (x.red || (xp = x.parent) == null) {
1064                          x.red = false;
1065                          break;
1066                      }
1067                      if (x == (xpl = xp.left)) {
1068 <                        TreeNode sib = xp.right;
1068 >                        TreeNode<V> sib = xp.right;
1069                          if (sib != null && sib.red) {
1070                              sib.red = false;
1071                              xp.red = true;
# Line 969 | Line 1075 | public class ConcurrentHashMapV8<K, V>
1075                          if (sib == null)
1076                              x = xp;
1077                          else {
1078 <                            TreeNode sl = sib.left, sr = sib.right;
1078 >                            TreeNode<V> sl = sib.left, sr = sib.right;
1079                              if ((sr == null || !sr.red) &&
1080                                  (sl == null || !sl.red)) {
1081                                  sib.red = true;
# Line 981 | Line 1087 | public class ConcurrentHashMapV8<K, V>
1087                                          sl.red = false;
1088                                      sib.red = true;
1089                                      rotateRight(sib);
1090 <                                    sib = (xp = x.parent) == null ? null : xp.right;
1090 >                                    sib = (xp = x.parent) == null ?
1091 >                                        null : xp.right;
1092                                  }
1093                                  if (sib != null) {
1094                                      sib.red = (xp == null) ? false : xp.red;
# Line 997 | Line 1104 | public class ConcurrentHashMapV8<K, V>
1104                          }
1105                      }
1106                      else { // symmetric
1107 <                        TreeNode sib = xpl;
1107 >                        TreeNode<V> sib = xpl;
1108                          if (sib != null && sib.red) {
1109                              sib.red = false;
1110                              xp.red = true;
# Line 1007 | Line 1114 | public class ConcurrentHashMapV8<K, V>
1114                          if (sib == null)
1115                              x = xp;
1116                          else {
1117 <                            TreeNode sl = sib.left, sr = sib.right;
1117 >                            TreeNode<V> sl = sib.left, sr = sib.right;
1118                              if ((sl == null || !sl.red) &&
1119                                  (sr == null || !sr.red)) {
1120                                  sib.red = true;
# Line 1019 | Line 1126 | public class ConcurrentHashMapV8<K, V>
1126                                          sr.red = false;
1127                                      sib.red = true;
1128                                      rotateLeft(sib);
1129 <                                    sib = (xp = x.parent) == null ? null : xp.left;
1129 >                                    sib = (xp = x.parent) == null ?
1130 >                                        null : xp.left;
1131                                  }
1132                                  if (sib != null) {
1133                                      sib.red = (xp == null) ? false : xp.red;
# Line 1049 | Line 1157 | public class ConcurrentHashMapV8<K, V>
1157      /* ---------------- Collision reduction methods -------------- */
1158  
1159      /**
1160 <     * Spreads higher bits to lower, and also forces top 2 bits to 0.
1160 >     * Spreads higher bits to lower, and also forces top bit to 0.
1161       * Because the table uses power-of-two masking, sets of hashes
1162       * that vary only in bits above the current mask will always
1163       * collide. (Among known examples are sets of Float keys holding
# Line 1067 | Line 1175 | public class ConcurrentHashMapV8<K, V>
1175      }
1176  
1177      /**
1178 <     * Replaces a list bin with a tree bin. Call only when locked.
1179 <     * Fails to replace if the given key is non-comparable or table
1180 <     * is, or needs, resizing.
1181 <     */
1182 <    private final void replaceWithTreeBin(Node[] tab, int index, Object key) {
1183 <        if ((key instanceof Comparable) &&
1184 <            (tab.length >= MAXIMUM_CAPACITY || counter.sum() < (long)sizeCtl)) {
1185 <            TreeBin t = new TreeBin();
1186 <            for (Node e = tabAt(tab, index); e != null; e = e.next)
1079 <                t.putTreeNode(e.hash & HASH_BITS, e.key, e.val);
1080 <            setTabAt(tab, index, new Node(MOVED, t, null, null));
1178 >     * Replaces a list bin with a tree bin if key is comparable.  Call
1179 >     * only when locked.
1180 >     */
1181 >    private final void replaceWithTreeBin(Node<V>[] tab, int index, Object key) {
1182 >        if (key instanceof Comparable) {
1183 >            TreeBin<V> t = new TreeBin<V>();
1184 >            for (Node<V> e = tabAt(tab, index); e != null; e = e.next)
1185 >                t.putTreeNode(e.hash, e.key, e.val);
1186 >            setTabAt(tab, index, new Node<V>(MOVED, t, null, null));
1187          }
1188      }
1189  
1190      /* ---------------- Internal access and update methods -------------- */
1191  
1192      /** Implementation for get and containsKey */
1193 <    private final Object internalGet(Object k) {
1193 >    @SuppressWarnings("unchecked") private final V internalGet(Object k) {
1194          int h = spread(k.hashCode());
1195 <        retry: for (Node[] tab = table; tab != null;) {
1196 <            Node e, p; Object ek, ev; int eh;      // locals to read fields once
1195 >        retry: for (Node<V>[] tab = table; tab != null;) {
1196 >            Node<V> e; Object ek; V ev; int eh; // locals to read fields once
1197              for (e = tabAt(tab, (tab.length - 1) & h); e != null; e = e.next) {
1198 <                if ((eh = e.hash) == MOVED) {
1198 >                if ((eh = e.hash) < 0) {
1199                      if ((ek = e.key) instanceof TreeBin)  // search TreeBin
1200 <                        return ((TreeBin)ek).getValue(h, k);
1201 <                    else {                        // restart with new table
1202 <                        tab = (Node[])ek;
1200 >                        return ((TreeBin<V>)ek).getValue(h, k);
1201 >                    else {                      // restart with new table
1202 >                        tab = (Node<V>[])ek;
1203                          continue retry;
1204                      }
1205                  }
1206 <                else if ((eh & HASH_BITS) == h && (ev = e.val) != null &&
1206 >                else if (eh == h && (ev = e.val) != null &&
1207                           ((ek = e.key) == k || k.equals(ek)))
1208                      return ev;
1209              }
# Line 1111 | Line 1217 | public class ConcurrentHashMapV8<K, V>
1217       * Replaces node value with v, conditional upon match of cv if
1218       * non-null.  If resulting value is null, delete.
1219       */
1220 <    private final Object internalReplace(Object k, Object v, Object cv) {
1220 >    @SuppressWarnings("unchecked") private final V internalReplace
1221 >        (Object k, V v, Object cv) {
1222          int h = spread(k.hashCode());
1223 <        Object oldVal = null;
1224 <        for (Node[] tab = table;;) {
1225 <            Node f; int i, fh; Object fk;
1223 >        V oldVal = null;
1224 >        for (Node<V>[] tab = table;;) {
1225 >            Node<V> f; int i, fh; Object fk;
1226              if (tab == null ||
1227                  (f = tabAt(tab, i = (tab.length - 1) & h)) == null)
1228                  break;
1229 <            else if ((fh = f.hash) == MOVED) {
1229 >            else if ((fh = f.hash) < 0) {
1230                  if ((fk = f.key) instanceof TreeBin) {
1231 <                    TreeBin t = (TreeBin)fk;
1231 >                    TreeBin<V> t = (TreeBin<V>)fk;
1232                      boolean validated = false;
1233                      boolean deleted = false;
1234                      t.acquire(0);
1235                      try {
1236                          if (tabAt(tab, i) == f) {
1237                              validated = true;
1238 <                            TreeNode p = t.getTreeNode(h, k, t.root);
1238 >                            TreeNode<V> p = t.getTreeNode(h, k, t.root);
1239                              if (p != null) {
1240 <                                Object pv = p.val;
1240 >                                V pv = p.val;
1241                                  if (cv == null || cv == pv || cv.equals(pv)) {
1242                                      oldVal = pv;
1243                                      if ((p.val = v) == null) {
# Line 1145 | Line 1252 | public class ConcurrentHashMapV8<K, V>
1252                      }
1253                      if (validated) {
1254                          if (deleted)
1255 <                            counter.add(-1L);
1255 >                            addCount(-1L, -1);
1256                          break;
1257                      }
1258                  }
1259                  else
1260 <                    tab = (Node[])fk;
1260 >                    tab = (Node<V>[])fk;
1261              }
1262 <            else if ((fh & HASH_BITS) != h && f.next == null) // precheck
1262 >            else if (fh != h && f.next == null) // precheck
1263                  break;                          // rules out possible existence
1264 <            else if ((fh & LOCKED) != 0) {
1158 <                checkForResize();               // try resizing if can't get lock
1159 <                f.tryAwaitLock(tab, i);
1160 <            }
1161 <            else if (f.casHash(fh, fh | LOCKED)) {
1264 >            else {
1265                  boolean validated = false;
1266                  boolean deleted = false;
1267 <                try {
1267 >                synchronized (f) {
1268                      if (tabAt(tab, i) == f) {
1269                          validated = true;
1270 <                        for (Node e = f, pred = null;;) {
1271 <                            Object ek, ev;
1272 <                            if ((e.hash & HASH_BITS) == h &&
1270 >                        for (Node<V> e = f, pred = null;;) {
1271 >                            Object ek; V ev;
1272 >                            if (e.hash == h &&
1273                                  ((ev = e.val) != null) &&
1274                                  ((ek = e.key) == k || k.equals(ek))) {
1275                                  if (cv == null || cv == ev || cv.equals(ev)) {
1276                                      oldVal = ev;
1277                                      if ((e.val = v) == null) {
1278                                          deleted = true;
1279 <                                        Node en = e.next;
1279 >                                        Node<V> en = e.next;
1280                                          if (pred != null)
1281                                              pred.next = en;
1282                                          else
# Line 1187 | Line 1290 | public class ConcurrentHashMapV8<K, V>
1290                                  break;
1291                          }
1292                      }
1190                } finally {
1191                    if (!f.casHash(fh | LOCKED, fh)) {
1192                        f.hash = fh;
1193                        synchronized (f) { f.notifyAll(); };
1194                    }
1293                  }
1294                  if (validated) {
1295                      if (deleted)
1296 <                        counter.add(-1L);
1296 >                        addCount(-1L, -1);
1297                      break;
1298                  }
1299              }
# Line 1204 | Line 1302 | public class ConcurrentHashMapV8<K, V>
1302      }
1303  
1304      /*
1305 <     * Internal versions of the five insertion methods, each a
1306 <     * little more complicated than the last. All have
1209 <     * the same basic structure as the first (internalPut):
1305 >     * Internal versions of insertion methods
1306 >     * All have the same basic structure as the first (internalPut):
1307       *  1. If table uninitialized, create
1308       *  2. If bin empty, try to CAS new node
1309       *  3. If bin stale, use new table
1310       *  4. if bin converted to TreeBin, validate and relay to TreeBin methods
1311       *  5. Lock and validate; if valid, scan and add or update
1312       *
1313 <     * The others interweave other checks and/or alternative actions:
1314 <     *  * Plain put checks for and performs resize after insertion.
1315 <     *  * putIfAbsent prescans for mapping without lock (and fails to add
1316 <     *    if present), which also makes pre-emptive resize checks worthwhile.
1317 <     *  * computeIfAbsent extends form used in putIfAbsent with additional
1318 <     *    mechanics to deal with, calls, potential exceptions and null
1319 <     *    returns from function call.
1223 <     *  * compute uses the same function-call mechanics, but without
1224 <     *    the prescans
1225 <     *  * putAll attempts to pre-allocate enough table space
1226 <     *    and more lazily performs count updates and checks.
1227 <     *
1228 <     * Someday when details settle down a bit more, it might be worth
1229 <     * some factoring to reduce sprawl.
1313 >     * The putAll method differs mainly in attempting to pre-allocate
1314 >     * enough table space, and also more lazily performs count updates
1315 >     * and checks.
1316 >     *
1317 >     * Most of the function-accepting methods can't be factored nicely
1318 >     * because they require different functional forms, so instead
1319 >     * sprawl out similar mechanics.
1320       */
1321  
1322 <    /** Implementation for put */
1323 <    private final Object internalPut(Object k, Object v) {
1322 >    /** Implementation for put and putIfAbsent */
1323 >    @SuppressWarnings("unchecked") private final V internalPut
1324 >        (K k, V v, boolean onlyIfAbsent) {
1325 >        if (k == null || v == null) throw new NullPointerException();
1326          int h = spread(k.hashCode());
1327 <        int count = 0;
1328 <        for (Node[] tab = table;;) {
1329 <            int i; Node f; int fh; Object fk;
1327 >        int len = 0;
1328 >        for (Node<V>[] tab = table;;) {
1329 >            int i, fh; Node<V> f; Object fk; V fv;
1330              if (tab == null)
1331                  tab = initTable();
1332              else if ((f = tabAt(tab, i = (tab.length - 1) & h)) == null) {
1333 <                if (casTabAt(tab, i, null, new Node(h, k, v, null)))
1333 >                if (casTabAt(tab, i, null, new Node<V>(h, k, v, null)))
1334                      break;                   // no lock when adding to empty bin
1335              }
1336 <            else if ((fh = f.hash) == MOVED) {
1336 >            else if ((fh = f.hash) < 0) {
1337                  if ((fk = f.key) instanceof TreeBin) {
1338 <                    TreeBin t = (TreeBin)fk;
1339 <                    Object oldVal = null;
1338 >                    TreeBin<V> t = (TreeBin<V>)fk;
1339 >                    V oldVal = null;
1340                      t.acquire(0);
1341                      try {
1342                          if (tabAt(tab, i) == f) {
1343 <                            count = 2;
1344 <                            TreeNode p = t.putTreeNode(h, k, v);
1343 >                            len = 2;
1344 >                            TreeNode<V> p = t.putTreeNode(h, k, v);
1345                              if (p != null) {
1346                                  oldVal = p.val;
1347 <                                p.val = v;
1347 >                                if (!onlyIfAbsent)
1348 >                                    p.val = v;
1349                              }
1350                          }
1351                      } finally {
1352                          t.release(0);
1353                      }
1354 <                    if (count != 0) {
1354 >                    if (len != 0) {
1355                          if (oldVal != null)
1356                              return oldVal;
1357                          break;
1358                      }
1359                  }
1360                  else
1361 <                    tab = (Node[])fk;
1361 >                    tab = (Node<V>[])fk;
1362              }
1363 <            else if ((fh & LOCKED) != 0) {
1364 <                checkForResize();
1365 <                f.tryAwaitLock(tab, i);
1366 <            }
1367 <            else if (f.casHash(fh, fh | LOCKED)) {
1368 <                Object oldVal = null;
1276 <                try {                        // needed in case equals() throws
1363 >            else if (onlyIfAbsent && fh == h && (fv = f.val) != null &&
1364 >                     ((fk = f.key) == k || k.equals(fk))) // peek while nearby
1365 >                return fv;
1366 >            else {
1367 >                V oldVal = null;
1368 >                synchronized (f) {
1369                      if (tabAt(tab, i) == f) {
1370 <                        count = 1;
1371 <                        for (Node e = f;; ++count) {
1372 <                            Object ek, ev;
1373 <                            if ((e.hash & HASH_BITS) == h &&
1370 >                        len = 1;
1371 >                        for (Node<V> e = f;; ++len) {
1372 >                            Object ek; V ev;
1373 >                            if (e.hash == h &&
1374                                  (ev = e.val) != null &&
1375                                  ((ek = e.key) == k || k.equals(ek))) {
1376                                  oldVal = ev;
1377 <                                e.val = v;
1377 >                                if (!onlyIfAbsent)
1378 >                                    e.val = v;
1379                                  break;
1380                              }
1381 <                            Node last = e;
1381 >                            Node<V> last = e;
1382                              if ((e = e.next) == null) {
1383 <                                last.next = new Node(h, k, v, null);
1384 <                                if (count >= TREE_THRESHOLD)
1383 >                                last.next = new Node<V>(h, k, v, null);
1384 >                                if (len >= TREE_THRESHOLD)
1385                                      replaceWithTreeBin(tab, i, k);
1386                                  break;
1387                              }
1388                          }
1389                      }
1297                } finally {                  // unlock and signal if needed
1298                    if (!f.casHash(fh | LOCKED, fh)) {
1299                        f.hash = fh;
1300                        synchronized (f) { f.notifyAll(); };
1301                    }
1390                  }
1391 <                if (count != 0) {
1391 >                if (len != 0) {
1392                      if (oldVal != null)
1393                          return oldVal;
1306                    if (tab.length <= 64)
1307                        count = 2;
1308                    break;
1309                }
1310            }
1311        }
1312        counter.add(1L);
1313        if (count > 1)
1314            checkForResize();
1315        return null;
1316    }
1317
1318    /** Implementation for putIfAbsent */
1319    private final Object internalPutIfAbsent(Object k, Object v) {
1320        int h = spread(k.hashCode());
1321        int count = 0;
1322        for (Node[] tab = table;;) {
1323            int i; Node f; int fh; Object fk, fv;
1324            if (tab == null)
1325                tab = initTable();
1326            else if ((f = tabAt(tab, i = (tab.length - 1) & h)) == null) {
1327                if (casTabAt(tab, i, null, new Node(h, k, v, null)))
1394                      break;
1329            }
1330            else if ((fh = f.hash) == MOVED) {
1331                if ((fk = f.key) instanceof TreeBin) {
1332                    TreeBin t = (TreeBin)fk;
1333                    Object oldVal = null;
1334                    t.acquire(0);
1335                    try {
1336                        if (tabAt(tab, i) == f) {
1337                            count = 2;
1338                            TreeNode p = t.putTreeNode(h, k, v);
1339                            if (p != null)
1340                                oldVal = p.val;
1341                        }
1342                    } finally {
1343                        t.release(0);
1344                    }
1345                    if (count != 0) {
1346                        if (oldVal != null)
1347                            return oldVal;
1348                        break;
1349                    }
1350                }
1351                else
1352                    tab = (Node[])fk;
1353            }
1354            else if ((fh & HASH_BITS) == h && (fv = f.val) != null &&
1355                     ((fk = f.key) == k || k.equals(fk)))
1356                return fv;
1357            else {
1358                Node g = f.next;
1359                if (g != null) { // at least 2 nodes -- search and maybe resize
1360                    for (Node e = g;;) {
1361                        Object ek, ev;
1362                        if ((e.hash & HASH_BITS) == h && (ev = e.val) != null &&
1363                            ((ek = e.key) == k || k.equals(ek)))
1364                            return ev;
1365                        if ((e = e.next) == null) {
1366                            checkForResize();
1367                            break;
1368                        }
1369                    }
1370                }
1371                if (((fh = f.hash) & LOCKED) != 0) {
1372                    checkForResize();
1373                    f.tryAwaitLock(tab, i);
1374                }
1375                else if (tabAt(tab, i) == f && f.casHash(fh, fh | LOCKED)) {
1376                    Object oldVal = null;
1377                    try {
1378                        if (tabAt(tab, i) == f) {
1379                            count = 1;
1380                            for (Node e = f;; ++count) {
1381                                Object ek, ev;
1382                                if ((e.hash & HASH_BITS) == h &&
1383                                    (ev = e.val) != null &&
1384                                    ((ek = e.key) == k || k.equals(ek))) {
1385                                    oldVal = ev;
1386                                    break;
1387                                }
1388                                Node last = e;
1389                                if ((e = e.next) == null) {
1390                                    last.next = new Node(h, k, v, null);
1391                                    if (count >= TREE_THRESHOLD)
1392                                        replaceWithTreeBin(tab, i, k);
1393                                    break;
1394                                }
1395                            }
1396                        }
1397                    } finally {
1398                        if (!f.casHash(fh | LOCKED, fh)) {
1399                            f.hash = fh;
1400                            synchronized (f) { f.notifyAll(); };
1401                        }
1402                    }
1403                    if (count != 0) {
1404                        if (oldVal != null)
1405                            return oldVal;
1406                        if (tab.length <= 64)
1407                            count = 2;
1408                        break;
1409                    }
1395                  }
1396              }
1397          }
1398 <        counter.add(1L);
1414 <        if (count > 1)
1415 <            checkForResize();
1398 >        addCount(1L, len);
1399          return null;
1400      }
1401  
1402      /** Implementation for computeIfAbsent */
1403 <    private final Object internalComputeIfAbsent(K k,
1404 <                                                 Fun<? super K, ?> mf) {
1403 >    @SuppressWarnings("unchecked") private final V internalComputeIfAbsent
1404 >        (K k, Fun<? super K, ? extends V> mf) {
1405 >        if (k == null || mf == null)
1406 >            throw new NullPointerException();
1407          int h = spread(k.hashCode());
1408 <        Object val = null;
1409 <        int count = 0;
1410 <        for (Node[] tab = table;;) {
1411 <            Node f; int i, fh; Object fk, fv;
1408 >        V val = null;
1409 >        int len = 0;
1410 >        for (Node<V>[] tab = table;;) {
1411 >            Node<V> f; int i; Object fk;
1412              if (tab == null)
1413                  tab = initTable();
1414              else if ((f = tabAt(tab, i = (tab.length - 1) & h)) == null) {
1415 <                Node node = new Node(fh = h | LOCKED, k, null, null);
1416 <                if (casTabAt(tab, i, null, node)) {
1417 <                    count = 1;
1418 <                    try {
1419 <                        if ((val = mf.apply(k)) != null)
1420 <                            node.val = val;
1421 <                    } finally {
1422 <                        if (val == null)
1423 <                            setTabAt(tab, i, null);
1424 <                        if (!node.casHash(fh, h)) {
1440 <                            node.hash = h;
1441 <                            synchronized (node) { node.notifyAll(); };
1415 >                Node<V> node = new Node<V>(h, k, null, null);
1416 >                synchronized (node) {
1417 >                    if (casTabAt(tab, i, null, node)) {
1418 >                        len = 1;
1419 >                        try {
1420 >                            if ((val = mf.apply(k)) != null)
1421 >                                node.val = val;
1422 >                        } finally {
1423 >                            if (val == null)
1424 >                                setTabAt(tab, i, null);
1425                          }
1426                      }
1427                  }
1428 <                if (count != 0)
1428 >                if (len != 0)
1429                      break;
1430              }
1431 <            else if ((fh = f.hash) == MOVED) {
1431 >            else if (f.hash < 0) {
1432                  if ((fk = f.key) instanceof TreeBin) {
1433 <                    TreeBin t = (TreeBin)fk;
1433 >                    TreeBin<V> t = (TreeBin<V>)fk;
1434                      boolean added = false;
1435                      t.acquire(0);
1436                      try {
1437                          if (tabAt(tab, i) == f) {
1438 <                            count = 1;
1439 <                            TreeNode p = t.getTreeNode(h, k, t.root);
1438 >                            len = 1;
1439 >                            TreeNode<V> p = t.getTreeNode(h, k, t.root);
1440                              if (p != null)
1441                                  val = p.val;
1442                              else if ((val = mf.apply(k)) != null) {
1443                                  added = true;
1444 <                                count = 2;
1444 >                                len = 2;
1445                                  t.putTreeNode(h, k, val);
1446                              }
1447                          }
1448                      } finally {
1449                          t.release(0);
1450                      }
1451 <                    if (count != 0) {
1451 >                    if (len != 0) {
1452                          if (!added)
1453                              return val;
1454                          break;
1455                      }
1456                  }
1457                  else
1458 <                    tab = (Node[])fk;
1458 >                    tab = (Node<V>[])fk;
1459              }
1477            else if ((fh & HASH_BITS) == h && (fv = f.val) != null &&
1478                     ((fk = f.key) == k || k.equals(fk)))
1479                return fv;
1460              else {
1461 <                Node g = f.next;
1462 <                if (g != null) {
1463 <                    for (Node e = g;;) {
1464 <                        Object ek, ev;
1465 <                        if ((e.hash & HASH_BITS) == h && (ev = e.val) != null &&
1486 <                            ((ek = e.key) == k || k.equals(ek)))
1487 <                            return ev;
1488 <                        if ((e = e.next) == null) {
1489 <                            checkForResize();
1490 <                            break;
1491 <                        }
1492 <                    }
1493 <                }
1494 <                if (((fh = f.hash) & LOCKED) != 0) {
1495 <                    checkForResize();
1496 <                    f.tryAwaitLock(tab, i);
1461 >                for (Node<V> e = f; e != null; e = e.next) { // prescan
1462 >                    Object ek; V ev;
1463 >                    if (e.hash == h && (ev = e.val) != null &&
1464 >                        ((ek = e.key) == k || k.equals(ek)))
1465 >                        return ev;
1466                  }
1467 <                else if (tabAt(tab, i) == f && f.casHash(fh, fh | LOCKED)) {
1468 <                    boolean added = false;
1469 <                    try {
1470 <                        if (tabAt(tab, i) == f) {
1471 <                            count = 1;
1472 <                            for (Node e = f;; ++count) {
1473 <                                Object ek, ev;
1474 <                                if ((e.hash & HASH_BITS) == h &&
1475 <                                    (ev = e.val) != null &&
1476 <                                    ((ek = e.key) == k || k.equals(ek))) {
1477 <                                    val = ev;
1478 <                                    break;
1479 <                                }
1480 <                                Node last = e;
1481 <                                if ((e = e.next) == null) {
1482 <                                    if ((val = mf.apply(k)) != null) {
1483 <                                        added = true;
1484 <                                        last.next = new Node(h, k, val, null);
1485 <                                        if (count >= TREE_THRESHOLD)
1517 <                                            replaceWithTreeBin(tab, i, k);
1518 <                                    }
1519 <                                    break;
1467 >                boolean added = false;
1468 >                synchronized (f) {
1469 >                    if (tabAt(tab, i) == f) {
1470 >                        len = 1;
1471 >                        for (Node<V> e = f;; ++len) {
1472 >                            Object ek; V ev;
1473 >                            if (e.hash == h &&
1474 >                                (ev = e.val) != null &&
1475 >                                ((ek = e.key) == k || k.equals(ek))) {
1476 >                                val = ev;
1477 >                                break;
1478 >                            }
1479 >                            Node<V> last = e;
1480 >                            if ((e = e.next) == null) {
1481 >                                if ((val = mf.apply(k)) != null) {
1482 >                                    added = true;
1483 >                                    last.next = new Node<V>(h, k, val, null);
1484 >                                    if (len >= TREE_THRESHOLD)
1485 >                                        replaceWithTreeBin(tab, i, k);
1486                                  }
1487 +                                break;
1488                              }
1489                          }
1523                    } finally {
1524                        if (!f.casHash(fh | LOCKED, fh)) {
1525                            f.hash = fh;
1526                            synchronized (f) { f.notifyAll(); };
1527                        }
1528                    }
1529                    if (count != 0) {
1530                        if (!added)
1531                            return val;
1532                        if (tab.length <= 64)
1533                            count = 2;
1534                        break;
1490                      }
1491                  }
1492 +                if (len != 0) {
1493 +                    if (!added)
1494 +                        return val;
1495 +                    break;
1496 +                }
1497              }
1498          }
1499 <        if (val != null) {
1500 <            counter.add(1L);
1541 <            if (count > 1)
1542 <                checkForResize();
1543 <        }
1499 >        if (val != null)
1500 >            addCount(1L, len);
1501          return val;
1502      }
1503  
1504      /** Implementation for compute */
1505 <    @SuppressWarnings("unchecked")
1506 <        private final Object internalCompute(K k, boolean onlyIfPresent,
1507 <                                             BiFun<? super K, ? super V, ? extends V> mf) {
1505 >    @SuppressWarnings("unchecked") private final V internalCompute
1506 >        (K k, boolean onlyIfPresent,
1507 >         BiFun<? super K, ? super V, ? extends V> mf) {
1508 >        if (k == null || mf == null)
1509 >            throw new NullPointerException();
1510          int h = spread(k.hashCode());
1511 <        Object val = null;
1511 >        V val = null;
1512          int delta = 0;
1513 <        int count = 0;
1514 <        for (Node[] tab = table;;) {
1515 <            Node f; int i, fh; Object fk;
1513 >        int len = 0;
1514 >        for (Node<V>[] tab = table;;) {
1515 >            Node<V> f; int i, fh; Object fk;
1516              if (tab == null)
1517                  tab = initTable();
1518              else if ((f = tabAt(tab, i = (tab.length - 1) & h)) == null) {
1519                  if (onlyIfPresent)
1520                      break;
1521 <                Node node = new Node(fh = h | LOCKED, k, null, null);
1522 <                if (casTabAt(tab, i, null, node)) {
1523 <                    try {
1524 <                        count = 1;
1525 <                        if ((val = mf.apply(k, null)) != null) {
1526 <                            node.val = val;
1527 <                            delta = 1;
1528 <                        }
1529 <                    } finally {
1530 <                        if (delta == 0)
1531 <                            setTabAt(tab, i, null);
1532 <                        if (!node.casHash(fh, h)) {
1574 <                            node.hash = h;
1575 <                            synchronized (node) { node.notifyAll(); };
1521 >                Node<V> node = new Node<V>(h, k, null, null);
1522 >                synchronized (node) {
1523 >                    if (casTabAt(tab, i, null, node)) {
1524 >                        try {
1525 >                            len = 1;
1526 >                            if ((val = mf.apply(k, null)) != null) {
1527 >                                node.val = val;
1528 >                                delta = 1;
1529 >                            }
1530 >                        } finally {
1531 >                            if (delta == 0)
1532 >                                setTabAt(tab, i, null);
1533                          }
1534                      }
1535                  }
1536 <                if (count != 0)
1536 >                if (len != 0)
1537                      break;
1538              }
1539 <            else if ((fh = f.hash) == MOVED) {
1539 >            else if ((fh = f.hash) < 0) {
1540                  if ((fk = f.key) instanceof TreeBin) {
1541 <                    TreeBin t = (TreeBin)fk;
1541 >                    TreeBin<V> t = (TreeBin<V>)fk;
1542                      t.acquire(0);
1543                      try {
1544                          if (tabAt(tab, i) == f) {
1545 <                            count = 1;
1546 <                            TreeNode p = t.getTreeNode(h, k, t.root);
1547 <                            Object pv = (p == null) ? null : p.val;
1548 <                            if ((val = mf.apply(k, (V)pv)) != null) {
1545 >                            len = 1;
1546 >                            TreeNode<V> p = t.getTreeNode(h, k, t.root);
1547 >                            if (p == null && onlyIfPresent)
1548 >                                break;
1549 >                            V pv = (p == null) ? null : p.val;
1550 >                            if ((val = mf.apply(k, pv)) != null) {
1551                                  if (p != null)
1552                                      p.val = val;
1553                                  else {
1554 <                                    count = 2;
1554 >                                    len = 2;
1555                                      delta = 1;
1556                                      t.putTreeNode(h, k, val);
1557                                  }
# Line 1605 | Line 1564 | public class ConcurrentHashMapV8<K, V>
1564                      } finally {
1565                          t.release(0);
1566                      }
1567 <                    if (count != 0)
1567 >                    if (len != 0)
1568                          break;
1569                  }
1570                  else
1571 <                    tab = (Node[])fk;
1571 >                    tab = (Node<V>[])fk;
1572              }
1573 <            else if ((fh & LOCKED) != 0) {
1574 <                checkForResize();
1616 <                f.tryAwaitLock(tab, i);
1617 <            }
1618 <            else if (f.casHash(fh, fh | LOCKED)) {
1619 <                try {
1573 >            else {
1574 >                synchronized (f) {
1575                      if (tabAt(tab, i) == f) {
1576 <                        count = 1;
1577 <                        for (Node e = f, pred = null;; ++count) {
1578 <                            Object ek, ev;
1579 <                            if ((e.hash & HASH_BITS) == h &&
1576 >                        len = 1;
1577 >                        for (Node<V> e = f, pred = null;; ++len) {
1578 >                            Object ek; V ev;
1579 >                            if (e.hash == h &&
1580                                  (ev = e.val) != null &&
1581                                  ((ek = e.key) == k || k.equals(ek))) {
1582 <                                val = mf.apply(k, (V)ev);
1582 >                                val = mf.apply(k, ev);
1583                                  if (val != null)
1584                                      e.val = val;
1585                                  else {
1586                                      delta = -1;
1587 <                                    Node en = e.next;
1587 >                                    Node<V> en = e.next;
1588                                      if (pred != null)
1589                                          pred.next = en;
1590                                      else
# Line 1639 | Line 1594 | public class ConcurrentHashMapV8<K, V>
1594                              }
1595                              pred = e;
1596                              if ((e = e.next) == null) {
1597 <                                if (!onlyIfPresent && (val = mf.apply(k, null)) != null) {
1598 <                                    pred.next = new Node(h, k, val, null);
1597 >                                if (!onlyIfPresent &&
1598 >                                    (val = mf.apply(k, null)) != null) {
1599 >                                    pred.next = new Node<V>(h, k, val, null);
1600                                      delta = 1;
1601 <                                    if (count >= TREE_THRESHOLD)
1601 >                                    if (len >= TREE_THRESHOLD)
1602                                          replaceWithTreeBin(tab, i, k);
1603                                  }
1604                                  break;
1605                              }
1606                          }
1607                      }
1652                } finally {
1653                    if (!f.casHash(fh | LOCKED, fh)) {
1654                        f.hash = fh;
1655                        synchronized (f) { f.notifyAll(); };
1656                    }
1608                  }
1609 <                if (count != 0) {
1659 <                    if (tab.length <= 64)
1660 <                        count = 2;
1609 >                if (len != 0)
1610                      break;
1662                }
1611              }
1612          }
1613 <        if (delta != 0) {
1614 <            counter.add((long)delta);
1667 <            if (count > 1)
1668 <                checkForResize();
1669 <        }
1613 >        if (delta != 0)
1614 >            addCount((long)delta, len);
1615          return val;
1616      }
1617  
1618 <    private final Object internalMerge(K k, V v,
1619 <                                       BiFun<? super V, ? super V, ? extends V> mf) {
1618 >    /** Implementation for merge */
1619 >    @SuppressWarnings("unchecked") private final V internalMerge
1620 >        (K k, V v, BiFun<? super V, ? super V, ? extends V> mf) {
1621 >        if (k == null || v == null || mf == null)
1622 >            throw new NullPointerException();
1623          int h = spread(k.hashCode());
1624 <        Object val = null;
1624 >        V val = null;
1625          int delta = 0;
1626 <        int count = 0;
1627 <        for (Node[] tab = table;;) {
1628 <            int i; Node f; int fh; Object fk, fv;
1626 >        int len = 0;
1627 >        for (Node<V>[] tab = table;;) {
1628 >            int i; Node<V> f; Object fk; V fv;
1629              if (tab == null)
1630                  tab = initTable();
1631              else if ((f = tabAt(tab, i = (tab.length - 1) & h)) == null) {
1632 <                if (casTabAt(tab, i, null, new Node(h, k, v, null))) {
1632 >                if (casTabAt(tab, i, null, new Node<V>(h, k, v, null))) {
1633                      delta = 1;
1634                      val = v;
1635                      break;
1636                  }
1637              }
1638 <            else if ((fh = f.hash) == MOVED) {
1638 >            else if (f.hash < 0) {
1639                  if ((fk = f.key) instanceof TreeBin) {
1640 <                    TreeBin t = (TreeBin)fk;
1640 >                    TreeBin<V> t = (TreeBin<V>)fk;
1641                      t.acquire(0);
1642                      try {
1643                          if (tabAt(tab, i) == f) {
1644 <                            count = 1;
1645 <                            TreeNode p = t.getTreeNode(h, k, t.root);
1646 <                            val = (p == null) ? v : mf.apply((V)p.val, v);
1644 >                            len = 1;
1645 >                            TreeNode<V> p = t.getTreeNode(h, k, t.root);
1646 >                            val = (p == null) ? v : mf.apply(p.val, v);
1647                              if (val != null) {
1648                                  if (p != null)
1649                                      p.val = val;
1650                                  else {
1651 <                                    count = 2;
1651 >                                    len = 2;
1652                                      delta = 1;
1653                                      t.putTreeNode(h, k, val);
1654                                  }
# Line 1713 | Line 1661 | public class ConcurrentHashMapV8<K, V>
1661                      } finally {
1662                          t.release(0);
1663                      }
1664 <                    if (count != 0)
1664 >                    if (len != 0)
1665                          break;
1666                  }
1667                  else
1668 <                    tab = (Node[])fk;
1721 <            }
1722 <            else if ((fh & LOCKED) != 0) {
1723 <                checkForResize();
1724 <                f.tryAwaitLock(tab, i);
1668 >                    tab = (Node<V>[])fk;
1669              }
1670 <            else if (f.casHash(fh, fh | LOCKED)) {
1671 <                try {
1670 >            else {
1671 >                synchronized (f) {
1672                      if (tabAt(tab, i) == f) {
1673 <                        count = 1;
1674 <                        for (Node e = f, pred = null;; ++count) {
1675 <                            Object ek, ev;
1676 <                            if ((e.hash & HASH_BITS) == h &&
1673 >                        len = 1;
1674 >                        for (Node<V> e = f, pred = null;; ++len) {
1675 >                            Object ek; V ev;
1676 >                            if (e.hash == h &&
1677                                  (ev = e.val) != null &&
1678                                  ((ek = e.key) == k || k.equals(ek))) {
1679 <                                val = mf.apply(v, (V)ev);
1679 >                                val = mf.apply(ev, v);
1680                                  if (val != null)
1681                                      e.val = val;
1682                                  else {
1683                                      delta = -1;
1684 <                                    Node en = e.next;
1684 >                                    Node<V> en = e.next;
1685                                      if (pred != null)
1686                                          pred.next = en;
1687                                      else
# Line 1748 | Line 1692 | public class ConcurrentHashMapV8<K, V>
1692                              pred = e;
1693                              if ((e = e.next) == null) {
1694                                  val = v;
1695 <                                pred.next = new Node(h, k, val, null);
1695 >                                pred.next = new Node<V>(h, k, val, null);
1696                                  delta = 1;
1697 <                                if (count >= TREE_THRESHOLD)
1697 >                                if (len >= TREE_THRESHOLD)
1698                                      replaceWithTreeBin(tab, i, k);
1699                                  break;
1700                              }
1701                          }
1702                      }
1759                } finally {
1760                    if (!f.casHash(fh | LOCKED, fh)) {
1761                        f.hash = fh;
1762                        synchronized (f) { f.notifyAll(); };
1763                    }
1703                  }
1704 <                if (count != 0) {
1766 <                    if (tab.length <= 64)
1767 <                        count = 2;
1704 >                if (len != 0)
1705                      break;
1769                }
1706              }
1707          }
1708 <        if (delta != 0) {
1709 <            counter.add((long)delta);
1774 <            if (count > 1)
1775 <                checkForResize();
1776 <        }
1708 >        if (delta != 0)
1709 >            addCount((long)delta, len);
1710          return val;
1711      }
1712  
1713      /** Implementation for putAll */
1714 <    private final void internalPutAll(Map<?, ?> m) {
1714 >    @SuppressWarnings("unchecked") private final void internalPutAll
1715 >        (Map<? extends K, ? extends V> m) {
1716          tryPresize(m.size());
1717          long delta = 0L;     // number of uncommitted additions
1718          boolean npe = false; // to throw exception on exit for nulls
1719          try {                // to clean up counts on other exceptions
1720 <            for (Map.Entry<?, ?> entry : m.entrySet()) {
1721 <                Object k, v;
1720 >            for (Map.Entry<?, ? extends V> entry : m.entrySet()) {
1721 >                Object k; V v;
1722                  if (entry == null || (k = entry.getKey()) == null ||
1723                      (v = entry.getValue()) == null) {
1724                      npe = true;
1725                      break;
1726                  }
1727                  int h = spread(k.hashCode());
1728 <                for (Node[] tab = table;;) {
1729 <                    int i; Node f; int fh; Object fk;
1728 >                for (Node<V>[] tab = table;;) {
1729 >                    int i; Node<V> f; int fh; Object fk;
1730                      if (tab == null)
1731                          tab = initTable();
1732                      else if ((f = tabAt(tab, i = (tab.length - 1) & h)) == null){
1733 <                        if (casTabAt(tab, i, null, new Node(h, k, v, null))) {
1733 >                        if (casTabAt(tab, i, null, new Node<V>(h, k, v, null))) {
1734                              ++delta;
1735                              break;
1736                          }
1737                      }
1738 <                    else if ((fh = f.hash) == MOVED) {
1738 >                    else if ((fh = f.hash) < 0) {
1739                          if ((fk = f.key) instanceof TreeBin) {
1740 <                            TreeBin t = (TreeBin)fk;
1740 >                            TreeBin<V> t = (TreeBin<V>)fk;
1741                              boolean validated = false;
1742                              t.acquire(0);
1743                              try {
1744                                  if (tabAt(tab, i) == f) {
1745                                      validated = true;
1746 <                                    TreeNode p = t.getTreeNode(h, k, t.root);
1746 >                                    TreeNode<V> p = t.getTreeNode(h, k, t.root);
1747                                      if (p != null)
1748                                          p.val = v;
1749                                      else {
# Line 1824 | Line 1758 | public class ConcurrentHashMapV8<K, V>
1758                                  break;
1759                          }
1760                          else
1761 <                            tab = (Node[])fk;
1761 >                            tab = (Node<V>[])fk;
1762                      }
1763 <                    else if ((fh & LOCKED) != 0) {
1764 <                        counter.add(delta);
1765 <                        delta = 0L;
1832 <                        checkForResize();
1833 <                        f.tryAwaitLock(tab, i);
1834 <                    }
1835 <                    else if (f.casHash(fh, fh | LOCKED)) {
1836 <                        int count = 0;
1837 <                        try {
1763 >                    else {
1764 >                        int len = 0;
1765 >                        synchronized (f) {
1766                              if (tabAt(tab, i) == f) {
1767 <                                count = 1;
1768 <                                for (Node e = f;; ++count) {
1769 <                                    Object ek, ev;
1770 <                                    if ((e.hash & HASH_BITS) == h &&
1767 >                                len = 1;
1768 >                                for (Node<V> e = f;; ++len) {
1769 >                                    Object ek; V ev;
1770 >                                    if (e.hash == h &&
1771                                          (ev = e.val) != null &&
1772                                          ((ek = e.key) == k || k.equals(ek))) {
1773                                          e.val = v;
1774                                          break;
1775                                      }
1776 <                                    Node last = e;
1776 >                                    Node<V> last = e;
1777                                      if ((e = e.next) == null) {
1778                                          ++delta;
1779 <                                        last.next = new Node(h, k, v, null);
1780 <                                        if (count >= TREE_THRESHOLD)
1779 >                                        last.next = new Node<V>(h, k, v, null);
1780 >                                        if (len >= TREE_THRESHOLD)
1781                                              replaceWithTreeBin(tab, i, k);
1782                                          break;
1783                                      }
1784                                  }
1785                              }
1858                        } finally {
1859                            if (!f.casHash(fh | LOCKED, fh)) {
1860                                f.hash = fh;
1861                                synchronized (f) { f.notifyAll(); };
1862                            }
1786                          }
1787 <                        if (count != 0) {
1788 <                            if (count > 1) {
1789 <                                counter.add(delta);
1867 <                                delta = 0L;
1868 <                                checkForResize();
1869 <                            }
1787 >                        if (len != 0) {
1788 >                            if (len > 1)
1789 >                                addCount(delta, len);
1790                              break;
1791                          }
1792                      }
1793                  }
1794              }
1795          } finally {
1796 <            if (delta != 0)
1797 <                counter.add(delta);
1796 >            if (delta != 0L)
1797 >                addCount(delta, 2);
1798          }
1799          if (npe)
1800              throw new NullPointerException();
1801      }
1802  
1803 +    /**
1804 +     * Implementation for clear. Steps through each bin, removing all
1805 +     * nodes.
1806 +     */
1807 +    @SuppressWarnings("unchecked") private final void internalClear() {
1808 +        long delta = 0L; // negative number of deletions
1809 +        int i = 0;
1810 +        Node<V>[] tab = table;
1811 +        while (tab != null && i < tab.length) {
1812 +            Node<V> f = tabAt(tab, i);
1813 +            if (f == null)
1814 +                ++i;
1815 +            else if (f.hash < 0) {
1816 +                Object fk;
1817 +                if ((fk = f.key) instanceof TreeBin) {
1818 +                    TreeBin<V> t = (TreeBin<V>)fk;
1819 +                    t.acquire(0);
1820 +                    try {
1821 +                        if (tabAt(tab, i) == f) {
1822 +                            for (Node<V> p = t.first; p != null; p = p.next) {
1823 +                                if (p.val != null) { // (currently always true)
1824 +                                    p.val = null;
1825 +                                    --delta;
1826 +                                }
1827 +                            }
1828 +                            t.first = null;
1829 +                            t.root = null;
1830 +                            ++i;
1831 +                        }
1832 +                    } finally {
1833 +                        t.release(0);
1834 +                    }
1835 +                }
1836 +                else
1837 +                    tab = (Node<V>[])fk;
1838 +            }
1839 +            else {
1840 +                synchronized (f) {
1841 +                    if (tabAt(tab, i) == f) {
1842 +                        for (Node<V> e = f; e != null; e = e.next) {
1843 +                            if (e.val != null) {  // (currently always true)
1844 +                                e.val = null;
1845 +                                --delta;
1846 +                            }
1847 +                        }
1848 +                        setTabAt(tab, i, null);
1849 +                        ++i;
1850 +                    }
1851 +                }
1852 +            }
1853 +        }
1854 +        if (delta != 0L)
1855 +            addCount(delta, -1);
1856 +    }
1857 +
1858      /* ---------------- Table Initialization and Resizing -------------- */
1859  
1860      /**
# Line 1899 | Line 1874 | public class ConcurrentHashMapV8<K, V>
1874      /**
1875       * Initializes table, using the size recorded in sizeCtl.
1876       */
1877 <    private final Node[] initTable() {
1878 <        Node[] tab; int sc;
1877 >    @SuppressWarnings("unchecked") private final Node<V>[] initTable() {
1878 >        Node<V>[] tab; int sc;
1879          while ((tab = table) == null) {
1880              if ((sc = sizeCtl) < 0)
1881                  Thread.yield(); // lost initialization race; just spin
1882 <            else if (UNSAFE.compareAndSwapInt(this, sizeCtlOffset, sc, -1)) {
1882 >            else if (U.compareAndSwapInt(this, SIZECTL, sc, -1)) {
1883                  try {
1884                      if ((tab = table) == null) {
1885                          int n = (sc > 0) ? sc : DEFAULT_CAPACITY;
1886 <                        tab = table = new Node[n];
1886 >                        @SuppressWarnings("rawtypes") Node[] tb = new Node[n];
1887 >                        table = tab = (Node<V>[])tb;
1888                          sc = n - (n >>> 2);
1889                      }
1890                  } finally {
# Line 1921 | Line 1897 | public class ConcurrentHashMapV8<K, V>
1897      }
1898  
1899      /**
1900 <     * If table is too small and not already resizing, creates next
1901 <     * table and transfers bins.  Rechecks occupancy after a transfer
1902 <     * to see if another resize is already needed because resizings
1903 <     * are lagging additions.
1904 <     */
1905 <    private final void checkForResize() {
1906 <        Node[] tab; int n, sc;
1907 <        while ((tab = table) != null &&
1908 <               (n = tab.length) < MAXIMUM_CAPACITY &&
1909 <               (sc = sizeCtl) >= 0 && counter.sum() >= (long)sc &&
1910 <               UNSAFE.compareAndSwapInt(this, sizeCtlOffset, sc, -1)) {
1911 <            try {
1912 <                if (tab == table) {
1913 <                    table = rebuild(tab);
1914 <                    sc = (n << 1) - (n >>> 1);
1900 >     * Adds to count, and if table is too small and not already
1901 >     * resizing, initiates transfer. If already resizing, helps
1902 >     * perform transfer if work is available.  Rechecks occupancy
1903 >     * after a transfer to see if another resize is already needed
1904 >     * because resizings are lagging additions.
1905 >     *
1906 >     * @param x the count to add
1907 >     * @param check if <0, don't check resize, if <= 1 only check if uncontended
1908 >     */
1909 >    private final void addCount(long x, int check) {
1910 >        CounterCell[] as; long b, s;
1911 >        if ((as = counterCells) != null ||
1912 >            !U.compareAndSwapLong(this, BASECOUNT, b = baseCount, s = b + x)) {
1913 >            CounterHashCode hc; CounterCell a; long v; int m;
1914 >            boolean uncontended = true;
1915 >            if ((hc = threadCounterHashCode.get()) == null ||
1916 >                as == null || (m = as.length - 1) < 0 ||
1917 >                (a = as[m & hc.code]) == null ||
1918 >                !(uncontended =
1919 >                  U.compareAndSwapLong(a, CELLVALUE, v = a.value, v + x))) {
1920 >                fullAddCount(x, hc, uncontended);
1921 >                return;
1922 >            }
1923 >            if (check <= 1)
1924 >                return;
1925 >            s = sumCount();
1926 >        }
1927 >        if (check >= 0) {
1928 >            Node<V>[] tab, nt; int sc;
1929 >            while (s >= (long)(sc = sizeCtl) && (tab = table) != null &&
1930 >                   tab.length < MAXIMUM_CAPACITY) {
1931 >                if (sc < 0) {
1932 >                    if (sc == -1 || transferIndex <= transferOrigin ||
1933 >                        (nt = nextTable) == null)
1934 >                        break;
1935 >                    if (U.compareAndSwapInt(this, SIZECTL, sc, sc - 1))
1936 >                        transfer(tab, nt);
1937                  }
1938 <            } finally {
1939 <                sizeCtl = sc;
1938 >                else if (U.compareAndSwapInt(this, SIZECTL, sc, -2))
1939 >                    transfer(tab, null);
1940 >                s = sumCount();
1941              }
1942          }
1943      }
# Line 1948 | Line 1947 | public class ConcurrentHashMapV8<K, V>
1947       *
1948       * @param size number of elements (doesn't need to be perfectly accurate)
1949       */
1950 <    private final void tryPresize(int size) {
1950 >    @SuppressWarnings("unchecked") private final void tryPresize(int size) {
1951          int c = (size >= (MAXIMUM_CAPACITY >>> 1)) ? MAXIMUM_CAPACITY :
1952              tableSizeFor(size + (size >>> 1) + 1);
1953          int sc;
1954          while ((sc = sizeCtl) >= 0) {
1955 <            Node[] tab = table; int n;
1955 >            Node<V>[] tab = table; int n;
1956              if (tab == null || (n = tab.length) == 0) {
1957                  n = (sc > c) ? sc : c;
1958 <                if (UNSAFE.compareAndSwapInt(this, sizeCtlOffset, sc, -1)) {
1958 >                if (U.compareAndSwapInt(this, SIZECTL, sc, -1)) {
1959                      try {
1960                          if (table == tab) {
1961 <                            table = new Node[n];
1961 >                            @SuppressWarnings("rawtypes") Node[] tb = new Node[n];
1962 >                            table = (Node<V>[])tb;
1963                              sc = n - (n >>> 2);
1964                          }
1965                      } finally {
# Line 1969 | Line 1969 | public class ConcurrentHashMapV8<K, V>
1969              }
1970              else if (c <= sc || n >= MAXIMUM_CAPACITY)
1971                  break;
1972 <            else if (UNSAFE.compareAndSwapInt(this, sizeCtlOffset, sc, -1)) {
1973 <                try {
1974 <                    if (table == tab) {
1975 <                        table = rebuild(tab);
1976 <                        sc = (n << 1) - (n >>> 1);
1977 <                    }
1978 <                } finally {
1979 <                    sizeCtl = sc;
1980 <                }
1981 <            }
1972 >            else if (tab == table &&
1973 >                     U.compareAndSwapInt(this, SIZECTL, sc, -2))
1974 >                transfer(tab, null);
1975          }
1976      }
1977  
1978      /*
1979       * Moves and/or copies the nodes in each bin to new table. See
1980       * above for explanation.
1988     *
1989     * @return the new table
1981       */
1982 <    private static final Node[] rebuild(Node[] tab) {
1983 <        int n = tab.length;
1984 <        Node[] nextTab = new Node[n << 1];
1985 <        Node fwd = new Node(MOVED, nextTab, null, null);
1986 <        int[] buffer = null;       // holds bins to revisit; null until needed
1987 <        Node rev = null;           // reverse forwarder; null until needed
1988 <        int nbuffered = 0;         // the number of bins in buffer list
1989 <        int bufferIndex = 0;       // buffer index of current buffered bin
1990 <        int bin = n - 1;           // current non-buffered bin or -1 if none
1991 <
1992 <        for (int i = bin;;) {      // start upwards sweep
1993 <            int fh; Node f;
1994 <            if ((f = tabAt(tab, i)) == null) {
1995 <                if (bin >= 0) {    // no lock needed (or available)
1996 <                    if (!casTabAt(tab, i, f, fwd))
1997 <                        continue;
1998 <                }
1999 <                else {             // transiently use a locked forwarding node
2000 <                    Node g = new Node(MOVED|LOCKED, nextTab, null, null);
2001 <                    if (!casTabAt(tab, i, f, g))
2002 <                        continue;
1982 >    @SuppressWarnings("unchecked") private final void transfer
1983 >        (Node<V>[] tab, Node<V>[] nextTab) {
1984 >        int n = tab.length, stride;
1985 >        if ((stride = (NCPU > 1) ? (n >>> 3) / NCPU : n) < MIN_TRANSFER_STRIDE)
1986 >            stride = MIN_TRANSFER_STRIDE; // subdivide range
1987 >        if (nextTab == null) {            // initiating
1988 >            try {
1989 >                @SuppressWarnings("rawtypes") Node[] tb = new Node[n << 1];
1990 >                nextTab = (Node<V>[])tb;
1991 >            } catch (Throwable ex) {      // try to cope with OOME
1992 >                sizeCtl = Integer.MAX_VALUE;
1993 >                return;
1994 >            }
1995 >            nextTable = nextTab;
1996 >            transferOrigin = n;
1997 >            transferIndex = n;
1998 >            Node<V> rev = new Node<V>(MOVED, tab, null, null);
1999 >            for (int k = n; k > 0;) {    // progressively reveal ready slots
2000 >                int nextk = (k > stride) ? k - stride : 0;
2001 >                for (int m = nextk; m < k; ++m)
2002 >                    nextTab[m] = rev;
2003 >                for (int m = n + nextk; m < n + k; ++m)
2004 >                    nextTab[m] = rev;
2005 >                U.putOrderedInt(this, TRANSFERORIGIN, k = nextk);
2006 >            }
2007 >        }
2008 >        int nextn = nextTab.length;
2009 >        Node<V> fwd = new Node<V>(MOVED, nextTab, null, null);
2010 >        boolean advance = true;
2011 >        for (int i = 0, bound = 0;;) {
2012 >            int nextIndex, nextBound; Node<V> f; Object fk;
2013 >            while (advance) {
2014 >                if (--i >= bound)
2015 >                    advance = false;
2016 >                else if ((nextIndex = transferIndex) <= transferOrigin) {
2017 >                    i = -1;
2018 >                    advance = false;
2019 >                }
2020 >                else if (U.compareAndSwapInt
2021 >                         (this, TRANSFERINDEX, nextIndex,
2022 >                          nextBound = (nextIndex > stride ?
2023 >                                       nextIndex - stride : 0))) {
2024 >                    bound = nextBound;
2025 >                    i = nextIndex - 1;
2026 >                    advance = false;
2027 >                }
2028 >            }
2029 >            if (i < 0 || i >= n || i + n >= nextn) {
2030 >                for (int sc;;) {
2031 >                    if (U.compareAndSwapInt(this, SIZECTL, sc = sizeCtl, ++sc)) {
2032 >                        if (sc == -1) {
2033 >                            nextTable = null;
2034 >                            table = nextTab;
2035 >                            sizeCtl = (n << 1) - (n >>> 1);
2036 >                        }
2037 >                        return;
2038 >                    }
2039 >                }
2040 >            }
2041 >            else if ((f = tabAt(tab, i)) == null) {
2042 >                if (casTabAt(tab, i, null, fwd)) {
2043                      setTabAt(nextTab, i, null);
2044                      setTabAt(nextTab, i + n, null);
2045 <                    setTabAt(tab, i, fwd);
2015 <                    if (!g.casHash(MOVED|LOCKED, MOVED)) {
2016 <                        g.hash = MOVED;
2017 <                        synchronized (g) { g.notifyAll(); }
2018 <                    }
2045 >                    advance = true;
2046                  }
2047              }
2048 <            else if ((fh = f.hash) == MOVED) {
2049 <                Object fk = f.key;
2050 <                if (fk instanceof TreeBin) {
2051 <                    TreeBin t = (TreeBin)fk;
2052 <                    boolean validated = false;
2053 <                    t.acquire(0);
2054 <                    try {
2055 <                        if (tabAt(tab, i) == f) {
2056 <                            validated = true;
2057 <                            splitTreeBin(nextTab, i, t);
2058 <                            setTabAt(tab, i, fwd);
2048 >            else if (f.hash >= 0) {
2049 >                synchronized (f) {
2050 >                    if (tabAt(tab, i) == f) {
2051 >                        int runBit = f.hash & n;
2052 >                        Node<V> lastRun = f, lo = null, hi = null;
2053 >                        for (Node<V> p = f.next; p != null; p = p.next) {
2054 >                            int b = p.hash & n;
2055 >                            if (b != runBit) {
2056 >                                runBit = b;
2057 >                                lastRun = p;
2058 >                            }
2059                          }
2060 <                    } finally {
2061 <                        t.release(0);
2060 >                        if (runBit == 0)
2061 >                            lo = lastRun;
2062 >                        else
2063 >                            hi = lastRun;
2064 >                        for (Node<V> p = f; p != lastRun; p = p.next) {
2065 >                            int ph = p.hash;
2066 >                            Object pk = p.key; V pv = p.val;
2067 >                            if ((ph & n) == 0)
2068 >                                lo = new Node<V>(ph, pk, pv, lo);
2069 >                            else
2070 >                                hi = new Node<V>(ph, pk, pv, hi);
2071 >                        }
2072 >                        setTabAt(nextTab, i, lo);
2073 >                        setTabAt(nextTab, i + n, hi);
2074 >                        setTabAt(tab, i, fwd);
2075 >                        advance = true;
2076                      }
2036                    if (!validated)
2037                        continue;
2077                  }
2078              }
2079 <            else if ((fh & LOCKED) == 0 && f.casHash(fh, fh|LOCKED)) {
2080 <                boolean validated = false;
2081 <                try {              // split to lo and hi lists; copying as needed
2079 >            else if ((fk = f.key) instanceof TreeBin) {
2080 >                TreeBin<V> t = (TreeBin<V>)fk;
2081 >                t.acquire(0);
2082 >                try {
2083                      if (tabAt(tab, i) == f) {
2084 <                        validated = true;
2085 <                        splitBin(nextTab, i, f);
2084 >                        TreeBin<V> lt = new TreeBin<V>();
2085 >                        TreeBin<V> ht = new TreeBin<V>();
2086 >                        int lc = 0, hc = 0;
2087 >                        for (Node<V> e = t.first; e != null; e = e.next) {
2088 >                            int h = e.hash;
2089 >                            Object k = e.key; V v = e.val;
2090 >                            if ((h & n) == 0) {
2091 >                                ++lc;
2092 >                                lt.putTreeNode(h, k, v);
2093 >                            }
2094 >                            else {
2095 >                                ++hc;
2096 >                                ht.putTreeNode(h, k, v);
2097 >                            }
2098 >                        }
2099 >                        Node<V> ln, hn; // throw away trees if too small
2100 >                        if (lc < TREE_THRESHOLD) {
2101 >                            ln = null;
2102 >                            for (Node<V> p = lt.first; p != null; p = p.next)
2103 >                                ln = new Node<V>(p.hash, p.key, p.val, ln);
2104 >                        }
2105 >                        else
2106 >                            ln = new Node<V>(MOVED, lt, null, null);
2107 >                        setTabAt(nextTab, i, ln);
2108 >                        if (hc < TREE_THRESHOLD) {
2109 >                            hn = null;
2110 >                            for (Node<V> p = ht.first; p != null; p = p.next)
2111 >                                hn = new Node<V>(p.hash, p.key, p.val, hn);
2112 >                        }
2113 >                        else
2114 >                            hn = new Node<V>(MOVED, ht, null, null);
2115 >                        setTabAt(nextTab, i + n, hn);
2116                          setTabAt(tab, i, fwd);
2117 +                        advance = true;
2118                      }
2119                  } finally {
2120 <                    if (!f.casHash(fh | LOCKED, fh)) {
2050 <                        f.hash = fh;
2051 <                        synchronized (f) { f.notifyAll(); };
2052 <                    }
2120 >                    t.release(0);
2121                  }
2054                if (!validated)
2055                    continue;
2056            }
2057            else {
2058                if (buffer == null) // initialize buffer for revisits
2059                    buffer = new int[TRANSFER_BUFFER_SIZE];
2060                if (bin < 0 && bufferIndex > 0) {
2061                    int j = buffer[--bufferIndex];
2062                    buffer[bufferIndex] = i;
2063                    i = j;         // swap with another bin
2064                    continue;
2065                }
2066                if (bin < 0 || nbuffered >= TRANSFER_BUFFER_SIZE) {
2067                    f.tryAwaitLock(tab, i);
2068                    continue;      // no other options -- block
2069                }
2070                if (rev == null)   // initialize reverse-forwarder
2071                    rev = new Node(MOVED, tab, null, null);
2072                if (tabAt(tab, i) != f || (f.hash & LOCKED) == 0)
2073                    continue;      // recheck before adding to list
2074                buffer[nbuffered++] = i;
2075                setTabAt(nextTab, i, rev);     // install place-holders
2076                setTabAt(nextTab, i + n, rev);
2077            }
2078
2079            if (bin > 0)
2080                i = --bin;
2081            else if (buffer != null && nbuffered > 0) {
2082                bin = -1;
2083                i = buffer[bufferIndex = --nbuffered];
2122              }
2123              else
2124 <                return nextTab;
2124 >                advance = true; // already processed
2125          }
2126      }
2127  
2128 <    /**
2129 <     * Splits a normal bin with list headed by e into lo and hi parts;
2130 <     * installs in given table.
2131 <     */
2132 <    private static void splitBin(Node[] nextTab, int i, Node e) {
2133 <        int bit = nextTab.length >>> 1; // bit to split on
2134 <        int runBit = e.hash & bit;
2135 <        Node lastRun = e, lo = null, hi = null;
2136 <        for (Node p = e.next; p != null; p = p.next) {
2099 <            int b = p.hash & bit;
2100 <            if (b != runBit) {
2101 <                runBit = b;
2102 <                lastRun = p;
2128 >    /* ---------------- Counter support -------------- */
2129 >
2130 >    final long sumCount() {
2131 >        CounterCell[] as = counterCells; CounterCell a;
2132 >        long sum = baseCount;
2133 >        if (as != null) {
2134 >            for (int i = 0; i < as.length; ++i) {
2135 >                if ((a = as[i]) != null)
2136 >                    sum += a.value;
2137              }
2138          }
2139 <        if (runBit == 0)
2106 <            lo = lastRun;
2107 <        else
2108 <            hi = lastRun;
2109 <        for (Node p = e; p != lastRun; p = p.next) {
2110 <            int ph = p.hash & HASH_BITS;
2111 <            Object pk = p.key, pv = p.val;
2112 <            if ((ph & bit) == 0)
2113 <                lo = new Node(ph, pk, pv, lo);
2114 <            else
2115 <                hi = new Node(ph, pk, pv, hi);
2116 <        }
2117 <        setTabAt(nextTab, i, lo);
2118 <        setTabAt(nextTab, i + bit, hi);
2139 >        return sum;
2140      }
2141  
2142 <    /**
2143 <     * Splits a tree bin into lo and hi parts; installs in given table.
2144 <     */
2145 <    private static void splitTreeBin(Node[] nextTab, int i, TreeBin t) {
2146 <        int bit = nextTab.length >>> 1;
2147 <        TreeBin lt = new TreeBin();
2148 <        TreeBin ht = new TreeBin();
2149 <        int lc = 0, hc = 0;
2150 <        for (Node e = t.first; e != null; e = e.next) {
2130 <            int h = e.hash & HASH_BITS;
2131 <            Object k = e.key, v = e.val;
2132 <            if ((h & bit) == 0) {
2133 <                ++lc;
2134 <                lt.putTreeNode(h, k, v);
2135 <            }
2136 <            else {
2137 <                ++hc;
2138 <                ht.putTreeNode(h, k, v);
2139 <            }
2140 <        }
2141 <        Node ln, hn; // throw away trees if too small
2142 <        if (lc <= (TREE_THRESHOLD >>> 1)) {
2143 <            ln = null;
2144 <            for (Node p = lt.first; p != null; p = p.next)
2145 <                ln = new Node(p.hash, p.key, p.val, ln);
2142 >    // See LongAdder version for explanation
2143 >    private final void fullAddCount(long x, CounterHashCode hc,
2144 >                                    boolean wasUncontended) {
2145 >        int h;
2146 >        if (hc == null) {
2147 >            hc = new CounterHashCode();
2148 >            int s = counterHashCodeGenerator.addAndGet(SEED_INCREMENT);
2149 >            h = hc.code = (s == 0) ? 1 : s; // Avoid zero
2150 >            threadCounterHashCode.set(hc);
2151          }
2152          else
2153 <            ln = new Node(MOVED, lt, null, null);
2154 <        setTabAt(nextTab, i, ln);
2155 <        if (hc <= (TREE_THRESHOLD >>> 1)) {
2156 <            hn = null;
2157 <            for (Node p = ht.first; p != null; p = p.next)
2158 <                hn = new Node(p.hash, p.key, p.val, hn);
2159 <        }
2160 <        else
2161 <            hn = new Node(MOVED, ht, null, null);
2162 <        setTabAt(nextTab, i + bit, hn);
2163 <    }
2164 <
2165 <    /**
2166 <     * Implementation for clear. Steps through each bin, removing all
2167 <     * nodes.
2168 <     */
2169 <    private final void internalClear() {
2170 <        long delta = 0L; // negative number of deletions
2171 <        int i = 0;
2172 <        Node[] tab = table;
2173 <        while (tab != null && i < tab.length) {
2169 <            int fh; Object fk;
2170 <            Node f = tabAt(tab, i);
2171 <            if (f == null)
2172 <                ++i;
2173 <            else if ((fh = f.hash) == MOVED) {
2174 <                if ((fk = f.key) instanceof TreeBin) {
2175 <                    TreeBin t = (TreeBin)fk;
2176 <                    t.acquire(0);
2177 <                    try {
2178 <                        if (tabAt(tab, i) == f) {
2179 <                            for (Node p = t.first; p != null; p = p.next) {
2180 <                                p.val = null;
2181 <                                --delta;
2153 >            h = hc.code;
2154 >        boolean collide = false;                // True if last slot nonempty
2155 >        for (;;) {
2156 >            CounterCell[] as; CounterCell a; int n; long v;
2157 >            if ((as = counterCells) != null && (n = as.length) > 0) {
2158 >                if ((a = as[(n - 1) & h]) == null) {
2159 >                    if (counterBusy == 0) {            // Try to attach new Cell
2160 >                        CounterCell r = new CounterCell(x); // Optimistic create
2161 >                        if (counterBusy == 0 &&
2162 >                            U.compareAndSwapInt(this, COUNTERBUSY, 0, 1)) {
2163 >                            boolean created = false;
2164 >                            try {               // Recheck under lock
2165 >                                CounterCell[] rs; int m, j;
2166 >                                if ((rs = counterCells) != null &&
2167 >                                    (m = rs.length) > 0 &&
2168 >                                    rs[j = (m - 1) & h] == null) {
2169 >                                    rs[j] = r;
2170 >                                    created = true;
2171 >                                }
2172 >                            } finally {
2173 >                                counterBusy = 0;
2174                              }
2175 <                            t.first = null;
2176 <                            t.root = null;
2177 <                            ++i;
2175 >                            if (created)
2176 >                                break;
2177 >                            continue;           // Slot is now non-empty
2178                          }
2187                    } finally {
2188                        t.release(0);
2179                      }
2180 +                    collide = false;
2181                  }
2182 <                else
2183 <                    tab = (Node[])fk;
2184 <            }
2185 <            else if ((fh & LOCKED) != 0) {
2186 <                counter.add(delta); // opportunistically update count
2187 <                delta = 0L;
2188 <                f.tryAwaitLock(tab, i);
2189 <            }
2190 <            else if (f.casHash(fh, fh | LOCKED)) {
2191 <                try {
2192 <                    if (tabAt(tab, i) == f) {
2193 <                        for (Node e = f; e != null; e = e.next) {
2194 <                            e.val = null;
2195 <                            --delta;
2182 >                else if (!wasUncontended)       // CAS already known to fail
2183 >                    wasUncontended = true;      // Continue after rehash
2184 >                else if (U.compareAndSwapLong(a, CELLVALUE, v = a.value, v + x))
2185 >                    break;
2186 >                else if (counterCells != as || n >= NCPU)
2187 >                    collide = false;            // At max size or stale
2188 >                else if (!collide)
2189 >                    collide = true;
2190 >                else if (counterBusy == 0 &&
2191 >                         U.compareAndSwapInt(this, COUNTERBUSY, 0, 1)) {
2192 >                    try {
2193 >                        if (counterCells == as) {// Expand table unless stale
2194 >                            CounterCell[] rs = new CounterCell[n << 1];
2195 >                            for (int i = 0; i < n; ++i)
2196 >                                rs[i] = as[i];
2197 >                            counterCells = rs;
2198                          }
2199 <                        setTabAt(tab, i, null);
2200 <                        ++i;
2199 >                    } finally {
2200 >                        counterBusy = 0;
2201                      }
2202 <                } finally {
2203 <                    if (!f.casHash(fh | LOCKED, fh)) {
2204 <                        f.hash = fh;
2205 <                        synchronized (f) { f.notifyAll(); };
2202 >                    collide = false;
2203 >                    continue;                   // Retry with expanded table
2204 >                }
2205 >                h ^= h << 13;                   // Rehash
2206 >                h ^= h >>> 17;
2207 >                h ^= h << 5;
2208 >            }
2209 >            else if (counterBusy == 0 && counterCells == as &&
2210 >                     U.compareAndSwapInt(this, COUNTERBUSY, 0, 1)) {
2211 >                boolean init = false;
2212 >                try {                           // Initialize table
2213 >                    if (counterCells == as) {
2214 >                        CounterCell[] rs = new CounterCell[2];
2215 >                        rs[h & 1] = new CounterCell(x);
2216 >                        counterCells = rs;
2217 >                        init = true;
2218                      }
2219 +                } finally {
2220 +                    counterBusy = 0;
2221                  }
2222 +                if (init)
2223 +                    break;
2224              }
2225 +            else if (U.compareAndSwapLong(this, BASECOUNT, v = baseCount, v + x))
2226 +                break;                          // Fall back on using base
2227          }
2228 <        if (delta != 0)
2218 <            counter.add(delta);
2228 >        hc.code = h;                            // Record index for next time
2229      }
2230  
2231      /* ----------------Table Traversal -------------- */
2232  
2233      /**
2234       * Encapsulates traversal for methods such as containsValue; also
2235 <     * serves as a base class for other iterators.
2235 >     * serves as a base class for other iterators and bulk tasks.
2236       *
2237       * At each step, the iterator snapshots the key ("nextKey") and
2238       * value ("nextVal") of a valid node (i.e., one that, at point of
# Line 2230 | Line 2240 | public class ConcurrentHashMapV8<K, V>
2240       * change (including to null, indicating deletion), field nextVal
2241       * might not be accurate at point of use, but still maintains the
2242       * weak consistency property of holding a value that was once
2243 <     * valid.
2243 >     * valid. To support iterator.remove, the nextKey field is not
2244 >     * updated (nulled out) when the iterator cannot advance.
2245       *
2246       * Internal traversals directly access these fields, as in:
2247       * {@code while (it.advance() != null) { process(it.nextKey); }}
# Line 2257 | Line 2268 | public class ConcurrentHashMapV8<K, V>
2268       * across threads, iteration terminates if a bounds checks fails
2269       * for a table read.
2270       *
2271 <     * This class extends ForkJoinTask to streamline parallel
2272 <     * iteration in bulk operations (see BulkTask). This adds only an
2273 <     * int of space overhead, which is close enough to negligible in
2274 <     * cases where it is not needed to not worry about it.
2271 >     * This class extends CountedCompleter to streamline parallel
2272 >     * iteration in bulk operations. This adds only a few fields of
2273 >     * space overhead, which is small enough in cases where it is not
2274 >     * needed to not worry about it.  Because CountedCompleter is
2275 >     * Serializable, but iterators need not be, we need to add warning
2276 >     * suppressions.
2277       */
2278 <    static class Traverser<K,V,R> extends ForkJoinTask<R> {
2278 >    @SuppressWarnings("serial") static class Traverser<K,V,R>
2279 >        extends CountedCompleter<R> {
2280          final ConcurrentHashMapV8<K, V> map;
2281 <        Node next;           // the next entry to use
2268 <        Node last;           // the last entry used
2281 >        Node<V> next;        // the next entry to use
2282          Object nextKey;      // cached key field of next
2283 <        Object nextVal;      // cached val field of next
2284 <        Node[] tab;          // current table; updated if resized
2283 >        V nextVal;           // cached val field of next
2284 >        Node<V>[] tab;       // current table; updated if resized
2285          int index;           // index of bin to use next
2286          int baseIndex;       // current index of initial table
2287          int baseLimit;       // index bound for initial table
2288 <        final int baseSize;  // initial table size
2288 >        int baseSize;        // initial table size
2289 >        int batch;           // split control
2290  
2291          /** Creates iterator for all entries in the table. */
2292          Traverser(ConcurrentHashMapV8<K, V> map) {
2293 <            this.tab = (this.map = map).table;
2280 <            baseLimit = baseSize = (tab == null) ? 0 : tab.length;
2293 >            this.map = map;
2294          }
2295  
2296 <        /** Creates iterator for split() methods */
2297 <        Traverser(Traverser<K,V,?> it, boolean split) {
2298 <            this.map = it.map;
2299 <            this.tab = it.tab;
2300 <            this.baseSize = it.baseSize;
2301 <            int lo = it.baseIndex;
2302 <            int hi = this.baseLimit = it.baseLimit;
2303 <            int i;
2304 <            if (split) // adjust parent
2305 <                i = it.baseLimit = (lo + hi + 1) >>> 1;
2306 <            else       // clone parent
2307 <                i = lo;
2308 <            this.index = this.baseIndex = i;
2296 >        /** Creates iterator for split() methods and task constructors */
2297 >        Traverser(ConcurrentHashMapV8<K,V> map, Traverser<K,V,?> it, int batch) {
2298 >            super(it);
2299 >            this.batch = batch;
2300 >            if ((this.map = map) != null && it != null) { // split parent
2301 >                Node<V>[] t;
2302 >                if ((t = it.tab) == null &&
2303 >                    (t = it.tab = map.table) != null)
2304 >                    it.baseLimit = it.baseSize = t.length;
2305 >                this.tab = t;
2306 >                this.baseSize = it.baseSize;
2307 >                int hi = this.baseLimit = it.baseLimit;
2308 >                it.baseLimit = this.index = this.baseIndex =
2309 >                    (hi + it.baseIndex + 1) >>> 1;
2310 >            }
2311          }
2312  
2313          /**
2314           * Advances next; returns nextVal or null if terminated.
2315           * See above for explanation.
2316           */
2317 <        final Object advance() {
2318 <            Node e = last = next;
2319 <            Object ev = null;
2317 >        @SuppressWarnings("unchecked") final V advance() {
2318 >            Node<V> e = next;
2319 >            V ev = null;
2320              outer: do {
2321                  if (e != null)                  // advance past used/skipped node
2322                      e = e.next;
2323                  while (e == null) {             // get to next non-null bin
2324 <                    Node[] t; int b, i, n; Object ek; // checks must use locals
2325 <                    if ((b = baseIndex) >= baseLimit || (i = index) < 0 ||
2326 <                        (t = tab) == null || i >= (n = t.length))
2324 >                    ConcurrentHashMapV8<K, V> m;
2325 >                    Node<V>[] t; int b, i, n; Object ek; //  must use locals
2326 >                    if ((t = tab) != null)
2327 >                        n = t.length;
2328 >                    else if ((m = map) != null && (t = tab = m.table) != null)
2329 >                        n = baseLimit = baseSize = t.length;
2330 >                    else
2331 >                        break outer;
2332 >                    if ((b = baseIndex) >= baseLimit ||
2333 >                        (i = index) < 0 || i >= n)
2334                          break outer;
2335 <                    else if ((e = tabAt(t, i)) != null && e.hash == MOVED) {
2335 >                    if ((e = tabAt(t, i)) != null && e.hash < 0) {
2336                          if ((ek = e.key) instanceof TreeBin)
2337 <                            e = ((TreeBin)ek).first;
2337 >                            e = ((TreeBin<V>)ek).first;
2338                          else {
2339 <                            tab = (Node[])ek;
2339 >                            tab = (Node<V>[])ek;
2340                              continue;           // restarts due to null val
2341                          }
2342                      }                           // visit upper slots if present
# Line 2327 | Line 2349 | public class ConcurrentHashMapV8<K, V>
2349          }
2350  
2351          public final void remove() {
2352 <            if (nextVal == null)
2353 <                advance();
2332 <            Node e = last;
2333 <            if (e == null)
2352 >            Object k = nextKey;
2353 >            if (k == null && (advance() == null || (k = nextKey) == null))
2354                  throw new IllegalStateException();
2355 <            last = null;
2336 <            map.remove(e.key);
2355 >            map.internalReplace(k, null, null);
2356          }
2357  
2358          public final boolean hasNext() {
# Line 2341 | Line 2360 | public class ConcurrentHashMapV8<K, V>
2360          }
2361  
2362          public final boolean hasMoreElements() { return hasNext(); }
2363 <        public final void setRawResult(Object x) { }
2364 <        public R getRawResult() { return null; }
2365 <        public boolean exec() { return true; }
2363 >
2364 >        public void compute() { } // default no-op CountedCompleter body
2365 >
2366 >        /**
2367 >         * Returns a batch value > 0 if this task should (and must) be
2368 >         * split, if so, adding to pending count, and in any case
2369 >         * updating batch value. The initial batch value is approx
2370 >         * exp2 of the number of times (minus one) to split task by
2371 >         * two before executing leaf action. This value is faster to
2372 >         * compute and more convenient to use as a guide to splitting
2373 >         * than is the depth, since it is used while dividing by two
2374 >         * anyway.
2375 >         */
2376 >        final int preSplit() {
2377 >            ConcurrentHashMapV8<K, V> m; int b; Node<V>[] t;  ForkJoinPool pool;
2378 >            if ((b = batch) < 0 && (m = map) != null) { // force initialization
2379 >                if ((t = tab) == null && (t = tab = m.table) != null)
2380 >                    baseLimit = baseSize = t.length;
2381 >                if (t != null) {
2382 >                    long n = m.sumCount();
2383 >                    int par = ((pool = getPool()) == null) ?
2384 >                        ForkJoinPool.getCommonPoolParallelism() :
2385 >                        pool.getParallelism();
2386 >                    int sp = par << 3; // slack of 8
2387 >                    b = (n <= 0L) ? 0 : (n < (long)sp) ? (int)n : sp;
2388 >                }
2389 >            }
2390 >            b = (b <= 1 || baseIndex == baseLimit) ? 0 : (b >>> 1);
2391 >            if ((batch = b) > 0)
2392 >                addToPendingCount(1);
2393 >            return b;
2394 >        }
2395 >
2396      }
2397  
2398      /* ---------------- Public operations -------------- */
# Line 2352 | Line 2401 | public class ConcurrentHashMapV8<K, V>
2401       * Creates a new, empty map with the default initial table size (16).
2402       */
2403      public ConcurrentHashMapV8() {
2355        this.counter = new LongAdder();
2404      }
2405  
2406      /**
# Line 2371 | Line 2419 | public class ConcurrentHashMapV8<K, V>
2419          int cap = ((initialCapacity >= (MAXIMUM_CAPACITY >>> 1)) ?
2420                     MAXIMUM_CAPACITY :
2421                     tableSizeFor(initialCapacity + (initialCapacity >>> 1) + 1));
2374        this.counter = new LongAdder();
2422          this.sizeCtl = cap;
2423      }
2424  
# Line 2381 | Line 2428 | public class ConcurrentHashMapV8<K, V>
2428       * @param m the map
2429       */
2430      public ConcurrentHashMapV8(Map<? extends K, ? extends V> m) {
2384        this.counter = new LongAdder();
2431          this.sizeCtl = DEFAULT_CAPACITY;
2432          internalPutAll(m);
2433      }
# Line 2432 | Line 2478 | public class ConcurrentHashMapV8<K, V>
2478          long size = (long)(1.0 + (long)initialCapacity / loadFactor);
2479          int cap = (size >= (long)MAXIMUM_CAPACITY) ?
2480              MAXIMUM_CAPACITY : tableSizeFor((int)size);
2435        this.counter = new LongAdder();
2481          this.sizeCtl = cap;
2482      }
2483  
2484      /**
2485 +     * Creates a new {@link Set} backed by a ConcurrentHashMapV8
2486 +     * from the given type to {@code Boolean.TRUE}.
2487 +     *
2488 +     * @return the new set
2489 +     */
2490 +    public static <K> KeySetView<K,Boolean> newKeySet() {
2491 +        return new KeySetView<K,Boolean>(new ConcurrentHashMapV8<K,Boolean>(),
2492 +                                      Boolean.TRUE);
2493 +    }
2494 +
2495 +    /**
2496 +     * Creates a new {@link Set} backed by a ConcurrentHashMapV8
2497 +     * from the given type to {@code Boolean.TRUE}.
2498 +     *
2499 +     * @param initialCapacity The implementation performs internal
2500 +     * sizing to accommodate this many elements.
2501 +     * @throws IllegalArgumentException if the initial capacity of
2502 +     * elements is negative
2503 +     * @return the new set
2504 +     */
2505 +    public static <K> KeySetView<K,Boolean> newKeySet(int initialCapacity) {
2506 +        return new KeySetView<K,Boolean>
2507 +            (new ConcurrentHashMapV8<K,Boolean>(initialCapacity), Boolean.TRUE);
2508 +    }
2509 +
2510 +    /**
2511       * {@inheritDoc}
2512       */
2513      public boolean isEmpty() {
2514 <        return counter.sum() <= 0L; // ignore transient negative values
2514 >        return sumCount() <= 0L; // ignore transient negative values
2515      }
2516  
2517      /**
2518       * {@inheritDoc}
2519       */
2520      public int size() {
2521 <        long n = counter.sum();
2521 >        long n = sumCount();
2522          return ((n < 0L) ? 0 :
2523                  (n > (long)Integer.MAX_VALUE) ? Integer.MAX_VALUE :
2524                  (int)n);
# Line 2455 | Line 2526 | public class ConcurrentHashMapV8<K, V>
2526  
2527      /**
2528       * Returns the number of mappings. This method should be used
2529 <     * instead of {@link #size} because a ConcurrentHashMap may
2529 >     * instead of {@link #size} because a ConcurrentHashMapV8 may
2530       * contain more mappings than can be represented as an int. The
2531 <     * value returned is a snapshot; the actual count may differ if
2532 <     * there are ongoing concurrent insertions of removals.
2531 >     * value returned is an estimate; the actual count may differ if
2532 >     * there are concurrent insertions or removals.
2533       *
2534       * @return the number of mappings
2535       */
2536      public long mappingCount() {
2537 <        long n = counter.sum();
2538 <        return (n < 0L) ? 0L : n;
2537 >        long n = sumCount();
2538 >        return (n < 0L) ? 0L : n; // ignore transient negative values
2539      }
2540  
2541      /**
# Line 2478 | Line 2549 | public class ConcurrentHashMapV8<K, V>
2549       *
2550       * @throws NullPointerException if the specified key is null
2551       */
2552 <    @SuppressWarnings("unchecked")
2553 <        public V get(Object key) {
2554 <        if (key == null)
2555 <            throw new NullPointerException();
2556 <        return (V)internalGet(key);
2552 >    public V get(Object key) {
2553 >        return internalGet(key);
2554 >    }
2555 >
2556 >    /**
2557 >     * Returns the value to which the specified key is mapped,
2558 >     * or the given defaultValue if this map contains no mapping for the key.
2559 >     *
2560 >     * @param key the key
2561 >     * @param defaultValue the value to return if this map contains
2562 >     * no mapping for the given key
2563 >     * @return the mapping for the key, if present; else the defaultValue
2564 >     * @throws NullPointerException if the specified key is null
2565 >     */
2566 >    public V getValueOrDefault(Object key, V defaultValue) {
2567 >        V v;
2568 >        return (v = internalGet(key)) == null ? defaultValue : v;
2569      }
2570  
2571      /**
# Line 2495 | Line 2578 | public class ConcurrentHashMapV8<K, V>
2578       * @throws NullPointerException if the specified key is null
2579       */
2580      public boolean containsKey(Object key) {
2498        if (key == null)
2499            throw new NullPointerException();
2581          return internalGet(key) != null;
2582      }
2583  
# Line 2513 | Line 2594 | public class ConcurrentHashMapV8<K, V>
2594      public boolean containsValue(Object value) {
2595          if (value == null)
2596              throw new NullPointerException();
2597 <        Object v;
2597 >        V v;
2598          Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
2599          while ((v = it.advance()) != null) {
2600              if (v == value || value.equals(v))
# Line 2537 | Line 2618 | public class ConcurrentHashMapV8<K, V>
2618       *         {@code false} otherwise
2619       * @throws NullPointerException if the specified value is null
2620       */
2621 <    public boolean contains(Object value) {
2621 >    @Deprecated public boolean contains(Object value) {
2622          return containsValue(value);
2623      }
2624  
# Line 2545 | Line 2626 | public class ConcurrentHashMapV8<K, V>
2626       * Maps the specified key to the specified value in this table.
2627       * Neither the key nor the value can be null.
2628       *
2629 <     * <p> The value can be retrieved by calling the {@code get} method
2629 >     * <p>The value can be retrieved by calling the {@code get} method
2630       * with a key that is equal to the original key.
2631       *
2632       * @param key key with which the specified value is to be associated
# Line 2554 | Line 2635 | public class ConcurrentHashMapV8<K, V>
2635       *         {@code null} if there was no mapping for {@code key}
2636       * @throws NullPointerException if the specified key or value is null
2637       */
2638 <    @SuppressWarnings("unchecked")
2639 <        public V put(K key, V value) {
2559 <        if (key == null || value == null)
2560 <            throw new NullPointerException();
2561 <        return (V)internalPut(key, value);
2638 >    public V put(K key, V value) {
2639 >        return internalPut(key, value, false);
2640      }
2641  
2642      /**
# Line 2568 | Line 2646 | public class ConcurrentHashMapV8<K, V>
2646       *         or {@code null} if there was no mapping for the key
2647       * @throws NullPointerException if the specified key or value is null
2648       */
2649 <    @SuppressWarnings("unchecked")
2650 <        public V putIfAbsent(K key, V value) {
2573 <        if (key == null || value == null)
2574 <            throw new NullPointerException();
2575 <        return (V)internalPutIfAbsent(key, value);
2649 >    public V putIfAbsent(K key, V value) {
2650 >        return internalPut(key, value, true);
2651      }
2652  
2653      /**
# Line 2616 | Line 2691 | public class ConcurrentHashMapV8<K, V>
2691       * @param key key with which the specified value is to be associated
2692       * @param mappingFunction the function to compute a value
2693       * @return the current (existing or computed) value associated with
2694 <     *         the specified key, or null if the computed value is null.
2694 >     *         the specified key, or null if the computed value is null
2695       * @throws NullPointerException if the specified key or mappingFunction
2696       *         is null
2697       * @throws IllegalStateException if the computation detectably
# Line 2625 | Line 2700 | public class ConcurrentHashMapV8<K, V>
2700       * @throws RuntimeException or Error if the mappingFunction does so,
2701       *         in which case the mapping is left unestablished
2702       */
2703 <    @SuppressWarnings("unchecked")
2704 <        public V computeIfAbsent(K key, Fun<? super K, ? extends V> mappingFunction) {
2705 <        if (key == null || mappingFunction == null)
2631 <            throw new NullPointerException();
2632 <        return (V)internalComputeIfAbsent(key, mappingFunction);
2703 >    public V computeIfAbsent
2704 >        (K key, Fun<? super K, ? extends V> mappingFunction) {
2705 >        return internalComputeIfAbsent(key, mappingFunction);
2706      }
2707  
2708      /**
# Line 2657 | Line 2730 | public class ConcurrentHashMapV8<K, V>
2730       *
2731       * @param key key with which the specified value is to be associated
2732       * @param remappingFunction the function to compute a value
2733 <     * @return the new value associated with
2661 <     *         the specified key, or null if none.
2733 >     * @return the new value associated with the specified key, or null if none
2734       * @throws NullPointerException if the specified key or remappingFunction
2735       *         is null
2736       * @throws IllegalStateException if the computation detectably
# Line 2667 | Line 2739 | public class ConcurrentHashMapV8<K, V>
2739       * @throws RuntimeException or Error if the remappingFunction does so,
2740       *         in which case the mapping is unchanged
2741       */
2742 <    public V computeIfPresent(K key, BiFun<? super K, ? super V, ? extends V> remappingFunction) {
2743 <        if (key == null || remappingFunction == null)
2744 <            throw new NullPointerException();
2673 <        return (V)internalCompute(key, true, remappingFunction);
2742 >    public V computeIfPresent
2743 >        (K key, BiFun<? super K, ? super V, ? extends V> remappingFunction) {
2744 >        return internalCompute(key, true, remappingFunction);
2745      }
2746  
2747      /**
# Line 2704 | Line 2775 | public class ConcurrentHashMapV8<K, V>
2775       *
2776       * @param key key with which the specified value is to be associated
2777       * @param remappingFunction the function to compute a value
2778 <     * @return the new value associated with
2708 <     *         the specified key, or null if none.
2778 >     * @return the new value associated with the specified key, or null if none
2779       * @throws NullPointerException if the specified key or remappingFunction
2780       *         is null
2781       * @throws IllegalStateException if the computation detectably
# Line 2714 | Line 2784 | public class ConcurrentHashMapV8<K, V>
2784       * @throws RuntimeException or Error if the remappingFunction does so,
2785       *         in which case the mapping is unchanged
2786       */
2787 <    //    @SuppressWarnings("unchecked")
2788 <    public V compute(K key, BiFun<? super K, ? super V, ? extends V> remappingFunction) {
2789 <        if (key == null || remappingFunction == null)
2720 <            throw new NullPointerException();
2721 <        return (V)internalCompute(key, false, remappingFunction);
2787 >    public V compute
2788 >        (K key, BiFun<? super K, ? super V, ? extends V> remappingFunction) {
2789 >        return internalCompute(key, false, remappingFunction);
2790      }
2791  
2792      /**
# Line 2746 | Line 2814 | public class ConcurrentHashMapV8<K, V>
2814       * so the computation should be short and simple, and must not
2815       * attempt to update any other mappings of this Map.
2816       */
2817 <    //    @SuppressWarnings("unchecked")
2818 <    public V merge(K key, V value, BiFun<? super V, ? super V, ? extends V> remappingFunction) {
2819 <        if (key == null || value == null || remappingFunction == null)
2820 <            throw new NullPointerException();
2753 <        return (V)internalMerge(key, value, remappingFunction);
2817 >    public V merge
2818 >        (K key, V value,
2819 >         BiFun<? super V, ? super V, ? extends V> remappingFunction) {
2820 >        return internalMerge(key, value, remappingFunction);
2821      }
2822  
2823      /**
# Line 2762 | Line 2829 | public class ConcurrentHashMapV8<K, V>
2829       *         {@code null} if there was no mapping for {@code key}
2830       * @throws NullPointerException if the specified key is null
2831       */
2832 <    @SuppressWarnings("unchecked")
2833 <        public V remove(Object key) {
2767 <        if (key == null)
2768 <            throw new NullPointerException();
2769 <        return (V)internalReplace(key, null, null);
2832 >    public V remove(Object key) {
2833 >        return internalReplace(key, null, null);
2834      }
2835  
2836      /**
# Line 2775 | Line 2839 | public class ConcurrentHashMapV8<K, V>
2839       * @throws NullPointerException if the specified key is null
2840       */
2841      public boolean remove(Object key, Object value) {
2842 <        if (key == null)
2779 <            throw new NullPointerException();
2780 <        if (value == null)
2781 <            return false;
2782 <        return internalReplace(key, null, value) != null;
2842 >        return value != null && internalReplace(key, null, value) != null;
2843      }
2844  
2845      /**
# Line 2800 | Line 2860 | public class ConcurrentHashMapV8<K, V>
2860       *         or {@code null} if there was no mapping for the key
2861       * @throws NullPointerException if the specified key or value is null
2862       */
2863 <    @SuppressWarnings("unchecked")
2804 <        public V replace(K key, V value) {
2863 >    public V replace(K key, V value) {
2864          if (key == null || value == null)
2865              throw new NullPointerException();
2866 <        return (V)internalReplace(key, value, null);
2866 >        return internalReplace(key, value, null);
2867      }
2868  
2869      /**
# Line 2817 | Line 2876 | public class ConcurrentHashMapV8<K, V>
2876      /**
2877       * Returns a {@link Set} view of the keys contained in this map.
2878       * The set is backed by the map, so changes to the map are
2879 <     * reflected in the set, and vice-versa.  The set supports element
2821 <     * removal, which removes the corresponding mapping from this map,
2822 <     * via the {@code Iterator.remove}, {@code Set.remove},
2823 <     * {@code removeAll}, {@code retainAll}, and {@code clear}
2824 <     * operations.  It does not support the {@code add} or
2825 <     * {@code addAll} operations.
2879 >     * reflected in the set, and vice-versa.
2880       *
2881 <     * <p>The view's {@code iterator} is a "weakly consistent" iterator
2882 <     * that will never throw {@link ConcurrentModificationException},
2883 <     * and guarantees to traverse elements as they existed upon
2884 <     * construction of the iterator, and may (but is not guaranteed to)
2885 <     * reflect any modifications subsequent to construction.
2881 >     * @return the set view
2882 >     */
2883 >    public KeySetView<K,V> keySet() {
2884 >        KeySetView<K,V> ks = keySet;
2885 >        return (ks != null) ? ks : (keySet = new KeySetView<K,V>(this, null));
2886 >    }
2887 >
2888 >    /**
2889 >     * Returns a {@link Set} view of the keys in this map, using the
2890 >     * given common mapped value for any additions (i.e., {@link
2891 >     * Collection#add} and {@link Collection#addAll}). This is of
2892 >     * course only appropriate if it is acceptable to use the same
2893 >     * value for all additions from this view.
2894 >     *
2895 >     * @param mappedValue the mapped value to use for any
2896 >     * additions.
2897 >     * @return the set view
2898 >     * @throws NullPointerException if the mappedValue is null
2899       */
2900 <    public Set<K> keySet() {
2901 <        KeySet<K,V> ks = keySet;
2902 <        return (ks != null) ? ks : (keySet = new KeySet<K,V>(this));
2900 >    public KeySetView<K,V> keySet(V mappedValue) {
2901 >        if (mappedValue == null)
2902 >            throw new NullPointerException();
2903 >        return new KeySetView<K,V>(this, mappedValue);
2904      }
2905  
2906      /**
2907       * Returns a {@link Collection} view of the values contained in this map.
2908       * The collection is backed by the map, so changes to the map are
2909 <     * reflected in the collection, and vice-versa.  The collection
2842 <     * supports element removal, which removes the corresponding
2843 <     * mapping from this map, via the {@code Iterator.remove},
2844 <     * {@code Collection.remove}, {@code removeAll},
2845 <     * {@code retainAll}, and {@code clear} operations.  It does not
2846 <     * support the {@code add} or {@code addAll} operations.
2847 <     *
2848 <     * <p>The view's {@code iterator} is a "weakly consistent" iterator
2849 <     * that will never throw {@link ConcurrentModificationException},
2850 <     * and guarantees to traverse elements as they existed upon
2851 <     * construction of the iterator, and may (but is not guaranteed to)
2852 <     * reflect any modifications subsequent to construction.
2909 >     * reflected in the collection, and vice-versa.
2910       */
2911 <    public Collection<V> values() {
2912 <        Values<K,V> vs = values;
2913 <        return (vs != null) ? vs : (values = new Values<K,V>(this));
2911 >    public ValuesView<K,V> values() {
2912 >        ValuesView<K,V> vs = values;
2913 >        return (vs != null) ? vs : (values = new ValuesView<K,V>(this));
2914      }
2915  
2916      /**
# Line 2873 | Line 2930 | public class ConcurrentHashMapV8<K, V>
2930       * reflect any modifications subsequent to construction.
2931       */
2932      public Set<Map.Entry<K,V>> entrySet() {
2933 <        EntrySet<K,V> es = entrySet;
2934 <        return (es != null) ? es : (entrySet = new EntrySet<K,V>(this));
2933 >        EntrySetView<K,V> es = entrySet;
2934 >        return (es != null) ? es : (entrySet = new EntrySetView<K,V>(this));
2935      }
2936  
2937      /**
# Line 2898 | Line 2955 | public class ConcurrentHashMapV8<K, V>
2955      }
2956  
2957      /**
2958 <     * Returns a partionable iterator of the keys in this map.
2958 >     * Returns a partitionable iterator of the keys in this map.
2959       *
2960 <     * @return a partionable iterator of the keys in this map
2960 >     * @return a partitionable iterator of the keys in this map
2961       */
2962      public Spliterator<K> keySpliterator() {
2963          return new KeyIterator<K,V>(this);
2964      }
2965  
2966      /**
2967 <     * Returns a partionable iterator of the values in this map.
2967 >     * Returns a partitionable iterator of the values in this map.
2968       *
2969 <     * @return a partionable iterator of the values in this map
2969 >     * @return a partitionable iterator of the values in this map
2970       */
2971      public Spliterator<V> valueSpliterator() {
2972          return new ValueIterator<K,V>(this);
2973      }
2974  
2975      /**
2976 <     * Returns a partionable iterator of the entries in this map.
2976 >     * Returns a partitionable iterator of the entries in this map.
2977       *
2978 <     * @return a partionable iterator of the entries in this map
2978 >     * @return a partitionable iterator of the entries in this map
2979       */
2980      public Spliterator<Map.Entry<K,V>> entrySpliterator() {
2981          return new EntryIterator<K,V>(this);
# Line 2934 | Line 2991 | public class ConcurrentHashMapV8<K, V>
2991      public int hashCode() {
2992          int h = 0;
2993          Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
2994 <        Object v;
2994 >        V v;
2995          while ((v = it.advance()) != null) {
2996              h += it.nextKey.hashCode() ^ v.hashCode();
2997          }
# Line 2956 | Line 3013 | public class ConcurrentHashMapV8<K, V>
3013          Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
3014          StringBuilder sb = new StringBuilder();
3015          sb.append('{');
3016 <        Object v;
3016 >        V v;
3017          if ((v = it.advance()) != null) {
3018              for (;;) {
3019                  Object k = it.nextKey;
# Line 2987 | Line 3044 | public class ConcurrentHashMapV8<K, V>
3044                  return false;
3045              Map<?,?> m = (Map<?,?>) o;
3046              Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
3047 <            Object val;
3047 >            V val;
3048              while ((val = it.advance()) != null) {
3049                  Object v = m.get(it.nextKey);
3050                  if (v == null || (v != val && !v.equals(val)))
# Line 3007 | Line 3064 | public class ConcurrentHashMapV8<K, V>
3064  
3065      /* ----------------Iterators -------------- */
3066  
3067 <    static final class KeyIterator<K,V> extends Traverser<K,V,Object>
3067 >    @SuppressWarnings("serial") static final class KeyIterator<K,V>
3068 >        extends Traverser<K,V,Object>
3069          implements Spliterator<K>, Enumeration<K> {
3070          KeyIterator(ConcurrentHashMapV8<K, V> map) { super(map); }
3071 <        KeyIterator(Traverser<K,V,Object> it, boolean split) {
3072 <            super(it, split);
3071 >        KeyIterator(ConcurrentHashMapV8<K, V> map, Traverser<K,V,Object> it) {
3072 >            super(map, it, -1);
3073          }
3074          public KeyIterator<K,V> split() {
3075 <            if (last != null || (next != null && nextVal == null))
3075 >            if (nextKey != null)
3076                  throw new IllegalStateException();
3077 <            return new KeyIterator<K,V>(this, true);
3077 >            return new KeyIterator<K,V>(map, this);
3078          }
3079 <        @SuppressWarnings("unchecked")
3022 <            public final K next() {
3079 >        @SuppressWarnings("unchecked") public final K next() {
3080              if (nextVal == null && advance() == null)
3081                  throw new NoSuchElementException();
3082              Object k = nextKey;
# Line 3030 | Line 3087 | public class ConcurrentHashMapV8<K, V>
3087          public final K nextElement() { return next(); }
3088      }
3089  
3090 <    static final class ValueIterator<K,V> extends Traverser<K,V,Object>
3090 >    @SuppressWarnings("serial") static final class ValueIterator<K,V>
3091 >        extends Traverser<K,V,Object>
3092          implements Spliterator<V>, Enumeration<V> {
3093          ValueIterator(ConcurrentHashMapV8<K, V> map) { super(map); }
3094 <        ValueIterator(Traverser<K,V,Object> it, boolean split) {
3095 <            super(it, split);
3094 >        ValueIterator(ConcurrentHashMapV8<K, V> map, Traverser<K,V,Object> it) {
3095 >            super(map, it, -1);
3096          }
3097          public ValueIterator<K,V> split() {
3098 <            if (last != null || (next != null && nextVal == null))
3098 >            if (nextKey != null)
3099                  throw new IllegalStateException();
3100 <            return new ValueIterator<K,V>(this, true);
3100 >            return new ValueIterator<K,V>(map, this);
3101          }
3102  
3103 <        @SuppressWarnings("unchecked")
3104 <            public final V next() {
3047 <            Object v;
3103 >        public final V next() {
3104 >            V v;
3105              if ((v = nextVal) == null && (v = advance()) == null)
3106                  throw new NoSuchElementException();
3107              nextVal = null;
3108 <            return (V) v;
3108 >            return v;
3109          }
3110  
3111          public final V nextElement() { return next(); }
3112      }
3113  
3114 <    static final class EntryIterator<K,V> extends Traverser<K,V,Object>
3114 >    @SuppressWarnings("serial") static final class EntryIterator<K,V>
3115 >        extends Traverser<K,V,Object>
3116          implements Spliterator<Map.Entry<K,V>> {
3117          EntryIterator(ConcurrentHashMapV8<K, V> map) { super(map); }
3118 <        EntryIterator(Traverser<K,V,Object> it, boolean split) {
3119 <            super(it, split);
3118 >        EntryIterator(ConcurrentHashMapV8<K, V> map, Traverser<K,V,Object> it) {
3119 >            super(map, it, -1);
3120          }
3121          public EntryIterator<K,V> split() {
3122 <            if (last != null || (next != null && nextVal == null))
3122 >            if (nextKey != null)
3123                  throw new IllegalStateException();
3124 <            return new EntryIterator<K,V>(this, true);
3124 >            return new EntryIterator<K,V>(map, this);
3125          }
3126  
3127 <        @SuppressWarnings("unchecked")
3128 <            public final Map.Entry<K,V> next() {
3071 <            Object v;
3127 >        @SuppressWarnings("unchecked") public final Map.Entry<K,V> next() {
3128 >            V v;
3129              if ((v = nextVal) == null && (v = advance()) == null)
3130                  throw new NoSuchElementException();
3131              Object k = nextKey;
3132              nextVal = null;
3133 <            return new MapEntry<K,V>((K)k, (V)v, map);
3133 >            return new MapEntry<K,V>((K)k, v, map);
3134          }
3135      }
3136  
# Line 3120 | Line 3177 | public class ConcurrentHashMapV8<K, V>
3177          }
3178      }
3179  
3123    /* ----------------Views -------------- */
3124
3180      /**
3181 <     * Base class for views.
3181 >     * Returns exportable snapshot entry for the given key and value
3182 >     * when write-through can't or shouldn't be used.
3183       */
3184 <    static abstract class CHMView<K, V> {
3185 <        final ConcurrentHashMapV8<K, V> map;
3130 <        CHMView(ConcurrentHashMapV8<K, V> map)  { this.map = map; }
3131 <        public final int size()                 { return map.size(); }
3132 <        public final boolean isEmpty()          { return map.isEmpty(); }
3133 <        public final void clear()               { map.clear(); }
3134 <
3135 <        // implementations below rely on concrete classes supplying these
3136 <        abstract public Iterator<?> iterator();
3137 <        abstract public boolean contains(Object o);
3138 <        abstract public boolean remove(Object o);
3139 <
3140 <        private static final String oomeMsg = "Required array size too large";
3141 <
3142 <        public final Object[] toArray() {
3143 <            long sz = map.mappingCount();
3144 <            if (sz > (long)(MAX_ARRAY_SIZE))
3145 <                throw new OutOfMemoryError(oomeMsg);
3146 <            int n = (int)sz;
3147 <            Object[] r = new Object[n];
3148 <            int i = 0;
3149 <            Iterator<?> it = iterator();
3150 <            while (it.hasNext()) {
3151 <                if (i == n) {
3152 <                    if (n >= MAX_ARRAY_SIZE)
3153 <                        throw new OutOfMemoryError(oomeMsg);
3154 <                    if (n >= MAX_ARRAY_SIZE - (MAX_ARRAY_SIZE >>> 1) - 1)
3155 <                        n = MAX_ARRAY_SIZE;
3156 <                    else
3157 <                        n += (n >>> 1) + 1;
3158 <                    r = Arrays.copyOf(r, n);
3159 <                }
3160 <                r[i++] = it.next();
3161 <            }
3162 <            return (i == n) ? r : Arrays.copyOf(r, i);
3163 <        }
3164 <
3165 <        @SuppressWarnings("unchecked")
3166 <            public final <T> T[] toArray(T[] a) {
3167 <            long sz = map.mappingCount();
3168 <            if (sz > (long)(MAX_ARRAY_SIZE))
3169 <                throw new OutOfMemoryError(oomeMsg);
3170 <            int m = (int)sz;
3171 <            T[] r = (a.length >= m) ? a :
3172 <                (T[])java.lang.reflect.Array
3173 <                .newInstance(a.getClass().getComponentType(), m);
3174 <            int n = r.length;
3175 <            int i = 0;
3176 <            Iterator<?> it = iterator();
3177 <            while (it.hasNext()) {
3178 <                if (i == n) {
3179 <                    if (n >= MAX_ARRAY_SIZE)
3180 <                        throw new OutOfMemoryError(oomeMsg);
3181 <                    if (n >= MAX_ARRAY_SIZE - (MAX_ARRAY_SIZE >>> 1) - 1)
3182 <                        n = MAX_ARRAY_SIZE;
3183 <                    else
3184 <                        n += (n >>> 1) + 1;
3185 <                    r = Arrays.copyOf(r, n);
3186 <                }
3187 <                r[i++] = (T)it.next();
3188 <            }
3189 <            if (a == r && i < n) {
3190 <                r[i] = null; // null-terminate
3191 <                return r;
3192 <            }
3193 <            return (i == n) ? r : Arrays.copyOf(r, i);
3194 <        }
3195 <
3196 <        public final int hashCode() {
3197 <            int h = 0;
3198 <            for (Iterator<?> it = iterator(); it.hasNext();)
3199 <                h += it.next().hashCode();
3200 <            return h;
3201 <        }
3202 <
3203 <        public final String toString() {
3204 <            StringBuilder sb = new StringBuilder();
3205 <            sb.append('[');
3206 <            Iterator<?> it = iterator();
3207 <            if (it.hasNext()) {
3208 <                for (;;) {
3209 <                    Object e = it.next();
3210 <                    sb.append(e == this ? "(this Collection)" : e);
3211 <                    if (!it.hasNext())
3212 <                        break;
3213 <                    sb.append(',').append(' ');
3214 <                }
3215 <            }
3216 <            return sb.append(']').toString();
3217 <        }
3218 <
3219 <        public final boolean containsAll(Collection<?> c) {
3220 <            if (c != this) {
3221 <                for (Iterator<?> it = c.iterator(); it.hasNext();) {
3222 <                    Object e = it.next();
3223 <                    if (e == null || !contains(e))
3224 <                        return false;
3225 <                }
3226 <            }
3227 <            return true;
3228 <        }
3229 <
3230 <        public final boolean removeAll(Collection<?> c) {
3231 <            boolean modified = false;
3232 <            for (Iterator<?> it = iterator(); it.hasNext();) {
3233 <                if (c.contains(it.next())) {
3234 <                    it.remove();
3235 <                    modified = true;
3236 <                }
3237 <            }
3238 <            return modified;
3239 <        }
3240 <
3241 <        public final boolean retainAll(Collection<?> c) {
3242 <            boolean modified = false;
3243 <            for (Iterator<?> it = iterator(); it.hasNext();) {
3244 <                if (!c.contains(it.next())) {
3245 <                    it.remove();
3246 <                    modified = true;
3247 <                }
3248 <            }
3249 <            return modified;
3250 <        }
3251 <
3252 <    }
3253 <
3254 <    static final class KeySet<K,V> extends CHMView<K,V> implements Set<K> {
3255 <        KeySet(ConcurrentHashMapV8<K, V> map)  {
3256 <            super(map);
3257 <        }
3258 <        public final boolean contains(Object o) { return map.containsKey(o); }
3259 <        public final boolean remove(Object o)   { return map.remove(o) != null; }
3260 <        public final Iterator<K> iterator() {
3261 <            return new KeyIterator<K,V>(map);
3262 <        }
3263 <        public final boolean add(K e) {
3264 <            throw new UnsupportedOperationException();
3265 <        }
3266 <        public final boolean addAll(Collection<? extends K> c) {
3267 <            throw new UnsupportedOperationException();
3268 <        }
3269 <        public boolean equals(Object o) {
3270 <            Set<?> c;
3271 <            return ((o instanceof Set) &&
3272 <                    ((c = (Set<?>)o) == this ||
3273 <                     (containsAll(c) && c.containsAll(this))));
3274 <        }
3275 <    }
3276 <
3277 <
3278 <    static final class Values<K,V> extends CHMView<K,V>
3279 <        implements Collection<V> {
3280 <        Values(ConcurrentHashMapV8<K, V> map)   { super(map); }
3281 <        public final boolean contains(Object o) { return map.containsValue(o); }
3282 <        public final boolean remove(Object o) {
3283 <            if (o != null) {
3284 <                Iterator<V> it = new ValueIterator<K,V>(map);
3285 <                while (it.hasNext()) {
3286 <                    if (o.equals(it.next())) {
3287 <                        it.remove();
3288 <                        return true;
3289 <                    }
3290 <                }
3291 <            }
3292 <            return false;
3293 <        }
3294 <        public final Iterator<V> iterator() {
3295 <            return new ValueIterator<K,V>(map);
3296 <        }
3297 <        public final boolean add(V e) {
3298 <            throw new UnsupportedOperationException();
3299 <        }
3300 <        public final boolean addAll(Collection<? extends V> c) {
3301 <            throw new UnsupportedOperationException();
3302 <        }
3303 <
3304 <    }
3305 <
3306 <    static final class EntrySet<K,V> extends CHMView<K,V>
3307 <        implements Set<Map.Entry<K,V>> {
3308 <        EntrySet(ConcurrentHashMapV8<K, V> map) { super(map); }
3309 <        public final boolean contains(Object o) {
3310 <            Object k, v, r; Map.Entry<?,?> e;
3311 <            return ((o instanceof Map.Entry) &&
3312 <                    (k = (e = (Map.Entry<?,?>)o).getKey()) != null &&
3313 <                    (r = map.get(k)) != null &&
3314 <                    (v = e.getValue()) != null &&
3315 <                    (v == r || v.equals(r)));
3316 <        }
3317 <        public final boolean remove(Object o) {
3318 <            Object k, v; Map.Entry<?,?> e;
3319 <            return ((o instanceof Map.Entry) &&
3320 <                    (k = (e = (Map.Entry<?,?>)o).getKey()) != null &&
3321 <                    (v = e.getValue()) != null &&
3322 <                    map.remove(k, v));
3323 <        }
3324 <        public final Iterator<Map.Entry<K,V>> iterator() {
3325 <            return new EntryIterator<K,V>(map);
3326 <        }
3327 <        public final boolean add(Entry<K,V> e) {
3328 <            throw new UnsupportedOperationException();
3329 <        }
3330 <        public final boolean addAll(Collection<? extends Entry<K,V>> c) {
3331 <            throw new UnsupportedOperationException();
3332 <        }
3333 <        public boolean equals(Object o) {
3334 <            Set<?> c;
3335 <            return ((o instanceof Set) &&
3336 <                    ((c = (Set<?>)o) == this ||
3337 <                     (containsAll(c) && c.containsAll(this))));
3338 <        }
3184 >    static <K,V> AbstractMap.SimpleEntry<K,V> entryFor(K k, V v) {
3185 >        return new AbstractMap.SimpleEntry<K,V>(k, v);
3186      }
3187  
3188      /* ---------------- Serialization Support -------------- */
# Line 3359 | Line 3206 | public class ConcurrentHashMapV8<K, V>
3206       * for each key-value mapping, followed by a null pair.
3207       * The key-value mappings are emitted in no particular order.
3208       */
3209 <    @SuppressWarnings("unchecked")
3210 <        private void writeObject(java.io.ObjectOutputStream s)
3209 >    @SuppressWarnings("unchecked") private void writeObject
3210 >        (java.io.ObjectOutputStream s)
3211          throws java.io.IOException {
3212          if (segments == null) { // for serialization compatibility
3213              segments = (Segment<K,V>[])
# Line 3370 | Line 3217 | public class ConcurrentHashMapV8<K, V>
3217          }
3218          s.defaultWriteObject();
3219          Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
3220 <        Object v;
3220 >        V v;
3221          while ((v = it.advance()) != null) {
3222              s.writeObject(it.nextKey);
3223              s.writeObject(v);
# Line 3384 | Line 3231 | public class ConcurrentHashMapV8<K, V>
3231       * Reconstitutes the instance from a stream (that is, deserializes it).
3232       * @param s the stream
3233       */
3234 <    @SuppressWarnings("unchecked")
3235 <        private void readObject(java.io.ObjectInputStream s)
3234 >    @SuppressWarnings("unchecked") private void readObject
3235 >        (java.io.ObjectInputStream s)
3236          throws java.io.IOException, ClassNotFoundException {
3237          s.defaultReadObject();
3238          this.segments = null; // unneeded
3392        // initialize transient final field
3393        UNSAFE.putObjectVolatile(this, counterOffset, new LongAdder());
3239  
3240          // Create all nodes, then place in table once size is known
3241          long size = 0L;
3242 <        Node p = null;
3242 >        Node<V> p = null;
3243          for (;;) {
3244              K k = (K) s.readObject();
3245              V v = (V) s.readObject();
3246              if (k != null && v != null) {
3247                  int h = spread(k.hashCode());
3248 <                p = new Node(h, k, v, p);
3248 >                p = new Node<V>(h, k, v, p);
3249                  ++size;
3250              }
3251              else
# Line 3418 | Line 3263 | public class ConcurrentHashMapV8<K, V>
3263              int sc = sizeCtl;
3264              boolean collide = false;
3265              if (n > sc &&
3266 <                UNSAFE.compareAndSwapInt(this, sizeCtlOffset, sc, -1)) {
3266 >                U.compareAndSwapInt(this, SIZECTL, sc, -1)) {
3267                  try {
3268                      if (table == null) {
3269                          init = true;
3270 <                        Node[] tab = new Node[n];
3270 >                        @SuppressWarnings("rawtypes") Node[] rt = new Node[n];
3271 >                        Node<V>[] tab = (Node<V>[])rt;
3272                          int mask = n - 1;
3273                          while (p != null) {
3274                              int j = p.hash & mask;
3275 <                            Node next = p.next;
3276 <                            Node q = p.next = tabAt(tab, j);
3275 >                            Node<V> next = p.next;
3276 >                            Node<V> q = p.next = tabAt(tab, j);
3277                              setTabAt(tab, j, p);
3278                              if (!collide && q != null && q.hash == p.hash)
3279                                  collide = true;
3280                              p = next;
3281                          }
3282                          table = tab;
3283 <                        counter.add(size);
3283 >                        addCount(size, -1);
3284                          sc = n - (n >>> 2);
3285                      }
3286                  } finally {
3287                      sizeCtl = sc;
3288                  }
3289                  if (collide) { // rescan and convert to TreeBins
3290 <                    Node[] tab = table;
3290 >                    Node<V>[] tab = table;
3291                      for (int i = 0; i < tab.length; ++i) {
3292                          int c = 0;
3293 <                        for (Node e = tabAt(tab, i); e != null; e = e.next) {
3293 >                        for (Node<V> e = tabAt(tab, i); e != null; e = e.next) {
3294                              if (++c > TREE_THRESHOLD &&
3295                                  (e.key instanceof Comparable)) {
3296                                  replaceWithTreeBin(tab, i, e.key);
# Line 3456 | Line 3302 | public class ConcurrentHashMapV8<K, V>
3302              }
3303              if (!init) { // Can only happen if unsafely published.
3304                  while (p != null) {
3305 <                    internalPut(p.key, p.val);
3305 >                    internalPut((K)p.key, p.val, false);
3306                      p = p.next;
3307                  }
3308              }
3309          }
3310      }
3311  
3466
3312      // -------------------------------------------------------
3313  
3314      // Sams
# Line 3505 | Line 3350 | public class ConcurrentHashMapV8<K, V>
3350  
3351      // -------------------------------------------------------
3352  
3353 +    // Sequential bulk operations
3354 +
3355      /**
3356 <     * Returns an extended {@link Parallel} view of this map using the
3510 <     * given executor for bulk parallel operations.
3356 >     * Performs the given action for each (key, value).
3357       *
3358 <     * @param executor the executor
3513 <     * @return a parallel view
3358 >     * @param action the action
3359       */
3360 <    public Parallel parallel(ForkJoinPool executor)  {
3361 <        return new Parallel(executor);
3360 >    @SuppressWarnings("unchecked") public void forEachSequentially
3361 >        (BiAction<K,V> action) {
3362 >        if (action == null) throw new NullPointerException();
3363 >        Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
3364 >        V v;
3365 >        while ((v = it.advance()) != null)
3366 >            action.apply((K)it.nextKey, v);
3367      }
3368  
3369      /**
3370 <     * An extended view of a ConcurrentHashMap supporting bulk
3371 <     * parallel operations. These operations are designed to be be
3372 <     * safely, and often sensibly, applied even with maps that are
3373 <     * being concurrently updated by other threads; for example, when
3374 <     * computing a snapshot summary of the values in a shared
3375 <     * registry.  There are three kinds of operation, each with four
3376 <     * forms, accepting functions with Keys, Values, Entries, and
3527 <     * (Key, Value) arguments and/or return values. Because the
3528 <     * elements of a ConcurrentHashMap are not ordered in any
3529 <     * particular way, and may be processed in different orders in
3530 <     * different parallel executions, the correctness of supplied
3531 <     * functions should not depend on any ordering, or on any other
3532 <     * objects or values that may transiently change while computation
3533 <     * is in progress; and except for forEach actions, should ideally
3534 <     * be side-effect-free.
3535 <     *
3536 <     * <ul>
3537 <     * <li> forEach: Perform a given action on each element.
3538 <     * A variant form applies a given transformation on each element
3539 <     * before performing the action.</li>
3540 <     *
3541 <     * <li> search: Return the first available non-null result of
3542 <     * applying a given function on each element; skipping further
3543 <     * search when a result is found.</li>
3544 <     *
3545 <     * <li> reduce: Accumulate each element.  The supplied reduction
3546 <     * function cannot rely on ordering (more formally, it should be
3547 <     * both associative and commutative).  There are five variants:
3548 <     *
3549 <     * <ul>
3550 <     *
3551 <     * <li> Plain reductions. (There is not a form of this method for
3552 <     * (key, value) function arguments since there is no corresponding
3553 <     * return type.)</li>
3554 <     *
3555 <     * <li> Mapped reductions that accumulate the results of a given
3556 <     * function applied to each element.</li>
3557 <     *
3558 <     * <li> Reductions to scalar doubles, longs, and ints, using a
3559 <     * given basis value.</li>
3560 <     *
3561 <     * </li>
3562 <     * </ul>
3563 <     * </ul>
3564 <     *
3565 <     * <p>The concurrency properties of the bulk operations follow
3566 <     * from those of ConcurrentHashMap: Any non-null result returned
3567 <     * from {@code get(key)} and related access methods bears a
3568 <     * happens-before relation with the associated insertion or
3569 <     * update.  The result of any bulk operation reflects the
3570 <     * composition of these per-element relations (but is not
3571 <     * necessarily atomic with respect to the map as a whole unless it
3572 <     * is somehow known to be quiescent).  Conversely, because keys
3573 <     * and values in the map are never null, null serves as a reliable
3574 <     * atomic indicator of the current lack of any result.  To
3575 <     * maintain this property, null serves as an implicit basis for
3576 <     * all non-scalar reduction operations. For the double, long, and
3577 <     * int versions, the basis should be one that, when combined with
3578 <     * any other value, returns that other value (more formally, it
3579 <     * should be the identity element for the reduction). Most common
3580 <     * reductions have these properties; for example, computing a sum
3581 <     * with basis 0 or a minimum with basis MAX_VALUE.
3582 <     *
3583 <     * <p>Search and transformation functions provided as arguments
3584 <     * should similarly return null to indicate the lack of any result
3585 <     * (in which case it is not used). In the case of mapped
3586 <     * reductions, this also enables transformations to serve as
3587 <     * filters, returning null (or, in the case of primitive
3588 <     * specializations, the identity basis) if the element should not
3589 <     * be combined. You can create compound transformations and
3590 <     * filterings by composing them yourself under this "null means
3591 <     * there is nothing there now" rule before using them in search or
3592 <     * reduce operations.
3593 <     *
3594 <     * <p>Methods accepting and/or returning Entry arguments maintain
3595 <     * key-value associations. They may be useful for example when
3596 <     * finding the key for the greatest value. Note that "plain" Entry
3597 <     * arguments can be supplied using {@code new
3598 <     * AbstractMap.SimpleEntry(k,v)}.
3599 <     *
3600 <     * <p> Bulk operations may complete abruptly, throwing an
3601 <     * exception encountered in the application of a supplied
3602 <     * function. Bear in mind when handling such exceptions that other
3603 <     * concurrently executing functions could also have thrown
3604 <     * exceptions, or would have done so if the first exception had
3605 <     * not occurred.
3606 <     *
3607 <     * <p>Parallel speedups compared to sequential processing are
3608 <     * common but not guaranteed.  Operations involving brief
3609 <     * functions on small maps may execute more slowly than sequential
3610 <     * loops if the underlying work to parallelize the computation is
3611 <     * more expensive than the computation itself. Similarly,
3612 <     * parallelization may not lead to much actual parallelism if all
3613 <     * processors are busy performing unrelated tasks.
3614 <     *
3615 <     * <p> All arguments to all task methods must be non-null.
3616 <     *
3617 <     * <p><em>jsr166e note: During transition, this class
3618 <     * uses nested functional interfaces with different names but the
3619 <     * same forms as those expected for JDK8.<em>
3370 >     * Performs the given action for each non-null transformation
3371 >     * of each (key, value).
3372 >     *
3373 >     * @param transformer a function returning the transformation
3374 >     * for an element, or null of there is no transformation (in
3375 >     * which case the action is not applied).
3376 >     * @param action the action
3377       */
3378 <    public class Parallel {
3379 <        final ForkJoinPool fjp;
3380 <
3381 <        /**
3382 <         * Returns an extended view of this map using the given
3383 <         * executor for bulk parallel operations.
3384 <         *
3385 <         * @param executor the executor
3386 <         */
3387 <        public Parallel(ForkJoinPool executor)  {
3631 <            this.fjp = executor;
3378 >    @SuppressWarnings("unchecked") public <U> void forEachSequentially
3379 >        (BiFun<? super K, ? super V, ? extends U> transformer,
3380 >         Action<U> action) {
3381 >        if (transformer == null || action == null)
3382 >            throw new NullPointerException();
3383 >        Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
3384 >        V v; U u;
3385 >        while ((v = it.advance()) != null) {
3386 >            if ((u = transformer.apply((K)it.nextKey, v)) != null)
3387 >                action.apply(u);
3388          }
3389 +    }
3390  
3391 <        /**
3392 <         * Performs the given action for each (key, value).
3393 <         *
3394 <         * @param action the action
3395 <         */
3396 <        public void forEach(BiAction<K,V> action) {
3397 <            fjp.invoke(ForkJoinTasks.forEach
3398 <                       (ConcurrentHashMapV8.this, action));
3391 >    /**
3392 >     * Returns a non-null result from applying the given search
3393 >     * function on each (key, value), or null if none.
3394 >     *
3395 >     * @param searchFunction a function returning a non-null
3396 >     * result on success, else null
3397 >     * @return a non-null result from applying the given search
3398 >     * function on each (key, value), or null if none
3399 >     */
3400 >    @SuppressWarnings("unchecked") public <U> U searchSequentially
3401 >        (BiFun<? super K, ? super V, ? extends U> searchFunction) {
3402 >        if (searchFunction == null) throw new NullPointerException();
3403 >        Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
3404 >        V v; U u;
3405 >        while ((v = it.advance()) != null) {
3406 >            if ((u = searchFunction.apply((K)it.nextKey, v)) != null)
3407 >                return u;
3408          }
3409 +        return null;
3410 +    }
3411  
3412 <        /**
3413 <         * Performs the given action for each non-null transformation
3414 <         * of each (key, value).
3415 <         *
3416 <         * @param transformer a function returning the transformation
3417 <         * for an element, or null of there is no transformation (in
3418 <         * which case the action is not applied).
3419 <         * @param action the action
3420 <         */
3421 <        public <U> void forEach(BiFun<? super K, ? super V, ? extends U> transformer,
3422 <                                Action<U> action) {
3423 <            fjp.invoke(ForkJoinTasks.forEach
3424 <                       (ConcurrentHashMapV8.this, transformer, action));
3412 >    /**
3413 >     * Returns the result of accumulating the given transformation
3414 >     * of all (key, value) pairs using the given reducer to
3415 >     * combine values, or null if none.
3416 >     *
3417 >     * @param transformer a function returning the transformation
3418 >     * for an element, or null of there is no transformation (in
3419 >     * which case it is not combined).
3420 >     * @param reducer a commutative associative combining function
3421 >     * @return the result of accumulating the given transformation
3422 >     * of all (key, value) pairs
3423 >     */
3424 >    @SuppressWarnings("unchecked") public <U> U reduceSequentially
3425 >        (BiFun<? super K, ? super V, ? extends U> transformer,
3426 >         BiFun<? super U, ? super U, ? extends U> reducer) {
3427 >        if (transformer == null || reducer == null)
3428 >            throw new NullPointerException();
3429 >        Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
3430 >        U r = null, u; V v;
3431 >        while ((v = it.advance()) != null) {
3432 >            if ((u = transformer.apply((K)it.nextKey, v)) != null)
3433 >                r = (r == null) ? u : reducer.apply(r, u);
3434          }
3435 +        return r;
3436 +    }
3437  
3438 <        /**
3439 <         * Returns a non-null result from applying the given search
3440 <         * function on each (key, value), or null if none.  Further
3441 <         * element processing is suppressed upon success. However,
3442 <         * this method does not return until other in-progress
3443 <         * parallel invocations of the search function also complete.
3444 <         *
3445 <         * @param searchFunction a function returning a non-null
3446 <         * result on success, else null
3447 <         * @return a non-null result from applying the given search
3448 <         * function on each (key, value), or null if none
3449 <         */
3450 <        public <U> U search(BiFun<? super K, ? super V, ? extends U> searchFunction) {
3451 <            return fjp.invoke(ForkJoinTasks.search
3452 <                              (ConcurrentHashMapV8.this, searchFunction));
3453 <        }
3438 >    /**
3439 >     * Returns the result of accumulating the given transformation
3440 >     * of all (key, value) pairs using the given reducer to
3441 >     * combine values, and the given basis as an identity value.
3442 >     *
3443 >     * @param transformer a function returning the transformation
3444 >     * for an element
3445 >     * @param basis the identity (initial default value) for the reduction
3446 >     * @param reducer a commutative associative combining function
3447 >     * @return the result of accumulating the given transformation
3448 >     * of all (key, value) pairs
3449 >     */
3450 >    @SuppressWarnings("unchecked") public double reduceToDoubleSequentially
3451 >        (ObjectByObjectToDouble<? super K, ? super V> transformer,
3452 >         double basis,
3453 >         DoubleByDoubleToDouble reducer) {
3454 >        if (transformer == null || reducer == null)
3455 >            throw new NullPointerException();
3456 >        Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
3457 >        double r = basis; V v;
3458 >        while ((v = it.advance()) != null)
3459 >            r = reducer.apply(r, transformer.apply((K)it.nextKey, v));
3460 >        return r;
3461 >    }
3462  
3463 <        /**
3464 <         * Returns the result of accumulating the given transformation
3465 <         * of all (key, value) pairs using the given reducer to
3466 <         * combine values, or null if none.
3467 <         *
3468 <         * @param transformer a function returning the transformation
3469 <         * for an element, or null of there is no transformation (in
3470 <         * which case it is not combined).
3471 <         * @param reducer a commutative associative combining function
3472 <         * @return the result of accumulating the given transformation
3473 <         * of all (key, value) pairs
3474 <         */
3475 <        public <U> U reduce(BiFun<? super K, ? super V, ? extends U> transformer,
3476 <                            BiFun<? super U, ? super U, ? extends U> reducer) {
3477 <            return fjp.invoke(ForkJoinTasks.reduce
3478 <                              (ConcurrentHashMapV8.this, transformer, reducer));
3479 <        }
3463 >    /**
3464 >     * Returns the result of accumulating the given transformation
3465 >     * of all (key, value) pairs using the given reducer to
3466 >     * combine values, and the given basis as an identity value.
3467 >     *
3468 >     * @param transformer a function returning the transformation
3469 >     * for an element
3470 >     * @param basis the identity (initial default value) for the reduction
3471 >     * @param reducer a commutative associative combining function
3472 >     * @return the result of accumulating the given transformation
3473 >     * of all (key, value) pairs
3474 >     */
3475 >    @SuppressWarnings("unchecked") public long reduceToLongSequentially
3476 >        (ObjectByObjectToLong<? super K, ? super V> transformer,
3477 >         long basis,
3478 >         LongByLongToLong reducer) {
3479 >        if (transformer == null || reducer == null)
3480 >            throw new NullPointerException();
3481 >        Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
3482 >        long r = basis; V v;
3483 >        while ((v = it.advance()) != null)
3484 >            r = reducer.apply(r, transformer.apply((K)it.nextKey, v));
3485 >        return r;
3486 >    }
3487  
3488 <        /**
3489 <         * Returns the result of accumulating the given transformation
3490 <         * of all (key, value) pairs using the given reducer to
3491 <         * combine values, and the given basis as an identity value.
3492 <         *
3493 <         * @param transformer a function returning the transformation
3494 <         * for an element
3495 <         * @param basis the identity (initial default value) for the reduction
3496 <         * @param reducer a commutative associative combining function
3497 <         * @return the result of accumulating the given transformation
3498 <         * of all (key, value) pairs
3499 <         */
3500 <        public double reduceToDouble(ObjectByObjectToDouble<? super K, ? super V> transformer,
3501 <                                     double basis,
3502 <                                     DoubleByDoubleToDouble reducer) {
3503 <            return fjp.invoke(ForkJoinTasks.reduceToDouble
3504 <                              (ConcurrentHashMapV8.this, transformer, basis, reducer));
3505 <        }
3488 >    /**
3489 >     * Returns the result of accumulating the given transformation
3490 >     * of all (key, value) pairs using the given reducer to
3491 >     * combine values, and the given basis as an identity value.
3492 >     *
3493 >     * @param transformer a function returning the transformation
3494 >     * for an element
3495 >     * @param basis the identity (initial default value) for the reduction
3496 >     * @param reducer a commutative associative combining function
3497 >     * @return the result of accumulating the given transformation
3498 >     * of all (key, value) pairs
3499 >     */
3500 >    @SuppressWarnings("unchecked") public int reduceToIntSequentially
3501 >        (ObjectByObjectToInt<? super K, ? super V> transformer,
3502 >         int basis,
3503 >         IntByIntToInt reducer) {
3504 >        if (transformer == null || reducer == null)
3505 >            throw new NullPointerException();
3506 >        Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
3507 >        int r = basis; V v;
3508 >        while ((v = it.advance()) != null)
3509 >            r = reducer.apply(r, transformer.apply((K)it.nextKey, v));
3510 >        return r;
3511 >    }
3512  
3513 <        /**
3514 <         * Returns the result of accumulating the given transformation
3515 <         * of all (key, value) pairs using the given reducer to
3516 <         * combine values, and the given basis as an identity value.
3517 <         *
3518 <         * @param transformer a function returning the transformation
3519 <         * for an element
3520 <         * @param basis the identity (initial default value) for the reduction
3521 <         * @param reducer a commutative associative combining function
3522 <         * @return the result of accumulating the given transformation
3523 <         * of all (key, value) pairs using the given reducer to
3524 <         * combine values, and the given basis as an identity value.
3725 <         */
3726 <        public long reduceToLong(ObjectByObjectToLong<? super K, ? super V> transformer,
3727 <                                 long basis,
3728 <                                 LongByLongToLong reducer) {
3729 <            return fjp.invoke(ForkJoinTasks.reduceToLong
3730 <                              (ConcurrentHashMapV8.this, transformer, basis, reducer));
3731 <        }
3513 >    /**
3514 >     * Performs the given action for each key.
3515 >     *
3516 >     * @param action the action
3517 >     */
3518 >    @SuppressWarnings("unchecked") public void forEachKeySequentially
3519 >        (Action<K> action) {
3520 >        if (action == null) throw new NullPointerException();
3521 >        Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
3522 >        while (it.advance() != null)
3523 >            action.apply((K)it.nextKey);
3524 >    }
3525  
3526 <        /**
3527 <         * Returns the result of accumulating the given transformation
3528 <         * of all (key, value) pairs using the given reducer to
3529 <         * combine values, and the given basis as an identity value.
3530 <         *
3531 <         * @param transformer a function returning the transformation
3532 <         * for an element
3533 <         * @param basis the identity (initial default value) for the reduction
3534 <         * @param reducer a commutative associative combining function
3535 <         * @return the result of accumulating the given transformation
3536 <         * of all (key, value) pairs
3537 <         */
3538 <        public int reduceToInt(ObjectByObjectToInt<? super K, ? super V> transformer,
3539 <                               int basis,
3540 <                               IntByIntToInt reducer) {
3541 <            return fjp.invoke(ForkJoinTasks.reduceToInt
3542 <                              (ConcurrentHashMapV8.this, transformer, basis, reducer));
3526 >    /**
3527 >     * Performs the given action for each non-null transformation
3528 >     * of each key.
3529 >     *
3530 >     * @param transformer a function returning the transformation
3531 >     * for an element, or null of there is no transformation (in
3532 >     * which case the action is not applied).
3533 >     * @param action the action
3534 >     */
3535 >    @SuppressWarnings("unchecked") public <U> void forEachKeySequentially
3536 >        (Fun<? super K, ? extends U> transformer,
3537 >         Action<U> action) {
3538 >        if (transformer == null || action == null)
3539 >            throw new NullPointerException();
3540 >        Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
3541 >        U u;
3542 >        while (it.advance() != null) {
3543 >            if ((u = transformer.apply((K)it.nextKey)) != null)
3544 >                action.apply(u);
3545          }
3546 +        ForkJoinTasks.forEachKey
3547 +            (this, transformer, action).invoke();
3548 +    }
3549  
3550 <        /**
3551 <         * Performs the given action for each key
3552 <         *
3553 <         * @param action the action
3554 <         */
3555 <        public void forEachKey(Action<K> action) {
3556 <            fjp.invoke(ForkJoinTasks.forEachKey
3557 <                       (ConcurrentHashMapV8.this, action));
3550 >    /**
3551 >     * Returns a non-null result from applying the given search
3552 >     * function on each key, or null if none.
3553 >     *
3554 >     * @param searchFunction a function returning a non-null
3555 >     * result on success, else null
3556 >     * @return a non-null result from applying the given search
3557 >     * function on each key, or null if none
3558 >     */
3559 >    @SuppressWarnings("unchecked") public <U> U searchKeysSequentially
3560 >        (Fun<? super K, ? extends U> searchFunction) {
3561 >        Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
3562 >        U u;
3563 >        while (it.advance() != null) {
3564 >            if ((u = searchFunction.apply((K)it.nextKey)) != null)
3565 >                return u;
3566          }
3567 +        return null;
3568 +    }
3569  
3570 <        /**
3571 <         * Performs the given action for each non-null transformation
3572 <         * of each key
3573 <         *
3574 <         * @param transformer a function returning the transformation
3575 <         * for an element, or null of there is no transformation (in
3576 <         * which case the action is not applied).
3577 <         * @param action the action
3578 <         */
3579 <        public <U> void forEachKey(Fun<? super K, ? extends U> transformer,
3580 <                                   Action<U> action) {
3581 <            fjp.invoke(ForkJoinTasks.forEachKey
3582 <                       (ConcurrentHashMapV8.this, transformer, action));
3570 >    /**
3571 >     * Returns the result of accumulating all keys using the given
3572 >     * reducer to combine values, or null if none.
3573 >     *
3574 >     * @param reducer a commutative associative combining function
3575 >     * @return the result of accumulating all keys using the given
3576 >     * reducer to combine values, or null if none
3577 >     */
3578 >    @SuppressWarnings("unchecked") public K reduceKeysSequentially
3579 >        (BiFun<? super K, ? super K, ? extends K> reducer) {
3580 >        if (reducer == null) throw new NullPointerException();
3581 >        Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
3582 >        K r = null;
3583 >        while (it.advance() != null) {
3584 >            K u = (K)it.nextKey;
3585 >            r = (r == null) ? u : reducer.apply(r, u);
3586 >        }
3587 >        return r;
3588 >    }
3589 >
3590 >    /**
3591 >     * Returns the result of accumulating the given transformation
3592 >     * of all keys using the given reducer to combine values, or
3593 >     * null if none.
3594 >     *
3595 >     * @param transformer a function returning the transformation
3596 >     * for an element, or null of there is no transformation (in
3597 >     * which case it is not combined).
3598 >     * @param reducer a commutative associative combining function
3599 >     * @return the result of accumulating the given transformation
3600 >     * of all keys
3601 >     */
3602 >    @SuppressWarnings("unchecked") public <U> U reduceKeysSequentially
3603 >        (Fun<? super K, ? extends U> transformer,
3604 >         BiFun<? super U, ? super U, ? extends U> reducer) {
3605 >        if (transformer == null || reducer == null)
3606 >            throw new NullPointerException();
3607 >        Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
3608 >        U r = null, u;
3609 >        while (it.advance() != null) {
3610 >            if ((u = transformer.apply((K)it.nextKey)) != null)
3611 >                r = (r == null) ? u : reducer.apply(r, u);
3612          }
3613 +        return r;
3614 +    }
3615  
3616 <        /**
3617 <         * Returns a non-null result from applying the given search
3618 <         * function on each key, or null if none.  Further element
3619 <         * processing is suppressed upon success. However, this method
3620 <         * does not return until other in-progress parallel
3621 <         * invocations of the search function also complete.
3622 <         *
3623 <         * @param searchFunction a function returning a non-null
3624 <         * result on success, else null
3625 <         * @return a non-null result from applying the given search
3626 <         * function on each key, or null if none
3627 <         */
3628 <        public <U> U searchKeys(Fun<? super K, ? extends U> searchFunction) {
3629 <            return fjp.invoke(ForkJoinTasks.searchKeys
3630 <                              (ConcurrentHashMapV8.this, searchFunction));
3616 >    /**
3617 >     * Returns the result of accumulating the given transformation
3618 >     * of all keys using the given reducer to combine values, and
3619 >     * the given basis as an identity value.
3620 >     *
3621 >     * @param transformer a function returning the transformation
3622 >     * for an element
3623 >     * @param basis the identity (initial default value) for the reduction
3624 >     * @param reducer a commutative associative combining function
3625 >     * @return  the result of accumulating the given transformation
3626 >     * of all keys
3627 >     */
3628 >    @SuppressWarnings("unchecked") public double reduceKeysToDoubleSequentially
3629 >        (ObjectToDouble<? super K> transformer,
3630 >         double basis,
3631 >         DoubleByDoubleToDouble reducer) {
3632 >        if (transformer == null || reducer == null)
3633 >            throw new NullPointerException();
3634 >        Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
3635 >        double r = basis;
3636 >        while (it.advance() != null)
3637 >            r = reducer.apply(r, transformer.apply((K)it.nextKey));
3638 >        return r;
3639 >    }
3640 >
3641 >    /**
3642 >     * Returns the result of accumulating the given transformation
3643 >     * of all keys using the given reducer to combine values, and
3644 >     * the given basis as an identity value.
3645 >     *
3646 >     * @param transformer a function returning the transformation
3647 >     * for an element
3648 >     * @param basis the identity (initial default value) for the reduction
3649 >     * @param reducer a commutative associative combining function
3650 >     * @return the result of accumulating the given transformation
3651 >     * of all keys
3652 >     */
3653 >    @SuppressWarnings("unchecked") public long reduceKeysToLongSequentially
3654 >        (ObjectToLong<? super K> transformer,
3655 >         long basis,
3656 >         LongByLongToLong reducer) {
3657 >        if (transformer == null || reducer == null)
3658 >            throw new NullPointerException();
3659 >        Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
3660 >        long r = basis;
3661 >        while (it.advance() != null)
3662 >            r = reducer.apply(r, transformer.apply((K)it.nextKey));
3663 >        return r;
3664 >    }
3665 >
3666 >    /**
3667 >     * Returns the result of accumulating the given transformation
3668 >     * of all keys using the given reducer to combine values, and
3669 >     * the given basis as an identity value.
3670 >     *
3671 >     * @param transformer a function returning the transformation
3672 >     * for an element
3673 >     * @param basis the identity (initial default value) for the reduction
3674 >     * @param reducer a commutative associative combining function
3675 >     * @return the result of accumulating the given transformation
3676 >     * of all keys
3677 >     */
3678 >    @SuppressWarnings("unchecked") public int reduceKeysToIntSequentially
3679 >        (ObjectToInt<? super K> transformer,
3680 >         int basis,
3681 >         IntByIntToInt reducer) {
3682 >        if (transformer == null || reducer == null)
3683 >            throw new NullPointerException();
3684 >        Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
3685 >        int r = basis;
3686 >        while (it.advance() != null)
3687 >            r = reducer.apply(r, transformer.apply((K)it.nextKey));
3688 >        return r;
3689 >    }
3690 >
3691 >    /**
3692 >     * Performs the given action for each value.
3693 >     *
3694 >     * @param action the action
3695 >     */
3696 >    public void forEachValueSequentially(Action<V> action) {
3697 >        if (action == null) throw new NullPointerException();
3698 >        Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
3699 >        V v;
3700 >        while ((v = it.advance()) != null)
3701 >            action.apply(v);
3702 >    }
3703 >
3704 >    /**
3705 >     * Performs the given action for each non-null transformation
3706 >     * of each value.
3707 >     *
3708 >     * @param transformer a function returning the transformation
3709 >     * for an element, or null of there is no transformation (in
3710 >     * which case the action is not applied).
3711 >     */
3712 >    public <U> void forEachValueSequentially
3713 >        (Fun<? super V, ? extends U> transformer,
3714 >         Action<U> action) {
3715 >        if (transformer == null || action == null)
3716 >            throw new NullPointerException();
3717 >        Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
3718 >        V v; U u;
3719 >        while ((v = it.advance()) != null) {
3720 >            if ((u = transformer.apply(v)) != null)
3721 >                action.apply(u);
3722          }
3723 +    }
3724  
3725 <        /**
3726 <         * Returns the result of accumulating all keys using the given
3727 <         * reducer to combine values, or null if none.
3728 <         *
3729 <         * @param reducer a commutative associative combining function
3730 <         * @return the result of accumulating all keys using the given
3731 <         * reducer to combine values, or null if none
3732 <         */
3733 <        public K reduceKeys(BiFun<? super K, ? super K, ? extends K> reducer) {
3734 <            return fjp.invoke(ForkJoinTasks.reduceKeys
3735 <                              (ConcurrentHashMapV8.this, reducer));
3725 >    /**
3726 >     * Returns a non-null result from applying the given search
3727 >     * function on each value, or null if none.
3728 >     *
3729 >     * @param searchFunction a function returning a non-null
3730 >     * result on success, else null
3731 >     * @return a non-null result from applying the given search
3732 >     * function on each value, or null if none
3733 >     */
3734 >    public <U> U searchValuesSequentially
3735 >        (Fun<? super V, ? extends U> searchFunction) {
3736 >        if (searchFunction == null) throw new NullPointerException();
3737 >        Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
3738 >        V v; U u;
3739 >        while ((v = it.advance()) != null) {
3740 >            if ((u = searchFunction.apply(v)) != null)
3741 >                return u;
3742          }
3743 +        return null;
3744 +    }
3745  
3746 <        /**
3747 <         * Returns the result of accumulating the given transformation
3748 <         * of all keys using the given reducer to combine values, or
3749 <         * null if none.
3750 <         *
3751 <         * @param transformer a function returning the transformation
3752 <         * for an element, or null of there is no transformation (in
3753 <         * which case it is not combined).
3754 <         * @param reducer a commutative associative combining function
3755 <         * @return the result of accumulating the given transformation
3756 <         * of all keys
3757 <         */
3758 <        public <U> U reduceKeys(Fun<? super K, ? extends U> transformer,
3759 <                                BiFun<? super U, ? super U, ? extends U> reducer) {
3760 <            return fjp.invoke(ForkJoinTasks.reduceKeys
3761 <                              (ConcurrentHashMapV8.this, transformer, reducer));
3746 >    /**
3747 >     * Returns the result of accumulating all values using the
3748 >     * given reducer to combine values, or null if none.
3749 >     *
3750 >     * @param reducer a commutative associative combining function
3751 >     * @return  the result of accumulating all values
3752 >     */
3753 >    public V reduceValuesSequentially
3754 >        (BiFun<? super V, ? super V, ? extends V> reducer) {
3755 >        if (reducer == null) throw new NullPointerException();
3756 >        Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
3757 >        V r = null; V v;
3758 >        while ((v = it.advance()) != null)
3759 >            r = (r == null) ? v : reducer.apply(r, v);
3760 >        return r;
3761 >    }
3762 >
3763 >    /**
3764 >     * Returns the result of accumulating the given transformation
3765 >     * of all values using the given reducer to combine values, or
3766 >     * null if none.
3767 >     *
3768 >     * @param transformer a function returning the transformation
3769 >     * for an element, or null of there is no transformation (in
3770 >     * which case it is not combined).
3771 >     * @param reducer a commutative associative combining function
3772 >     * @return the result of accumulating the given transformation
3773 >     * of all values
3774 >     */
3775 >    public <U> U reduceValuesSequentially
3776 >        (Fun<? super V, ? extends U> transformer,
3777 >         BiFun<? super U, ? super U, ? extends U> reducer) {
3778 >        if (transformer == null || reducer == null)
3779 >            throw new NullPointerException();
3780 >        Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
3781 >        U r = null, u; V v;
3782 >        while ((v = it.advance()) != null) {
3783 >            if ((u = transformer.apply(v)) != null)
3784 >                r = (r == null) ? u : reducer.apply(r, u);
3785          }
3786 +        return r;
3787 +    }
3788  
3789 <        /**
3790 <         * Returns the result of accumulating the given transformation
3791 <         * of all keys using the given reducer to combine values, and
3792 <         * the given basis as an identity value.
3793 <         *
3794 <         * @param transformer a function returning the transformation
3795 <         * for an element
3796 <         * @param basis the identity (initial default value) for the reduction
3797 <         * @param reducer a commutative associative combining function
3798 <         * @return  the result of accumulating the given transformation
3799 <         * of all keys
3800 <         */
3801 <        public double reduceKeysToDouble(ObjectToDouble<? super K> transformer,
3802 <                                         double basis,
3803 <                                         DoubleByDoubleToDouble reducer) {
3804 <            return fjp.invoke(ForkJoinTasks.reduceKeysToDouble
3805 <                              (ConcurrentHashMapV8.this, transformer, basis, reducer));
3789 >    /**
3790 >     * Returns the result of accumulating the given transformation
3791 >     * of all values using the given reducer to combine values,
3792 >     * and the given basis as an identity value.
3793 >     *
3794 >     * @param transformer a function returning the transformation
3795 >     * for an element
3796 >     * @param basis the identity (initial default value) for the reduction
3797 >     * @param reducer a commutative associative combining function
3798 >     * @return the result of accumulating the given transformation
3799 >     * of all values
3800 >     */
3801 >    public double reduceValuesToDoubleSequentially
3802 >        (ObjectToDouble<? super V> transformer,
3803 >         double basis,
3804 >         DoubleByDoubleToDouble reducer) {
3805 >        if (transformer == null || reducer == null)
3806 >            throw new NullPointerException();
3807 >        Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
3808 >        double r = basis; V v;
3809 >        while ((v = it.advance()) != null)
3810 >            r = reducer.apply(r, transformer.apply(v));
3811 >        return r;
3812 >    }
3813 >
3814 >    /**
3815 >     * Returns the result of accumulating the given transformation
3816 >     * of all values using the given reducer to combine values,
3817 >     * and the given basis as an identity value.
3818 >     *
3819 >     * @param transformer a function returning the transformation
3820 >     * for an element
3821 >     * @param basis the identity (initial default value) for the reduction
3822 >     * @param reducer a commutative associative combining function
3823 >     * @return the result of accumulating the given transformation
3824 >     * of all values
3825 >     */
3826 >    public long reduceValuesToLongSequentially
3827 >        (ObjectToLong<? super V> transformer,
3828 >         long basis,
3829 >         LongByLongToLong reducer) {
3830 >        if (transformer == null || reducer == null)
3831 >            throw new NullPointerException();
3832 >        Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
3833 >        long r = basis; V v;
3834 >        while ((v = it.advance()) != null)
3835 >            r = reducer.apply(r, transformer.apply(v));
3836 >        return r;
3837 >    }
3838 >
3839 >    /**
3840 >     * Returns the result of accumulating the given transformation
3841 >     * of all values using the given reducer to combine values,
3842 >     * and the given basis as an identity value.
3843 >     *
3844 >     * @param transformer a function returning the transformation
3845 >     * for an element
3846 >     * @param basis the identity (initial default value) for the reduction
3847 >     * @param reducer a commutative associative combining function
3848 >     * @return the result of accumulating the given transformation
3849 >     * of all values
3850 >     */
3851 >    public int reduceValuesToIntSequentially
3852 >        (ObjectToInt<? super V> transformer,
3853 >         int basis,
3854 >         IntByIntToInt reducer) {
3855 >        if (transformer == null || reducer == null)
3856 >            throw new NullPointerException();
3857 >        Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
3858 >        int r = basis; V v;
3859 >        while ((v = it.advance()) != null)
3860 >            r = reducer.apply(r, transformer.apply(v));
3861 >        return r;
3862 >    }
3863 >
3864 >    /**
3865 >     * Performs the given action for each entry.
3866 >     *
3867 >     * @param action the action
3868 >     */
3869 >    @SuppressWarnings("unchecked") public void forEachEntrySequentially
3870 >        (Action<Map.Entry<K,V>> action) {
3871 >        if (action == null) throw new NullPointerException();
3872 >        Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
3873 >        V v;
3874 >        while ((v = it.advance()) != null)
3875 >            action.apply(entryFor((K)it.nextKey, v));
3876 >    }
3877 >
3878 >    /**
3879 >     * Performs the given action for each non-null transformation
3880 >     * of each entry.
3881 >     *
3882 >     * @param transformer a function returning the transformation
3883 >     * for an element, or null of there is no transformation (in
3884 >     * which case the action is not applied).
3885 >     * @param action the action
3886 >     */
3887 >    @SuppressWarnings("unchecked") public <U> void forEachEntrySequentially
3888 >        (Fun<Map.Entry<K,V>, ? extends U> transformer,
3889 >         Action<U> action) {
3890 >        if (transformer == null || action == null)
3891 >            throw new NullPointerException();
3892 >        Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
3893 >        V v; U u;
3894 >        while ((v = it.advance()) != null) {
3895 >            if ((u = transformer.apply(entryFor((K)it.nextKey, v))) != null)
3896 >                action.apply(u);
3897          }
3898 +    }
3899  
3900 <        /**
3901 <         * Returns the result of accumulating the given transformation
3902 <         * of all keys using the given reducer to combine values, and
3903 <         * the given basis as an identity value.
3904 <         *
3905 <         * @param transformer a function returning the transformation
3906 <         * for an element
3907 <         * @param basis the identity (initial default value) for the reduction
3908 <         * @param reducer a commutative associative combining function
3909 <         * @return the result of accumulating the given transformation
3910 <         * of all keys
3911 <         */
3912 <        public long reduceKeysToLong(ObjectToLong<? super K> transformer,
3913 <                                     long basis,
3914 <                                     LongByLongToLong reducer) {
3915 <            return fjp.invoke(ForkJoinTasks.reduceKeysToLong
3916 <                              (ConcurrentHashMapV8.this, transformer, basis, reducer));
3900 >    /**
3901 >     * Returns a non-null result from applying the given search
3902 >     * function on each entry, or null if none.
3903 >     *
3904 >     * @param searchFunction a function returning a non-null
3905 >     * result on success, else null
3906 >     * @return a non-null result from applying the given search
3907 >     * function on each entry, or null if none
3908 >     */
3909 >    @SuppressWarnings("unchecked") public <U> U searchEntriesSequentially
3910 >        (Fun<Map.Entry<K,V>, ? extends U> searchFunction) {
3911 >        if (searchFunction == null) throw new NullPointerException();
3912 >        Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
3913 >        V v; U u;
3914 >        while ((v = it.advance()) != null) {
3915 >            if ((u = searchFunction.apply(entryFor((K)it.nextKey, v))) != null)
3916 >                return u;
3917          }
3918 +        return null;
3919 +    }
3920  
3921 <        /**
3922 <         * Returns the result of accumulating the given transformation
3923 <         * of all keys using the given reducer to combine values, and
3924 <         * the given basis as an identity value.
3925 <         *
3926 <         * @param transformer a function returning the transformation
3927 <         * for an element
3928 <         * @param basis the identity (initial default value) for the reduction
3929 <         * @param reducer a commutative associative combining function
3930 <         * @return the result of accumulating the given transformation
3931 <         * of all keys
3932 <         */
3933 <        public int reduceKeysToInt(ObjectToInt<? super K> transformer,
3934 <                                   int basis,
3935 <                                   IntByIntToInt reducer) {
3878 <            return fjp.invoke(ForkJoinTasks.reduceKeysToInt
3879 <                              (ConcurrentHashMapV8.this, transformer, basis, reducer));
3921 >    /**
3922 >     * Returns the result of accumulating all entries using the
3923 >     * given reducer to combine values, or null if none.
3924 >     *
3925 >     * @param reducer a commutative associative combining function
3926 >     * @return the result of accumulating all entries
3927 >     */
3928 >    @SuppressWarnings("unchecked") public Map.Entry<K,V> reduceEntriesSequentially
3929 >        (BiFun<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer) {
3930 >        if (reducer == null) throw new NullPointerException();
3931 >        Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
3932 >        Map.Entry<K,V> r = null; V v;
3933 >        while ((v = it.advance()) != null) {
3934 >            Map.Entry<K,V> u = entryFor((K)it.nextKey, v);
3935 >            r = (r == null) ? u : reducer.apply(r, u);
3936          }
3937 +        return r;
3938 +    }
3939  
3940 <        /**
3941 <         * Performs the given action for each value
3942 <         *
3943 <         * @param action the action
3944 <         */
3945 <        public void forEachValue(Action<V> action) {
3946 <            fjp.invoke(ForkJoinTasks.forEachValue
3947 <                       (ConcurrentHashMapV8.this, action));
3940 >    /**
3941 >     * Returns the result of accumulating the given transformation
3942 >     * of all entries using the given reducer to combine values,
3943 >     * or null if none.
3944 >     *
3945 >     * @param transformer a function returning the transformation
3946 >     * for an element, or null of there is no transformation (in
3947 >     * which case it is not combined).
3948 >     * @param reducer a commutative associative combining function
3949 >     * @return the result of accumulating the given transformation
3950 >     * of all entries
3951 >     */
3952 >    @SuppressWarnings("unchecked") public <U> U reduceEntriesSequentially
3953 >        (Fun<Map.Entry<K,V>, ? extends U> transformer,
3954 >         BiFun<? super U, ? super U, ? extends U> reducer) {
3955 >        if (transformer == null || reducer == null)
3956 >            throw new NullPointerException();
3957 >        Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
3958 >        U r = null, u; V v;
3959 >        while ((v = it.advance()) != null) {
3960 >            if ((u = transformer.apply(entryFor((K)it.nextKey, v))) != null)
3961 >                r = (r == null) ? u : reducer.apply(r, u);
3962          }
3963 +        return r;
3964 +    }
3965 +
3966 +    /**
3967 +     * Returns the result of accumulating the given transformation
3968 +     * of all entries using the given reducer to combine values,
3969 +     * and the given basis as an identity value.
3970 +     *
3971 +     * @param transformer a function returning the transformation
3972 +     * for an element
3973 +     * @param basis the identity (initial default value) for the reduction
3974 +     * @param reducer a commutative associative combining function
3975 +     * @return the result of accumulating the given transformation
3976 +     * of all entries
3977 +     */
3978 +    @SuppressWarnings("unchecked") public double reduceEntriesToDoubleSequentially
3979 +        (ObjectToDouble<Map.Entry<K,V>> transformer,
3980 +         double basis,
3981 +         DoubleByDoubleToDouble reducer) {
3982 +        if (transformer == null || reducer == null)
3983 +            throw new NullPointerException();
3984 +        Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
3985 +        double r = basis; V v;
3986 +        while ((v = it.advance()) != null)
3987 +            r = reducer.apply(r, transformer.apply(entryFor((K)it.nextKey, v)));
3988 +        return r;
3989 +    }
3990 +
3991 +    /**
3992 +     * Returns the result of accumulating the given transformation
3993 +     * of all entries using the given reducer to combine values,
3994 +     * and the given basis as an identity value.
3995 +     *
3996 +     * @param transformer a function returning the transformation
3997 +     * for an element
3998 +     * @param basis the identity (initial default value) for the reduction
3999 +     * @param reducer a commutative associative combining function
4000 +     * @return  the result of accumulating the given transformation
4001 +     * of all entries
4002 +     */
4003 +    @SuppressWarnings("unchecked") public long reduceEntriesToLongSequentially
4004 +        (ObjectToLong<Map.Entry<K,V>> transformer,
4005 +         long basis,
4006 +         LongByLongToLong reducer) {
4007 +        if (transformer == null || reducer == null)
4008 +            throw new NullPointerException();
4009 +        Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
4010 +        long r = basis; V v;
4011 +        while ((v = it.advance()) != null)
4012 +            r = reducer.apply(r, transformer.apply(entryFor((K)it.nextKey, v)));
4013 +        return r;
4014 +    }
4015 +
4016 +    /**
4017 +     * Returns the result of accumulating the given transformation
4018 +     * of all entries using the given reducer to combine values,
4019 +     * and the given basis as an identity value.
4020 +     *
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 entries
4027 +     */
4028 +    @SuppressWarnings("unchecked") public int reduceEntriesToIntSequentially
4029 +        (ObjectToInt<Map.Entry<K,V>> transformer,
4030 +         int basis,
4031 +         IntByIntToInt reducer) {
4032 +        if (transformer == null || reducer == null)
4033 +            throw new NullPointerException();
4034 +        Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
4035 +        int r = basis; V v;
4036 +        while ((v = it.advance()) != null)
4037 +            r = reducer.apply(r, transformer.apply(entryFor((K)it.nextKey, v)));
4038 +        return r;
4039 +    }
4040 +
4041 +    // Parallel bulk operations
4042 +
4043 +    /**
4044 +     * Performs the given action for each (key, value).
4045 +     *
4046 +     * @param action the action
4047 +     */
4048 +    public void forEachInParallel(BiAction<K,V> action) {
4049 +        ForkJoinTasks.forEach
4050 +            (this, action).invoke();
4051 +    }
4052 +
4053 +    /**
4054 +     * Performs the given action for each non-null transformation
4055 +     * of each (key, value).
4056 +     *
4057 +     * @param transformer a function returning the transformation
4058 +     * for an element, or null of there is no transformation (in
4059 +     * which case the action is not applied).
4060 +     * @param action the action
4061 +     */
4062 +    public <U> void forEachInParallel
4063 +        (BiFun<? super K, ? super V, ? extends U> transformer,
4064 +                            Action<U> action) {
4065 +        ForkJoinTasks.forEach
4066 +            (this, transformer, action).invoke();
4067 +    }
4068 +
4069 +    /**
4070 +     * Returns a non-null result from applying the given search
4071 +     * function on each (key, value), or null if none.  Upon
4072 +     * success, further element processing is suppressed and the
4073 +     * results of any other parallel invocations of the search
4074 +     * function are ignored.
4075 +     *
4076 +     * @param searchFunction a function returning a non-null
4077 +     * result on success, else null
4078 +     * @return a non-null result from applying the given search
4079 +     * function on each (key, value), or null if none
4080 +     */
4081 +    public <U> U searchInParallel
4082 +        (BiFun<? super K, ? super V, ? extends U> searchFunction) {
4083 +        return ForkJoinTasks.search
4084 +            (this, searchFunction).invoke();
4085 +    }
4086 +
4087 +    /**
4088 +     * Returns the result of accumulating the given transformation
4089 +     * of all (key, value) pairs using the given reducer to
4090 +     * combine values, or null if none.
4091 +     *
4092 +     * @param transformer a function returning the transformation
4093 +     * for an element, or null of there is no transformation (in
4094 +     * which case it is not combined).
4095 +     * @param reducer a commutative associative combining function
4096 +     * @return the result of accumulating the given transformation
4097 +     * of all (key, value) pairs
4098 +     */
4099 +    public <U> U reduceInParallel
4100 +        (BiFun<? super K, ? super V, ? extends U> transformer,
4101 +         BiFun<? super U, ? super U, ? extends U> reducer) {
4102 +        return ForkJoinTasks.reduce
4103 +            (this, transformer, reducer).invoke();
4104 +    }
4105 +
4106 +    /**
4107 +     * Returns the result of accumulating the given transformation
4108 +     * of all (key, value) pairs using the given reducer to
4109 +     * combine values, and the given basis as an identity value.
4110 +     *
4111 +     * @param transformer a function returning the transformation
4112 +     * for an element
4113 +     * @param basis the identity (initial default value) for the reduction
4114 +     * @param reducer a commutative associative combining function
4115 +     * @return the result of accumulating the given transformation
4116 +     * of all (key, value) pairs
4117 +     */
4118 +    public double reduceToDoubleInParallel
4119 +        (ObjectByObjectToDouble<? super K, ? super V> transformer,
4120 +         double basis,
4121 +         DoubleByDoubleToDouble reducer) {
4122 +        return ForkJoinTasks.reduceToDouble
4123 +            (this, transformer, basis, reducer).invoke();
4124 +    }
4125 +
4126 +    /**
4127 +     * Returns the result of accumulating the given transformation
4128 +     * of all (key, value) pairs using the given reducer to
4129 +     * combine values, and the given basis as an identity value.
4130 +     *
4131 +     * @param transformer a function returning the transformation
4132 +     * for an element
4133 +     * @param basis the identity (initial default value) for the reduction
4134 +     * @param reducer a commutative associative combining function
4135 +     * @return the result of accumulating the given transformation
4136 +     * of all (key, value) pairs
4137 +     */
4138 +    public long reduceToLongInParallel
4139 +        (ObjectByObjectToLong<? super K, ? super V> transformer,
4140 +         long basis,
4141 +         LongByLongToLong reducer) {
4142 +        return ForkJoinTasks.reduceToLong
4143 +            (this, transformer, basis, reducer).invoke();
4144 +    }
4145 +
4146 +    /**
4147 +     * Returns the result of accumulating the given transformation
4148 +     * of all (key, value) pairs using the given reducer to
4149 +     * combine values, and the given basis as an identity value.
4150 +     *
4151 +     * @param transformer a function returning the transformation
4152 +     * for an element
4153 +     * @param basis the identity (initial default value) for the reduction
4154 +     * @param reducer a commutative associative combining function
4155 +     * @return the result of accumulating the given transformation
4156 +     * of all (key, value) pairs
4157 +     */
4158 +    public int reduceToIntInParallel
4159 +        (ObjectByObjectToInt<? super K, ? super V> transformer,
4160 +         int basis,
4161 +         IntByIntToInt reducer) {
4162 +        return ForkJoinTasks.reduceToInt
4163 +            (this, transformer, basis, reducer).invoke();
4164 +    }
4165 +
4166 +    /**
4167 +     * Performs the given action for each key.
4168 +     *
4169 +     * @param action the action
4170 +     */
4171 +    public void forEachKeyInParallel(Action<K> action) {
4172 +        ForkJoinTasks.forEachKey
4173 +            (this, action).invoke();
4174 +    }
4175 +
4176 +    /**
4177 +     * Performs the given action for each non-null transformation
4178 +     * of each key.
4179 +     *
4180 +     * @param transformer a function returning the transformation
4181 +     * for an element, or null of there is no transformation (in
4182 +     * which case the action is not applied).
4183 +     * @param action the action
4184 +     */
4185 +    public <U> void forEachKeyInParallel
4186 +        (Fun<? super K, ? extends U> transformer,
4187 +         Action<U> action) {
4188 +        ForkJoinTasks.forEachKey
4189 +            (this, transformer, action).invoke();
4190 +    }
4191 +
4192 +    /**
4193 +     * Returns a non-null result from applying the given search
4194 +     * function on each key, or null if none. Upon success,
4195 +     * further element processing is suppressed and the results of
4196 +     * any other parallel invocations of the search function are
4197 +     * ignored.
4198 +     *
4199 +     * @param searchFunction a function returning a non-null
4200 +     * result on success, else null
4201 +     * @return a non-null result from applying the given search
4202 +     * function on each key, or null if none
4203 +     */
4204 +    public <U> U searchKeysInParallel
4205 +        (Fun<? super K, ? extends U> searchFunction) {
4206 +        return ForkJoinTasks.searchKeys
4207 +            (this, searchFunction).invoke();
4208 +    }
4209 +
4210 +    /**
4211 +     * Returns the result of accumulating all keys using the given
4212 +     * reducer to combine values, or null if none.
4213 +     *
4214 +     * @param reducer a commutative associative combining function
4215 +     * @return the result of accumulating all keys using the given
4216 +     * reducer to combine values, or null if none
4217 +     */
4218 +    public K reduceKeysInParallel
4219 +        (BiFun<? super K, ? super K, ? extends K> reducer) {
4220 +        return ForkJoinTasks.reduceKeys
4221 +            (this, reducer).invoke();
4222 +    }
4223 +
4224 +    /**
4225 +     * Returns the result of accumulating the given transformation
4226 +     * of all keys using the given reducer to combine values, or
4227 +     * null if none.
4228 +     *
4229 +     * @param transformer a function returning the transformation
4230 +     * for an element, or null of there is no transformation (in
4231 +     * which case it is not combined).
4232 +     * @param reducer a commutative associative combining function
4233 +     * @return the result of accumulating the given transformation
4234 +     * of all keys
4235 +     */
4236 +    public <U> U reduceKeysInParallel
4237 +        (Fun<? super K, ? extends U> transformer,
4238 +         BiFun<? super U, ? super U, ? extends U> reducer) {
4239 +        return ForkJoinTasks.reduceKeys
4240 +            (this, transformer, reducer).invoke();
4241 +    }
4242 +
4243 +    /**
4244 +     * Returns the result of accumulating the given transformation
4245 +     * of all keys using the given reducer to combine values, and
4246 +     * the given basis as an identity value.
4247 +     *
4248 +     * @param transformer a function returning the transformation
4249 +     * for an element
4250 +     * @param basis the identity (initial default value) for the reduction
4251 +     * @param reducer a commutative associative combining function
4252 +     * @return  the result of accumulating the given transformation
4253 +     * of all keys
4254 +     */
4255 +    public double reduceKeysToDoubleInParallel
4256 +        (ObjectToDouble<? super K> transformer,
4257 +         double basis,
4258 +         DoubleByDoubleToDouble reducer) {
4259 +        return ForkJoinTasks.reduceKeysToDouble
4260 +            (this, transformer, basis, reducer).invoke();
4261 +    }
4262 +
4263 +    /**
4264 +     * Returns the result of accumulating the given transformation
4265 +     * of all keys using the given reducer to combine values, and
4266 +     * the given basis as an identity value.
4267 +     *
4268 +     * @param transformer a function returning the transformation
4269 +     * for an element
4270 +     * @param basis the identity (initial default value) for the reduction
4271 +     * @param reducer a commutative associative combining function
4272 +     * @return the result of accumulating the given transformation
4273 +     * of all keys
4274 +     */
4275 +    public long reduceKeysToLongInParallel
4276 +        (ObjectToLong<? super K> transformer,
4277 +         long basis,
4278 +         LongByLongToLong reducer) {
4279 +        return ForkJoinTasks.reduceKeysToLong
4280 +            (this, transformer, basis, 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 int reduceKeysToIntInParallel
4296 +        (ObjectToInt<? super K> transformer,
4297 +         int basis,
4298 +         IntByIntToInt reducer) {
4299 +        return ForkJoinTasks.reduceKeysToInt
4300 +            (this, transformer, basis, reducer).invoke();
4301 +    }
4302 +
4303 +    /**
4304 +     * Performs the given action for each value.
4305 +     *
4306 +     * @param action the action
4307 +     */
4308 +    public void forEachValueInParallel(Action<V> action) {
4309 +        ForkJoinTasks.forEachValue
4310 +            (this, action).invoke();
4311 +    }
4312 +
4313 +    /**
4314 +     * Performs the given action for each non-null transformation
4315 +     * of each value.
4316 +     *
4317 +     * @param transformer a function returning the transformation
4318 +     * for an element, or null of there is no transformation (in
4319 +     * which case the action is not applied).
4320 +     */
4321 +    public <U> void forEachValueInParallel
4322 +        (Fun<? super V, ? extends U> transformer,
4323 +         Action<U> action) {
4324 +        ForkJoinTasks.forEachValue
4325 +            (this, transformer, action).invoke();
4326 +    }
4327 +
4328 +    /**
4329 +     * Returns a non-null result from applying the given search
4330 +     * function on each value, or null if none.  Upon success,
4331 +     * further element processing is suppressed and the results of
4332 +     * any other parallel invocations of the search function are
4333 +     * ignored.
4334 +     *
4335 +     * @param searchFunction a function returning a non-null
4336 +     * result on success, else null
4337 +     * @return a non-null result from applying the given search
4338 +     * function on each value, or null if none
4339 +     */
4340 +    public <U> U searchValuesInParallel
4341 +        (Fun<? super V, ? extends U> searchFunction) {
4342 +        return ForkJoinTasks.searchValues
4343 +            (this, searchFunction).invoke();
4344 +    }
4345 +
4346 +    /**
4347 +     * Returns the result of accumulating all values using the
4348 +     * given reducer to combine values, or null if none.
4349 +     *
4350 +     * @param reducer a commutative associative combining function
4351 +     * @return  the result of accumulating all values
4352 +     */
4353 +    public V reduceValuesInParallel
4354 +        (BiFun<? super V, ? super V, ? extends V> reducer) {
4355 +        return ForkJoinTasks.reduceValues
4356 +            (this, reducer).invoke();
4357 +    }
4358 +
4359 +    /**
4360 +     * Returns the result of accumulating the given transformation
4361 +     * of all values using the given reducer to combine values, or
4362 +     * null if none.
4363 +     *
4364 +     * @param transformer a function returning the transformation
4365 +     * for an element, or null of there is no transformation (in
4366 +     * which case it is not combined).
4367 +     * @param reducer a commutative associative combining function
4368 +     * @return the result of accumulating the given transformation
4369 +     * of all values
4370 +     */
4371 +    public <U> U reduceValuesInParallel
4372 +        (Fun<? super V, ? extends U> transformer,
4373 +         BiFun<? super U, ? super U, ? extends U> reducer) {
4374 +        return ForkJoinTasks.reduceValues
4375 +            (this, transformer, reducer).invoke();
4376 +    }
4377 +
4378 +    /**
4379 +     * Returns the result of accumulating the given transformation
4380 +     * of all values using the given reducer to combine values,
4381 +     * and the given basis as an identity value.
4382 +     *
4383 +     * @param transformer a function returning the transformation
4384 +     * for an element
4385 +     * @param basis the identity (initial default value) for the reduction
4386 +     * @param reducer a commutative associative combining function
4387 +     * @return the result of accumulating the given transformation
4388 +     * of all values
4389 +     */
4390 +    public double reduceValuesToDoubleInParallel
4391 +        (ObjectToDouble<? super V> transformer,
4392 +         double basis,
4393 +         DoubleByDoubleToDouble reducer) {
4394 +        return ForkJoinTasks.reduceValuesToDouble
4395 +            (this, transformer, basis, reducer).invoke();
4396 +    }
4397 +
4398 +    /**
4399 +     * Returns the result of accumulating the given transformation
4400 +     * of all values using the given reducer to combine values,
4401 +     * and the given basis as an identity value.
4402 +     *
4403 +     * @param transformer a function returning the transformation
4404 +     * for an element
4405 +     * @param basis the identity (initial default value) for the reduction
4406 +     * @param reducer a commutative associative combining function
4407 +     * @return the result of accumulating the given transformation
4408 +     * of all values
4409 +     */
4410 +    public long reduceValuesToLongInParallel
4411 +        (ObjectToLong<? super V> transformer,
4412 +         long basis,
4413 +         LongByLongToLong reducer) {
4414 +        return ForkJoinTasks.reduceValuesToLong
4415 +            (this, transformer, basis, reducer).invoke();
4416 +    }
4417 +
4418 +    /**
4419 +     * Returns the result of accumulating the given transformation
4420 +     * of all values using the given reducer to combine values,
4421 +     * and the given basis as an identity value.
4422 +     *
4423 +     * @param transformer a function returning the transformation
4424 +     * for an element
4425 +     * @param basis the identity (initial default value) for the reduction
4426 +     * @param reducer a commutative associative combining function
4427 +     * @return the result of accumulating the given transformation
4428 +     * of all values
4429 +     */
4430 +    public int reduceValuesToIntInParallel
4431 +        (ObjectToInt<? super V> transformer,
4432 +         int basis,
4433 +         IntByIntToInt reducer) {
4434 +        return ForkJoinTasks.reduceValuesToInt
4435 +            (this, transformer, basis, reducer).invoke();
4436 +    }
4437 +
4438 +    /**
4439 +     * Performs the given action for each entry.
4440 +     *
4441 +     * @param action the action
4442 +     */
4443 +    public void forEachEntryInParallel(Action<Map.Entry<K,V>> action) {
4444 +        ForkJoinTasks.forEachEntry
4445 +            (this, action).invoke();
4446 +    }
4447 +
4448 +    /**
4449 +     * Performs the given action for each non-null transformation
4450 +     * of each entry.
4451 +     *
4452 +     * @param transformer a function returning the transformation
4453 +     * for an element, or null of there is no transformation (in
4454 +     * which case the action is not applied).
4455 +     * @param action the action
4456 +     */
4457 +    public <U> void forEachEntryInParallel
4458 +        (Fun<Map.Entry<K,V>, ? extends U> transformer,
4459 +         Action<U> action) {
4460 +        ForkJoinTasks.forEachEntry
4461 +            (this, transformer, action).invoke();
4462 +    }
4463 +
4464 +    /**
4465 +     * Returns a non-null result from applying the given search
4466 +     * function on each entry, or null if none.  Upon success,
4467 +     * further element processing is suppressed and the results of
4468 +     * any other parallel invocations of the search function are
4469 +     * ignored.
4470 +     *
4471 +     * @param searchFunction a function returning a non-null
4472 +     * result on success, else null
4473 +     * @return a non-null result from applying the given search
4474 +     * function on each entry, or null if none
4475 +     */
4476 +    public <U> U searchEntriesInParallel
4477 +        (Fun<Map.Entry<K,V>, ? extends U> searchFunction) {
4478 +        return ForkJoinTasks.searchEntries
4479 +            (this, searchFunction).invoke();
4480 +    }
4481 +
4482 +    /**
4483 +     * Returns the result of accumulating all entries using the
4484 +     * given reducer to combine values, or null if none.
4485 +     *
4486 +     * @param reducer a commutative associative combining function
4487 +     * @return the result of accumulating all entries
4488 +     */
4489 +    public Map.Entry<K,V> reduceEntriesInParallel
4490 +        (BiFun<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer) {
4491 +        return ForkJoinTasks.reduceEntries
4492 +            (this, reducer).invoke();
4493 +    }
4494 +
4495 +    /**
4496 +     * Returns the result of accumulating the given transformation
4497 +     * of all entries using the given reducer to combine values,
4498 +     * or null if none.
4499 +     *
4500 +     * @param transformer a function returning the transformation
4501 +     * for an element, or null of there is no transformation (in
4502 +     * which case it is not combined).
4503 +     * @param reducer a commutative associative combining function
4504 +     * @return the result of accumulating the given transformation
4505 +     * of all entries
4506 +     */
4507 +    public <U> U reduceEntriesInParallel
4508 +        (Fun<Map.Entry<K,V>, ? extends U> transformer,
4509 +         BiFun<? super U, ? super U, ? extends U> reducer) {
4510 +        return ForkJoinTasks.reduceEntries
4511 +            (this, transformer, reducer).invoke();
4512 +    }
4513 +
4514 +    /**
4515 +     * Returns the result of accumulating the given transformation
4516 +     * of all entries using the given reducer to combine values,
4517 +     * and the given basis as an identity value.
4518 +     *
4519 +     * @param transformer a function returning the transformation
4520 +     * for an element
4521 +     * @param basis the identity (initial default value) for the reduction
4522 +     * @param reducer a commutative associative combining function
4523 +     * @return the result of accumulating the given transformation
4524 +     * of all entries
4525 +     */
4526 +    public double reduceEntriesToDoubleInParallel
4527 +        (ObjectToDouble<Map.Entry<K,V>> transformer,
4528 +         double basis,
4529 +         DoubleByDoubleToDouble reducer) {
4530 +        return ForkJoinTasks.reduceEntriesToDouble
4531 +            (this, transformer, basis, reducer).invoke();
4532 +    }
4533 +
4534 +    /**
4535 +     * Returns the result of accumulating the given transformation
4536 +     * of all entries using the given reducer to combine values,
4537 +     * and the given basis as an identity value.
4538 +     *
4539 +     * @param transformer a function returning the transformation
4540 +     * for an element
4541 +     * @param basis the identity (initial default value) for the reduction
4542 +     * @param reducer a commutative associative combining function
4543 +     * @return  the result of accumulating the given transformation
4544 +     * of all entries
4545 +     */
4546 +    public long reduceEntriesToLongInParallel
4547 +        (ObjectToLong<Map.Entry<K,V>> transformer,
4548 +         long basis,
4549 +         LongByLongToLong reducer) {
4550 +        return ForkJoinTasks.reduceEntriesToLong
4551 +            (this, transformer, basis, reducer).invoke();
4552 +    }
4553 +
4554 +    /**
4555 +     * Returns the result of accumulating the given transformation
4556 +     * of all entries using the given reducer to combine values,
4557 +     * and the given basis as an identity value.
4558 +     *
4559 +     * @param transformer a function returning the transformation
4560 +     * for an element
4561 +     * @param basis the identity (initial default value) for the reduction
4562 +     * @param reducer a commutative associative combining function
4563 +     * @return the result of accumulating the given transformation
4564 +     * of all entries
4565 +     */
4566 +    public int reduceEntriesToIntInParallel
4567 +        (ObjectToInt<Map.Entry<K,V>> transformer,
4568 +         int basis,
4569 +         IntByIntToInt reducer) {
4570 +        return ForkJoinTasks.reduceEntriesToInt
4571 +            (this, transformer, basis, reducer).invoke();
4572 +    }
4573 +
4574 +
4575 +    /* ----------------Views -------------- */
4576 +
4577 +    /**
4578 +     * Base class for views.
4579 +     */
4580 +    abstract static class CHMView<K, V> {
4581 +        final ConcurrentHashMapV8<K, V> map;
4582 +        CHMView(ConcurrentHashMapV8<K, V> map)  { this.map = map; }
4583  
4584          /**
4585 <         * Performs the given action for each non-null transformation
3894 <         * of each value
4585 >         * Returns the map backing this view.
4586           *
4587 <         * @param transformer a function returning the transformation
3897 <         * for an element, or null of there is no transformation (in
3898 <         * which case the action is not applied).
4587 >         * @return the map backing this view
4588           */
4589 <        public <U> void forEachValue(Fun<? super V, ? extends U> transformer,
4590 <                                     Action<U> action) {
4591 <            fjp.invoke(ForkJoinTasks.forEachValue
4592 <                       (ConcurrentHashMapV8.this, transformer, action));
4589 >        public ConcurrentHashMapV8<K,V> getMap() { return map; }
4590 >
4591 >        public final int size()                 { return map.size(); }
4592 >        public final boolean isEmpty()          { return map.isEmpty(); }
4593 >        public final void clear()               { map.clear(); }
4594 >
4595 >        // implementations below rely on concrete classes supplying these
4596 >        public abstract Iterator<?> iterator();
4597 >        public abstract boolean contains(Object o);
4598 >        public abstract boolean remove(Object o);
4599 >
4600 >        private static final String oomeMsg = "Required array size too large";
4601 >
4602 >        public final Object[] toArray() {
4603 >            long sz = map.mappingCount();
4604 >            if (sz > (long)(MAX_ARRAY_SIZE))
4605 >                throw new OutOfMemoryError(oomeMsg);
4606 >            int n = (int)sz;
4607 >            Object[] r = new Object[n];
4608 >            int i = 0;
4609 >            Iterator<?> it = iterator();
4610 >            while (it.hasNext()) {
4611 >                if (i == n) {
4612 >                    if (n >= MAX_ARRAY_SIZE)
4613 >                        throw new OutOfMemoryError(oomeMsg);
4614 >                    if (n >= MAX_ARRAY_SIZE - (MAX_ARRAY_SIZE >>> 1) - 1)
4615 >                        n = MAX_ARRAY_SIZE;
4616 >                    else
4617 >                        n += (n >>> 1) + 1;
4618 >                    r = Arrays.copyOf(r, n);
4619 >                }
4620 >                r[i++] = it.next();
4621 >            }
4622 >            return (i == n) ? r : Arrays.copyOf(r, i);
4623          }
4624  
4625 <        /**
4626 <         * Returns a non-null result from applying the given search
4627 <         * function on each value, or null if none.  Further element
4628 <         * processing is suppressed upon success. However, this method
4629 <         * does not return until other in-progress parallel
4630 <         * invocations of the search function also complete.
4631 <         *
4632 <         * @param searchFunction a function returning a non-null
4633 <         * result on success, else null
4634 <         * @return a non-null result from applying the given search
4635 <         * function on each value, or null if none
4636 <         *
4637 <         */
4638 <        public <U> U searchValues(Fun<? super V, ? extends U> searchFunction) {
4639 <            return fjp.invoke(ForkJoinTasks.searchValues
4640 <                              (ConcurrentHashMapV8.this, searchFunction));
4625 >        @SuppressWarnings("unchecked") public final <T> T[] toArray(T[] a) {
4626 >            long sz = map.mappingCount();
4627 >            if (sz > (long)(MAX_ARRAY_SIZE))
4628 >                throw new OutOfMemoryError(oomeMsg);
4629 >            int m = (int)sz;
4630 >            T[] r = (a.length >= m) ? a :
4631 >                (T[])java.lang.reflect.Array
4632 >                .newInstance(a.getClass().getComponentType(), m);
4633 >            int n = r.length;
4634 >            int i = 0;
4635 >            Iterator<?> it = iterator();
4636 >            while (it.hasNext()) {
4637 >                if (i == n) {
4638 >                    if (n >= MAX_ARRAY_SIZE)
4639 >                        throw new OutOfMemoryError(oomeMsg);
4640 >                    if (n >= MAX_ARRAY_SIZE - (MAX_ARRAY_SIZE >>> 1) - 1)
4641 >                        n = MAX_ARRAY_SIZE;
4642 >                    else
4643 >                        n += (n >>> 1) + 1;
4644 >                    r = Arrays.copyOf(r, n);
4645 >                }
4646 >                r[i++] = (T)it.next();
4647 >            }
4648 >            if (a == r && i < n) {
4649 >                r[i] = null; // null-terminate
4650 >                return r;
4651 >            }
4652 >            return (i == n) ? r : Arrays.copyOf(r, i);
4653          }
4654  
4655 <        /**
4656 <         * Returns the result of accumulating all values using the
4657 <         * given reducer to combine values, or null if none.
4658 <         *
4659 <         * @param reducer a commutative associative combining function
3929 <         * @return  the result of accumulating all values
3930 <         */
3931 <        public V reduceValues(BiFun<? super V, ? super V, ? extends V> reducer) {
3932 <            return fjp.invoke(ForkJoinTasks.reduceValues
3933 <                              (ConcurrentHashMapV8.this, reducer));
4655 >        public final int hashCode() {
4656 >            int h = 0;
4657 >            for (Iterator<?> it = iterator(); it.hasNext();)
4658 >                h += it.next().hashCode();
4659 >            return h;
4660          }
4661  
4662 <        /**
4663 <         * Returns the result of accumulating the given transformation
4664 <         * of all values using the given reducer to combine values, or
4665 <         * null if none.
4666 <         *
4667 <         * @param transformer a function returning the transformation
4668 <         * for an element, or null of there is no transformation (in
4669 <         * which case it is not combined).
4670 <         * @param reducer a commutative associative combining function
4671 <         * @return the result of accumulating the given transformation
4672 <         * of all values
4673 <         */
4674 <        public <U> U reduceValues(Fun<? super V, ? extends U> transformer,
4675 <                                  BiFun<? super U, ? super U, ? extends U> reducer) {
3950 <            return fjp.invoke(ForkJoinTasks.reduceValues
3951 <                              (ConcurrentHashMapV8.this, transformer, reducer));
4662 >        public final String toString() {
4663 >            StringBuilder sb = new StringBuilder();
4664 >            sb.append('[');
4665 >            Iterator<?> it = iterator();
4666 >            if (it.hasNext()) {
4667 >                for (;;) {
4668 >                    Object e = it.next();
4669 >                    sb.append(e == this ? "(this Collection)" : e);
4670 >                    if (!it.hasNext())
4671 >                        break;
4672 >                    sb.append(',').append(' ');
4673 >                }
4674 >            }
4675 >            return sb.append(']').toString();
4676          }
4677  
4678 <        /**
4679 <         * Returns the result of accumulating the given transformation
4680 <         * of all values using the given reducer to combine values,
4681 <         * and the given basis as an identity value.
4682 <         *
4683 <         * @param transformer a function returning the transformation
4684 <         * for an element
4685 <         * @param basis the identity (initial default value) for the reduction
4686 <         * @param reducer a commutative associative combining function
3963 <         * @return the result of accumulating the given transformation
3964 <         * of all values
3965 <         */
3966 <        public double reduceValuesToDouble(ObjectToDouble<? super V> transformer,
3967 <                                           double basis,
3968 <                                           DoubleByDoubleToDouble reducer) {
3969 <            return fjp.invoke(ForkJoinTasks.reduceValuesToDouble
3970 <                              (ConcurrentHashMapV8.this, transformer, basis, reducer));
4678 >        public final boolean containsAll(Collection<?> c) {
4679 >            if (c != this) {
4680 >                for (Iterator<?> it = c.iterator(); it.hasNext();) {
4681 >                    Object e = it.next();
4682 >                    if (e == null || !contains(e))
4683 >                        return false;
4684 >                }
4685 >            }
4686 >            return true;
4687          }
4688  
4689 <        /**
4690 <         * Returns the result of accumulating the given transformation
4691 <         * of all values using the given reducer to combine values,
4692 <         * and the given basis as an identity value.
4693 <         *
4694 <         * @param transformer a function returning the transformation
4695 <         * for an element
4696 <         * @param basis the identity (initial default value) for the reduction
4697 <         * @param reducer a commutative associative combining function
3982 <         * @return the result of accumulating the given transformation
3983 <         * of all values
3984 <         */
3985 <        public long reduceValuesToLong(ObjectToLong<? super V> transformer,
3986 <                                       long basis,
3987 <                                       LongByLongToLong reducer) {
3988 <            return fjp.invoke(ForkJoinTasks.reduceValuesToLong
3989 <                              (ConcurrentHashMapV8.this, transformer, basis, reducer));
4689 >        public final boolean removeAll(Collection<?> c) {
4690 >            boolean modified = false;
4691 >            for (Iterator<?> it = iterator(); it.hasNext();) {
4692 >                if (c.contains(it.next())) {
4693 >                    it.remove();
4694 >                    modified = true;
4695 >                }
4696 >            }
4697 >            return modified;
4698          }
4699  
4700 <        /**
4701 <         * Returns the result of accumulating the given transformation
4702 <         * of all values using the given reducer to combine values,
4703 <         * and the given basis as an identity value.
4704 <         *
4705 <         * @param transformer a function returning the transformation
4706 <         * for an element
4707 <         * @param basis the identity (initial default value) for the reduction
4708 <         * @param reducer a commutative associative combining function
4001 <         * @return the result of accumulating the given transformation
4002 <         * of all values
4003 <         */
4004 <        public int reduceValuesToInt(ObjectToInt<? super V> transformer,
4005 <                                     int basis,
4006 <                                     IntByIntToInt reducer) {
4007 <            return fjp.invoke(ForkJoinTasks.reduceValuesToInt
4008 <                              (ConcurrentHashMapV8.this, transformer, basis, reducer));
4700 >        public final boolean retainAll(Collection<?> c) {
4701 >            boolean modified = false;
4702 >            for (Iterator<?> it = iterator(); it.hasNext();) {
4703 >                if (!c.contains(it.next())) {
4704 >                    it.remove();
4705 >                    modified = true;
4706 >                }
4707 >            }
4708 >            return modified;
4709          }
4710  
4711 <        /**
4712 <         * Perform the given action for each entry
4713 <         *
4714 <         * @param action the action
4715 <         */
4716 <        public void forEachEntry(Action<Map.Entry<K,V>> action) {
4717 <            fjp.invoke(ForkJoinTasks.forEachEntry
4718 <                       (ConcurrentHashMapV8.this, action));
4711 >    }
4712 >
4713 >    /**
4714 >     * A view of a ConcurrentHashMapV8 as a {@link Set} of keys, in
4715 >     * which additions may optionally be enabled by mapping to a
4716 >     * common value.  This class cannot be directly instantiated. See
4717 >     * {@link #keySet}, {@link #keySet(Object)}, {@link #newKeySet()},
4718 >     * {@link #newKeySet(int)}.
4719 >     */
4720 >    public static class KeySetView<K,V> extends CHMView<K,V>
4721 >        implements Set<K>, java.io.Serializable {
4722 >        private static final long serialVersionUID = 7249069246763182397L;
4723 >        private final V value;
4724 >        KeySetView(ConcurrentHashMapV8<K, V> map, V value) {  // non-public
4725 >            super(map);
4726 >            this.value = value;
4727          }
4728  
4729          /**
4730 <         * Perform the given action for each non-null transformation
4731 <         * of each entry
4730 >         * Returns the default mapped value for additions,
4731 >         * or {@code null} if additions are not supported.
4732           *
4733 <         * @param transformer a function returning the transformation
4734 <         * for an element, or null of there is no transformation (in
4027 <         * which case the action is not applied).
4028 <         * @param action the action
4733 >         * @return the default mapped value for additions, or {@code null}
4734 >         * if not supported.
4735           */
4736 <        public <U> void forEachEntry(Fun<Map.Entry<K,V>, ? extends U> transformer,
4737 <                                     Action<U> action) {
4738 <            fjp.invoke(ForkJoinTasks.forEachEntry
4739 <                       (ConcurrentHashMapV8.this, transformer, action));
4740 <        }
4736 >        public V getMappedValue() { return value; }
4737 >
4738 >        // implement Set API
4739 >
4740 >        public boolean contains(Object o) { return map.containsKey(o); }
4741 >        public boolean remove(Object o)   { return map.remove(o) != null; }
4742  
4743          /**
4744 <         * Returns a non-null result from applying the given search
4745 <         * function on each entry, or null if none.  Further element
4746 <         * processing is suppressed upon success. However, this method
4747 <         * does not return until other in-progress parallel
4748 <         * invocations of the search function also complete.
4744 >         * Returns a "weakly consistent" iterator that will never
4745 >         * throw {@link ConcurrentModificationException}, and
4746 >         * guarantees to traverse elements as they existed upon
4747 >         * construction of the iterator, and may (but is not
4748 >         * guaranteed to) reflect any modifications subsequent to
4749 >         * construction.
4750           *
4751 <         * @param searchFunction a function returning a non-null
4044 <         * result on success, else null
4045 <         * @return a non-null result from applying the given search
4046 <         * function on each entry, or null if none
4751 >         * @return an iterator over the keys of this map
4752           */
4753 <        public <U> U searchEntries(Fun<Map.Entry<K,V>, ? extends U> searchFunction) {
4754 <            return fjp.invoke(ForkJoinTasks.searchEntries
4755 <                              (ConcurrentHashMapV8.this, searchFunction));
4753 >        public Iterator<K> iterator()     { return new KeyIterator<K,V>(map); }
4754 >        public boolean add(K e) {
4755 >            V v;
4756 >            if ((v = value) == null)
4757 >                throw new UnsupportedOperationException();
4758 >            if (e == null)
4759 >                throw new NullPointerException();
4760 >            return map.internalPut(e, v, true) == null;
4761 >        }
4762 >        public boolean addAll(Collection<? extends K> c) {
4763 >            boolean added = false;
4764 >            V v;
4765 >            if ((v = value) == null)
4766 >                throw new UnsupportedOperationException();
4767 >            for (K e : c) {
4768 >                if (e == null)
4769 >                    throw new NullPointerException();
4770 >                if (map.internalPut(e, v, true) == null)
4771 >                    added = true;
4772 >            }
4773 >            return added;
4774 >        }
4775 >        public boolean equals(Object o) {
4776 >            Set<?> c;
4777 >            return ((o instanceof Set) &&
4778 >                    ((c = (Set<?>)o) == this ||
4779 >                     (containsAll(c) && c.containsAll(this))));
4780          }
4781 +    }
4782  
4783 <        /**
4784 <         * Returns the result of accumulating all entries using the
4785 <         * given reducer to combine values, or null if none.
4786 <         *
4787 <         * @param reducer a commutative associative combining function
4788 <         * @return the result of accumulating all entries
4789 <         */
4790 <        public Map.Entry<K,V> reduceEntries(BiFun<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer) {
4791 <            return fjp.invoke(ForkJoinTasks.reduceEntries
4792 <                              (ConcurrentHashMapV8.this, reducer));
4783 >    /**
4784 >     * A view of a ConcurrentHashMapV8 as a {@link Collection} of
4785 >     * values, in which additions are disabled. This class cannot be
4786 >     * directly instantiated. See {@link #values},
4787 >     *
4788 >     * <p>The view's {@code iterator} is a "weakly consistent" iterator
4789 >     * that will never throw {@link ConcurrentModificationException},
4790 >     * and guarantees to traverse elements as they existed upon
4791 >     * construction of the iterator, and may (but is not guaranteed to)
4792 >     * reflect any modifications subsequent to construction.
4793 >     */
4794 >    public static final class ValuesView<K,V> extends CHMView<K,V>
4795 >        implements Collection<V> {
4796 >        ValuesView(ConcurrentHashMapV8<K, V> map)   { super(map); }
4797 >        public final boolean contains(Object o) { return map.containsValue(o); }
4798 >        public final boolean remove(Object o) {
4799 >            if (o != null) {
4800 >                Iterator<V> it = new ValueIterator<K,V>(map);
4801 >                while (it.hasNext()) {
4802 >                    if (o.equals(it.next())) {
4803 >                        it.remove();
4804 >                        return true;
4805 >                    }
4806 >                }
4807 >            }
4808 >            return false;
4809          }
4810  
4811          /**
4812 <         * Returns the result of accumulating the given transformation
4813 <         * of all entries using the given reducer to combine values,
4814 <         * or null if none.
4812 >         * Returns a "weakly consistent" iterator that will never
4813 >         * throw {@link ConcurrentModificationException}, and
4814 >         * guarantees to traverse elements as they existed upon
4815 >         * construction of the iterator, and may (but is not
4816 >         * guaranteed to) reflect any modifications subsequent to
4817 >         * construction.
4818           *
4819 <         * @param transformer a function returning the transformation
4071 <         * for an element, or null of there is no transformation (in
4072 <         * which case it is not combined).
4073 <         * @param reducer a commutative associative combining function
4074 <         * @return the result of accumulating the given transformation
4075 <         * of all entries
4819 >         * @return an iterator over the values of this map
4820           */
4821 <        public <U> U reduceEntries(Fun<Map.Entry<K,V>, ? extends U> transformer,
4822 <                                   BiFun<? super U, ? super U, ? extends U> reducer) {
4823 <            return fjp.invoke(ForkJoinTasks.reduceEntries
4824 <                              (ConcurrentHashMapV8.this, transformer, reducer));
4821 >        public final Iterator<V> iterator() {
4822 >            return new ValueIterator<K,V>(map);
4823 >        }
4824 >        public final boolean add(V e) {
4825 >            throw new UnsupportedOperationException();
4826 >        }
4827 >        public final boolean addAll(Collection<? extends V> c) {
4828 >            throw new UnsupportedOperationException();
4829          }
4830  
4831 <        /**
4832 <         * Returns the result of accumulating the given transformation
4833 <         * of all entries using the given reducer to combine values,
4834 <         * and the given basis as an identity value.
4835 <         *
4836 <         * @param transformer a function returning the transformation
4837 <         * for an element
4838 <         * @param basis the identity (initial default value) for the reduction
4839 <         * @param reducer a commutative associative combining function
4840 <         * @return the result of accumulating the given transformation
4841 <         * of all entries
4842 <         */
4843 <        public double reduceEntriesToDouble(ObjectToDouble<Map.Entry<K,V>> transformer,
4844 <                                            double basis,
4845 <                                            DoubleByDoubleToDouble reducer) {
4846 <            return fjp.invoke(ForkJoinTasks.reduceEntriesToDouble
4847 <                              (ConcurrentHashMapV8.this, transformer, basis, reducer));
4831 >    }
4832 >
4833 >    /**
4834 >     * A view of a ConcurrentHashMapV8 as a {@link Set} of (key, value)
4835 >     * entries.  This class cannot be directly instantiated. See
4836 >     * {@link #entrySet}.
4837 >     */
4838 >    public static final class EntrySetView<K,V> extends CHMView<K,V>
4839 >        implements Set<Map.Entry<K,V>> {
4840 >        EntrySetView(ConcurrentHashMapV8<K, V> map) { super(map); }
4841 >        public final boolean contains(Object o) {
4842 >            Object k, v, r; Map.Entry<?,?> e;
4843 >            return ((o instanceof Map.Entry) &&
4844 >                    (k = (e = (Map.Entry<?,?>)o).getKey()) != null &&
4845 >                    (r = map.get(k)) != null &&
4846 >                    (v = e.getValue()) != null &&
4847 >                    (v == r || v.equals(r)));
4848 >        }
4849 >        public final boolean remove(Object o) {
4850 >            Object k, v; Map.Entry<?,?> e;
4851 >            return ((o instanceof Map.Entry) &&
4852 >                    (k = (e = (Map.Entry<?,?>)o).getKey()) != null &&
4853 >                    (v = e.getValue()) != null &&
4854 >                    map.remove(k, v));
4855          }
4856  
4857          /**
4858 <         * Returns the result of accumulating the given transformation
4859 <         * of all entries using the given reducer to combine values,
4860 <         * and the given basis as an identity value.
4858 >         * Returns a "weakly consistent" iterator that will never
4859 >         * throw {@link ConcurrentModificationException}, and
4860 >         * guarantees to traverse elements as they existed upon
4861 >         * construction of the iterator, and may (but is not
4862 >         * guaranteed to) reflect any modifications subsequent to
4863 >         * construction.
4864           *
4865 <         * @param transformer a function returning the transformation
4108 <         * for an element
4109 <         * @param basis the identity (initial default value) for the reduction
4110 <         * @param reducer a commutative associative combining function
4111 <         * @return  the result of accumulating the given transformation
4112 <         * of all entries
4865 >         * @return an iterator over the entries of this map
4866           */
4867 <        public long reduceEntriesToLong(ObjectToLong<Map.Entry<K,V>> transformer,
4868 <                                        long basis,
4116 <                                        LongByLongToLong reducer) {
4117 <            return fjp.invoke(ForkJoinTasks.reduceEntriesToLong
4118 <                              (ConcurrentHashMapV8.this, transformer, basis, reducer));
4867 >        public final Iterator<Map.Entry<K,V>> iterator() {
4868 >            return new EntryIterator<K,V>(map);
4869          }
4870  
4871 <        /**
4872 <         * Returns the result of accumulating the given transformation
4873 <         * of all entries using the given reducer to combine values,
4874 <         * and the given basis as an identity value.
4875 <         *
4876 <         * @param transformer a function returning the transformation
4877 <         * for an element
4878 <         * @param basis the identity (initial default value) for the reduction
4879 <         * @param reducer a commutative associative combining function
4880 <         * @return the result of accumulating the given transformation
4881 <         * of all entries
4882 <         */
4883 <        public int reduceEntriesToInt(ObjectToInt<Map.Entry<K,V>> transformer,
4884 <                                      int basis,
4885 <                                      IntByIntToInt reducer) {
4886 <            return fjp.invoke(ForkJoinTasks.reduceEntriesToInt
4887 <                              (ConcurrentHashMapV8.this, transformer, basis, reducer));
4871 >        public final boolean add(Entry<K,V> e) {
4872 >            K key = e.getKey();
4873 >            V value = e.getValue();
4874 >            if (key == null || value == null)
4875 >                throw new NullPointerException();
4876 >            return map.internalPut(key, value, false) == null;
4877 >        }
4878 >        public final boolean addAll(Collection<? extends Entry<K,V>> c) {
4879 >            boolean added = false;
4880 >            for (Entry<K,V> e : c) {
4881 >                if (add(e))
4882 >                    added = true;
4883 >            }
4884 >            return added;
4885 >        }
4886 >        public boolean equals(Object o) {
4887 >            Set<?> c;
4888 >            return ((o instanceof Set) &&
4889 >                    ((c = (Set<?>)o) == this ||
4890 >                     (containsAll(c) && c.containsAll(this))));
4891          }
4892      }
4893  
# Line 4142 | Line 4895 | public class ConcurrentHashMapV8<K, V>
4895  
4896      /**
4897       * Predefined tasks for performing bulk parallel operations on
4898 <     * ConcurrentHashMaps. These tasks follow the forms and rules used
4899 <     * in class {@link Parallel}. Each method has the same name, but
4900 <     * returns a task rather than invoking it. These methods may be
4901 <     * useful in custom applications such as submitting a task without
4902 <     * waiting for completion, or combining with other tasks.
4898 >     * ConcurrentHashMapV8s. These tasks follow the forms and rules used
4899 >     * for bulk operations. Each method has the same name, but returns
4900 >     * a task rather than invoking it. These methods may be useful in
4901 >     * custom applications such as submitting a task without waiting
4902 >     * for completion, using a custom pool, or combining with other
4903 >     * tasks.
4904       */
4905      public static class ForkJoinTasks {
4906          private ForkJoinTasks() {}
# Line 4159 | Line 4913 | public class ConcurrentHashMapV8<K, V>
4913           * @param action the action
4914           * @return the task
4915           */
4916 <        public static <K,V> ForkJoinTask<Void> forEach
4916 >        public static <K,V> ForkJoinTask<Void> forEach
4917              (ConcurrentHashMapV8<K,V> map,
4918               BiAction<K,V> action) {
4919              if (action == null) throw new NullPointerException();
4920 <            return new ForEachMappingTask<K,V>(map, action);
4920 >            return new ForEachMappingTask<K,V>(map, null, -1, action);
4921          }
4922  
4923          /**
# Line 4172 | Line 4926 | public class ConcurrentHashMapV8<K, V>
4926           *
4927           * @param map the map
4928           * @param transformer a function returning the transformation
4929 <         * for an element, or null of there is no transformation (in
4930 <         * which case the action is not applied).
4929 >         * for an element, or null if there is no transformation (in
4930 >         * which case the action is not applied)
4931           * @param action the action
4932           * @return the task
4933           */
4934 <        public static <K,V,U> ForkJoinTask<Void> forEach
4934 >        public static <K,V,U> ForkJoinTask<Void> forEach
4935              (ConcurrentHashMapV8<K,V> map,
4936               BiFun<? super K, ? super V, ? extends U> transformer,
4937               Action<U> action) {
4938              if (transformer == null || action == null)
4939                  throw new NullPointerException();
4940              return new ForEachTransformedMappingTask<K,V,U>
4941 <                (map, transformer, action);
4941 >                (map, null, -1, transformer, action);
4942          }
4943  
4944          /**
4945 <         * Returns a task that when invoked, returns a non-null
4946 <         * result from applying the given search function on each
4947 <         * (key, value), or null if none.  Further element processing
4948 <         * is suppressed upon success. However, this method does not
4949 <         * return until other in-progress parallel invocations of the
4196 <         * search function also complete.
4945 >         * Returns a task that when invoked, returns a non-null result
4946 >         * from applying the given search function on each (key,
4947 >         * value), or null if none. Upon success, further element
4948 >         * processing is suppressed and the results of any other
4949 >         * parallel invocations of the search function are ignored.
4950           *
4951           * @param map the map
4952           * @param searchFunction a function returning a non-null
# Line 4205 | Line 4958 | public class ConcurrentHashMapV8<K, V>
4958               BiFun<? super K, ? super V, ? extends U> searchFunction) {
4959              if (searchFunction == null) throw new NullPointerException();
4960              return new SearchMappingsTask<K,V,U>
4961 <                (map, searchFunction,
4961 >                (map, null, -1, searchFunction,
4962                   new AtomicReference<U>());
4963          }
4964  
# Line 4216 | Line 4969 | public class ConcurrentHashMapV8<K, V>
4969           *
4970           * @param map the map
4971           * @param transformer a function returning the transformation
4972 <         * for an element, or null of there is no transformation (in
4972 >         * for an element, or null if there is no transformation (in
4973           * which case it is not combined).
4974           * @param reducer a commutative associative combining function
4975           * @return the task
# Line 4228 | Line 4981 | public class ConcurrentHashMapV8<K, V>
4981              if (transformer == null || reducer == null)
4982                  throw new NullPointerException();
4983              return new MapReduceMappingsTask<K,V,U>
4984 <                (map, transformer, reducer);
4984 >                (map, null, -1, null, transformer, reducer);
4985          }
4986  
4987          /**
# Line 4252 | Line 5005 | public class ConcurrentHashMapV8<K, V>
5005              if (transformer == null || reducer == null)
5006                  throw new NullPointerException();
5007              return new MapReduceMappingsToDoubleTask<K,V>
5008 <                (map, transformer, basis, reducer);
5008 >                (map, null, -1, null, transformer, basis, reducer);
5009          }
5010  
5011          /**
# Line 4276 | Line 5029 | public class ConcurrentHashMapV8<K, V>
5029              if (transformer == null || reducer == null)
5030                  throw new NullPointerException();
5031              return new MapReduceMappingsToLongTask<K,V>
5032 <                (map, transformer, basis, reducer);
5032 >                (map, null, -1, null, transformer, basis, reducer);
5033          }
5034  
5035          /**
# Line 4299 | Line 5052 | public class ConcurrentHashMapV8<K, V>
5052              if (transformer == null || reducer == null)
5053                  throw new NullPointerException();
5054              return new MapReduceMappingsToIntTask<K,V>
5055 <                (map, transformer, basis, reducer);
5055 >                (map, null, -1, null, transformer, basis, reducer);
5056          }
5057  
5058          /**
5059           * Returns a task that when invoked, performs the given action
5060 <         * for each key
5060 >         * for each key.
5061           *
5062           * @param map the map
5063           * @param action the action
5064           * @return the task
5065           */
5066 <        public static <K,V> ForkJoinTask<Void> forEachKey
5066 >        public static <K,V> ForkJoinTask<Void> forEachKey
5067              (ConcurrentHashMapV8<K,V> map,
5068               Action<K> action) {
5069              if (action == null) throw new NullPointerException();
5070 <            return new ForEachKeyTask<K,V>(map, action);
5070 >            return new ForEachKeyTask<K,V>(map, null, -1, action);
5071          }
5072  
5073          /**
5074           * Returns a task that when invoked, performs the given action
5075 <         * for each non-null transformation of each key
5075 >         * for each non-null transformation of each key.
5076           *
5077           * @param map the map
5078           * @param transformer a function returning the transformation
5079 <         * for an element, or null of there is no transformation (in
5080 <         * which case the action is not applied).
5079 >         * for an element, or null if there is no transformation (in
5080 >         * which case the action is not applied)
5081           * @param action the action
5082           * @return the task
5083           */
5084 <        public static <K,V,U> ForkJoinTask<Void> forEachKey
5084 >        public static <K,V,U> ForkJoinTask<Void> forEachKey
5085              (ConcurrentHashMapV8<K,V> map,
5086               Fun<? super K, ? extends U> transformer,
5087               Action<U> action) {
5088              if (transformer == null || action == null)
5089                  throw new NullPointerException();
5090              return new ForEachTransformedKeyTask<K,V,U>
5091 <                (map, transformer, action);
5091 >                (map, null, -1, transformer, action);
5092          }
5093  
5094          /**
5095           * Returns a task that when invoked, returns a non-null result
5096           * from applying the given search function on each key, or
5097 <         * null if none.  Further element processing is suppressed
5098 <         * upon success. However, this method does not return until
5099 <         * other in-progress parallel invocations of the search
4347 <         * function also complete.
5097 >         * null if none.  Upon success, further element processing is
5098 >         * suppressed and the results of any other parallel
5099 >         * invocations of the search function are ignored.
5100           *
5101           * @param map the map
5102           * @param searchFunction a function returning a non-null
# Line 4356 | Line 5108 | public class ConcurrentHashMapV8<K, V>
5108               Fun<? super K, ? extends U> searchFunction) {
5109              if (searchFunction == null) throw new NullPointerException();
5110              return new SearchKeysTask<K,V,U>
5111 <                (map, searchFunction,
5111 >                (map, null, -1, searchFunction,
5112                   new AtomicReference<U>());
5113          }
5114  
# Line 4374 | Line 5126 | public class ConcurrentHashMapV8<K, V>
5126               BiFun<? super K, ? super K, ? extends K> reducer) {
5127              if (reducer == null) throw new NullPointerException();
5128              return new ReduceKeysTask<K,V>
5129 <                (map, reducer);
5129 >                (map, null, -1, null, reducer);
5130          }
5131 +
5132          /**
5133           * Returns a task that when invoked, returns the result of
5134           * accumulating the given transformation of all keys using the given
# Line 4383 | Line 5136 | public class ConcurrentHashMapV8<K, V>
5136           *
5137           * @param map the map
5138           * @param transformer a function returning the transformation
5139 <         * for an element, or null of there is no transformation (in
5139 >         * for an element, or null if there is no transformation (in
5140           * which case it is not combined).
5141           * @param reducer a commutative associative combining function
5142           * @return the task
# Line 4395 | Line 5148 | public class ConcurrentHashMapV8<K, V>
5148              if (transformer == null || reducer == null)
5149                  throw new NullPointerException();
5150              return new MapReduceKeysTask<K,V,U>
5151 <                (map, transformer, reducer);
5151 >                (map, null, -1, null, transformer, reducer);
5152          }
5153  
5154          /**
# Line 4419 | Line 5172 | public class ConcurrentHashMapV8<K, V>
5172              if (transformer == null || reducer == null)
5173                  throw new NullPointerException();
5174              return new MapReduceKeysToDoubleTask<K,V>
5175 <                (map, transformer, basis, reducer);
5175 >                (map, null, -1, null, transformer, basis, reducer);
5176          }
5177  
5178          /**
# Line 4443 | Line 5196 | public class ConcurrentHashMapV8<K, V>
5196              if (transformer == null || reducer == null)
5197                  throw new NullPointerException();
5198              return new MapReduceKeysToLongTask<K,V>
5199 <                (map, transformer, basis, reducer);
5199 >                (map, null, -1, null, transformer, basis, reducer);
5200          }
5201  
5202          /**
# Line 4467 | Line 5220 | public class ConcurrentHashMapV8<K, V>
5220              if (transformer == null || reducer == null)
5221                  throw new NullPointerException();
5222              return new MapReduceKeysToIntTask<K,V>
5223 <                (map, transformer, basis, reducer);
5223 >                (map, null, -1, null, transformer, basis, reducer);
5224          }
5225  
5226          /**
5227           * Returns a task that when invoked, performs the given action
5228 <         * for each value
5228 >         * for each value.
5229           *
5230           * @param map the map
5231           * @param action the action
5232           */
5233 <        public static <K,V> ForkJoinTask<Void> forEachValue
5233 >        public static <K,V> ForkJoinTask<Void> forEachValue
5234              (ConcurrentHashMapV8<K,V> map,
5235               Action<V> action) {
5236              if (action == null) throw new NullPointerException();
5237 <            return new ForEachValueTask<K,V>(map, action);
5237 >            return new ForEachValueTask<K,V>(map, null, -1, action);
5238          }
5239  
5240          /**
5241           * Returns a task that when invoked, performs the given action
5242 <         * for each non-null transformation of each value
5242 >         * for each non-null transformation of each value.
5243           *
5244           * @param map the map
5245           * @param transformer a function returning the transformation
5246 <         * for an element, or null of there is no transformation (in
5247 <         * which case the action is not applied).
5246 >         * for an element, or null if there is no transformation (in
5247 >         * which case the action is not applied)
5248           * @param action the action
5249           */
5250 <        public static <K,V,U> ForkJoinTask<Void> forEachValue
5250 >        public static <K,V,U> ForkJoinTask<Void> forEachValue
5251              (ConcurrentHashMapV8<K,V> map,
5252               Fun<? super V, ? extends U> transformer,
5253               Action<U> action) {
5254              if (transformer == null || action == null)
5255                  throw new NullPointerException();
5256              return new ForEachTransformedValueTask<K,V,U>
5257 <                (map, transformer, action);
5257 >                (map, null, -1, transformer, action);
5258          }
5259  
5260          /**
5261           * Returns a task that when invoked, returns a non-null result
5262           * from applying the given search function on each value, or
5263 <         * null if none.  Further element processing is suppressed
5264 <         * upon success. However, this method does not return until
5265 <         * other in-progress parallel invocations of the search
4513 <         * function also complete.
5263 >         * null if none.  Upon success, further element processing is
5264 >         * suppressed and the results of any other parallel
5265 >         * invocations of the search function are ignored.
5266           *
5267           * @param map the map
5268           * @param searchFunction a function returning a non-null
5269           * result on success, else null
5270           * @return the task
4519         *
5271           */
5272          public static <K,V,U> ForkJoinTask<U> searchValues
5273              (ConcurrentHashMapV8<K,V> map,
5274               Fun<? super V, ? extends U> searchFunction) {
5275              if (searchFunction == null) throw new NullPointerException();
5276              return new SearchValuesTask<K,V,U>
5277 <                (map, searchFunction,
5277 >                (map, null, -1, searchFunction,
5278                   new AtomicReference<U>());
5279          }
5280  
# Line 4541 | Line 5292 | public class ConcurrentHashMapV8<K, V>
5292               BiFun<? super V, ? super V, ? extends V> reducer) {
5293              if (reducer == null) throw new NullPointerException();
5294              return new ReduceValuesTask<K,V>
5295 <                (map, reducer);
5295 >                (map, null, -1, null, reducer);
5296          }
5297  
5298          /**
# Line 4551 | Line 5302 | public class ConcurrentHashMapV8<K, V>
5302           *
5303           * @param map the map
5304           * @param transformer a function returning the transformation
5305 <         * for an element, or null of there is no transformation (in
5305 >         * for an element, or null if there is no transformation (in
5306           * which case it is not combined).
5307           * @param reducer a commutative associative combining function
5308           * @return the task
# Line 4563 | Line 5314 | public class ConcurrentHashMapV8<K, V>
5314              if (transformer == null || reducer == null)
5315                  throw new NullPointerException();
5316              return new MapReduceValuesTask<K,V,U>
5317 <                (map, transformer, reducer);
5317 >                (map, null, -1, null, transformer, reducer);
5318          }
5319  
5320          /**
# Line 4587 | Line 5338 | public class ConcurrentHashMapV8<K, V>
5338              if (transformer == null || reducer == null)
5339                  throw new NullPointerException();
5340              return new MapReduceValuesToDoubleTask<K,V>
5341 <                (map, transformer, basis, reducer);
5341 >                (map, null, -1, null, transformer, basis, reducer);
5342          }
5343  
5344          /**
# Line 4611 | Line 5362 | public class ConcurrentHashMapV8<K, V>
5362              if (transformer == null || reducer == null)
5363                  throw new NullPointerException();
5364              return new MapReduceValuesToLongTask<K,V>
5365 <                (map, transformer, basis, reducer);
5365 >                (map, null, -1, null, transformer, basis, reducer);
5366          }
5367  
5368          /**
# Line 4635 | Line 5386 | public class ConcurrentHashMapV8<K, V>
5386              if (transformer == null || reducer == null)
5387                  throw new NullPointerException();
5388              return new MapReduceValuesToIntTask<K,V>
5389 <                (map, transformer, basis, reducer);
5389 >                (map, null, -1, null, transformer, basis, reducer);
5390          }
5391  
5392          /**
5393           * Returns a task that when invoked, perform the given action
5394 <         * for each entry
5394 >         * for each entry.
5395           *
5396           * @param map the map
5397           * @param action the action
5398           */
5399 <        public static <K,V> ForkJoinTask<Void> forEachEntry
5399 >        public static <K,V> ForkJoinTask<Void> forEachEntry
5400              (ConcurrentHashMapV8<K,V> map,
5401               Action<Map.Entry<K,V>> action) {
5402              if (action == null) throw new NullPointerException();
5403 <            return new ForEachEntryTask<K,V>(map, action);
5403 >            return new ForEachEntryTask<K,V>(map, null, -1, action);
5404          }
5405  
5406          /**
5407           * Returns a task that when invoked, perform the given action
5408 <         * for each non-null transformation of each entry
5408 >         * for each non-null transformation of each entry.
5409           *
5410           * @param map the map
5411           * @param transformer a function returning the transformation
5412 <         * for an element, or null of there is no transformation (in
5413 <         * which case the action is not applied).
5412 >         * for an element, or null if there is no transformation (in
5413 >         * which case the action is not applied)
5414           * @param action the action
5415           */
5416 <        public static <K,V,U> ForkJoinTask<Void> forEachEntry
5416 >        public static <K,V,U> ForkJoinTask<Void> forEachEntry
5417              (ConcurrentHashMapV8<K,V> map,
5418               Fun<Map.Entry<K,V>, ? extends U> transformer,
5419               Action<U> action) {
5420              if (transformer == null || action == null)
5421                  throw new NullPointerException();
5422              return new ForEachTransformedEntryTask<K,V,U>
5423 <                (map, transformer, action);
5423 >                (map, null, -1, transformer, action);
5424          }
5425  
5426          /**
5427           * Returns a task that when invoked, returns a non-null result
5428           * from applying the given search function on each entry, or
5429 <         * null if none.  Further element processing is suppressed
5430 <         * upon success. However, this method does not return until
5431 <         * other in-progress parallel invocations of the search
4681 <         * function also complete.
5429 >         * null if none.  Upon success, further element processing is
5430 >         * suppressed and the results of any other parallel
5431 >         * invocations of the search function are ignored.
5432           *
5433           * @param map the map
5434           * @param searchFunction a function returning a non-null
5435           * result on success, else null
5436           * @return the task
4687         *
5437           */
5438          public static <K,V,U> ForkJoinTask<U> searchEntries
5439              (ConcurrentHashMapV8<K,V> map,
5440               Fun<Map.Entry<K,V>, ? extends U> searchFunction) {
5441              if (searchFunction == null) throw new NullPointerException();
5442              return new SearchEntriesTask<K,V,U>
5443 <                (map, searchFunction,
5443 >                (map, null, -1, searchFunction,
5444                   new AtomicReference<U>());
5445          }
5446  
# Line 4709 | Line 5458 | public class ConcurrentHashMapV8<K, V>
5458               BiFun<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer) {
5459              if (reducer == null) throw new NullPointerException();
5460              return new ReduceEntriesTask<K,V>
5461 <                (map, reducer);
5461 >                (map, null, -1, null, reducer);
5462          }
5463  
5464          /**
# Line 4719 | Line 5468 | public class ConcurrentHashMapV8<K, V>
5468           *
5469           * @param map the map
5470           * @param transformer a function returning the transformation
5471 <         * for an element, or null of there is no transformation (in
5471 >         * for an element, or null if there is no transformation (in
5472           * which case it is not combined).
5473           * @param reducer a commutative associative combining function
5474           * @return the task
# Line 4731 | Line 5480 | public class ConcurrentHashMapV8<K, V>
5480              if (transformer == null || reducer == null)
5481                  throw new NullPointerException();
5482              return new MapReduceEntriesTask<K,V,U>
5483 <                (map, transformer, reducer);
5483 >                (map, null, -1, null, transformer, reducer);
5484          }
5485  
5486          /**
# Line 4755 | Line 5504 | public class ConcurrentHashMapV8<K, V>
5504              if (transformer == null || reducer == null)
5505                  throw new NullPointerException();
5506              return new MapReduceEntriesToDoubleTask<K,V>
5507 <                (map, transformer, basis, reducer);
5507 >                (map, null, -1, null, transformer, basis, reducer);
5508          }
5509  
5510          /**
# Line 4779 | Line 5528 | public class ConcurrentHashMapV8<K, V>
5528              if (transformer == null || reducer == null)
5529                  throw new NullPointerException();
5530              return new MapReduceEntriesToLongTask<K,V>
5531 <                (map, transformer, basis, reducer);
5531 >                (map, null, -1, null, transformer, basis, reducer);
5532          }
5533  
5534          /**
# Line 4803 | Line 5552 | public class ConcurrentHashMapV8<K, V>
5552              if (transformer == null || reducer == null)
5553                  throw new NullPointerException();
5554              return new MapReduceEntriesToIntTask<K,V>
5555 <                (map, transformer, basis, reducer);
5555 >                (map, null, -1, null, transformer, basis, reducer);
5556          }
5557      }
5558  
5559      // -------------------------------------------------------
5560  
4812    /**
4813     * Base for FJ tasks for bulk operations. This adds a variant of
4814     * CountedCompleters and some split and merge bookeeping to
4815     * iterator functionality. The forEach and reduce methods are
4816     * similar to those illustrated in CountedCompleter documentation,
4817     * except that bottom-up reduction completions perform them within
4818     * their compute methods. The search methods are like forEach
4819     * except they continually poll for success and exit early.  Also,
4820     * exceptions are handled in a simpler manner, by just trying to
4821     * complete root task exceptionally.
4822     */
4823    static abstract class BulkTask<K,V,R> extends Traverser<K,V,R> {
4824        final BulkTask<K,V,?> parent;  // completion target
4825        int batch;                     // split control
4826        int pending;                   // completion control
4827
4828        /** Constructor for root tasks */
4829        BulkTask(ConcurrentHashMapV8<K,V> map) {
4830            super(map);
4831            this.parent = null;
4832            this.batch = -1; // force call to batch() on execution
4833        }
4834
4835        /** Constructor for subtasks */
4836        BulkTask(BulkTask<K,V,?> parent, int batch, boolean split) {
4837            super(parent, split);
4838            this.parent = parent;
4839            this.batch = batch;
4840        }
4841
4842        // FJ methods
4843
4844        /**
4845         * Propagate completion. Note that all reduce actions
4846         * bypass this method to combine while completing.
4847         */
4848        final void tryComplete() {
4849            BulkTask<K,V,?> a = this, s = a;
4850            for (int c;;) {
4851                if ((c = a.pending) == 0) {
4852                    if ((a = (s = a).parent) == null) {
4853                        s.quietlyComplete();
4854                        break;
4855                    }
4856                }
4857                else if (U.compareAndSwapInt(a, PENDING, c, c - 1))
4858                    break;
4859            }
4860        }
4861
4862        /**
4863         * Force root task to throw exception unless already complete.
4864         */
4865        final void tryAbortComputation(Throwable ex) {
4866            for (BulkTask<K,V,?> a = this;;) {
4867                BulkTask<K,V,?> p = a.parent;
4868                if (p == null) {
4869                    a.completeExceptionally(ex);
4870                    break;
4871                }
4872                a = p;
4873            }
4874        }
4875
4876        public final boolean exec() {
4877            try {
4878                compute();
4879            }
4880            catch(Throwable ex) {
4881                tryAbortComputation(ex);
4882            }
4883            return false;
4884        }
4885
4886        public abstract void compute();
4887
4888        // utilities
4889
4890        /** CompareAndSet pending count */
4891        final boolean casPending(int cmp, int val) {
4892            return U.compareAndSwapInt(this, PENDING, cmp, val);
4893        }
4894
4895        /**
4896         * Return approx exp2 of the number of times (minus one) to
4897         * split task by two before executing leaf action. This value
4898         * is faster to compute and more convenient to use as a guide
4899         * to splitting than is the depth, since it is used while
4900         * dividing by two anyway.
4901         */
4902        final int batch() {
4903            int b = batch;
4904            if (b < 0) {
4905                long n = map.counter.sum();
4906                int sp = getPool().getParallelism() << 3; // slack of 8
4907                b = batch = (n <= 0L)? 0 : (n < (long)sp) ? (int)n : sp;
4908            }
4909            return b;
4910        }
4911
4912        /**
4913         * Error message for hoisted null checks of functions
4914         */
4915        static final String NullFunctionMessage =
4916            "Unexpected null function";
4917
4918        /**
4919         * Return exportable snapshot entry
4920         */
4921        static <K,V> AbstractMap.SimpleEntry<K,V> entryFor(K k, V v) {
4922            return new AbstractMap.SimpleEntry(k, v);
4923        }
4924
4925        // Unsafe mechanics
4926        private static final sun.misc.Unsafe U;
4927        private static final long PENDING;
4928        static {
4929            try {
4930                U = sun.misc.Unsafe.getUnsafe();
4931                PENDING = U.objectFieldOffset
4932                    (BulkTask.class.getDeclaredField("pending"));
4933            } catch (Exception e) {
4934                throw new Error(e);
4935            }
4936        }
4937    }
4938
5561      /*
5562       * Task classes. Coded in a regular but ugly format/style to
5563       * simplify checks that each variant differs in the right way from
5564 <     * others.
5564 >     * others. The null screenings exist because compilers cannot tell
5565 >     * that we've already null-checked task arguments, so we force
5566 >     * simplest hoisted bypass to help avoid convoluted traps.
5567       */
5568  
5569 <    static final class ForEachKeyTask<K,V>
5570 <        extends BulkTask<K,V,Void> {
5569 >    @SuppressWarnings("serial") static final class ForEachKeyTask<K,V>
5570 >        extends Traverser<K,V,Void> {
5571          final Action<K> action;
5572          ForEachKeyTask
5573 <            (ConcurrentHashMapV8<K,V> m,
4950 <             Action<K> action) {
4951 <            super(m);
4952 <            this.action = action;
4953 <        }
4954 <        ForEachKeyTask
4955 <            (BulkTask<K,V,?> p, int b, boolean split,
5573 >            (ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b,
5574               Action<K> action) {
5575 <            super(p, b, split);
5575 >            super(m, p, b);
5576              this.action = action;
5577          }
5578 <        public final void compute() {
5579 <            final Action<K> action = this.action;
5580 <            if (action == null)
5581 <                throw new Error(NullFunctionMessage);
5582 <            int b = batch(), c;
5583 <            while (b > 1 && baseIndex != baseLimit) {
5584 <                do {} while (!casPending(c = pending, c+1));
5585 <                new ForEachKeyTask<K,V>(this, b >>>= 1, true, action).fork();
5586 <            }
4969 <            while (advance() != null)
4970 <                action.apply((K)nextKey);
4971 <            tryComplete();
5578 >        @SuppressWarnings("unchecked") public final void compute() {
5579 >            final Action<K> action;
5580 >            if ((action = this.action) != null) {
5581 >                for (int b; (b = preSplit()) > 0;)
5582 >                    new ForEachKeyTask<K,V>(map, this, b, action).fork();
5583 >                while (advance() != null)
5584 >                    action.apply((K)nextKey);
5585 >                propagateCompletion();
5586 >            }
5587          }
5588      }
5589  
5590 <    static final class ForEachValueTask<K,V>
5591 <        extends BulkTask<K,V,Void> {
5590 >    @SuppressWarnings("serial") static final class ForEachValueTask<K,V>
5591 >        extends Traverser<K,V,Void> {
5592          final Action<V> action;
5593          ForEachValueTask
5594 <            (ConcurrentHashMapV8<K,V> m,
5594 >            (ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b,
5595               Action<V> action) {
5596 <            super(m);
5596 >            super(m, p, b);
5597              this.action = action;
5598          }
5599 <        ForEachValueTask
5600 <            (BulkTask<K,V,?> p, int b, boolean split,
5601 <             Action<V> action) {
5602 <            super(p, b, split);
5603 <            this.action = action;
5604 <        }
5605 <        public final void compute() {
5606 <            final Action<V> action = this.action;
5607 <            if (action == null)
5608 <                throw new Error(NullFunctionMessage);
4994 <            int b = batch(), c;
4995 <            while (b > 1 && baseIndex != baseLimit) {
4996 <                do {} while (!casPending(c = pending, c+1));
4997 <                new ForEachValueTask<K,V>(this, b >>>= 1, true, action).fork();
4998 <            }
4999 <            Object v;
5000 <            while ((v = advance()) != null)
5001 <                action.apply((V)v);
5002 <            tryComplete();
5599 >        @SuppressWarnings("unchecked") public final void compute() {
5600 >            final Action<V> action;
5601 >            if ((action = this.action) != null) {
5602 >                for (int b; (b = preSplit()) > 0;)
5603 >                    new ForEachValueTask<K,V>(map, this, b, action).fork();
5604 >                V v;
5605 >                while ((v = advance()) != null)
5606 >                    action.apply(v);
5607 >                propagateCompletion();
5608 >            }
5609          }
5610      }
5611  
5612 <    static final class ForEachEntryTask<K,V>
5613 <        extends BulkTask<K,V,Void> {
5612 >    @SuppressWarnings("serial") static final class ForEachEntryTask<K,V>
5613 >        extends Traverser<K,V,Void> {
5614          final Action<Entry<K,V>> action;
5615          ForEachEntryTask
5616 <            (ConcurrentHashMapV8<K,V> m,
5011 <             Action<Entry<K,V>> action) {
5012 <            super(m);
5013 <            this.action = action;
5014 <        }
5015 <        ForEachEntryTask
5016 <            (BulkTask<K,V,?> p, int b, boolean split,
5616 >            (ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b,
5617               Action<Entry<K,V>> action) {
5618 <            super(p, b, split);
5618 >            super(m, p, b);
5619              this.action = action;
5620          }
5621 <        public final void compute() {
5622 <            final Action<Entry<K,V>> action = this.action;
5623 <            if (action == null)
5624 <                throw new Error(NullFunctionMessage);
5625 <            int b = batch(), c;
5626 <            while (b > 1 && baseIndex != baseLimit) {
5627 <                do {} while (!casPending(c = pending, c+1));
5628 <                new ForEachEntryTask<K,V>(this, b >>>= 1, true, action).fork();
5629 <            }
5630 <            Object v;
5031 <            while ((v = advance()) != null)
5032 <                action.apply(entryFor((K)nextKey, (V)v));
5033 <            tryComplete();
5621 >        @SuppressWarnings("unchecked") public final void compute() {
5622 >            final Action<Entry<K,V>> action;
5623 >            if ((action = this.action) != null) {
5624 >                for (int b; (b = preSplit()) > 0;)
5625 >                    new ForEachEntryTask<K,V>(map, this, b, action).fork();
5626 >                V v;
5627 >                while ((v = advance()) != null)
5628 >                    action.apply(entryFor((K)nextKey, v));
5629 >                propagateCompletion();
5630 >            }
5631          }
5632      }
5633  
5634 <    static final class ForEachMappingTask<K,V>
5635 <        extends BulkTask<K,V,Void> {
5634 >    @SuppressWarnings("serial") static final class ForEachMappingTask<K,V>
5635 >        extends Traverser<K,V,Void> {
5636          final BiAction<K,V> action;
5637          ForEachMappingTask
5638 <            (ConcurrentHashMapV8<K,V> m,
5042 <             BiAction<K,V> action) {
5043 <            super(m);
5044 <            this.action = action;
5045 <        }
5046 <        ForEachMappingTask
5047 <            (BulkTask<K,V,?> p, int b, boolean split,
5638 >            (ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b,
5639               BiAction<K,V> action) {
5640 <            super(p, b, split);
5640 >            super(m, p, b);
5641              this.action = action;
5642          }
5643 <
5644 <        public final void compute() {
5645 <            final BiAction<K,V> action = this.action;
5646 <            if (action == null)
5647 <                throw new Error(NullFunctionMessage);
5648 <            int b = batch(), c;
5649 <            while (b > 1 && baseIndex != baseLimit) {
5650 <                do {} while (!casPending(c = pending, c+1));
5651 <                new ForEachMappingTask<K,V>(this, b >>>= 1, true,
5652 <                                            action).fork();
5062 <            }
5063 <            Object v;
5064 <            while ((v = advance()) != null)
5065 <                action.apply((K)nextKey, (V)v);
5066 <            tryComplete();
5643 >        @SuppressWarnings("unchecked") public final void compute() {
5644 >            final BiAction<K,V> action;
5645 >            if ((action = this.action) != null) {
5646 >                for (int b; (b = preSplit()) > 0;)
5647 >                    new ForEachMappingTask<K,V>(map, this, b, action).fork();
5648 >                V v;
5649 >                while ((v = advance()) != null)
5650 >                    action.apply((K)nextKey, v);
5651 >                propagateCompletion();
5652 >            }
5653          }
5654      }
5655  
5656 <    static final class ForEachTransformedKeyTask<K,V,U>
5657 <        extends BulkTask<K,V,Void> {
5656 >    @SuppressWarnings("serial") static final class ForEachTransformedKeyTask<K,V,U>
5657 >        extends Traverser<K,V,Void> {
5658          final Fun<? super K, ? extends U> transformer;
5659          final Action<U> action;
5660          ForEachTransformedKeyTask
5661 <            (ConcurrentHashMapV8<K,V> m,
5662 <             Fun<? super K, ? extends U> transformer,
5663 <             Action<U> action) {
5664 <            super(m);
5665 <            this.transformer = transformer;
5666 <            this.action = action;
5667 <
5668 <        }
5669 <        ForEachTransformedKeyTask
5670 <            (BulkTask<K,V,?> p, int b, boolean split,
5671 <             Fun<? super K, ? extends U> transformer,
5672 <             Action<U> action) {
5673 <            super(p, b, split);
5674 <            this.transformer = transformer;
5675 <            this.action = action;
5676 <        }
5677 <        public final void compute() {
5678 <            final Fun<? super K, ? extends U> transformer =
5679 <                this.transformer;
5094 <            final Action<U> action = this.action;
5095 <            if (transformer == null || action == null)
5096 <                throw new Error(NullFunctionMessage);
5097 <            int b = batch(), c;
5098 <            while (b > 1 && baseIndex != baseLimit) {
5099 <                do {} while (!casPending(c = pending, c+1));
5100 <                new ForEachTransformedKeyTask<K,V,U>
5101 <                    (this, b >>>= 1, true, transformer, action).fork();
5102 <            }
5103 <            U u;
5104 <            while (advance() != null) {
5105 <                if ((u = transformer.apply((K)nextKey)) != null)
5106 <                    action.apply(u);
5661 >            (ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b,
5662 >             Fun<? super K, ? extends U> transformer, Action<U> action) {
5663 >            super(m, p, b);
5664 >            this.transformer = transformer; this.action = action;
5665 >        }
5666 >        @SuppressWarnings("unchecked") public final void compute() {
5667 >            final Fun<? super K, ? extends U> transformer;
5668 >            final Action<U> action;
5669 >            if ((transformer = this.transformer) != null &&
5670 >                (action = this.action) != null) {
5671 >                for (int b; (b = preSplit()) > 0;)
5672 >                    new ForEachTransformedKeyTask<K,V,U>
5673 >                        (map, this, b, transformer, action).fork();
5674 >                U u;
5675 >                while (advance() != null) {
5676 >                    if ((u = transformer.apply((K)nextKey)) != null)
5677 >                        action.apply(u);
5678 >                }
5679 >                propagateCompletion();
5680              }
5108            tryComplete();
5681          }
5682      }
5683  
5684 <    static final class ForEachTransformedValueTask<K,V,U>
5685 <        extends BulkTask<K,V,Void> {
5684 >    @SuppressWarnings("serial") static final class ForEachTransformedValueTask<K,V,U>
5685 >        extends Traverser<K,V,Void> {
5686          final Fun<? super V, ? extends U> transformer;
5687          final Action<U> action;
5688          ForEachTransformedValueTask
5689 <            (ConcurrentHashMapV8<K,V> m,
5690 <             Fun<? super V, ? extends U> transformer,
5691 <             Action<U> action) {
5692 <            super(m);
5693 <            this.transformer = transformer;
5694 <            this.action = action;
5695 <
5696 <        }
5697 <        ForEachTransformedValueTask
5698 <            (BulkTask<K,V,?> p, int b, boolean split,
5699 <             Fun<? super V, ? extends U> transformer,
5700 <             Action<U> action) {
5701 <            super(p, b, split);
5702 <            this.transformer = transformer;
5703 <            this.action = action;
5704 <        }
5705 <        public final void compute() {
5706 <            final Fun<? super V, ? extends U> transformer =
5707 <                this.transformer;
5136 <            final Action<U> action = this.action;
5137 <            if (transformer == null || action == null)
5138 <                throw new Error(NullFunctionMessage);
5139 <            int b = batch(), c;
5140 <            while (b > 1 && baseIndex != baseLimit) {
5141 <                do {} while (!casPending(c = pending, c+1));
5142 <                new ForEachTransformedValueTask<K,V,U>
5143 <                    (this, b >>>= 1, true, transformer, action).fork();
5144 <            }
5145 <            Object v; U u;
5146 <            while ((v = advance()) != null) {
5147 <                if ((u = transformer.apply((V)v)) != null)
5148 <                    action.apply(u);
5689 >            (ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b,
5690 >             Fun<? super V, ? extends U> transformer, Action<U> action) {
5691 >            super(m, p, b);
5692 >            this.transformer = transformer; this.action = action;
5693 >        }
5694 >        @SuppressWarnings("unchecked") public final void compute() {
5695 >            final Fun<? super V, ? extends U> transformer;
5696 >            final Action<U> action;
5697 >            if ((transformer = this.transformer) != null &&
5698 >                (action = this.action) != null) {
5699 >                for (int b; (b = preSplit()) > 0;)
5700 >                    new ForEachTransformedValueTask<K,V,U>
5701 >                        (map, this, b, transformer, action).fork();
5702 >                V v; U u;
5703 >                while ((v = advance()) != null) {
5704 >                    if ((u = transformer.apply(v)) != null)
5705 >                        action.apply(u);
5706 >                }
5707 >                propagateCompletion();
5708              }
5150            tryComplete();
5709          }
5710      }
5711  
5712 <    static final class ForEachTransformedEntryTask<K,V,U>
5713 <        extends BulkTask<K,V,Void> {
5712 >    @SuppressWarnings("serial") static final class ForEachTransformedEntryTask<K,V,U>
5713 >        extends Traverser<K,V,Void> {
5714          final Fun<Map.Entry<K,V>, ? extends U> transformer;
5715          final Action<U> action;
5716          ForEachTransformedEntryTask
5717 <            (ConcurrentHashMapV8<K,V> m,
5718 <             Fun<Map.Entry<K,V>, ? extends U> transformer,
5719 <             Action<U> action) {
5720 <            super(m);
5721 <            this.transformer = transformer;
5722 <            this.action = action;
5723 <
5724 <        }
5725 <        ForEachTransformedEntryTask
5726 <            (BulkTask<K,V,?> p, int b, boolean split,
5727 <             Fun<Map.Entry<K,V>, ? extends U> transformer,
5728 <             Action<U> action) {
5729 <            super(p, b, split);
5730 <            this.transformer = transformer;
5731 <            this.action = action;
5732 <        }
5733 <        public final void compute() {
5734 <            final Fun<Map.Entry<K,V>, ? extends U> transformer =
5735 <                this.transformer;
5736 <            final Action<U> action = this.action;
5179 <            if (transformer == null || action == null)
5180 <                throw new Error(NullFunctionMessage);
5181 <            int b = batch(), c;
5182 <            while (b > 1 && baseIndex != baseLimit) {
5183 <                do {} while (!casPending(c = pending, c+1));
5184 <                new ForEachTransformedEntryTask<K,V,U>
5185 <                    (this, b >>>= 1, true, transformer, action).fork();
5186 <            }
5187 <            Object v; U u;
5188 <            while ((v = advance()) != null) {
5189 <                if ((u = transformer.apply(entryFor((K)nextKey, (V)v))) != null)
5190 <                    action.apply(u);
5717 >            (ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b,
5718 >             Fun<Map.Entry<K,V>, ? extends U> transformer, Action<U> action) {
5719 >            super(m, p, b);
5720 >            this.transformer = transformer; this.action = action;
5721 >        }
5722 >        @SuppressWarnings("unchecked") public final void compute() {
5723 >            final Fun<Map.Entry<K,V>, ? extends U> transformer;
5724 >            final Action<U> action;
5725 >            if ((transformer = this.transformer) != null &&
5726 >                (action = this.action) != null) {
5727 >                for (int b; (b = preSplit()) > 0;)
5728 >                    new ForEachTransformedEntryTask<K,V,U>
5729 >                        (map, this, b, transformer, action).fork();
5730 >                V v; U u;
5731 >                while ((v = advance()) != null) {
5732 >                    if ((u = transformer.apply(entryFor((K)nextKey,
5733 >                                                        v))) != null)
5734 >                        action.apply(u);
5735 >                }
5736 >                propagateCompletion();
5737              }
5192            tryComplete();
5738          }
5739      }
5740  
5741 <    static final class ForEachTransformedMappingTask<K,V,U>
5742 <        extends BulkTask<K,V,Void> {
5741 >    @SuppressWarnings("serial") static final class ForEachTransformedMappingTask<K,V,U>
5742 >        extends Traverser<K,V,Void> {
5743          final BiFun<? super K, ? super V, ? extends U> transformer;
5744          final Action<U> action;
5745          ForEachTransformedMappingTask
5746 <            (ConcurrentHashMapV8<K,V> m,
5202 <             BiFun<? super K, ? super V, ? extends U> transformer,
5203 <             Action<U> action) {
5204 <            super(m);
5205 <            this.transformer = transformer;
5206 <            this.action = action;
5207 <
5208 <        }
5209 <        ForEachTransformedMappingTask
5210 <            (BulkTask<K,V,?> p, int b, boolean split,
5746 >            (ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b,
5747               BiFun<? super K, ? super V, ? extends U> transformer,
5748               Action<U> action) {
5749 <            super(p, b, split);
5750 <            this.transformer = transformer;
5215 <            this.action = action;
5749 >            super(m, p, b);
5750 >            this.transformer = transformer; this.action = action;
5751          }
5752 <        public final void compute() {
5753 <            final BiFun<? super K, ? super V, ? extends U> transformer =
5754 <                this.transformer;
5755 <            final Action<U> action = this.action;
5756 <            if (transformer == null || action == null)
5757 <                throw new Error(NullFunctionMessage);
5758 <            int b = batch(), c;
5759 <            while (b > 1 && baseIndex != baseLimit) {
5760 <                do {} while (!casPending(c = pending, c+1));
5761 <                new ForEachTransformedMappingTask<K,V,U>
5762 <                    (this, b >>>= 1, true, transformer, action).fork();
5763 <            }
5764 <            Object v; U u;
5765 <            while ((v = advance()) != null) {
5231 <                if ((u = transformer.apply((K)nextKey, (V)v)) != null)
5232 <                    action.apply(u);
5752 >        @SuppressWarnings("unchecked") public final void compute() {
5753 >            final BiFun<? super K, ? super V, ? extends U> transformer;
5754 >            final Action<U> action;
5755 >            if ((transformer = this.transformer) != null &&
5756 >                (action = this.action) != null) {
5757 >                for (int b; (b = preSplit()) > 0;)
5758 >                    new ForEachTransformedMappingTask<K,V,U>
5759 >                        (map, this, b, transformer, action).fork();
5760 >                V v; U u;
5761 >                while ((v = advance()) != null) {
5762 >                    if ((u = transformer.apply((K)nextKey, v)) != null)
5763 >                        action.apply(u);
5764 >                }
5765 >                propagateCompletion();
5766              }
5234            tryComplete();
5767          }
5768      }
5769  
5770 <    static final class SearchKeysTask<K,V,U>
5771 <        extends BulkTask<K,V,U> {
5770 >    @SuppressWarnings("serial") static final class SearchKeysTask<K,V,U>
5771 >        extends Traverser<K,V,U> {
5772          final Fun<? super K, ? extends U> searchFunction;
5773          final AtomicReference<U> result;
5774          SearchKeysTask
5775 <            (ConcurrentHashMapV8<K,V> m,
5244 <             Fun<? super K, ? extends U> searchFunction,
5245 <             AtomicReference<U> result) {
5246 <            super(m);
5247 <            this.searchFunction = searchFunction; this.result = result;
5248 <        }
5249 <        SearchKeysTask
5250 <            (BulkTask<K,V,?> p, int b, boolean split,
5775 >            (ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b,
5776               Fun<? super K, ? extends U> searchFunction,
5777               AtomicReference<U> result) {
5778 <            super(p, b, split);
5778 >            super(m, p, b);
5779              this.searchFunction = searchFunction; this.result = result;
5780          }
5781 <        public final void compute() {
5782 <            AtomicReference<U> result = this.result;
5783 <            final Fun<? super K, ? extends U> searchFunction =
5784 <                this.searchFunction;
5785 <            if (searchFunction == null || result == null)
5786 <                throw new Error(NullFunctionMessage);
5787 <            int b = batch(), c;
5788 <            while (b > 1 && baseIndex != baseLimit && result.get() == null) {
5789 <                do {} while (!casPending(c = pending, c+1));
5790 <                new SearchKeysTask<K,V,U>(this, b >>>= 1, true,
5791 <                                          searchFunction, result).fork();
5792 <            }
5793 <            U u;
5794 <            while (result.get() == null && advance() != null) {
5795 <                if ((u = searchFunction.apply((K)nextKey)) != null) {
5796 <                    result.compareAndSet(null, u);
5797 <                    break;
5781 >        public final U getRawResult() { return result.get(); }
5782 >        @SuppressWarnings("unchecked") public final void compute() {
5783 >            final Fun<? super K, ? extends U> searchFunction;
5784 >            final AtomicReference<U> result;
5785 >            if ((searchFunction = this.searchFunction) != null &&
5786 >                (result = this.result) != null) {
5787 >                for (int b;;) {
5788 >                    if (result.get() != null)
5789 >                        return;
5790 >                    if ((b = preSplit()) <= 0)
5791 >                        break;
5792 >                    new SearchKeysTask<K,V,U>
5793 >                        (map, this, b, searchFunction, result).fork();
5794 >                }
5795 >                while (result.get() == null) {
5796 >                    U u;
5797 >                    if (advance() == null) {
5798 >                        propagateCompletion();
5799 >                        break;
5800 >                    }
5801 >                    if ((u = searchFunction.apply((K)nextKey)) != null) {
5802 >                        if (result.compareAndSet(null, u))
5803 >                            quietlyCompleteRoot();
5804 >                        break;
5805 >                    }
5806                  }
5807              }
5275            tryComplete();
5808          }
5277        public final U getRawResult() { return result.get(); }
5809      }
5810  
5811 <    static final class SearchValuesTask<K,V,U>
5812 <        extends BulkTask<K,V,U> {
5811 >    @SuppressWarnings("serial") static final class SearchValuesTask<K,V,U>
5812 >        extends Traverser<K,V,U> {
5813          final Fun<? super V, ? extends U> searchFunction;
5814          final AtomicReference<U> result;
5815          SearchValuesTask
5816 <            (ConcurrentHashMapV8<K,V> m,
5286 <             Fun<? super V, ? extends U> searchFunction,
5287 <             AtomicReference<U> result) {
5288 <            super(m);
5289 <            this.searchFunction = searchFunction; this.result = result;
5290 <        }
5291 <        SearchValuesTask
5292 <            (BulkTask<K,V,?> p, int b, boolean split,
5816 >            (ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b,
5817               Fun<? super V, ? extends U> searchFunction,
5818               AtomicReference<U> result) {
5819 <            super(p, b, split);
5819 >            super(m, p, b);
5820              this.searchFunction = searchFunction; this.result = result;
5821          }
5822 <        public final void compute() {
5823 <            AtomicReference<U> result = this.result;
5824 <            final Fun<? super V, ? extends U> searchFunction =
5825 <                this.searchFunction;
5826 <            if (searchFunction == null || result == null)
5827 <                throw new Error(NullFunctionMessage);
5828 <            int b = batch(), c;
5829 <            while (b > 1 && baseIndex != baseLimit && result.get() == null) {
5830 <                do {} while (!casPending(c = pending, c+1));
5831 <                new SearchValuesTask<K,V,U>(this, b >>>= 1, true,
5832 <                                            searchFunction, result).fork();
5833 <            }
5834 <            Object v; U u;
5835 <            while (result.get() == null && (v = advance()) != null) {
5836 <                if ((u = searchFunction.apply((V)v)) != null) {
5837 <                    result.compareAndSet(null, u);
5838 <                    break;
5822 >        public final U getRawResult() { return result.get(); }
5823 >        @SuppressWarnings("unchecked") public final void compute() {
5824 >            final Fun<? super V, ? extends U> searchFunction;
5825 >            final AtomicReference<U> result;
5826 >            if ((searchFunction = this.searchFunction) != null &&
5827 >                (result = this.result) != null) {
5828 >                for (int b;;) {
5829 >                    if (result.get() != null)
5830 >                        return;
5831 >                    if ((b = preSplit()) <= 0)
5832 >                        break;
5833 >                    new SearchValuesTask<K,V,U>
5834 >                        (map, this, b, searchFunction, result).fork();
5835 >                }
5836 >                while (result.get() == null) {
5837 >                    V v; U u;
5838 >                    if ((v = advance()) == null) {
5839 >                        propagateCompletion();
5840 >                        break;
5841 >                    }
5842 >                    if ((u = searchFunction.apply(v)) != null) {
5843 >                        if (result.compareAndSet(null, u))
5844 >                            quietlyCompleteRoot();
5845 >                        break;
5846 >                    }
5847                  }
5848              }
5317            tryComplete();
5849          }
5319        public final U getRawResult() { return result.get(); }
5850      }
5851  
5852 <    static final class SearchEntriesTask<K,V,U>
5853 <        extends BulkTask<K,V,U> {
5852 >    @SuppressWarnings("serial") static final class SearchEntriesTask<K,V,U>
5853 >        extends Traverser<K,V,U> {
5854          final Fun<Entry<K,V>, ? extends U> searchFunction;
5855          final AtomicReference<U> result;
5856          SearchEntriesTask
5857 <            (ConcurrentHashMapV8<K,V> m,
5328 <             Fun<Entry<K,V>, ? extends U> searchFunction,
5329 <             AtomicReference<U> result) {
5330 <            super(m);
5331 <            this.searchFunction = searchFunction; this.result = result;
5332 <        }
5333 <        SearchEntriesTask
5334 <            (BulkTask<K,V,?> p, int b, boolean split,
5857 >            (ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b,
5858               Fun<Entry<K,V>, ? extends U> searchFunction,
5859               AtomicReference<U> result) {
5860 <            super(p, b, split);
5860 >            super(m, p, b);
5861              this.searchFunction = searchFunction; this.result = result;
5862          }
5863 <        public final void compute() {
5864 <            AtomicReference<U> result = this.result;
5865 <            final Fun<Entry<K,V>, ? extends U> searchFunction =
5866 <                this.searchFunction;
5867 <            if (searchFunction == null || result == null)
5868 <                throw new Error(NullFunctionMessage);
5869 <            int b = batch(), c;
5870 <            while (b > 1 && baseIndex != baseLimit && result.get() == null) {
5871 <                do {} while (!casPending(c = pending, c+1));
5872 <                new SearchEntriesTask<K,V,U>(this, b >>>= 1, true,
5873 <                                             searchFunction, result).fork();
5874 <            }
5875 <            Object v; U u;
5876 <            while (result.get() == null && (v = advance()) != null) {
5877 <                if ((u = searchFunction.apply(entryFor((K)nextKey, (V)v))) != null) {
5878 <                    result.compareAndSet(null, u);
5879 <                    break;
5863 >        public final U getRawResult() { return result.get(); }
5864 >        @SuppressWarnings("unchecked") public final void compute() {
5865 >            final Fun<Entry<K,V>, ? extends U> searchFunction;
5866 >            final AtomicReference<U> result;
5867 >            if ((searchFunction = this.searchFunction) != null &&
5868 >                (result = this.result) != null) {
5869 >                for (int b;;) {
5870 >                    if (result.get() != null)
5871 >                        return;
5872 >                    if ((b = preSplit()) <= 0)
5873 >                        break;
5874 >                    new SearchEntriesTask<K,V,U>
5875 >                        (map, this, b, searchFunction, result).fork();
5876 >                }
5877 >                while (result.get() == null) {
5878 >                    V v; U u;
5879 >                    if ((v = advance()) == null) {
5880 >                        propagateCompletion();
5881 >                        break;
5882 >                    }
5883 >                    if ((u = searchFunction.apply(entryFor((K)nextKey,
5884 >                                                           v))) != null) {
5885 >                        if (result.compareAndSet(null, u))
5886 >                            quietlyCompleteRoot();
5887 >                        return;
5888 >                    }
5889                  }
5890              }
5359            tryComplete();
5891          }
5361        public final U getRawResult() { return result.get(); }
5892      }
5893  
5894 <    static final class SearchMappingsTask<K,V,U>
5895 <        extends BulkTask<K,V,U> {
5894 >    @SuppressWarnings("serial") static final class SearchMappingsTask<K,V,U>
5895 >        extends Traverser<K,V,U> {
5896          final BiFun<? super K, ? super V, ? extends U> searchFunction;
5897          final AtomicReference<U> result;
5898          SearchMappingsTask
5899 <            (ConcurrentHashMapV8<K,V> m,
5370 <             BiFun<? super K, ? super V, ? extends U> searchFunction,
5371 <             AtomicReference<U> result) {
5372 <            super(m);
5373 <            this.searchFunction = searchFunction; this.result = result;
5374 <        }
5375 <        SearchMappingsTask
5376 <            (BulkTask<K,V,?> p, int b, boolean split,
5899 >            (ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b,
5900               BiFun<? super K, ? super V, ? extends U> searchFunction,
5901               AtomicReference<U> result) {
5902 <            super(p, b, split);
5902 >            super(m, p, b);
5903              this.searchFunction = searchFunction; this.result = result;
5904          }
5905 <        public final void compute() {
5906 <            AtomicReference<U> result = this.result;
5907 <            final BiFun<? super K, ? super V, ? extends U> searchFunction =
5908 <                this.searchFunction;
5909 <            if (searchFunction == null || result == null)
5910 <                throw new Error(NullFunctionMessage);
5911 <            int b = batch(), c;
5912 <            while (b > 1 && baseIndex != baseLimit && result.get() == null) {
5913 <                do {} while (!casPending(c = pending, c+1));
5914 <                new SearchMappingsTask<K,V,U>(this, b >>>= 1, true,
5915 <                                              searchFunction, result).fork();
5916 <            }
5917 <            Object v; U u;
5918 <            while (result.get() == null && (v = advance()) != null) {
5919 <                if ((u = searchFunction.apply((K)nextKey, (V)v)) != null) {
5920 <                    result.compareAndSet(null, u);
5921 <                    break;
5905 >        public final U getRawResult() { return result.get(); }
5906 >        @SuppressWarnings("unchecked") public final void compute() {
5907 >            final BiFun<? super K, ? super V, ? extends U> searchFunction;
5908 >            final AtomicReference<U> result;
5909 >            if ((searchFunction = this.searchFunction) != null &&
5910 >                (result = this.result) != null) {
5911 >                for (int b;;) {
5912 >                    if (result.get() != null)
5913 >                        return;
5914 >                    if ((b = preSplit()) <= 0)
5915 >                        break;
5916 >                    new SearchMappingsTask<K,V,U>
5917 >                        (map, this, b, searchFunction, result).fork();
5918 >                }
5919 >                while (result.get() == null) {
5920 >                    V v; U u;
5921 >                    if ((v = advance()) == null) {
5922 >                        propagateCompletion();
5923 >                        break;
5924 >                    }
5925 >                    if ((u = searchFunction.apply((K)nextKey, v)) != null) {
5926 >                        if (result.compareAndSet(null, u))
5927 >                            quietlyCompleteRoot();
5928 >                        break;
5929 >                    }
5930                  }
5931              }
5401            tryComplete();
5932          }
5403        public final U getRawResult() { return result.get(); }
5933      }
5934  
5935 <    static final class ReduceKeysTask<K,V>
5936 <        extends BulkTask<K,V,K> {
5935 >    @SuppressWarnings("serial") static final class ReduceKeysTask<K,V>
5936 >        extends Traverser<K,V,K> {
5937          final BiFun<? super K, ? super K, ? extends K> reducer;
5938          K result;
5939 <        ReduceKeysTask<K,V> sibling;
5411 <        ReduceKeysTask
5412 <            (ConcurrentHashMapV8<K,V> m,
5413 <             BiFun<? super K, ? super K, ? extends K> reducer) {
5414 <            super(m);
5415 <            this.reducer = reducer;
5416 <        }
5939 >        ReduceKeysTask<K,V> rights, nextRight;
5940          ReduceKeysTask
5941 <            (BulkTask<K,V,?> p, int b, boolean split,
5941 >            (ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b,
5942 >             ReduceKeysTask<K,V> nextRight,
5943               BiFun<? super K, ? super K, ? extends K> reducer) {
5944 <            super(p, b, split);
5944 >            super(m, p, b); this.nextRight = nextRight;
5945              this.reducer = reducer;
5946          }
5947 <
5948 <        public final void compute() {
5949 <            ReduceKeysTask<K,V> t = this;
5950 <            final BiFun<? super K, ? super K, ? extends K> reducer =
5951 <                this.reducer;
5952 <            if (reducer == null)
5953 <                throw new Error(NullFunctionMessage);
5954 <            int b = batch();
5955 <            while (b > 1 && t.baseIndex != t.baseLimit) {
5956 <                b >>>= 1;
5957 <                t.pending = 1;
5434 <                ReduceKeysTask<K,V> rt =
5435 <                    new ReduceKeysTask<K,V>
5436 <                    (t, b, true, reducer);
5437 <                t = new ReduceKeysTask<K,V>
5438 <                    (t, b, false, reducer);
5439 <                t.sibling = rt;
5440 <                rt.sibling = t;
5441 <                rt.fork();
5442 <            }
5443 <            K r = null;
5444 <            while (t.advance() != null) {
5445 <                K u = (K)t.nextKey;
5446 <                r = (r == null) ? u : reducer.apply(r, u);
5447 <            }
5448 <            t.result = r;
5449 <            for (;;) {
5450 <                int c; BulkTask<K,V,?> par; ReduceKeysTask<K,V> s, p; K u;
5451 <                if ((par = t.parent) == null ||
5452 <                    !(par instanceof ReduceKeysTask)) {
5453 <                    t.quietlyComplete();
5454 <                    break;
5947 >        public final K getRawResult() { return result; }
5948 >        @SuppressWarnings("unchecked") public final void compute() {
5949 >            final BiFun<? super K, ? super K, ? extends K> reducer;
5950 >            if ((reducer = this.reducer) != null) {
5951 >                for (int b; (b = preSplit()) > 0;)
5952 >                    (rights = new ReduceKeysTask<K,V>
5953 >                     (map, this, b, rights, reducer)).fork();
5954 >                K r = null;
5955 >                while (advance() != null) {
5956 >                    K u = (K)nextKey;
5957 >                    r = (r == null) ? u : reducer.apply(r, u);
5958                  }
5959 <                else if ((c = (p = (ReduceKeysTask<K,V>)par).pending) == 0) {
5960 <                    if ((s = t.sibling) != null && (u = s.result) != null)
5961 <                        r = (r == null) ? u : reducer.apply(r, u);
5962 <                    (t = p).result = r;
5959 >                result = r;
5960 >                CountedCompleter<?> c;
5961 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5962 >                    ReduceKeysTask<K,V>
5963 >                        t = (ReduceKeysTask<K,V>)c,
5964 >                        s = t.rights;
5965 >                    while (s != null) {
5966 >                        K tr, sr;
5967 >                        if ((sr = s.result) != null)
5968 >                            t.result = (((tr = t.result) == null) ? sr :
5969 >                                        reducer.apply(tr, sr));
5970 >                        s = t.rights = s.nextRight;
5971 >                    }
5972                  }
5461                else if (p.casPending(c, 0))
5462                    break;
5973              }
5974          }
5465        public final K getRawResult() { return result; }
5975      }
5976  
5977 <    static final class ReduceValuesTask<K,V>
5978 <        extends BulkTask<K,V,V> {
5977 >    @SuppressWarnings("serial") static final class ReduceValuesTask<K,V>
5978 >        extends Traverser<K,V,V> {
5979          final BiFun<? super V, ? super V, ? extends V> reducer;
5980          V result;
5981 <        ReduceValuesTask<K,V> sibling;
5473 <        ReduceValuesTask
5474 <            (ConcurrentHashMapV8<K,V> m,
5475 <             BiFun<? super V, ? super V, ? extends V> reducer) {
5476 <            super(m);
5477 <            this.reducer = reducer;
5478 <        }
5981 >        ReduceValuesTask<K,V> rights, nextRight;
5982          ReduceValuesTask
5983 <            (BulkTask<K,V,?> p, int b, boolean split,
5983 >            (ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b,
5984 >             ReduceValuesTask<K,V> nextRight,
5985               BiFun<? super V, ? super V, ? extends V> reducer) {
5986 <            super(p, b, split);
5986 >            super(m, p, b); this.nextRight = nextRight;
5987              this.reducer = reducer;
5988          }
5989 <
5990 <        public final void compute() {
5991 <            ReduceValuesTask<K,V> t = this;
5992 <            final BiFun<? super V, ? super V, ? extends V> reducer =
5993 <                this.reducer;
5994 <            if (reducer == null)
5995 <                throw new Error(NullFunctionMessage);
5996 <            int b = batch();
5997 <            while (b > 1 && t.baseIndex != t.baseLimit) {
5998 <                b >>>= 1;
5999 <                t.pending = 1;
6000 <                ReduceValuesTask<K,V> rt =
5497 <                    new ReduceValuesTask<K,V>
5498 <                    (t, b, true, reducer);
5499 <                t = new ReduceValuesTask<K,V>
5500 <                    (t, b, false, reducer);
5501 <                t.sibling = rt;
5502 <                rt.sibling = t;
5503 <                rt.fork();
5504 <            }
5505 <            V r = null;
5506 <            Object v;
5507 <            while ((v = t.advance()) != null) {
5508 <                V u = (V)v;
5509 <                r = (r == null) ? u : reducer.apply(r, u);
5510 <            }
5511 <            t.result = r;
5512 <            for (;;) {
5513 <                int c; BulkTask<K,V,?> par; ReduceValuesTask<K,V> s, p; V u;
5514 <                if ((par = t.parent) == null ||
5515 <                    !(par instanceof ReduceValuesTask)) {
5516 <                    t.quietlyComplete();
5517 <                    break;
5989 >        public final V getRawResult() { return result; }
5990 >        @SuppressWarnings("unchecked") public final void compute() {
5991 >            final BiFun<? super V, ? super V, ? extends V> reducer;
5992 >            if ((reducer = this.reducer) != null) {
5993 >                for (int b; (b = preSplit()) > 0;)
5994 >                    (rights = new ReduceValuesTask<K,V>
5995 >                     (map, this, b, rights, reducer)).fork();
5996 >                V r = null;
5997 >                V v;
5998 >                while ((v = advance()) != null) {
5999 >                    V u = v;
6000 >                    r = (r == null) ? u : reducer.apply(r, u);
6001                  }
6002 <                else if ((c = (p = (ReduceValuesTask<K,V>)par).pending) == 0) {
6003 <                    if ((s = t.sibling) != null && (u = s.result) != null)
6004 <                        r = (r == null) ? u : reducer.apply(r, u);
6005 <                    (t = p).result = r;
6002 >                result = r;
6003 >                CountedCompleter<?> c;
6004 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
6005 >                    ReduceValuesTask<K,V>
6006 >                        t = (ReduceValuesTask<K,V>)c,
6007 >                        s = t.rights;
6008 >                    while (s != null) {
6009 >                        V tr, sr;
6010 >                        if ((sr = s.result) != null)
6011 >                            t.result = (((tr = t.result) == null) ? sr :
6012 >                                        reducer.apply(tr, sr));
6013 >                        s = t.rights = s.nextRight;
6014 >                    }
6015                  }
5524                else if (p.casPending(c, 0))
5525                    break;
6016              }
6017          }
5528        public final V getRawResult() { return result; }
6018      }
6019  
6020 <    static final class ReduceEntriesTask<K,V>
6021 <        extends BulkTask<K,V,Map.Entry<K,V>> {
6020 >    @SuppressWarnings("serial") static final class ReduceEntriesTask<K,V>
6021 >        extends Traverser<K,V,Map.Entry<K,V>> {
6022          final BiFun<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer;
6023          Map.Entry<K,V> result;
6024 <        ReduceEntriesTask<K,V> sibling;
6024 >        ReduceEntriesTask<K,V> rights, nextRight;
6025          ReduceEntriesTask
6026 <            (ConcurrentHashMapV8<K,V> m,
6026 >            (ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b,
6027 >             ReduceEntriesTask<K,V> nextRight,
6028               BiFun<Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer) {
6029 <            super(m);
5540 <            this.reducer = reducer;
5541 <        }
5542 <        ReduceEntriesTask
5543 <            (BulkTask<K,V,?> p, int b, boolean split,
5544 <             BiFun<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer) {
5545 <            super(p, b, split);
6029 >            super(m, p, b); this.nextRight = nextRight;
6030              this.reducer = reducer;
6031          }
6032 <
6033 <        public final void compute() {
6034 <            ReduceEntriesTask<K,V> t = this;
6035 <            final BiFun<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer =
6036 <                this.reducer;
6037 <            if (reducer == null)
6038 <                throw new Error(NullFunctionMessage);
6039 <            int b = batch();
6040 <            while (b > 1 && t.baseIndex != t.baseLimit) {
6041 <                b >>>= 1;
6042 <                t.pending = 1;
6043 <                ReduceEntriesTask<K,V> rt =
5560 <                    new ReduceEntriesTask<K,V>
5561 <                    (t, b, true, reducer);
5562 <                t = new ReduceEntriesTask<K,V>
5563 <                    (t, b, false, reducer);
5564 <                t.sibling = rt;
5565 <                rt.sibling = t;
5566 <                rt.fork();
5567 <            }
5568 <            Map.Entry<K,V> r = null;
5569 <            Object v;
5570 <            while ((v = t.advance()) != null) {
5571 <                Map.Entry<K,V> u = entryFor((K)t.nextKey, (V)v);
5572 <                r = (r == null) ? u : reducer.apply(r, u);
5573 <            }
5574 <            t.result = r;
5575 <            for (;;) {
5576 <                int c; BulkTask<K,V,?> par; ReduceEntriesTask<K,V> s, p;
5577 <                Map.Entry<K,V> u;
5578 <                if ((par = t.parent) == null ||
5579 <                    !(par instanceof ReduceEntriesTask)) {
5580 <                    t.quietlyComplete();
5581 <                    break;
6032 >        public final Map.Entry<K,V> getRawResult() { return result; }
6033 >        @SuppressWarnings("unchecked") public final void compute() {
6034 >            final BiFun<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer;
6035 >            if ((reducer = this.reducer) != null) {
6036 >                for (int b; (b = preSplit()) > 0;)
6037 >                    (rights = new ReduceEntriesTask<K,V>
6038 >                     (map, this, b, rights, reducer)).fork();
6039 >                Map.Entry<K,V> r = null;
6040 >                V v;
6041 >                while ((v = advance()) != null) {
6042 >                    Map.Entry<K,V> u = entryFor((K)nextKey, v);
6043 >                    r = (r == null) ? u : reducer.apply(r, u);
6044                  }
6045 <                else if ((c = (p = (ReduceEntriesTask<K,V>)par).pending) == 0) {
6046 <                    if ((s = t.sibling) != null && (u = s.result) != null)
6047 <                        r = (r == null) ? u : reducer.apply(r, u);
6048 <                    (t = p).result = r;
6045 >                result = r;
6046 >                CountedCompleter<?> c;
6047 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
6048 >                    ReduceEntriesTask<K,V>
6049 >                        t = (ReduceEntriesTask<K,V>)c,
6050 >                        s = t.rights;
6051 >                    while (s != null) {
6052 >                        Map.Entry<K,V> tr, sr;
6053 >                        if ((sr = s.result) != null)
6054 >                            t.result = (((tr = t.result) == null) ? sr :
6055 >                                        reducer.apply(tr, sr));
6056 >                        s = t.rights = s.nextRight;
6057 >                    }
6058                  }
5588                else if (p.casPending(c, 0))
5589                    break;
6059              }
6060          }
5592        public final Map.Entry<K,V> getRawResult() { return result; }
6061      }
6062  
6063 <    static final class MapReduceKeysTask<K,V,U>
6064 <        extends BulkTask<K,V,U> {
6063 >    @SuppressWarnings("serial") static final class MapReduceKeysTask<K,V,U>
6064 >        extends Traverser<K,V,U> {
6065          final Fun<? super K, ? extends U> transformer;
6066          final BiFun<? super U, ? super U, ? extends U> reducer;
6067          U result;
6068 <        MapReduceKeysTask<K,V,U> sibling;
5601 <        MapReduceKeysTask
5602 <            (ConcurrentHashMapV8<K,V> m,
5603 <             Fun<? super K, ? extends U> transformer,
5604 <             BiFun<? super U, ? super U, ? extends U> reducer) {
5605 <            super(m);
5606 <            this.transformer = transformer;
5607 <            this.reducer = reducer;
5608 <        }
6068 >        MapReduceKeysTask<K,V,U> rights, nextRight;
6069          MapReduceKeysTask
6070 <            (BulkTask<K,V,?> p, int b, boolean split,
6070 >            (ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b,
6071 >             MapReduceKeysTask<K,V,U> nextRight,
6072               Fun<? super K, ? extends U> transformer,
6073               BiFun<? super U, ? super U, ? extends U> reducer) {
6074 <            super(p, b, split);
6074 >            super(m, p, b); this.nextRight = nextRight;
6075              this.transformer = transformer;
6076              this.reducer = reducer;
6077          }
6078 <        public final void compute() {
6079 <            MapReduceKeysTask<K,V,U> t = this;
6080 <            final Fun<? super K, ? extends U> transformer =
6081 <                this.transformer;
6082 <            final BiFun<? super U, ? super U, ? extends U> reducer =
6083 <                this.reducer;
6084 <            if (transformer == null || reducer == null)
6085 <                throw new Error(NullFunctionMessage);
6086 <            int b = batch();
6087 <            while (b > 1 && t.baseIndex != t.baseLimit) {
6088 <                b >>>= 1;
6089 <                t.pending = 1;
5629 <                MapReduceKeysTask<K,V,U> rt =
5630 <                    new MapReduceKeysTask<K,V,U>
5631 <                    (t, b, true, transformer, reducer);
5632 <                t = new MapReduceKeysTask<K,V,U>
5633 <                    (t, b, false, transformer, reducer);
5634 <                t.sibling = rt;
5635 <                rt.sibling = t;
5636 <                rt.fork();
5637 <            }
5638 <            U r = null, u;
5639 <            while (t.advance() != null) {
5640 <                if ((u = transformer.apply((K)t.nextKey)) != null)
5641 <                    r = (r == null) ? u : reducer.apply(r, u);
5642 <            }
5643 <            t.result = r;
5644 <            for (;;) {
5645 <                int c; BulkTask<K,V,?> par; MapReduceKeysTask<K,V,U> s, p;
5646 <                if ((par = t.parent) == null ||
5647 <                    !(par instanceof MapReduceKeysTask)) {
5648 <                    t.quietlyComplete();
5649 <                    break;
5650 <                }
5651 <                else if ((c = (p = (MapReduceKeysTask<K,V,U>)par).pending) == 0) {
5652 <                    if ((s = t.sibling) != null && (u = s.result) != null)
6078 >        public final U getRawResult() { return result; }
6079 >        @SuppressWarnings("unchecked") public final void compute() {
6080 >            final Fun<? super K, ? extends U> transformer;
6081 >            final BiFun<? super U, ? super U, ? extends U> reducer;
6082 >            if ((transformer = this.transformer) != null &&
6083 >                (reducer = this.reducer) != null) {
6084 >                for (int b; (b = preSplit()) > 0;)
6085 >                    (rights = new MapReduceKeysTask<K,V,U>
6086 >                     (map, this, b, rights, transformer, reducer)).fork();
6087 >                U r = null, u;
6088 >                while (advance() != null) {
6089 >                    if ((u = transformer.apply((K)nextKey)) != null)
6090                          r = (r == null) ? u : reducer.apply(r, u);
5654                    (t = p).result = r;
6091                  }
6092 <                else if (p.casPending(c, 0))
6093 <                    break;
6092 >                result = r;
6093 >                CountedCompleter<?> c;
6094 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
6095 >                    MapReduceKeysTask<K,V,U>
6096 >                        t = (MapReduceKeysTask<K,V,U>)c,
6097 >                        s = t.rights;
6098 >                    while (s != null) {
6099 >                        U tr, sr;
6100 >                        if ((sr = s.result) != null)
6101 >                            t.result = (((tr = t.result) == null) ? sr :
6102 >                                        reducer.apply(tr, sr));
6103 >                        s = t.rights = s.nextRight;
6104 >                    }
6105 >                }
6106              }
6107          }
5660        public final U getRawResult() { return result; }
6108      }
6109  
6110 <    static final class MapReduceValuesTask<K,V,U>
6111 <        extends BulkTask<K,V,U> {
6110 >    @SuppressWarnings("serial") static final class MapReduceValuesTask<K,V,U>
6111 >        extends Traverser<K,V,U> {
6112          final Fun<? super V, ? extends U> transformer;
6113          final BiFun<? super U, ? super U, ? extends U> reducer;
6114          U result;
6115 <        MapReduceValuesTask<K,V,U> sibling;
5669 <        MapReduceValuesTask
5670 <            (ConcurrentHashMapV8<K,V> m,
5671 <             Fun<? super V, ? extends U> transformer,
5672 <             BiFun<? super U, ? super U, ? extends U> reducer) {
5673 <            super(m);
5674 <            this.transformer = transformer;
5675 <            this.reducer = reducer;
5676 <        }
6115 >        MapReduceValuesTask<K,V,U> rights, nextRight;
6116          MapReduceValuesTask
6117 <            (BulkTask<K,V,?> p, int b, boolean split,
6117 >            (ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b,
6118 >             MapReduceValuesTask<K,V,U> nextRight,
6119               Fun<? super V, ? extends U> transformer,
6120               BiFun<? super U, ? super U, ? extends U> reducer) {
6121 <            super(p, b, split);
6121 >            super(m, p, b); this.nextRight = nextRight;
6122              this.transformer = transformer;
6123              this.reducer = reducer;
6124          }
6125 <        public final void compute() {
6126 <            MapReduceValuesTask<K,V,U> t = this;
6127 <            final Fun<? super V, ? extends U> transformer =
6128 <                this.transformer;
6129 <            final BiFun<? super U, ? super U, ? extends U> reducer =
6130 <                this.reducer;
6131 <            if (transformer == null || reducer == null)
6132 <                throw new Error(NullFunctionMessage);
6133 <            int b = batch();
6134 <            while (b > 1 && t.baseIndex != t.baseLimit) {
6135 <                b >>>= 1;
6136 <                t.pending = 1;
6137 <                MapReduceValuesTask<K,V,U> rt =
5698 <                    new MapReduceValuesTask<K,V,U>
5699 <                    (t, b, true, transformer, reducer);
5700 <                t = new MapReduceValuesTask<K,V,U>
5701 <                    (t, b, false, transformer, reducer);
5702 <                t.sibling = rt;
5703 <                rt.sibling = t;
5704 <                rt.fork();
5705 <            }
5706 <            U r = null, u;
5707 <            Object v;
5708 <            while ((v = t.advance()) != null) {
5709 <                if ((u = transformer.apply((V)v)) != null)
5710 <                    r = (r == null) ? u : reducer.apply(r, u);
5711 <            }
5712 <            t.result = r;
5713 <            for (;;) {
5714 <                int c; BulkTask<K,V,?> par; MapReduceValuesTask<K,V,U> s, p;
5715 <                if ((par = t.parent) == null ||
5716 <                    !(par instanceof MapReduceValuesTask)) {
5717 <                    t.quietlyComplete();
5718 <                    break;
5719 <                }
5720 <                else if ((c = (p = (MapReduceValuesTask<K,V,U>)par).pending) == 0) {
5721 <                    if ((s = t.sibling) != null && (u = s.result) != null)
6125 >        public final U getRawResult() { return result; }
6126 >        @SuppressWarnings("unchecked") public final void compute() {
6127 >            final Fun<? super V, ? extends U> transformer;
6128 >            final BiFun<? super U, ? super U, ? extends U> reducer;
6129 >            if ((transformer = this.transformer) != null &&
6130 >                (reducer = this.reducer) != null) {
6131 >                for (int b; (b = preSplit()) > 0;)
6132 >                    (rights = new MapReduceValuesTask<K,V,U>
6133 >                     (map, this, b, rights, transformer, reducer)).fork();
6134 >                U r = null, u;
6135 >                V v;
6136 >                while ((v = advance()) != null) {
6137 >                    if ((u = transformer.apply(v)) != null)
6138                          r = (r == null) ? u : reducer.apply(r, u);
5723                    (t = p).result = r;
6139                  }
6140 <                else if (p.casPending(c, 0))
6141 <                    break;
6140 >                result = r;
6141 >                CountedCompleter<?> c;
6142 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
6143 >                    MapReduceValuesTask<K,V,U>
6144 >                        t = (MapReduceValuesTask<K,V,U>)c,
6145 >                        s = t.rights;
6146 >                    while (s != null) {
6147 >                        U tr, sr;
6148 >                        if ((sr = s.result) != null)
6149 >                            t.result = (((tr = t.result) == null) ? sr :
6150 >                                        reducer.apply(tr, sr));
6151 >                        s = t.rights = s.nextRight;
6152 >                    }
6153 >                }
6154              }
6155          }
5729        public final U getRawResult() { return result; }
6156      }
6157  
6158 <    static final class MapReduceEntriesTask<K,V,U>
6159 <        extends BulkTask<K,V,U> {
6158 >    @SuppressWarnings("serial") static final class MapReduceEntriesTask<K,V,U>
6159 >        extends Traverser<K,V,U> {
6160          final Fun<Map.Entry<K,V>, ? extends U> transformer;
6161          final BiFun<? super U, ? super U, ? extends U> reducer;
6162          U result;
6163 <        MapReduceEntriesTask<K,V,U> sibling;
5738 <        MapReduceEntriesTask
5739 <            (ConcurrentHashMapV8<K,V> m,
5740 <             Fun<Map.Entry<K,V>, ? extends U> transformer,
5741 <             BiFun<? super U, ? super U, ? extends U> reducer) {
5742 <            super(m);
5743 <            this.transformer = transformer;
5744 <            this.reducer = reducer;
5745 <        }
6163 >        MapReduceEntriesTask<K,V,U> rights, nextRight;
6164          MapReduceEntriesTask
6165 <            (BulkTask<K,V,?> p, int b, boolean split,
6165 >            (ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b,
6166 >             MapReduceEntriesTask<K,V,U> nextRight,
6167               Fun<Map.Entry<K,V>, ? extends U> transformer,
6168               BiFun<? super U, ? super U, ? extends U> reducer) {
6169 <            super(p, b, split);
6169 >            super(m, p, b); this.nextRight = nextRight;
6170              this.transformer = transformer;
6171              this.reducer = reducer;
6172          }
6173 <        public final void compute() {
6174 <            MapReduceEntriesTask<K,V,U> t = this;
6175 <            final Fun<Map.Entry<K,V>, ? extends U> transformer =
6176 <                this.transformer;
6177 <            final BiFun<? super U, ? super U, ? extends U> reducer =
6178 <                this.reducer;
6179 <            if (transformer == null || reducer == null)
6180 <                throw new Error(NullFunctionMessage);
6181 <            int b = batch();
6182 <            while (b > 1 && t.baseIndex != t.baseLimit) {
6183 <                b >>>= 1;
6184 <                t.pending = 1;
6185 <                MapReduceEntriesTask<K,V,U> rt =
6186 <                    new MapReduceEntriesTask<K,V,U>
5768 <                    (t, b, true, transformer, reducer);
5769 <                t = new MapReduceEntriesTask<K,V,U>
5770 <                    (t, b, false, transformer, reducer);
5771 <                t.sibling = rt;
5772 <                rt.sibling = t;
5773 <                rt.fork();
5774 <            }
5775 <            U r = null, u;
5776 <            Object v;
5777 <            while ((v = t.advance()) != null) {
5778 <                if ((u = transformer.apply(entryFor((K)t.nextKey, (V)v))) != null)
5779 <                    r = (r == null) ? u : reducer.apply(r, u);
5780 <            }
5781 <            t.result = r;
5782 <            for (;;) {
5783 <                int c; BulkTask<K,V,?> par; MapReduceEntriesTask<K,V,U> s, p;
5784 <                if ((par = t.parent) == null ||
5785 <                    !(par instanceof MapReduceEntriesTask)) {
5786 <                    t.quietlyComplete();
5787 <                    break;
5788 <                }
5789 <                else if ((c = (p = (MapReduceEntriesTask<K,V,U>)par).pending) == 0) {
5790 <                    if ((s = t.sibling) != null && (u = s.result) != null)
6173 >        public final U getRawResult() { return result; }
6174 >        @SuppressWarnings("unchecked") public final void compute() {
6175 >            final Fun<Map.Entry<K,V>, ? extends U> transformer;
6176 >            final BiFun<? super U, ? super U, ? extends U> reducer;
6177 >            if ((transformer = this.transformer) != null &&
6178 >                (reducer = this.reducer) != null) {
6179 >                for (int b; (b = preSplit()) > 0;)
6180 >                    (rights = new MapReduceEntriesTask<K,V,U>
6181 >                     (map, this, b, rights, transformer, reducer)).fork();
6182 >                U r = null, u;
6183 >                V v;
6184 >                while ((v = advance()) != null) {
6185 >                    if ((u = transformer.apply(entryFor((K)nextKey,
6186 >                                                        v))) != null)
6187                          r = (r == null) ? u : reducer.apply(r, u);
5792                    (t = p).result = r;
6188                  }
6189 <                else if (p.casPending(c, 0))
6190 <                    break;
6189 >                result = r;
6190 >                CountedCompleter<?> c;
6191 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
6192 >                    MapReduceEntriesTask<K,V,U>
6193 >                        t = (MapReduceEntriesTask<K,V,U>)c,
6194 >                        s = t.rights;
6195 >                    while (s != null) {
6196 >                        U tr, sr;
6197 >                        if ((sr = s.result) != null)
6198 >                            t.result = (((tr = t.result) == null) ? sr :
6199 >                                        reducer.apply(tr, sr));
6200 >                        s = t.rights = s.nextRight;
6201 >                    }
6202 >                }
6203              }
6204          }
5798        public final U getRawResult() { return result; }
6205      }
6206  
6207 <    static final class MapReduceMappingsTask<K,V,U>
6208 <        extends BulkTask<K,V,U> {
6207 >    @SuppressWarnings("serial") static final class MapReduceMappingsTask<K,V,U>
6208 >        extends Traverser<K,V,U> {
6209          final BiFun<? super K, ? super V, ? extends U> transformer;
6210          final BiFun<? super U, ? super U, ? extends U> reducer;
6211          U result;
6212 <        MapReduceMappingsTask<K,V,U> sibling;
5807 <        MapReduceMappingsTask
5808 <            (ConcurrentHashMapV8<K,V> m,
5809 <             BiFun<? super K, ? super V, ? extends U> transformer,
5810 <             BiFun<? super U, ? super U, ? extends U> reducer) {
5811 <            super(m);
5812 <            this.transformer = transformer;
5813 <            this.reducer = reducer;
5814 <        }
6212 >        MapReduceMappingsTask<K,V,U> rights, nextRight;
6213          MapReduceMappingsTask
6214 <            (BulkTask<K,V,?> p, int b, boolean split,
6214 >            (ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b,
6215 >             MapReduceMappingsTask<K,V,U> nextRight,
6216               BiFun<? super K, ? super V, ? extends U> transformer,
6217               BiFun<? super U, ? super U, ? extends U> reducer) {
6218 <            super(p, b, split);
6218 >            super(m, p, b); this.nextRight = nextRight;
6219              this.transformer = transformer;
6220              this.reducer = reducer;
6221          }
6222 <        public final void compute() {
6223 <            MapReduceMappingsTask<K,V,U> t = this;
6224 <            final BiFun<? super K, ? super V, ? extends U> transformer =
6225 <                this.transformer;
6226 <            final BiFun<? super U, ? super U, ? extends U> reducer =
6227 <                this.reducer;
6228 <            if (transformer == null || reducer == null)
6229 <                throw new Error(NullFunctionMessage);
6230 <            int b = batch();
6231 <            while (b > 1 && t.baseIndex != t.baseLimit) {
6232 <                b >>>= 1;
6233 <                t.pending = 1;
6234 <                MapReduceMappingsTask<K,V,U> rt =
5836 <                    new MapReduceMappingsTask<K,V,U>
5837 <                    (t, b, true, transformer, reducer);
5838 <                t = new MapReduceMappingsTask<K,V,U>
5839 <                    (t, b, false, transformer, reducer);
5840 <                t.sibling = rt;
5841 <                rt.sibling = t;
5842 <                rt.fork();
5843 <            }
5844 <            U r = null, u;
5845 <            Object v;
5846 <            while ((v = t.advance()) != null) {
5847 <                if ((u = transformer.apply((K)t.nextKey, (V)v)) != null)
5848 <                    r = (r == null) ? u : reducer.apply(r, u);
5849 <            }
5850 <            for (;;) {
5851 <                int c; BulkTask<K,V,?> par; MapReduceMappingsTask<K,V,U> s, p;
5852 <                if ((par = t.parent) == null ||
5853 <                    !(par instanceof MapReduceMappingsTask)) {
5854 <                    t.quietlyComplete();
5855 <                    break;
5856 <                }
5857 <                else if ((c = (p = (MapReduceMappingsTask<K,V,U>)par).pending) == 0) {
5858 <                    if ((s = t.sibling) != null && (u = s.result) != null)
6222 >        public final U getRawResult() { return result; }
6223 >        @SuppressWarnings("unchecked") public final void compute() {
6224 >            final BiFun<? super K, ? super V, ? extends U> transformer;
6225 >            final BiFun<? super U, ? super U, ? extends U> reducer;
6226 >            if ((transformer = this.transformer) != null &&
6227 >                (reducer = this.reducer) != null) {
6228 >                for (int b; (b = preSplit()) > 0;)
6229 >                    (rights = new MapReduceMappingsTask<K,V,U>
6230 >                     (map, this, b, rights, transformer, reducer)).fork();
6231 >                U r = null, u;
6232 >                V v;
6233 >                while ((v = advance()) != null) {
6234 >                    if ((u = transformer.apply((K)nextKey, v)) != null)
6235                          r = (r == null) ? u : reducer.apply(r, u);
5860                    (t = p).result = r;
6236                  }
6237 <                else if (p.casPending(c, 0))
6238 <                    break;
6237 >                result = r;
6238 >                CountedCompleter<?> c;
6239 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
6240 >                    MapReduceMappingsTask<K,V,U>
6241 >                        t = (MapReduceMappingsTask<K,V,U>)c,
6242 >                        s = t.rights;
6243 >                    while (s != null) {
6244 >                        U tr, sr;
6245 >                        if ((sr = s.result) != null)
6246 >                            t.result = (((tr = t.result) == null) ? sr :
6247 >                                        reducer.apply(tr, sr));
6248 >                        s = t.rights = s.nextRight;
6249 >                    }
6250 >                }
6251              }
6252          }
5866        public final U getRawResult() { return result; }
6253      }
6254  
6255 <    static final class MapReduceKeysToDoubleTask<K,V>
6256 <        extends BulkTask<K,V,Double> {
6255 >    @SuppressWarnings("serial") static final class MapReduceKeysToDoubleTask<K,V>
6256 >        extends Traverser<K,V,Double> {
6257          final ObjectToDouble<? super K> transformer;
6258          final DoubleByDoubleToDouble reducer;
6259          final double basis;
6260          double result;
6261 <        MapReduceKeysToDoubleTask<K,V> sibling;
5876 <        MapReduceKeysToDoubleTask
5877 <            (ConcurrentHashMapV8<K,V> m,
5878 <             ObjectToDouble<? super K> transformer,
5879 <             double basis,
5880 <             DoubleByDoubleToDouble reducer) {
5881 <            super(m);
5882 <            this.transformer = transformer;
5883 <            this.basis = basis; this.reducer = reducer;
5884 <        }
6261 >        MapReduceKeysToDoubleTask<K,V> rights, nextRight;
6262          MapReduceKeysToDoubleTask
6263 <            (BulkTask<K,V,?> p, int b, boolean split,
6263 >            (ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b,
6264 >             MapReduceKeysToDoubleTask<K,V> nextRight,
6265               ObjectToDouble<? super K> transformer,
6266               double basis,
6267               DoubleByDoubleToDouble reducer) {
6268 <            super(p, b, split);
6268 >            super(m, p, b); this.nextRight = nextRight;
6269              this.transformer = transformer;
6270              this.basis = basis; this.reducer = reducer;
6271          }
6272 <        public final void compute() {
6273 <            MapReduceKeysToDoubleTask<K,V> t = this;
6274 <            final ObjectToDouble<? super K> transformer =
6275 <                this.transformer;
6276 <            final DoubleByDoubleToDouble reducer = this.reducer;
6277 <            if (transformer == null || reducer == null)
6278 <                throw new Error(NullFunctionMessage);
6279 <            final double id = this.basis;
6280 <            int b = batch();
6281 <            while (b > 1 && t.baseIndex != t.baseLimit) {
6282 <                b >>>= 1;
6283 <                t.pending = 1;
6284 <                MapReduceKeysToDoubleTask<K,V> rt =
6285 <                    new MapReduceKeysToDoubleTask<K,V>
6286 <                    (t, b, true, transformer, id, reducer);
6287 <                t = new MapReduceKeysToDoubleTask<K,V>
6288 <                    (t, b, false, transformer, id, reducer);
6289 <                t.sibling = rt;
6290 <                rt.sibling = t;
6291 <                rt.fork();
6292 <            }
6293 <            double r = id;
5916 <            while (t.advance() != null)
5917 <                r = reducer.apply(r, transformer.apply((K)t.nextKey));
5918 <            t.result = r;
5919 <            for (;;) {
5920 <                int c; BulkTask<K,V,?> par; MapReduceKeysToDoubleTask<K,V> s, p;
5921 <                if ((par = t.parent) == null ||
5922 <                    !(par instanceof MapReduceKeysToDoubleTask)) {
5923 <                    t.quietlyComplete();
5924 <                    break;
5925 <                }
5926 <                else if ((c = (p = (MapReduceKeysToDoubleTask<K,V>)par).pending) == 0) {
5927 <                    if ((s = t.sibling) != null)
5928 <                        r = reducer.apply(r, s.result);
5929 <                    (t = p).result = r;
6272 >        public final Double getRawResult() { return result; }
6273 >        @SuppressWarnings("unchecked") public final void compute() {
6274 >            final ObjectToDouble<? super K> transformer;
6275 >            final DoubleByDoubleToDouble reducer;
6276 >            if ((transformer = this.transformer) != null &&
6277 >                (reducer = this.reducer) != null) {
6278 >                double r = this.basis;
6279 >                for (int b; (b = preSplit()) > 0;)
6280 >                    (rights = new MapReduceKeysToDoubleTask<K,V>
6281 >                     (map, this, b, rights, transformer, r, reducer)).fork();
6282 >                while (advance() != null)
6283 >                    r = reducer.apply(r, transformer.apply((K)nextKey));
6284 >                result = r;
6285 >                CountedCompleter<?> c;
6286 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
6287 >                    MapReduceKeysToDoubleTask<K,V>
6288 >                        t = (MapReduceKeysToDoubleTask<K,V>)c,
6289 >                        s = t.rights;
6290 >                    while (s != null) {
6291 >                        t.result = reducer.apply(t.result, s.result);
6292 >                        s = t.rights = s.nextRight;
6293 >                    }
6294                  }
5931                else if (p.casPending(c, 0))
5932                    break;
6295              }
6296          }
5935        public final Double getRawResult() { return result; }
6297      }
6298  
6299 <    static final class MapReduceValuesToDoubleTask<K,V>
6300 <        extends BulkTask<K,V,Double> {
6299 >    @SuppressWarnings("serial") static final class MapReduceValuesToDoubleTask<K,V>
6300 >        extends Traverser<K,V,Double> {
6301          final ObjectToDouble<? super V> transformer;
6302          final DoubleByDoubleToDouble reducer;
6303          final double basis;
6304          double result;
6305 <        MapReduceValuesToDoubleTask<K,V> sibling;
5945 <        MapReduceValuesToDoubleTask
5946 <            (ConcurrentHashMapV8<K,V> m,
5947 <             ObjectToDouble<? super V> transformer,
5948 <             double basis,
5949 <             DoubleByDoubleToDouble reducer) {
5950 <            super(m);
5951 <            this.transformer = transformer;
5952 <            this.basis = basis; this.reducer = reducer;
5953 <        }
6305 >        MapReduceValuesToDoubleTask<K,V> rights, nextRight;
6306          MapReduceValuesToDoubleTask
6307 <            (BulkTask<K,V,?> p, int b, boolean split,
6307 >            (ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b,
6308 >             MapReduceValuesToDoubleTask<K,V> nextRight,
6309               ObjectToDouble<? super V> transformer,
6310               double basis,
6311               DoubleByDoubleToDouble reducer) {
6312 <            super(p, b, split);
6312 >            super(m, p, b); this.nextRight = nextRight;
6313              this.transformer = transformer;
6314              this.basis = basis; this.reducer = reducer;
6315          }
6316 <        public final void compute() {
6317 <            MapReduceValuesToDoubleTask<K,V> t = this;
6318 <            final ObjectToDouble<? super V> transformer =
6319 <                this.transformer;
6320 <            final DoubleByDoubleToDouble reducer = this.reducer;
6321 <            if (transformer == null || reducer == null)
6322 <                throw new Error(NullFunctionMessage);
6323 <            final double id = this.basis;
6324 <            int b = batch();
6325 <            while (b > 1 && t.baseIndex != t.baseLimit) {
6326 <                b >>>= 1;
6327 <                t.pending = 1;
6328 <                MapReduceValuesToDoubleTask<K,V> rt =
6329 <                    new MapReduceValuesToDoubleTask<K,V>
6330 <                    (t, b, true, transformer, id, reducer);
6331 <                t = new MapReduceValuesToDoubleTask<K,V>
6332 <                    (t, b, false, transformer, id, reducer);
6333 <                t.sibling = rt;
6334 <                rt.sibling = t;
6335 <                rt.fork();
6336 <            }
6337 <            double r = id;
6338 <            Object v;
5986 <            while ((v = t.advance()) != null)
5987 <                r = reducer.apply(r, transformer.apply((V)v));
5988 <            t.result = r;
5989 <            for (;;) {
5990 <                int c; BulkTask<K,V,?> par; MapReduceValuesToDoubleTask<K,V> s, p;
5991 <                if ((par = t.parent) == null ||
5992 <                    !(par instanceof MapReduceValuesToDoubleTask)) {
5993 <                    t.quietlyComplete();
5994 <                    break;
5995 <                }
5996 <                else if ((c = (p = (MapReduceValuesToDoubleTask<K,V>)par).pending) == 0) {
5997 <                    if ((s = t.sibling) != null)
5998 <                        r = reducer.apply(r, s.result);
5999 <                    (t = p).result = r;
6316 >        public final Double getRawResult() { return result; }
6317 >        @SuppressWarnings("unchecked") public final void compute() {
6318 >            final ObjectToDouble<? super V> transformer;
6319 >            final DoubleByDoubleToDouble reducer;
6320 >            if ((transformer = this.transformer) != null &&
6321 >                (reducer = this.reducer) != null) {
6322 >                double r = this.basis;
6323 >                for (int b; (b = preSplit()) > 0;)
6324 >                    (rights = new MapReduceValuesToDoubleTask<K,V>
6325 >                     (map, this, b, rights, transformer, r, reducer)).fork();
6326 >                V v;
6327 >                while ((v = advance()) != null)
6328 >                    r = reducer.apply(r, transformer.apply(v));
6329 >                result = r;
6330 >                CountedCompleter<?> c;
6331 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
6332 >                    MapReduceValuesToDoubleTask<K,V>
6333 >                        t = (MapReduceValuesToDoubleTask<K,V>)c,
6334 >                        s = t.rights;
6335 >                    while (s != null) {
6336 >                        t.result = reducer.apply(t.result, s.result);
6337 >                        s = t.rights = s.nextRight;
6338 >                    }
6339                  }
6001                else if (p.casPending(c, 0))
6002                    break;
6340              }
6341          }
6005        public final Double getRawResult() { return result; }
6342      }
6343  
6344 <    static final class MapReduceEntriesToDoubleTask<K,V>
6345 <        extends BulkTask<K,V,Double> {
6344 >    @SuppressWarnings("serial") static final class MapReduceEntriesToDoubleTask<K,V>
6345 >        extends Traverser<K,V,Double> {
6346          final ObjectToDouble<Map.Entry<K,V>> transformer;
6347          final DoubleByDoubleToDouble reducer;
6348          final double basis;
6349          double result;
6350 <        MapReduceEntriesToDoubleTask<K,V> sibling;
6015 <        MapReduceEntriesToDoubleTask
6016 <            (ConcurrentHashMapV8<K,V> m,
6017 <             ObjectToDouble<Map.Entry<K,V>> transformer,
6018 <             double basis,
6019 <             DoubleByDoubleToDouble reducer) {
6020 <            super(m);
6021 <            this.transformer = transformer;
6022 <            this.basis = basis; this.reducer = reducer;
6023 <        }
6350 >        MapReduceEntriesToDoubleTask<K,V> rights, nextRight;
6351          MapReduceEntriesToDoubleTask
6352 <            (BulkTask<K,V,?> p, int b, boolean split,
6352 >            (ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b,
6353 >             MapReduceEntriesToDoubleTask<K,V> nextRight,
6354               ObjectToDouble<Map.Entry<K,V>> transformer,
6355               double basis,
6356               DoubleByDoubleToDouble reducer) {
6357 <            super(p, b, split);
6357 >            super(m, p, b); this.nextRight = nextRight;
6358              this.transformer = transformer;
6359              this.basis = basis; this.reducer = reducer;
6360          }
6361 <        public final void compute() {
6362 <            MapReduceEntriesToDoubleTask<K,V> t = this;
6363 <            final ObjectToDouble<Map.Entry<K,V>> transformer =
6364 <                this.transformer;
6365 <            final DoubleByDoubleToDouble reducer = this.reducer;
6366 <            if (transformer == null || reducer == null)
6367 <                throw new Error(NullFunctionMessage);
6368 <            final double id = this.basis;
6369 <            int b = batch();
6370 <            while (b > 1 && t.baseIndex != t.baseLimit) {
6371 <                b >>>= 1;
6372 <                t.pending = 1;
6373 <                MapReduceEntriesToDoubleTask<K,V> rt =
6374 <                    new MapReduceEntriesToDoubleTask<K,V>
6375 <                    (t, b, true, transformer, id, reducer);
6376 <                t = new MapReduceEntriesToDoubleTask<K,V>
6377 <                    (t, b, false, transformer, id, reducer);
6378 <                t.sibling = rt;
6379 <                rt.sibling = t;
6380 <                rt.fork();
6381 <            }
6382 <            double r = id;
6383 <            Object v;
6384 <            while ((v = t.advance()) != null)
6057 <                r = reducer.apply(r, transformer.apply(entryFor((K)t.nextKey, (V)v)));
6058 <            t.result = r;
6059 <            for (;;) {
6060 <                int c; BulkTask<K,V,?> par; MapReduceEntriesToDoubleTask<K,V> s, p;
6061 <                if ((par = t.parent) == null ||
6062 <                    !(par instanceof MapReduceEntriesToDoubleTask)) {
6063 <                    t.quietlyComplete();
6064 <                    break;
6065 <                }
6066 <                else if ((c = (p = (MapReduceEntriesToDoubleTask<K,V>)par).pending) == 0) {
6067 <                    if ((s = t.sibling) != null)
6068 <                        r = reducer.apply(r, s.result);
6069 <                    (t = p).result = r;
6361 >        public final Double getRawResult() { return result; }
6362 >        @SuppressWarnings("unchecked") public final void compute() {
6363 >            final ObjectToDouble<Map.Entry<K,V>> transformer;
6364 >            final DoubleByDoubleToDouble reducer;
6365 >            if ((transformer = this.transformer) != null &&
6366 >                (reducer = this.reducer) != null) {
6367 >                double r = this.basis;
6368 >                for (int b; (b = preSplit()) > 0;)
6369 >                    (rights = new MapReduceEntriesToDoubleTask<K,V>
6370 >                     (map, this, b, rights, transformer, r, reducer)).fork();
6371 >                V v;
6372 >                while ((v = advance()) != null)
6373 >                    r = reducer.apply(r, transformer.apply(entryFor((K)nextKey,
6374 >                                                                    v)));
6375 >                result = r;
6376 >                CountedCompleter<?> c;
6377 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
6378 >                    MapReduceEntriesToDoubleTask<K,V>
6379 >                        t = (MapReduceEntriesToDoubleTask<K,V>)c,
6380 >                        s = t.rights;
6381 >                    while (s != null) {
6382 >                        t.result = reducer.apply(t.result, s.result);
6383 >                        s = t.rights = s.nextRight;
6384 >                    }
6385                  }
6071                else if (p.casPending(c, 0))
6072                    break;
6386              }
6387          }
6075        public final Double getRawResult() { return result; }
6388      }
6389  
6390 <    static final class MapReduceMappingsToDoubleTask<K,V>
6391 <        extends BulkTask<K,V,Double> {
6390 >    @SuppressWarnings("serial") static final class MapReduceMappingsToDoubleTask<K,V>
6391 >        extends Traverser<K,V,Double> {
6392          final ObjectByObjectToDouble<? super K, ? super V> transformer;
6393          final DoubleByDoubleToDouble reducer;
6394          final double basis;
6395          double result;
6396 <        MapReduceMappingsToDoubleTask<K,V> sibling;
6396 >        MapReduceMappingsToDoubleTask<K,V> rights, nextRight;
6397          MapReduceMappingsToDoubleTask
6398 <            (ConcurrentHashMapV8<K,V> m,
6398 >            (ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b,
6399 >             MapReduceMappingsToDoubleTask<K,V> nextRight,
6400               ObjectByObjectToDouble<? super K, ? super V> transformer,
6401               double basis,
6402               DoubleByDoubleToDouble reducer) {
6403 <            super(m);
6403 >            super(m, p, b); this.nextRight = nextRight;
6404              this.transformer = transformer;
6405              this.basis = basis; this.reducer = reducer;
6406          }
6407 <        MapReduceMappingsToDoubleTask
6408 <            (BulkTask<K,V,?> p, int b, boolean split,
6409 <             ObjectByObjectToDouble<? super K, ? super V> transformer,
6410 <             double basis,
6411 <             DoubleByDoubleToDouble reducer) {
6412 <            super(p, b, split);
6413 <            this.transformer = transformer;
6414 <            this.basis = basis; this.reducer = reducer;
6415 <        }
6416 <        public final void compute() {
6417 <            MapReduceMappingsToDoubleTask<K,V> t = this;
6418 <            final ObjectByObjectToDouble<? super K, ? super V> transformer =
6419 <                this.transformer;
6420 <            final DoubleByDoubleToDouble reducer = this.reducer;
6421 <            if (transformer == null || reducer == null)
6422 <                throw new Error(NullFunctionMessage);
6423 <            final double id = this.basis;
6424 <            int b = batch();
6425 <            while (b > 1 && t.baseIndex != t.baseLimit) {
6426 <                b >>>= 1;
6427 <                t.pending = 1;
6428 <                MapReduceMappingsToDoubleTask<K,V> rt =
6429 <                    new MapReduceMappingsToDoubleTask<K,V>
6117 <                    (t, b, true, transformer, id, reducer);
6118 <                t = new MapReduceMappingsToDoubleTask<K,V>
6119 <                    (t, b, false, transformer, id, reducer);
6120 <                t.sibling = rt;
6121 <                rt.sibling = t;
6122 <                rt.fork();
6123 <            }
6124 <            double r = id;
6125 <            Object v;
6126 <            while ((v = t.advance()) != null)
6127 <                r = reducer.apply(r, transformer.apply((K)t.nextKey, (V)v));
6128 <            t.result = r;
6129 <            for (;;) {
6130 <                int c; BulkTask<K,V,?> par; MapReduceMappingsToDoubleTask<K,V> s, p;
6131 <                if ((par = t.parent) == null ||
6132 <                    !(par instanceof MapReduceMappingsToDoubleTask)) {
6133 <                    t.quietlyComplete();
6134 <                    break;
6135 <                }
6136 <                else if ((c = (p = (MapReduceMappingsToDoubleTask<K,V>)par).pending) == 0) {
6137 <                    if ((s = t.sibling) != null)
6138 <                        r = reducer.apply(r, s.result);
6139 <                    (t = p).result = r;
6407 >        public final Double getRawResult() { return result; }
6408 >        @SuppressWarnings("unchecked") public final void compute() {
6409 >            final ObjectByObjectToDouble<? super K, ? super V> transformer;
6410 >            final DoubleByDoubleToDouble reducer;
6411 >            if ((transformer = this.transformer) != null &&
6412 >                (reducer = this.reducer) != null) {
6413 >                double r = this.basis;
6414 >                for (int b; (b = preSplit()) > 0;)
6415 >                    (rights = new MapReduceMappingsToDoubleTask<K,V>
6416 >                     (map, this, b, rights, transformer, r, reducer)).fork();
6417 >                V v;
6418 >                while ((v = advance()) != null)
6419 >                    r = reducer.apply(r, transformer.apply((K)nextKey, v));
6420 >                result = r;
6421 >                CountedCompleter<?> c;
6422 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
6423 >                    MapReduceMappingsToDoubleTask<K,V>
6424 >                        t = (MapReduceMappingsToDoubleTask<K,V>)c,
6425 >                        s = t.rights;
6426 >                    while (s != null) {
6427 >                        t.result = reducer.apply(t.result, s.result);
6428 >                        s = t.rights = s.nextRight;
6429 >                    }
6430                  }
6141                else if (p.casPending(c, 0))
6142                    break;
6431              }
6432          }
6145        public final Double getRawResult() { return result; }
6433      }
6434  
6435 <    static final class MapReduceKeysToLongTask<K,V>
6436 <        extends BulkTask<K,V,Long> {
6435 >    @SuppressWarnings("serial") static final class MapReduceKeysToLongTask<K,V>
6436 >        extends Traverser<K,V,Long> {
6437          final ObjectToLong<? super K> transformer;
6438          final LongByLongToLong reducer;
6439          final long basis;
6440          long result;
6441 <        MapReduceKeysToLongTask<K,V> sibling;
6155 <        MapReduceKeysToLongTask
6156 <            (ConcurrentHashMapV8<K,V> m,
6157 <             ObjectToLong<? super K> transformer,
6158 <             long basis,
6159 <             LongByLongToLong reducer) {
6160 <            super(m);
6161 <            this.transformer = transformer;
6162 <            this.basis = basis; this.reducer = reducer;
6163 <        }
6441 >        MapReduceKeysToLongTask<K,V> rights, nextRight;
6442          MapReduceKeysToLongTask
6443 <            (BulkTask<K,V,?> p, int b, boolean split,
6443 >            (ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b,
6444 >             MapReduceKeysToLongTask<K,V> nextRight,
6445               ObjectToLong<? super K> transformer,
6446               long basis,
6447               LongByLongToLong reducer) {
6448 <            super(p, b, split);
6448 >            super(m, p, b); this.nextRight = nextRight;
6449              this.transformer = transformer;
6450              this.basis = basis; this.reducer = reducer;
6451          }
6452 <        public final void compute() {
6453 <            MapReduceKeysToLongTask<K,V> t = this;
6454 <            final ObjectToLong<? super K> transformer =
6455 <                this.transformer;
6456 <            final LongByLongToLong reducer = this.reducer;
6457 <            if (transformer == null || reducer == null)
6458 <                throw new Error(NullFunctionMessage);
6459 <            final long id = this.basis;
6460 <            int b = batch();
6461 <            while (b > 1 && t.baseIndex != t.baseLimit) {
6462 <                b >>>= 1;
6463 <                t.pending = 1;
6464 <                MapReduceKeysToLongTask<K,V> rt =
6465 <                    new MapReduceKeysToLongTask<K,V>
6466 <                    (t, b, true, transformer, id, reducer);
6467 <                t = new MapReduceKeysToLongTask<K,V>
6468 <                    (t, b, false, transformer, id, reducer);
6469 <                t.sibling = rt;
6470 <                rt.sibling = t;
6471 <                rt.fork();
6472 <            }
6473 <            long r = id;
6195 <            while (t.advance() != null)
6196 <                r = reducer.apply(r, transformer.apply((K)t.nextKey));
6197 <            t.result = r;
6198 <            for (;;) {
6199 <                int c; BulkTask<K,V,?> par; MapReduceKeysToLongTask<K,V> s, p;
6200 <                if ((par = t.parent) == null ||
6201 <                    !(par instanceof MapReduceKeysToLongTask)) {
6202 <                    t.quietlyComplete();
6203 <                    break;
6204 <                }
6205 <                else if ((c = (p = (MapReduceKeysToLongTask<K,V>)par).pending) == 0) {
6206 <                    if ((s = t.sibling) != null)
6207 <                        r = reducer.apply(r, s.result);
6208 <                    (t = p).result = r;
6452 >        public final Long getRawResult() { return result; }
6453 >        @SuppressWarnings("unchecked") public final void compute() {
6454 >            final ObjectToLong<? super K> transformer;
6455 >            final LongByLongToLong reducer;
6456 >            if ((transformer = this.transformer) != null &&
6457 >                (reducer = this.reducer) != null) {
6458 >                long r = this.basis;
6459 >                for (int b; (b = preSplit()) > 0;)
6460 >                    (rights = new MapReduceKeysToLongTask<K,V>
6461 >                     (map, this, b, rights, transformer, r, reducer)).fork();
6462 >                while (advance() != null)
6463 >                    r = reducer.apply(r, transformer.apply((K)nextKey));
6464 >                result = r;
6465 >                CountedCompleter<?> c;
6466 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
6467 >                    MapReduceKeysToLongTask<K,V>
6468 >                        t = (MapReduceKeysToLongTask<K,V>)c,
6469 >                        s = t.rights;
6470 >                    while (s != null) {
6471 >                        t.result = reducer.apply(t.result, s.result);
6472 >                        s = t.rights = s.nextRight;
6473 >                    }
6474                  }
6210                else if (p.casPending(c, 0))
6211                    break;
6475              }
6476          }
6214        public final Long getRawResult() { return result; }
6477      }
6478  
6479 <    static final class MapReduceValuesToLongTask<K,V>
6480 <        extends BulkTask<K,V,Long> {
6479 >    @SuppressWarnings("serial") static final class MapReduceValuesToLongTask<K,V>
6480 >        extends Traverser<K,V,Long> {
6481          final ObjectToLong<? super V> transformer;
6482          final LongByLongToLong reducer;
6483          final long basis;
6484          long result;
6485 <        MapReduceValuesToLongTask<K,V> sibling;
6224 <        MapReduceValuesToLongTask
6225 <            (ConcurrentHashMapV8<K,V> m,
6226 <             ObjectToLong<? super V> transformer,
6227 <             long basis,
6228 <             LongByLongToLong reducer) {
6229 <            super(m);
6230 <            this.transformer = transformer;
6231 <            this.basis = basis; this.reducer = reducer;
6232 <        }
6485 >        MapReduceValuesToLongTask<K,V> rights, nextRight;
6486          MapReduceValuesToLongTask
6487 <            (BulkTask<K,V,?> p, int b, boolean split,
6487 >            (ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b,
6488 >             MapReduceValuesToLongTask<K,V> nextRight,
6489               ObjectToLong<? super V> transformer,
6490               long basis,
6491               LongByLongToLong reducer) {
6492 <            super(p, b, split);
6492 >            super(m, p, b); this.nextRight = nextRight;
6493              this.transformer = transformer;
6494              this.basis = basis; this.reducer = reducer;
6495          }
6496 <        public final void compute() {
6497 <            MapReduceValuesToLongTask<K,V> t = this;
6498 <            final ObjectToLong<? super V> transformer =
6499 <                this.transformer;
6500 <            final LongByLongToLong reducer = this.reducer;
6501 <            if (transformer == null || reducer == null)
6502 <                throw new Error(NullFunctionMessage);
6503 <            final long id = this.basis;
6504 <            int b = batch();
6505 <            while (b > 1 && t.baseIndex != t.baseLimit) {
6506 <                b >>>= 1;
6507 <                t.pending = 1;
6508 <                MapReduceValuesToLongTask<K,V> rt =
6509 <                    new MapReduceValuesToLongTask<K,V>
6510 <                    (t, b, true, transformer, id, reducer);
6511 <                t = new MapReduceValuesToLongTask<K,V>
6512 <                    (t, b, false, transformer, id, reducer);
6513 <                t.sibling = rt;
6514 <                rt.sibling = t;
6515 <                rt.fork();
6516 <            }
6517 <            long r = id;
6518 <            Object v;
6265 <            while ((v = t.advance()) != null)
6266 <                r = reducer.apply(r, transformer.apply((V)v));
6267 <            t.result = r;
6268 <            for (;;) {
6269 <                int c; BulkTask<K,V,?> par; MapReduceValuesToLongTask<K,V> s, p;
6270 <                if ((par = t.parent) == null ||
6271 <                    !(par instanceof MapReduceValuesToLongTask)) {
6272 <                    t.quietlyComplete();
6273 <                    break;
6274 <                }
6275 <                else if ((c = (p = (MapReduceValuesToLongTask<K,V>)par).pending) == 0) {
6276 <                    if ((s = t.sibling) != null)
6277 <                        r = reducer.apply(r, s.result);
6278 <                    (t = p).result = r;
6496 >        public final Long getRawResult() { return result; }
6497 >        @SuppressWarnings("unchecked") public final void compute() {
6498 >            final ObjectToLong<? super V> transformer;
6499 >            final LongByLongToLong reducer;
6500 >            if ((transformer = this.transformer) != null &&
6501 >                (reducer = this.reducer) != null) {
6502 >                long r = this.basis;
6503 >                for (int b; (b = preSplit()) > 0;)
6504 >                    (rights = new MapReduceValuesToLongTask<K,V>
6505 >                     (map, this, b, rights, transformer, r, reducer)).fork();
6506 >                V v;
6507 >                while ((v = advance()) != null)
6508 >                    r = reducer.apply(r, transformer.apply(v));
6509 >                result = r;
6510 >                CountedCompleter<?> c;
6511 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
6512 >                    MapReduceValuesToLongTask<K,V>
6513 >                        t = (MapReduceValuesToLongTask<K,V>)c,
6514 >                        s = t.rights;
6515 >                    while (s != null) {
6516 >                        t.result = reducer.apply(t.result, s.result);
6517 >                        s = t.rights = s.nextRight;
6518 >                    }
6519                  }
6280                else if (p.casPending(c, 0))
6281                    break;
6520              }
6521          }
6284        public final Long getRawResult() { return result; }
6522      }
6523  
6524 <    static final class MapReduceEntriesToLongTask<K,V>
6525 <        extends BulkTask<K,V,Long> {
6524 >    @SuppressWarnings("serial") static final class MapReduceEntriesToLongTask<K,V>
6525 >        extends Traverser<K,V,Long> {
6526          final ObjectToLong<Map.Entry<K,V>> transformer;
6527          final LongByLongToLong reducer;
6528          final long basis;
6529          long result;
6530 <        MapReduceEntriesToLongTask<K,V> sibling;
6294 <        MapReduceEntriesToLongTask
6295 <            (ConcurrentHashMapV8<K,V> m,
6296 <             ObjectToLong<Map.Entry<K,V>> transformer,
6297 <             long basis,
6298 <             LongByLongToLong reducer) {
6299 <            super(m);
6300 <            this.transformer = transformer;
6301 <            this.basis = basis; this.reducer = reducer;
6302 <        }
6530 >        MapReduceEntriesToLongTask<K,V> rights, nextRight;
6531          MapReduceEntriesToLongTask
6532 <            (BulkTask<K,V,?> p, int b, boolean split,
6532 >            (ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b,
6533 >             MapReduceEntriesToLongTask<K,V> nextRight,
6534               ObjectToLong<Map.Entry<K,V>> transformer,
6535               long basis,
6536               LongByLongToLong reducer) {
6537 <            super(p, b, split);
6537 >            super(m, p, b); this.nextRight = nextRight;
6538              this.transformer = transformer;
6539              this.basis = basis; this.reducer = reducer;
6540          }
6541 <        public final void compute() {
6542 <            MapReduceEntriesToLongTask<K,V> t = this;
6543 <            final ObjectToLong<Map.Entry<K,V>> transformer =
6544 <                this.transformer;
6545 <            final LongByLongToLong reducer = this.reducer;
6546 <            if (transformer == null || reducer == null)
6547 <                throw new Error(NullFunctionMessage);
6548 <            final long id = this.basis;
6549 <            int b = batch();
6550 <            while (b > 1 && t.baseIndex != t.baseLimit) {
6551 <                b >>>= 1;
6552 <                t.pending = 1;
6553 <                MapReduceEntriesToLongTask<K,V> rt =
6554 <                    new MapReduceEntriesToLongTask<K,V>
6555 <                    (t, b, true, transformer, id, reducer);
6556 <                t = new MapReduceEntriesToLongTask<K,V>
6557 <                    (t, b, false, transformer, id, reducer);
6558 <                t.sibling = rt;
6559 <                rt.sibling = t;
6560 <                rt.fork();
6561 <            }
6562 <            long r = id;
6563 <            Object v;
6564 <            while ((v = t.advance()) != null)
6336 <                r = reducer.apply(r, transformer.apply(entryFor((K)t.nextKey, (V)v)));
6337 <            t.result = r;
6338 <            for (;;) {
6339 <                int c; BulkTask<K,V,?> par; MapReduceEntriesToLongTask<K,V> s, p;
6340 <                if ((par = t.parent) == null ||
6341 <                    !(par instanceof MapReduceEntriesToLongTask)) {
6342 <                    t.quietlyComplete();
6343 <                    break;
6344 <                }
6345 <                else if ((c = (p = (MapReduceEntriesToLongTask<K,V>)par).pending) == 0) {
6346 <                    if ((s = t.sibling) != null)
6347 <                        r = reducer.apply(r, s.result);
6348 <                    (t = p).result = r;
6541 >        public final Long getRawResult() { return result; }
6542 >        @SuppressWarnings("unchecked") public final void compute() {
6543 >            final ObjectToLong<Map.Entry<K,V>> transformer;
6544 >            final LongByLongToLong reducer;
6545 >            if ((transformer = this.transformer) != null &&
6546 >                (reducer = this.reducer) != null) {
6547 >                long r = this.basis;
6548 >                for (int b; (b = preSplit()) > 0;)
6549 >                    (rights = new MapReduceEntriesToLongTask<K,V>
6550 >                     (map, this, b, rights, transformer, r, reducer)).fork();
6551 >                V v;
6552 >                while ((v = advance()) != null)
6553 >                    r = reducer.apply(r, transformer.apply(entryFor((K)nextKey,
6554 >                                                                    v)));
6555 >                result = r;
6556 >                CountedCompleter<?> c;
6557 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
6558 >                    MapReduceEntriesToLongTask<K,V>
6559 >                        t = (MapReduceEntriesToLongTask<K,V>)c,
6560 >                        s = t.rights;
6561 >                    while (s != null) {
6562 >                        t.result = reducer.apply(t.result, s.result);
6563 >                        s = t.rights = s.nextRight;
6564 >                    }
6565                  }
6350                else if (p.casPending(c, 0))
6351                    break;
6566              }
6567          }
6354        public final Long getRawResult() { return result; }
6568      }
6569  
6570 <    static final class MapReduceMappingsToLongTask<K,V>
6571 <        extends BulkTask<K,V,Long> {
6570 >    @SuppressWarnings("serial") static final class MapReduceMappingsToLongTask<K,V>
6571 >        extends Traverser<K,V,Long> {
6572          final ObjectByObjectToLong<? super K, ? super V> transformer;
6573          final LongByLongToLong reducer;
6574          final long basis;
6575          long result;
6576 <        MapReduceMappingsToLongTask<K,V> sibling;
6364 <        MapReduceMappingsToLongTask
6365 <            (ConcurrentHashMapV8<K,V> m,
6366 <             ObjectByObjectToLong<? super K, ? super V> transformer,
6367 <             long basis,
6368 <             LongByLongToLong reducer) {
6369 <            super(m);
6370 <            this.transformer = transformer;
6371 <            this.basis = basis; this.reducer = reducer;
6372 <        }
6576 >        MapReduceMappingsToLongTask<K,V> rights, nextRight;
6577          MapReduceMappingsToLongTask
6578 <            (BulkTask<K,V,?> p, int b, boolean split,
6578 >            (ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b,
6579 >             MapReduceMappingsToLongTask<K,V> nextRight,
6580               ObjectByObjectToLong<? super K, ? super V> transformer,
6581               long basis,
6582               LongByLongToLong reducer) {
6583 <            super(p, b, split);
6583 >            super(m, p, b); this.nextRight = nextRight;
6584              this.transformer = transformer;
6585              this.basis = basis; this.reducer = reducer;
6586          }
6587 <        public final void compute() {
6588 <            MapReduceMappingsToLongTask<K,V> t = this;
6589 <            final ObjectByObjectToLong<? super K, ? super V> transformer =
6590 <                this.transformer;
6591 <            final LongByLongToLong reducer = this.reducer;
6592 <            if (transformer == null || reducer == null)
6593 <                throw new Error(NullFunctionMessage);
6594 <            final long id = this.basis;
6595 <            int b = batch();
6596 <            while (b > 1 && t.baseIndex != t.baseLimit) {
6597 <                b >>>= 1;
6598 <                t.pending = 1;
6599 <                MapReduceMappingsToLongTask<K,V> rt =
6600 <                    new MapReduceMappingsToLongTask<K,V>
6601 <                    (t, b, true, transformer, id, reducer);
6602 <                t = new MapReduceMappingsToLongTask<K,V>
6603 <                    (t, b, false, transformer, id, reducer);
6604 <                t.sibling = rt;
6605 <                rt.sibling = t;
6606 <                rt.fork();
6607 <            }
6608 <            long r = id;
6609 <            Object v;
6405 <            while ((v = t.advance()) != null)
6406 <                r = reducer.apply(r, transformer.apply((K)t.nextKey, (V)v));
6407 <            t.result = r;
6408 <            for (;;) {
6409 <                int c; BulkTask<K,V,?> par; MapReduceMappingsToLongTask<K,V> s, p;
6410 <                if ((par = t.parent) == null ||
6411 <                    !(par instanceof MapReduceMappingsToLongTask)) {
6412 <                    t.quietlyComplete();
6413 <                    break;
6414 <                }
6415 <                else if ((c = (p = (MapReduceMappingsToLongTask<K,V>)par).pending) == 0) {
6416 <                    if ((s = t.sibling) != null)
6417 <                        r = reducer.apply(r, s.result);
6418 <                    (t = p).result = r;
6587 >        public final Long getRawResult() { return result; }
6588 >        @SuppressWarnings("unchecked") public final void compute() {
6589 >            final ObjectByObjectToLong<? super K, ? super V> transformer;
6590 >            final LongByLongToLong reducer;
6591 >            if ((transformer = this.transformer) != null &&
6592 >                (reducer = this.reducer) != null) {
6593 >                long r = this.basis;
6594 >                for (int b; (b = preSplit()) > 0;)
6595 >                    (rights = new MapReduceMappingsToLongTask<K,V>
6596 >                     (map, this, b, rights, transformer, r, reducer)).fork();
6597 >                V v;
6598 >                while ((v = advance()) != null)
6599 >                    r = reducer.apply(r, transformer.apply((K)nextKey, v));
6600 >                result = r;
6601 >                CountedCompleter<?> c;
6602 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
6603 >                    MapReduceMappingsToLongTask<K,V>
6604 >                        t = (MapReduceMappingsToLongTask<K,V>)c,
6605 >                        s = t.rights;
6606 >                    while (s != null) {
6607 >                        t.result = reducer.apply(t.result, s.result);
6608 >                        s = t.rights = s.nextRight;
6609 >                    }
6610                  }
6420                else if (p.casPending(c, 0))
6421                    break;
6611              }
6612          }
6424        public final Long getRawResult() { return result; }
6613      }
6614  
6615 <    static final class MapReduceKeysToIntTask<K,V>
6616 <        extends BulkTask<K,V,Integer> {
6615 >    @SuppressWarnings("serial") static final class MapReduceKeysToIntTask<K,V>
6616 >        extends Traverser<K,V,Integer> {
6617          final ObjectToInt<? super K> transformer;
6618          final IntByIntToInt reducer;
6619          final int basis;
6620          int result;
6621 <        MapReduceKeysToIntTask<K,V> sibling;
6434 <        MapReduceKeysToIntTask
6435 <            (ConcurrentHashMapV8<K,V> m,
6436 <             ObjectToInt<? super K> transformer,
6437 <             int basis,
6438 <             IntByIntToInt reducer) {
6439 <            super(m);
6440 <            this.transformer = transformer;
6441 <            this.basis = basis; this.reducer = reducer;
6442 <        }
6621 >        MapReduceKeysToIntTask<K,V> rights, nextRight;
6622          MapReduceKeysToIntTask
6623 <            (BulkTask<K,V,?> p, int b, boolean split,
6623 >            (ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b,
6624 >             MapReduceKeysToIntTask<K,V> nextRight,
6625               ObjectToInt<? super K> transformer,
6626               int basis,
6627               IntByIntToInt reducer) {
6628 <            super(p, b, split);
6628 >            super(m, p, b); this.nextRight = nextRight;
6629              this.transformer = transformer;
6630              this.basis = basis; this.reducer = reducer;
6631          }
6632 <        public final void compute() {
6633 <            MapReduceKeysToIntTask<K,V> t = this;
6634 <            final ObjectToInt<? super K> transformer =
6635 <                this.transformer;
6636 <            final IntByIntToInt reducer = this.reducer;
6637 <            if (transformer == null || reducer == null)
6638 <                throw new Error(NullFunctionMessage);
6639 <            final int id = this.basis;
6640 <            int b = batch();
6641 <            while (b > 1 && t.baseIndex != t.baseLimit) {
6642 <                b >>>= 1;
6643 <                t.pending = 1;
6644 <                MapReduceKeysToIntTask<K,V> rt =
6645 <                    new MapReduceKeysToIntTask<K,V>
6646 <                    (t, b, true, transformer, id, reducer);
6647 <                t = new MapReduceKeysToIntTask<K,V>
6648 <                    (t, b, false, transformer, id, reducer);
6649 <                t.sibling = rt;
6650 <                rt.sibling = t;
6651 <                rt.fork();
6652 <            }
6653 <            int r = id;
6474 <            while (t.advance() != null)
6475 <                r = reducer.apply(r, transformer.apply((K)t.nextKey));
6476 <            t.result = r;
6477 <            for (;;) {
6478 <                int c; BulkTask<K,V,?> par; MapReduceKeysToIntTask<K,V> s, p;
6479 <                if ((par = t.parent) == null ||
6480 <                    !(par instanceof MapReduceKeysToIntTask)) {
6481 <                    t.quietlyComplete();
6482 <                    break;
6483 <                }
6484 <                else if ((c = (p = (MapReduceKeysToIntTask<K,V>)par).pending) == 0) {
6485 <                    if ((s = t.sibling) != null)
6486 <                        r = reducer.apply(r, s.result);
6487 <                    (t = p).result = r;
6632 >        public final Integer getRawResult() { return result; }
6633 >        @SuppressWarnings("unchecked") public final void compute() {
6634 >            final ObjectToInt<? super K> transformer;
6635 >            final IntByIntToInt reducer;
6636 >            if ((transformer = this.transformer) != null &&
6637 >                (reducer = this.reducer) != null) {
6638 >                int r = this.basis;
6639 >                for (int b; (b = preSplit()) > 0;)
6640 >                    (rights = new MapReduceKeysToIntTask<K,V>
6641 >                     (map, this, b, rights, transformer, r, reducer)).fork();
6642 >                while (advance() != null)
6643 >                    r = reducer.apply(r, transformer.apply((K)nextKey));
6644 >                result = r;
6645 >                CountedCompleter<?> c;
6646 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
6647 >                    MapReduceKeysToIntTask<K,V>
6648 >                        t = (MapReduceKeysToIntTask<K,V>)c,
6649 >                        s = t.rights;
6650 >                    while (s != null) {
6651 >                        t.result = reducer.apply(t.result, s.result);
6652 >                        s = t.rights = s.nextRight;
6653 >                    }
6654                  }
6489                else if (p.casPending(c, 0))
6490                    break;
6655              }
6656          }
6493        public final Integer getRawResult() { return result; }
6657      }
6658  
6659 <    static final class MapReduceValuesToIntTask<K,V>
6660 <        extends BulkTask<K,V,Integer> {
6659 >    @SuppressWarnings("serial") static final class MapReduceValuesToIntTask<K,V>
6660 >        extends Traverser<K,V,Integer> {
6661          final ObjectToInt<? super V> transformer;
6662          final IntByIntToInt reducer;
6663          final int basis;
6664          int result;
6665 <        MapReduceValuesToIntTask<K,V> sibling;
6503 <        MapReduceValuesToIntTask
6504 <            (ConcurrentHashMapV8<K,V> m,
6505 <             ObjectToInt<? super V> transformer,
6506 <             int basis,
6507 <             IntByIntToInt reducer) {
6508 <            super(m);
6509 <            this.transformer = transformer;
6510 <            this.basis = basis; this.reducer = reducer;
6511 <        }
6665 >        MapReduceValuesToIntTask<K,V> rights, nextRight;
6666          MapReduceValuesToIntTask
6667 <            (BulkTask<K,V,?> p, int b, boolean split,
6667 >            (ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b,
6668 >             MapReduceValuesToIntTask<K,V> nextRight,
6669               ObjectToInt<? super V> transformer,
6670               int basis,
6671               IntByIntToInt reducer) {
6672 <            super(p, b, split);
6672 >            super(m, p, b); this.nextRight = nextRight;
6673              this.transformer = transformer;
6674              this.basis = basis; this.reducer = reducer;
6675          }
6676 <        public final void compute() {
6677 <            MapReduceValuesToIntTask<K,V> t = this;
6678 <            final ObjectToInt<? super V> transformer =
6679 <                this.transformer;
6680 <            final IntByIntToInt reducer = this.reducer;
6681 <            if (transformer == null || reducer == null)
6682 <                throw new Error(NullFunctionMessage);
6683 <            final int id = this.basis;
6684 <            int b = batch();
6685 <            while (b > 1 && t.baseIndex != t.baseLimit) {
6686 <                b >>>= 1;
6687 <                t.pending = 1;
6688 <                MapReduceValuesToIntTask<K,V> rt =
6689 <                    new MapReduceValuesToIntTask<K,V>
6690 <                    (t, b, true, transformer, id, reducer);
6691 <                t = new MapReduceValuesToIntTask<K,V>
6692 <                    (t, b, false, transformer, id, reducer);
6693 <                t.sibling = rt;
6694 <                rt.sibling = t;
6695 <                rt.fork();
6696 <            }
6697 <            int r = id;
6698 <            Object v;
6544 <            while ((v = t.advance()) != null)
6545 <                r = reducer.apply(r, transformer.apply((V)v));
6546 <            t.result = r;
6547 <            for (;;) {
6548 <                int c; BulkTask<K,V,?> par; MapReduceValuesToIntTask<K,V> s, p;
6549 <                if ((par = t.parent) == null ||
6550 <                    !(par instanceof MapReduceValuesToIntTask)) {
6551 <                    t.quietlyComplete();
6552 <                    break;
6553 <                }
6554 <                else if ((c = (p = (MapReduceValuesToIntTask<K,V>)par).pending) == 0) {
6555 <                    if ((s = t.sibling) != null)
6556 <                        r = reducer.apply(r, s.result);
6557 <                    (t = p).result = r;
6676 >        public final Integer getRawResult() { return result; }
6677 >        @SuppressWarnings("unchecked") public final void compute() {
6678 >            final ObjectToInt<? super V> transformer;
6679 >            final IntByIntToInt reducer;
6680 >            if ((transformer = this.transformer) != null &&
6681 >                (reducer = this.reducer) != null) {
6682 >                int r = this.basis;
6683 >                for (int b; (b = preSplit()) > 0;)
6684 >                    (rights = new MapReduceValuesToIntTask<K,V>
6685 >                     (map, this, b, rights, transformer, r, reducer)).fork();
6686 >                V v;
6687 >                while ((v = advance()) != null)
6688 >                    r = reducer.apply(r, transformer.apply(v));
6689 >                result = r;
6690 >                CountedCompleter<?> c;
6691 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
6692 >                    MapReduceValuesToIntTask<K,V>
6693 >                        t = (MapReduceValuesToIntTask<K,V>)c,
6694 >                        s = t.rights;
6695 >                    while (s != null) {
6696 >                        t.result = reducer.apply(t.result, s.result);
6697 >                        s = t.rights = s.nextRight;
6698 >                    }
6699                  }
6559                else if (p.casPending(c, 0))
6560                    break;
6700              }
6701          }
6563        public final Integer getRawResult() { return result; }
6702      }
6703  
6704 <    static final class MapReduceEntriesToIntTask<K,V>
6705 <        extends BulkTask<K,V,Integer> {
6704 >    @SuppressWarnings("serial") static final class MapReduceEntriesToIntTask<K,V>
6705 >        extends Traverser<K,V,Integer> {
6706          final ObjectToInt<Map.Entry<K,V>> transformer;
6707          final IntByIntToInt reducer;
6708          final int basis;
6709          int result;
6710 <        MapReduceEntriesToIntTask<K,V> sibling;
6573 <        MapReduceEntriesToIntTask
6574 <            (ConcurrentHashMapV8<K,V> m,
6575 <             ObjectToInt<Map.Entry<K,V>> transformer,
6576 <             int basis,
6577 <             IntByIntToInt reducer) {
6578 <            super(m);
6579 <            this.transformer = transformer;
6580 <            this.basis = basis; this.reducer = reducer;
6581 <        }
6710 >        MapReduceEntriesToIntTask<K,V> rights, nextRight;
6711          MapReduceEntriesToIntTask
6712 <            (BulkTask<K,V,?> p, int b, boolean split,
6712 >            (ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b,
6713 >             MapReduceEntriesToIntTask<K,V> nextRight,
6714               ObjectToInt<Map.Entry<K,V>> transformer,
6715               int basis,
6716               IntByIntToInt reducer) {
6717 <            super(p, b, split);
6717 >            super(m, p, b); this.nextRight = nextRight;
6718              this.transformer = transformer;
6719              this.basis = basis; this.reducer = reducer;
6720          }
6721 <        public final void compute() {
6722 <            MapReduceEntriesToIntTask<K,V> t = this;
6723 <            final ObjectToInt<Map.Entry<K,V>> transformer =
6724 <                this.transformer;
6725 <            final IntByIntToInt reducer = this.reducer;
6726 <            if (transformer == null || reducer == null)
6727 <                throw new Error(NullFunctionMessage);
6728 <            final int id = this.basis;
6729 <            int b = batch();
6730 <            while (b > 1 && t.baseIndex != t.baseLimit) {
6731 <                b >>>= 1;
6732 <                t.pending = 1;
6733 <                MapReduceEntriesToIntTask<K,V> rt =
6734 <                    new MapReduceEntriesToIntTask<K,V>
6735 <                    (t, b, true, transformer, id, reducer);
6736 <                t = new MapReduceEntriesToIntTask<K,V>
6737 <                    (t, b, false, transformer, id, reducer);
6738 <                t.sibling = rt;
6739 <                rt.sibling = t;
6740 <                rt.fork();
6741 <            }
6742 <            int r = id;
6743 <            Object v;
6744 <            while ((v = t.advance()) != null)
6615 <                r = reducer.apply(r, transformer.apply(entryFor((K)t.nextKey, (V)v)));
6616 <            t.result = r;
6617 <            for (;;) {
6618 <                int c; BulkTask<K,V,?> par; MapReduceEntriesToIntTask<K,V> s, p;
6619 <                if ((par = t.parent) == null ||
6620 <                    !(par instanceof MapReduceEntriesToIntTask)) {
6621 <                    t.quietlyComplete();
6622 <                    break;
6623 <                }
6624 <                else if ((c = (p = (MapReduceEntriesToIntTask<K,V>)par).pending) == 0) {
6625 <                    if ((s = t.sibling) != null)
6626 <                        r = reducer.apply(r, s.result);
6627 <                    (t = p).result = r;
6721 >        public final Integer getRawResult() { return result; }
6722 >        @SuppressWarnings("unchecked") public final void compute() {
6723 >            final ObjectToInt<Map.Entry<K,V>> transformer;
6724 >            final IntByIntToInt reducer;
6725 >            if ((transformer = this.transformer) != null &&
6726 >                (reducer = this.reducer) != null) {
6727 >                int r = this.basis;
6728 >                for (int b; (b = preSplit()) > 0;)
6729 >                    (rights = new MapReduceEntriesToIntTask<K,V>
6730 >                     (map, this, b, rights, transformer, r, reducer)).fork();
6731 >                V v;
6732 >                while ((v = advance()) != null)
6733 >                    r = reducer.apply(r, transformer.apply(entryFor((K)nextKey,
6734 >                                                                    v)));
6735 >                result = r;
6736 >                CountedCompleter<?> c;
6737 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
6738 >                    MapReduceEntriesToIntTask<K,V>
6739 >                        t = (MapReduceEntriesToIntTask<K,V>)c,
6740 >                        s = t.rights;
6741 >                    while (s != null) {
6742 >                        t.result = reducer.apply(t.result, s.result);
6743 >                        s = t.rights = s.nextRight;
6744 >                    }
6745                  }
6629                else if (p.casPending(c, 0))
6630                    break;
6746              }
6747          }
6633        public final Integer getRawResult() { return result; }
6748      }
6749  
6750 <    static final class MapReduceMappingsToIntTask<K,V>
6751 <        extends BulkTask<K,V,Integer> {
6750 >    @SuppressWarnings("serial") static final class MapReduceMappingsToIntTask<K,V>
6751 >        extends Traverser<K,V,Integer> {
6752          final ObjectByObjectToInt<? super K, ? super V> transformer;
6753          final IntByIntToInt reducer;
6754          final int basis;
6755          int result;
6756 <        MapReduceMappingsToIntTask<K,V> sibling;
6756 >        MapReduceMappingsToIntTask<K,V> rights, nextRight;
6757          MapReduceMappingsToIntTask
6758 <            (ConcurrentHashMapV8<K,V> m,
6758 >            (ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b,
6759 >             MapReduceMappingsToIntTask<K,V> nextRight,
6760               ObjectByObjectToInt<? super K, ? super V> transformer,
6761               int basis,
6762               IntByIntToInt reducer) {
6763 <            super(m);
6763 >            super(m, p, b); this.nextRight = nextRight;
6764              this.transformer = transformer;
6765              this.basis = basis; this.reducer = reducer;
6766          }
6767 <        MapReduceMappingsToIntTask
6768 <            (BulkTask<K,V,?> p, int b, boolean split,
6769 <             ObjectByObjectToInt<? super K, ? super V> transformer,
6770 <             int basis,
6771 <             IntByIntToInt reducer) {
6772 <            super(p, b, split);
6773 <            this.transformer = transformer;
6774 <            this.basis = basis; this.reducer = reducer;
6775 <        }
6776 <        public final void compute() {
6777 <            MapReduceMappingsToIntTask<K,V> t = this;
6778 <            final ObjectByObjectToInt<? super K, ? super V> transformer =
6779 <                this.transformer;
6780 <            final IntByIntToInt reducer = this.reducer;
6781 <            if (transformer == null || reducer == null)
6782 <                throw new Error(NullFunctionMessage);
6783 <            final int id = this.basis;
6784 <            int b = batch();
6785 <            while (b > 1 && t.baseIndex != t.baseLimit) {
6786 <                b >>>= 1;
6787 <                t.pending = 1;
6788 <                MapReduceMappingsToIntTask<K,V> rt =
6789 <                    new MapReduceMappingsToIntTask<K,V>
6675 <                    (t, b, true, transformer, id, reducer);
6676 <                t = new MapReduceMappingsToIntTask<K,V>
6677 <                    (t, b, false, transformer, id, reducer);
6678 <                t.sibling = rt;
6679 <                rt.sibling = t;
6680 <                rt.fork();
6681 <            }
6682 <            int r = id;
6683 <            Object v;
6684 <            while ((v = t.advance()) != null)
6685 <                r = reducer.apply(r, transformer.apply((K)t.nextKey, (V)v));
6686 <            t.result = r;
6687 <            for (;;) {
6688 <                int c; BulkTask<K,V,?> par; MapReduceMappingsToIntTask<K,V> s, p;
6689 <                if ((par = t.parent) == null ||
6690 <                    !(par instanceof MapReduceMappingsToIntTask)) {
6691 <                    t.quietlyComplete();
6692 <                    break;
6693 <                }
6694 <                else if ((c = (p = (MapReduceMappingsToIntTask<K,V>)par).pending) == 0) {
6695 <                    if ((s = t.sibling) != null)
6696 <                        r = reducer.apply(r, s.result);
6697 <                    (t = p).result = r;
6767 >        public final Integer getRawResult() { return result; }
6768 >        @SuppressWarnings("unchecked") public final void compute() {
6769 >            final ObjectByObjectToInt<? super K, ? super V> transformer;
6770 >            final IntByIntToInt reducer;
6771 >            if ((transformer = this.transformer) != null &&
6772 >                (reducer = this.reducer) != null) {
6773 >                int r = this.basis;
6774 >                for (int b; (b = preSplit()) > 0;)
6775 >                    (rights = new MapReduceMappingsToIntTask<K,V>
6776 >                     (map, this, b, rights, transformer, r, reducer)).fork();
6777 >                V v;
6778 >                while ((v = advance()) != null)
6779 >                    r = reducer.apply(r, transformer.apply((K)nextKey, v));
6780 >                result = r;
6781 >                CountedCompleter<?> c;
6782 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
6783 >                    MapReduceMappingsToIntTask<K,V>
6784 >                        t = (MapReduceMappingsToIntTask<K,V>)c,
6785 >                        s = t.rights;
6786 >                    while (s != null) {
6787 >                        t.result = reducer.apply(t.result, s.result);
6788 >                        s = t.rights = s.nextRight;
6789 >                    }
6790                  }
6699                else if (p.casPending(c, 0))
6700                    break;
6791              }
6792          }
6703        public final Integer getRawResult() { return result; }
6793      }
6794  
6706
6795      // Unsafe mechanics
6796 <    private static final sun.misc.Unsafe UNSAFE;
6797 <    private static final long counterOffset;
6798 <    private static final long sizeCtlOffset;
6796 >    private static final sun.misc.Unsafe U;
6797 >    private static final long SIZECTL;
6798 >    private static final long TRANSFERINDEX;
6799 >    private static final long TRANSFERORIGIN;
6800 >    private static final long BASECOUNT;
6801 >    private static final long COUNTERBUSY;
6802 >    private static final long CELLVALUE;
6803      private static final long ABASE;
6804      private static final int ASHIFT;
6805  
6806      static {
6807          int ss;
6808          try {
6809 <            UNSAFE = getUnsafe();
6809 >            U = getUnsafe();
6810              Class<?> k = ConcurrentHashMapV8.class;
6811 <            counterOffset = UNSAFE.objectFieldOffset
6720 <                (k.getDeclaredField("counter"));
6721 <            sizeCtlOffset = UNSAFE.objectFieldOffset
6811 >            SIZECTL = U.objectFieldOffset
6812                  (k.getDeclaredField("sizeCtl"));
6813 +            TRANSFERINDEX = U.objectFieldOffset
6814 +                (k.getDeclaredField("transferIndex"));
6815 +            TRANSFERORIGIN = U.objectFieldOffset
6816 +                (k.getDeclaredField("transferOrigin"));
6817 +            BASECOUNT = U.objectFieldOffset
6818 +                (k.getDeclaredField("baseCount"));
6819 +            COUNTERBUSY = U.objectFieldOffset
6820 +                (k.getDeclaredField("counterBusy"));
6821 +            Class<?> ck = CounterCell.class;
6822 +            CELLVALUE = U.objectFieldOffset
6823 +                (ck.getDeclaredField("value"));
6824              Class<?> sc = Node[].class;
6825 <            ABASE = UNSAFE.arrayBaseOffset(sc);
6826 <            ss = UNSAFE.arrayIndexScale(sc);
6825 >            ABASE = U.arrayBaseOffset(sc);
6826 >            ss = U.arrayIndexScale(sc);
6827 >            ASHIFT = 31 - Integer.numberOfLeadingZeros(ss);
6828          } catch (Exception e) {
6829              throw new Error(e);
6830          }
6831          if ((ss & (ss-1)) != 0)
6832              throw new Error("data type scale not a power of two");
6731        ASHIFT = 31 - Integer.numberOfLeadingZeros(ss);
6833      }
6834  
6835      /**
# Line 6741 | Line 6842 | public class ConcurrentHashMapV8<K, V>
6842      private static sun.misc.Unsafe getUnsafe() {
6843          try {
6844              return sun.misc.Unsafe.getUnsafe();
6845 <        } catch (SecurityException se) {
6846 <            try {
6847 <                return java.security.AccessController.doPrivileged
6848 <                    (new java.security
6849 <                     .PrivilegedExceptionAction<sun.misc.Unsafe>() {
6850 <                        public sun.misc.Unsafe run() throws Exception {
6851 <                            java.lang.reflect.Field f = sun.misc
6852 <                                .Unsafe.class.getDeclaredField("theUnsafe");
6853 <                            f.setAccessible(true);
6854 <                            return (sun.misc.Unsafe) f.get(null);
6855 <                        }});
6856 <            } catch (java.security.PrivilegedActionException e) {
6857 <                throw new RuntimeException("Could not initialize intrinsics",
6858 <                                           e.getCause());
6859 <            }
6845 >        } catch (SecurityException tryReflectionInstead) {}
6846 >        try {
6847 >            return java.security.AccessController.doPrivileged
6848 >            (new java.security.PrivilegedExceptionAction<sun.misc.Unsafe>() {
6849 >                public sun.misc.Unsafe run() throws Exception {
6850 >                    Class<sun.misc.Unsafe> k = sun.misc.Unsafe.class;
6851 >                    for (java.lang.reflect.Field f : k.getDeclaredFields()) {
6852 >                        f.setAccessible(true);
6853 >                        Object x = f.get(null);
6854 >                        if (k.isInstance(x))
6855 >                            return k.cast(x);
6856 >                    }
6857 >                    throw new NoSuchFieldError("the Unsafe");
6858 >                }});
6859 >        } catch (java.security.PrivilegedActionException e) {
6860 >            throw new RuntimeException("Could not initialize intrinsics",
6861 >                                       e.getCause());
6862          }
6863      }
6864   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines