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.34 by jsr166, Mon Dec 19 19:18:35 2011 UTC vs.
Revision 1.61 by dl, Thu Sep 13 10:41:37 2012 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines