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.58 by jsr166, Tue Aug 14 05:55:08 2012 UTC vs.
Revision 1.90 by dl, Tue Jan 22 20:17:55 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 715 | Line 817 | public class ConcurrentHashMapV8<K, V>
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);
1787 >                        if (len != 0) {
1788 >                            if (len > 1) {
1789 >                                addCount(delta, len);
1790                                  delta = 0L;
1868                                checkForResize();
1791                              }
1792                              break;
1793                          }
# Line 1873 | Line 1795 | public class ConcurrentHashMapV8<K, V>
1795                  }
1796              }
1797          } finally {
1798 <            if (delta != 0)
1799 <                counter.add(delta);
1798 >            if (delta != 0L)
1799 >                addCount(delta, 2);
1800          }
1801          if (npe)
1802              throw new NullPointerException();
1803      }
1804  
1805 +    /**
1806 +     * Implementation for clear. Steps through each bin, removing all
1807 +     * nodes.
1808 +     */
1809 +    @SuppressWarnings("unchecked") private final void internalClear() {
1810 +        long delta = 0L; // negative number of deletions
1811 +        int i = 0;
1812 +        Node<V>[] tab = table;
1813 +        while (tab != null && i < tab.length) {
1814 +            Node<V> f = tabAt(tab, i);
1815 +            if (f == null)
1816 +                ++i;
1817 +            else if (f.hash < 0) {
1818 +                Object fk;
1819 +                if ((fk = f.key) instanceof TreeBin) {
1820 +                    TreeBin<V> t = (TreeBin<V>)fk;
1821 +                    t.acquire(0);
1822 +                    try {
1823 +                        if (tabAt(tab, i) == f) {
1824 +                            for (Node<V> p = t.first; p != null; p = p.next) {
1825 +                                if (p.val != null) { // (currently always true)
1826 +                                    p.val = null;
1827 +                                    --delta;
1828 +                                }
1829 +                            }
1830 +                            t.first = null;
1831 +                            t.root = null;
1832 +                            ++i;
1833 +                        }
1834 +                    } finally {
1835 +                        t.release(0);
1836 +                    }
1837 +                }
1838 +                else
1839 +                    tab = (Node<V>[])fk;
1840 +            }
1841 +            else {
1842 +                synchronized (f) {
1843 +                    if (tabAt(tab, i) == f) {
1844 +                        for (Node<V> e = f; e != null; e = e.next) {
1845 +                            if (e.val != null) {  // (currently always true)
1846 +                                e.val = null;
1847 +                                --delta;
1848 +                            }
1849 +                        }
1850 +                        setTabAt(tab, i, null);
1851 +                        ++i;
1852 +                    }
1853 +                }
1854 +            }
1855 +        }
1856 +        if (delta != 0L)
1857 +            addCount(delta, -1);
1858 +    }
1859 +
1860      /* ---------------- Table Initialization and Resizing -------------- */
1861  
1862      /**
# Line 1899 | Line 1876 | public class ConcurrentHashMapV8<K, V>
1876      /**
1877       * Initializes table, using the size recorded in sizeCtl.
1878       */
1879 <    private final Node[] initTable() {
1880 <        Node[] tab; int sc;
1879 >    @SuppressWarnings("unchecked") private final Node<V>[] initTable() {
1880 >        Node<V>[] tab; int sc;
1881          while ((tab = table) == null) {
1882              if ((sc = sizeCtl) < 0)
1883                  Thread.yield(); // lost initialization race; just spin
1884 <            else if (UNSAFE.compareAndSwapInt(this, sizeCtlOffset, sc, -1)) {
1884 >            else if (U.compareAndSwapInt(this, SIZECTL, sc, -1)) {
1885                  try {
1886                      if ((tab = table) == null) {
1887                          int n = (sc > 0) ? sc : DEFAULT_CAPACITY;
1888 <                        tab = table = new Node[n];
1888 >                        @SuppressWarnings("rawtypes") Node[] tb = new Node[n];
1889 >                        table = tab = (Node<V>[])tb;
1890                          sc = n - (n >>> 2);
1891                      }
1892                  } finally {
# Line 1921 | Line 1899 | public class ConcurrentHashMapV8<K, V>
1899      }
1900  
1901      /**
1902 <     * If table is too small and not already resizing, creates next
1903 <     * table and transfers bins.  Rechecks occupancy after a transfer
1904 <     * to see if another resize is already needed because resizings
1905 <     * are lagging additions.
1906 <     */
1907 <    private final void checkForResize() {
1908 <        Node[] tab; int n, sc;
1909 <        while ((tab = table) != null &&
1910 <               (n = tab.length) < MAXIMUM_CAPACITY &&
1911 <               (sc = sizeCtl) >= 0 && counter.sum() >= (long)sc &&
1912 <               UNSAFE.compareAndSwapInt(this, sizeCtlOffset, sc, -1)) {
1913 <            try {
1914 <                if (tab == table) {
1915 <                    table = rebuild(tab);
1916 <                    sc = (n << 1) - (n >>> 1);
1902 >     * Adds to count, and if table is too small and not already
1903 >     * resizing, initiates transfer. If already resizing, helps
1904 >     * perform transfer if work is available.  Rechecks occupancy
1905 >     * after a transfer to see if another resize is already needed
1906 >     * because resizings are lagging additions.
1907 >     *
1908 >     * @param x the count to add
1909 >     * @param check if <0, don't check resize, if <= 1 only check if uncontended
1910 >     */
1911 >    private final void addCount(long x, int check) {
1912 >        CounterCell[] as; long b, s;
1913 >        if ((as = counterCells) != null ||
1914 >            !U.compareAndSwapLong(this, BASECOUNT, b = baseCount, s = b + x)) {
1915 >            CounterHashCode hc; CounterCell a; long v; int m;
1916 >            boolean uncontended = true;
1917 >            if ((hc = threadCounterHashCode.get()) == null ||
1918 >                as == null || (m = as.length - 1) < 0 ||
1919 >                (a = as[m & hc.code]) == null ||
1920 >                !(uncontended =
1921 >                  U.compareAndSwapLong(a, CELLVALUE, v = a.value, v + x))) {
1922 >                fullAddCount(x, hc, uncontended);
1923 >                return;
1924 >            }
1925 >            if (check <= 1)
1926 >                return;
1927 >            s = sumCount();
1928 >        }
1929 >        if (check >= 0) {
1930 >            Node<V>[] tab, nt; int sc;
1931 >            while (s >= (long)(sc = sizeCtl) && (tab = table) != null &&
1932 >                   tab.length < MAXIMUM_CAPACITY) {
1933 >                if (sc < 0) {
1934 >                    if (sc == -1 || transferIndex <= transferOrigin ||
1935 >                        (nt = nextTable) == null)
1936 >                        break;
1937 >                    if (U.compareAndSwapInt(this, SIZECTL, sc, sc - 1))
1938 >                        transfer(tab, nt);
1939                  }
1940 <            } finally {
1941 <                sizeCtl = sc;
1940 >                else if (U.compareAndSwapInt(this, SIZECTL, sc, -2))
1941 >                    transfer(tab, null);
1942 >                s = sumCount();
1943              }
1944          }
1945      }
# Line 1948 | Line 1949 | public class ConcurrentHashMapV8<K, V>
1949       *
1950       * @param size number of elements (doesn't need to be perfectly accurate)
1951       */
1952 <    private final void tryPresize(int size) {
1952 >    @SuppressWarnings("unchecked") private final void tryPresize(int size) {
1953          int c = (size >= (MAXIMUM_CAPACITY >>> 1)) ? MAXIMUM_CAPACITY :
1954              tableSizeFor(size + (size >>> 1) + 1);
1955          int sc;
1956          while ((sc = sizeCtl) >= 0) {
1957 <            Node[] tab = table; int n;
1957 >            Node<V>[] tab = table; int n;
1958              if (tab == null || (n = tab.length) == 0) {
1959                  n = (sc > c) ? sc : c;
1960 <                if (UNSAFE.compareAndSwapInt(this, sizeCtlOffset, sc, -1)) {
1960 >                if (U.compareAndSwapInt(this, SIZECTL, sc, -1)) {
1961                      try {
1962                          if (table == tab) {
1963 <                            table = new Node[n];
1963 >                            @SuppressWarnings("rawtypes") Node[] tb = new Node[n];
1964 >                            table = (Node<V>[])tb;
1965                              sc = n - (n >>> 2);
1966                          }
1967                      } finally {
# Line 1969 | Line 1971 | public class ConcurrentHashMapV8<K, V>
1971              }
1972              else if (c <= sc || n >= MAXIMUM_CAPACITY)
1973                  break;
1974 <            else if (UNSAFE.compareAndSwapInt(this, sizeCtlOffset, sc, -1)) {
1975 <                try {
1976 <                    if (table == tab) {
1975 <                        table = rebuild(tab);
1976 <                        sc = (n << 1) - (n >>> 1);
1977 <                    }
1978 <                } finally {
1979 <                    sizeCtl = sc;
1980 <                }
1981 <            }
1974 >            else if (tab == table &&
1975 >                     U.compareAndSwapInt(this, SIZECTL, sc, -2))
1976 >                transfer(tab, null);
1977          }
1978      }
1979  
1980      /*
1981       * Moves and/or copies the nodes in each bin to new table. See
1982       * above for explanation.
1988     *
1989     * @return the new table
1983       */
1984 <    private static final Node[] rebuild(Node[] tab) {
1985 <        int n = tab.length;
1986 <        Node[] nextTab = new Node[n << 1];
1987 <        Node fwd = new Node(MOVED, nextTab, null, null);
1988 <        int[] buffer = null;       // holds bins to revisit; null until needed
1989 <        Node rev = null;           // reverse forwarder; null until needed
1990 <        int nbuffered = 0;         // the number of bins in buffer list
1991 <        int bufferIndex = 0;       // buffer index of current buffered bin
1992 <        int bin = n - 1;           // current non-buffered bin or -1 if none
1993 <
1994 <        for (int i = bin;;) {      // start upwards sweep
1995 <            int fh; Node f;
1996 <            if ((f = tabAt(tab, i)) == null) {
1997 <                if (bin >= 0) {    // no lock needed (or available)
1998 <                    if (!casTabAt(tab, i, f, fwd))
1999 <                        continue;
2000 <                }
2001 <                else {             // transiently use a locked forwarding node
2002 <                    Node g = new Node(MOVED|LOCKED, nextTab, null, null);
2003 <                    if (!casTabAt(tab, i, f, g))
2004 <                        continue;
1984 >    @SuppressWarnings("unchecked") private final void transfer
1985 >        (Node<V>[] tab, Node<V>[] nextTab) {
1986 >        int n = tab.length, stride;
1987 >        if ((stride = (NCPU > 1) ? (n >>> 3) / NCPU : n) < MIN_TRANSFER_STRIDE)
1988 >            stride = MIN_TRANSFER_STRIDE; // subdivide range
1989 >        if (nextTab == null) {            // initiating
1990 >            try {
1991 >                @SuppressWarnings("rawtypes") Node[] tb = new Node[n << 1];
1992 >                nextTab = (Node<V>[])tb;
1993 >            } catch (Throwable ex) {      // try to cope with OOME
1994 >                sizeCtl = Integer.MAX_VALUE;
1995 >                return;
1996 >            }
1997 >            nextTable = nextTab;
1998 >            transferOrigin = n;
1999 >            transferIndex = n;
2000 >            Node<V> rev = new Node<V>(MOVED, tab, null, null);
2001 >            for (int k = n; k > 0;) {    // progressively reveal ready slots
2002 >                int nextk = (k > stride) ? k - stride : 0;
2003 >                for (int m = nextk; m < k; ++m)
2004 >                    nextTab[m] = rev;
2005 >                for (int m = n + nextk; m < n + k; ++m)
2006 >                    nextTab[m] = rev;
2007 >                U.putOrderedInt(this, TRANSFERORIGIN, k = nextk);
2008 >            }
2009 >        }
2010 >        int nextn = nextTab.length;
2011 >        Node<V> fwd = new Node<V>(MOVED, nextTab, null, null);
2012 >        boolean advance = true;
2013 >        for (int i = 0, bound = 0;;) {
2014 >            int nextIndex, nextBound; Node<V> f; Object fk;
2015 >            while (advance) {
2016 >                if (--i >= bound)
2017 >                    advance = false;
2018 >                else if ((nextIndex = transferIndex) <= transferOrigin) {
2019 >                    i = -1;
2020 >                    advance = false;
2021 >                }
2022 >                else if (U.compareAndSwapInt
2023 >                         (this, TRANSFERINDEX, nextIndex,
2024 >                          nextBound = (nextIndex > stride ?
2025 >                                       nextIndex - stride : 0))) {
2026 >                    bound = nextBound;
2027 >                    i = nextIndex - 1;
2028 >                    advance = false;
2029 >                }
2030 >            }
2031 >            if (i < 0 || i >= n || i + n >= nextn) {
2032 >                for (int sc;;) {
2033 >                    if (U.compareAndSwapInt(this, SIZECTL, sc = sizeCtl, ++sc)) {
2034 >                        if (sc == -1) {
2035 >                            nextTable = null;
2036 >                            table = nextTab;
2037 >                            sizeCtl = (n << 1) - (n >>> 1);
2038 >                        }
2039 >                        return;
2040 >                    }
2041 >                }
2042 >            }
2043 >            else if ((f = tabAt(tab, i)) == null) {
2044 >                if (casTabAt(tab, i, null, fwd)) {
2045                      setTabAt(nextTab, i, null);
2046                      setTabAt(nextTab, i + n, null);
2047 <                    setTabAt(tab, i, fwd);
2015 <                    if (!g.casHash(MOVED|LOCKED, MOVED)) {
2016 <                        g.hash = MOVED;
2017 <                        synchronized (g) { g.notifyAll(); }
2018 <                    }
2047 >                    advance = true;
2048                  }
2049              }
2050 <            else if ((fh = f.hash) == MOVED) {
2051 <                Object fk = f.key;
2052 <                if (fk instanceof TreeBin) {
2053 <                    TreeBin t = (TreeBin)fk;
2054 <                    boolean validated = false;
2055 <                    t.acquire(0);
2056 <                    try {
2057 <                        if (tabAt(tab, i) == f) {
2058 <                            validated = true;
2059 <                            splitTreeBin(nextTab, i, t);
2060 <                            setTabAt(tab, i, fwd);
2050 >            else if (f.hash >= 0) {
2051 >                synchronized (f) {
2052 >                    if (tabAt(tab, i) == f) {
2053 >                        int runBit = f.hash & n;
2054 >                        Node<V> lastRun = f, lo = null, hi = null;
2055 >                        for (Node<V> p = f.next; p != null; p = p.next) {
2056 >                            int b = p.hash & n;
2057 >                            if (b != runBit) {
2058 >                                runBit = b;
2059 >                                lastRun = p;
2060 >                            }
2061                          }
2062 <                    } finally {
2063 <                        t.release(0);
2062 >                        if (runBit == 0)
2063 >                            lo = lastRun;
2064 >                        else
2065 >                            hi = lastRun;
2066 >                        for (Node<V> p = f; p != lastRun; p = p.next) {
2067 >                            int ph = p.hash;
2068 >                            Object pk = p.key; V pv = p.val;
2069 >                            if ((ph & n) == 0)
2070 >                                lo = new Node<V>(ph, pk, pv, lo);
2071 >                            else
2072 >                                hi = new Node<V>(ph, pk, pv, hi);
2073 >                        }
2074 >                        setTabAt(nextTab, i, lo);
2075 >                        setTabAt(nextTab, i + n, hi);
2076 >                        setTabAt(tab, i, fwd);
2077 >                        advance = true;
2078                      }
2036                    if (!validated)
2037                        continue;
2079                  }
2080              }
2081 <            else if ((fh & LOCKED) == 0 && f.casHash(fh, fh|LOCKED)) {
2082 <                boolean validated = false;
2083 <                try {              // split to lo and hi lists; copying as needed
2081 >            else if ((fk = f.key) instanceof TreeBin) {
2082 >                TreeBin<V> t = (TreeBin<V>)fk;
2083 >                t.acquire(0);
2084 >                try {
2085                      if (tabAt(tab, i) == f) {
2086 <                        validated = true;
2087 <                        splitBin(nextTab, i, f);
2086 >                        TreeBin<V> lt = new TreeBin<V>();
2087 >                        TreeBin<V> ht = new TreeBin<V>();
2088 >                        int lc = 0, hc = 0;
2089 >                        for (Node<V> e = t.first; e != null; e = e.next) {
2090 >                            int h = e.hash;
2091 >                            Object k = e.key; V v = e.val;
2092 >                            if ((h & n) == 0) {
2093 >                                ++lc;
2094 >                                lt.putTreeNode(h, k, v);
2095 >                            }
2096 >                            else {
2097 >                                ++hc;
2098 >                                ht.putTreeNode(h, k, v);
2099 >                            }
2100 >                        }
2101 >                        Node<V> ln, hn; // throw away trees if too small
2102 >                        if (lc < TREE_THRESHOLD) {
2103 >                            ln = null;
2104 >                            for (Node<V> p = lt.first; p != null; p = p.next)
2105 >                                ln = new Node<V>(p.hash, p.key, p.val, ln);
2106 >                        }
2107 >                        else
2108 >                            ln = new Node<V>(MOVED, lt, null, null);
2109 >                        setTabAt(nextTab, i, ln);
2110 >                        if (hc < TREE_THRESHOLD) {
2111 >                            hn = null;
2112 >                            for (Node<V> p = ht.first; p != null; p = p.next)
2113 >                                hn = new Node<V>(p.hash, p.key, p.val, hn);
2114 >                        }
2115 >                        else
2116 >                            hn = new Node<V>(MOVED, ht, null, null);
2117 >                        setTabAt(nextTab, i + n, hn);
2118                          setTabAt(tab, i, fwd);
2119 +                        advance = true;
2120                      }
2121                  } finally {
2122 <                    if (!f.casHash(fh | LOCKED, fh)) {
2050 <                        f.hash = fh;
2051 <                        synchronized (f) { f.notifyAll(); };
2052 <                    }
2122 >                    t.release(0);
2123                  }
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];
2124              }
2125              else
2126 <                return nextTab;
2126 >                advance = true; // already processed
2127          }
2128      }
2129  
2130 <    /**
2131 <     * Splits a normal bin with list headed by e into lo and hi parts;
2132 <     * installs in given table.
2133 <     */
2134 <    private static void splitBin(Node[] nextTab, int i, Node e) {
2135 <        int bit = nextTab.length >>> 1; // bit to split on
2136 <        int runBit = e.hash & bit;
2137 <        Node lastRun = e, lo = null, hi = null;
2138 <        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;
2130 >    /* ---------------- Counter support -------------- */
2131 >
2132 >    final long sumCount() {
2133 >        CounterCell[] as = counterCells; CounterCell a;
2134 >        long sum = baseCount;
2135 >        if (as != null) {
2136 >            for (int i = 0; i < as.length; ++i) {
2137 >                if ((a = as[i]) != null)
2138 >                    sum += a.value;
2139              }
2140          }
2141 <        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);
2141 >        return sum;
2142      }
2143  
2144 <    /**
2145 <     * Splits a tree bin into lo and hi parts; installs in given table.
2146 <     */
2147 <    private static void splitTreeBin(Node[] nextTab, int i, TreeBin t) {
2148 <        int bit = nextTab.length >>> 1;
2149 <        TreeBin lt = new TreeBin();
2150 <        TreeBin ht = new TreeBin();
2151 <        int lc = 0, hc = 0;
2152 <        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);
2146 <        }
2147 <        else
2148 <            ln = new Node(MOVED, lt, null, null);
2149 <        setTabAt(nextTab, i, ln);
2150 <        if (hc <= (TREE_THRESHOLD >>> 1)) {
2151 <            hn = null;
2152 <            for (Node p = ht.first; p != null; p = p.next)
2153 <                hn = new Node(p.hash, p.key, p.val, hn);
2144 >    // See LongAdder version for explanation
2145 >    private final void fullAddCount(long x, CounterHashCode hc,
2146 >                                    boolean wasUncontended) {
2147 >        int h;
2148 >        if (hc == null) {
2149 >            hc = new CounterHashCode();
2150 >            int s = counterHashCodeGenerator.addAndGet(SEED_INCREMENT);
2151 >            h = hc.code = (s == 0) ? 1 : s; // Avoid zero
2152 >            threadCounterHashCode.set(hc);
2153          }
2154          else
2155 <            hn = new Node(MOVED, ht, null, null);
2156 <        setTabAt(nextTab, i + bit, hn);
2157 <    }
2158 <
2159 <    /**
2160 <     * Implementation for clear. Steps through each bin, removing all
2161 <     * nodes.
2162 <     */
2163 <    private final void internalClear() {
2164 <        long delta = 0L; // negative number of deletions
2165 <        int i = 0;
2166 <        Node[] tab = table;
2167 <        while (tab != null && i < tab.length) {
2168 <            int fh; Object fk;
2169 <            Node f = tabAt(tab, i);
2170 <            if (f == null)
2171 <                ++i;
2172 <            else if ((fh = f.hash) == MOVED) {
2173 <                if ((fk = f.key) instanceof TreeBin) {
2174 <                    TreeBin t = (TreeBin)fk;
2175 <                    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;
2155 >            h = hc.code;
2156 >        boolean collide = false;                // True if last slot nonempty
2157 >        for (;;) {
2158 >            CounterCell[] as; CounterCell a; int n; long v;
2159 >            if ((as = counterCells) != null && (n = as.length) > 0) {
2160 >                if ((a = as[(n - 1) & h]) == null) {
2161 >                    if (counterBusy == 0) {            // Try to attach new Cell
2162 >                        CounterCell r = new CounterCell(x); // Optimistic create
2163 >                        if (counterBusy == 0 &&
2164 >                            U.compareAndSwapInt(this, COUNTERBUSY, 0, 1)) {
2165 >                            boolean created = false;
2166 >                            try {               // Recheck under lock
2167 >                                CounterCell[] rs; int m, j;
2168 >                                if ((rs = counterCells) != null &&
2169 >                                    (m = rs.length) > 0 &&
2170 >                                    rs[j = (m - 1) & h] == null) {
2171 >                                    rs[j] = r;
2172 >                                    created = true;
2173 >                                }
2174 >                            } finally {
2175 >                                counterBusy = 0;
2176                              }
2177 <                            t.first = null;
2178 <                            t.root = null;
2179 <                            ++i;
2177 >                            if (created)
2178 >                                break;
2179 >                            continue;           // Slot is now non-empty
2180                          }
2187                    } finally {
2188                        t.release(0);
2181                      }
2182 +                    collide = false;
2183                  }
2184 <                else
2185 <                    tab = (Node[])fk;
2186 <            }
2187 <            else if ((fh & LOCKED) != 0) {
2188 <                counter.add(delta); // opportunistically update count
2189 <                delta = 0L;
2190 <                f.tryAwaitLock(tab, i);
2191 <            }
2192 <            else if (f.casHash(fh, fh | LOCKED)) {
2193 <                try {
2194 <                    if (tabAt(tab, i) == f) {
2195 <                        for (Node e = f; e != null; e = e.next) {
2196 <                            e.val = null;
2197 <                            --delta;
2184 >                else if (!wasUncontended)       // CAS already known to fail
2185 >                    wasUncontended = true;      // Continue after rehash
2186 >                else if (U.compareAndSwapLong(a, CELLVALUE, v = a.value, v + x))
2187 >                    break;
2188 >                else if (counterCells != as || n >= NCPU)
2189 >                    collide = false;            // At max size or stale
2190 >                else if (!collide)
2191 >                    collide = true;
2192 >                else if (counterBusy == 0 &&
2193 >                         U.compareAndSwapInt(this, COUNTERBUSY, 0, 1)) {
2194 >                    try {
2195 >                        if (counterCells == as) {// Expand table unless stale
2196 >                            CounterCell[] rs = new CounterCell[n << 1];
2197 >                            for (int i = 0; i < n; ++i)
2198 >                                rs[i] = as[i];
2199 >                            counterCells = rs;
2200                          }
2201 <                        setTabAt(tab, i, null);
2202 <                        ++i;
2201 >                    } finally {
2202 >                        counterBusy = 0;
2203                      }
2204 <                } finally {
2205 <                    if (!f.casHash(fh | LOCKED, fh)) {
2206 <                        f.hash = fh;
2207 <                        synchronized (f) { f.notifyAll(); };
2204 >                    collide = false;
2205 >                    continue;                   // Retry with expanded table
2206 >                }
2207 >                h ^= h << 13;                   // Rehash
2208 >                h ^= h >>> 17;
2209 >                h ^= h << 5;
2210 >            }
2211 >            else if (counterBusy == 0 && counterCells == as &&
2212 >                     U.compareAndSwapInt(this, COUNTERBUSY, 0, 1)) {
2213 >                boolean init = false;
2214 >                try {                           // Initialize table
2215 >                    if (counterCells == as) {
2216 >                        CounterCell[] rs = new CounterCell[2];
2217 >                        rs[h & 1] = new CounterCell(x);
2218 >                        counterCells = rs;
2219 >                        init = true;
2220                      }
2221 +                } finally {
2222 +                    counterBusy = 0;
2223                  }
2224 +                if (init)
2225 +                    break;
2226              }
2227 +            else if (U.compareAndSwapLong(this, BASECOUNT, v = baseCount, v + x))
2228 +                break;                          // Fall back on using base
2229          }
2230 <        if (delta != 0)
2218 <            counter.add(delta);
2230 >        hc.code = h;                            // Record index for next time
2231      }
2232  
2233      /* ----------------Table Traversal -------------- */
2234  
2235      /**
2236       * Encapsulates traversal for methods such as containsValue; also
2237 <     * serves as a base class for other iterators.
2237 >     * serves as a base class for other iterators and bulk tasks.
2238       *
2239       * At each step, the iterator snapshots the key ("nextKey") and
2240       * value ("nextVal") of a valid node (i.e., one that, at point of
# Line 2230 | Line 2242 | public class ConcurrentHashMapV8<K, V>
2242       * change (including to null, indicating deletion), field nextVal
2243       * might not be accurate at point of use, but still maintains the
2244       * weak consistency property of holding a value that was once
2245 <     * valid.
2245 >     * valid. To support iterator.remove, the nextKey field is not
2246 >     * updated (nulled out) when the iterator cannot advance.
2247       *
2248       * Internal traversals directly access these fields, as in:
2249       * {@code while (it.advance() != null) { process(it.nextKey); }}
# Line 2257 | Line 2270 | public class ConcurrentHashMapV8<K, V>
2270       * across threads, iteration terminates if a bounds checks fails
2271       * for a table read.
2272       *
2273 <     * This class extends ForkJoinTask to streamline parallel
2274 <     * iteration in bulk operations (see BulkTask). This adds only an
2275 <     * int of space overhead, which is close enough to negligible in
2276 <     * cases where it is not needed to not worry about it.
2273 >     * This class extends CountedCompleter to streamline parallel
2274 >     * iteration in bulk operations. This adds only a few fields of
2275 >     * space overhead, which is small enough in cases where it is not
2276 >     * needed to not worry about it.  Because CountedCompleter is
2277 >     * Serializable, but iterators need not be, we need to add warning
2278 >     * suppressions.
2279       */
2280 <    static class Traverser<K,V,R> extends ForkJoinTask<R> {
2280 >    @SuppressWarnings("serial") static class Traverser<K,V,R>
2281 >        extends CountedCompleter<R> {
2282          final ConcurrentHashMapV8<K, V> map;
2283 <        Node next;           // the next entry to use
2268 <        Node last;           // the last entry used
2283 >        Node<V> next;        // the next entry to use
2284          Object nextKey;      // cached key field of next
2285 <        Object nextVal;      // cached val field of next
2286 <        Node[] tab;          // current table; updated if resized
2285 >        V nextVal;           // cached val field of next
2286 >        Node<V>[] tab;       // current table; updated if resized
2287          int index;           // index of bin to use next
2288          int baseIndex;       // current index of initial table
2289          int baseLimit;       // index bound for initial table
2290 <        final int baseSize;  // initial table size
2290 >        int baseSize;        // initial table size
2291 >        int batch;           // split control
2292  
2293          /** Creates iterator for all entries in the table. */
2294          Traverser(ConcurrentHashMapV8<K, V> map) {
2295 <            this.tab = (this.map = map).table;
2280 <            baseLimit = baseSize = (tab == null) ? 0 : tab.length;
2295 >            this.map = map;
2296          }
2297  
2298 <        /** Creates iterator for split() methods */
2299 <        Traverser(Traverser<K,V,?> it, boolean split) {
2300 <            this.map = it.map;
2301 <            this.tab = it.tab;
2302 <            this.baseSize = it.baseSize;
2303 <            int lo = it.baseIndex;
2304 <            int hi = this.baseLimit = it.baseLimit;
2305 <            int i;
2306 <            if (split) // adjust parent
2307 <                i = it.baseLimit = (lo + hi + 1) >>> 1;
2308 <            else       // clone parent
2309 <                i = lo;
2310 <            this.index = this.baseIndex = i;
2298 >        /** Creates iterator for split() methods and task constructors */
2299 >        Traverser(ConcurrentHashMapV8<K,V> map, Traverser<K,V,?> it, int batch) {
2300 >            super(it);
2301 >            this.batch = batch;
2302 >            if ((this.map = map) != null && it != null) { // split parent
2303 >                Node<V>[] t;
2304 >                if ((t = it.tab) == null &&
2305 >                    (t = it.tab = map.table) != null)
2306 >                    it.baseLimit = it.baseSize = t.length;
2307 >                this.tab = t;
2308 >                this.baseSize = it.baseSize;
2309 >                int hi = this.baseLimit = it.baseLimit;
2310 >                it.baseLimit = this.index = this.baseIndex =
2311 >                    (hi + it.baseIndex + 1) >>> 1;
2312 >            }
2313          }
2314  
2315          /**
2316           * Advances next; returns nextVal or null if terminated.
2317           * See above for explanation.
2318           */
2319 <        final Object advance() {
2320 <            Node e = last = next;
2321 <            Object ev = null;
2319 >        @SuppressWarnings("unchecked") final V advance() {
2320 >            Node<V> e = next;
2321 >            V ev = null;
2322              outer: do {
2323                  if (e != null)                  // advance past used/skipped node
2324                      e = e.next;
2325                  while (e == null) {             // get to next non-null bin
2326 <                    Node[] t; int b, i, n; Object ek; // checks must use locals
2327 <                    if ((b = baseIndex) >= baseLimit || (i = index) < 0 ||
2328 <                        (t = tab) == null || i >= (n = t.length))
2326 >                    ConcurrentHashMapV8<K, V> m;
2327 >                    Node<V>[] t; int b, i, n; Object ek; //  must use locals
2328 >                    if ((t = tab) != null)
2329 >                        n = t.length;
2330 >                    else if ((m = map) != null && (t = tab = m.table) != null)
2331 >                        n = baseLimit = baseSize = t.length;
2332 >                    else
2333                          break outer;
2334 <                    else if ((e = tabAt(t, i)) != null && e.hash == MOVED) {
2334 >                    if ((b = baseIndex) >= baseLimit ||
2335 >                        (i = index) < 0 || i >= n)
2336 >                        break outer;
2337 >                    if ((e = tabAt(t, i)) != null && e.hash < 0) {
2338                          if ((ek = e.key) instanceof TreeBin)
2339 <                            e = ((TreeBin)ek).first;
2339 >                            e = ((TreeBin<V>)ek).first;
2340                          else {
2341 <                            tab = (Node[])ek;
2341 >                            tab = (Node<V>[])ek;
2342                              continue;           // restarts due to null val
2343                          }
2344                      }                           // visit upper slots if present
# Line 2327 | Line 2351 | public class ConcurrentHashMapV8<K, V>
2351          }
2352  
2353          public final void remove() {
2354 <            if (nextVal == null && last == null)
2355 <                advance();
2332 <            Node e = last;
2333 <            if (e == null)
2354 >            Object k = nextKey;
2355 >            if (k == null && (advance() == null || (k = nextKey) == null))
2356                  throw new IllegalStateException();
2357 <            last = null;
2336 <            map.remove(e.key);
2357 >            map.internalReplace(k, null, null);
2358          }
2359  
2360          public final boolean hasNext() {
# Line 2341 | Line 2362 | public class ConcurrentHashMapV8<K, V>
2362          }
2363  
2364          public final boolean hasMoreElements() { return hasNext(); }
2365 <        public final void setRawResult(Object x) { }
2366 <        public R getRawResult() { return null; }
2367 <        public boolean exec() { return true; }
2365 >
2366 >        public void compute() { } // default no-op CountedCompleter body
2367 >
2368 >        /**
2369 >         * Returns a batch value > 0 if this task should (and must) be
2370 >         * split, if so, adding to pending count, and in any case
2371 >         * updating batch value. The initial batch value is approx
2372 >         * exp2 of the number of times (minus one) to split task by
2373 >         * two before executing leaf action. This value is faster to
2374 >         * compute and more convenient to use as a guide to splitting
2375 >         * than is the depth, since it is used while dividing by two
2376 >         * anyway.
2377 >         */
2378 >        final int preSplit() {
2379 >            ConcurrentHashMapV8<K, V> m; int b; Node<V>[] t;  ForkJoinPool pool;
2380 >            if ((b = batch) < 0 && (m = map) != null) { // force initialization
2381 >                if ((t = tab) == null && (t = tab = m.table) != null)
2382 >                    baseLimit = baseSize = t.length;
2383 >                if (t != null) {
2384 >                    long n = m.sumCount();
2385 >                    int par = ((pool = getPool()) == null) ?
2386 >                        ForkJoinPool.getCommonPoolParallelism() :
2387 >                        pool.getParallelism();
2388 >                    int sp = par << 3; // slack of 8
2389 >                    b = (n <= 0L) ? 0 : (n < (long)sp) ? (int)n : sp;
2390 >                }
2391 >            }
2392 >            b = (b <= 1 || baseIndex == baseLimit) ? 0 : (b >>> 1);
2393 >            if ((batch = b) > 0)
2394 >                addToPendingCount(1);
2395 >            return b;
2396 >        }
2397 >
2398      }
2399  
2400      /* ---------------- Public operations -------------- */
# Line 2352 | Line 2403 | public class ConcurrentHashMapV8<K, V>
2403       * Creates a new, empty map with the default initial table size (16).
2404       */
2405      public ConcurrentHashMapV8() {
2355        this.counter = new LongAdder();
2406      }
2407  
2408      /**
# Line 2371 | Line 2421 | public class ConcurrentHashMapV8<K, V>
2421          int cap = ((initialCapacity >= (MAXIMUM_CAPACITY >>> 1)) ?
2422                     MAXIMUM_CAPACITY :
2423                     tableSizeFor(initialCapacity + (initialCapacity >>> 1) + 1));
2374        this.counter = new LongAdder();
2424          this.sizeCtl = cap;
2425      }
2426  
# Line 2381 | Line 2430 | public class ConcurrentHashMapV8<K, V>
2430       * @param m the map
2431       */
2432      public ConcurrentHashMapV8(Map<? extends K, ? extends V> m) {
2384        this.counter = new LongAdder();
2433          this.sizeCtl = DEFAULT_CAPACITY;
2434          internalPutAll(m);
2435      }
# Line 2432 | Line 2480 | public class ConcurrentHashMapV8<K, V>
2480          long size = (long)(1.0 + (long)initialCapacity / loadFactor);
2481          int cap = (size >= (long)MAXIMUM_CAPACITY) ?
2482              MAXIMUM_CAPACITY : tableSizeFor((int)size);
2435        this.counter = new LongAdder();
2483          this.sizeCtl = cap;
2484      }
2485  
2486      /**
2487 +     * Creates a new {@link Set} backed by a ConcurrentHashMapV8
2488 +     * from the given type to {@code Boolean.TRUE}.
2489 +     *
2490 +     * @return the new set
2491 +     */
2492 +    public static <K> KeySetView<K,Boolean> newKeySet() {
2493 +        return new KeySetView<K,Boolean>(new ConcurrentHashMapV8<K,Boolean>(),
2494 +                                      Boolean.TRUE);
2495 +    }
2496 +
2497 +    /**
2498 +     * Creates a new {@link Set} backed by a ConcurrentHashMapV8
2499 +     * from the given type to {@code Boolean.TRUE}.
2500 +     *
2501 +     * @param initialCapacity The implementation performs internal
2502 +     * sizing to accommodate this many elements.
2503 +     * @throws IllegalArgumentException if the initial capacity of
2504 +     * elements is negative
2505 +     * @return the new set
2506 +     */
2507 +    public static <K> KeySetView<K,Boolean> newKeySet(int initialCapacity) {
2508 +        return new KeySetView<K,Boolean>
2509 +            (new ConcurrentHashMapV8<K,Boolean>(initialCapacity), Boolean.TRUE);
2510 +    }
2511 +
2512 +    /**
2513       * {@inheritDoc}
2514       */
2515      public boolean isEmpty() {
2516 <        return counter.sum() <= 0L; // ignore transient negative values
2516 >        return sumCount() <= 0L; // ignore transient negative values
2517      }
2518  
2519      /**
2520       * {@inheritDoc}
2521       */
2522      public int size() {
2523 <        long n = counter.sum();
2523 >        long n = sumCount();
2524          return ((n < 0L) ? 0 :
2525                  (n > (long)Integer.MAX_VALUE) ? Integer.MAX_VALUE :
2526                  (int)n);
# Line 2455 | Line 2528 | public class ConcurrentHashMapV8<K, V>
2528  
2529      /**
2530       * Returns the number of mappings. This method should be used
2531 <     * instead of {@link #size} because a ConcurrentHashMap may
2531 >     * instead of {@link #size} because a ConcurrentHashMapV8 may
2532       * contain more mappings than can be represented as an int. The
2533 <     * value returned is a snapshot; the actual count may differ if
2534 <     * there are ongoing concurrent insertions of removals.
2533 >     * value returned is an estimate; the actual count may differ if
2534 >     * there are concurrent insertions or removals.
2535       *
2536       * @return the number of mappings
2537       */
2538      public long mappingCount() {
2539 <        long n = counter.sum();
2540 <        return (n < 0L) ? 0L : n;
2539 >        long n = sumCount();
2540 >        return (n < 0L) ? 0L : n; // ignore transient negative values
2541      }
2542  
2543      /**
# Line 2478 | Line 2551 | public class ConcurrentHashMapV8<K, V>
2551       *
2552       * @throws NullPointerException if the specified key is null
2553       */
2554 <    @SuppressWarnings("unchecked")
2555 <        public V get(Object key) {
2556 <        if (key == null)
2557 <            throw new NullPointerException();
2558 <        return (V)internalGet(key);
2554 >    public V get(Object key) {
2555 >        return internalGet(key);
2556 >    }
2557 >
2558 >    /**
2559 >     * Returns the value to which the specified key is mapped,
2560 >     * or the given defaultValue if this map contains no mapping for the key.
2561 >     *
2562 >     * @param key the key
2563 >     * @param defaultValue the value to return if this map contains
2564 >     * no mapping for the given key
2565 >     * @return the mapping for the key, if present; else the defaultValue
2566 >     * @throws NullPointerException if the specified key is null
2567 >     */
2568 >    public V getValueOrDefault(Object key, V defaultValue) {
2569 >        V v;
2570 >        return (v = internalGet(key)) == null ? defaultValue : v;
2571      }
2572  
2573      /**
# Line 2495 | Line 2580 | public class ConcurrentHashMapV8<K, V>
2580       * @throws NullPointerException if the specified key is null
2581       */
2582      public boolean containsKey(Object key) {
2498        if (key == null)
2499            throw new NullPointerException();
2583          return internalGet(key) != null;
2584      }
2585  
# Line 2513 | Line 2596 | public class ConcurrentHashMapV8<K, V>
2596      public boolean containsValue(Object value) {
2597          if (value == null)
2598              throw new NullPointerException();
2599 <        Object v;
2599 >        V v;
2600          Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
2601          while ((v = it.advance()) != null) {
2602              if (v == value || value.equals(v))
# Line 2537 | Line 2620 | public class ConcurrentHashMapV8<K, V>
2620       *         {@code false} otherwise
2621       * @throws NullPointerException if the specified value is null
2622       */
2623 <    public boolean contains(Object value) {
2623 >    @Deprecated public boolean contains(Object value) {
2624          return containsValue(value);
2625      }
2626  
# Line 2545 | Line 2628 | public class ConcurrentHashMapV8<K, V>
2628       * Maps the specified key to the specified value in this table.
2629       * Neither the key nor the value can be null.
2630       *
2631 <     * <p> The value can be retrieved by calling the {@code get} method
2631 >     * <p>The value can be retrieved by calling the {@code get} method
2632       * with a key that is equal to the original key.
2633       *
2634       * @param key key with which the specified value is to be associated
# Line 2554 | Line 2637 | public class ConcurrentHashMapV8<K, V>
2637       *         {@code null} if there was no mapping for {@code key}
2638       * @throws NullPointerException if the specified key or value is null
2639       */
2640 <    @SuppressWarnings("unchecked")
2641 <        public V put(K key, V value) {
2559 <        if (key == null || value == null)
2560 <            throw new NullPointerException();
2561 <        return (V)internalPut(key, value);
2640 >    public V put(K key, V value) {
2641 >        return internalPut(key, value, false);
2642      }
2643  
2644      /**
# Line 2568 | Line 2648 | public class ConcurrentHashMapV8<K, V>
2648       *         or {@code null} if there was no mapping for the key
2649       * @throws NullPointerException if the specified key or value is null
2650       */
2651 <    @SuppressWarnings("unchecked")
2652 <        public V putIfAbsent(K key, V value) {
2573 <        if (key == null || value == null)
2574 <            throw new NullPointerException();
2575 <        return (V)internalPutIfAbsent(key, value);
2651 >    public V putIfAbsent(K key, V value) {
2652 >        return internalPut(key, value, true);
2653      }
2654  
2655      /**
# Line 2616 | Line 2693 | public class ConcurrentHashMapV8<K, V>
2693       * @param key key with which the specified value is to be associated
2694       * @param mappingFunction the function to compute a value
2695       * @return the current (existing or computed) value associated with
2696 <     *         the specified key, or null if the computed value is null.
2696 >     *         the specified key, or null if the computed value is null
2697       * @throws NullPointerException if the specified key or mappingFunction
2698       *         is null
2699       * @throws IllegalStateException if the computation detectably
# Line 2625 | Line 2702 | public class ConcurrentHashMapV8<K, V>
2702       * @throws RuntimeException or Error if the mappingFunction does so,
2703       *         in which case the mapping is left unestablished
2704       */
2705 <    @SuppressWarnings("unchecked")
2706 <        public V computeIfAbsent(K key, Fun<? super K, ? extends V> mappingFunction) {
2707 <        if (key == null || mappingFunction == null)
2631 <            throw new NullPointerException();
2632 <        return (V)internalComputeIfAbsent(key, mappingFunction);
2705 >    public V computeIfAbsent
2706 >        (K key, Fun<? super K, ? extends V> mappingFunction) {
2707 >        return internalComputeIfAbsent(key, mappingFunction);
2708      }
2709  
2710      /**
# Line 2666 | Line 2741 | public class ConcurrentHashMapV8<K, V>
2741       * @throws RuntimeException or Error if the remappingFunction does so,
2742       *         in which case the mapping is unchanged
2743       */
2744 <    public V computeIfPresent(K key, BiFun<? super K, ? super V, ? extends V> remappingFunction) {
2745 <        if (key == null || remappingFunction == null)
2746 <            throw new NullPointerException();
2672 <        return (V)internalCompute(key, true, remappingFunction);
2744 >    public V computeIfPresent
2745 >        (K key, BiFun<? super K, ? super V, ? extends V> remappingFunction) {
2746 >        return internalCompute(key, true, remappingFunction);
2747      }
2748  
2749      /**
# Line 2712 | Line 2786 | public class ConcurrentHashMapV8<K, V>
2786       * @throws RuntimeException or Error if the remappingFunction does so,
2787       *         in which case the mapping is unchanged
2788       */
2789 <    //    @SuppressWarnings("unchecked")
2790 <    public V compute(K key, BiFun<? super K, ? super V, ? extends V> remappingFunction) {
2791 <        if (key == null || remappingFunction == null)
2718 <            throw new NullPointerException();
2719 <        return (V)internalCompute(key, false, remappingFunction);
2789 >    public V compute
2790 >        (K key, BiFun<? super K, ? super V, ? extends V> remappingFunction) {
2791 >        return internalCompute(key, false, remappingFunction);
2792      }
2793  
2794      /**
# Line 2744 | Line 2816 | public class ConcurrentHashMapV8<K, V>
2816       * so the computation should be short and simple, and must not
2817       * attempt to update any other mappings of this Map.
2818       */
2819 <    //    @SuppressWarnings("unchecked")
2820 <    public V merge(K key, V value, BiFun<? super V, ? super V, ? extends V> remappingFunction) {
2821 <        if (key == null || value == null || remappingFunction == null)
2822 <            throw new NullPointerException();
2751 <        return (V)internalMerge(key, value, remappingFunction);
2819 >    public V merge
2820 >        (K key, V value,
2821 >         BiFun<? super V, ? super V, ? extends V> remappingFunction) {
2822 >        return internalMerge(key, value, remappingFunction);
2823      }
2824  
2825      /**
# Line 2760 | Line 2831 | public class ConcurrentHashMapV8<K, V>
2831       *         {@code null} if there was no mapping for {@code key}
2832       * @throws NullPointerException if the specified key is null
2833       */
2834 <    @SuppressWarnings("unchecked")
2835 <        public V remove(Object key) {
2765 <        if (key == null)
2766 <            throw new NullPointerException();
2767 <        return (V)internalReplace(key, null, null);
2834 >    public V remove(Object key) {
2835 >        return internalReplace(key, null, null);
2836      }
2837  
2838      /**
# Line 2773 | Line 2841 | public class ConcurrentHashMapV8<K, V>
2841       * @throws NullPointerException if the specified key is null
2842       */
2843      public boolean remove(Object key, Object value) {
2844 <        if (key == null)
2777 <            throw new NullPointerException();
2778 <        if (value == null)
2779 <            return false;
2780 <        return internalReplace(key, null, value) != null;
2844 >        return value != null && internalReplace(key, null, value) != null;
2845      }
2846  
2847      /**
# Line 2798 | Line 2862 | public class ConcurrentHashMapV8<K, V>
2862       *         or {@code null} if there was no mapping for the key
2863       * @throws NullPointerException if the specified key or value is null
2864       */
2865 <    @SuppressWarnings("unchecked")
2802 <        public V replace(K key, V value) {
2865 >    public V replace(K key, V value) {
2866          if (key == null || value == null)
2867              throw new NullPointerException();
2868 <        return (V)internalReplace(key, value, null);
2868 >        return internalReplace(key, value, null);
2869      }
2870  
2871      /**
# Line 2815 | Line 2878 | public class ConcurrentHashMapV8<K, V>
2878      /**
2879       * Returns a {@link Set} view of the keys contained in this map.
2880       * The set is backed by the map, so changes to the map are
2881 <     * reflected in the set, and vice-versa.  The set supports element
2819 <     * removal, which removes the corresponding mapping from this map,
2820 <     * via the {@code Iterator.remove}, {@code Set.remove},
2821 <     * {@code removeAll}, {@code retainAll}, and {@code clear}
2822 <     * operations.  It does not support the {@code add} or
2823 <     * {@code addAll} operations.
2881 >     * reflected in the set, and vice-versa.
2882       *
2883 <     * <p>The view's {@code iterator} is a "weakly consistent" iterator
2884 <     * that will never throw {@link ConcurrentModificationException},
2885 <     * and guarantees to traverse elements as they existed upon
2886 <     * construction of the iterator, and may (but is not guaranteed to)
2887 <     * reflect any modifications subsequent to construction.
2883 >     * @return the set view
2884 >     */
2885 >    public KeySetView<K,V> keySet() {
2886 >        KeySetView<K,V> ks = keySet;
2887 >        return (ks != null) ? ks : (keySet = new KeySetView<K,V>(this, null));
2888 >    }
2889 >
2890 >    /**
2891 >     * Returns a {@link Set} view of the keys in this map, using the
2892 >     * given common mapped value for any additions (i.e., {@link
2893 >     * Collection#add} and {@link Collection#addAll}). This is of
2894 >     * course only appropriate if it is acceptable to use the same
2895 >     * value for all additions from this view.
2896 >     *
2897 >     * @param mappedValue the mapped value to use for any
2898 >     * additions.
2899 >     * @return the set view
2900 >     * @throws NullPointerException if the mappedValue is null
2901       */
2902 <    public Set<K> keySet() {
2903 <        KeySet<K,V> ks = keySet;
2904 <        return (ks != null) ? ks : (keySet = new KeySet<K,V>(this));
2902 >    public KeySetView<K,V> keySet(V mappedValue) {
2903 >        if (mappedValue == null)
2904 >            throw new NullPointerException();
2905 >        return new KeySetView<K,V>(this, mappedValue);
2906      }
2907  
2908      /**
2909       * Returns a {@link Collection} view of the values contained in this map.
2910       * The collection is backed by the map, so changes to the map are
2911 <     * reflected in the collection, and vice-versa.  The collection
2840 <     * supports element removal, which removes the corresponding
2841 <     * mapping from this map, via the {@code Iterator.remove},
2842 <     * {@code Collection.remove}, {@code removeAll},
2843 <     * {@code retainAll}, and {@code clear} operations.  It does not
2844 <     * support the {@code add} or {@code addAll} operations.
2845 <     *
2846 <     * <p>The view's {@code iterator} is a "weakly consistent" iterator
2847 <     * that will never throw {@link ConcurrentModificationException},
2848 <     * and guarantees to traverse elements as they existed upon
2849 <     * construction of the iterator, and may (but is not guaranteed to)
2850 <     * reflect any modifications subsequent to construction.
2911 >     * reflected in the collection, and vice-versa.
2912       */
2913 <    public Collection<V> values() {
2914 <        Values<K,V> vs = values;
2915 <        return (vs != null) ? vs : (values = new Values<K,V>(this));
2913 >    public ValuesView<K,V> values() {
2914 >        ValuesView<K,V> vs = values;
2915 >        return (vs != null) ? vs : (values = new ValuesView<K,V>(this));
2916      }
2917  
2918      /**
# Line 2871 | Line 2932 | public class ConcurrentHashMapV8<K, V>
2932       * reflect any modifications subsequent to construction.
2933       */
2934      public Set<Map.Entry<K,V>> entrySet() {
2935 <        EntrySet<K,V> es = entrySet;
2936 <        return (es != null) ? es : (entrySet = new EntrySet<K,V>(this));
2935 >        EntrySetView<K,V> es = entrySet;
2936 >        return (es != null) ? es : (entrySet = new EntrySetView<K,V>(this));
2937      }
2938  
2939      /**
# Line 2932 | Line 2993 | public class ConcurrentHashMapV8<K, V>
2993      public int hashCode() {
2994          int h = 0;
2995          Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
2996 <        Object v;
2996 >        V v;
2997          while ((v = it.advance()) != null) {
2998              h += it.nextKey.hashCode() ^ v.hashCode();
2999          }
# Line 2954 | Line 3015 | public class ConcurrentHashMapV8<K, V>
3015          Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
3016          StringBuilder sb = new StringBuilder();
3017          sb.append('{');
3018 <        Object v;
3018 >        V v;
3019          if ((v = it.advance()) != null) {
3020              for (;;) {
3021                  Object k = it.nextKey;
# Line 2985 | Line 3046 | public class ConcurrentHashMapV8<K, V>
3046                  return false;
3047              Map<?,?> m = (Map<?,?>) o;
3048              Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
3049 <            Object val;
3049 >            V val;
3050              while ((val = it.advance()) != null) {
3051                  Object v = m.get(it.nextKey);
3052                  if (v == null || (v != val && !v.equals(val)))
# Line 3005 | Line 3066 | public class ConcurrentHashMapV8<K, V>
3066  
3067      /* ----------------Iterators -------------- */
3068  
3069 <    static final class KeyIterator<K,V> extends Traverser<K,V,Object>
3069 >    @SuppressWarnings("serial") static final class KeyIterator<K,V>
3070 >        extends Traverser<K,V,Object>
3071          implements Spliterator<K>, Enumeration<K> {
3072          KeyIterator(ConcurrentHashMapV8<K, V> map) { super(map); }
3073 <        KeyIterator(Traverser<K,V,Object> it, boolean split) {
3074 <            super(it, split);
3073 >        KeyIterator(ConcurrentHashMapV8<K, V> map, Traverser<K,V,Object> it) {
3074 >            super(map, it, -1);
3075          }
3076          public KeyIterator<K,V> split() {
3077 <            if (last != null || (next != null && nextVal == null))
3077 >            if (nextKey != null)
3078                  throw new IllegalStateException();
3079 <            return new KeyIterator<K,V>(this, true);
3079 >            return new KeyIterator<K,V>(map, this);
3080          }
3081 <        @SuppressWarnings("unchecked")
3020 <            public final K next() {
3081 >        @SuppressWarnings("unchecked") public final K next() {
3082              if (nextVal == null && advance() == null)
3083                  throw new NoSuchElementException();
3084              Object k = nextKey;
# Line 3028 | Line 3089 | public class ConcurrentHashMapV8<K, V>
3089          public final K nextElement() { return next(); }
3090      }
3091  
3092 <    static final class ValueIterator<K,V> extends Traverser<K,V,Object>
3092 >    @SuppressWarnings("serial") static final class ValueIterator<K,V>
3093 >        extends Traverser<K,V,Object>
3094          implements Spliterator<V>, Enumeration<V> {
3095          ValueIterator(ConcurrentHashMapV8<K, V> map) { super(map); }
3096 <        ValueIterator(Traverser<K,V,Object> it, boolean split) {
3097 <            super(it, split);
3096 >        ValueIterator(ConcurrentHashMapV8<K, V> map, Traverser<K,V,Object> it) {
3097 >            super(map, it, -1);
3098          }
3099          public ValueIterator<K,V> split() {
3100 <            if (last != null || (next != null && nextVal == null))
3100 >            if (nextKey != null)
3101                  throw new IllegalStateException();
3102 <            return new ValueIterator<K,V>(this, true);
3102 >            return new ValueIterator<K,V>(map, this);
3103          }
3104  
3105 <        @SuppressWarnings("unchecked")
3106 <            public final V next() {
3045 <            Object v;
3105 >        public final V next() {
3106 >            V v;
3107              if ((v = nextVal) == null && (v = advance()) == null)
3108                  throw new NoSuchElementException();
3109              nextVal = null;
3110 <            return (V) v;
3110 >            return v;
3111          }
3112  
3113          public final V nextElement() { return next(); }
3114      }
3115  
3116 <    static final class EntryIterator<K,V> extends Traverser<K,V,Object>
3116 >    @SuppressWarnings("serial") static final class EntryIterator<K,V>
3117 >        extends Traverser<K,V,Object>
3118          implements Spliterator<Map.Entry<K,V>> {
3119          EntryIterator(ConcurrentHashMapV8<K, V> map) { super(map); }
3120 <        EntryIterator(Traverser<K,V,Object> it, boolean split) {
3121 <            super(it, split);
3120 >        EntryIterator(ConcurrentHashMapV8<K, V> map, Traverser<K,V,Object> it) {
3121 >            super(map, it, -1);
3122          }
3123          public EntryIterator<K,V> split() {
3124 <            if (last != null || (next != null && nextVal == null))
3124 >            if (nextKey != null)
3125                  throw new IllegalStateException();
3126 <            return new EntryIterator<K,V>(this, true);
3126 >            return new EntryIterator<K,V>(map, this);
3127          }
3128  
3129 <        @SuppressWarnings("unchecked")
3130 <            public final Map.Entry<K,V> next() {
3069 <            Object v;
3129 >        @SuppressWarnings("unchecked") public final Map.Entry<K,V> next() {
3130 >            V v;
3131              if ((v = nextVal) == null && (v = advance()) == null)
3132                  throw new NoSuchElementException();
3133              Object k = nextKey;
3134              nextVal = null;
3135 <            return new MapEntry<K,V>((K)k, (V)v, map);
3135 >            return new MapEntry<K,V>((K)k, v, map);
3136          }
3137      }
3138  
# Line 3118 | Line 3179 | public class ConcurrentHashMapV8<K, V>
3179          }
3180      }
3181  
3121    /* ----------------Views -------------- */
3122
3182      /**
3183 <     * Base class for views.
3183 >     * Returns exportable snapshot entry for the given key and value
3184 >     * when write-through can't or shouldn't be used.
3185       */
3186 <    static abstract class CHMView<K, V> {
3187 <        final ConcurrentHashMapV8<K, V> map;
3128 <        CHMView(ConcurrentHashMapV8<K, V> map)  { this.map = map; }
3129 <        public final int size()                 { return map.size(); }
3130 <        public final boolean isEmpty()          { return map.isEmpty(); }
3131 <        public final void clear()               { map.clear(); }
3132 <
3133 <        // implementations below rely on concrete classes supplying these
3134 <        abstract public Iterator<?> iterator();
3135 <        abstract public boolean contains(Object o);
3136 <        abstract public boolean remove(Object o);
3137 <
3138 <        private static final String oomeMsg = "Required array size too large";
3139 <
3140 <        public final Object[] toArray() {
3141 <            long sz = map.mappingCount();
3142 <            if (sz > (long)(MAX_ARRAY_SIZE))
3143 <                throw new OutOfMemoryError(oomeMsg);
3144 <            int n = (int)sz;
3145 <            Object[] r = new Object[n];
3146 <            int i = 0;
3147 <            Iterator<?> it = iterator();
3148 <            while (it.hasNext()) {
3149 <                if (i == n) {
3150 <                    if (n >= MAX_ARRAY_SIZE)
3151 <                        throw new OutOfMemoryError(oomeMsg);
3152 <                    if (n >= MAX_ARRAY_SIZE - (MAX_ARRAY_SIZE >>> 1) - 1)
3153 <                        n = MAX_ARRAY_SIZE;
3154 <                    else
3155 <                        n += (n >>> 1) + 1;
3156 <                    r = Arrays.copyOf(r, n);
3157 <                }
3158 <                r[i++] = it.next();
3159 <            }
3160 <            return (i == n) ? r : Arrays.copyOf(r, i);
3161 <        }
3162 <
3163 <        @SuppressWarnings("unchecked")
3164 <            public final <T> T[] toArray(T[] a) {
3165 <            long sz = map.mappingCount();
3166 <            if (sz > (long)(MAX_ARRAY_SIZE))
3167 <                throw new OutOfMemoryError(oomeMsg);
3168 <            int m = (int)sz;
3169 <            T[] r = (a.length >= m) ? a :
3170 <                (T[])java.lang.reflect.Array
3171 <                .newInstance(a.getClass().getComponentType(), m);
3172 <            int n = r.length;
3173 <            int i = 0;
3174 <            Iterator<?> it = iterator();
3175 <            while (it.hasNext()) {
3176 <                if (i == n) {
3177 <                    if (n >= MAX_ARRAY_SIZE)
3178 <                        throw new OutOfMemoryError(oomeMsg);
3179 <                    if (n >= MAX_ARRAY_SIZE - (MAX_ARRAY_SIZE >>> 1) - 1)
3180 <                        n = MAX_ARRAY_SIZE;
3181 <                    else
3182 <                        n += (n >>> 1) + 1;
3183 <                    r = Arrays.copyOf(r, n);
3184 <                }
3185 <                r[i++] = (T)it.next();
3186 <            }
3187 <            if (a == r && i < n) {
3188 <                r[i] = null; // null-terminate
3189 <                return r;
3190 <            }
3191 <            return (i == n) ? r : Arrays.copyOf(r, i);
3192 <        }
3193 <
3194 <        public final int hashCode() {
3195 <            int h = 0;
3196 <            for (Iterator<?> it = iterator(); it.hasNext();)
3197 <                h += it.next().hashCode();
3198 <            return h;
3199 <        }
3200 <
3201 <        public final String toString() {
3202 <            StringBuilder sb = new StringBuilder();
3203 <            sb.append('[');
3204 <            Iterator<?> it = iterator();
3205 <            if (it.hasNext()) {
3206 <                for (;;) {
3207 <                    Object e = it.next();
3208 <                    sb.append(e == this ? "(this Collection)" : e);
3209 <                    if (!it.hasNext())
3210 <                        break;
3211 <                    sb.append(',').append(' ');
3212 <                }
3213 <            }
3214 <            return sb.append(']').toString();
3215 <        }
3216 <
3217 <        public final boolean containsAll(Collection<?> c) {
3218 <            if (c != this) {
3219 <                for (Iterator<?> it = c.iterator(); it.hasNext();) {
3220 <                    Object e = it.next();
3221 <                    if (e == null || !contains(e))
3222 <                        return false;
3223 <                }
3224 <            }
3225 <            return true;
3226 <        }
3227 <
3228 <        public final boolean removeAll(Collection<?> c) {
3229 <            boolean modified = false;
3230 <            for (Iterator<?> it = iterator(); it.hasNext();) {
3231 <                if (c.contains(it.next())) {
3232 <                    it.remove();
3233 <                    modified = true;
3234 <                }
3235 <            }
3236 <            return modified;
3237 <        }
3238 <
3239 <        public final boolean retainAll(Collection<?> c) {
3240 <            boolean modified = false;
3241 <            for (Iterator<?> it = iterator(); it.hasNext();) {
3242 <                if (!c.contains(it.next())) {
3243 <                    it.remove();
3244 <                    modified = true;
3245 <                }
3246 <            }
3247 <            return modified;
3248 <        }
3249 <
3250 <    }
3251 <
3252 <    static final class KeySet<K,V> extends CHMView<K,V> implements Set<K> {
3253 <        KeySet(ConcurrentHashMapV8<K, V> map)  {
3254 <            super(map);
3255 <        }
3256 <        public final boolean contains(Object o) { return map.containsKey(o); }
3257 <        public final boolean remove(Object o)   { return map.remove(o) != null; }
3258 <        public final Iterator<K> iterator() {
3259 <            return new KeyIterator<K,V>(map);
3260 <        }
3261 <        public final boolean add(K e) {
3262 <            throw new UnsupportedOperationException();
3263 <        }
3264 <        public final boolean addAll(Collection<? extends K> c) {
3265 <            throw new UnsupportedOperationException();
3266 <        }
3267 <        public boolean equals(Object o) {
3268 <            Set<?> c;
3269 <            return ((o instanceof Set) &&
3270 <                    ((c = (Set<?>)o) == this ||
3271 <                     (containsAll(c) && c.containsAll(this))));
3272 <        }
3273 <    }
3274 <
3275 <
3276 <    static final class Values<K,V> extends CHMView<K,V>
3277 <        implements Collection<V> {
3278 <        Values(ConcurrentHashMapV8<K, V> map)   { super(map); }
3279 <        public final boolean contains(Object o) { return map.containsValue(o); }
3280 <        public final boolean remove(Object o) {
3281 <            if (o != null) {
3282 <                Iterator<V> it = new ValueIterator<K,V>(map);
3283 <                while (it.hasNext()) {
3284 <                    if (o.equals(it.next())) {
3285 <                        it.remove();
3286 <                        return true;
3287 <                    }
3288 <                }
3289 <            }
3290 <            return false;
3291 <        }
3292 <        public final Iterator<V> iterator() {
3293 <            return new ValueIterator<K,V>(map);
3294 <        }
3295 <        public final boolean add(V e) {
3296 <            throw new UnsupportedOperationException();
3297 <        }
3298 <        public final boolean addAll(Collection<? extends V> c) {
3299 <            throw new UnsupportedOperationException();
3300 <        }
3301 <
3302 <    }
3303 <
3304 <    static final class EntrySet<K,V> extends CHMView<K,V>
3305 <        implements Set<Map.Entry<K,V>> {
3306 <        EntrySet(ConcurrentHashMapV8<K, V> map) { super(map); }
3307 <        public final boolean contains(Object o) {
3308 <            Object k, v, r; Map.Entry<?,?> e;
3309 <            return ((o instanceof Map.Entry) &&
3310 <                    (k = (e = (Map.Entry<?,?>)o).getKey()) != null &&
3311 <                    (r = map.get(k)) != null &&
3312 <                    (v = e.getValue()) != null &&
3313 <                    (v == r || v.equals(r)));
3314 <        }
3315 <        public final boolean remove(Object o) {
3316 <            Object k, v; Map.Entry<?,?> e;
3317 <            return ((o instanceof Map.Entry) &&
3318 <                    (k = (e = (Map.Entry<?,?>)o).getKey()) != null &&
3319 <                    (v = e.getValue()) != null &&
3320 <                    map.remove(k, v));
3321 <        }
3322 <        public final Iterator<Map.Entry<K,V>> iterator() {
3323 <            return new EntryIterator<K,V>(map);
3324 <        }
3325 <        public final boolean add(Entry<K,V> e) {
3326 <            throw new UnsupportedOperationException();
3327 <        }
3328 <        public final boolean addAll(Collection<? extends Entry<K,V>> c) {
3329 <            throw new UnsupportedOperationException();
3330 <        }
3331 <        public boolean equals(Object o) {
3332 <            Set<?> c;
3333 <            return ((o instanceof Set) &&
3334 <                    ((c = (Set<?>)o) == this ||
3335 <                     (containsAll(c) && c.containsAll(this))));
3336 <        }
3186 >    static <K,V> AbstractMap.SimpleEntry<K,V> entryFor(K k, V v) {
3187 >        return new AbstractMap.SimpleEntry<K,V>(k, v);
3188      }
3189  
3190      /* ---------------- Serialization Support -------------- */
# Line 3357 | Line 3208 | public class ConcurrentHashMapV8<K, V>
3208       * for each key-value mapping, followed by a null pair.
3209       * The key-value mappings are emitted in no particular order.
3210       */
3211 <    @SuppressWarnings("unchecked")
3212 <        private void writeObject(java.io.ObjectOutputStream s)
3211 >    @SuppressWarnings("unchecked") private void writeObject
3212 >        (java.io.ObjectOutputStream s)
3213          throws java.io.IOException {
3214          if (segments == null) { // for serialization compatibility
3215              segments = (Segment<K,V>[])
# Line 3368 | Line 3219 | public class ConcurrentHashMapV8<K, V>
3219          }
3220          s.defaultWriteObject();
3221          Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
3222 <        Object v;
3222 >        V v;
3223          while ((v = it.advance()) != null) {
3224              s.writeObject(it.nextKey);
3225              s.writeObject(v);
# Line 3382 | Line 3233 | public class ConcurrentHashMapV8<K, V>
3233       * Reconstitutes the instance from a stream (that is, deserializes it).
3234       * @param s the stream
3235       */
3236 <    @SuppressWarnings("unchecked")
3237 <        private void readObject(java.io.ObjectInputStream s)
3236 >    @SuppressWarnings("unchecked") private void readObject
3237 >        (java.io.ObjectInputStream s)
3238          throws java.io.IOException, ClassNotFoundException {
3239          s.defaultReadObject();
3240          this.segments = null; // unneeded
3390        // initialize transient final field
3391        UNSAFE.putObjectVolatile(this, counterOffset, new LongAdder());
3241  
3242          // Create all nodes, then place in table once size is known
3243          long size = 0L;
3244 <        Node p = null;
3244 >        Node<V> p = null;
3245          for (;;) {
3246              K k = (K) s.readObject();
3247              V v = (V) s.readObject();
3248              if (k != null && v != null) {
3249                  int h = spread(k.hashCode());
3250 <                p = new Node(h, k, v, p);
3250 >                p = new Node<V>(h, k, v, p);
3251                  ++size;
3252              }
3253              else
# Line 3416 | Line 3265 | public class ConcurrentHashMapV8<K, V>
3265              int sc = sizeCtl;
3266              boolean collide = false;
3267              if (n > sc &&
3268 <                UNSAFE.compareAndSwapInt(this, sizeCtlOffset, sc, -1)) {
3268 >                U.compareAndSwapInt(this, SIZECTL, sc, -1)) {
3269                  try {
3270                      if (table == null) {
3271                          init = true;
3272 <                        Node[] tab = new Node[n];
3272 >                        @SuppressWarnings("rawtypes") Node[] rt = new Node[n];
3273 >                        Node<V>[] tab = (Node<V>[])rt;
3274                          int mask = n - 1;
3275                          while (p != null) {
3276                              int j = p.hash & mask;
3277 <                            Node next = p.next;
3278 <                            Node q = p.next = tabAt(tab, j);
3277 >                            Node<V> next = p.next;
3278 >                            Node<V> q = p.next = tabAt(tab, j);
3279                              setTabAt(tab, j, p);
3280                              if (!collide && q != null && q.hash == p.hash)
3281                                  collide = true;
3282                              p = next;
3283                          }
3284                          table = tab;
3285 <                        counter.add(size);
3285 >                        addCount(size, -1);
3286                          sc = n - (n >>> 2);
3287                      }
3288                  } finally {
3289                      sizeCtl = sc;
3290                  }
3291                  if (collide) { // rescan and convert to TreeBins
3292 <                    Node[] tab = table;
3292 >                    Node<V>[] tab = table;
3293                      for (int i = 0; i < tab.length; ++i) {
3294                          int c = 0;
3295 <                        for (Node e = tabAt(tab, i); e != null; e = e.next) {
3295 >                        for (Node<V> e = tabAt(tab, i); e != null; e = e.next) {
3296                              if (++c > TREE_THRESHOLD &&
3297                                  (e.key instanceof Comparable)) {
3298                                  replaceWithTreeBin(tab, i, e.key);
# Line 3454 | Line 3304 | public class ConcurrentHashMapV8<K, V>
3304              }
3305              if (!init) { // Can only happen if unsafely published.
3306                  while (p != null) {
3307 <                    internalPut(p.key, p.val);
3307 >                    internalPut((K)p.key, p.val, false);
3308                      p = p.next;
3309                  }
3310              }
3311          }
3312      }
3313  
3464
3314      // -------------------------------------------------------
3315  
3316      // Sams
# Line 3503 | Line 3352 | public class ConcurrentHashMapV8<K, V>
3352  
3353      // -------------------------------------------------------
3354  
3355 +    // Sequential bulk operations
3356 +
3357      /**
3358 <     * Returns an extended {@link Parallel} view of this map using the
3508 <     * given executor for bulk parallel operations.
3358 >     * Performs the given action for each (key, value).
3359       *
3360 <     * @param executor the executor
3511 <     * @return a parallel view
3360 >     * @param action the action
3361       */
3362 <    public Parallel parallel(ForkJoinPool executor)  {
3363 <        return new Parallel(executor);
3362 >    @SuppressWarnings("unchecked") public void forEachSequentially
3363 >        (BiAction<K,V> action) {
3364 >        if (action == null) throw new NullPointerException();
3365 >        Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
3366 >        V v;
3367 >        while ((v = it.advance()) != null)
3368 >            action.apply((K)it.nextKey, v);
3369      }
3370  
3371      /**
3372 <     * An extended view of a ConcurrentHashMap supporting bulk
3373 <     * parallel operations. These operations are designed to be
3374 <     * safely, and often sensibly, applied even with maps that are
3375 <     * being concurrently updated by other threads; for example, when
3376 <     * computing a snapshot summary of the values in a shared
3377 <     * registry.  There are three kinds of operation, each with four
3378 <     * forms, accepting functions with Keys, Values, Entries, and
3525 <     * (Key, Value) arguments and/or return values. Because the
3526 <     * elements of a ConcurrentHashMap are not ordered in any
3527 <     * particular way, and may be processed in different orders in
3528 <     * different parallel executions, the correctness of supplied
3529 <     * functions should not depend on any ordering, or on any other
3530 <     * objects or values that may transiently change while computation
3531 <     * is in progress; and except for forEach actions, should ideally
3532 <     * be side-effect-free.
3533 <     *
3534 <     * <ul>
3535 <     * <li> forEach: Perform a given action on each element.
3536 <     * A variant form applies a given transformation on each element
3537 <     * before performing the action.</li>
3538 <     *
3539 <     * <li> search: Return the first available non-null result of
3540 <     * applying a given function on each element; skipping further
3541 <     * search when a result is found.</li>
3542 <     *
3543 <     * <li> reduce: Accumulate each element.  The supplied reduction
3544 <     * function cannot rely on ordering (more formally, it should be
3545 <     * both associative and commutative).  There are five variants:
3546 <     *
3547 <     * <ul>
3548 <     *
3549 <     * <li> Plain reductions. (There is not a form of this method for
3550 <     * (key, value) function arguments since there is no corresponding
3551 <     * return type.)</li>
3552 <     *
3553 <     * <li> Mapped reductions that accumulate the results of a given
3554 <     * function applied to each element.</li>
3555 <     *
3556 <     * <li> Reductions to scalar doubles, longs, and ints, using a
3557 <     * given basis value.</li>
3558 <     *
3559 <     * </li>
3560 <     * </ul>
3561 <     * </ul>
3562 <     *
3563 <     * <p>The concurrency properties of the bulk operations follow
3564 <     * from those of ConcurrentHashMap: Any non-null result returned
3565 <     * from {@code get(key)} and related access methods bears a
3566 <     * happens-before relation with the associated insertion or
3567 <     * update.  The result of any bulk operation reflects the
3568 <     * composition of these per-element relations (but is not
3569 <     * necessarily atomic with respect to the map as a whole unless it
3570 <     * is somehow known to be quiescent).  Conversely, because keys
3571 <     * and values in the map are never null, null serves as a reliable
3572 <     * atomic indicator of the current lack of any result.  To
3573 <     * maintain this property, null serves as an implicit basis for
3574 <     * all non-scalar reduction operations. For the double, long, and
3575 <     * int versions, the basis should be one that, when combined with
3576 <     * any other value, returns that other value (more formally, it
3577 <     * should be the identity element for the reduction). Most common
3578 <     * reductions have these properties; for example, computing a sum
3579 <     * with basis 0 or a minimum with basis MAX_VALUE.
3580 <     *
3581 <     * <p>Search and transformation functions provided as arguments
3582 <     * should similarly return null to indicate the lack of any result
3583 <     * (in which case it is not used). In the case of mapped
3584 <     * reductions, this also enables transformations to serve as
3585 <     * filters, returning null (or, in the case of primitive
3586 <     * specializations, the identity basis) if the element should not
3587 <     * be combined. You can create compound transformations and
3588 <     * filterings by composing them yourself under this "null means
3589 <     * there is nothing there now" rule before using them in search or
3590 <     * reduce operations.
3591 <     *
3592 <     * <p>Methods accepting and/or returning Entry arguments maintain
3593 <     * key-value associations. They may be useful for example when
3594 <     * finding the key for the greatest value. Note that "plain" Entry
3595 <     * arguments can be supplied using {@code new
3596 <     * AbstractMap.SimpleEntry(k,v)}.
3597 <     *
3598 <     * <p> Bulk operations may complete abruptly, throwing an
3599 <     * exception encountered in the application of a supplied
3600 <     * function. Bear in mind when handling such exceptions that other
3601 <     * concurrently executing functions could also have thrown
3602 <     * exceptions, or would have done so if the first exception had
3603 <     * not occurred.
3604 <     *
3605 <     * <p>Parallel speedups compared to sequential processing are
3606 <     * common but not guaranteed.  Operations involving brief
3607 <     * functions on small maps may execute more slowly than sequential
3608 <     * loops if the underlying work to parallelize the computation is
3609 <     * more expensive than the computation itself. Similarly,
3610 <     * parallelization may not lead to much actual parallelism if all
3611 <     * processors are busy performing unrelated tasks.
3612 <     *
3613 <     * <p> All arguments to all task methods must be non-null.
3614 <     *
3615 <     * <p><em>jsr166e note: During transition, this class
3616 <     * uses nested functional interfaces with different names but the
3617 <     * same forms as those expected for JDK8.<em>
3372 >     * Performs the given action for each non-null transformation
3373 >     * of each (key, value).
3374 >     *
3375 >     * @param transformer a function returning the transformation
3376 >     * for an element, or null of there is no transformation (in
3377 >     * which case the action is not applied).
3378 >     * @param action the action
3379       */
3380 <    public class Parallel {
3381 <        final ForkJoinPool fjp;
3382 <
3383 <        /**
3384 <         * Returns an extended view of this map using the given
3385 <         * executor for bulk parallel operations.
3386 <         *
3387 <         * @param executor the executor
3388 <         */
3389 <        public Parallel(ForkJoinPool executor)  {
3629 <            this.fjp = executor;
3380 >    @SuppressWarnings("unchecked") public <U> void forEachSequentially
3381 >        (BiFun<? super K, ? super V, ? extends U> transformer,
3382 >         Action<U> action) {
3383 >        if (transformer == null || action == null)
3384 >            throw new NullPointerException();
3385 >        Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
3386 >        V v; U u;
3387 >        while ((v = it.advance()) != null) {
3388 >            if ((u = transformer.apply((K)it.nextKey, v)) != null)
3389 >                action.apply(u);
3390          }
3391 +    }
3392  
3393 <        /**
3394 <         * Performs the given action for each (key, value).
3395 <         *
3396 <         * @param action the action
3397 <         */
3398 <        public void forEach(BiAction<K,V> action) {
3399 <            fjp.invoke(ForkJoinTasks.forEach
3400 <                       (ConcurrentHashMapV8.this, action));
3393 >    /**
3394 >     * Returns a non-null result from applying the given search
3395 >     * function on each (key, value), or null if none.
3396 >     *
3397 >     * @param searchFunction a function returning a non-null
3398 >     * result on success, else null
3399 >     * @return a non-null result from applying the given search
3400 >     * function on each (key, value), or null if none
3401 >     */
3402 >    @SuppressWarnings("unchecked") public <U> U searchSequentially
3403 >        (BiFun<? super K, ? super V, ? extends U> searchFunction) {
3404 >        if (searchFunction == null) throw new NullPointerException();
3405 >        Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
3406 >        V v; U u;
3407 >        while ((v = it.advance()) != null) {
3408 >            if ((u = searchFunction.apply((K)it.nextKey, v)) != null)
3409 >                return u;
3410          }
3411 +        return null;
3412 +    }
3413  
3414 <        /**
3415 <         * Performs the given action for each non-null transformation
3416 <         * of each (key, value).
3417 <         *
3418 <         * @param transformer a function returning the transformation
3419 <         * for an element, or null of there is no transformation (in
3420 <         * which case the action is not applied).
3421 <         * @param action the action
3422 <         */
3423 <        public <U> void forEach(BiFun<? super K, ? super V, ? extends U> transformer,
3424 <                                Action<U> action) {
3425 <            fjp.invoke(ForkJoinTasks.forEach
3426 <                       (ConcurrentHashMapV8.this, transformer, action));
3414 >    /**
3415 >     * Returns the result of accumulating the given transformation
3416 >     * of all (key, value) pairs using the given reducer to
3417 >     * combine values, or null if none.
3418 >     *
3419 >     * @param transformer a function returning the transformation
3420 >     * for an element, or null of there is no transformation (in
3421 >     * which case it is not combined).
3422 >     * @param reducer a commutative associative combining function
3423 >     * @return the result of accumulating the given transformation
3424 >     * of all (key, value) pairs
3425 >     */
3426 >    @SuppressWarnings("unchecked") public <U> U reduceSequentially
3427 >        (BiFun<? super K, ? super V, ? extends U> transformer,
3428 >         BiFun<? super U, ? super U, ? extends U> reducer) {
3429 >        if (transformer == null || reducer == null)
3430 >            throw new NullPointerException();
3431 >        Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
3432 >        U r = null, u; V v;
3433 >        while ((v = it.advance()) != null) {
3434 >            if ((u = transformer.apply((K)it.nextKey, v)) != null)
3435 >                r = (r == null) ? u : reducer.apply(r, u);
3436          }
3437 +        return r;
3438 +    }
3439  
3440 <        /**
3441 <         * Returns a non-null result from applying the given search
3442 <         * function on each (key, value), or null if none.  Further
3443 <         * element processing is suppressed upon success. However,
3444 <         * this method does not return until other in-progress
3445 <         * parallel invocations of the search function also complete.
3446 <         *
3447 <         * @param searchFunction a function returning a non-null
3448 <         * result on success, else null
3449 <         * @return a non-null result from applying the given search
3450 <         * function on each (key, value), or null if none
3451 <         */
3452 <        public <U> U search(BiFun<? super K, ? super V, ? extends U> searchFunction) {
3453 <            return fjp.invoke(ForkJoinTasks.search
3454 <                              (ConcurrentHashMapV8.this, searchFunction));
3455 <        }
3440 >    /**
3441 >     * Returns the result of accumulating the given transformation
3442 >     * of all (key, value) pairs using the given reducer to
3443 >     * combine values, and the given basis as an identity value.
3444 >     *
3445 >     * @param transformer a function returning the transformation
3446 >     * for an element
3447 >     * @param basis the identity (initial default value) for the reduction
3448 >     * @param reducer a commutative associative combining function
3449 >     * @return the result of accumulating the given transformation
3450 >     * of all (key, value) pairs
3451 >     */
3452 >    @SuppressWarnings("unchecked") public double reduceToDoubleSequentially
3453 >        (ObjectByObjectToDouble<? super K, ? super V> transformer,
3454 >         double basis,
3455 >         DoubleByDoubleToDouble reducer) {
3456 >        if (transformer == null || reducer == null)
3457 >            throw new NullPointerException();
3458 >        Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
3459 >        double r = basis; V v;
3460 >        while ((v = it.advance()) != null)
3461 >            r = reducer.apply(r, transformer.apply((K)it.nextKey, v));
3462 >        return r;
3463 >    }
3464  
3465 <        /**
3466 <         * Returns the result of accumulating the given transformation
3467 <         * of all (key, value) pairs using the given reducer to
3468 <         * combine values, or null if none.
3469 <         *
3470 <         * @param transformer a function returning the transformation
3471 <         * for an element, or null of there is no transformation (in
3472 <         * which case it is not combined).
3473 <         * @param reducer a commutative associative combining function
3474 <         * @return the result of accumulating the given transformation
3475 <         * of all (key, value) pairs
3476 <         */
3477 <        public <U> U reduce(BiFun<? super K, ? super V, ? extends U> transformer,
3478 <                            BiFun<? super U, ? super U, ? extends U> reducer) {
3479 <            return fjp.invoke(ForkJoinTasks.reduce
3480 <                              (ConcurrentHashMapV8.this, transformer, reducer));
3481 <        }
3465 >    /**
3466 >     * Returns the result of accumulating the given transformation
3467 >     * of all (key, value) pairs using the given reducer to
3468 >     * combine values, and the given basis as an identity value.
3469 >     *
3470 >     * @param transformer a function returning the transformation
3471 >     * for an element
3472 >     * @param basis the identity (initial default value) for the reduction
3473 >     * @param reducer a commutative associative combining function
3474 >     * @return the result of accumulating the given transformation
3475 >     * of all (key, value) pairs
3476 >     */
3477 >    @SuppressWarnings("unchecked") public long reduceToLongSequentially
3478 >        (ObjectByObjectToLong<? super K, ? super V> transformer,
3479 >         long basis,
3480 >         LongByLongToLong reducer) {
3481 >        if (transformer == null || reducer == null)
3482 >            throw new NullPointerException();
3483 >        Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
3484 >        long r = basis; V v;
3485 >        while ((v = it.advance()) != null)
3486 >            r = reducer.apply(r, transformer.apply((K)it.nextKey, v));
3487 >        return r;
3488 >    }
3489  
3490 <        /**
3491 <         * Returns the result of accumulating the given transformation
3492 <         * of all (key, value) pairs using the given reducer to
3493 <         * combine values, and the given basis as an identity value.
3494 <         *
3495 <         * @param transformer a function returning the transformation
3496 <         * for an element
3497 <         * @param basis the identity (initial default value) for the reduction
3498 <         * @param reducer a commutative associative combining function
3499 <         * @return the result of accumulating the given transformation
3500 <         * of all (key, value) pairs
3501 <         */
3502 <        public double reduceToDouble(ObjectByObjectToDouble<? super K, ? super V> transformer,
3503 <                                     double basis,
3504 <                                     DoubleByDoubleToDouble reducer) {
3505 <            return fjp.invoke(ForkJoinTasks.reduceToDouble
3506 <                              (ConcurrentHashMapV8.this, transformer, basis, reducer));
3507 <        }
3490 >    /**
3491 >     * Returns the result of accumulating the given transformation
3492 >     * of all (key, value) pairs using the given reducer to
3493 >     * combine values, and the given basis as an identity value.
3494 >     *
3495 >     * @param transformer a function returning the transformation
3496 >     * for an element
3497 >     * @param basis the identity (initial default value) for the reduction
3498 >     * @param reducer a commutative associative combining function
3499 >     * @return the result of accumulating the given transformation
3500 >     * of all (key, value) pairs
3501 >     */
3502 >    @SuppressWarnings("unchecked") public int reduceToIntSequentially
3503 >        (ObjectByObjectToInt<? super K, ? super V> transformer,
3504 >         int basis,
3505 >         IntByIntToInt reducer) {
3506 >        if (transformer == null || reducer == null)
3507 >            throw new NullPointerException();
3508 >        Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
3509 >        int r = basis; V v;
3510 >        while ((v = it.advance()) != null)
3511 >            r = reducer.apply(r, transformer.apply((K)it.nextKey, v));
3512 >        return r;
3513 >    }
3514  
3515 <        /**
3516 <         * Returns the result of accumulating the given transformation
3517 <         * of all (key, value) pairs using the given reducer to
3518 <         * combine values, and the given basis as an identity value.
3519 <         *
3520 <         * @param transformer a function returning the transformation
3521 <         * for an element
3522 <         * @param basis the identity (initial default value) for the reduction
3523 <         * @param reducer a commutative associative combining function
3524 <         * @return the result of accumulating the given transformation
3525 <         * of all (key, value) pairs using the given reducer to
3526 <         * combine values, and the given basis as an identity value.
3723 <         */
3724 <        public long reduceToLong(ObjectByObjectToLong<? super K, ? super V> transformer,
3725 <                                 long basis,
3726 <                                 LongByLongToLong reducer) {
3727 <            return fjp.invoke(ForkJoinTasks.reduceToLong
3728 <                              (ConcurrentHashMapV8.this, transformer, basis, reducer));
3729 <        }
3515 >    /**
3516 >     * Performs the given action for each key.
3517 >     *
3518 >     * @param action the action
3519 >     */
3520 >    @SuppressWarnings("unchecked") public void forEachKeySequentially
3521 >        (Action<K> action) {
3522 >        if (action == null) throw new NullPointerException();
3523 >        Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
3524 >        while (it.advance() != null)
3525 >            action.apply((K)it.nextKey);
3526 >    }
3527  
3528 <        /**
3529 <         * Returns the result of accumulating the given transformation
3530 <         * of all (key, value) pairs using the given reducer to
3531 <         * combine values, and the given basis as an identity value.
3532 <         *
3533 <         * @param transformer a function returning the transformation
3534 <         * for an element
3535 <         * @param basis the identity (initial default value) for the reduction
3536 <         * @param reducer a commutative associative combining function
3537 <         * @return the result of accumulating the given transformation
3538 <         * of all (key, value) pairs
3539 <         */
3540 <        public int reduceToInt(ObjectByObjectToInt<? super K, ? super V> transformer,
3541 <                               int basis,
3542 <                               IntByIntToInt reducer) {
3543 <            return fjp.invoke(ForkJoinTasks.reduceToInt
3544 <                              (ConcurrentHashMapV8.this, transformer, basis, reducer));
3528 >    /**
3529 >     * Performs the given action for each non-null transformation
3530 >     * of each key.
3531 >     *
3532 >     * @param transformer a function returning the transformation
3533 >     * for an element, or null of there is no transformation (in
3534 >     * which case the action is not applied).
3535 >     * @param action the action
3536 >     */
3537 >    @SuppressWarnings("unchecked") public <U> void forEachKeySequentially
3538 >        (Fun<? super K, ? extends U> transformer,
3539 >         Action<U> action) {
3540 >        if (transformer == null || action == null)
3541 >            throw new NullPointerException();
3542 >        Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
3543 >        U u;
3544 >        while (it.advance() != null) {
3545 >            if ((u = transformer.apply((K)it.nextKey)) != null)
3546 >                action.apply(u);
3547          }
3548 +        ForkJoinTasks.forEachKey
3549 +            (this, transformer, action).invoke();
3550 +    }
3551  
3552 <        /**
3553 <         * Performs the given action for each key.
3554 <         *
3555 <         * @param action the action
3556 <         */
3557 <        public void forEachKey(Action<K> action) {
3558 <            fjp.invoke(ForkJoinTasks.forEachKey
3559 <                       (ConcurrentHashMapV8.this, action));
3552 >    /**
3553 >     * Returns a non-null result from applying the given search
3554 >     * function on each key, or null if none.
3555 >     *
3556 >     * @param searchFunction a function returning a non-null
3557 >     * result on success, else null
3558 >     * @return a non-null result from applying the given search
3559 >     * function on each key, or null if none
3560 >     */
3561 >    @SuppressWarnings("unchecked") public <U> U searchKeysSequentially
3562 >        (Fun<? super K, ? extends U> searchFunction) {
3563 >        Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
3564 >        U u;
3565 >        while (it.advance() != null) {
3566 >            if ((u = searchFunction.apply((K)it.nextKey)) != null)
3567 >                return u;
3568          }
3569 +        return null;
3570 +    }
3571  
3572 <        /**
3573 <         * Performs the given action for each non-null transformation
3574 <         * of each key.
3575 <         *
3576 <         * @param transformer a function returning the transformation
3577 <         * for an element, or null of there is no transformation (in
3578 <         * which case the action is not applied).
3579 <         * @param action the action
3580 <         */
3581 <        public <U> void forEachKey(Fun<? super K, ? extends U> transformer,
3582 <                                   Action<U> action) {
3583 <            fjp.invoke(ForkJoinTasks.forEachKey
3584 <                       (ConcurrentHashMapV8.this, transformer, action));
3572 >    /**
3573 >     * Returns the result of accumulating all keys using the given
3574 >     * reducer to combine values, or null if none.
3575 >     *
3576 >     * @param reducer a commutative associative combining function
3577 >     * @return the result of accumulating all keys using the given
3578 >     * reducer to combine values, or null if none
3579 >     */
3580 >    @SuppressWarnings("unchecked") public K reduceKeysSequentially
3581 >        (BiFun<? super K, ? super K, ? extends K> reducer) {
3582 >        if (reducer == null) throw new NullPointerException();
3583 >        Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
3584 >        K r = null;
3585 >        while (it.advance() != null) {
3586 >            K u = (K)it.nextKey;
3587 >            r = (r == null) ? u : reducer.apply(r, u);
3588 >        }
3589 >        return r;
3590 >    }
3591 >
3592 >    /**
3593 >     * Returns the result of accumulating the given transformation
3594 >     * of all keys using the given reducer to combine values, or
3595 >     * null if none.
3596 >     *
3597 >     * @param transformer a function returning the transformation
3598 >     * for an element, or null of there is no transformation (in
3599 >     * which case it is not combined).
3600 >     * @param reducer a commutative associative combining function
3601 >     * @return the result of accumulating the given transformation
3602 >     * of all keys
3603 >     */
3604 >    @SuppressWarnings("unchecked") public <U> U reduceKeysSequentially
3605 >        (Fun<? super K, ? extends U> transformer,
3606 >         BiFun<? super U, ? super U, ? extends U> reducer) {
3607 >        if (transformer == null || reducer == null)
3608 >            throw new NullPointerException();
3609 >        Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
3610 >        U r = null, u;
3611 >        while (it.advance() != null) {
3612 >            if ((u = transformer.apply((K)it.nextKey)) != null)
3613 >                r = (r == null) ? u : reducer.apply(r, u);
3614          }
3615 +        return r;
3616 +    }
3617  
3618 <        /**
3619 <         * Returns a non-null result from applying the given search
3620 <         * function on each key, or null if none.  Further element
3621 <         * processing is suppressed upon success. However, this method
3622 <         * does not return until other in-progress parallel
3623 <         * invocations of the search function also complete.
3624 <         *
3625 <         * @param searchFunction a function returning a non-null
3626 <         * result on success, else null
3627 <         * @return a non-null result from applying the given search
3628 <         * function on each key, or null if none
3629 <         */
3630 <        public <U> U searchKeys(Fun<? super K, ? extends U> searchFunction) {
3631 <            return fjp.invoke(ForkJoinTasks.searchKeys
3632 <                              (ConcurrentHashMapV8.this, searchFunction));
3618 >    /**
3619 >     * Returns the result of accumulating the given transformation
3620 >     * of all keys using the given reducer to combine values, and
3621 >     * the given basis as an identity value.
3622 >     *
3623 >     * @param transformer a function returning the transformation
3624 >     * for an element
3625 >     * @param basis the identity (initial default value) for the reduction
3626 >     * @param reducer a commutative associative combining function
3627 >     * @return  the result of accumulating the given transformation
3628 >     * of all keys
3629 >     */
3630 >    @SuppressWarnings("unchecked") public double reduceKeysToDoubleSequentially
3631 >        (ObjectToDouble<? super K> transformer,
3632 >         double basis,
3633 >         DoubleByDoubleToDouble reducer) {
3634 >        if (transformer == null || reducer == null)
3635 >            throw new NullPointerException();
3636 >        Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
3637 >        double r = basis;
3638 >        while (it.advance() != null)
3639 >            r = reducer.apply(r, transformer.apply((K)it.nextKey));
3640 >        return r;
3641 >    }
3642 >
3643 >    /**
3644 >     * Returns the result of accumulating the given transformation
3645 >     * of all keys using the given reducer to combine values, and
3646 >     * the given basis as an identity value.
3647 >     *
3648 >     * @param transformer a function returning the transformation
3649 >     * for an element
3650 >     * @param basis the identity (initial default value) for the reduction
3651 >     * @param reducer a commutative associative combining function
3652 >     * @return the result of accumulating the given transformation
3653 >     * of all keys
3654 >     */
3655 >    @SuppressWarnings("unchecked") public long reduceKeysToLongSequentially
3656 >        (ObjectToLong<? super K> transformer,
3657 >         long basis,
3658 >         LongByLongToLong reducer) {
3659 >        if (transformer == null || reducer == null)
3660 >            throw new NullPointerException();
3661 >        Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
3662 >        long r = basis;
3663 >        while (it.advance() != null)
3664 >            r = reducer.apply(r, transformer.apply((K)it.nextKey));
3665 >        return r;
3666 >    }
3667 >
3668 >    /**
3669 >     * Returns the result of accumulating the given transformation
3670 >     * of all keys using the given reducer to combine values, and
3671 >     * the given basis as an identity value.
3672 >     *
3673 >     * @param transformer a function returning the transformation
3674 >     * for an element
3675 >     * @param basis the identity (initial default value) for the reduction
3676 >     * @param reducer a commutative associative combining function
3677 >     * @return the result of accumulating the given transformation
3678 >     * of all keys
3679 >     */
3680 >    @SuppressWarnings("unchecked") public int reduceKeysToIntSequentially
3681 >        (ObjectToInt<? super K> transformer,
3682 >         int basis,
3683 >         IntByIntToInt reducer) {
3684 >        if (transformer == null || reducer == null)
3685 >            throw new NullPointerException();
3686 >        Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
3687 >        int r = basis;
3688 >        while (it.advance() != null)
3689 >            r = reducer.apply(r, transformer.apply((K)it.nextKey));
3690 >        return r;
3691 >    }
3692 >
3693 >    /**
3694 >     * Performs the given action for each value.
3695 >     *
3696 >     * @param action the action
3697 >     */
3698 >    public void forEachValueSequentially(Action<V> action) {
3699 >        if (action == null) throw new NullPointerException();
3700 >        Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
3701 >        V v;
3702 >        while ((v = it.advance()) != null)
3703 >            action.apply(v);
3704 >    }
3705 >
3706 >    /**
3707 >     * Performs the given action for each non-null transformation
3708 >     * of each value.
3709 >     *
3710 >     * @param transformer a function returning the transformation
3711 >     * for an element, or null of there is no transformation (in
3712 >     * which case the action is not applied).
3713 >     */
3714 >    public <U> void forEachValueSequentially
3715 >        (Fun<? super V, ? extends U> transformer,
3716 >         Action<U> action) {
3717 >        if (transformer == null || action == null)
3718 >            throw new NullPointerException();
3719 >        Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
3720 >        V v; U u;
3721 >        while ((v = it.advance()) != null) {
3722 >            if ((u = transformer.apply(v)) != null)
3723 >                action.apply(u);
3724          }
3725 +    }
3726  
3727 <        /**
3728 <         * Returns the result of accumulating all keys using the given
3729 <         * reducer to combine values, or null if none.
3730 <         *
3731 <         * @param reducer a commutative associative combining function
3732 <         * @return the result of accumulating all keys using the given
3733 <         * reducer to combine values, or null if none
3734 <         */
3735 <        public K reduceKeys(BiFun<? super K, ? super K, ? extends K> reducer) {
3736 <            return fjp.invoke(ForkJoinTasks.reduceKeys
3737 <                              (ConcurrentHashMapV8.this, reducer));
3727 >    /**
3728 >     * Returns a non-null result from applying the given search
3729 >     * function on each value, or null if none.
3730 >     *
3731 >     * @param searchFunction a function returning a non-null
3732 >     * result on success, else null
3733 >     * @return a non-null result from applying the given search
3734 >     * function on each value, or null if none
3735 >     */
3736 >    public <U> U searchValuesSequentially
3737 >        (Fun<? super V, ? extends U> searchFunction) {
3738 >        if (searchFunction == null) throw new NullPointerException();
3739 >        Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
3740 >        V v; U u;
3741 >        while ((v = it.advance()) != null) {
3742 >            if ((u = searchFunction.apply(v)) != null)
3743 >                return u;
3744          }
3745 +        return null;
3746 +    }
3747  
3748 <        /**
3749 <         * Returns the result of accumulating the given transformation
3750 <         * of all keys using the given reducer to combine values, or
3751 <         * null if none.
3752 <         *
3753 <         * @param transformer a function returning the transformation
3754 <         * for an element, or null of there is no transformation (in
3755 <         * which case it is not combined).
3756 <         * @param reducer a commutative associative combining function
3757 <         * @return the result of accumulating the given transformation
3758 <         * of all keys
3759 <         */
3760 <        public <U> U reduceKeys(Fun<? super K, ? extends U> transformer,
3761 <                                BiFun<? super U, ? super U, ? extends U> reducer) {
3762 <            return fjp.invoke(ForkJoinTasks.reduceKeys
3763 <                              (ConcurrentHashMapV8.this, transformer, reducer));
3748 >    /**
3749 >     * Returns the result of accumulating all values using the
3750 >     * given reducer to combine values, or null if none.
3751 >     *
3752 >     * @param reducer a commutative associative combining function
3753 >     * @return  the result of accumulating all values
3754 >     */
3755 >    public V reduceValuesSequentially
3756 >        (BiFun<? super V, ? super V, ? extends V> reducer) {
3757 >        if (reducer == null) throw new NullPointerException();
3758 >        Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
3759 >        V r = null; V v;
3760 >        while ((v = it.advance()) != null)
3761 >            r = (r == null) ? v : reducer.apply(r, v);
3762 >        return r;
3763 >    }
3764 >
3765 >    /**
3766 >     * Returns the result of accumulating the given transformation
3767 >     * of all values using the given reducer to combine values, or
3768 >     * null if none.
3769 >     *
3770 >     * @param transformer a function returning the transformation
3771 >     * for an element, or null of there is no transformation (in
3772 >     * which case it is not combined).
3773 >     * @param reducer a commutative associative combining function
3774 >     * @return the result of accumulating the given transformation
3775 >     * of all values
3776 >     */
3777 >    public <U> U reduceValuesSequentially
3778 >        (Fun<? super V, ? extends U> transformer,
3779 >         BiFun<? super U, ? super U, ? extends U> reducer) {
3780 >        if (transformer == null || reducer == null)
3781 >            throw new NullPointerException();
3782 >        Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
3783 >        U r = null, u; V v;
3784 >        while ((v = it.advance()) != null) {
3785 >            if ((u = transformer.apply(v)) != null)
3786 >                r = (r == null) ? u : reducer.apply(r, u);
3787          }
3788 +        return r;
3789 +    }
3790  
3791 <        /**
3792 <         * Returns the result of accumulating the given transformation
3793 <         * of all keys using the given reducer to combine values, and
3794 <         * the given basis as an identity value.
3795 <         *
3796 <         * @param transformer a function returning the transformation
3797 <         * for an element
3798 <         * @param basis the identity (initial default value) for the reduction
3799 <         * @param reducer a commutative associative combining function
3800 <         * @return  the result of accumulating the given transformation
3801 <         * of all keys
3802 <         */
3803 <        public double reduceKeysToDouble(ObjectToDouble<? super K> transformer,
3804 <                                         double basis,
3805 <                                         DoubleByDoubleToDouble reducer) {
3806 <            return fjp.invoke(ForkJoinTasks.reduceKeysToDouble
3807 <                              (ConcurrentHashMapV8.this, transformer, basis, reducer));
3791 >    /**
3792 >     * Returns the result of accumulating the given transformation
3793 >     * of all values using the given reducer to combine values,
3794 >     * and the given basis as an identity value.
3795 >     *
3796 >     * @param transformer a function returning the transformation
3797 >     * for an element
3798 >     * @param basis the identity (initial default value) for the reduction
3799 >     * @param reducer a commutative associative combining function
3800 >     * @return the result of accumulating the given transformation
3801 >     * of all values
3802 >     */
3803 >    public double reduceValuesToDoubleSequentially
3804 >        (ObjectToDouble<? super V> transformer,
3805 >         double basis,
3806 >         DoubleByDoubleToDouble reducer) {
3807 >        if (transformer == null || reducer == null)
3808 >            throw new NullPointerException();
3809 >        Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
3810 >        double r = basis; V v;
3811 >        while ((v = it.advance()) != null)
3812 >            r = reducer.apply(r, transformer.apply(v));
3813 >        return r;
3814 >    }
3815 >
3816 >    /**
3817 >     * Returns the result of accumulating the given transformation
3818 >     * of all values using the given reducer to combine values,
3819 >     * and the given basis as an identity value.
3820 >     *
3821 >     * @param transformer a function returning the transformation
3822 >     * for an element
3823 >     * @param basis the identity (initial default value) for the reduction
3824 >     * @param reducer a commutative associative combining function
3825 >     * @return the result of accumulating the given transformation
3826 >     * of all values
3827 >     */
3828 >    public long reduceValuesToLongSequentially
3829 >        (ObjectToLong<? super V> transformer,
3830 >         long basis,
3831 >         LongByLongToLong reducer) {
3832 >        if (transformer == null || reducer == null)
3833 >            throw new NullPointerException();
3834 >        Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
3835 >        long r = basis; V v;
3836 >        while ((v = it.advance()) != null)
3837 >            r = reducer.apply(r, transformer.apply(v));
3838 >        return r;
3839 >    }
3840 >
3841 >    /**
3842 >     * Returns the result of accumulating the given transformation
3843 >     * of all values using the given reducer to combine values,
3844 >     * and the given basis as an identity value.
3845 >     *
3846 >     * @param transformer a function returning the transformation
3847 >     * for an element
3848 >     * @param basis the identity (initial default value) for the reduction
3849 >     * @param reducer a commutative associative combining function
3850 >     * @return the result of accumulating the given transformation
3851 >     * of all values
3852 >     */
3853 >    public int reduceValuesToIntSequentially
3854 >        (ObjectToInt<? super V> transformer,
3855 >         int basis,
3856 >         IntByIntToInt reducer) {
3857 >        if (transformer == null || reducer == null)
3858 >            throw new NullPointerException();
3859 >        Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
3860 >        int r = basis; V v;
3861 >        while ((v = it.advance()) != null)
3862 >            r = reducer.apply(r, transformer.apply(v));
3863 >        return r;
3864 >    }
3865 >
3866 >    /**
3867 >     * Performs the given action for each entry.
3868 >     *
3869 >     * @param action the action
3870 >     */
3871 >    @SuppressWarnings("unchecked") public void forEachEntrySequentially
3872 >        (Action<Map.Entry<K,V>> action) {
3873 >        if (action == null) throw new NullPointerException();
3874 >        Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
3875 >        V v;
3876 >        while ((v = it.advance()) != null)
3877 >            action.apply(entryFor((K)it.nextKey, v));
3878 >    }
3879 >
3880 >    /**
3881 >     * Performs the given action for each non-null transformation
3882 >     * of each entry.
3883 >     *
3884 >     * @param transformer a function returning the transformation
3885 >     * for an element, or null of there is no transformation (in
3886 >     * which case the action is not applied).
3887 >     * @param action the action
3888 >     */
3889 >    @SuppressWarnings("unchecked") public <U> void forEachEntrySequentially
3890 >        (Fun<Map.Entry<K,V>, ? extends U> transformer,
3891 >         Action<U> action) {
3892 >        if (transformer == null || action == null)
3893 >            throw new NullPointerException();
3894 >        Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
3895 >        V v; U u;
3896 >        while ((v = it.advance()) != null) {
3897 >            if ((u = transformer.apply(entryFor((K)it.nextKey, v))) != null)
3898 >                action.apply(u);
3899          }
3900 +    }
3901  
3902 <        /**
3903 <         * Returns the result of accumulating the given transformation
3904 <         * of all keys using the given reducer to combine values, and
3905 <         * the given basis as an identity value.
3906 <         *
3907 <         * @param transformer a function returning the transformation
3908 <         * for an element
3909 <         * @param basis the identity (initial default value) for the reduction
3910 <         * @param reducer a commutative associative combining function
3911 <         * @return the result of accumulating the given transformation
3912 <         * of all keys
3913 <         */
3914 <        public long reduceKeysToLong(ObjectToLong<? super K> transformer,
3915 <                                     long basis,
3916 <                                     LongByLongToLong reducer) {
3917 <            return fjp.invoke(ForkJoinTasks.reduceKeysToLong
3918 <                              (ConcurrentHashMapV8.this, transformer, basis, reducer));
3902 >    /**
3903 >     * Returns a non-null result from applying the given search
3904 >     * function on each entry, or null if none.
3905 >     *
3906 >     * @param searchFunction a function returning a non-null
3907 >     * result on success, else null
3908 >     * @return a non-null result from applying the given search
3909 >     * function on each entry, or null if none
3910 >     */
3911 >    @SuppressWarnings("unchecked") public <U> U searchEntriesSequentially
3912 >        (Fun<Map.Entry<K,V>, ? extends U> searchFunction) {
3913 >        if (searchFunction == null) throw new NullPointerException();
3914 >        Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
3915 >        V v; U u;
3916 >        while ((v = it.advance()) != null) {
3917 >            if ((u = searchFunction.apply(entryFor((K)it.nextKey, v))) != null)
3918 >                return u;
3919          }
3920 +        return null;
3921 +    }
3922  
3923 <        /**
3924 <         * Returns the result of accumulating the given transformation
3925 <         * of all keys using the given reducer to combine values, and
3926 <         * the given basis as an identity value.
3927 <         *
3928 <         * @param transformer a function returning the transformation
3929 <         * for an element
3930 <         * @param basis the identity (initial default value) for the reduction
3931 <         * @param reducer a commutative associative combining function
3932 <         * @return the result of accumulating the given transformation
3933 <         * of all keys
3934 <         */
3935 <        public int reduceKeysToInt(ObjectToInt<? super K> transformer,
3936 <                                   int basis,
3937 <                                   IntByIntToInt reducer) {
3876 <            return fjp.invoke(ForkJoinTasks.reduceKeysToInt
3877 <                              (ConcurrentHashMapV8.this, transformer, basis, reducer));
3923 >    /**
3924 >     * Returns the result of accumulating all entries using the
3925 >     * given reducer to combine values, or null if none.
3926 >     *
3927 >     * @param reducer a commutative associative combining function
3928 >     * @return the result of accumulating all entries
3929 >     */
3930 >    @SuppressWarnings("unchecked") public Map.Entry<K,V> reduceEntriesSequentially
3931 >        (BiFun<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer) {
3932 >        if (reducer == null) throw new NullPointerException();
3933 >        Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
3934 >        Map.Entry<K,V> r = null; V v;
3935 >        while ((v = it.advance()) != null) {
3936 >            Map.Entry<K,V> u = entryFor((K)it.nextKey, v);
3937 >            r = (r == null) ? u : reducer.apply(r, u);
3938          }
3939 +        return r;
3940 +    }
3941  
3942 <        /**
3943 <         * Performs the given action for each value.
3944 <         *
3945 <         * @param action the action
3946 <         */
3947 <        public void forEachValue(Action<V> action) {
3948 <            fjp.invoke(ForkJoinTasks.forEachValue
3949 <                       (ConcurrentHashMapV8.this, action));
3942 >    /**
3943 >     * Returns the result of accumulating the given transformation
3944 >     * of all entries using the given reducer to combine values,
3945 >     * or null if none.
3946 >     *
3947 >     * @param transformer a function returning the transformation
3948 >     * for an element, or null of there is no transformation (in
3949 >     * which case it is not combined).
3950 >     * @param reducer a commutative associative combining function
3951 >     * @return the result of accumulating the given transformation
3952 >     * of all entries
3953 >     */
3954 >    @SuppressWarnings("unchecked") public <U> U reduceEntriesSequentially
3955 >        (Fun<Map.Entry<K,V>, ? extends U> transformer,
3956 >         BiFun<? super U, ? super U, ? extends U> reducer) {
3957 >        if (transformer == null || reducer == null)
3958 >            throw new NullPointerException();
3959 >        Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
3960 >        U r = null, u; V v;
3961 >        while ((v = it.advance()) != null) {
3962 >            if ((u = transformer.apply(entryFor((K)it.nextKey, v))) != null)
3963 >                r = (r == null) ? u : reducer.apply(r, u);
3964          }
3965 +        return r;
3966 +    }
3967 +
3968 +    /**
3969 +     * Returns the result of accumulating the given transformation
3970 +     * of all entries using the given reducer to combine values,
3971 +     * and the given basis as an identity value.
3972 +     *
3973 +     * @param transformer a function returning the transformation
3974 +     * for an element
3975 +     * @param basis the identity (initial default value) for the reduction
3976 +     * @param reducer a commutative associative combining function
3977 +     * @return the result of accumulating the given transformation
3978 +     * of all entries
3979 +     */
3980 +    @SuppressWarnings("unchecked") public double reduceEntriesToDoubleSequentially
3981 +        (ObjectToDouble<Map.Entry<K,V>> transformer,
3982 +         double basis,
3983 +         DoubleByDoubleToDouble reducer) {
3984 +        if (transformer == null || reducer == null)
3985 +            throw new NullPointerException();
3986 +        Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
3987 +        double r = basis; V v;
3988 +        while ((v = it.advance()) != null)
3989 +            r = reducer.apply(r, transformer.apply(entryFor((K)it.nextKey, v)));
3990 +        return r;
3991 +    }
3992 +
3993 +    /**
3994 +     * Returns the result of accumulating the given transformation
3995 +     * of all entries using the given reducer to combine values,
3996 +     * and the given basis as an identity value.
3997 +     *
3998 +     * @param transformer a function returning the transformation
3999 +     * for an element
4000 +     * @param basis the identity (initial default value) for the reduction
4001 +     * @param reducer a commutative associative combining function
4002 +     * @return  the result of accumulating the given transformation
4003 +     * of all entries
4004 +     */
4005 +    @SuppressWarnings("unchecked") public long reduceEntriesToLongSequentially
4006 +        (ObjectToLong<Map.Entry<K,V>> transformer,
4007 +         long basis,
4008 +         LongByLongToLong reducer) {
4009 +        if (transformer == null || reducer == null)
4010 +            throw new NullPointerException();
4011 +        Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
4012 +        long r = basis; V v;
4013 +        while ((v = it.advance()) != null)
4014 +            r = reducer.apply(r, transformer.apply(entryFor((K)it.nextKey, v)));
4015 +        return r;
4016 +    }
4017 +
4018 +    /**
4019 +     * Returns the result of accumulating the given transformation
4020 +     * of all entries using the given reducer to combine values,
4021 +     * and the given basis as an identity value.
4022 +     *
4023 +     * @param transformer a function returning the transformation
4024 +     * for an element
4025 +     * @param basis the identity (initial default value) for the reduction
4026 +     * @param reducer a commutative associative combining function
4027 +     * @return the result of accumulating the given transformation
4028 +     * of all entries
4029 +     */
4030 +    @SuppressWarnings("unchecked") public int reduceEntriesToIntSequentially
4031 +        (ObjectToInt<Map.Entry<K,V>> transformer,
4032 +         int basis,
4033 +         IntByIntToInt reducer) {
4034 +        if (transformer == null || reducer == null)
4035 +            throw new NullPointerException();
4036 +        Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
4037 +        int r = basis; V v;
4038 +        while ((v = it.advance()) != null)
4039 +            r = reducer.apply(r, transformer.apply(entryFor((K)it.nextKey, v)));
4040 +        return r;
4041 +    }
4042 +
4043 +    // Parallel bulk operations
4044 +
4045 +    /**
4046 +     * Performs the given action for each (key, value).
4047 +     *
4048 +     * @param action the action
4049 +     */
4050 +    public void forEachInParallel(BiAction<K,V> action) {
4051 +        ForkJoinTasks.forEach
4052 +            (this, action).invoke();
4053 +    }
4054 +
4055 +    /**
4056 +     * Performs the given action for each non-null transformation
4057 +     * of each (key, value).
4058 +     *
4059 +     * @param transformer a function returning the transformation
4060 +     * for an element, or null of there is no transformation (in
4061 +     * which case the action is not applied).
4062 +     * @param action the action
4063 +     */
4064 +    public <U> void forEachInParallel
4065 +        (BiFun<? super K, ? super V, ? extends U> transformer,
4066 +                            Action<U> action) {
4067 +        ForkJoinTasks.forEach
4068 +            (this, transformer, action).invoke();
4069 +    }
4070 +
4071 +    /**
4072 +     * Returns a non-null result from applying the given search
4073 +     * function on each (key, value), or null if none.  Upon
4074 +     * success, further element processing is suppressed and the
4075 +     * results of any other parallel invocations of the search
4076 +     * function are ignored.
4077 +     *
4078 +     * @param searchFunction a function returning a non-null
4079 +     * result on success, else null
4080 +     * @return a non-null result from applying the given search
4081 +     * function on each (key, value), or null if none
4082 +     */
4083 +    public <U> U searchInParallel
4084 +        (BiFun<? super K, ? super V, ? extends U> searchFunction) {
4085 +        return ForkJoinTasks.search
4086 +            (this, searchFunction).invoke();
4087 +    }
4088 +
4089 +    /**
4090 +     * Returns the result of accumulating the given transformation
4091 +     * of all (key, value) pairs using the given reducer to
4092 +     * combine values, or null if none.
4093 +     *
4094 +     * @param transformer a function returning the transformation
4095 +     * for an element, or null of there is no transformation (in
4096 +     * which case it is not combined).
4097 +     * @param reducer a commutative associative combining function
4098 +     * @return the result of accumulating the given transformation
4099 +     * of all (key, value) pairs
4100 +     */
4101 +    public <U> U reduceInParallel
4102 +        (BiFun<? super K, ? super V, ? extends U> transformer,
4103 +         BiFun<? super U, ? super U, ? extends U> reducer) {
4104 +        return ForkJoinTasks.reduce
4105 +            (this, transformer, reducer).invoke();
4106 +    }
4107 +
4108 +    /**
4109 +     * Returns the result of accumulating the given transformation
4110 +     * of all (key, value) pairs using the given reducer to
4111 +     * combine values, and the given basis as an identity value.
4112 +     *
4113 +     * @param transformer a function returning the transformation
4114 +     * for an element
4115 +     * @param basis the identity (initial default value) for the reduction
4116 +     * @param reducer a commutative associative combining function
4117 +     * @return the result of accumulating the given transformation
4118 +     * of all (key, value) pairs
4119 +     */
4120 +    public double reduceToDoubleInParallel
4121 +        (ObjectByObjectToDouble<? super K, ? super V> transformer,
4122 +         double basis,
4123 +         DoubleByDoubleToDouble reducer) {
4124 +        return ForkJoinTasks.reduceToDouble
4125 +            (this, transformer, basis, reducer).invoke();
4126 +    }
4127 +
4128 +    /**
4129 +     * Returns the result of accumulating the given transformation
4130 +     * of all (key, value) pairs using the given reducer to
4131 +     * combine values, and the given basis as an identity value.
4132 +     *
4133 +     * @param transformer a function returning the transformation
4134 +     * for an element
4135 +     * @param basis the identity (initial default value) for the reduction
4136 +     * @param reducer a commutative associative combining function
4137 +     * @return the result of accumulating the given transformation
4138 +     * of all (key, value) pairs
4139 +     */
4140 +    public long reduceToLongInParallel
4141 +        (ObjectByObjectToLong<? super K, ? super V> transformer,
4142 +         long basis,
4143 +         LongByLongToLong reducer) {
4144 +        return ForkJoinTasks.reduceToLong
4145 +            (this, transformer, basis, reducer).invoke();
4146 +    }
4147 +
4148 +    /**
4149 +     * Returns the result of accumulating the given transformation
4150 +     * of all (key, value) pairs using the given reducer to
4151 +     * combine values, and the given basis as an identity value.
4152 +     *
4153 +     * @param transformer a function returning the transformation
4154 +     * for an element
4155 +     * @param basis the identity (initial default value) for the reduction
4156 +     * @param reducer a commutative associative combining function
4157 +     * @return the result of accumulating the given transformation
4158 +     * of all (key, value) pairs
4159 +     */
4160 +    public int reduceToIntInParallel
4161 +        (ObjectByObjectToInt<? super K, ? super V> transformer,
4162 +         int basis,
4163 +         IntByIntToInt reducer) {
4164 +        return ForkJoinTasks.reduceToInt
4165 +            (this, transformer, basis, reducer).invoke();
4166 +    }
4167 +
4168 +    /**
4169 +     * Performs the given action for each key.
4170 +     *
4171 +     * @param action the action
4172 +     */
4173 +    public void forEachKeyInParallel(Action<K> action) {
4174 +        ForkJoinTasks.forEachKey
4175 +            (this, action).invoke();
4176 +    }
4177 +
4178 +    /**
4179 +     * Performs the given action for each non-null transformation
4180 +     * of each key.
4181 +     *
4182 +     * @param transformer a function returning the transformation
4183 +     * for an element, or null of there is no transformation (in
4184 +     * which case the action is not applied).
4185 +     * @param action the action
4186 +     */
4187 +    public <U> void forEachKeyInParallel
4188 +        (Fun<? super K, ? extends U> transformer,
4189 +         Action<U> action) {
4190 +        ForkJoinTasks.forEachKey
4191 +            (this, transformer, action).invoke();
4192 +    }
4193 +
4194 +    /**
4195 +     * Returns a non-null result from applying the given search
4196 +     * function on each key, or null if none. Upon success,
4197 +     * further element processing is suppressed and the results of
4198 +     * any other parallel invocations of the search function are
4199 +     * ignored.
4200 +     *
4201 +     * @param searchFunction a function returning a non-null
4202 +     * result on success, else null
4203 +     * @return a non-null result from applying the given search
4204 +     * function on each key, or null if none
4205 +     */
4206 +    public <U> U searchKeysInParallel
4207 +        (Fun<? super K, ? extends U> searchFunction) {
4208 +        return ForkJoinTasks.searchKeys
4209 +            (this, searchFunction).invoke();
4210 +    }
4211 +
4212 +    /**
4213 +     * Returns the result of accumulating all keys using the given
4214 +     * reducer to combine values, or null if none.
4215 +     *
4216 +     * @param reducer a commutative associative combining function
4217 +     * @return the result of accumulating all keys using the given
4218 +     * reducer to combine values, or null if none
4219 +     */
4220 +    public K reduceKeysInParallel
4221 +        (BiFun<? super K, ? super K, ? extends K> reducer) {
4222 +        return ForkJoinTasks.reduceKeys
4223 +            (this, reducer).invoke();
4224 +    }
4225 +
4226 +    /**
4227 +     * Returns the result of accumulating the given transformation
4228 +     * of all keys using the given reducer to combine values, or
4229 +     * null if none.
4230 +     *
4231 +     * @param transformer a function returning the transformation
4232 +     * for an element, or null of there is no transformation (in
4233 +     * which case it is not combined).
4234 +     * @param reducer a commutative associative combining function
4235 +     * @return the result of accumulating the given transformation
4236 +     * of all keys
4237 +     */
4238 +    public <U> U reduceKeysInParallel
4239 +        (Fun<? super K, ? extends U> transformer,
4240 +         BiFun<? super U, ? super U, ? extends U> reducer) {
4241 +        return ForkJoinTasks.reduceKeys
4242 +            (this, transformer, reducer).invoke();
4243 +    }
4244 +
4245 +    /**
4246 +     * Returns the result of accumulating the given transformation
4247 +     * of all keys using the given reducer to combine values, and
4248 +     * the given basis as an identity value.
4249 +     *
4250 +     * @param transformer a function returning the transformation
4251 +     * for an element
4252 +     * @param basis the identity (initial default value) for the reduction
4253 +     * @param reducer a commutative associative combining function
4254 +     * @return  the result of accumulating the given transformation
4255 +     * of all keys
4256 +     */
4257 +    public double reduceKeysToDoubleInParallel
4258 +        (ObjectToDouble<? super K> transformer,
4259 +         double basis,
4260 +         DoubleByDoubleToDouble reducer) {
4261 +        return ForkJoinTasks.reduceKeysToDouble
4262 +            (this, transformer, basis, reducer).invoke();
4263 +    }
4264 +
4265 +    /**
4266 +     * Returns the result of accumulating the given transformation
4267 +     * of all keys using the given reducer to combine values, and
4268 +     * the given basis as an identity value.
4269 +     *
4270 +     * @param transformer a function returning the transformation
4271 +     * for an element
4272 +     * @param basis the identity (initial default value) for the reduction
4273 +     * @param reducer a commutative associative combining function
4274 +     * @return the result of accumulating the given transformation
4275 +     * of all keys
4276 +     */
4277 +    public long reduceKeysToLongInParallel
4278 +        (ObjectToLong<? super K> transformer,
4279 +         long basis,
4280 +         LongByLongToLong reducer) {
4281 +        return ForkJoinTasks.reduceKeysToLong
4282 +            (this, transformer, basis, reducer).invoke();
4283 +    }
4284 +
4285 +    /**
4286 +     * Returns the result of accumulating the given transformation
4287 +     * of all keys using the given reducer to combine values, and
4288 +     * the given basis as an identity value.
4289 +     *
4290 +     * @param transformer a function returning the transformation
4291 +     * for an element
4292 +     * @param basis the identity (initial default value) for the reduction
4293 +     * @param reducer a commutative associative combining function
4294 +     * @return the result of accumulating the given transformation
4295 +     * of all keys
4296 +     */
4297 +    public int reduceKeysToIntInParallel
4298 +        (ObjectToInt<? super K> transformer,
4299 +         int basis,
4300 +         IntByIntToInt reducer) {
4301 +        return ForkJoinTasks.reduceKeysToInt
4302 +            (this, transformer, basis, reducer).invoke();
4303 +    }
4304 +
4305 +    /**
4306 +     * Performs the given action for each value.
4307 +     *
4308 +     * @param action the action
4309 +     */
4310 +    public void forEachValueInParallel(Action<V> action) {
4311 +        ForkJoinTasks.forEachValue
4312 +            (this, action).invoke();
4313 +    }
4314 +
4315 +    /**
4316 +     * Performs the given action for each non-null transformation
4317 +     * of each value.
4318 +     *
4319 +     * @param transformer a function returning the transformation
4320 +     * for an element, or null of there is no transformation (in
4321 +     * which case the action is not applied).
4322 +     */
4323 +    public <U> void forEachValueInParallel
4324 +        (Fun<? super V, ? extends U> transformer,
4325 +         Action<U> action) {
4326 +        ForkJoinTasks.forEachValue
4327 +            (this, transformer, action).invoke();
4328 +    }
4329 +
4330 +    /**
4331 +     * Returns a non-null result from applying the given search
4332 +     * function on each value, or null if none.  Upon success,
4333 +     * further element processing is suppressed and the results of
4334 +     * any other parallel invocations of the search function are
4335 +     * ignored.
4336 +     *
4337 +     * @param searchFunction a function returning a non-null
4338 +     * result on success, else null
4339 +     * @return a non-null result from applying the given search
4340 +     * function on each value, or null if none
4341 +     */
4342 +    public <U> U searchValuesInParallel
4343 +        (Fun<? super V, ? extends U> searchFunction) {
4344 +        return ForkJoinTasks.searchValues
4345 +            (this, searchFunction).invoke();
4346 +    }
4347 +
4348 +    /**
4349 +     * Returns the result of accumulating all values using the
4350 +     * given reducer to combine values, or null if none.
4351 +     *
4352 +     * @param reducer a commutative associative combining function
4353 +     * @return  the result of accumulating all values
4354 +     */
4355 +    public V reduceValuesInParallel
4356 +        (BiFun<? super V, ? super V, ? extends V> reducer) {
4357 +        return ForkJoinTasks.reduceValues
4358 +            (this, reducer).invoke();
4359 +    }
4360 +
4361 +    /**
4362 +     * Returns the result of accumulating the given transformation
4363 +     * of all values using the given reducer to combine values, or
4364 +     * null if none.
4365 +     *
4366 +     * @param transformer a function returning the transformation
4367 +     * for an element, or null of there is no transformation (in
4368 +     * which case it is not combined).
4369 +     * @param reducer a commutative associative combining function
4370 +     * @return the result of accumulating the given transformation
4371 +     * of all values
4372 +     */
4373 +    public <U> U reduceValuesInParallel
4374 +        (Fun<? super V, ? extends U> transformer,
4375 +         BiFun<? super U, ? super U, ? extends U> reducer) {
4376 +        return ForkJoinTasks.reduceValues
4377 +            (this, transformer, reducer).invoke();
4378 +    }
4379 +
4380 +    /**
4381 +     * Returns the result of accumulating the given transformation
4382 +     * of all values using the given reducer to combine values,
4383 +     * and the given basis as an identity value.
4384 +     *
4385 +     * @param transformer a function returning the transformation
4386 +     * for an element
4387 +     * @param basis the identity (initial default value) for the reduction
4388 +     * @param reducer a commutative associative combining function
4389 +     * @return the result of accumulating the given transformation
4390 +     * of all values
4391 +     */
4392 +    public double reduceValuesToDoubleInParallel
4393 +        (ObjectToDouble<? super V> transformer,
4394 +         double basis,
4395 +         DoubleByDoubleToDouble reducer) {
4396 +        return ForkJoinTasks.reduceValuesToDouble
4397 +            (this, transformer, basis, reducer).invoke();
4398 +    }
4399 +
4400 +    /**
4401 +     * Returns the result of accumulating the given transformation
4402 +     * of all values using the given reducer to combine values,
4403 +     * and the given basis as an identity value.
4404 +     *
4405 +     * @param transformer a function returning the transformation
4406 +     * for an element
4407 +     * @param basis the identity (initial default value) for the reduction
4408 +     * @param reducer a commutative associative combining function
4409 +     * @return the result of accumulating the given transformation
4410 +     * of all values
4411 +     */
4412 +    public long reduceValuesToLongInParallel
4413 +        (ObjectToLong<? super V> transformer,
4414 +         long basis,
4415 +         LongByLongToLong reducer) {
4416 +        return ForkJoinTasks.reduceValuesToLong
4417 +            (this, transformer, basis, reducer).invoke();
4418 +    }
4419 +
4420 +    /**
4421 +     * Returns the result of accumulating the given transformation
4422 +     * of all values using the given reducer to combine values,
4423 +     * and the given basis as an identity value.
4424 +     *
4425 +     * @param transformer a function returning the transformation
4426 +     * for an element
4427 +     * @param basis the identity (initial default value) for the reduction
4428 +     * @param reducer a commutative associative combining function
4429 +     * @return the result of accumulating the given transformation
4430 +     * of all values
4431 +     */
4432 +    public int reduceValuesToIntInParallel
4433 +        (ObjectToInt<? super V> transformer,
4434 +         int basis,
4435 +         IntByIntToInt reducer) {
4436 +        return ForkJoinTasks.reduceValuesToInt
4437 +            (this, transformer, basis, reducer).invoke();
4438 +    }
4439 +
4440 +    /**
4441 +     * Performs the given action for each entry.
4442 +     *
4443 +     * @param action the action
4444 +     */
4445 +    public void forEachEntryInParallel(Action<Map.Entry<K,V>> action) {
4446 +        ForkJoinTasks.forEachEntry
4447 +            (this, action).invoke();
4448 +    }
4449 +
4450 +    /**
4451 +     * Performs the given action for each non-null transformation
4452 +     * of each entry.
4453 +     *
4454 +     * @param transformer a function returning the transformation
4455 +     * for an element, or null of there is no transformation (in
4456 +     * which case the action is not applied).
4457 +     * @param action the action
4458 +     */
4459 +    public <U> void forEachEntryInParallel
4460 +        (Fun<Map.Entry<K,V>, ? extends U> transformer,
4461 +         Action<U> action) {
4462 +        ForkJoinTasks.forEachEntry
4463 +            (this, transformer, action).invoke();
4464 +    }
4465 +
4466 +    /**
4467 +     * Returns a non-null result from applying the given search
4468 +     * function on each entry, or null if none.  Upon success,
4469 +     * further element processing is suppressed and the results of
4470 +     * any other parallel invocations of the search function are
4471 +     * ignored.
4472 +     *
4473 +     * @param searchFunction a function returning a non-null
4474 +     * result on success, else null
4475 +     * @return a non-null result from applying the given search
4476 +     * function on each entry, or null if none
4477 +     */
4478 +    public <U> U searchEntriesInParallel
4479 +        (Fun<Map.Entry<K,V>, ? extends U> searchFunction) {
4480 +        return ForkJoinTasks.searchEntries
4481 +            (this, searchFunction).invoke();
4482 +    }
4483 +
4484 +    /**
4485 +     * Returns the result of accumulating all entries using the
4486 +     * given reducer to combine values, or null if none.
4487 +     *
4488 +     * @param reducer a commutative associative combining function
4489 +     * @return the result of accumulating all entries
4490 +     */
4491 +    public Map.Entry<K,V> reduceEntriesInParallel
4492 +        (BiFun<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer) {
4493 +        return ForkJoinTasks.reduceEntries
4494 +            (this, reducer).invoke();
4495 +    }
4496 +
4497 +    /**
4498 +     * Returns the result of accumulating the given transformation
4499 +     * of all entries using the given reducer to combine values,
4500 +     * or null if none.
4501 +     *
4502 +     * @param transformer a function returning the transformation
4503 +     * for an element, or null of there is no transformation (in
4504 +     * which case it is not combined).
4505 +     * @param reducer a commutative associative combining function
4506 +     * @return the result of accumulating the given transformation
4507 +     * of all entries
4508 +     */
4509 +    public <U> U reduceEntriesInParallel
4510 +        (Fun<Map.Entry<K,V>, ? extends U> transformer,
4511 +         BiFun<? super U, ? super U, ? extends U> reducer) {
4512 +        return ForkJoinTasks.reduceEntries
4513 +            (this, transformer, reducer).invoke();
4514 +    }
4515 +
4516 +    /**
4517 +     * Returns the result of accumulating the given transformation
4518 +     * of all entries using the given reducer to combine values,
4519 +     * and the given basis as an identity value.
4520 +     *
4521 +     * @param transformer a function returning the transformation
4522 +     * for an element
4523 +     * @param basis the identity (initial default value) for the reduction
4524 +     * @param reducer a commutative associative combining function
4525 +     * @return the result of accumulating the given transformation
4526 +     * of all entries
4527 +     */
4528 +    public double reduceEntriesToDoubleInParallel
4529 +        (ObjectToDouble<Map.Entry<K,V>> transformer,
4530 +         double basis,
4531 +         DoubleByDoubleToDouble reducer) {
4532 +        return ForkJoinTasks.reduceEntriesToDouble
4533 +            (this, transformer, basis, reducer).invoke();
4534 +    }
4535 +
4536 +    /**
4537 +     * Returns the result of accumulating the given transformation
4538 +     * of all entries using the given reducer to combine values,
4539 +     * and the given basis as an identity value.
4540 +     *
4541 +     * @param transformer a function returning the transformation
4542 +     * for an element
4543 +     * @param basis the identity (initial default value) for the reduction
4544 +     * @param reducer a commutative associative combining function
4545 +     * @return  the result of accumulating the given transformation
4546 +     * of all entries
4547 +     */
4548 +    public long reduceEntriesToLongInParallel
4549 +        (ObjectToLong<Map.Entry<K,V>> transformer,
4550 +         long basis,
4551 +         LongByLongToLong reducer) {
4552 +        return ForkJoinTasks.reduceEntriesToLong
4553 +            (this, transformer, basis, reducer).invoke();
4554 +    }
4555 +
4556 +    /**
4557 +     * Returns the result of accumulating the given transformation
4558 +     * of all entries using the given reducer to combine values,
4559 +     * and the given basis as an identity value.
4560 +     *
4561 +     * @param transformer a function returning the transformation
4562 +     * for an element
4563 +     * @param basis the identity (initial default value) for the reduction
4564 +     * @param reducer a commutative associative combining function
4565 +     * @return the result of accumulating the given transformation
4566 +     * of all entries
4567 +     */
4568 +    public int reduceEntriesToIntInParallel
4569 +        (ObjectToInt<Map.Entry<K,V>> transformer,
4570 +         int basis,
4571 +         IntByIntToInt reducer) {
4572 +        return ForkJoinTasks.reduceEntriesToInt
4573 +            (this, transformer, basis, reducer).invoke();
4574 +    }
4575 +
4576 +
4577 +    /* ----------------Views -------------- */
4578 +
4579 +    /**
4580 +     * Base class for views.
4581 +     */
4582 +    abstract static class CHMView<K, V> {
4583 +        final ConcurrentHashMapV8<K, V> map;
4584 +        CHMView(ConcurrentHashMapV8<K, V> map)  { this.map = map; }
4585  
4586          /**
4587 <         * Performs the given action for each non-null transformation
3892 <         * of each value.
4587 >         * Returns the map backing this view.
4588           *
4589 <         * @param transformer a function returning the transformation
3895 <         * for an element, or null of there is no transformation (in
3896 <         * which case the action is not applied).
4589 >         * @return the map backing this view
4590           */
4591 <        public <U> void forEachValue(Fun<? super V, ? extends U> transformer,
4592 <                                     Action<U> action) {
4593 <            fjp.invoke(ForkJoinTasks.forEachValue
4594 <                       (ConcurrentHashMapV8.this, transformer, action));
4591 >        public ConcurrentHashMapV8<K,V> getMap() { return map; }
4592 >
4593 >        public final int size()                 { return map.size(); }
4594 >        public final boolean isEmpty()          { return map.isEmpty(); }
4595 >        public final void clear()               { map.clear(); }
4596 >
4597 >        // implementations below rely on concrete classes supplying these
4598 >        public abstract Iterator<?> iterator();
4599 >        public abstract boolean contains(Object o);
4600 >        public abstract boolean remove(Object o);
4601 >
4602 >        private static final String oomeMsg = "Required array size too large";
4603 >
4604 >        public final Object[] toArray() {
4605 >            long sz = map.mappingCount();
4606 >            if (sz > (long)(MAX_ARRAY_SIZE))
4607 >                throw new OutOfMemoryError(oomeMsg);
4608 >            int n = (int)sz;
4609 >            Object[] r = new Object[n];
4610 >            int i = 0;
4611 >            Iterator<?> it = iterator();
4612 >            while (it.hasNext()) {
4613 >                if (i == n) {
4614 >                    if (n >= MAX_ARRAY_SIZE)
4615 >                        throw new OutOfMemoryError(oomeMsg);
4616 >                    if (n >= MAX_ARRAY_SIZE - (MAX_ARRAY_SIZE >>> 1) - 1)
4617 >                        n = MAX_ARRAY_SIZE;
4618 >                    else
4619 >                        n += (n >>> 1) + 1;
4620 >                    r = Arrays.copyOf(r, n);
4621 >                }
4622 >                r[i++] = it.next();
4623 >            }
4624 >            return (i == n) ? r : Arrays.copyOf(r, i);
4625          }
4626  
4627 <        /**
4628 <         * Returns a non-null result from applying the given search
4629 <         * function on each value, or null if none.  Further element
4630 <         * processing is suppressed upon success. However, this method
4631 <         * does not return until other in-progress parallel
4632 <         * invocations of the search function also complete.
4633 <         *
4634 <         * @param searchFunction a function returning a non-null
4635 <         * result on success, else null
4636 <         * @return a non-null result from applying the given search
4637 <         * function on each value, or null if none
4638 <         *
4639 <         */
4640 <        public <U> U searchValues(Fun<? super V, ? extends U> searchFunction) {
4641 <            return fjp.invoke(ForkJoinTasks.searchValues
4642 <                              (ConcurrentHashMapV8.this, searchFunction));
4627 >        @SuppressWarnings("unchecked") public final <T> T[] toArray(T[] a) {
4628 >            long sz = map.mappingCount();
4629 >            if (sz > (long)(MAX_ARRAY_SIZE))
4630 >                throw new OutOfMemoryError(oomeMsg);
4631 >            int m = (int)sz;
4632 >            T[] r = (a.length >= m) ? a :
4633 >                (T[])java.lang.reflect.Array
4634 >                .newInstance(a.getClass().getComponentType(), m);
4635 >            int n = r.length;
4636 >            int i = 0;
4637 >            Iterator<?> it = iterator();
4638 >            while (it.hasNext()) {
4639 >                if (i == n) {
4640 >                    if (n >= MAX_ARRAY_SIZE)
4641 >                        throw new OutOfMemoryError(oomeMsg);
4642 >                    if (n >= MAX_ARRAY_SIZE - (MAX_ARRAY_SIZE >>> 1) - 1)
4643 >                        n = MAX_ARRAY_SIZE;
4644 >                    else
4645 >                        n += (n >>> 1) + 1;
4646 >                    r = Arrays.copyOf(r, n);
4647 >                }
4648 >                r[i++] = (T)it.next();
4649 >            }
4650 >            if (a == r && i < n) {
4651 >                r[i] = null; // null-terminate
4652 >                return r;
4653 >            }
4654 >            return (i == n) ? r : Arrays.copyOf(r, i);
4655          }
4656  
4657 <        /**
4658 <         * Returns the result of accumulating all values using the
4659 <         * given reducer to combine values, or null if none.
4660 <         *
4661 <         * @param reducer a commutative associative combining function
3927 <         * @return  the result of accumulating all values
3928 <         */
3929 <        public V reduceValues(BiFun<? super V, ? super V, ? extends V> reducer) {
3930 <            return fjp.invoke(ForkJoinTasks.reduceValues
3931 <                              (ConcurrentHashMapV8.this, reducer));
4657 >        public final int hashCode() {
4658 >            int h = 0;
4659 >            for (Iterator<?> it = iterator(); it.hasNext();)
4660 >                h += it.next().hashCode();
4661 >            return h;
4662          }
4663  
4664 <        /**
4665 <         * Returns the result of accumulating the given transformation
4666 <         * of all values using the given reducer to combine values, or
4667 <         * null if none.
4668 <         *
4669 <         * @param transformer a function returning the transformation
4670 <         * for an element, or null of there is no transformation (in
4671 <         * which case it is not combined).
4672 <         * @param reducer a commutative associative combining function
4673 <         * @return the result of accumulating the given transformation
4674 <         * of all values
4675 <         */
4676 <        public <U> U reduceValues(Fun<? super V, ? extends U> transformer,
4677 <                                  BiFun<? super U, ? super U, ? extends U> reducer) {
3948 <            return fjp.invoke(ForkJoinTasks.reduceValues
3949 <                              (ConcurrentHashMapV8.this, transformer, reducer));
4664 >        public final String toString() {
4665 >            StringBuilder sb = new StringBuilder();
4666 >            sb.append('[');
4667 >            Iterator<?> it = iterator();
4668 >            if (it.hasNext()) {
4669 >                for (;;) {
4670 >                    Object e = it.next();
4671 >                    sb.append(e == this ? "(this Collection)" : e);
4672 >                    if (!it.hasNext())
4673 >                        break;
4674 >                    sb.append(',').append(' ');
4675 >                }
4676 >            }
4677 >            return sb.append(']').toString();
4678          }
4679  
4680 <        /**
4681 <         * Returns the result of accumulating the given transformation
4682 <         * of all values using the given reducer to combine values,
4683 <         * and the given basis as an identity value.
4684 <         *
4685 <         * @param transformer a function returning the transformation
4686 <         * for an element
4687 <         * @param basis the identity (initial default value) for the reduction
4688 <         * @param reducer a commutative associative combining function
3961 <         * @return the result of accumulating the given transformation
3962 <         * of all values
3963 <         */
3964 <        public double reduceValuesToDouble(ObjectToDouble<? super V> transformer,
3965 <                                           double basis,
3966 <                                           DoubleByDoubleToDouble reducer) {
3967 <            return fjp.invoke(ForkJoinTasks.reduceValuesToDouble
3968 <                              (ConcurrentHashMapV8.this, transformer, basis, reducer));
4680 >        public final boolean containsAll(Collection<?> c) {
4681 >            if (c != this) {
4682 >                for (Iterator<?> it = c.iterator(); it.hasNext();) {
4683 >                    Object e = it.next();
4684 >                    if (e == null || !contains(e))
4685 >                        return false;
4686 >                }
4687 >            }
4688 >            return true;
4689          }
4690  
4691 <        /**
4692 <         * Returns the result of accumulating the given transformation
4693 <         * of all values using the given reducer to combine values,
4694 <         * and the given basis as an identity value.
4695 <         *
4696 <         * @param transformer a function returning the transformation
4697 <         * for an element
4698 <         * @param basis the identity (initial default value) for the reduction
4699 <         * @param reducer a commutative associative combining function
3980 <         * @return the result of accumulating the given transformation
3981 <         * of all values
3982 <         */
3983 <        public long reduceValuesToLong(ObjectToLong<? super V> transformer,
3984 <                                       long basis,
3985 <                                       LongByLongToLong reducer) {
3986 <            return fjp.invoke(ForkJoinTasks.reduceValuesToLong
3987 <                              (ConcurrentHashMapV8.this, transformer, basis, reducer));
4691 >        public final boolean removeAll(Collection<?> c) {
4692 >            boolean modified = false;
4693 >            for (Iterator<?> it = iterator(); it.hasNext();) {
4694 >                if (c.contains(it.next())) {
4695 >                    it.remove();
4696 >                    modified = true;
4697 >                }
4698 >            }
4699 >            return modified;
4700          }
4701  
4702 <        /**
4703 <         * Returns the result of accumulating the given transformation
4704 <         * of all values using the given reducer to combine values,
4705 <         * and the given basis as an identity value.
4706 <         *
4707 <         * @param transformer a function returning the transformation
4708 <         * for an element
4709 <         * @param basis the identity (initial default value) for the reduction
4710 <         * @param reducer a commutative associative combining function
3999 <         * @return the result of accumulating the given transformation
4000 <         * of all values
4001 <         */
4002 <        public int reduceValuesToInt(ObjectToInt<? super V> transformer,
4003 <                                     int basis,
4004 <                                     IntByIntToInt reducer) {
4005 <            return fjp.invoke(ForkJoinTasks.reduceValuesToInt
4006 <                              (ConcurrentHashMapV8.this, transformer, basis, reducer));
4702 >        public final boolean retainAll(Collection<?> c) {
4703 >            boolean modified = false;
4704 >            for (Iterator<?> it = iterator(); it.hasNext();) {
4705 >                if (!c.contains(it.next())) {
4706 >                    it.remove();
4707 >                    modified = true;
4708 >                }
4709 >            }
4710 >            return modified;
4711          }
4712  
4713 <        /**
4714 <         * Performs the given action for each entry.
4715 <         *
4716 <         * @param action the action
4717 <         */
4718 <        public void forEachEntry(Action<Map.Entry<K,V>> action) {
4719 <            fjp.invoke(ForkJoinTasks.forEachEntry
4720 <                       (ConcurrentHashMapV8.this, action));
4713 >    }
4714 >
4715 >    /**
4716 >     * A view of a ConcurrentHashMapV8 as a {@link Set} of keys, in
4717 >     * which additions may optionally be enabled by mapping to a
4718 >     * common value.  This class cannot be directly instantiated. See
4719 >     * {@link #keySet}, {@link #keySet(Object)}, {@link #newKeySet()},
4720 >     * {@link #newKeySet(int)}.
4721 >     */
4722 >    public static class KeySetView<K,V> extends CHMView<K,V>
4723 >        implements Set<K>, java.io.Serializable {
4724 >        private static final long serialVersionUID = 7249069246763182397L;
4725 >        private final V value;
4726 >        KeySetView(ConcurrentHashMapV8<K, V> map, V value) {  // non-public
4727 >            super(map);
4728 >            this.value = value;
4729          }
4730  
4731          /**
4732 <         * Performs the given action for each non-null transformation
4733 <         * of each entry.
4732 >         * Returns the default mapped value for additions,
4733 >         * or {@code null} if additions are not supported.
4734           *
4735 <         * @param transformer a function returning the transformation
4736 <         * for an element, or null of there is no transformation (in
4025 <         * which case the action is not applied).
4026 <         * @param action the action
4735 >         * @return the default mapped value for additions, or {@code null}
4736 >         * if not supported.
4737           */
4738 <        public <U> void forEachEntry(Fun<Map.Entry<K,V>, ? extends U> transformer,
4739 <                                     Action<U> action) {
4740 <            fjp.invoke(ForkJoinTasks.forEachEntry
4741 <                       (ConcurrentHashMapV8.this, transformer, action));
4742 <        }
4738 >        public V getMappedValue() { return value; }
4739 >
4740 >        // implement Set API
4741 >
4742 >        public boolean contains(Object o) { return map.containsKey(o); }
4743 >        public boolean remove(Object o)   { return map.remove(o) != null; }
4744  
4745          /**
4746 <         * Returns a non-null result from applying the given search
4747 <         * function on each entry, or null if none.  Further element
4748 <         * processing is suppressed upon success. However, this method
4749 <         * does not return until other in-progress parallel
4750 <         * invocations of the search function also complete.
4746 >         * Returns a "weakly consistent" iterator that will never
4747 >         * throw {@link ConcurrentModificationException}, and
4748 >         * guarantees to traverse elements as they existed upon
4749 >         * construction of the iterator, and may (but is not
4750 >         * guaranteed to) reflect any modifications subsequent to
4751 >         * construction.
4752           *
4753 <         * @param searchFunction a function returning a non-null
4042 <         * result on success, else null
4043 <         * @return a non-null result from applying the given search
4044 <         * function on each entry, or null if none
4753 >         * @return an iterator over the keys of this map
4754           */
4755 <        public <U> U searchEntries(Fun<Map.Entry<K,V>, ? extends U> searchFunction) {
4756 <            return fjp.invoke(ForkJoinTasks.searchEntries
4757 <                              (ConcurrentHashMapV8.this, searchFunction));
4755 >        public Iterator<K> iterator()     { return new KeyIterator<K,V>(map); }
4756 >        public boolean add(K e) {
4757 >            V v;
4758 >            if ((v = value) == null)
4759 >                throw new UnsupportedOperationException();
4760 >            if (e == null)
4761 >                throw new NullPointerException();
4762 >            return map.internalPut(e, v, true) == null;
4763 >        }
4764 >        public boolean addAll(Collection<? extends K> c) {
4765 >            boolean added = false;
4766 >            V v;
4767 >            if ((v = value) == null)
4768 >                throw new UnsupportedOperationException();
4769 >            for (K e : c) {
4770 >                if (e == null)
4771 >                    throw new NullPointerException();
4772 >                if (map.internalPut(e, v, true) == null)
4773 >                    added = true;
4774 >            }
4775 >            return added;
4776 >        }
4777 >        public boolean equals(Object o) {
4778 >            Set<?> c;
4779 >            return ((o instanceof Set) &&
4780 >                    ((c = (Set<?>)o) == this ||
4781 >                     (containsAll(c) && c.containsAll(this))));
4782          }
4783 +    }
4784  
4785 <        /**
4786 <         * Returns the result of accumulating all entries using the
4787 <         * given reducer to combine values, or null if none.
4788 <         *
4789 <         * @param reducer a commutative associative combining function
4790 <         * @return the result of accumulating all entries
4791 <         */
4792 <        public Map.Entry<K,V> reduceEntries(BiFun<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer) {
4793 <            return fjp.invoke(ForkJoinTasks.reduceEntries
4794 <                              (ConcurrentHashMapV8.this, reducer));
4785 >    /**
4786 >     * A view of a ConcurrentHashMapV8 as a {@link Collection} of
4787 >     * values, in which additions are disabled. This class cannot be
4788 >     * directly instantiated. See {@link #values},
4789 >     *
4790 >     * <p>The view's {@code iterator} is a "weakly consistent" iterator
4791 >     * that will never throw {@link ConcurrentModificationException},
4792 >     * and guarantees to traverse elements as they existed upon
4793 >     * construction of the iterator, and may (but is not guaranteed to)
4794 >     * reflect any modifications subsequent to construction.
4795 >     */
4796 >    public static final class ValuesView<K,V> extends CHMView<K,V>
4797 >        implements Collection<V> {
4798 >        ValuesView(ConcurrentHashMapV8<K, V> map)   { super(map); }
4799 >        public final boolean contains(Object o) { return map.containsValue(o); }
4800 >        public final boolean remove(Object o) {
4801 >            if (o != null) {
4802 >                Iterator<V> it = new ValueIterator<K,V>(map);
4803 >                while (it.hasNext()) {
4804 >                    if (o.equals(it.next())) {
4805 >                        it.remove();
4806 >                        return true;
4807 >                    }
4808 >                }
4809 >            }
4810 >            return false;
4811          }
4812  
4813          /**
4814 <         * Returns the result of accumulating the given transformation
4815 <         * of all entries using the given reducer to combine values,
4816 <         * or null if none.
4814 >         * Returns a "weakly consistent" iterator that will never
4815 >         * throw {@link ConcurrentModificationException}, and
4816 >         * guarantees to traverse elements as they existed upon
4817 >         * construction of the iterator, and may (but is not
4818 >         * guaranteed to) reflect any modifications subsequent to
4819 >         * construction.
4820           *
4821 <         * @param transformer a function returning the transformation
4069 <         * for an element, or null of there is no transformation (in
4070 <         * which case it is not combined).
4071 <         * @param reducer a commutative associative combining function
4072 <         * @return the result of accumulating the given transformation
4073 <         * of all entries
4821 >         * @return an iterator over the values of this map
4822           */
4823 <        public <U> U reduceEntries(Fun<Map.Entry<K,V>, ? extends U> transformer,
4824 <                                   BiFun<? super U, ? super U, ? extends U> reducer) {
4825 <            return fjp.invoke(ForkJoinTasks.reduceEntries
4826 <                              (ConcurrentHashMapV8.this, transformer, reducer));
4823 >        public final Iterator<V> iterator() {
4824 >            return new ValueIterator<K,V>(map);
4825 >        }
4826 >        public final boolean add(V e) {
4827 >            throw new UnsupportedOperationException();
4828 >        }
4829 >        public final boolean addAll(Collection<? extends V> c) {
4830 >            throw new UnsupportedOperationException();
4831          }
4832  
4833 <        /**
4834 <         * Returns the result of accumulating the given transformation
4835 <         * of all entries using the given reducer to combine values,
4836 <         * and the given basis as an identity value.
4837 <         *
4838 <         * @param transformer a function returning the transformation
4839 <         * for an element
4840 <         * @param basis the identity (initial default value) for the reduction
4841 <         * @param reducer a commutative associative combining function
4842 <         * @return the result of accumulating the given transformation
4843 <         * of all entries
4844 <         */
4845 <        public double reduceEntriesToDouble(ObjectToDouble<Map.Entry<K,V>> transformer,
4846 <                                            double basis,
4847 <                                            DoubleByDoubleToDouble reducer) {
4848 <            return fjp.invoke(ForkJoinTasks.reduceEntriesToDouble
4849 <                              (ConcurrentHashMapV8.this, transformer, basis, reducer));
4833 >    }
4834 >
4835 >    /**
4836 >     * A view of a ConcurrentHashMapV8 as a {@link Set} of (key, value)
4837 >     * entries.  This class cannot be directly instantiated. See
4838 >     * {@link #entrySet}.
4839 >     */
4840 >    public static final class EntrySetView<K,V> extends CHMView<K,V>
4841 >        implements Set<Map.Entry<K,V>> {
4842 >        EntrySetView(ConcurrentHashMapV8<K, V> map) { super(map); }
4843 >        public final boolean contains(Object o) {
4844 >            Object k, v, r; Map.Entry<?,?> e;
4845 >            return ((o instanceof Map.Entry) &&
4846 >                    (k = (e = (Map.Entry<?,?>)o).getKey()) != null &&
4847 >                    (r = map.get(k)) != null &&
4848 >                    (v = e.getValue()) != null &&
4849 >                    (v == r || v.equals(r)));
4850 >        }
4851 >        public final boolean remove(Object o) {
4852 >            Object k, v; Map.Entry<?,?> e;
4853 >            return ((o instanceof Map.Entry) &&
4854 >                    (k = (e = (Map.Entry<?,?>)o).getKey()) != null &&
4855 >                    (v = e.getValue()) != null &&
4856 >                    map.remove(k, v));
4857          }
4858  
4859          /**
4860 <         * Returns the result of accumulating the given transformation
4861 <         * of all entries using the given reducer to combine values,
4862 <         * and the given basis as an identity value.
4860 >         * Returns a "weakly consistent" iterator that will never
4861 >         * throw {@link ConcurrentModificationException}, and
4862 >         * guarantees to traverse elements as they existed upon
4863 >         * construction of the iterator, and may (but is not
4864 >         * guaranteed to) reflect any modifications subsequent to
4865 >         * construction.
4866           *
4867 <         * @param transformer a function returning the transformation
4106 <         * for an element
4107 <         * @param basis the identity (initial default value) for the reduction
4108 <         * @param reducer a commutative associative combining function
4109 <         * @return  the result of accumulating the given transformation
4110 <         * of all entries
4867 >         * @return an iterator over the entries of this map
4868           */
4869 <        public long reduceEntriesToLong(ObjectToLong<Map.Entry<K,V>> transformer,
4870 <                                        long basis,
4114 <                                        LongByLongToLong reducer) {
4115 <            return fjp.invoke(ForkJoinTasks.reduceEntriesToLong
4116 <                              (ConcurrentHashMapV8.this, transformer, basis, reducer));
4869 >        public final Iterator<Map.Entry<K,V>> iterator() {
4870 >            return new EntryIterator<K,V>(map);
4871          }
4872  
4873 <        /**
4874 <         * Returns the result of accumulating the given transformation
4875 <         * of all entries using the given reducer to combine values,
4876 <         * and the given basis as an identity value.
4877 <         *
4878 <         * @param transformer a function returning the transformation
4879 <         * for an element
4880 <         * @param basis the identity (initial default value) for the reduction
4881 <         * @param reducer a commutative associative combining function
4882 <         * @return the result of accumulating the given transformation
4883 <         * of all entries
4884 <         */
4885 <        public int reduceEntriesToInt(ObjectToInt<Map.Entry<K,V>> transformer,
4886 <                                      int basis,
4887 <                                      IntByIntToInt reducer) {
4888 <            return fjp.invoke(ForkJoinTasks.reduceEntriesToInt
4889 <                              (ConcurrentHashMapV8.this, transformer, basis, reducer));
4873 >        public final boolean add(Entry<K,V> e) {
4874 >            K key = e.getKey();
4875 >            V value = e.getValue();
4876 >            if (key == null || value == null)
4877 >                throw new NullPointerException();
4878 >            return map.internalPut(key, value, false) == null;
4879 >        }
4880 >        public final boolean addAll(Collection<? extends Entry<K,V>> c) {
4881 >            boolean added = false;
4882 >            for (Entry<K,V> e : c) {
4883 >                if (add(e))
4884 >                    added = true;
4885 >            }
4886 >            return added;
4887 >        }
4888 >        public boolean equals(Object o) {
4889 >            Set<?> c;
4890 >            return ((o instanceof Set) &&
4891 >                    ((c = (Set<?>)o) == this ||
4892 >                     (containsAll(c) && c.containsAll(this))));
4893          }
4894      }
4895  
# Line 4140 | Line 4897 | public class ConcurrentHashMapV8<K, V>
4897  
4898      /**
4899       * Predefined tasks for performing bulk parallel operations on
4900 <     * ConcurrentHashMaps. These tasks follow the forms and rules used
4901 <     * in class {@link Parallel}. Each method has the same name, but
4902 <     * returns a task rather than invoking it. These methods may be
4903 <     * useful in custom applications such as submitting a task without
4904 <     * waiting for completion, or combining with other tasks.
4900 >     * ConcurrentHashMapV8s. These tasks follow the forms and rules used
4901 >     * for bulk operations. Each method has the same name, but returns
4902 >     * a task rather than invoking it. These methods may be useful in
4903 >     * custom applications such as submitting a task without waiting
4904 >     * for completion, using a custom pool, or combining with other
4905 >     * tasks.
4906       */
4907      public static class ForkJoinTasks {
4908          private ForkJoinTasks() {}
# Line 4161 | Line 4919 | public class ConcurrentHashMapV8<K, V>
4919              (ConcurrentHashMapV8<K,V> map,
4920               BiAction<K,V> action) {
4921              if (action == null) throw new NullPointerException();
4922 <            return new ForEachMappingTask<K,V>(map, action);
4922 >            return new ForEachMappingTask<K,V>(map, null, -1, action);
4923          }
4924  
4925          /**
# Line 4170 | Line 4928 | public class ConcurrentHashMapV8<K, V>
4928           *
4929           * @param map the map
4930           * @param transformer a function returning the transformation
4931 <         * for an element, or null of there is no transformation (in
4932 <         * which case the action is not applied).
4931 >         * for an element, or null if there is no transformation (in
4932 >         * which case the action is not applied)
4933           * @param action the action
4934           * @return the task
4935           */
# Line 4182 | Line 4940 | public class ConcurrentHashMapV8<K, V>
4940              if (transformer == null || action == null)
4941                  throw new NullPointerException();
4942              return new ForEachTransformedMappingTask<K,V,U>
4943 <                (map, transformer, action);
4943 >                (map, null, -1, transformer, action);
4944          }
4945  
4946          /**
4947 <         * Returns a task that when invoked, returns a non-null
4948 <         * result from applying the given search function on each
4949 <         * (key, value), or null if none.  Further element processing
4950 <         * is suppressed upon success. However, this method does not
4951 <         * return until other in-progress parallel invocations of the
4194 <         * search function also complete.
4947 >         * Returns a task that when invoked, returns a non-null result
4948 >         * from applying the given search function on each (key,
4949 >         * value), or null if none. Upon success, further element
4950 >         * processing is suppressed and the results of any other
4951 >         * parallel invocations of the search function are ignored.
4952           *
4953           * @param map the map
4954           * @param searchFunction a function returning a non-null
# Line 4203 | Line 4960 | public class ConcurrentHashMapV8<K, V>
4960               BiFun<? super K, ? super V, ? extends U> searchFunction) {
4961              if (searchFunction == null) throw new NullPointerException();
4962              return new SearchMappingsTask<K,V,U>
4963 <                (map, searchFunction,
4963 >                (map, null, -1, searchFunction,
4964                   new AtomicReference<U>());
4965          }
4966  
# Line 4214 | Line 4971 | public class ConcurrentHashMapV8<K, V>
4971           *
4972           * @param map the map
4973           * @param transformer a function returning the transformation
4974 <         * for an element, or null of there is no transformation (in
4974 >         * for an element, or null if there is no transformation (in
4975           * which case it is not combined).
4976           * @param reducer a commutative associative combining function
4977           * @return the task
# Line 4226 | Line 4983 | public class ConcurrentHashMapV8<K, V>
4983              if (transformer == null || reducer == null)
4984                  throw new NullPointerException();
4985              return new MapReduceMappingsTask<K,V,U>
4986 <                (map, transformer, reducer);
4986 >                (map, null, -1, null, transformer, reducer);
4987          }
4988  
4989          /**
# Line 4250 | Line 5007 | public class ConcurrentHashMapV8<K, V>
5007              if (transformer == null || reducer == null)
5008                  throw new NullPointerException();
5009              return new MapReduceMappingsToDoubleTask<K,V>
5010 <                (map, transformer, basis, reducer);
5010 >                (map, null, -1, null, transformer, basis, reducer);
5011          }
5012  
5013          /**
# Line 4274 | Line 5031 | public class ConcurrentHashMapV8<K, V>
5031              if (transformer == null || reducer == null)
5032                  throw new NullPointerException();
5033              return new MapReduceMappingsToLongTask<K,V>
5034 <                (map, transformer, basis, reducer);
5034 >                (map, null, -1, null, transformer, basis, reducer);
5035          }
5036  
5037          /**
# Line 4297 | Line 5054 | public class ConcurrentHashMapV8<K, V>
5054              if (transformer == null || reducer == null)
5055                  throw new NullPointerException();
5056              return new MapReduceMappingsToIntTask<K,V>
5057 <                (map, transformer, basis, reducer);
5057 >                (map, null, -1, null, transformer, basis, reducer);
5058          }
5059  
5060          /**
# Line 4312 | Line 5069 | public class ConcurrentHashMapV8<K, V>
5069              (ConcurrentHashMapV8<K,V> map,
5070               Action<K> action) {
5071              if (action == null) throw new NullPointerException();
5072 <            return new ForEachKeyTask<K,V>(map, action);
5072 >            return new ForEachKeyTask<K,V>(map, null, -1, action);
5073          }
5074  
5075          /**
# Line 4321 | Line 5078 | public class ConcurrentHashMapV8<K, V>
5078           *
5079           * @param map the map
5080           * @param transformer a function returning the transformation
5081 <         * for an element, or null of there is no transformation (in
5082 <         * which case the action is not applied).
5081 >         * for an element, or null if there is no transformation (in
5082 >         * which case the action is not applied)
5083           * @param action the action
5084           * @return the task
5085           */
# Line 4333 | Line 5090 | public class ConcurrentHashMapV8<K, V>
5090              if (transformer == null || action == null)
5091                  throw new NullPointerException();
5092              return new ForEachTransformedKeyTask<K,V,U>
5093 <                (map, transformer, action);
5093 >                (map, null, -1, transformer, action);
5094          }
5095  
5096          /**
5097           * Returns a task that when invoked, returns a non-null result
5098           * from applying the given search function on each key, or
5099 <         * null if none.  Further element processing is suppressed
5100 <         * upon success. However, this method does not return until
5101 <         * other in-progress parallel invocations of the search
4345 <         * function also complete.
5099 >         * null if none.  Upon success, further element processing is
5100 >         * suppressed and the results of any other parallel
5101 >         * invocations of the search function are ignored.
5102           *
5103           * @param map the map
5104           * @param searchFunction a function returning a non-null
# Line 4354 | Line 5110 | public class ConcurrentHashMapV8<K, V>
5110               Fun<? super K, ? extends U> searchFunction) {
5111              if (searchFunction == null) throw new NullPointerException();
5112              return new SearchKeysTask<K,V,U>
5113 <                (map, searchFunction,
5113 >                (map, null, -1, searchFunction,
5114                   new AtomicReference<U>());
5115          }
5116  
# Line 4372 | Line 5128 | public class ConcurrentHashMapV8<K, V>
5128               BiFun<? super K, ? super K, ? extends K> reducer) {
5129              if (reducer == null) throw new NullPointerException();
5130              return new ReduceKeysTask<K,V>
5131 <                (map, reducer);
5131 >                (map, null, -1, null, reducer);
5132          }
5133  
5134          /**
# Line 4382 | Line 5138 | public class ConcurrentHashMapV8<K, V>
5138           *
5139           * @param map the map
5140           * @param transformer a function returning the transformation
5141 <         * for an element, or null of there is no transformation (in
5141 >         * for an element, or null if there is no transformation (in
5142           * which case it is not combined).
5143           * @param reducer a commutative associative combining function
5144           * @return the task
# Line 4394 | Line 5150 | public class ConcurrentHashMapV8<K, V>
5150              if (transformer == null || reducer == null)
5151                  throw new NullPointerException();
5152              return new MapReduceKeysTask<K,V,U>
5153 <                (map, transformer, reducer);
5153 >                (map, null, -1, null, transformer, reducer);
5154          }
5155  
5156          /**
# Line 4418 | Line 5174 | public class ConcurrentHashMapV8<K, V>
5174              if (transformer == null || reducer == null)
5175                  throw new NullPointerException();
5176              return new MapReduceKeysToDoubleTask<K,V>
5177 <                (map, transformer, basis, reducer);
5177 >                (map, null, -1, null, transformer, basis, reducer);
5178          }
5179  
5180          /**
# Line 4442 | Line 5198 | public class ConcurrentHashMapV8<K, V>
5198              if (transformer == null || reducer == null)
5199                  throw new NullPointerException();
5200              return new MapReduceKeysToLongTask<K,V>
5201 <                (map, transformer, basis, reducer);
5201 >                (map, null, -1, null, transformer, basis, reducer);
5202          }
5203  
5204          /**
# Line 4466 | Line 5222 | public class ConcurrentHashMapV8<K, V>
5222              if (transformer == null || reducer == null)
5223                  throw new NullPointerException();
5224              return new MapReduceKeysToIntTask<K,V>
5225 <                (map, transformer, basis, reducer);
5225 >                (map, null, -1, null, transformer, basis, reducer);
5226          }
5227  
5228          /**
# Line 4480 | Line 5236 | public class ConcurrentHashMapV8<K, V>
5236              (ConcurrentHashMapV8<K,V> map,
5237               Action<V> action) {
5238              if (action == null) throw new NullPointerException();
5239 <            return new ForEachValueTask<K,V>(map, action);
5239 >            return new ForEachValueTask<K,V>(map, null, -1, action);
5240          }
5241  
5242          /**
# Line 4489 | Line 5245 | public class ConcurrentHashMapV8<K, V>
5245           *
5246           * @param map the map
5247           * @param transformer a function returning the transformation
5248 <         * for an element, or null of there is no transformation (in
5249 <         * which case the action is not applied).
5248 >         * for an element, or null if there is no transformation (in
5249 >         * which case the action is not applied)
5250           * @param action the action
5251           */
5252          public static <K,V,U> ForkJoinTask<Void> forEachValue
# Line 4500 | Line 5256 | public class ConcurrentHashMapV8<K, V>
5256              if (transformer == null || action == null)
5257                  throw new NullPointerException();
5258              return new ForEachTransformedValueTask<K,V,U>
5259 <                (map, transformer, action);
5259 >                (map, null, -1, transformer, action);
5260          }
5261  
5262          /**
5263           * Returns a task that when invoked, returns a non-null result
5264           * from applying the given search function on each value, or
5265 <         * null if none.  Further element processing is suppressed
5266 <         * upon success. However, this method does not return until
5267 <         * other in-progress parallel invocations of the search
4512 <         * function also complete.
5265 >         * null if none.  Upon success, further element processing is
5266 >         * suppressed and the results of any other parallel
5267 >         * invocations of the search function are ignored.
5268           *
5269           * @param map the map
5270           * @param searchFunction a function returning a non-null
5271           * result on success, else null
5272           * @return the task
4518         *
5273           */
5274          public static <K,V,U> ForkJoinTask<U> searchValues
5275              (ConcurrentHashMapV8<K,V> map,
5276               Fun<? super V, ? extends U> searchFunction) {
5277              if (searchFunction == null) throw new NullPointerException();
5278              return new SearchValuesTask<K,V,U>
5279 <                (map, searchFunction,
5279 >                (map, null, -1, searchFunction,
5280                   new AtomicReference<U>());
5281          }
5282  
# Line 4540 | Line 5294 | public class ConcurrentHashMapV8<K, V>
5294               BiFun<? super V, ? super V, ? extends V> reducer) {
5295              if (reducer == null) throw new NullPointerException();
5296              return new ReduceValuesTask<K,V>
5297 <                (map, reducer);
5297 >                (map, null, -1, null, reducer);
5298          }
5299  
5300          /**
# Line 4550 | Line 5304 | public class ConcurrentHashMapV8<K, V>
5304           *
5305           * @param map the map
5306           * @param transformer a function returning the transformation
5307 <         * for an element, or null of there is no transformation (in
5307 >         * for an element, or null if there is no transformation (in
5308           * which case it is not combined).
5309           * @param reducer a commutative associative combining function
5310           * @return the task
# Line 4562 | Line 5316 | public class ConcurrentHashMapV8<K, V>
5316              if (transformer == null || reducer == null)
5317                  throw new NullPointerException();
5318              return new MapReduceValuesTask<K,V,U>
5319 <                (map, transformer, reducer);
5319 >                (map, null, -1, null, transformer, reducer);
5320          }
5321  
5322          /**
# Line 4586 | Line 5340 | public class ConcurrentHashMapV8<K, V>
5340              if (transformer == null || reducer == null)
5341                  throw new NullPointerException();
5342              return new MapReduceValuesToDoubleTask<K,V>
5343 <                (map, transformer, basis, reducer);
5343 >                (map, null, -1, null, transformer, basis, reducer);
5344          }
5345  
5346          /**
# Line 4610 | Line 5364 | public class ConcurrentHashMapV8<K, V>
5364              if (transformer == null || reducer == null)
5365                  throw new NullPointerException();
5366              return new MapReduceValuesToLongTask<K,V>
5367 <                (map, transformer, basis, reducer);
5367 >                (map, null, -1, null, transformer, basis, reducer);
5368          }
5369  
5370          /**
# Line 4634 | Line 5388 | public class ConcurrentHashMapV8<K, V>
5388              if (transformer == null || reducer == null)
5389                  throw new NullPointerException();
5390              return new MapReduceValuesToIntTask<K,V>
5391 <                (map, transformer, basis, reducer);
5391 >                (map, null, -1, null, transformer, basis, reducer);
5392          }
5393  
5394          /**
# Line 4648 | Line 5402 | public class ConcurrentHashMapV8<K, V>
5402              (ConcurrentHashMapV8<K,V> map,
5403               Action<Map.Entry<K,V>> action) {
5404              if (action == null) throw new NullPointerException();
5405 <            return new ForEachEntryTask<K,V>(map, action);
5405 >            return new ForEachEntryTask<K,V>(map, null, -1, action);
5406          }
5407  
5408          /**
# Line 4657 | Line 5411 | public class ConcurrentHashMapV8<K, V>
5411           *
5412           * @param map the map
5413           * @param transformer a function returning the transformation
5414 <         * for an element, or null of there is no transformation (in
5415 <         * which case the action is not applied).
5414 >         * for an element, or null if there is no transformation (in
5415 >         * which case the action is not applied)
5416           * @param action the action
5417           */
5418          public static <K,V,U> ForkJoinTask<Void> forEachEntry
# Line 4668 | Line 5422 | public class ConcurrentHashMapV8<K, V>
5422              if (transformer == null || action == null)
5423                  throw new NullPointerException();
5424              return new ForEachTransformedEntryTask<K,V,U>
5425 <                (map, transformer, action);
5425 >                (map, null, -1, transformer, action);
5426          }
5427  
5428          /**
5429           * Returns a task that when invoked, returns a non-null result
5430           * from applying the given search function on each entry, or
5431 <         * null if none.  Further element processing is suppressed
5432 <         * upon success. However, this method does not return until
5433 <         * other in-progress parallel invocations of the search
4680 <         * function also complete.
5431 >         * null if none.  Upon success, further element processing is
5432 >         * suppressed and the results of any other parallel
5433 >         * invocations of the search function are ignored.
5434           *
5435           * @param map the map
5436           * @param searchFunction a function returning a non-null
5437           * result on success, else null
5438           * @return the task
4686         *
5439           */
5440          public static <K,V,U> ForkJoinTask<U> searchEntries
5441              (ConcurrentHashMapV8<K,V> map,
5442               Fun<Map.Entry<K,V>, ? extends U> searchFunction) {
5443              if (searchFunction == null) throw new NullPointerException();
5444              return new SearchEntriesTask<K,V,U>
5445 <                (map, searchFunction,
5445 >                (map, null, -1, searchFunction,
5446                   new AtomicReference<U>());
5447          }
5448  
# Line 4708 | Line 5460 | public class ConcurrentHashMapV8<K, V>
5460               BiFun<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer) {
5461              if (reducer == null) throw new NullPointerException();
5462              return new ReduceEntriesTask<K,V>
5463 <                (map, reducer);
5463 >                (map, null, -1, null, reducer);
5464          }
5465  
5466          /**
# Line 4718 | Line 5470 | public class ConcurrentHashMapV8<K, V>
5470           *
5471           * @param map the map
5472           * @param transformer a function returning the transformation
5473 <         * for an element, or null of there is no transformation (in
5473 >         * for an element, or null if there is no transformation (in
5474           * which case it is not combined).
5475           * @param reducer a commutative associative combining function
5476           * @return the task
# Line 4730 | Line 5482 | public class ConcurrentHashMapV8<K, V>
5482              if (transformer == null || reducer == null)
5483                  throw new NullPointerException();
5484              return new MapReduceEntriesTask<K,V,U>
5485 <                (map, transformer, reducer);
5485 >                (map, null, -1, null, transformer, reducer);
5486          }
5487  
5488          /**
# Line 4754 | Line 5506 | public class ConcurrentHashMapV8<K, V>
5506              if (transformer == null || reducer == null)
5507                  throw new NullPointerException();
5508              return new MapReduceEntriesToDoubleTask<K,V>
5509 <                (map, transformer, basis, reducer);
5509 >                (map, null, -1, null, transformer, basis, reducer);
5510          }
5511  
5512          /**
# Line 4778 | Line 5530 | public class ConcurrentHashMapV8<K, V>
5530              if (transformer == null || reducer == null)
5531                  throw new NullPointerException();
5532              return new MapReduceEntriesToLongTask<K,V>
5533 <                (map, transformer, basis, reducer);
5533 >                (map, null, -1, null, transformer, basis, reducer);
5534          }
5535  
5536          /**
# Line 4802 | Line 5554 | public class ConcurrentHashMapV8<K, V>
5554              if (transformer == null || reducer == null)
5555                  throw new NullPointerException();
5556              return new MapReduceEntriesToIntTask<K,V>
5557 <                (map, transformer, basis, reducer);
5557 >                (map, null, -1, null, transformer, basis, reducer);
5558          }
5559      }
5560  
5561      // -------------------------------------------------------
5562  
4811    /**
4812     * Base for FJ tasks for bulk operations. This adds a variant of
4813     * CountedCompleters and some split and merge bookkeeping to
4814     * iterator functionality. The forEach and reduce methods are
4815     * similar to those illustrated in CountedCompleter documentation,
4816     * except that bottom-up reduction completions perform them within
4817     * their compute methods. The search methods are like forEach
4818     * except they continually poll for success and exit early.  Also,
4819     * exceptions are handled in a simpler manner, by just trying to
4820     * complete root task exceptionally.
4821     */
4822    static abstract class BulkTask<K,V,R> extends Traverser<K,V,R> {
4823        final BulkTask<K,V,?> parent;  // completion target
4824        int batch;                     // split control
4825        int pending;                   // completion control
4826
4827        /** Constructor for root tasks */
4828        BulkTask(ConcurrentHashMapV8<K,V> map) {
4829            super(map);
4830            this.parent = null;
4831            this.batch = -1; // force call to batch() on execution
4832        }
4833
4834        /** Constructor for subtasks */
4835        BulkTask(BulkTask<K,V,?> parent, int batch, boolean split) {
4836            super(parent, split);
4837            this.parent = parent;
4838            this.batch = batch;
4839        }
4840
4841        // FJ methods
4842
4843        /**
4844         * Propagates completion. Note that all reduce actions
4845         * bypass this method to combine while completing.
4846         */
4847        final void tryComplete() {
4848            BulkTask<K,V,?> a = this, s = a;
4849            for (int c;;) {
4850                if ((c = a.pending) == 0) {
4851                    if ((a = (s = a).parent) == null) {
4852                        s.quietlyComplete();
4853                        break;
4854                    }
4855                }
4856                else if (U.compareAndSwapInt(a, PENDING, c, c - 1))
4857                    break;
4858            }
4859        }
4860
4861        /**
4862         * Forces root task to throw exception unless already complete.
4863         */
4864        final void tryAbortComputation(Throwable ex) {
4865            for (BulkTask<K,V,?> a = this;;) {
4866                BulkTask<K,V,?> p = a.parent;
4867                if (p == null) {
4868                    a.completeExceptionally(ex);
4869                    break;
4870                }
4871                a = p;
4872            }
4873        }
4874
4875        public final boolean exec() {
4876            try {
4877                compute();
4878            }
4879            catch (Throwable ex) {
4880                tryAbortComputation(ex);
4881            }
4882            return false;
4883        }
4884
4885        public abstract void compute();
4886
4887        // utilities
4888
4889        /** CompareAndSet pending count */
4890        final boolean casPending(int cmp, int val) {
4891            return U.compareAndSwapInt(this, PENDING, cmp, val);
4892        }
4893
4894        /**
4895         * Returns approx exp2 of the number of times (minus one) to
4896         * split task by two before executing leaf action. This value
4897         * is faster to compute and more convenient to use as a guide
4898         * to splitting than is the depth, since it is used while
4899         * dividing by two anyway.
4900         */
4901        final int batch() {
4902            int b = batch;
4903            if (b < 0) {
4904                long n = map.counter.sum();
4905                int sp = getPool().getParallelism() << 3; // slack of 8
4906                b = batch = (n <= 0L) ? 0 : (n < (long)sp) ? (int)n : sp;
4907            }
4908            return b;
4909        }
4910
4911        /**
4912         * Error message for hoisted null checks of functions
4913         */
4914        static final String NullFunctionMessage =
4915            "Unexpected null function";
4916
4917        /**
4918         * Returns exportable snapshot entry.
4919         */
4920        static <K,V> AbstractMap.SimpleEntry<K,V> entryFor(K k, V v) {
4921            return new AbstractMap.SimpleEntry(k, v);
4922        }
4923
4924        // Unsafe mechanics
4925        private static final sun.misc.Unsafe U;
4926        private static final long PENDING;
4927        static {
4928            try {
4929                U = sun.misc.Unsafe.getUnsafe();
4930                PENDING = U.objectFieldOffset
4931                    (BulkTask.class.getDeclaredField("pending"));
4932            } catch (Exception e) {
4933                throw new Error(e);
4934            }
4935        }
4936    }
4937
5563      /*
5564       * Task classes. Coded in a regular but ugly format/style to
5565       * simplify checks that each variant differs in the right way from
5566 <     * others.
5566 >     * others. The null screenings exist because compilers cannot tell
5567 >     * that we've already null-checked task arguments, so we force
5568 >     * simplest hoisted bypass to help avoid convoluted traps.
5569       */
5570  
5571 <    static final class ForEachKeyTask<K,V>
5572 <        extends BulkTask<K,V,Void> {
5571 >    @SuppressWarnings("serial") static final class ForEachKeyTask<K,V>
5572 >        extends Traverser<K,V,Void> {
5573          final Action<K> action;
5574          ForEachKeyTask
5575 <            (ConcurrentHashMapV8<K,V> m,
4949 <             Action<K> action) {
4950 <            super(m);
4951 <            this.action = action;
4952 <        }
4953 <        ForEachKeyTask
4954 <            (BulkTask<K,V,?> p, int b, boolean split,
5575 >            (ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b,
5576               Action<K> action) {
5577 <            super(p, b, split);
5577 >            super(m, p, b);
5578              this.action = action;
5579          }
5580 <        public final void compute() {
5581 <            final Action<K> action = this.action;
5582 <            if (action == null)
5583 <                throw new Error(NullFunctionMessage);
5584 <            int b = batch(), c;
5585 <            while (b > 1 && baseIndex != baseLimit) {
5586 <                do {} while (!casPending(c = pending, c+1));
5587 <                new ForEachKeyTask<K,V>(this, b >>>= 1, true, action).fork();
5588 <            }
4968 <            while (advance() != null)
4969 <                action.apply((K)nextKey);
4970 <            tryComplete();
5580 >        @SuppressWarnings("unchecked") public final void compute() {
5581 >            final Action<K> action;
5582 >            if ((action = this.action) != null) {
5583 >                for (int b; (b = preSplit()) > 0;)
5584 >                    new ForEachKeyTask<K,V>(map, this, b, action).fork();
5585 >                while (advance() != null)
5586 >                    action.apply((K)nextKey);
5587 >                propagateCompletion();
5588 >            }
5589          }
5590      }
5591  
5592 <    static final class ForEachValueTask<K,V>
5593 <        extends BulkTask<K,V,Void> {
5592 >    @SuppressWarnings("serial") static final class ForEachValueTask<K,V>
5593 >        extends Traverser<K,V,Void> {
5594          final Action<V> action;
5595          ForEachValueTask
5596 <            (ConcurrentHashMapV8<K,V> m,
5596 >            (ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b,
5597               Action<V> action) {
5598 <            super(m);
5598 >            super(m, p, b);
5599              this.action = action;
5600          }
5601 <        ForEachValueTask
5602 <            (BulkTask<K,V,?> p, int b, boolean split,
5603 <             Action<V> action) {
5604 <            super(p, b, split);
5605 <            this.action = action;
5606 <        }
5607 <        public final void compute() {
5608 <            final Action<V> action = this.action;
5609 <            if (action == null)
5610 <                throw new Error(NullFunctionMessage);
4993 <            int b = batch(), c;
4994 <            while (b > 1 && baseIndex != baseLimit) {
4995 <                do {} while (!casPending(c = pending, c+1));
4996 <                new ForEachValueTask<K,V>(this, b >>>= 1, true, action).fork();
4997 <            }
4998 <            Object v;
4999 <            while ((v = advance()) != null)
5000 <                action.apply((V)v);
5001 <            tryComplete();
5601 >        @SuppressWarnings("unchecked") public final void compute() {
5602 >            final Action<V> action;
5603 >            if ((action = this.action) != null) {
5604 >                for (int b; (b = preSplit()) > 0;)
5605 >                    new ForEachValueTask<K,V>(map, this, b, action).fork();
5606 >                V v;
5607 >                while ((v = advance()) != null)
5608 >                    action.apply(v);
5609 >                propagateCompletion();
5610 >            }
5611          }
5612      }
5613  
5614 <    static final class ForEachEntryTask<K,V>
5615 <        extends BulkTask<K,V,Void> {
5614 >    @SuppressWarnings("serial") static final class ForEachEntryTask<K,V>
5615 >        extends Traverser<K,V,Void> {
5616          final Action<Entry<K,V>> action;
5617          ForEachEntryTask
5618 <            (ConcurrentHashMapV8<K,V> m,
5010 <             Action<Entry<K,V>> action) {
5011 <            super(m);
5012 <            this.action = action;
5013 <        }
5014 <        ForEachEntryTask
5015 <            (BulkTask<K,V,?> p, int b, boolean split,
5618 >            (ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b,
5619               Action<Entry<K,V>> action) {
5620 <            super(p, b, split);
5620 >            super(m, p, b);
5621              this.action = action;
5622          }
5623 <        public final void compute() {
5624 <            final Action<Entry<K,V>> action = this.action;
5625 <            if (action == null)
5626 <                throw new Error(NullFunctionMessage);
5627 <            int b = batch(), c;
5628 <            while (b > 1 && baseIndex != baseLimit) {
5629 <                do {} while (!casPending(c = pending, c+1));
5630 <                new ForEachEntryTask<K,V>(this, b >>>= 1, true, action).fork();
5631 <            }
5632 <            Object v;
5030 <            while ((v = advance()) != null)
5031 <                action.apply(entryFor((K)nextKey, (V)v));
5032 <            tryComplete();
5623 >        @SuppressWarnings("unchecked") public final void compute() {
5624 >            final Action<Entry<K,V>> action;
5625 >            if ((action = this.action) != null) {
5626 >                for (int b; (b = preSplit()) > 0;)
5627 >                    new ForEachEntryTask<K,V>(map, this, b, action).fork();
5628 >                V v;
5629 >                while ((v = advance()) != null)
5630 >                    action.apply(entryFor((K)nextKey, v));
5631 >                propagateCompletion();
5632 >            }
5633          }
5634      }
5635  
5636 <    static final class ForEachMappingTask<K,V>
5637 <        extends BulkTask<K,V,Void> {
5636 >    @SuppressWarnings("serial") static final class ForEachMappingTask<K,V>
5637 >        extends Traverser<K,V,Void> {
5638          final BiAction<K,V> action;
5639          ForEachMappingTask
5640 <            (ConcurrentHashMapV8<K,V> m,
5041 <             BiAction<K,V> action) {
5042 <            super(m);
5043 <            this.action = action;
5044 <        }
5045 <        ForEachMappingTask
5046 <            (BulkTask<K,V,?> p, int b, boolean split,
5640 >            (ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b,
5641               BiAction<K,V> action) {
5642 <            super(p, b, split);
5642 >            super(m, p, b);
5643              this.action = action;
5644          }
5645 <
5646 <        public final void compute() {
5647 <            final BiAction<K,V> action = this.action;
5648 <            if (action == null)
5649 <                throw new Error(NullFunctionMessage);
5650 <            int b = batch(), c;
5651 <            while (b > 1 && baseIndex != baseLimit) {
5652 <                do {} while (!casPending(c = pending, c+1));
5653 <                new ForEachMappingTask<K,V>(this, b >>>= 1, true,
5654 <                                            action).fork();
5061 <            }
5062 <            Object v;
5063 <            while ((v = advance()) != null)
5064 <                action.apply((K)nextKey, (V)v);
5065 <            tryComplete();
5645 >        @SuppressWarnings("unchecked") public final void compute() {
5646 >            final BiAction<K,V> action;
5647 >            if ((action = this.action) != null) {
5648 >                for (int b; (b = preSplit()) > 0;)
5649 >                    new ForEachMappingTask<K,V>(map, this, b, action).fork();
5650 >                V v;
5651 >                while ((v = advance()) != null)
5652 >                    action.apply((K)nextKey, v);
5653 >                propagateCompletion();
5654 >            }
5655          }
5656      }
5657  
5658 <    static final class ForEachTransformedKeyTask<K,V,U>
5659 <        extends BulkTask<K,V,Void> {
5658 >    @SuppressWarnings("serial") static final class ForEachTransformedKeyTask<K,V,U>
5659 >        extends Traverser<K,V,Void> {
5660          final Fun<? super K, ? extends U> transformer;
5661          final Action<U> action;
5662          ForEachTransformedKeyTask
5663 <            (ConcurrentHashMapV8<K,V> m,
5664 <             Fun<? super K, ? extends U> transformer,
5665 <             Action<U> action) {
5666 <            super(m);
5667 <            this.transformer = transformer;
5668 <            this.action = action;
5669 <
5670 <        }
5671 <        ForEachTransformedKeyTask
5672 <            (BulkTask<K,V,?> p, int b, boolean split,
5673 <             Fun<? super K, ? extends U> transformer,
5674 <             Action<U> action) {
5675 <            super(p, b, split);
5676 <            this.transformer = transformer;
5677 <            this.action = action;
5678 <        }
5679 <        public final void compute() {
5680 <            final Fun<? super K, ? extends U> transformer =
5681 <                this.transformer;
5093 <            final Action<U> action = this.action;
5094 <            if (transformer == null || action == null)
5095 <                throw new Error(NullFunctionMessage);
5096 <            int b = batch(), c;
5097 <            while (b > 1 && baseIndex != baseLimit) {
5098 <                do {} while (!casPending(c = pending, c+1));
5099 <                new ForEachTransformedKeyTask<K,V,U>
5100 <                    (this, b >>>= 1, true, transformer, action).fork();
5101 <            }
5102 <            U u;
5103 <            while (advance() != null) {
5104 <                if ((u = transformer.apply((K)nextKey)) != null)
5105 <                    action.apply(u);
5663 >            (ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b,
5664 >             Fun<? super K, ? extends U> transformer, Action<U> action) {
5665 >            super(m, p, b);
5666 >            this.transformer = transformer; this.action = action;
5667 >        }
5668 >        @SuppressWarnings("unchecked") public final void compute() {
5669 >            final Fun<? super K, ? extends U> transformer;
5670 >            final Action<U> action;
5671 >            if ((transformer = this.transformer) != null &&
5672 >                (action = this.action) != null) {
5673 >                for (int b; (b = preSplit()) > 0;)
5674 >                    new ForEachTransformedKeyTask<K,V,U>
5675 >                        (map, this, b, transformer, action).fork();
5676 >                U u;
5677 >                while (advance() != null) {
5678 >                    if ((u = transformer.apply((K)nextKey)) != null)
5679 >                        action.apply(u);
5680 >                }
5681 >                propagateCompletion();
5682              }
5107            tryComplete();
5683          }
5684      }
5685  
5686 <    static final class ForEachTransformedValueTask<K,V,U>
5687 <        extends BulkTask<K,V,Void> {
5686 >    @SuppressWarnings("serial") static final class ForEachTransformedValueTask<K,V,U>
5687 >        extends Traverser<K,V,Void> {
5688          final Fun<? super V, ? extends U> transformer;
5689          final Action<U> action;
5690          ForEachTransformedValueTask
5691 <            (ConcurrentHashMapV8<K,V> m,
5692 <             Fun<? super V, ? extends U> transformer,
5693 <             Action<U> action) {
5694 <            super(m);
5695 <            this.transformer = transformer;
5696 <            this.action = action;
5697 <
5698 <        }
5699 <        ForEachTransformedValueTask
5700 <            (BulkTask<K,V,?> p, int b, boolean split,
5701 <             Fun<? super V, ? extends U> transformer,
5702 <             Action<U> action) {
5703 <            super(p, b, split);
5704 <            this.transformer = transformer;
5705 <            this.action = action;
5706 <        }
5707 <        public final void compute() {
5708 <            final Fun<? super V, ? extends U> transformer =
5709 <                this.transformer;
5135 <            final Action<U> action = this.action;
5136 <            if (transformer == null || action == null)
5137 <                throw new Error(NullFunctionMessage);
5138 <            int b = batch(), c;
5139 <            while (b > 1 && baseIndex != baseLimit) {
5140 <                do {} while (!casPending(c = pending, c+1));
5141 <                new ForEachTransformedValueTask<K,V,U>
5142 <                    (this, b >>>= 1, true, transformer, action).fork();
5143 <            }
5144 <            Object v; U u;
5145 <            while ((v = advance()) != null) {
5146 <                if ((u = transformer.apply((V)v)) != null)
5147 <                    action.apply(u);
5691 >            (ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b,
5692 >             Fun<? super V, ? extends U> transformer, Action<U> action) {
5693 >            super(m, p, b);
5694 >            this.transformer = transformer; this.action = action;
5695 >        }
5696 >        @SuppressWarnings("unchecked") public final void compute() {
5697 >            final Fun<? super V, ? extends U> transformer;
5698 >            final Action<U> action;
5699 >            if ((transformer = this.transformer) != null &&
5700 >                (action = this.action) != null) {
5701 >                for (int b; (b = preSplit()) > 0;)
5702 >                    new ForEachTransformedValueTask<K,V,U>
5703 >                        (map, this, b, transformer, action).fork();
5704 >                V v; U u;
5705 >                while ((v = advance()) != null) {
5706 >                    if ((u = transformer.apply(v)) != null)
5707 >                        action.apply(u);
5708 >                }
5709 >                propagateCompletion();
5710              }
5149            tryComplete();
5711          }
5712      }
5713  
5714 <    static final class ForEachTransformedEntryTask<K,V,U>
5715 <        extends BulkTask<K,V,Void> {
5714 >    @SuppressWarnings("serial") static final class ForEachTransformedEntryTask<K,V,U>
5715 >        extends Traverser<K,V,Void> {
5716          final Fun<Map.Entry<K,V>, ? extends U> transformer;
5717          final Action<U> action;
5718          ForEachTransformedEntryTask
5719 <            (ConcurrentHashMapV8<K,V> m,
5720 <             Fun<Map.Entry<K,V>, ? extends U> transformer,
5721 <             Action<U> action) {
5722 <            super(m);
5723 <            this.transformer = transformer;
5724 <            this.action = action;
5725 <
5726 <        }
5727 <        ForEachTransformedEntryTask
5728 <            (BulkTask<K,V,?> p, int b, boolean split,
5729 <             Fun<Map.Entry<K,V>, ? extends U> transformer,
5730 <             Action<U> action) {
5731 <            super(p, b, split);
5732 <            this.transformer = transformer;
5733 <            this.action = action;
5734 <        }
5735 <        public final void compute() {
5736 <            final Fun<Map.Entry<K,V>, ? extends U> transformer =
5737 <                this.transformer;
5738 <            final Action<U> action = this.action;
5178 <            if (transformer == null || action == null)
5179 <                throw new Error(NullFunctionMessage);
5180 <            int b = batch(), c;
5181 <            while (b > 1 && baseIndex != baseLimit) {
5182 <                do {} while (!casPending(c = pending, c+1));
5183 <                new ForEachTransformedEntryTask<K,V,U>
5184 <                    (this, b >>>= 1, true, transformer, action).fork();
5185 <            }
5186 <            Object v; U u;
5187 <            while ((v = advance()) != null) {
5188 <                if ((u = transformer.apply(entryFor((K)nextKey, (V)v))) != null)
5189 <                    action.apply(u);
5719 >            (ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b,
5720 >             Fun<Map.Entry<K,V>, ? extends U> transformer, Action<U> action) {
5721 >            super(m, p, b);
5722 >            this.transformer = transformer; this.action = action;
5723 >        }
5724 >        @SuppressWarnings("unchecked") public final void compute() {
5725 >            final Fun<Map.Entry<K,V>, ? extends U> transformer;
5726 >            final Action<U> action;
5727 >            if ((transformer = this.transformer) != null &&
5728 >                (action = this.action) != null) {
5729 >                for (int b; (b = preSplit()) > 0;)
5730 >                    new ForEachTransformedEntryTask<K,V,U>
5731 >                        (map, this, b, transformer, action).fork();
5732 >                V v; U u;
5733 >                while ((v = advance()) != null) {
5734 >                    if ((u = transformer.apply(entryFor((K)nextKey,
5735 >                                                        v))) != null)
5736 >                        action.apply(u);
5737 >                }
5738 >                propagateCompletion();
5739              }
5191            tryComplete();
5740          }
5741      }
5742  
5743 <    static final class ForEachTransformedMappingTask<K,V,U>
5744 <        extends BulkTask<K,V,Void> {
5743 >    @SuppressWarnings("serial") static final class ForEachTransformedMappingTask<K,V,U>
5744 >        extends Traverser<K,V,Void> {
5745          final BiFun<? super K, ? super V, ? extends U> transformer;
5746          final Action<U> action;
5747          ForEachTransformedMappingTask
5748 <            (ConcurrentHashMapV8<K,V> m,
5201 <             BiFun<? super K, ? super V, ? extends U> transformer,
5202 <             Action<U> action) {
5203 <            super(m);
5204 <            this.transformer = transformer;
5205 <            this.action = action;
5206 <
5207 <        }
5208 <        ForEachTransformedMappingTask
5209 <            (BulkTask<K,V,?> p, int b, boolean split,
5748 >            (ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b,
5749               BiFun<? super K, ? super V, ? extends U> transformer,
5750               Action<U> action) {
5751 <            super(p, b, split);
5752 <            this.transformer = transformer;
5214 <            this.action = action;
5751 >            super(m, p, b);
5752 >            this.transformer = transformer; this.action = action;
5753          }
5754 <        public final void compute() {
5755 <            final BiFun<? super K, ? super V, ? extends U> transformer =
5756 <                this.transformer;
5757 <            final Action<U> action = this.action;
5758 <            if (transformer == null || action == null)
5759 <                throw new Error(NullFunctionMessage);
5760 <            int b = batch(), c;
5761 <            while (b > 1 && baseIndex != baseLimit) {
5762 <                do {} while (!casPending(c = pending, c+1));
5763 <                new ForEachTransformedMappingTask<K,V,U>
5764 <                    (this, b >>>= 1, true, transformer, action).fork();
5765 <            }
5766 <            Object v; U u;
5767 <            while ((v = advance()) != null) {
5230 <                if ((u = transformer.apply((K)nextKey, (V)v)) != null)
5231 <                    action.apply(u);
5754 >        @SuppressWarnings("unchecked") public final void compute() {
5755 >            final BiFun<? super K, ? super V, ? extends U> transformer;
5756 >            final Action<U> action;
5757 >            if ((transformer = this.transformer) != null &&
5758 >                (action = this.action) != null) {
5759 >                for (int b; (b = preSplit()) > 0;)
5760 >                    new ForEachTransformedMappingTask<K,V,U>
5761 >                        (map, this, b, transformer, action).fork();
5762 >                V v; U u;
5763 >                while ((v = advance()) != null) {
5764 >                    if ((u = transformer.apply((K)nextKey, v)) != null)
5765 >                        action.apply(u);
5766 >                }
5767 >                propagateCompletion();
5768              }
5233            tryComplete();
5769          }
5770      }
5771  
5772 <    static final class SearchKeysTask<K,V,U>
5773 <        extends BulkTask<K,V,U> {
5772 >    @SuppressWarnings("serial") static final class SearchKeysTask<K,V,U>
5773 >        extends Traverser<K,V,U> {
5774          final Fun<? super K, ? extends U> searchFunction;
5775          final AtomicReference<U> result;
5776          SearchKeysTask
5777 <            (ConcurrentHashMapV8<K,V> m,
5243 <             Fun<? super K, ? extends U> searchFunction,
5244 <             AtomicReference<U> result) {
5245 <            super(m);
5246 <            this.searchFunction = searchFunction; this.result = result;
5247 <        }
5248 <        SearchKeysTask
5249 <            (BulkTask<K,V,?> p, int b, boolean split,
5777 >            (ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b,
5778               Fun<? super K, ? extends U> searchFunction,
5779               AtomicReference<U> result) {
5780 <            super(p, b, split);
5780 >            super(m, p, b);
5781              this.searchFunction = searchFunction; this.result = result;
5782          }
5783 <        public final void compute() {
5784 <            AtomicReference<U> result = this.result;
5785 <            final Fun<? super K, ? extends U> searchFunction =
5786 <                this.searchFunction;
5787 <            if (searchFunction == null || result == null)
5788 <                throw new Error(NullFunctionMessage);
5789 <            int b = batch(), c;
5790 <            while (b > 1 && baseIndex != baseLimit && result.get() == null) {
5791 <                do {} while (!casPending(c = pending, c+1));
5792 <                new SearchKeysTask<K,V,U>(this, b >>>= 1, true,
5793 <                                          searchFunction, result).fork();
5794 <            }
5795 <            U u;
5796 <            while (result.get() == null && advance() != null) {
5797 <                if ((u = searchFunction.apply((K)nextKey)) != null) {
5798 <                    result.compareAndSet(null, u);
5799 <                    break;
5783 >        public final U getRawResult() { return result.get(); }
5784 >        @SuppressWarnings("unchecked") public final void compute() {
5785 >            final Fun<? super K, ? extends U> searchFunction;
5786 >            final AtomicReference<U> result;
5787 >            if ((searchFunction = this.searchFunction) != null &&
5788 >                (result = this.result) != null) {
5789 >                for (int b;;) {
5790 >                    if (result.get() != null)
5791 >                        return;
5792 >                    if ((b = preSplit()) <= 0)
5793 >                        break;
5794 >                    new SearchKeysTask<K,V,U>
5795 >                        (map, this, b, searchFunction, result).fork();
5796 >                }
5797 >                while (result.get() == null) {
5798 >                    U u;
5799 >                    if (advance() == null) {
5800 >                        propagateCompletion();
5801 >                        break;
5802 >                    }
5803 >                    if ((u = searchFunction.apply((K)nextKey)) != null) {
5804 >                        if (result.compareAndSet(null, u))
5805 >                            quietlyCompleteRoot();
5806 >                        break;
5807 >                    }
5808                  }
5809              }
5274            tryComplete();
5810          }
5276        public final U getRawResult() { return result.get(); }
5811      }
5812  
5813 <    static final class SearchValuesTask<K,V,U>
5814 <        extends BulkTask<K,V,U> {
5813 >    @SuppressWarnings("serial") static final class SearchValuesTask<K,V,U>
5814 >        extends Traverser<K,V,U> {
5815          final Fun<? super V, ? extends U> searchFunction;
5816          final AtomicReference<U> result;
5817          SearchValuesTask
5818 <            (ConcurrentHashMapV8<K,V> m,
5285 <             Fun<? super V, ? extends U> searchFunction,
5286 <             AtomicReference<U> result) {
5287 <            super(m);
5288 <            this.searchFunction = searchFunction; this.result = result;
5289 <        }
5290 <        SearchValuesTask
5291 <            (BulkTask<K,V,?> p, int b, boolean split,
5818 >            (ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b,
5819               Fun<? super V, ? extends U> searchFunction,
5820               AtomicReference<U> result) {
5821 <            super(p, b, split);
5821 >            super(m, p, b);
5822              this.searchFunction = searchFunction; this.result = result;
5823          }
5824 <        public final void compute() {
5825 <            AtomicReference<U> result = this.result;
5826 <            final Fun<? super V, ? extends U> searchFunction =
5827 <                this.searchFunction;
5828 <            if (searchFunction == null || result == null)
5829 <                throw new Error(NullFunctionMessage);
5830 <            int b = batch(), c;
5831 <            while (b > 1 && baseIndex != baseLimit && result.get() == null) {
5832 <                do {} while (!casPending(c = pending, c+1));
5833 <                new SearchValuesTask<K,V,U>(this, b >>>= 1, true,
5834 <                                            searchFunction, result).fork();
5835 <            }
5836 <            Object v; U u;
5837 <            while (result.get() == null && (v = advance()) != null) {
5838 <                if ((u = searchFunction.apply((V)v)) != null) {
5839 <                    result.compareAndSet(null, u);
5840 <                    break;
5824 >        public final U getRawResult() { return result.get(); }
5825 >        @SuppressWarnings("unchecked") public final void compute() {
5826 >            final Fun<? super V, ? extends U> searchFunction;
5827 >            final AtomicReference<U> result;
5828 >            if ((searchFunction = this.searchFunction) != null &&
5829 >                (result = this.result) != null) {
5830 >                for (int b;;) {
5831 >                    if (result.get() != null)
5832 >                        return;
5833 >                    if ((b = preSplit()) <= 0)
5834 >                        break;
5835 >                    new SearchValuesTask<K,V,U>
5836 >                        (map, this, b, searchFunction, result).fork();
5837 >                }
5838 >                while (result.get() == null) {
5839 >                    V v; U u;
5840 >                    if ((v = advance()) == null) {
5841 >                        propagateCompletion();
5842 >                        break;
5843 >                    }
5844 >                    if ((u = searchFunction.apply(v)) != null) {
5845 >                        if (result.compareAndSet(null, u))
5846 >                            quietlyCompleteRoot();
5847 >                        break;
5848 >                    }
5849                  }
5850              }
5316            tryComplete();
5851          }
5318        public final U getRawResult() { return result.get(); }
5852      }
5853  
5854 <    static final class SearchEntriesTask<K,V,U>
5855 <        extends BulkTask<K,V,U> {
5854 >    @SuppressWarnings("serial") static final class SearchEntriesTask<K,V,U>
5855 >        extends Traverser<K,V,U> {
5856          final Fun<Entry<K,V>, ? extends U> searchFunction;
5857          final AtomicReference<U> result;
5858          SearchEntriesTask
5859 <            (ConcurrentHashMapV8<K,V> m,
5327 <             Fun<Entry<K,V>, ? extends U> searchFunction,
5328 <             AtomicReference<U> result) {
5329 <            super(m);
5330 <            this.searchFunction = searchFunction; this.result = result;
5331 <        }
5332 <        SearchEntriesTask
5333 <            (BulkTask<K,V,?> p, int b, boolean split,
5859 >            (ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b,
5860               Fun<Entry<K,V>, ? extends U> searchFunction,
5861               AtomicReference<U> result) {
5862 <            super(p, b, split);
5862 >            super(m, p, b);
5863              this.searchFunction = searchFunction; this.result = result;
5864          }
5865 <        public final void compute() {
5866 <            AtomicReference<U> result = this.result;
5867 <            final Fun<Entry<K,V>, ? extends U> searchFunction =
5868 <                this.searchFunction;
5869 <            if (searchFunction == null || result == null)
5870 <                throw new Error(NullFunctionMessage);
5871 <            int b = batch(), c;
5872 <            while (b > 1 && baseIndex != baseLimit && result.get() == null) {
5873 <                do {} while (!casPending(c = pending, c+1));
5874 <                new SearchEntriesTask<K,V,U>(this, b >>>= 1, true,
5875 <                                             searchFunction, result).fork();
5876 <            }
5877 <            Object v; U u;
5878 <            while (result.get() == null && (v = advance()) != null) {
5879 <                if ((u = searchFunction.apply(entryFor((K)nextKey, (V)v))) != null) {
5880 <                    result.compareAndSet(null, u);
5881 <                    break;
5865 >        public final U getRawResult() { return result.get(); }
5866 >        @SuppressWarnings("unchecked") public final void compute() {
5867 >            final Fun<Entry<K,V>, ? extends U> searchFunction;
5868 >            final AtomicReference<U> result;
5869 >            if ((searchFunction = this.searchFunction) != null &&
5870 >                (result = this.result) != null) {
5871 >                for (int b;;) {
5872 >                    if (result.get() != null)
5873 >                        return;
5874 >                    if ((b = preSplit()) <= 0)
5875 >                        break;
5876 >                    new SearchEntriesTask<K,V,U>
5877 >                        (map, this, b, searchFunction, result).fork();
5878 >                }
5879 >                while (result.get() == null) {
5880 >                    V v; U u;
5881 >                    if ((v = advance()) == null) {
5882 >                        propagateCompletion();
5883 >                        break;
5884 >                    }
5885 >                    if ((u = searchFunction.apply(entryFor((K)nextKey,
5886 >                                                           v))) != null) {
5887 >                        if (result.compareAndSet(null, u))
5888 >                            quietlyCompleteRoot();
5889 >                        return;
5890 >                    }
5891                  }
5892              }
5358            tryComplete();
5893          }
5360        public final U getRawResult() { return result.get(); }
5894      }
5895  
5896 <    static final class SearchMappingsTask<K,V,U>
5897 <        extends BulkTask<K,V,U> {
5896 >    @SuppressWarnings("serial") static final class SearchMappingsTask<K,V,U>
5897 >        extends Traverser<K,V,U> {
5898          final BiFun<? super K, ? super V, ? extends U> searchFunction;
5899          final AtomicReference<U> result;
5900          SearchMappingsTask
5901 <            (ConcurrentHashMapV8<K,V> m,
5369 <             BiFun<? super K, ? super V, ? extends U> searchFunction,
5370 <             AtomicReference<U> result) {
5371 <            super(m);
5372 <            this.searchFunction = searchFunction; this.result = result;
5373 <        }
5374 <        SearchMappingsTask
5375 <            (BulkTask<K,V,?> p, int b, boolean split,
5901 >            (ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b,
5902               BiFun<? super K, ? super V, ? extends U> searchFunction,
5903               AtomicReference<U> result) {
5904 <            super(p, b, split);
5904 >            super(m, p, b);
5905              this.searchFunction = searchFunction; this.result = result;
5906          }
5907 <        public final void compute() {
5908 <            AtomicReference<U> result = this.result;
5909 <            final BiFun<? super K, ? super V, ? extends U> searchFunction =
5910 <                this.searchFunction;
5911 <            if (searchFunction == null || result == null)
5912 <                throw new Error(NullFunctionMessage);
5913 <            int b = batch(), c;
5914 <            while (b > 1 && baseIndex != baseLimit && result.get() == null) {
5915 <                do {} while (!casPending(c = pending, c+1));
5916 <                new SearchMappingsTask<K,V,U>(this, b >>>= 1, true,
5917 <                                              searchFunction, result).fork();
5918 <            }
5919 <            Object v; U u;
5920 <            while (result.get() == null && (v = advance()) != null) {
5921 <                if ((u = searchFunction.apply((K)nextKey, (V)v)) != null) {
5922 <                    result.compareAndSet(null, u);
5923 <                    break;
5907 >        public final U getRawResult() { return result.get(); }
5908 >        @SuppressWarnings("unchecked") public final void compute() {
5909 >            final BiFun<? super K, ? super V, ? extends U> searchFunction;
5910 >            final AtomicReference<U> result;
5911 >            if ((searchFunction = this.searchFunction) != null &&
5912 >                (result = this.result) != null) {
5913 >                for (int b;;) {
5914 >                    if (result.get() != null)
5915 >                        return;
5916 >                    if ((b = preSplit()) <= 0)
5917 >                        break;
5918 >                    new SearchMappingsTask<K,V,U>
5919 >                        (map, this, b, searchFunction, result).fork();
5920 >                }
5921 >                while (result.get() == null) {
5922 >                    V v; U u;
5923 >                    if ((v = advance()) == null) {
5924 >                        propagateCompletion();
5925 >                        break;
5926 >                    }
5927 >                    if ((u = searchFunction.apply((K)nextKey, v)) != null) {
5928 >                        if (result.compareAndSet(null, u))
5929 >                            quietlyCompleteRoot();
5930 >                        break;
5931 >                    }
5932                  }
5933              }
5400            tryComplete();
5934          }
5402        public final U getRawResult() { return result.get(); }
5935      }
5936  
5937 <    static final class ReduceKeysTask<K,V>
5938 <        extends BulkTask<K,V,K> {
5937 >    @SuppressWarnings("serial") static final class ReduceKeysTask<K,V>
5938 >        extends Traverser<K,V,K> {
5939          final BiFun<? super K, ? super K, ? extends K> reducer;
5940          K result;
5941 <        ReduceKeysTask<K,V> sibling;
5410 <        ReduceKeysTask
5411 <            (ConcurrentHashMapV8<K,V> m,
5412 <             BiFun<? super K, ? super K, ? extends K> reducer) {
5413 <            super(m);
5414 <            this.reducer = reducer;
5415 <        }
5941 >        ReduceKeysTask<K,V> rights, nextRight;
5942          ReduceKeysTask
5943 <            (BulkTask<K,V,?> p, int b, boolean split,
5943 >            (ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b,
5944 >             ReduceKeysTask<K,V> nextRight,
5945               BiFun<? super K, ? super K, ? extends K> reducer) {
5946 <            super(p, b, split);
5946 >            super(m, p, b); this.nextRight = nextRight;
5947              this.reducer = reducer;
5948          }
5949 <
5950 <        public final void compute() {
5951 <            ReduceKeysTask<K,V> t = this;
5952 <            final BiFun<? super K, ? super K, ? extends K> reducer =
5953 <                this.reducer;
5954 <            if (reducer == null)
5955 <                throw new Error(NullFunctionMessage);
5956 <            int b = batch();
5957 <            while (b > 1 && t.baseIndex != t.baseLimit) {
5958 <                b >>>= 1;
5959 <                t.pending = 1;
5433 <                ReduceKeysTask<K,V> rt =
5434 <                    new ReduceKeysTask<K,V>
5435 <                    (t, b, true, reducer);
5436 <                t = new ReduceKeysTask<K,V>
5437 <                    (t, b, false, reducer);
5438 <                t.sibling = rt;
5439 <                rt.sibling = t;
5440 <                rt.fork();
5441 <            }
5442 <            K r = null;
5443 <            while (t.advance() != null) {
5444 <                K u = (K)t.nextKey;
5445 <                r = (r == null) ? u : reducer.apply(r, u);
5446 <            }
5447 <            t.result = r;
5448 <            for (;;) {
5449 <                int c; BulkTask<K,V,?> par; ReduceKeysTask<K,V> s, p; K u;
5450 <                if ((par = t.parent) == null ||
5451 <                    !(par instanceof ReduceKeysTask)) {
5452 <                    t.quietlyComplete();
5453 <                    break;
5949 >        public final K getRawResult() { return result; }
5950 >        @SuppressWarnings("unchecked") public final void compute() {
5951 >            final BiFun<? super K, ? super K, ? extends K> reducer;
5952 >            if ((reducer = this.reducer) != null) {
5953 >                for (int b; (b = preSplit()) > 0;)
5954 >                    (rights = new ReduceKeysTask<K,V>
5955 >                     (map, this, b, rights, reducer)).fork();
5956 >                K r = null;
5957 >                while (advance() != null) {
5958 >                    K u = (K)nextKey;
5959 >                    r = (r == null) ? u : reducer.apply(r, u);
5960                  }
5961 <                else if ((c = (p = (ReduceKeysTask<K,V>)par).pending) == 0) {
5962 <                    if ((s = t.sibling) != null && (u = s.result) != null)
5963 <                        r = (r == null) ? u : reducer.apply(r, u);
5964 <                    (t = p).result = r;
5961 >                result = r;
5962 >                CountedCompleter<?> c;
5963 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5964 >                    ReduceKeysTask<K,V>
5965 >                        t = (ReduceKeysTask<K,V>)c,
5966 >                        s = t.rights;
5967 >                    while (s != null) {
5968 >                        K tr, sr;
5969 >                        if ((sr = s.result) != null)
5970 >                            t.result = (((tr = t.result) == null) ? sr :
5971 >                                        reducer.apply(tr, sr));
5972 >                        s = t.rights = s.nextRight;
5973 >                    }
5974                  }
5460                else if (p.casPending(c, 0))
5461                    break;
5975              }
5976          }
5464        public final K getRawResult() { return result; }
5977      }
5978  
5979 <    static final class ReduceValuesTask<K,V>
5980 <        extends BulkTask<K,V,V> {
5979 >    @SuppressWarnings("serial") static final class ReduceValuesTask<K,V>
5980 >        extends Traverser<K,V,V> {
5981          final BiFun<? super V, ? super V, ? extends V> reducer;
5982          V result;
5983 <        ReduceValuesTask<K,V> sibling;
5472 <        ReduceValuesTask
5473 <            (ConcurrentHashMapV8<K,V> m,
5474 <             BiFun<? super V, ? super V, ? extends V> reducer) {
5475 <            super(m);
5476 <            this.reducer = reducer;
5477 <        }
5983 >        ReduceValuesTask<K,V> rights, nextRight;
5984          ReduceValuesTask
5985 <            (BulkTask<K,V,?> p, int b, boolean split,
5985 >            (ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b,
5986 >             ReduceValuesTask<K,V> nextRight,
5987               BiFun<? super V, ? super V, ? extends V> reducer) {
5988 <            super(p, b, split);
5988 >            super(m, p, b); this.nextRight = nextRight;
5989              this.reducer = reducer;
5990          }
5991 <
5992 <        public final void compute() {
5993 <            ReduceValuesTask<K,V> t = this;
5994 <            final BiFun<? super V, ? super V, ? extends V> reducer =
5995 <                this.reducer;
5996 <            if (reducer == null)
5997 <                throw new Error(NullFunctionMessage);
5998 <            int b = batch();
5999 <            while (b > 1 && t.baseIndex != t.baseLimit) {
6000 <                b >>>= 1;
6001 <                t.pending = 1;
6002 <                ReduceValuesTask<K,V> rt =
5496 <                    new ReduceValuesTask<K,V>
5497 <                    (t, b, true, reducer);
5498 <                t = new ReduceValuesTask<K,V>
5499 <                    (t, b, false, reducer);
5500 <                t.sibling = rt;
5501 <                rt.sibling = t;
5502 <                rt.fork();
5503 <            }
5504 <            V r = null;
5505 <            Object v;
5506 <            while ((v = t.advance()) != null) {
5507 <                V u = (V)v;
5508 <                r = (r == null) ? u : reducer.apply(r, u);
5509 <            }
5510 <            t.result = r;
5511 <            for (;;) {
5512 <                int c; BulkTask<K,V,?> par; ReduceValuesTask<K,V> s, p; V u;
5513 <                if ((par = t.parent) == null ||
5514 <                    !(par instanceof ReduceValuesTask)) {
5515 <                    t.quietlyComplete();
5516 <                    break;
5991 >        public final V getRawResult() { return result; }
5992 >        @SuppressWarnings("unchecked") public final void compute() {
5993 >            final BiFun<? super V, ? super V, ? extends V> reducer;
5994 >            if ((reducer = this.reducer) != null) {
5995 >                for (int b; (b = preSplit()) > 0;)
5996 >                    (rights = new ReduceValuesTask<K,V>
5997 >                     (map, this, b, rights, reducer)).fork();
5998 >                V r = null;
5999 >                V v;
6000 >                while ((v = advance()) != null) {
6001 >                    V u = v;
6002 >                    r = (r == null) ? u : reducer.apply(r, u);
6003                  }
6004 <                else if ((c = (p = (ReduceValuesTask<K,V>)par).pending) == 0) {
6005 <                    if ((s = t.sibling) != null && (u = s.result) != null)
6006 <                        r = (r == null) ? u : reducer.apply(r, u);
6007 <                    (t = p).result = r;
6004 >                result = r;
6005 >                CountedCompleter<?> c;
6006 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
6007 >                    ReduceValuesTask<K,V>
6008 >                        t = (ReduceValuesTask<K,V>)c,
6009 >                        s = t.rights;
6010 >                    while (s != null) {
6011 >                        V tr, sr;
6012 >                        if ((sr = s.result) != null)
6013 >                            t.result = (((tr = t.result) == null) ? sr :
6014 >                                        reducer.apply(tr, sr));
6015 >                        s = t.rights = s.nextRight;
6016 >                    }
6017                  }
5523                else if (p.casPending(c, 0))
5524                    break;
6018              }
6019          }
5527        public final V getRawResult() { return result; }
6020      }
6021  
6022 <    static final class ReduceEntriesTask<K,V>
6023 <        extends BulkTask<K,V,Map.Entry<K,V>> {
6022 >    @SuppressWarnings("serial") static final class ReduceEntriesTask<K,V>
6023 >        extends Traverser<K,V,Map.Entry<K,V>> {
6024          final BiFun<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer;
6025          Map.Entry<K,V> result;
6026 <        ReduceEntriesTask<K,V> sibling;
6026 >        ReduceEntriesTask<K,V> rights, nextRight;
6027          ReduceEntriesTask
6028 <            (ConcurrentHashMapV8<K,V> m,
6028 >            (ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b,
6029 >             ReduceEntriesTask<K,V> nextRight,
6030               BiFun<Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer) {
6031 <            super(m);
5539 <            this.reducer = reducer;
5540 <        }
5541 <        ReduceEntriesTask
5542 <            (BulkTask<K,V,?> p, int b, boolean split,
5543 <             BiFun<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer) {
5544 <            super(p, b, split);
6031 >            super(m, p, b); this.nextRight = nextRight;
6032              this.reducer = reducer;
6033          }
6034 <
6035 <        public final void compute() {
6036 <            ReduceEntriesTask<K,V> t = this;
6037 <            final BiFun<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer =
6038 <                this.reducer;
6039 <            if (reducer == null)
6040 <                throw new Error(NullFunctionMessage);
6041 <            int b = batch();
6042 <            while (b > 1 && t.baseIndex != t.baseLimit) {
6043 <                b >>>= 1;
6044 <                t.pending = 1;
6045 <                ReduceEntriesTask<K,V> rt =
5559 <                    new ReduceEntriesTask<K,V>
5560 <                    (t, b, true, reducer);
5561 <                t = new ReduceEntriesTask<K,V>
5562 <                    (t, b, false, reducer);
5563 <                t.sibling = rt;
5564 <                rt.sibling = t;
5565 <                rt.fork();
5566 <            }
5567 <            Map.Entry<K,V> r = null;
5568 <            Object v;
5569 <            while ((v = t.advance()) != null) {
5570 <                Map.Entry<K,V> u = entryFor((K)t.nextKey, (V)v);
5571 <                r = (r == null) ? u : reducer.apply(r, u);
5572 <            }
5573 <            t.result = r;
5574 <            for (;;) {
5575 <                int c; BulkTask<K,V,?> par; ReduceEntriesTask<K,V> s, p;
5576 <                Map.Entry<K,V> u;
5577 <                if ((par = t.parent) == null ||
5578 <                    !(par instanceof ReduceEntriesTask)) {
5579 <                    t.quietlyComplete();
5580 <                    break;
6034 >        public final Map.Entry<K,V> getRawResult() { return result; }
6035 >        @SuppressWarnings("unchecked") public final void compute() {
6036 >            final BiFun<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer;
6037 >            if ((reducer = this.reducer) != null) {
6038 >                for (int b; (b = preSplit()) > 0;)
6039 >                    (rights = new ReduceEntriesTask<K,V>
6040 >                     (map, this, b, rights, reducer)).fork();
6041 >                Map.Entry<K,V> r = null;
6042 >                V v;
6043 >                while ((v = advance()) != null) {
6044 >                    Map.Entry<K,V> u = entryFor((K)nextKey, v);
6045 >                    r = (r == null) ? u : reducer.apply(r, u);
6046                  }
6047 <                else if ((c = (p = (ReduceEntriesTask<K,V>)par).pending) == 0) {
6048 <                    if ((s = t.sibling) != null && (u = s.result) != null)
6049 <                        r = (r == null) ? u : reducer.apply(r, u);
6050 <                    (t = p).result = r;
6047 >                result = r;
6048 >                CountedCompleter<?> c;
6049 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
6050 >                    ReduceEntriesTask<K,V>
6051 >                        t = (ReduceEntriesTask<K,V>)c,
6052 >                        s = t.rights;
6053 >                    while (s != null) {
6054 >                        Map.Entry<K,V> tr, sr;
6055 >                        if ((sr = s.result) != null)
6056 >                            t.result = (((tr = t.result) == null) ? sr :
6057 >                                        reducer.apply(tr, sr));
6058 >                        s = t.rights = s.nextRight;
6059 >                    }
6060                  }
5587                else if (p.casPending(c, 0))
5588                    break;
6061              }
6062          }
5591        public final Map.Entry<K,V> getRawResult() { return result; }
6063      }
6064  
6065 <    static final class MapReduceKeysTask<K,V,U>
6066 <        extends BulkTask<K,V,U> {
6065 >    @SuppressWarnings("serial") static final class MapReduceKeysTask<K,V,U>
6066 >        extends Traverser<K,V,U> {
6067          final Fun<? super K, ? extends U> transformer;
6068          final BiFun<? super U, ? super U, ? extends U> reducer;
6069          U result;
6070 <        MapReduceKeysTask<K,V,U> sibling;
5600 <        MapReduceKeysTask
5601 <            (ConcurrentHashMapV8<K,V> m,
5602 <             Fun<? super K, ? extends U> transformer,
5603 <             BiFun<? super U, ? super U, ? extends U> reducer) {
5604 <            super(m);
5605 <            this.transformer = transformer;
5606 <            this.reducer = reducer;
5607 <        }
6070 >        MapReduceKeysTask<K,V,U> rights, nextRight;
6071          MapReduceKeysTask
6072 <            (BulkTask<K,V,?> p, int b, boolean split,
6072 >            (ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b,
6073 >             MapReduceKeysTask<K,V,U> nextRight,
6074               Fun<? super K, ? extends U> transformer,
6075               BiFun<? super U, ? super U, ? extends U> reducer) {
6076 <            super(p, b, split);
6076 >            super(m, p, b); this.nextRight = nextRight;
6077              this.transformer = transformer;
6078              this.reducer = reducer;
6079          }
6080 <        public final void compute() {
6081 <            MapReduceKeysTask<K,V,U> t = this;
6082 <            final Fun<? super K, ? extends U> transformer =
6083 <                this.transformer;
6084 <            final BiFun<? super U, ? super U, ? extends U> reducer =
6085 <                this.reducer;
6086 <            if (transformer == null || reducer == null)
6087 <                throw new Error(NullFunctionMessage);
6088 <            int b = batch();
6089 <            while (b > 1 && t.baseIndex != t.baseLimit) {
6090 <                b >>>= 1;
6091 <                t.pending = 1;
5628 <                MapReduceKeysTask<K,V,U> rt =
5629 <                    new MapReduceKeysTask<K,V,U>
5630 <                    (t, b, true, transformer, reducer);
5631 <                t = new MapReduceKeysTask<K,V,U>
5632 <                    (t, b, false, transformer, reducer);
5633 <                t.sibling = rt;
5634 <                rt.sibling = t;
5635 <                rt.fork();
5636 <            }
5637 <            U r = null, u;
5638 <            while (t.advance() != null) {
5639 <                if ((u = transformer.apply((K)t.nextKey)) != null)
5640 <                    r = (r == null) ? u : reducer.apply(r, u);
5641 <            }
5642 <            t.result = r;
5643 <            for (;;) {
5644 <                int c; BulkTask<K,V,?> par; MapReduceKeysTask<K,V,U> s, p;
5645 <                if ((par = t.parent) == null ||
5646 <                    !(par instanceof MapReduceKeysTask)) {
5647 <                    t.quietlyComplete();
5648 <                    break;
5649 <                }
5650 <                else if ((c = (p = (MapReduceKeysTask<K,V,U>)par).pending) == 0) {
5651 <                    if ((s = t.sibling) != null && (u = s.result) != null)
6080 >        public final U getRawResult() { return result; }
6081 >        @SuppressWarnings("unchecked") public final void compute() {
6082 >            final Fun<? super K, ? extends U> transformer;
6083 >            final BiFun<? super U, ? super U, ? extends U> reducer;
6084 >            if ((transformer = this.transformer) != null &&
6085 >                (reducer = this.reducer) != null) {
6086 >                for (int b; (b = preSplit()) > 0;)
6087 >                    (rights = new MapReduceKeysTask<K,V,U>
6088 >                     (map, this, b, rights, transformer, reducer)).fork();
6089 >                U r = null, u;
6090 >                while (advance() != null) {
6091 >                    if ((u = transformer.apply((K)nextKey)) != null)
6092                          r = (r == null) ? u : reducer.apply(r, u);
5653                    (t = p).result = r;
6093                  }
6094 <                else if (p.casPending(c, 0))
6095 <                    break;
6094 >                result = r;
6095 >                CountedCompleter<?> c;
6096 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
6097 >                    MapReduceKeysTask<K,V,U>
6098 >                        t = (MapReduceKeysTask<K,V,U>)c,
6099 >                        s = t.rights;
6100 >                    while (s != null) {
6101 >                        U tr, sr;
6102 >                        if ((sr = s.result) != null)
6103 >                            t.result = (((tr = t.result) == null) ? sr :
6104 >                                        reducer.apply(tr, sr));
6105 >                        s = t.rights = s.nextRight;
6106 >                    }
6107 >                }
6108              }
6109          }
5659        public final U getRawResult() { return result; }
6110      }
6111  
6112 <    static final class MapReduceValuesTask<K,V,U>
6113 <        extends BulkTask<K,V,U> {
6112 >    @SuppressWarnings("serial") static final class MapReduceValuesTask<K,V,U>
6113 >        extends Traverser<K,V,U> {
6114          final Fun<? super V, ? extends U> transformer;
6115          final BiFun<? super U, ? super U, ? extends U> reducer;
6116          U result;
6117 <        MapReduceValuesTask<K,V,U> sibling;
5668 <        MapReduceValuesTask
5669 <            (ConcurrentHashMapV8<K,V> m,
5670 <             Fun<? super V, ? extends U> transformer,
5671 <             BiFun<? super U, ? super U, ? extends U> reducer) {
5672 <            super(m);
5673 <            this.transformer = transformer;
5674 <            this.reducer = reducer;
5675 <        }
6117 >        MapReduceValuesTask<K,V,U> rights, nextRight;
6118          MapReduceValuesTask
6119 <            (BulkTask<K,V,?> p, int b, boolean split,
6119 >            (ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b,
6120 >             MapReduceValuesTask<K,V,U> nextRight,
6121               Fun<? super V, ? extends U> transformer,
6122               BiFun<? super U, ? super U, ? extends U> reducer) {
6123 <            super(p, b, split);
6123 >            super(m, p, b); this.nextRight = nextRight;
6124              this.transformer = transformer;
6125              this.reducer = reducer;
6126          }
6127 <        public final void compute() {
6128 <            MapReduceValuesTask<K,V,U> t = this;
6129 <            final Fun<? super V, ? extends U> transformer =
6130 <                this.transformer;
6131 <            final BiFun<? super U, ? super U, ? extends U> reducer =
6132 <                this.reducer;
6133 <            if (transformer == null || reducer == null)
6134 <                throw new Error(NullFunctionMessage);
6135 <            int b = batch();
6136 <            while (b > 1 && t.baseIndex != t.baseLimit) {
6137 <                b >>>= 1;
6138 <                t.pending = 1;
6139 <                MapReduceValuesTask<K,V,U> rt =
5697 <                    new MapReduceValuesTask<K,V,U>
5698 <                    (t, b, true, transformer, reducer);
5699 <                t = new MapReduceValuesTask<K,V,U>
5700 <                    (t, b, false, transformer, reducer);
5701 <                t.sibling = rt;
5702 <                rt.sibling = t;
5703 <                rt.fork();
5704 <            }
5705 <            U r = null, u;
5706 <            Object v;
5707 <            while ((v = t.advance()) != null) {
5708 <                if ((u = transformer.apply((V)v)) != null)
5709 <                    r = (r == null) ? u : reducer.apply(r, u);
5710 <            }
5711 <            t.result = r;
5712 <            for (;;) {
5713 <                int c; BulkTask<K,V,?> par; MapReduceValuesTask<K,V,U> s, p;
5714 <                if ((par = t.parent) == null ||
5715 <                    !(par instanceof MapReduceValuesTask)) {
5716 <                    t.quietlyComplete();
5717 <                    break;
5718 <                }
5719 <                else if ((c = (p = (MapReduceValuesTask<K,V,U>)par).pending) == 0) {
5720 <                    if ((s = t.sibling) != null && (u = s.result) != null)
6127 >        public final U getRawResult() { return result; }
6128 >        @SuppressWarnings("unchecked") public final void compute() {
6129 >            final Fun<? super V, ? extends U> transformer;
6130 >            final BiFun<? super U, ? super U, ? extends U> reducer;
6131 >            if ((transformer = this.transformer) != null &&
6132 >                (reducer = this.reducer) != null) {
6133 >                for (int b; (b = preSplit()) > 0;)
6134 >                    (rights = new MapReduceValuesTask<K,V,U>
6135 >                     (map, this, b, rights, transformer, reducer)).fork();
6136 >                U r = null, u;
6137 >                V v;
6138 >                while ((v = advance()) != null) {
6139 >                    if ((u = transformer.apply(v)) != null)
6140                          r = (r == null) ? u : reducer.apply(r, u);
5722                    (t = p).result = r;
6141                  }
6142 <                else if (p.casPending(c, 0))
6143 <                    break;
6142 >                result = r;
6143 >                CountedCompleter<?> c;
6144 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
6145 >                    MapReduceValuesTask<K,V,U>
6146 >                        t = (MapReduceValuesTask<K,V,U>)c,
6147 >                        s = t.rights;
6148 >                    while (s != null) {
6149 >                        U tr, sr;
6150 >                        if ((sr = s.result) != null)
6151 >                            t.result = (((tr = t.result) == null) ? sr :
6152 >                                        reducer.apply(tr, sr));
6153 >                        s = t.rights = s.nextRight;
6154 >                    }
6155 >                }
6156              }
6157          }
5728        public final U getRawResult() { return result; }
6158      }
6159  
6160 <    static final class MapReduceEntriesTask<K,V,U>
6161 <        extends BulkTask<K,V,U> {
6160 >    @SuppressWarnings("serial") static final class MapReduceEntriesTask<K,V,U>
6161 >        extends Traverser<K,V,U> {
6162          final Fun<Map.Entry<K,V>, ? extends U> transformer;
6163          final BiFun<? super U, ? super U, ? extends U> reducer;
6164          U result;
6165 <        MapReduceEntriesTask<K,V,U> sibling;
5737 <        MapReduceEntriesTask
5738 <            (ConcurrentHashMapV8<K,V> m,
5739 <             Fun<Map.Entry<K,V>, ? extends U> transformer,
5740 <             BiFun<? super U, ? super U, ? extends U> reducer) {
5741 <            super(m);
5742 <            this.transformer = transformer;
5743 <            this.reducer = reducer;
5744 <        }
6165 >        MapReduceEntriesTask<K,V,U> rights, nextRight;
6166          MapReduceEntriesTask
6167 <            (BulkTask<K,V,?> p, int b, boolean split,
6167 >            (ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b,
6168 >             MapReduceEntriesTask<K,V,U> nextRight,
6169               Fun<Map.Entry<K,V>, ? extends U> transformer,
6170               BiFun<? super U, ? super U, ? extends U> reducer) {
6171 <            super(p, b, split);
6171 >            super(m, p, b); this.nextRight = nextRight;
6172              this.transformer = transformer;
6173              this.reducer = reducer;
6174          }
6175 <        public final void compute() {
6176 <            MapReduceEntriesTask<K,V,U> t = this;
6177 <            final Fun<Map.Entry<K,V>, ? extends U> transformer =
6178 <                this.transformer;
6179 <            final BiFun<? super U, ? super U, ? extends U> reducer =
6180 <                this.reducer;
6181 <            if (transformer == null || reducer == null)
6182 <                throw new Error(NullFunctionMessage);
6183 <            int b = batch();
6184 <            while (b > 1 && t.baseIndex != t.baseLimit) {
6185 <                b >>>= 1;
6186 <                t.pending = 1;
6187 <                MapReduceEntriesTask<K,V,U> rt =
6188 <                    new MapReduceEntriesTask<K,V,U>
5767 <                    (t, b, true, transformer, reducer);
5768 <                t = new MapReduceEntriesTask<K,V,U>
5769 <                    (t, b, false, transformer, reducer);
5770 <                t.sibling = rt;
5771 <                rt.sibling = t;
5772 <                rt.fork();
5773 <            }
5774 <            U r = null, u;
5775 <            Object v;
5776 <            while ((v = t.advance()) != null) {
5777 <                if ((u = transformer.apply(entryFor((K)t.nextKey, (V)v))) != null)
5778 <                    r = (r == null) ? u : reducer.apply(r, u);
5779 <            }
5780 <            t.result = r;
5781 <            for (;;) {
5782 <                int c; BulkTask<K,V,?> par; MapReduceEntriesTask<K,V,U> s, p;
5783 <                if ((par = t.parent) == null ||
5784 <                    !(par instanceof MapReduceEntriesTask)) {
5785 <                    t.quietlyComplete();
5786 <                    break;
5787 <                }
5788 <                else if ((c = (p = (MapReduceEntriesTask<K,V,U>)par).pending) == 0) {
5789 <                    if ((s = t.sibling) != null && (u = s.result) != null)
6175 >        public final U getRawResult() { return result; }
6176 >        @SuppressWarnings("unchecked") public final void compute() {
6177 >            final Fun<Map.Entry<K,V>, ? extends U> transformer;
6178 >            final BiFun<? super U, ? super U, ? extends U> reducer;
6179 >            if ((transformer = this.transformer) != null &&
6180 >                (reducer = this.reducer) != null) {
6181 >                for (int b; (b = preSplit()) > 0;)
6182 >                    (rights = new MapReduceEntriesTask<K,V,U>
6183 >                     (map, this, b, rights, transformer, reducer)).fork();
6184 >                U r = null, u;
6185 >                V v;
6186 >                while ((v = advance()) != null) {
6187 >                    if ((u = transformer.apply(entryFor((K)nextKey,
6188 >                                                        v))) != null)
6189                          r = (r == null) ? u : reducer.apply(r, u);
5791                    (t = p).result = r;
6190                  }
6191 <                else if (p.casPending(c, 0))
6192 <                    break;
6191 >                result = r;
6192 >                CountedCompleter<?> c;
6193 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
6194 >                    MapReduceEntriesTask<K,V,U>
6195 >                        t = (MapReduceEntriesTask<K,V,U>)c,
6196 >                        s = t.rights;
6197 >                    while (s != null) {
6198 >                        U tr, sr;
6199 >                        if ((sr = s.result) != null)
6200 >                            t.result = (((tr = t.result) == null) ? sr :
6201 >                                        reducer.apply(tr, sr));
6202 >                        s = t.rights = s.nextRight;
6203 >                    }
6204 >                }
6205              }
6206          }
5797        public final U getRawResult() { return result; }
6207      }
6208  
6209 <    static final class MapReduceMappingsTask<K,V,U>
6210 <        extends BulkTask<K,V,U> {
6209 >    @SuppressWarnings("serial") static final class MapReduceMappingsTask<K,V,U>
6210 >        extends Traverser<K,V,U> {
6211          final BiFun<? super K, ? super V, ? extends U> transformer;
6212          final BiFun<? super U, ? super U, ? extends U> reducer;
6213          U result;
6214 <        MapReduceMappingsTask<K,V,U> sibling;
5806 <        MapReduceMappingsTask
5807 <            (ConcurrentHashMapV8<K,V> m,
5808 <             BiFun<? super K, ? super V, ? extends U> transformer,
5809 <             BiFun<? super U, ? super U, ? extends U> reducer) {
5810 <            super(m);
5811 <            this.transformer = transformer;
5812 <            this.reducer = reducer;
5813 <        }
6214 >        MapReduceMappingsTask<K,V,U> rights, nextRight;
6215          MapReduceMappingsTask
6216 <            (BulkTask<K,V,?> p, int b, boolean split,
6216 >            (ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b,
6217 >             MapReduceMappingsTask<K,V,U> nextRight,
6218               BiFun<? super K, ? super V, ? extends U> transformer,
6219               BiFun<? super U, ? super U, ? extends U> reducer) {
6220 <            super(p, b, split);
6220 >            super(m, p, b); this.nextRight = nextRight;
6221              this.transformer = transformer;
6222              this.reducer = reducer;
6223          }
6224 <        public final void compute() {
6225 <            MapReduceMappingsTask<K,V,U> t = this;
6226 <            final BiFun<? super K, ? super V, ? extends U> transformer =
6227 <                this.transformer;
6228 <            final BiFun<? super U, ? super U, ? extends U> reducer =
6229 <                this.reducer;
6230 <            if (transformer == null || reducer == null)
6231 <                throw new Error(NullFunctionMessage);
6232 <            int b = batch();
6233 <            while (b > 1 && t.baseIndex != t.baseLimit) {
6234 <                b >>>= 1;
6235 <                t.pending = 1;
6236 <                MapReduceMappingsTask<K,V,U> rt =
5835 <                    new MapReduceMappingsTask<K,V,U>
5836 <                    (t, b, true, transformer, reducer);
5837 <                t = new MapReduceMappingsTask<K,V,U>
5838 <                    (t, b, false, transformer, reducer);
5839 <                t.sibling = rt;
5840 <                rt.sibling = t;
5841 <                rt.fork();
5842 <            }
5843 <            U r = null, u;
5844 <            Object v;
5845 <            while ((v = t.advance()) != null) {
5846 <                if ((u = transformer.apply((K)t.nextKey, (V)v)) != null)
5847 <                    r = (r == null) ? u : reducer.apply(r, u);
5848 <            }
5849 <            for (;;) {
5850 <                int c; BulkTask<K,V,?> par; MapReduceMappingsTask<K,V,U> s, p;
5851 <                if ((par = t.parent) == null ||
5852 <                    !(par instanceof MapReduceMappingsTask)) {
5853 <                    t.quietlyComplete();
5854 <                    break;
5855 <                }
5856 <                else if ((c = (p = (MapReduceMappingsTask<K,V,U>)par).pending) == 0) {
5857 <                    if ((s = t.sibling) != null && (u = s.result) != null)
6224 >        public final U getRawResult() { return result; }
6225 >        @SuppressWarnings("unchecked") public final void compute() {
6226 >            final BiFun<? super K, ? super V, ? extends U> transformer;
6227 >            final BiFun<? super U, ? super U, ? extends U> reducer;
6228 >            if ((transformer = this.transformer) != null &&
6229 >                (reducer = this.reducer) != null) {
6230 >                for (int b; (b = preSplit()) > 0;)
6231 >                    (rights = new MapReduceMappingsTask<K,V,U>
6232 >                     (map, this, b, rights, transformer, reducer)).fork();
6233 >                U r = null, u;
6234 >                V v;
6235 >                while ((v = advance()) != null) {
6236 >                    if ((u = transformer.apply((K)nextKey, v)) != null)
6237                          r = (r == null) ? u : reducer.apply(r, u);
5859                    (t = p).result = r;
6238                  }
6239 <                else if (p.casPending(c, 0))
6240 <                    break;
6239 >                result = r;
6240 >                CountedCompleter<?> c;
6241 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
6242 >                    MapReduceMappingsTask<K,V,U>
6243 >                        t = (MapReduceMappingsTask<K,V,U>)c,
6244 >                        s = t.rights;
6245 >                    while (s != null) {
6246 >                        U tr, sr;
6247 >                        if ((sr = s.result) != null)
6248 >                            t.result = (((tr = t.result) == null) ? sr :
6249 >                                        reducer.apply(tr, sr));
6250 >                        s = t.rights = s.nextRight;
6251 >                    }
6252 >                }
6253              }
6254          }
5865        public final U getRawResult() { return result; }
6255      }
6256  
6257 <    static final class MapReduceKeysToDoubleTask<K,V>
6258 <        extends BulkTask<K,V,Double> {
6257 >    @SuppressWarnings("serial") static final class MapReduceKeysToDoubleTask<K,V>
6258 >        extends Traverser<K,V,Double> {
6259          final ObjectToDouble<? super K> transformer;
6260          final DoubleByDoubleToDouble reducer;
6261          final double basis;
6262          double result;
6263 <        MapReduceKeysToDoubleTask<K,V> sibling;
5875 <        MapReduceKeysToDoubleTask
5876 <            (ConcurrentHashMapV8<K,V> m,
5877 <             ObjectToDouble<? super K> transformer,
5878 <             double basis,
5879 <             DoubleByDoubleToDouble reducer) {
5880 <            super(m);
5881 <            this.transformer = transformer;
5882 <            this.basis = basis; this.reducer = reducer;
5883 <        }
6263 >        MapReduceKeysToDoubleTask<K,V> rights, nextRight;
6264          MapReduceKeysToDoubleTask
6265 <            (BulkTask<K,V,?> p, int b, boolean split,
6265 >            (ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b,
6266 >             MapReduceKeysToDoubleTask<K,V> nextRight,
6267               ObjectToDouble<? super K> transformer,
6268               double basis,
6269               DoubleByDoubleToDouble reducer) {
6270 <            super(p, b, split);
6270 >            super(m, p, b); this.nextRight = nextRight;
6271              this.transformer = transformer;
6272              this.basis = basis; this.reducer = reducer;
6273          }
6274 <        public final void compute() {
6275 <            MapReduceKeysToDoubleTask<K,V> t = this;
6276 <            final ObjectToDouble<? super K> transformer =
6277 <                this.transformer;
6278 <            final DoubleByDoubleToDouble reducer = this.reducer;
6279 <            if (transformer == null || reducer == null)
6280 <                throw new Error(NullFunctionMessage);
6281 <            final double id = this.basis;
6282 <            int b = batch();
6283 <            while (b > 1 && t.baseIndex != t.baseLimit) {
6284 <                b >>>= 1;
6285 <                t.pending = 1;
6286 <                MapReduceKeysToDoubleTask<K,V> rt =
6287 <                    new MapReduceKeysToDoubleTask<K,V>
6288 <                    (t, b, true, transformer, id, reducer);
6289 <                t = new MapReduceKeysToDoubleTask<K,V>
6290 <                    (t, b, false, transformer, id, reducer);
6291 <                t.sibling = rt;
6292 <                rt.sibling = t;
6293 <                rt.fork();
6294 <            }
6295 <            double r = id;
5915 <            while (t.advance() != null)
5916 <                r = reducer.apply(r, transformer.apply((K)t.nextKey));
5917 <            t.result = r;
5918 <            for (;;) {
5919 <                int c; BulkTask<K,V,?> par; MapReduceKeysToDoubleTask<K,V> s, p;
5920 <                if ((par = t.parent) == null ||
5921 <                    !(par instanceof MapReduceKeysToDoubleTask)) {
5922 <                    t.quietlyComplete();
5923 <                    break;
5924 <                }
5925 <                else if ((c = (p = (MapReduceKeysToDoubleTask<K,V>)par).pending) == 0) {
5926 <                    if ((s = t.sibling) != null)
5927 <                        r = reducer.apply(r, s.result);
5928 <                    (t = p).result = r;
6274 >        public final Double getRawResult() { return result; }
6275 >        @SuppressWarnings("unchecked") public final void compute() {
6276 >            final ObjectToDouble<? super K> transformer;
6277 >            final DoubleByDoubleToDouble reducer;
6278 >            if ((transformer = this.transformer) != null &&
6279 >                (reducer = this.reducer) != null) {
6280 >                double r = this.basis;
6281 >                for (int b; (b = preSplit()) > 0;)
6282 >                    (rights = new MapReduceKeysToDoubleTask<K,V>
6283 >                     (map, this, b, rights, transformer, r, reducer)).fork();
6284 >                while (advance() != null)
6285 >                    r = reducer.apply(r, transformer.apply((K)nextKey));
6286 >                result = r;
6287 >                CountedCompleter<?> c;
6288 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
6289 >                    MapReduceKeysToDoubleTask<K,V>
6290 >                        t = (MapReduceKeysToDoubleTask<K,V>)c,
6291 >                        s = t.rights;
6292 >                    while (s != null) {
6293 >                        t.result = reducer.apply(t.result, s.result);
6294 >                        s = t.rights = s.nextRight;
6295 >                    }
6296                  }
5930                else if (p.casPending(c, 0))
5931                    break;
6297              }
6298          }
5934        public final Double getRawResult() { return result; }
6299      }
6300  
6301 <    static final class MapReduceValuesToDoubleTask<K,V>
6302 <        extends BulkTask<K,V,Double> {
6301 >    @SuppressWarnings("serial") static final class MapReduceValuesToDoubleTask<K,V>
6302 >        extends Traverser<K,V,Double> {
6303          final ObjectToDouble<? super V> transformer;
6304          final DoubleByDoubleToDouble reducer;
6305          final double basis;
6306          double result;
6307 <        MapReduceValuesToDoubleTask<K,V> sibling;
5944 <        MapReduceValuesToDoubleTask
5945 <            (ConcurrentHashMapV8<K,V> m,
5946 <             ObjectToDouble<? super V> transformer,
5947 <             double basis,
5948 <             DoubleByDoubleToDouble reducer) {
5949 <            super(m);
5950 <            this.transformer = transformer;
5951 <            this.basis = basis; this.reducer = reducer;
5952 <        }
6307 >        MapReduceValuesToDoubleTask<K,V> rights, nextRight;
6308          MapReduceValuesToDoubleTask
6309 <            (BulkTask<K,V,?> p, int b, boolean split,
6309 >            (ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b,
6310 >             MapReduceValuesToDoubleTask<K,V> nextRight,
6311               ObjectToDouble<? super V> transformer,
6312               double basis,
6313               DoubleByDoubleToDouble reducer) {
6314 <            super(p, b, split);
6314 >            super(m, p, b); this.nextRight = nextRight;
6315              this.transformer = transformer;
6316              this.basis = basis; this.reducer = reducer;
6317          }
6318 <        public final void compute() {
6319 <            MapReduceValuesToDoubleTask<K,V> t = this;
6320 <            final ObjectToDouble<? super V> transformer =
6321 <                this.transformer;
6322 <            final DoubleByDoubleToDouble reducer = this.reducer;
6323 <            if (transformer == null || reducer == null)
6324 <                throw new Error(NullFunctionMessage);
6325 <            final double id = this.basis;
6326 <            int b = batch();
6327 <            while (b > 1 && t.baseIndex != t.baseLimit) {
6328 <                b >>>= 1;
6329 <                t.pending = 1;
6330 <                MapReduceValuesToDoubleTask<K,V> rt =
6331 <                    new MapReduceValuesToDoubleTask<K,V>
6332 <                    (t, b, true, transformer, id, reducer);
6333 <                t = new MapReduceValuesToDoubleTask<K,V>
6334 <                    (t, b, false, transformer, id, reducer);
6335 <                t.sibling = rt;
6336 <                rt.sibling = t;
6337 <                rt.fork();
6338 <            }
6339 <            double r = id;
6340 <            Object v;
5985 <            while ((v = t.advance()) != null)
5986 <                r = reducer.apply(r, transformer.apply((V)v));
5987 <            t.result = r;
5988 <            for (;;) {
5989 <                int c; BulkTask<K,V,?> par; MapReduceValuesToDoubleTask<K,V> s, p;
5990 <                if ((par = t.parent) == null ||
5991 <                    !(par instanceof MapReduceValuesToDoubleTask)) {
5992 <                    t.quietlyComplete();
5993 <                    break;
5994 <                }
5995 <                else if ((c = (p = (MapReduceValuesToDoubleTask<K,V>)par).pending) == 0) {
5996 <                    if ((s = t.sibling) != null)
5997 <                        r = reducer.apply(r, s.result);
5998 <                    (t = p).result = r;
6318 >        public final Double getRawResult() { return result; }
6319 >        @SuppressWarnings("unchecked") public final void compute() {
6320 >            final ObjectToDouble<? super V> transformer;
6321 >            final DoubleByDoubleToDouble reducer;
6322 >            if ((transformer = this.transformer) != null &&
6323 >                (reducer = this.reducer) != null) {
6324 >                double r = this.basis;
6325 >                for (int b; (b = preSplit()) > 0;)
6326 >                    (rights = new MapReduceValuesToDoubleTask<K,V>
6327 >                     (map, this, b, rights, transformer, r, reducer)).fork();
6328 >                V v;
6329 >                while ((v = advance()) != null)
6330 >                    r = reducer.apply(r, transformer.apply(v));
6331 >                result = r;
6332 >                CountedCompleter<?> c;
6333 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
6334 >                    MapReduceValuesToDoubleTask<K,V>
6335 >                        t = (MapReduceValuesToDoubleTask<K,V>)c,
6336 >                        s = t.rights;
6337 >                    while (s != null) {
6338 >                        t.result = reducer.apply(t.result, s.result);
6339 >                        s = t.rights = s.nextRight;
6340 >                    }
6341                  }
6000                else if (p.casPending(c, 0))
6001                    break;
6342              }
6343          }
6004        public final Double getRawResult() { return result; }
6344      }
6345  
6346 <    static final class MapReduceEntriesToDoubleTask<K,V>
6347 <        extends BulkTask<K,V,Double> {
6346 >    @SuppressWarnings("serial") static final class MapReduceEntriesToDoubleTask<K,V>
6347 >        extends Traverser<K,V,Double> {
6348          final ObjectToDouble<Map.Entry<K,V>> transformer;
6349          final DoubleByDoubleToDouble reducer;
6350          final double basis;
6351          double result;
6352 <        MapReduceEntriesToDoubleTask<K,V> sibling;
6014 <        MapReduceEntriesToDoubleTask
6015 <            (ConcurrentHashMapV8<K,V> m,
6016 <             ObjectToDouble<Map.Entry<K,V>> transformer,
6017 <             double basis,
6018 <             DoubleByDoubleToDouble reducer) {
6019 <            super(m);
6020 <            this.transformer = transformer;
6021 <            this.basis = basis; this.reducer = reducer;
6022 <        }
6352 >        MapReduceEntriesToDoubleTask<K,V> rights, nextRight;
6353          MapReduceEntriesToDoubleTask
6354 <            (BulkTask<K,V,?> p, int b, boolean split,
6354 >            (ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b,
6355 >             MapReduceEntriesToDoubleTask<K,V> nextRight,
6356               ObjectToDouble<Map.Entry<K,V>> transformer,
6357               double basis,
6358               DoubleByDoubleToDouble reducer) {
6359 <            super(p, b, split);
6359 >            super(m, p, b); this.nextRight = nextRight;
6360              this.transformer = transformer;
6361              this.basis = basis; this.reducer = reducer;
6362          }
6363 <        public final void compute() {
6364 <            MapReduceEntriesToDoubleTask<K,V> t = this;
6365 <            final ObjectToDouble<Map.Entry<K,V>> transformer =
6366 <                this.transformer;
6367 <            final DoubleByDoubleToDouble reducer = this.reducer;
6368 <            if (transformer == null || reducer == null)
6369 <                throw new Error(NullFunctionMessage);
6370 <            final double id = this.basis;
6371 <            int b = batch();
6372 <            while (b > 1 && t.baseIndex != t.baseLimit) {
6373 <                b >>>= 1;
6374 <                t.pending = 1;
6375 <                MapReduceEntriesToDoubleTask<K,V> rt =
6376 <                    new MapReduceEntriesToDoubleTask<K,V>
6377 <                    (t, b, true, transformer, id, reducer);
6378 <                t = new MapReduceEntriesToDoubleTask<K,V>
6379 <                    (t, b, false, transformer, id, reducer);
6380 <                t.sibling = rt;
6381 <                rt.sibling = t;
6382 <                rt.fork();
6383 <            }
6384 <            double r = id;
6385 <            Object v;
6386 <            while ((v = t.advance()) != null)
6056 <                r = reducer.apply(r, transformer.apply(entryFor((K)t.nextKey, (V)v)));
6057 <            t.result = r;
6058 <            for (;;) {
6059 <                int c; BulkTask<K,V,?> par; MapReduceEntriesToDoubleTask<K,V> s, p;
6060 <                if ((par = t.parent) == null ||
6061 <                    !(par instanceof MapReduceEntriesToDoubleTask)) {
6062 <                    t.quietlyComplete();
6063 <                    break;
6064 <                }
6065 <                else if ((c = (p = (MapReduceEntriesToDoubleTask<K,V>)par).pending) == 0) {
6066 <                    if ((s = t.sibling) != null)
6067 <                        r = reducer.apply(r, s.result);
6068 <                    (t = p).result = r;
6363 >        public final Double getRawResult() { return result; }
6364 >        @SuppressWarnings("unchecked") public final void compute() {
6365 >            final ObjectToDouble<Map.Entry<K,V>> transformer;
6366 >            final DoubleByDoubleToDouble reducer;
6367 >            if ((transformer = this.transformer) != null &&
6368 >                (reducer = this.reducer) != null) {
6369 >                double r = this.basis;
6370 >                for (int b; (b = preSplit()) > 0;)
6371 >                    (rights = new MapReduceEntriesToDoubleTask<K,V>
6372 >                     (map, this, b, rights, transformer, r, reducer)).fork();
6373 >                V v;
6374 >                while ((v = advance()) != null)
6375 >                    r = reducer.apply(r, transformer.apply(entryFor((K)nextKey,
6376 >                                                                    v)));
6377 >                result = r;
6378 >                CountedCompleter<?> c;
6379 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
6380 >                    MapReduceEntriesToDoubleTask<K,V>
6381 >                        t = (MapReduceEntriesToDoubleTask<K,V>)c,
6382 >                        s = t.rights;
6383 >                    while (s != null) {
6384 >                        t.result = reducer.apply(t.result, s.result);
6385 >                        s = t.rights = s.nextRight;
6386 >                    }
6387                  }
6070                else if (p.casPending(c, 0))
6071                    break;
6388              }
6389          }
6074        public final Double getRawResult() { return result; }
6390      }
6391  
6392 <    static final class MapReduceMappingsToDoubleTask<K,V>
6393 <        extends BulkTask<K,V,Double> {
6392 >    @SuppressWarnings("serial") static final class MapReduceMappingsToDoubleTask<K,V>
6393 >        extends Traverser<K,V,Double> {
6394          final ObjectByObjectToDouble<? super K, ? super V> transformer;
6395          final DoubleByDoubleToDouble reducer;
6396          final double basis;
6397          double result;
6398 <        MapReduceMappingsToDoubleTask<K,V> sibling;
6398 >        MapReduceMappingsToDoubleTask<K,V> rights, nextRight;
6399          MapReduceMappingsToDoubleTask
6400 <            (ConcurrentHashMapV8<K,V> m,
6400 >            (ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b,
6401 >             MapReduceMappingsToDoubleTask<K,V> nextRight,
6402               ObjectByObjectToDouble<? super K, ? super V> transformer,
6403               double basis,
6404               DoubleByDoubleToDouble reducer) {
6405 <            super(m);
6405 >            super(m, p, b); this.nextRight = nextRight;
6406              this.transformer = transformer;
6407              this.basis = basis; this.reducer = reducer;
6408          }
6409 <        MapReduceMappingsToDoubleTask
6410 <            (BulkTask<K,V,?> p, int b, boolean split,
6411 <             ObjectByObjectToDouble<? super K, ? super V> transformer,
6412 <             double basis,
6413 <             DoubleByDoubleToDouble reducer) {
6414 <            super(p, b, split);
6415 <            this.transformer = transformer;
6416 <            this.basis = basis; this.reducer = reducer;
6417 <        }
6418 <        public final void compute() {
6419 <            MapReduceMappingsToDoubleTask<K,V> t = this;
6420 <            final ObjectByObjectToDouble<? super K, ? super V> transformer =
6421 <                this.transformer;
6422 <            final DoubleByDoubleToDouble reducer = this.reducer;
6423 <            if (transformer == null || reducer == null)
6424 <                throw new Error(NullFunctionMessage);
6425 <            final double id = this.basis;
6426 <            int b = batch();
6427 <            while (b > 1 && t.baseIndex != t.baseLimit) {
6428 <                b >>>= 1;
6429 <                t.pending = 1;
6430 <                MapReduceMappingsToDoubleTask<K,V> rt =
6431 <                    new MapReduceMappingsToDoubleTask<K,V>
6116 <                    (t, b, true, transformer, id, reducer);
6117 <                t = new MapReduceMappingsToDoubleTask<K,V>
6118 <                    (t, b, false, transformer, id, reducer);
6119 <                t.sibling = rt;
6120 <                rt.sibling = t;
6121 <                rt.fork();
6122 <            }
6123 <            double r = id;
6124 <            Object v;
6125 <            while ((v = t.advance()) != null)
6126 <                r = reducer.apply(r, transformer.apply((K)t.nextKey, (V)v));
6127 <            t.result = r;
6128 <            for (;;) {
6129 <                int c; BulkTask<K,V,?> par; MapReduceMappingsToDoubleTask<K,V> s, p;
6130 <                if ((par = t.parent) == null ||
6131 <                    !(par instanceof MapReduceMappingsToDoubleTask)) {
6132 <                    t.quietlyComplete();
6133 <                    break;
6134 <                }
6135 <                else if ((c = (p = (MapReduceMappingsToDoubleTask<K,V>)par).pending) == 0) {
6136 <                    if ((s = t.sibling) != null)
6137 <                        r = reducer.apply(r, s.result);
6138 <                    (t = p).result = r;
6409 >        public final Double getRawResult() { return result; }
6410 >        @SuppressWarnings("unchecked") public final void compute() {
6411 >            final ObjectByObjectToDouble<? super K, ? super V> transformer;
6412 >            final DoubleByDoubleToDouble reducer;
6413 >            if ((transformer = this.transformer) != null &&
6414 >                (reducer = this.reducer) != null) {
6415 >                double r = this.basis;
6416 >                for (int b; (b = preSplit()) > 0;)
6417 >                    (rights = new MapReduceMappingsToDoubleTask<K,V>
6418 >                     (map, this, b, rights, transformer, r, reducer)).fork();
6419 >                V v;
6420 >                while ((v = advance()) != null)
6421 >                    r = reducer.apply(r, transformer.apply((K)nextKey, v));
6422 >                result = r;
6423 >                CountedCompleter<?> c;
6424 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
6425 >                    MapReduceMappingsToDoubleTask<K,V>
6426 >                        t = (MapReduceMappingsToDoubleTask<K,V>)c,
6427 >                        s = t.rights;
6428 >                    while (s != null) {
6429 >                        t.result = reducer.apply(t.result, s.result);
6430 >                        s = t.rights = s.nextRight;
6431 >                    }
6432                  }
6140                else if (p.casPending(c, 0))
6141                    break;
6433              }
6434          }
6144        public final Double getRawResult() { return result; }
6435      }
6436  
6437 <    static final class MapReduceKeysToLongTask<K,V>
6438 <        extends BulkTask<K,V,Long> {
6437 >    @SuppressWarnings("serial") static final class MapReduceKeysToLongTask<K,V>
6438 >        extends Traverser<K,V,Long> {
6439          final ObjectToLong<? super K> transformer;
6440          final LongByLongToLong reducer;
6441          final long basis;
6442          long result;
6443 <        MapReduceKeysToLongTask<K,V> sibling;
6154 <        MapReduceKeysToLongTask
6155 <            (ConcurrentHashMapV8<K,V> m,
6156 <             ObjectToLong<? super K> transformer,
6157 <             long basis,
6158 <             LongByLongToLong reducer) {
6159 <            super(m);
6160 <            this.transformer = transformer;
6161 <            this.basis = basis; this.reducer = reducer;
6162 <        }
6443 >        MapReduceKeysToLongTask<K,V> rights, nextRight;
6444          MapReduceKeysToLongTask
6445 <            (BulkTask<K,V,?> p, int b, boolean split,
6445 >            (ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b,
6446 >             MapReduceKeysToLongTask<K,V> nextRight,
6447               ObjectToLong<? super K> transformer,
6448               long basis,
6449               LongByLongToLong reducer) {
6450 <            super(p, b, split);
6450 >            super(m, p, b); this.nextRight = nextRight;
6451              this.transformer = transformer;
6452              this.basis = basis; this.reducer = reducer;
6453          }
6454 <        public final void compute() {
6455 <            MapReduceKeysToLongTask<K,V> t = this;
6456 <            final ObjectToLong<? super K> transformer =
6457 <                this.transformer;
6458 <            final LongByLongToLong reducer = this.reducer;
6459 <            if (transformer == null || reducer == null)
6460 <                throw new Error(NullFunctionMessage);
6461 <            final long id = this.basis;
6462 <            int b = batch();
6463 <            while (b > 1 && t.baseIndex != t.baseLimit) {
6464 <                b >>>= 1;
6465 <                t.pending = 1;
6466 <                MapReduceKeysToLongTask<K,V> rt =
6467 <                    new MapReduceKeysToLongTask<K,V>
6468 <                    (t, b, true, transformer, id, reducer);
6469 <                t = new MapReduceKeysToLongTask<K,V>
6470 <                    (t, b, false, transformer, id, reducer);
6471 <                t.sibling = rt;
6472 <                rt.sibling = t;
6473 <                rt.fork();
6474 <            }
6475 <            long r = id;
6194 <            while (t.advance() != null)
6195 <                r = reducer.apply(r, transformer.apply((K)t.nextKey));
6196 <            t.result = r;
6197 <            for (;;) {
6198 <                int c; BulkTask<K,V,?> par; MapReduceKeysToLongTask<K,V> s, p;
6199 <                if ((par = t.parent) == null ||
6200 <                    !(par instanceof MapReduceKeysToLongTask)) {
6201 <                    t.quietlyComplete();
6202 <                    break;
6203 <                }
6204 <                else if ((c = (p = (MapReduceKeysToLongTask<K,V>)par).pending) == 0) {
6205 <                    if ((s = t.sibling) != null)
6206 <                        r = reducer.apply(r, s.result);
6207 <                    (t = p).result = r;
6454 >        public final Long getRawResult() { return result; }
6455 >        @SuppressWarnings("unchecked") public final void compute() {
6456 >            final ObjectToLong<? super K> transformer;
6457 >            final LongByLongToLong reducer;
6458 >            if ((transformer = this.transformer) != null &&
6459 >                (reducer = this.reducer) != null) {
6460 >                long r = this.basis;
6461 >                for (int b; (b = preSplit()) > 0;)
6462 >                    (rights = new MapReduceKeysToLongTask<K,V>
6463 >                     (map, this, b, rights, transformer, r, reducer)).fork();
6464 >                while (advance() != null)
6465 >                    r = reducer.apply(r, transformer.apply((K)nextKey));
6466 >                result = r;
6467 >                CountedCompleter<?> c;
6468 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
6469 >                    MapReduceKeysToLongTask<K,V>
6470 >                        t = (MapReduceKeysToLongTask<K,V>)c,
6471 >                        s = t.rights;
6472 >                    while (s != null) {
6473 >                        t.result = reducer.apply(t.result, s.result);
6474 >                        s = t.rights = s.nextRight;
6475 >                    }
6476                  }
6209                else if (p.casPending(c, 0))
6210                    break;
6477              }
6478          }
6213        public final Long getRawResult() { return result; }
6479      }
6480  
6481 <    static final class MapReduceValuesToLongTask<K,V>
6482 <        extends BulkTask<K,V,Long> {
6481 >    @SuppressWarnings("serial") static final class MapReduceValuesToLongTask<K,V>
6482 >        extends Traverser<K,V,Long> {
6483          final ObjectToLong<? super V> transformer;
6484          final LongByLongToLong reducer;
6485          final long basis;
6486          long result;
6487 <        MapReduceValuesToLongTask<K,V> sibling;
6223 <        MapReduceValuesToLongTask
6224 <            (ConcurrentHashMapV8<K,V> m,
6225 <             ObjectToLong<? super V> transformer,
6226 <             long basis,
6227 <             LongByLongToLong reducer) {
6228 <            super(m);
6229 <            this.transformer = transformer;
6230 <            this.basis = basis; this.reducer = reducer;
6231 <        }
6487 >        MapReduceValuesToLongTask<K,V> rights, nextRight;
6488          MapReduceValuesToLongTask
6489 <            (BulkTask<K,V,?> p, int b, boolean split,
6489 >            (ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b,
6490 >             MapReduceValuesToLongTask<K,V> nextRight,
6491               ObjectToLong<? super V> transformer,
6492               long basis,
6493               LongByLongToLong reducer) {
6494 <            super(p, b, split);
6494 >            super(m, p, b); this.nextRight = nextRight;
6495              this.transformer = transformer;
6496              this.basis = basis; this.reducer = reducer;
6497          }
6498 <        public final void compute() {
6499 <            MapReduceValuesToLongTask<K,V> t = this;
6500 <            final ObjectToLong<? super V> transformer =
6501 <                this.transformer;
6502 <            final LongByLongToLong reducer = this.reducer;
6503 <            if (transformer == null || reducer == null)
6504 <                throw new Error(NullFunctionMessage);
6505 <            final long id = this.basis;
6506 <            int b = batch();
6507 <            while (b > 1 && t.baseIndex != t.baseLimit) {
6508 <                b >>>= 1;
6509 <                t.pending = 1;
6510 <                MapReduceValuesToLongTask<K,V> rt =
6511 <                    new MapReduceValuesToLongTask<K,V>
6512 <                    (t, b, true, transformer, id, reducer);
6513 <                t = new MapReduceValuesToLongTask<K,V>
6514 <                    (t, b, false, transformer, id, reducer);
6515 <                t.sibling = rt;
6516 <                rt.sibling = t;
6517 <                rt.fork();
6518 <            }
6519 <            long r = id;
6520 <            Object v;
6264 <            while ((v = t.advance()) != null)
6265 <                r = reducer.apply(r, transformer.apply((V)v));
6266 <            t.result = r;
6267 <            for (;;) {
6268 <                int c; BulkTask<K,V,?> par; MapReduceValuesToLongTask<K,V> s, p;
6269 <                if ((par = t.parent) == null ||
6270 <                    !(par instanceof MapReduceValuesToLongTask)) {
6271 <                    t.quietlyComplete();
6272 <                    break;
6273 <                }
6274 <                else if ((c = (p = (MapReduceValuesToLongTask<K,V>)par).pending) == 0) {
6275 <                    if ((s = t.sibling) != null)
6276 <                        r = reducer.apply(r, s.result);
6277 <                    (t = p).result = r;
6498 >        public final Long getRawResult() { return result; }
6499 >        @SuppressWarnings("unchecked") public final void compute() {
6500 >            final ObjectToLong<? super V> transformer;
6501 >            final LongByLongToLong reducer;
6502 >            if ((transformer = this.transformer) != null &&
6503 >                (reducer = this.reducer) != null) {
6504 >                long r = this.basis;
6505 >                for (int b; (b = preSplit()) > 0;)
6506 >                    (rights = new MapReduceValuesToLongTask<K,V>
6507 >                     (map, this, b, rights, transformer, r, reducer)).fork();
6508 >                V v;
6509 >                while ((v = advance()) != null)
6510 >                    r = reducer.apply(r, transformer.apply(v));
6511 >                result = r;
6512 >                CountedCompleter<?> c;
6513 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
6514 >                    MapReduceValuesToLongTask<K,V>
6515 >                        t = (MapReduceValuesToLongTask<K,V>)c,
6516 >                        s = t.rights;
6517 >                    while (s != null) {
6518 >                        t.result = reducer.apply(t.result, s.result);
6519 >                        s = t.rights = s.nextRight;
6520 >                    }
6521                  }
6279                else if (p.casPending(c, 0))
6280                    break;
6522              }
6523          }
6283        public final Long getRawResult() { return result; }
6524      }
6525  
6526 <    static final class MapReduceEntriesToLongTask<K,V>
6527 <        extends BulkTask<K,V,Long> {
6526 >    @SuppressWarnings("serial") static final class MapReduceEntriesToLongTask<K,V>
6527 >        extends Traverser<K,V,Long> {
6528          final ObjectToLong<Map.Entry<K,V>> transformer;
6529          final LongByLongToLong reducer;
6530          final long basis;
6531          long result;
6532 <        MapReduceEntriesToLongTask<K,V> sibling;
6293 <        MapReduceEntriesToLongTask
6294 <            (ConcurrentHashMapV8<K,V> m,
6295 <             ObjectToLong<Map.Entry<K,V>> transformer,
6296 <             long basis,
6297 <             LongByLongToLong reducer) {
6298 <            super(m);
6299 <            this.transformer = transformer;
6300 <            this.basis = basis; this.reducer = reducer;
6301 <        }
6532 >        MapReduceEntriesToLongTask<K,V> rights, nextRight;
6533          MapReduceEntriesToLongTask
6534 <            (BulkTask<K,V,?> p, int b, boolean split,
6534 >            (ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b,
6535 >             MapReduceEntriesToLongTask<K,V> nextRight,
6536               ObjectToLong<Map.Entry<K,V>> transformer,
6537               long basis,
6538               LongByLongToLong reducer) {
6539 <            super(p, b, split);
6539 >            super(m, p, b); this.nextRight = nextRight;
6540              this.transformer = transformer;
6541              this.basis = basis; this.reducer = reducer;
6542          }
6543 <        public final void compute() {
6544 <            MapReduceEntriesToLongTask<K,V> t = this;
6545 <            final ObjectToLong<Map.Entry<K,V>> transformer =
6546 <                this.transformer;
6547 <            final LongByLongToLong reducer = this.reducer;
6548 <            if (transformer == null || reducer == null)
6549 <                throw new Error(NullFunctionMessage);
6550 <            final long id = this.basis;
6551 <            int b = batch();
6552 <            while (b > 1 && t.baseIndex != t.baseLimit) {
6553 <                b >>>= 1;
6554 <                t.pending = 1;
6555 <                MapReduceEntriesToLongTask<K,V> rt =
6556 <                    new MapReduceEntriesToLongTask<K,V>
6557 <                    (t, b, true, transformer, id, reducer);
6558 <                t = new MapReduceEntriesToLongTask<K,V>
6559 <                    (t, b, false, transformer, id, reducer);
6560 <                t.sibling = rt;
6561 <                rt.sibling = t;
6562 <                rt.fork();
6563 <            }
6564 <            long r = id;
6565 <            Object v;
6566 <            while ((v = t.advance()) != null)
6335 <                r = reducer.apply(r, transformer.apply(entryFor((K)t.nextKey, (V)v)));
6336 <            t.result = r;
6337 <            for (;;) {
6338 <                int c; BulkTask<K,V,?> par; MapReduceEntriesToLongTask<K,V> s, p;
6339 <                if ((par = t.parent) == null ||
6340 <                    !(par instanceof MapReduceEntriesToLongTask)) {
6341 <                    t.quietlyComplete();
6342 <                    break;
6343 <                }
6344 <                else if ((c = (p = (MapReduceEntriesToLongTask<K,V>)par).pending) == 0) {
6345 <                    if ((s = t.sibling) != null)
6346 <                        r = reducer.apply(r, s.result);
6347 <                    (t = p).result = r;
6543 >        public final Long getRawResult() { return result; }
6544 >        @SuppressWarnings("unchecked") public final void compute() {
6545 >            final ObjectToLong<Map.Entry<K,V>> transformer;
6546 >            final LongByLongToLong reducer;
6547 >            if ((transformer = this.transformer) != null &&
6548 >                (reducer = this.reducer) != null) {
6549 >                long r = this.basis;
6550 >                for (int b; (b = preSplit()) > 0;)
6551 >                    (rights = new MapReduceEntriesToLongTask<K,V>
6552 >                     (map, this, b, rights, transformer, r, reducer)).fork();
6553 >                V v;
6554 >                while ((v = advance()) != null)
6555 >                    r = reducer.apply(r, transformer.apply(entryFor((K)nextKey,
6556 >                                                                    v)));
6557 >                result = r;
6558 >                CountedCompleter<?> c;
6559 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
6560 >                    MapReduceEntriesToLongTask<K,V>
6561 >                        t = (MapReduceEntriesToLongTask<K,V>)c,
6562 >                        s = t.rights;
6563 >                    while (s != null) {
6564 >                        t.result = reducer.apply(t.result, s.result);
6565 >                        s = t.rights = s.nextRight;
6566 >                    }
6567                  }
6349                else if (p.casPending(c, 0))
6350                    break;
6568              }
6569          }
6353        public final Long getRawResult() { return result; }
6570      }
6571  
6572 <    static final class MapReduceMappingsToLongTask<K,V>
6573 <        extends BulkTask<K,V,Long> {
6572 >    @SuppressWarnings("serial") static final class MapReduceMappingsToLongTask<K,V>
6573 >        extends Traverser<K,V,Long> {
6574          final ObjectByObjectToLong<? super K, ? super V> transformer;
6575          final LongByLongToLong reducer;
6576          final long basis;
6577          long result;
6578 <        MapReduceMappingsToLongTask<K,V> sibling;
6363 <        MapReduceMappingsToLongTask
6364 <            (ConcurrentHashMapV8<K,V> m,
6365 <             ObjectByObjectToLong<? super K, ? super V> transformer,
6366 <             long basis,
6367 <             LongByLongToLong reducer) {
6368 <            super(m);
6369 <            this.transformer = transformer;
6370 <            this.basis = basis; this.reducer = reducer;
6371 <        }
6578 >        MapReduceMappingsToLongTask<K,V> rights, nextRight;
6579          MapReduceMappingsToLongTask
6580 <            (BulkTask<K,V,?> p, int b, boolean split,
6580 >            (ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b,
6581 >             MapReduceMappingsToLongTask<K,V> nextRight,
6582               ObjectByObjectToLong<? super K, ? super V> transformer,
6583               long basis,
6584               LongByLongToLong reducer) {
6585 <            super(p, b, split);
6585 >            super(m, p, b); this.nextRight = nextRight;
6586              this.transformer = transformer;
6587              this.basis = basis; this.reducer = reducer;
6588          }
6589 <        public final void compute() {
6590 <            MapReduceMappingsToLongTask<K,V> t = this;
6591 <            final ObjectByObjectToLong<? super K, ? super V> transformer =
6592 <                this.transformer;
6593 <            final LongByLongToLong reducer = this.reducer;
6594 <            if (transformer == null || reducer == null)
6595 <                throw new Error(NullFunctionMessage);
6596 <            final long id = this.basis;
6597 <            int b = batch();
6598 <            while (b > 1 && t.baseIndex != t.baseLimit) {
6599 <                b >>>= 1;
6600 <                t.pending = 1;
6601 <                MapReduceMappingsToLongTask<K,V> rt =
6602 <                    new MapReduceMappingsToLongTask<K,V>
6603 <                    (t, b, true, transformer, id, reducer);
6604 <                t = new MapReduceMappingsToLongTask<K,V>
6605 <                    (t, b, false, transformer, id, reducer);
6606 <                t.sibling = rt;
6607 <                rt.sibling = t;
6608 <                rt.fork();
6609 <            }
6610 <            long r = id;
6611 <            Object v;
6404 <            while ((v = t.advance()) != null)
6405 <                r = reducer.apply(r, transformer.apply((K)t.nextKey, (V)v));
6406 <            t.result = r;
6407 <            for (;;) {
6408 <                int c; BulkTask<K,V,?> par; MapReduceMappingsToLongTask<K,V> s, p;
6409 <                if ((par = t.parent) == null ||
6410 <                    !(par instanceof MapReduceMappingsToLongTask)) {
6411 <                    t.quietlyComplete();
6412 <                    break;
6413 <                }
6414 <                else if ((c = (p = (MapReduceMappingsToLongTask<K,V>)par).pending) == 0) {
6415 <                    if ((s = t.sibling) != null)
6416 <                        r = reducer.apply(r, s.result);
6417 <                    (t = p).result = r;
6589 >        public final Long getRawResult() { return result; }
6590 >        @SuppressWarnings("unchecked") public final void compute() {
6591 >            final ObjectByObjectToLong<? super K, ? super V> transformer;
6592 >            final LongByLongToLong reducer;
6593 >            if ((transformer = this.transformer) != null &&
6594 >                (reducer = this.reducer) != null) {
6595 >                long r = this.basis;
6596 >                for (int b; (b = preSplit()) > 0;)
6597 >                    (rights = new MapReduceMappingsToLongTask<K,V>
6598 >                     (map, this, b, rights, transformer, r, reducer)).fork();
6599 >                V v;
6600 >                while ((v = advance()) != null)
6601 >                    r = reducer.apply(r, transformer.apply((K)nextKey, v));
6602 >                result = r;
6603 >                CountedCompleter<?> c;
6604 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
6605 >                    MapReduceMappingsToLongTask<K,V>
6606 >                        t = (MapReduceMappingsToLongTask<K,V>)c,
6607 >                        s = t.rights;
6608 >                    while (s != null) {
6609 >                        t.result = reducer.apply(t.result, s.result);
6610 >                        s = t.rights = s.nextRight;
6611 >                    }
6612                  }
6419                else if (p.casPending(c, 0))
6420                    break;
6613              }
6614          }
6423        public final Long getRawResult() { return result; }
6615      }
6616  
6617 <    static final class MapReduceKeysToIntTask<K,V>
6618 <        extends BulkTask<K,V,Integer> {
6617 >    @SuppressWarnings("serial") static final class MapReduceKeysToIntTask<K,V>
6618 >        extends Traverser<K,V,Integer> {
6619          final ObjectToInt<? super K> transformer;
6620          final IntByIntToInt reducer;
6621          final int basis;
6622          int result;
6623 <        MapReduceKeysToIntTask<K,V> sibling;
6433 <        MapReduceKeysToIntTask
6434 <            (ConcurrentHashMapV8<K,V> m,
6435 <             ObjectToInt<? super K> transformer,
6436 <             int basis,
6437 <             IntByIntToInt reducer) {
6438 <            super(m);
6439 <            this.transformer = transformer;
6440 <            this.basis = basis; this.reducer = reducer;
6441 <        }
6623 >        MapReduceKeysToIntTask<K,V> rights, nextRight;
6624          MapReduceKeysToIntTask
6625 <            (BulkTask<K,V,?> p, int b, boolean split,
6625 >            (ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b,
6626 >             MapReduceKeysToIntTask<K,V> nextRight,
6627               ObjectToInt<? super K> transformer,
6628               int basis,
6629               IntByIntToInt reducer) {
6630 <            super(p, b, split);
6630 >            super(m, p, b); this.nextRight = nextRight;
6631              this.transformer = transformer;
6632              this.basis = basis; this.reducer = reducer;
6633          }
6634 <        public final void compute() {
6635 <            MapReduceKeysToIntTask<K,V> t = this;
6636 <            final ObjectToInt<? super K> transformer =
6637 <                this.transformer;
6638 <            final IntByIntToInt reducer = this.reducer;
6639 <            if (transformer == null || reducer == null)
6640 <                throw new Error(NullFunctionMessage);
6641 <            final int id = this.basis;
6642 <            int b = batch();
6643 <            while (b > 1 && t.baseIndex != t.baseLimit) {
6644 <                b >>>= 1;
6645 <                t.pending = 1;
6646 <                MapReduceKeysToIntTask<K,V> rt =
6647 <                    new MapReduceKeysToIntTask<K,V>
6648 <                    (t, b, true, transformer, id, reducer);
6649 <                t = new MapReduceKeysToIntTask<K,V>
6650 <                    (t, b, false, transformer, id, reducer);
6651 <                t.sibling = rt;
6652 <                rt.sibling = t;
6653 <                rt.fork();
6654 <            }
6655 <            int r = id;
6473 <            while (t.advance() != null)
6474 <                r = reducer.apply(r, transformer.apply((K)t.nextKey));
6475 <            t.result = r;
6476 <            for (;;) {
6477 <                int c; BulkTask<K,V,?> par; MapReduceKeysToIntTask<K,V> s, p;
6478 <                if ((par = t.parent) == null ||
6479 <                    !(par instanceof MapReduceKeysToIntTask)) {
6480 <                    t.quietlyComplete();
6481 <                    break;
6482 <                }
6483 <                else if ((c = (p = (MapReduceKeysToIntTask<K,V>)par).pending) == 0) {
6484 <                    if ((s = t.sibling) != null)
6485 <                        r = reducer.apply(r, s.result);
6486 <                    (t = p).result = r;
6634 >        public final Integer getRawResult() { return result; }
6635 >        @SuppressWarnings("unchecked") public final void compute() {
6636 >            final ObjectToInt<? super K> transformer;
6637 >            final IntByIntToInt reducer;
6638 >            if ((transformer = this.transformer) != null &&
6639 >                (reducer = this.reducer) != null) {
6640 >                int r = this.basis;
6641 >                for (int b; (b = preSplit()) > 0;)
6642 >                    (rights = new MapReduceKeysToIntTask<K,V>
6643 >                     (map, this, b, rights, transformer, r, reducer)).fork();
6644 >                while (advance() != null)
6645 >                    r = reducer.apply(r, transformer.apply((K)nextKey));
6646 >                result = r;
6647 >                CountedCompleter<?> c;
6648 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
6649 >                    MapReduceKeysToIntTask<K,V>
6650 >                        t = (MapReduceKeysToIntTask<K,V>)c,
6651 >                        s = t.rights;
6652 >                    while (s != null) {
6653 >                        t.result = reducer.apply(t.result, s.result);
6654 >                        s = t.rights = s.nextRight;
6655 >                    }
6656                  }
6488                else if (p.casPending(c, 0))
6489                    break;
6657              }
6658          }
6492        public final Integer getRawResult() { return result; }
6659      }
6660  
6661 <    static final class MapReduceValuesToIntTask<K,V>
6662 <        extends BulkTask<K,V,Integer> {
6661 >    @SuppressWarnings("serial") static final class MapReduceValuesToIntTask<K,V>
6662 >        extends Traverser<K,V,Integer> {
6663          final ObjectToInt<? super V> transformer;
6664          final IntByIntToInt reducer;
6665          final int basis;
6666          int result;
6667 <        MapReduceValuesToIntTask<K,V> sibling;
6502 <        MapReduceValuesToIntTask
6503 <            (ConcurrentHashMapV8<K,V> m,
6504 <             ObjectToInt<? super V> transformer,
6505 <             int basis,
6506 <             IntByIntToInt reducer) {
6507 <            super(m);
6508 <            this.transformer = transformer;
6509 <            this.basis = basis; this.reducer = reducer;
6510 <        }
6667 >        MapReduceValuesToIntTask<K,V> rights, nextRight;
6668          MapReduceValuesToIntTask
6669 <            (BulkTask<K,V,?> p, int b, boolean split,
6669 >            (ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b,
6670 >             MapReduceValuesToIntTask<K,V> nextRight,
6671               ObjectToInt<? super V> transformer,
6672               int basis,
6673               IntByIntToInt reducer) {
6674 <            super(p, b, split);
6674 >            super(m, p, b); this.nextRight = nextRight;
6675              this.transformer = transformer;
6676              this.basis = basis; this.reducer = reducer;
6677          }
6678 <        public final void compute() {
6679 <            MapReduceValuesToIntTask<K,V> t = this;
6680 <            final ObjectToInt<? super V> transformer =
6681 <                this.transformer;
6682 <            final IntByIntToInt reducer = this.reducer;
6683 <            if (transformer == null || reducer == null)
6684 <                throw new Error(NullFunctionMessage);
6685 <            final int id = this.basis;
6686 <            int b = batch();
6687 <            while (b > 1 && t.baseIndex != t.baseLimit) {
6688 <                b >>>= 1;
6689 <                t.pending = 1;
6690 <                MapReduceValuesToIntTask<K,V> rt =
6691 <                    new MapReduceValuesToIntTask<K,V>
6692 <                    (t, b, true, transformer, id, reducer);
6693 <                t = new MapReduceValuesToIntTask<K,V>
6694 <                    (t, b, false, transformer, id, reducer);
6695 <                t.sibling = rt;
6696 <                rt.sibling = t;
6697 <                rt.fork();
6698 <            }
6699 <            int r = id;
6700 <            Object v;
6543 <            while ((v = t.advance()) != null)
6544 <                r = reducer.apply(r, transformer.apply((V)v));
6545 <            t.result = r;
6546 <            for (;;) {
6547 <                int c; BulkTask<K,V,?> par; MapReduceValuesToIntTask<K,V> s, p;
6548 <                if ((par = t.parent) == null ||
6549 <                    !(par instanceof MapReduceValuesToIntTask)) {
6550 <                    t.quietlyComplete();
6551 <                    break;
6552 <                }
6553 <                else if ((c = (p = (MapReduceValuesToIntTask<K,V>)par).pending) == 0) {
6554 <                    if ((s = t.sibling) != null)
6555 <                        r = reducer.apply(r, s.result);
6556 <                    (t = p).result = r;
6678 >        public final Integer getRawResult() { return result; }
6679 >        @SuppressWarnings("unchecked") public final void compute() {
6680 >            final ObjectToInt<? super V> transformer;
6681 >            final IntByIntToInt reducer;
6682 >            if ((transformer = this.transformer) != null &&
6683 >                (reducer = this.reducer) != null) {
6684 >                int r = this.basis;
6685 >                for (int b; (b = preSplit()) > 0;)
6686 >                    (rights = new MapReduceValuesToIntTask<K,V>
6687 >                     (map, this, b, rights, transformer, r, reducer)).fork();
6688 >                V v;
6689 >                while ((v = advance()) != null)
6690 >                    r = reducer.apply(r, transformer.apply(v));
6691 >                result = r;
6692 >                CountedCompleter<?> c;
6693 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
6694 >                    MapReduceValuesToIntTask<K,V>
6695 >                        t = (MapReduceValuesToIntTask<K,V>)c,
6696 >                        s = t.rights;
6697 >                    while (s != null) {
6698 >                        t.result = reducer.apply(t.result, s.result);
6699 >                        s = t.rights = s.nextRight;
6700 >                    }
6701                  }
6558                else if (p.casPending(c, 0))
6559                    break;
6702              }
6703          }
6562        public final Integer getRawResult() { return result; }
6704      }
6705  
6706 <    static final class MapReduceEntriesToIntTask<K,V>
6707 <        extends BulkTask<K,V,Integer> {
6706 >    @SuppressWarnings("serial") static final class MapReduceEntriesToIntTask<K,V>
6707 >        extends Traverser<K,V,Integer> {
6708          final ObjectToInt<Map.Entry<K,V>> transformer;
6709          final IntByIntToInt reducer;
6710          final int basis;
6711          int result;
6712 <        MapReduceEntriesToIntTask<K,V> sibling;
6572 <        MapReduceEntriesToIntTask
6573 <            (ConcurrentHashMapV8<K,V> m,
6574 <             ObjectToInt<Map.Entry<K,V>> transformer,
6575 <             int basis,
6576 <             IntByIntToInt reducer) {
6577 <            super(m);
6578 <            this.transformer = transformer;
6579 <            this.basis = basis; this.reducer = reducer;
6580 <        }
6712 >        MapReduceEntriesToIntTask<K,V> rights, nextRight;
6713          MapReduceEntriesToIntTask
6714 <            (BulkTask<K,V,?> p, int b, boolean split,
6714 >            (ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b,
6715 >             MapReduceEntriesToIntTask<K,V> nextRight,
6716               ObjectToInt<Map.Entry<K,V>> transformer,
6717               int basis,
6718               IntByIntToInt reducer) {
6719 <            super(p, b, split);
6719 >            super(m, p, b); this.nextRight = nextRight;
6720              this.transformer = transformer;
6721              this.basis = basis; this.reducer = reducer;
6722          }
6723 <        public final void compute() {
6724 <            MapReduceEntriesToIntTask<K,V> t = this;
6725 <            final ObjectToInt<Map.Entry<K,V>> transformer =
6726 <                this.transformer;
6727 <            final IntByIntToInt reducer = this.reducer;
6728 <            if (transformer == null || reducer == null)
6729 <                throw new Error(NullFunctionMessage);
6730 <            final int id = this.basis;
6731 <            int b = batch();
6732 <            while (b > 1 && t.baseIndex != t.baseLimit) {
6733 <                b >>>= 1;
6734 <                t.pending = 1;
6735 <                MapReduceEntriesToIntTask<K,V> rt =
6736 <                    new MapReduceEntriesToIntTask<K,V>
6737 <                    (t, b, true, transformer, id, reducer);
6738 <                t = new MapReduceEntriesToIntTask<K,V>
6739 <                    (t, b, false, transformer, id, reducer);
6740 <                t.sibling = rt;
6741 <                rt.sibling = t;
6742 <                rt.fork();
6743 <            }
6744 <            int r = id;
6745 <            Object v;
6746 <            while ((v = t.advance()) != null)
6614 <                r = reducer.apply(r, transformer.apply(entryFor((K)t.nextKey, (V)v)));
6615 <            t.result = r;
6616 <            for (;;) {
6617 <                int c; BulkTask<K,V,?> par; MapReduceEntriesToIntTask<K,V> s, p;
6618 <                if ((par = t.parent) == null ||
6619 <                    !(par instanceof MapReduceEntriesToIntTask)) {
6620 <                    t.quietlyComplete();
6621 <                    break;
6622 <                }
6623 <                else if ((c = (p = (MapReduceEntriesToIntTask<K,V>)par).pending) == 0) {
6624 <                    if ((s = t.sibling) != null)
6625 <                        r = reducer.apply(r, s.result);
6626 <                    (t = p).result = r;
6723 >        public final Integer getRawResult() { return result; }
6724 >        @SuppressWarnings("unchecked") public final void compute() {
6725 >            final ObjectToInt<Map.Entry<K,V>> transformer;
6726 >            final IntByIntToInt reducer;
6727 >            if ((transformer = this.transformer) != null &&
6728 >                (reducer = this.reducer) != null) {
6729 >                int r = this.basis;
6730 >                for (int b; (b = preSplit()) > 0;)
6731 >                    (rights = new MapReduceEntriesToIntTask<K,V>
6732 >                     (map, this, b, rights, transformer, r, reducer)).fork();
6733 >                V v;
6734 >                while ((v = advance()) != null)
6735 >                    r = reducer.apply(r, transformer.apply(entryFor((K)nextKey,
6736 >                                                                    v)));
6737 >                result = r;
6738 >                CountedCompleter<?> c;
6739 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
6740 >                    MapReduceEntriesToIntTask<K,V>
6741 >                        t = (MapReduceEntriesToIntTask<K,V>)c,
6742 >                        s = t.rights;
6743 >                    while (s != null) {
6744 >                        t.result = reducer.apply(t.result, s.result);
6745 >                        s = t.rights = s.nextRight;
6746 >                    }
6747                  }
6628                else if (p.casPending(c, 0))
6629                    break;
6748              }
6749          }
6632        public final Integer getRawResult() { return result; }
6750      }
6751  
6752 <    static final class MapReduceMappingsToIntTask<K,V>
6753 <        extends BulkTask<K,V,Integer> {
6752 >    @SuppressWarnings("serial") static final class MapReduceMappingsToIntTask<K,V>
6753 >        extends Traverser<K,V,Integer> {
6754          final ObjectByObjectToInt<? super K, ? super V> transformer;
6755          final IntByIntToInt reducer;
6756          final int basis;
6757          int result;
6758 <        MapReduceMappingsToIntTask<K,V> sibling;
6758 >        MapReduceMappingsToIntTask<K,V> rights, nextRight;
6759          MapReduceMappingsToIntTask
6760 <            (ConcurrentHashMapV8<K,V> m,
6760 >            (ConcurrentHashMapV8<K,V> m, Traverser<K,V,?> p, int b,
6761 >             MapReduceMappingsToIntTask<K,V> nextRight,
6762               ObjectByObjectToInt<? super K, ? super V> transformer,
6763               int basis,
6764               IntByIntToInt reducer) {
6765 <            super(m);
6765 >            super(m, p, b); this.nextRight = nextRight;
6766              this.transformer = transformer;
6767              this.basis = basis; this.reducer = reducer;
6768          }
6769 <        MapReduceMappingsToIntTask
6770 <            (BulkTask<K,V,?> p, int b, boolean split,
6771 <             ObjectByObjectToInt<? super K, ? super V> transformer,
6772 <             int basis,
6773 <             IntByIntToInt reducer) {
6774 <            super(p, b, split);
6775 <            this.transformer = transformer;
6776 <            this.basis = basis; this.reducer = reducer;
6777 <        }
6778 <        public final void compute() {
6779 <            MapReduceMappingsToIntTask<K,V> t = this;
6780 <            final ObjectByObjectToInt<? super K, ? super V> transformer =
6781 <                this.transformer;
6782 <            final IntByIntToInt reducer = this.reducer;
6783 <            if (transformer == null || reducer == null)
6784 <                throw new Error(NullFunctionMessage);
6785 <            final int id = this.basis;
6786 <            int b = batch();
6787 <            while (b > 1 && t.baseIndex != t.baseLimit) {
6788 <                b >>>= 1;
6789 <                t.pending = 1;
6790 <                MapReduceMappingsToIntTask<K,V> rt =
6791 <                    new MapReduceMappingsToIntTask<K,V>
6674 <                    (t, b, true, transformer, id, reducer);
6675 <                t = new MapReduceMappingsToIntTask<K,V>
6676 <                    (t, b, false, transformer, id, reducer);
6677 <                t.sibling = rt;
6678 <                rt.sibling = t;
6679 <                rt.fork();
6680 <            }
6681 <            int r = id;
6682 <            Object v;
6683 <            while ((v = t.advance()) != null)
6684 <                r = reducer.apply(r, transformer.apply((K)t.nextKey, (V)v));
6685 <            t.result = r;
6686 <            for (;;) {
6687 <                int c; BulkTask<K,V,?> par; MapReduceMappingsToIntTask<K,V> s, p;
6688 <                if ((par = t.parent) == null ||
6689 <                    !(par instanceof MapReduceMappingsToIntTask)) {
6690 <                    t.quietlyComplete();
6691 <                    break;
6692 <                }
6693 <                else if ((c = (p = (MapReduceMappingsToIntTask<K,V>)par).pending) == 0) {
6694 <                    if ((s = t.sibling) != null)
6695 <                        r = reducer.apply(r, s.result);
6696 <                    (t = p).result = r;
6769 >        public final Integer getRawResult() { return result; }
6770 >        @SuppressWarnings("unchecked") public final void compute() {
6771 >            final ObjectByObjectToInt<? super K, ? super V> transformer;
6772 >            final IntByIntToInt reducer;
6773 >            if ((transformer = this.transformer) != null &&
6774 >                (reducer = this.reducer) != null) {
6775 >                int r = this.basis;
6776 >                for (int b; (b = preSplit()) > 0;)
6777 >                    (rights = new MapReduceMappingsToIntTask<K,V>
6778 >                     (map, this, b, rights, transformer, r, reducer)).fork();
6779 >                V v;
6780 >                while ((v = advance()) != null)
6781 >                    r = reducer.apply(r, transformer.apply((K)nextKey, v));
6782 >                result = r;
6783 >                CountedCompleter<?> c;
6784 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
6785 >                    MapReduceMappingsToIntTask<K,V>
6786 >                        t = (MapReduceMappingsToIntTask<K,V>)c,
6787 >                        s = t.rights;
6788 >                    while (s != null) {
6789 >                        t.result = reducer.apply(t.result, s.result);
6790 >                        s = t.rights = s.nextRight;
6791 >                    }
6792                  }
6698                else if (p.casPending(c, 0))
6699                    break;
6793              }
6794          }
6702        public final Integer getRawResult() { return result; }
6795      }
6796  
6705
6797      // Unsafe mechanics
6798 <    private static final sun.misc.Unsafe UNSAFE;
6799 <    private static final long counterOffset;
6800 <    private static final long sizeCtlOffset;
6798 >    private static final sun.misc.Unsafe U;
6799 >    private static final long SIZECTL;
6800 >    private static final long TRANSFERINDEX;
6801 >    private static final long TRANSFERORIGIN;
6802 >    private static final long BASECOUNT;
6803 >    private static final long COUNTERBUSY;
6804 >    private static final long CELLVALUE;
6805      private static final long ABASE;
6806      private static final int ASHIFT;
6807  
6808      static {
6714        int ss;
6809          try {
6810 <            UNSAFE = getUnsafe();
6810 >            U = getUnsafe();
6811              Class<?> k = ConcurrentHashMapV8.class;
6812 <            counterOffset = UNSAFE.objectFieldOffset
6719 <                (k.getDeclaredField("counter"));
6720 <            sizeCtlOffset = UNSAFE.objectFieldOffset
6812 >            SIZECTL = U.objectFieldOffset
6813                  (k.getDeclaredField("sizeCtl"));
6814 +            TRANSFERINDEX = U.objectFieldOffset
6815 +                (k.getDeclaredField("transferIndex"));
6816 +            TRANSFERORIGIN = U.objectFieldOffset
6817 +                (k.getDeclaredField("transferOrigin"));
6818 +            BASECOUNT = U.objectFieldOffset
6819 +                (k.getDeclaredField("baseCount"));
6820 +            COUNTERBUSY = U.objectFieldOffset
6821 +                (k.getDeclaredField("counterBusy"));
6822 +            Class<?> ck = CounterCell.class;
6823 +            CELLVALUE = U.objectFieldOffset
6824 +                (ck.getDeclaredField("value"));
6825              Class<?> sc = Node[].class;
6826 <            ABASE = UNSAFE.arrayBaseOffset(sc);
6827 <            ss = UNSAFE.arrayIndexScale(sc);
6826 >            ABASE = U.arrayBaseOffset(sc);
6827 >            int scale = U.arrayIndexScale(sc);
6828 >            if ((scale & (scale - 1)) != 0)
6829 >                throw new Error("data type scale not a power of two");
6830 >            ASHIFT = 31 - Integer.numberOfLeadingZeros(scale);
6831          } catch (Exception e) {
6832              throw new Error(e);
6833          }
6728        if ((ss & (ss-1)) != 0)
6729            throw new Error("data type scale not a power of two");
6730        ASHIFT = 31 - Integer.numberOfLeadingZeros(ss);
6834      }
6835  
6836      /**
# Line 6740 | Line 6843 | public class ConcurrentHashMapV8<K, V>
6843      private static sun.misc.Unsafe getUnsafe() {
6844          try {
6845              return sun.misc.Unsafe.getUnsafe();
6846 <        } catch (SecurityException se) {
6847 <            try {
6848 <                return java.security.AccessController.doPrivileged
6849 <                    (new java.security
6850 <                     .PrivilegedExceptionAction<sun.misc.Unsafe>() {
6851 <                        public sun.misc.Unsafe run() throws Exception {
6852 <                            java.lang.reflect.Field f = sun.misc
6853 <                                .Unsafe.class.getDeclaredField("theUnsafe");
6854 <                            f.setAccessible(true);
6855 <                            return (sun.misc.Unsafe) f.get(null);
6856 <                        }});
6857 <            } catch (java.security.PrivilegedActionException e) {
6858 <                throw new RuntimeException("Could not initialize intrinsics",
6859 <                                           e.getCause());
6860 <            }
6846 >        } catch (SecurityException tryReflectionInstead) {}
6847 >        try {
6848 >            return java.security.AccessController.doPrivileged
6849 >            (new java.security.PrivilegedExceptionAction<sun.misc.Unsafe>() {
6850 >                public sun.misc.Unsafe run() throws Exception {
6851 >                    Class<sun.misc.Unsafe> k = sun.misc.Unsafe.class;
6852 >                    for (java.lang.reflect.Field f : k.getDeclaredFields()) {
6853 >                        f.setAccessible(true);
6854 >                        Object x = f.get(null);
6855 >                        if (k.isInstance(x))
6856 >                            return k.cast(x);
6857 >                    }
6858 >                    throw new NoSuchFieldError("the Unsafe");
6859 >                }});
6860 >        } catch (java.security.PrivilegedActionException e) {
6861 >            throw new RuntimeException("Could not initialize intrinsics",
6862 >                                       e.getCause());
6863          }
6864      }
6865   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines