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.60 by dl, Thu Aug 16 12:24:58 2012 UTC vs.
Revision 1.102 by dl, Wed Jun 19 14:55:40 2013 UTC

# Line 5 | Line 5
5   */
6  
7   package jsr166e;
8 < import jsr166e.LongAdder;
8 >
9   import jsr166e.ForkJoinPool;
10 import jsr166e.ForkJoinTask;
10  
11 < import java.util.Comparator;
11 > import java.io.ObjectStreamField;
12 > import java.io.Serializable;
13 > import java.lang.reflect.ParameterizedType;
14 > import java.lang.reflect.Type;
15   import java.util.Arrays;
14 import java.util.Map;
15 import java.util.Set;
16   import java.util.Collection;
17 < import java.util.AbstractMap;
18 < import java.util.AbstractSet;
19 < import java.util.AbstractCollection;
20 < import java.util.Hashtable;
17 > import java.util.Comparator;
18 > import java.util.ConcurrentModificationException;
19 > import java.util.Enumeration;
20   import java.util.HashMap;
21 + import java.util.Hashtable;
22   import java.util.Iterator;
23 < import java.util.Enumeration;
24 < import java.util.ConcurrentModificationException;
23 > import java.util.Map;
24   import java.util.NoSuchElementException;
25 + import java.util.Set;
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;
27   import java.util.concurrent.atomic.AtomicReference;
28 <
29 < import java.io.Serializable;
28 > import java.util.concurrent.atomic.AtomicInteger;
29 > import java.util.concurrent.locks.LockSupport;
30 > import java.util.concurrent.locks.ReentrantLock;
31  
32   /**
33   * A hash table supporting full concurrency of retrievals and
# Line 43 | Line 41 | import java.io.Serializable;
41   * interoperable with {@code Hashtable} in programs that rely on its
42   * thread safety but not on its synchronization details.
43   *
44 < * <p> Retrieval operations (including {@code get}) generally do not
44 > * <p>Retrieval operations (including {@code get}) generally do not
45   * block, so may overlap with update operations (including {@code put}
46   * and {@code remove}). Retrievals reflect the results of the most
47   * recently <em>completed</em> update operations holding upon their
# Line 64 | Line 62 | import java.io.Serializable;
62   * that may be adequate for monitoring or estimation purposes, but not
63   * for program control.
64   *
65 < * <p> The table is dynamically expanded when there are too many
65 > * <p>The table is dynamically expanded when there are too many
66   * collisions (i.e., keys that have distinct hash codes but fall into
67   * the same slot modulo the table size), with the expected average
68   * effect of maintaining roughly two bins per mapping (corresponding
# Line 83 | Line 81 | import java.io.Serializable;
81   * expected {@code concurrencyLevel} as an additional hint for
82   * internal sizing.  Note that using many keys with exactly the same
83   * {@code hashCode()} is a sure way to slow down performance of any
84 < * hash table.
84 > * hash table. To ameliorate impact, when keys are {@link Comparable},
85 > * this class may use comparison order among keys to help break ties.
86 > *
87 > * <p>A {@link Set} projection of a ConcurrentHashMapV8 may be created
88 > * (using {@link #newKeySet()} or {@link #newKeySet(int)}), or viewed
89 > * (using {@link #keySet(Object)} when only keys are of interest, and the
90 > * mapped values are (perhaps transiently) not used or all take the
91 > * same mapping value.
92   *
93   * <p>This class and its views and iterators implement all of the
94   * <em>optional</em> methods of the {@link Map} and {@link Iterator}
95   * interfaces.
96   *
97 < * <p> Like {@link Hashtable} but unlike {@link HashMap}, this class
97 > * <p>Like {@link Hashtable} but unlike {@link HashMap}, this class
98   * does <em>not</em> allow {@code null} to be used as a key or value.
99   *
100 + * <p>ConcurrentHashMapV8s support a set of sequential and parallel bulk
101 + * operations that are designed
102 + * to be safely, and often sensibly, applied even with maps that are
103 + * being concurrently updated by other threads; for example, when
104 + * computing a snapshot summary of the values in a shared registry.
105 + * There are three kinds of operation, each with four forms, accepting
106 + * functions with Keys, Values, Entries, and (Key, Value) arguments
107 + * and/or return values. Because the elements of a ConcurrentHashMapV8
108 + * are not ordered in any particular way, and may be processed in
109 + * different orders in different parallel executions, the correctness
110 + * of supplied functions should not depend on any ordering, or on any
111 + * other objects or values that may transiently change while
112 + * computation is in progress; and except for forEach actions, should
113 + * ideally be side-effect-free. Bulk operations on {@link java.util.Map.Entry}
114 + * objects do not support method {@code setValue}.
115 + *
116 + * <ul>
117 + * <li> forEach: Perform a given action on each element.
118 + * A variant form applies a given transformation on each element
119 + * before performing the action.</li>
120 + *
121 + * <li> search: Return the first available non-null result of
122 + * applying a given function on each element; skipping further
123 + * search when a result is found.</li>
124 + *
125 + * <li> reduce: Accumulate each element.  The supplied reduction
126 + * function cannot rely on ordering (more formally, it should be
127 + * both associative and commutative).  There are five variants:
128 + *
129 + * <ul>
130 + *
131 + * <li> Plain reductions. (There is not a form of this method for
132 + * (key, value) function arguments since there is no corresponding
133 + * return type.)</li>
134 + *
135 + * <li> Mapped reductions that accumulate the results of a given
136 + * function applied to each element.</li>
137 + *
138 + * <li> Reductions to scalar doubles, longs, and ints, using a
139 + * given basis value.</li>
140 + *
141 + * </ul>
142 + * </li>
143 + * </ul>
144 + *
145 + * <p>These bulk operations accept a {@code parallelismThreshold}
146 + * argument. Methods proceed sequentially if the current map size is
147 + * estimated to be less than the given threshold. Using a value of
148 + * {@code Long.MAX_VALUE} suppresses all parallelism.  Using a value
149 + * of {@code 1} results in maximal parallelism by partitioning into
150 + * enough subtasks to fully utilize the {@link
151 + * ForkJoinPool#commonPool()} that is used for all parallel
152 + * computations. Normally, you would initially choose one of these
153 + * extreme values, and then measure performance of using in-between
154 + * values that trade off overhead versus throughput.
155 + *
156 + * <p>The concurrency properties of bulk operations follow
157 + * from those of ConcurrentHashMapV8: Any non-null result returned
158 + * from {@code get(key)} and related access methods bears a
159 + * happens-before relation with the associated insertion or
160 + * update.  The result of any bulk operation reflects the
161 + * composition of these per-element relations (but is not
162 + * necessarily atomic with respect to the map as a whole unless it
163 + * is somehow known to be quiescent).  Conversely, because keys
164 + * and values in the map are never null, null serves as a reliable
165 + * atomic indicator of the current lack of any result.  To
166 + * maintain this property, null serves as an implicit basis for
167 + * all non-scalar reduction operations. For the double, long, and
168 + * int versions, the basis should be one that, when combined with
169 + * any other value, returns that other value (more formally, it
170 + * should be the identity element for the reduction). Most common
171 + * reductions have these properties; for example, computing a sum
172 + * with basis 0 or a minimum with basis MAX_VALUE.
173 + *
174 + * <p>Search and transformation functions provided as arguments
175 + * should similarly return null to indicate the lack of any result
176 + * (in which case it is not used). In the case of mapped
177 + * reductions, this also enables transformations to serve as
178 + * filters, returning null (or, in the case of primitive
179 + * specializations, the identity basis) if the element should not
180 + * be combined. You can create compound transformations and
181 + * filterings by composing them yourself under this "null means
182 + * there is nothing there now" rule before using them in search or
183 + * reduce operations.
184 + *
185 + * <p>Methods accepting and/or returning Entry arguments maintain
186 + * key-value associations. They may be useful for example when
187 + * finding the key for the greatest value. Note that "plain" Entry
188 + * arguments can be supplied using {@code new
189 + * AbstractMap.SimpleEntry(k,v)}.
190 + *
191 + * <p>Bulk operations may complete abruptly, throwing an
192 + * exception encountered in the application of a supplied
193 + * function. Bear in mind when handling such exceptions that other
194 + * concurrently executing functions could also have thrown
195 + * exceptions, or would have done so if the first exception had
196 + * not occurred.
197 + *
198 + * <p>Speedups for parallel compared to sequential forms are common
199 + * but not guaranteed.  Parallel operations involving brief functions
200 + * on small maps may execute more slowly than sequential forms if the
201 + * underlying work to parallelize the computation is more expensive
202 + * than the computation itself.  Similarly, parallelization may not
203 + * lead to much actual parallelism if all processors are busy
204 + * performing unrelated tasks.
205 + *
206 + * <p>All arguments to all task methods must be non-null.
207 + *
208 + * <p><em>jsr166e note: During transition, this class
209 + * uses nested functional interfaces with different names but the
210 + * same forms as those expected for JDK8.</em>
211 + *
212   * <p>This class is a member of the
213   * <a href="{@docRoot}/../technotes/guides/collections/index.html">
214   * Java Collections Framework</a>.
215   *
99 * <p><em>jsr166e note: This class is a candidate replacement for
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 *
216   * @since 1.5
217   * @author Doug Lea
218   * @param <K> the type of keys maintained by this map
219   * @param <V> the type of mapped values
220   */
221 < public class ConcurrentHashMapV8<K, V>
222 <    implements ConcurrentMap<K, V>, Serializable {
221 > public class ConcurrentHashMapV8<K,V>
222 >    implements ConcurrentMap<K,V>, Serializable {
223      private static final long serialVersionUID = 7249069246763182397L;
224  
225      /**
226 <     * A partitionable iterator. A Spliterator can be traversed
227 <     * directly, but can also be partitioned (before traversal) by
228 <     * creating another Spliterator that covers a non-overlapping
229 <     * portion of the elements, and so may be amenable to parallel
230 <     * execution.
231 <     *
232 <     * <p> This interface exports a subset of expected JDK8
233 <     * functionality.
234 <     *
235 <     * <p>Sample usage: Here is one (of the several) ways to compute
236 <     * the sum of the values held in a map using the ForkJoin
237 <     * framework. As illustrated here, Spliterators are well suited to
238 <     * designs in which a task repeatedly splits off half its work
239 <     * into forked subtasks until small enough to process directly,
240 <     * and then joins these subtasks. Variants of this style can also
241 <     * be used in completion-based designs.
242 <     *
243 <     * <pre>
244 <     * {@code ConcurrentHashMapV8<String, Long> m = ...
245 <     * // split as if have 8 * parallelism, for load balance
246 <     * int n = m.size();
247 <     * int p = aForkJoinPool.getParallelism() * 8;
136 <     * int split = (n < p)? n : p;
137 <     * long sum = aForkJoinPool.invoke(new SumValues(m.valueSpliterator(), split, null));
138 <     * // ...
139 <     * static class SumValues extends RecursiveTask<Long> {
140 <     *   final Spliterator<Long> s;
141 <     *   final int split;             // split while > 1
142 <     *   final SumValues nextJoin;    // records forked subtasks to join
143 <     *   SumValues(Spliterator<Long> s, int depth, SumValues nextJoin) {
144 <     *     this.s = s; this.depth = depth; this.nextJoin = nextJoin;
145 <     *   }
146 <     *   public Long compute() {
147 <     *     long sum = 0;
148 <     *     SumValues subtasks = null; // fork subtasks
149 <     *     for (int s = split >>> 1; s > 0; s >>>= 1)
150 <     *       (subtasks = new SumValues(s.split(), s, subtasks)).fork();
151 <     *     while (s.hasNext())        // directly process remaining elements
152 <     *       sum += s.next();
153 <     *     for (SumValues t = subtasks; t != null; t = t.nextJoin)
154 <     *       sum += t.join();         // collect subtask results
155 <     *     return sum;
156 <     *   }
157 <     * }
158 <     * }</pre>
159 <     */
160 <    public static interface Spliterator<T> extends Iterator<T> {
161 <        /**
162 <         * Returns a Spliterator covering approximately half of the
163 <         * elements, guaranteed not to overlap with those subsequently
164 <         * returned by this Spliterator.  After invoking this method,
165 <         * the current Spliterator will <em>not</em> produce any of
166 <         * the elements of the returned Spliterator, but the two
167 <         * Spliterators together will produce all of the elements that
168 <         * would have been produced by this Spliterator had this
169 <         * method not been called. The exact number of elements
170 <         * produced by the returned Spliterator is not guaranteed, and
171 <         * may be zero (i.e., with {@code hasNext()} reporting {@code
172 <         * false}) if this Spliterator cannot be further split.
173 <         *
174 <         * @return a Spliterator covering approximately half of the
175 <         * elements
176 <         * @throws IllegalStateException if this Spliterator has
177 <         * already commenced traversing elements
178 <         */
179 <        Spliterator<T> split();
226 >     * An object for traversing and partitioning elements of a source.
227 >     * This interface provides a subset of the functionality of JDK8
228 >     * java.util.Spliterator.
229 >     */
230 >    public static interface ConcurrentHashMapSpliterator<T> {
231 >        /**
232 >         * If possible, returns a new spliterator covering
233 >         * approximately one half of the elements, which will not be
234 >         * covered by this spliterator. Returns null if cannot be
235 >         * split.
236 >         */
237 >        ConcurrentHashMapSpliterator<T> trySplit();
238 >        /**
239 >         * Returns an estimate of the number of elements covered by
240 >         * this Spliterator.
241 >         */
242 >        long estimateSize();
243 >
244 >        /** Applies the action to each untraversed element */
245 >        void forEachRemaining(Action<? super T> action);
246 >        /** If an element remains, applies the action and returns true. */
247 >        boolean tryAdvance(Action<? super T> action);
248      }
249  
250 +    // Sams
251 +    /** Interface describing a void action of one argument */
252 +    public interface Action<A> { void apply(A a); }
253 +    /** Interface describing a void action of two arguments */
254 +    public interface BiAction<A,B> { void apply(A a, B b); }
255 +    /** Interface describing a function of one argument */
256 +    public interface Fun<A,T> { T apply(A a); }
257 +    /** Interface describing a function of two arguments */
258 +    public interface BiFun<A,B,T> { T apply(A a, B b); }
259 +    /** Interface describing a function mapping its argument to a double */
260 +    public interface ObjectToDouble<A> { double apply(A a); }
261 +    /** Interface describing a function mapping its argument to a long */
262 +    public interface ObjectToLong<A> { long apply(A a); }
263 +    /** Interface describing a function mapping its argument to an int */
264 +    public interface ObjectToInt<A> {int apply(A a); }
265 +    /** Interface describing a function mapping two arguments to a double */
266 +    public interface ObjectByObjectToDouble<A,B> { double apply(A a, B b); }
267 +    /** Interface describing a function mapping two arguments to a long */
268 +    public interface ObjectByObjectToLong<A,B> { long apply(A a, B b); }
269 +    /** Interface describing a function mapping two arguments to an int */
270 +    public interface ObjectByObjectToInt<A,B> {int apply(A a, B b); }
271 +    /** Interface describing a function mapping two doubles to a double */
272 +    public interface DoubleByDoubleToDouble { double apply(double a, double b); }
273 +    /** Interface describing a function mapping two longs to a long */
274 +    public interface LongByLongToLong { long apply(long a, long b); }
275 +    /** Interface describing a function mapping two ints to an int */
276 +    public interface IntByIntToInt { int apply(int a, int b); }
277 +
278      /*
279       * Overview:
280       *
# Line 189 | Line 285 | public class ConcurrentHashMapV8<K, V>
285       * the same or better than java.util.HashMap, and to support high
286       * initial insertion rates on an empty table by many threads.
287       *
288 <     * Each key-value mapping is held in a Node.  Because Node fields
289 <     * can contain special values, they are defined using plain Object
290 <     * types. Similarly in turn, all internal methods that use them
291 <     * work off Object types. And similarly, so do the internal
292 <     * methods of auxiliary iterator and view classes.  All public
293 <     * generic typed methods relay in/out of these internal methods,
294 <     * supplying null-checks and casts as needed. This also allows
295 <     * many of the public methods to be factored into a smaller number
296 <     * of internal methods (although sadly not so for the five
297 <     * variants of put-related operations). The validation-based
298 <     * approach explained below leads to a lot of code sprawl because
299 <     * retry-control precludes factoring into smaller methods.
288 >     * This map usually acts as a binned (bucketed) hash table.  Each
289 >     * key-value mapping is held in a Node.  Most nodes are instances
290 >     * of the basic Node class with hash, key, value, and next
291 >     * fields. However, various subclasses exist: TreeNodes are
292 >     * arranged in balanced trees, not lists.  TreeBins hold the roots
293 >     * of sets of TreeNodes. ForwardingNodes are placed at the heads
294 >     * of bins during resizing. ReservationNodes are used as
295 >     * placeholders while establishing values in computeIfAbsent and
296 >     * related methods.  The types TreeBin, ForwardingNode, and
297 >     * ReservationNode do not hold normal user keys, values, or
298 >     * hashes, and are readily distinguishable during search etc
299 >     * because they have negative hash fields and null key and value
300 >     * fields. (These special nodes are either uncommon or transient,
301 >     * so the impact of carrying around some unused fields is
302 >     * insignficant.)
303       *
304       * The table is lazily initialized to a power-of-two size upon the
305       * first insertion.  Each bin in the table normally contains a
# Line 208 | Line 307 | public class ConcurrentHashMapV8<K, V>
307       * Table accesses require volatile/atomic reads, writes, and
308       * CASes.  Because there is no other way to arrange this without
309       * adding further indirections, we use intrinsics
310 <     * (sun.misc.Unsafe) operations.  The lists of nodes within bins
311 <     * are always accurately traversable under volatile reads, so long
312 <     * as lookups check hash code and non-nullness of value before
313 <     * checking key equality.
314 <     *
315 <     * We use the top two bits of Node hash fields for control
217 <     * purposes -- they are available anyway because of addressing
218 <     * constraints.  As explained further below, these top bits are
219 <     * used as follows:
220 <     *  00 - Normal
221 <     *  01 - Locked
222 <     *  11 - Locked and may have a thread waiting for lock
223 <     *  10 - Node is a forwarding node
224 <     *
225 <     * The lower 30 bits of each Node's hash field contain a
226 <     * transformation of the key's hash code, except for forwarding
227 <     * nodes, for which the lower bits are zero (and so always have
228 <     * hash field == MOVED).
310 >     * (sun.misc.Unsafe) operations.
311 >     *
312 >     * We use the top (sign) bit of Node hash fields for control
313 >     * purposes -- it is available anyway because of addressing
314 >     * constraints.  Nodes with negative hash fields are specially
315 >     * handled or ignored in map methods.
316       *
317       * Insertion (via put or its variants) of the first node in an
318       * empty bin is performed by just CASing it to the bin.  This is
# Line 234 | Line 321 | public class ConcurrentHashMapV8<K, V>
321       * delete, and replace) require locks.  We do not want to waste
322       * the space required to associate a distinct lock object with
323       * each bin, so instead use the first node of a bin list itself as
324 <     * a lock. Blocking support for these locks relies on the builtin
325 <     * "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.
324 >     * a lock. Locking support for these locks relies on builtin
325 >     * "synchronized" monitors.
326       *
327       * Using the first node of a list as a lock does not by itself
328       * suffice though: When a node is locked, any update must first
329       * validate that it is still the first node after locking it, and
330       * retry if not. Because new nodes are always appended to lists,
331       * once a node is first in a bin, it remains first until deleted
332 <     * or the bin becomes invalidated (upon resizing).  However,
250 <     * operations that only conditionally update may inspect nodes
251 <     * until the point of update. This is a converse of sorts to the
252 <     * lazy locking technique described by Herlihy & Shavit.
332 >     * or the bin becomes invalidated (upon resizing).
333       *
334       * The main disadvantage of per-bin locks is that other update
335       * operations on other nodes in a bin list protected by the same
# Line 282 | Line 362 | public class ConcurrentHashMapV8<K, V>
362       * sometimes deviate significantly from uniform randomness.  This
363       * includes the case when N > (1<<30), so some keys MUST collide.
364       * Similarly for dumb or hostile usages in which multiple keys are
365 <     * designed to have identical hash codes. Also, although we guard
366 <     * against the worst effects of this (see method spread), sets of
367 <     * hashes may differ only in bits that do not impact their bin
368 <     * index for a given power-of-two mask.  So we use a secondary
369 <     * strategy that applies when the number of nodes in a bin exceeds
370 <     * 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
365 >     * designed to have identical hash codes or ones that differs only
366 >     * in masked-out high bits. So we use a secondary strategy that
367 >     * applies when the number of nodes in a bin exceeds a
368 >     * threshold. These TreeBins use a balanced tree to hold nodes (a
369 >     * specialized form of red-black trees), bounding search time to
370 >     * O(log N).  Each search step in a TreeBin is at least twice as
371       * slow as in a regular list, but given that N cannot exceed
372       * (1<<64) (before running out of addresses) this bounds search
373       * steps, lock hold times, etc, to reasonable constants (roughly
# Line 301 | Line 378 | public class ConcurrentHashMapV8<K, V>
378       * iterators in the same way.
379       *
380       * The table is resized when occupancy exceeds a percentage
381 <     * threshold (nominally, 0.75, but see below).  Only a single
382 <     * thread performs the resize (using field "sizeCtl", to arrange
383 <     * exclusion), but the table otherwise remains usable for reads
384 <     * and updates. Resizing proceeds by transferring bins, one by
385 <     * one, from the table to the next table.  Because we are using
386 <     * power-of-two expansion, the elements from each bin must either
387 <     * stay at same index, or move with a power of two offset. We
388 <     * eliminate unnecessary node creation by catching cases where old
389 <     * nodes can be reused because their next fields won't change.  On
390 <     * average, only about one-sixth of them need cloning when a table
391 <     * doubles. The nodes they replace will be garbage collectable as
392 <     * soon as they are no longer referenced by any reader thread that
393 <     * may be in the midst of concurrently traversing table.  Upon
394 <     * transfer, the old table bin contains only a special forwarding
395 <     * node (with hash field "MOVED") that contains the next table as
396 <     * its key. On encountering a forwarding node, access and update
397 <     * operations restart, using the new table.
398 <     *
399 <     * Each bin transfer requires its bin lock. However, unlike other
400 <     * cases, a transfer can skip a bin if it fails to acquire its
401 <     * lock, and revisit it later (unless it is a TreeBin). Method
402 <     * rebuild maintains a buffer of TRANSFER_BUFFER_SIZE bins that
403 <     * have been skipped because of failure to acquire a lock, and
404 <     * blocks only if none are available (i.e., only very rarely).
405 <     * The transfer operation must also ensure that all accessible
406 <     * bins in both the old and new table are usable by any traversal.
407 <     * When there are no lock acquisition failures, this is arranged
408 <     * simply by proceeding from the last bin (table.length - 1) up
409 <     * towards the first.  Upon seeing a forwarding node, traversals
410 <     * (see class Iter) arrange to move to the new table
411 <     * without revisiting nodes.  However, when any node is skipped
412 <     * during a transfer, all earlier table bins may have become
413 <     * visible, so are initialized with a reverse-forwarding node back
414 <     * to the old table until the new ones are established. (This
415 <     * sometimes requires transiently locking a forwarding node, which
416 <     * is possible under the above encoding.) These more expensive
417 <     * mechanics trigger only when necessary.
381 >     * threshold (nominally, 0.75, but see below).  Any thread
382 >     * noticing an overfull bin may assist in resizing after the
383 >     * initiating thread allocates and sets up the replacement
384 >     * array. However, rather than stalling, these other threads may
385 >     * proceed with insertions etc.  The use of TreeBins shields us
386 >     * from the worst case effects of overfilling while resizes are in
387 >     * progress.  Resizing proceeds by transferring bins, one by one,
388 >     * from the table to the next table. To enable concurrency, the
389 >     * next table must be (incrementally) prefilled with place-holders
390 >     * serving as reverse forwarders to the old table.  Because we are
391 >     * using power-of-two expansion, the elements from each bin must
392 >     * either stay at same index, or move with a power of two
393 >     * offset. We eliminate unnecessary node creation by catching
394 >     * cases where old nodes can be reused because their next fields
395 >     * won't change.  On average, only about one-sixth of them need
396 >     * cloning when a table doubles. The nodes they replace will be
397 >     * garbage collectable as soon as they are no longer referenced by
398 >     * any reader thread that may be in the midst of concurrently
399 >     * traversing table.  Upon transfer, the old table bin contains
400 >     * only a special forwarding node (with hash field "MOVED") that
401 >     * contains the next table as its key. On encountering a
402 >     * forwarding node, access and update operations restart, using
403 >     * the new table.
404 >     *
405 >     * Each bin transfer requires its bin lock, which can stall
406 >     * waiting for locks while resizing. However, because other
407 >     * threads can join in and help resize rather than contend for
408 >     * locks, average aggregate waits become shorter as resizing
409 >     * progresses.  The transfer operation must also ensure that all
410 >     * accessible bins in both the old and new table are usable by any
411 >     * traversal.  This is arranged by proceeding from the last bin
412 >     * (table.length - 1) up towards the first.  Upon seeing a
413 >     * forwarding node, traversals (see class Traverser) arrange to
414 >     * move to the new table without revisiting nodes.  However, to
415 >     * ensure that no intervening nodes are skipped, bin splitting can
416 >     * only begin after the associated reverse-forwarders are in
417 >     * place.
418       *
419       * The traversal scheme also applies to partial traversals of
420       * ranges of bins (via an alternate Traverser constructor)
# Line 352 | Line 429 | public class ConcurrentHashMapV8<K, V>
429       * These cases attempt to override the initial capacity settings,
430       * but harmlessly fail to take effect in cases of races.
431       *
432 <     * The element count is maintained using a LongAdder, which avoids
433 <     * contention on updates but can encounter cache thrashing if read
434 <     * too frequently during concurrent access. To avoid reading so
435 <     * often, resizing is attempted either when a bin lock is
436 <     * contended, or upon adding to a bin already holding two or more
437 <     * nodes (checked before adding in the xIfAbsent methods, after
438 <     * adding in others). Under uniform hash distributions, the
439 <     * probability of this occurring at threshold is around 13%,
440 <     * meaning that only about 1 in 8 puts check threshold (and after
441 <     * resizing, many fewer do so). But this approximation has high
442 <     * variance for small table sizes, so we check on any collision
443 <     * for sizes <= 64. The bulk putAll operation further reduces
444 <     * contention by only committing count updates upon these size
445 <     * checks.
432 >     * The element count is maintained using a specialization of
433 >     * LongAdder. We need to incorporate a specialization rather than
434 >     * just use a LongAdder in order to access implicit
435 >     * contention-sensing that leads to creation of multiple
436 >     * CounterCells.  The counter mechanics avoid contention on
437 >     * updates but can encounter cache thrashing if read too
438 >     * frequently during concurrent access. To avoid reading so often,
439 >     * resizing under contention is attempted only upon adding to a
440 >     * bin already holding two or more nodes. Under uniform hash
441 >     * distributions, the probability of this occurring at threshold
442 >     * is around 13%, meaning that only about 1 in 8 puts check
443 >     * threshold (and after resizing, many fewer do so).
444 >     *
445 >     * TreeBins use a special form of comparison for search and
446 >     * related operations (which is the main reason we cannot use
447 >     * existing collections such as TreeMaps). TreeBins contain
448 >     * Comparable elements, but may contain others, as well as
449 >     * elements that are Comparable but not necessarily Comparable
450 >     * for the same T, so we cannot invoke compareTo among them. To
451 >     * handle this, the tree is ordered primarily by hash value, then
452 >     * by Comparable.compareTo order if applicable.  On lookup at a
453 >     * node, if elements are not comparable or compare as 0 then both
454 >     * left and right children may need to be searched in the case of
455 >     * tied hash values. (This corresponds to the full list search
456 >     * that would be necessary if all elements were non-Comparable and
457 >     * had tied hashes.)  The red-black balancing code is updated from
458 >     * pre-jdk-collections
459 >     * (http://gee.cs.oswego.edu/dl/classes/collections/RBCell.java)
460 >     * based in turn on Cormen, Leiserson, and Rivest "Introduction to
461 >     * Algorithms" (CLR).
462 >     *
463 >     * TreeBins also require an additional locking mechanism.  While
464 >     * list traversal is always possible by readers even during
465 >     * updates, tree traversal is not, mainly beause of tree-rotations
466 >     * that may change the root node and/or its linkages.  TreeBins
467 >     * include a simple read-write lock mechanism parasitic on the
468 >     * main bin-synchronization strategy: Structural adjustments
469 >     * associated with an insertion or removal are already bin-locked
470 >     * (and so cannot conflict with other writers) but must wait for
471 >     * ongoing readers to finish. Since there can be only one such
472 >     * waiter, we use a simple scheme using a single "waiter" field to
473 >     * block writers.  However, readers need never block.  If the root
474 >     * lock is held, they proceed along the slow traversal path (via
475 >     * next-pointers) until the lock becomes available or the list is
476 >     * exhausted, whichever comes first. These cases are not fast, but
477 >     * maximize aggregate expected throughput.
478       *
479       * Maintaining API and serialization compatibility with previous
480       * versions of this class introduces several oddities. Mainly: We
# Line 375 | Line 484 | public class ConcurrentHashMapV8<K, V>
484       * time that we can guarantee to honor it.) We also declare an
485       * unused "Segment" class that is instantiated in minimal form
486       * only when serializing.
487 +     *
488 +     * This file is organized to make things a little easier to follow
489 +     * while reading than they might otherwise: First the main static
490 +     * declarations and utilities, then fields, then main public
491 +     * methods (with a few factorings of multiple public methods into
492 +     * internal ones), then sizing methods, trees, traversers, and
493 +     * bulk operations.
494       */
495  
496      /* ---------------- Constants -------------- */
# Line 416 | Line 532 | public class ConcurrentHashMapV8<K, V>
532      private static final float LOAD_FACTOR = 0.75f;
533  
534      /**
419     * The buffer size for skipped bins during transfers. The
420     * value is arbitrary but should be large enough to avoid
421     * most locking stalls during resizes.
422     */
423    private static final int TRANSFER_BUFFER_SIZE = 32;
424
425    /**
535       * The bin count threshold for using a tree rather than list for a
536 <     * bin.  The value reflects the approximate break-even point for
537 <     * using tree-based operations.
536 >     * bin.  Bins are converted to trees when adding an element to a
537 >     * bin with at least this many nodes. The value must be greater
538 >     * than 2, and should be at least 8 to mesh with assumptions in
539 >     * tree removal about conversion back to plain bins upon
540 >     * shrinkage.
541       */
542 <    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 forwarding nodes
437 <    static final int LOCKED    = 0x40000000; // set/tested only as a bit
438 <    static final int WAITING   = 0xc0000000; // both bits set/tested together
439 <    static final int HASH_BITS = 0x3fffffff; // usable bits of normal node hash
440 <
441 <    /* ---------------- Fields -------------- */
542 >    static final int TREEIFY_THRESHOLD = 8;
543  
544      /**
545 <     * The array of bins. Lazily initialized upon first insertion.
546 <     * Size is always a power of two. Accessed directly by iterators.
545 >     * The bin count threshold for untreeifying a (split) bin during a
546 >     * resize operation. Should be less than TREEIFY_THRESHOLD, and at
547 >     * most 6 to mesh with shrinkage detection under removal.
548       */
549 <    transient volatile Node[] table;
549 >    static final int UNTREEIFY_THRESHOLD = 6;
550  
551      /**
552 <     * The counter maintaining number of elements.
552 >     * The smallest table capacity for which bins may be treeified.
553 >     * (Otherwise the table is resized if too many nodes in a bin.)
554 >     * The value should be at least 4 * TREEIFY_THRESHOLD to avoid
555 >     * conflicts between resizing and treeification thresholds.
556       */
557 <    private transient final LongAdder counter;
557 >    static final int MIN_TREEIFY_CAPACITY = 64;
558  
559      /**
560 <     * Table initialization and resizing control.  When negative, the
561 <     * table is being initialized or resized. Otherwise, when table is
562 <     * null, holds the initial table size to use upon creation, or 0
563 <     * for default. After initialization, holds the next element count
564 <     * value upon which to resize the table.
560 >     * Minimum number of rebinnings per transfer step. Ranges are
561 >     * subdivided to allow multiple resizer threads.  This value
562 >     * serves as a lower bound to avoid resizers encountering
563 >     * excessive memory contention.  The value should be at least
564 >     * DEFAULT_CAPACITY.
565       */
566 <    private transient volatile int sizeCtl;
462 <
463 <    // views
464 <    private transient KeySet<K,V> keySet;
465 <    private transient Values<K,V> values;
466 <    private transient EntrySet<K,V> entrySet;
467 <
468 <    /** For serialization compatibility. Null unless serialized; see below */
469 <    private Segment<K,V>[] segments;
470 <
471 <    /* ---------------- Table element access -------------- */
566 >    private static final int MIN_TRANSFER_STRIDE = 16;
567  
568      /*
569 <     * 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.
569 >     * Encodings for Node hash fields. See above for explanation.
570       */
571 <
572 <    static final Node tabAt(Node[] tab, int i) { // used by Iter
573 <        return (Node)UNSAFE.getObjectVolatile(tab, ((long)i<<ASHIFT)+ABASE);
574 <    }
575 <
576 <    private static final boolean casTabAt(Node[] tab, int i, Node c, Node v) {
577 <        return UNSAFE.compareAndSwapObject(tab, ((long)i<<ASHIFT)+ABASE, c, v);
578 <    }
579 <
580 <    private static final void setTabAt(Node[] tab, int i, Node v) {
581 <        UNSAFE.putObjectVolatile(tab, ((long)i<<ASHIFT)+ABASE, v);
582 <    }
571 >    static final int MOVED     = 0x8fffffff; // (-1) hash for forwarding nodes
572 >    static final int TREEBIN   = 0x80000000; // hash for heads of treea
573 >    static final int RESERVED  = 0x80000001; // hash for transient reservations
574 >    static final int HASH_BITS = 0x7fffffff; // usable bits of normal node hash
575 >
576 >    /** Number of CPUS, to place bounds on some sizings */
577 >    static final int NCPU = Runtime.getRuntime().availableProcessors();
578 >
579 >    /** For serialization compatibility. */
580 >    private static final ObjectStreamField[] serialPersistentFields = {
581 >        new ObjectStreamField("segments", Segment[].class),
582 >        new ObjectStreamField("segmentMask", Integer.TYPE),
583 >        new ObjectStreamField("segmentShift", Integer.TYPE)
584 >    };
585  
586      /* ---------------- Nodes -------------- */
587  
588      /**
589 <     * Key-value entry. Note that this is never exported out as a
590 <     * user-visible Map.Entry (see MapEntry below). Nodes with a hash
591 <     * field of MOVED are special, and do not contain user keys or
592 <     * values.  Otherwise, keys are never null, and null val fields
593 <     * indicate that a node is in the process of being deleted or
594 <     * created. For purposes of read-only access, a key may be read
595 <     * before a val, but can only be used after checking val to be
596 <     * non-null.
597 <     */
598 <    static class Node {
599 <        volatile int hash;
600 <        final Object key;
512 <        volatile Object val;
513 <        volatile Node next;
589 >     * Key-value entry.  This class is never exported out as a
590 >     * user-mutable Map.Entry (i.e., one supporting setValue; see
591 >     * MapEntry below), but can be used for read-only traversals used
592 >     * in bulk tasks.  Subclasses of Node with a negativehash field
593 >     * are special, and contain null keys and values (but are never
594 >     * exported).  Otherwise, keys and vals are never null.
595 >     */
596 >    static class Node<K,V> implements Map.Entry<K,V> {
597 >        final int hash;
598 >        final K key;
599 >        volatile V val;
600 >        Node<K,V> next;
601  
602 <        Node(int hash, Object key, Object val, Node next) {
602 >        Node(int hash, K key, V val, Node<K,V> next) {
603              this.hash = hash;
604              this.key = key;
605              this.val = val;
606              this.next = next;
607          }
608  
609 <        /** CompareAndSet the hash field */
610 <        final boolean casHash(int cmp, int val) {
611 <            return UNSAFE.compareAndSwapInt(this, hashOffset, cmp, val);
612 <        }
613 <
614 <        /** The number of spins before blocking for a lock */
528 <        static final int MAX_SPINS =
529 <            Runtime.getRuntime().availableProcessors() > 1 ? 64 : 1;
530 <
531 <        /**
532 <         * Spins a while if LOCKED bit set and this node is the first
533 <         * of its bin, and then sets WAITING bits on hash field and
534 <         * blocks (once) if they are still set.  It is OK for this
535 <         * method to return even if lock is not available upon exit,
536 <         * which enables these simple single-wait mechanics.
537 <         *
538 <         * The corresponding signalling operation is performed within
539 <         * callers: Upon detecting that WAITING has been set when
540 <         * unlocking lock (via a failed CAS from non-waiting LOCKED
541 <         * state), unlockers acquire the sync lock and perform a
542 <         * notifyAll.
543 <         */
544 <        final void tryAwaitLock(Node[] tab, int i) {
545 <            if (tab != null && i >= 0 && i < tab.length) { // bounds check
546 <                int r = ThreadLocalRandom.current().nextInt(); // randomize spins
547 <                int spins = MAX_SPINS, h;
548 <                while (tabAt(tab, i) == this && ((h = hash) & LOCKED) != 0) {
549 <                    if (spins >= 0) {
550 <                        r ^= r << 1; r ^= r >>> 3; r ^= r << 10; // xorshift
551 <                        if (r >= 0 && --spins == 0)
552 <                            Thread.yield();  // yield before block
553 <                    }
554 <                    else if (casHash(h, h | WAITING)) {
555 <                        synchronized (this) {
556 <                            if (tabAt(tab, i) == this &&
557 <                                (hash & WAITING) == WAITING) {
558 <                                try {
559 <                                    wait();
560 <                                } catch (InterruptedException ie) {
561 <                                    Thread.currentThread().interrupt();
562 <                                }
563 <                            }
564 <                            else
565 <                                notifyAll(); // possibly won race vs signaller
566 <                        }
567 <                        break;
568 <                    }
569 <                }
570 <            }
571 <        }
572 <
573 <        // Unsafe mechanics for casHash
574 <        private static final sun.misc.Unsafe UNSAFE;
575 <        private static final long hashOffset;
576 <
577 <        static {
578 <            try {
579 <                UNSAFE = getUnsafe();
580 <                Class<?> k = Node.class;
581 <                hashOffset = UNSAFE.objectFieldOffset
582 <                    (k.getDeclaredField("hash"));
583 <            } catch (Exception e) {
584 <                throw new Error(e);
585 <            }
586 <        }
587 <    }
588 <
589 <    /* ---------------- TreeBins -------------- */
590 <
591 <    /**
592 <     * Nodes for use in TreeBins
593 <     */
594 <    static final class TreeNode extends Node {
595 <        TreeNode parent;  // red-black tree links
596 <        TreeNode left;
597 <        TreeNode right;
598 <        TreeNode prev;    // needed to unlink next upon deletion
599 <        boolean red;
600 <
601 <        TreeNode(int hash, Object key, Object val, Node next, TreeNode parent) {
602 <            super(hash, key, val, next);
603 <            this.parent = parent;
604 <        }
605 <    }
606 <
607 <    /**
608 <     * A specialized form of red-black tree for use in bins
609 <     * whose size exceeds a threshold.
610 <     *
611 <     * TreeBins use a special form of comparison for search and
612 <     * related operations (which is the main reason we cannot use
613 <     * existing collections such as TreeMaps). TreeBins contain
614 <     * Comparable elements, but may contain others, as well as
615 <     * elements that are Comparable but not necessarily Comparable<T>
616 <     * for the same T, so we cannot invoke compareTo among them. To
617 <     * handle this, the tree is ordered primarily by hash value, then
618 <     * by getClass().getName() order, and then by Comparator order
619 <     * among elements of the same class.  On lookup at a node, if
620 <     * elements are not comparable or compare as 0, both left and
621 <     * right children may need to be searched in the case of tied hash
622 <     * values. (This corresponds to the full list search that would be
623 <     * necessary if all elements were non-Comparable and had tied
624 <     * hashes.)  The red-black balancing code is updated from
625 <     * pre-jdk-collections
626 <     * (http://gee.cs.oswego.edu/dl/classes/collections/RBCell.java)
627 <     * based in turn on Cormen, Leiserson, and Rivest "Introduction to
628 <     * Algorithms" (CLR).
629 <     *
630 <     * TreeBins also maintain a separate locking discipline than
631 <     * regular bins. Because they are forwarded via special MOVED
632 <     * nodes at bin heads (which can never change once established),
633 <     * we cannot use those nodes as locks. Instead, TreeBin
634 <     * extends AbstractQueuedSynchronizer to support a simple form of
635 <     * read-write lock. For update operations and table validation,
636 <     * the exclusive form of lock behaves in the same way as bin-head
637 <     * locks. However, lookups use shared read-lock mechanics to allow
638 <     * multiple readers in the absence of writers.  Additionally,
639 <     * these lookups do not ever block: While the lock is not
640 <     * available, they proceed along the slow traversal path (via
641 <     * next-pointers) until the lock becomes available or the list is
642 <     * exhausted, whichever comes first. (These cases are not fast,
643 <     * but maximize aggregate expected throughput.)  The AQS mechanics
644 <     * for doing this are straightforward.  The lock state is held as
645 <     * AQS getState().  Read counts are negative; the write count (1)
646 <     * is positive.  There are no signalling preferences among readers
647 <     * and writers. Since we don't need to export full Lock API, we
648 <     * just override the minimal AQS methods and use them directly.
649 <     */
650 <    static final class TreeBin extends AbstractQueuedSynchronizer {
651 <        private static final long serialVersionUID = 2249069246763182397L;
652 <        transient TreeNode root;  // root of tree
653 <        transient TreeNode first; // head of next-pointer list
654 <
655 <        /* AQS overrides */
656 <        public final boolean isHeldExclusively() { return getState() > 0; }
657 <        public final boolean tryAcquire(int ignore) {
658 <            if (compareAndSetState(0, 1)) {
659 <                setExclusiveOwnerThread(Thread.currentThread());
660 <                return true;
661 <            }
662 <            return false;
663 <        }
664 <        public final boolean tryRelease(int ignore) {
665 <            setExclusiveOwnerThread(null);
666 <            setState(0);
667 <            return true;
668 <        }
669 <        public final int tryAcquireShared(int ignore) {
670 <            for (int c;;) {
671 <                if ((c = getState()) > 0)
672 <                    return -1;
673 <                if (compareAndSetState(c, c -1))
674 <                    return 1;
675 <            }
676 <        }
677 <        public final boolean tryReleaseShared(int ignore) {
678 <            int c;
679 <            do {} while (!compareAndSetState(c = getState(), c + 1));
680 <            return c == -1;
681 <        }
682 <
683 <        /** From CLR */
684 <        private void rotateLeft(TreeNode p) {
685 <            if (p != null) {
686 <                TreeNode r = p.right, pp, rl;
687 <                if ((rl = p.right = r.left) != null)
688 <                    rl.parent = p;
689 <                if ((pp = r.parent = p.parent) == null)
690 <                    root = r;
691 <                else if (pp.left == p)
692 <                    pp.left = r;
693 <                else
694 <                    pp.right = r;
695 <                r.left = p;
696 <                p.parent = r;
697 <            }
698 <        }
699 <
700 <        /** From CLR */
701 <        private void rotateRight(TreeNode p) {
702 <            if (p != null) {
703 <                TreeNode l = p.left, pp, lr;
704 <                if ((lr = p.left = l.right) != null)
705 <                    lr.parent = p;
706 <                if ((pp = l.parent = p.parent) == null)
707 <                    root = l;
708 <                else if (pp.right == p)
709 <                    pp.right = l;
710 <                else
711 <                    pp.left = l;
712 <                l.right = p;
713 <                p.parent = l;
714 <            }
715 <        }
716 <
717 <        /**
718 <         * Returns the TreeNode (or null if not found) for the given key
719 <         * starting at given root.
720 <         */
721 <        @SuppressWarnings("unchecked") // suppress Comparable cast warning
722 <        final TreeNode getTreeNode(int h, Object k, TreeNode p) {
723 <            Class<?> c = k.getClass();
724 <            while (p != null) {
725 <                int dir, ph;  Object pk; Class<?> pc;
726 <                if ((ph = p.hash) == h) {
727 <                    if ((pk = p.key) == k || k.equals(pk))
728 <                        return p;
729 <                    if (c != (pc = pk.getClass()) ||
730 <                        !(k instanceof Comparable) ||
731 <                        (dir = ((Comparable)k).compareTo((Comparable)pk)) == 0) {
732 <                        dir = (c == pc) ? 0 : c.getName().compareTo(pc.getName());
733 <                        TreeNode r = null, s = null, pl, pr;
734 <                        if (dir >= 0) {
735 <                            if ((pl = p.left) != null && h <= pl.hash)
736 <                                s = pl;
737 <                        }
738 <                        else if ((pr = p.right) != null && h >= pr.hash)
739 <                            s = pr;
740 <                        if (s != null && (r = getTreeNode(h, k, s)) != null)
741 <                            return r;
742 <                    }
743 <                }
744 <                else
745 <                    dir = (h < ph) ? -1 : 1;
746 <                p = (dir > 0) ? p.right : p.left;
747 <            }
748 <            return null;
609 >        public final K getKey()       { return key; }
610 >        public final V getValue()     { return val; }
611 >        public final int hashCode()   { return key.hashCode() ^ val.hashCode(); }
612 >        public final String toString(){ return key + "=" + val; }
613 >        public final V setValue(V value) {
614 >            throw new UnsupportedOperationException();
615          }
616  
617 <        /**
618 <         * Wrapper for getTreeNode used by CHM.get. Tries to obtain
619 <         * read-lock to call getTreeNode, but during failure to get
620 <         * lock, searches along next links.
621 <         */
622 <        final Object getValue(int h, Object k) {
623 <            Node r = null;
758 <            int c = getState(); // Must read lock state first
759 <            for (Node e = first; e != null; e = e.next) {
760 <                if (c <= 0 && compareAndSetState(c, c - 1)) {
761 <                    try {
762 <                        r = getTreeNode(h, k, root);
763 <                    } finally {
764 <                        releaseShared(0);
765 <                    }
766 <                    break;
767 <                }
768 <                else if ((e.hash & HASH_BITS) == h && k.equals(e.key)) {
769 <                    r = e;
770 <                    break;
771 <                }
772 <                else
773 <                    c = getState();
774 <            }
775 <            return r == null ? null : r.val;
617 >        public final boolean equals(Object o) {
618 >            Object k, v, u; Map.Entry<?,?> e;
619 >            return ((o instanceof Map.Entry) &&
620 >                    (k = (e = (Map.Entry<?,?>)o).getKey()) != null &&
621 >                    (v = e.getValue()) != null &&
622 >                    (k == key || k.equals(key)) &&
623 >                    (v == (u = val) || v.equals(u)));
624          }
625  
626          /**
627 <         * Finds or adds a node.
780 <         * @return null if added
627 >         * Virtualized support for map.get(); overridden in subclasses.
628           */
629 <        @SuppressWarnings("unchecked") // suppress Comparable cast warning
630 <        final TreeNode putTreeNode(int h, Object k, Object v) {
631 <            Class<?> c = k.getClass();
632 <            TreeNode pp = root, p = null;
633 <            int dir = 0;
634 <            while (pp != null) { // find existing node or leaf to insert at
635 <                int ph;  Object pk; Class<?> pc;
636 <                p = pp;
637 <                if ((ph = p.hash) == h) {
791 <                    if ((pk = p.key) == k || k.equals(pk))
792 <                        return p;
793 <                    if (c != (pc = pk.getClass()) ||
794 <                        !(k instanceof Comparable) ||
795 <                        (dir = ((Comparable)k).compareTo((Comparable)pk)) == 0) {
796 <                        dir = (c == pc) ? 0 : c.getName().compareTo(pc.getName());
797 <                        TreeNode r = null, s = null, pl, pr;
798 <                        if (dir >= 0) {
799 <                            if ((pl = p.left) != null && h <= pl.hash)
800 <                                s = pl;
801 <                        }
802 <                        else if ((pr = p.right) != null && h >= pr.hash)
803 <                            s = pr;
804 <                        if (s != null && (r = getTreeNode(h, k, s)) != null)
805 <                            return r;
806 <                    }
807 <                }
808 <                else
809 <                    dir = (h < ph) ? -1 : 1;
810 <                pp = (dir > 0) ? p.right : p.left;
811 <            }
812 <
813 <            TreeNode f = first;
814 <            TreeNode x = first = new TreeNode(h, k, v, f, p);
815 <            if (p == null)
816 <                root = x;
817 <            else { // attach and rebalance; adapted from CLR
818 <                TreeNode xp, xpp;
819 <                if (f != null)
820 <                    f.prev = x;
821 <                if (dir <= 0)
822 <                    p.left = x;
823 <                else
824 <                    p.right = x;
825 <                x.red = true;
826 <                while (x != null && (xp = x.parent) != null && xp.red &&
827 <                       (xpp = xp.parent) != null) {
828 <                    TreeNode xppl = xpp.left;
829 <                    if (xp == xppl) {
830 <                        TreeNode y = xpp.right;
831 <                        if (y != null && y.red) {
832 <                            y.red = false;
833 <                            xp.red = false;
834 <                            xpp.red = true;
835 <                            x = xpp;
836 <                        }
837 <                        else {
838 <                            if (x == xp.right) {
839 <                                rotateLeft(x = xp);
840 <                                xpp = (xp = x.parent) == null ? null : xp.parent;
841 <                            }
842 <                            if (xp != null) {
843 <                                xp.red = false;
844 <                                if (xpp != null) {
845 <                                    xpp.red = true;
846 <                                    rotateRight(xpp);
847 <                                }
848 <                            }
849 <                        }
850 <                    }
851 <                    else {
852 <                        TreeNode y = xppl;
853 <                        if (y != null && y.red) {
854 <                            y.red = false;
855 <                            xp.red = false;
856 <                            xpp.red = true;
857 <                            x = xpp;
858 <                        }
859 <                        else {
860 <                            if (x == xp.left) {
861 <                                rotateRight(x = xp);
862 <                                xpp = (xp = x.parent) == null ? null : xp.parent;
863 <                            }
864 <                            if (xp != null) {
865 <                                xp.red = false;
866 <                                if (xpp != null) {
867 <                                    xpp.red = true;
868 <                                    rotateLeft(xpp);
869 <                                }
870 <                            }
871 <                        }
872 <                    }
873 <                }
874 <                TreeNode r = root;
875 <                if (r != null && r.red)
876 <                    r.red = false;
629 >        Node<K,V> find(int h, Object k) {
630 >            Node<K,V> e = this;
631 >            if (k != null) {
632 >                do {
633 >                    K ek;
634 >                    if (e.hash == h &&
635 >                        ((ek = e.key) == k || (ek != null && k.equals(ek))))
636 >                        return e;
637 >                } while ((e = e.next) != null);
638              }
639              return null;
640          }
880
881        /**
882         * Removes the given node, that must be present before this
883         * call.  This is messier than typical red-black deletion code
884         * because we cannot swap the contents of an interior node
885         * with a leaf successor that is pinned by "next" pointers
886         * that are accessible independently of lock. So instead we
887         * swap the tree linkages.
888         */
889        final void deleteTreeNode(TreeNode p) {
890            TreeNode next = (TreeNode)p.next; // unlink traversal pointers
891            TreeNode pred = p.prev;
892            if (pred == null)
893                first = next;
894            else
895                pred.next = next;
896            if (next != null)
897                next.prev = pred;
898            TreeNode replacement;
899            TreeNode pl = p.left;
900            TreeNode pr = p.right;
901            if (pl != null && pr != null) {
902                TreeNode s = pr, sl;
903                while ((sl = s.left) != null) // find successor
904                    s = sl;
905                boolean c = s.red; s.red = p.red; p.red = c; // swap colors
906                TreeNode sr = s.right;
907                TreeNode pp = p.parent;
908                if (s == pr) { // p was s's direct parent
909                    p.parent = s;
910                    s.right = p;
911                }
912                else {
913                    TreeNode sp = s.parent;
914                    if ((p.parent = sp) != null) {
915                        if (s == sp.left)
916                            sp.left = p;
917                        else
918                            sp.right = p;
919                    }
920                    if ((s.right = pr) != null)
921                        pr.parent = s;
922                }
923                p.left = null;
924                if ((p.right = sr) != null)
925                    sr.parent = p;
926                if ((s.left = pl) != null)
927                    pl.parent = s;
928                if ((s.parent = pp) == null)
929                    root = s;
930                else if (p == pp.left)
931                    pp.left = s;
932                else
933                    pp.right = s;
934                replacement = sr;
935            }
936            else
937                replacement = (pl != null) ? pl : pr;
938            TreeNode pp = p.parent;
939            if (replacement == null) {
940                if (pp == null) {
941                    root = null;
942                    return;
943                }
944                replacement = p;
945            }
946            else {
947                replacement.parent = pp;
948                if (pp == null)
949                    root = replacement;
950                else if (p == pp.left)
951                    pp.left = replacement;
952                else
953                    pp.right = replacement;
954                p.left = p.right = p.parent = null;
955            }
956            if (!p.red) { // rebalance, from CLR
957                TreeNode x = replacement;
958                while (x != null) {
959                    TreeNode xp, xpl;
960                    if (x.red || (xp = x.parent) == null) {
961                        x.red = false;
962                        break;
963                    }
964                    if (x == (xpl = xp.left)) {
965                        TreeNode sib = xp.right;
966                        if (sib != null && sib.red) {
967                            sib.red = false;
968                            xp.red = true;
969                            rotateLeft(xp);
970                            sib = (xp = x.parent) == null ? null : xp.right;
971                        }
972                        if (sib == null)
973                            x = xp;
974                        else {
975                            TreeNode sl = sib.left, sr = sib.right;
976                            if ((sr == null || !sr.red) &&
977                                (sl == null || !sl.red)) {
978                                sib.red = true;
979                                x = xp;
980                            }
981                            else {
982                                if (sr == null || !sr.red) {
983                                    if (sl != null)
984                                        sl.red = false;
985                                    sib.red = true;
986                                    rotateRight(sib);
987                                    sib = (xp = x.parent) == null ? null : xp.right;
988                                }
989                                if (sib != null) {
990                                    sib.red = (xp == null) ? false : xp.red;
991                                    if ((sr = sib.right) != null)
992                                        sr.red = false;
993                                }
994                                if (xp != null) {
995                                    xp.red = false;
996                                    rotateLeft(xp);
997                                }
998                                x = root;
999                            }
1000                        }
1001                    }
1002                    else { // symmetric
1003                        TreeNode sib = xpl;
1004                        if (sib != null && sib.red) {
1005                            sib.red = false;
1006                            xp.red = true;
1007                            rotateRight(xp);
1008                            sib = (xp = x.parent) == null ? null : xp.left;
1009                        }
1010                        if (sib == null)
1011                            x = xp;
1012                        else {
1013                            TreeNode sl = sib.left, sr = sib.right;
1014                            if ((sl == null || !sl.red) &&
1015                                (sr == null || !sr.red)) {
1016                                sib.red = true;
1017                                x = xp;
1018                            }
1019                            else {
1020                                if (sl == null || !sl.red) {
1021                                    if (sr != null)
1022                                        sr.red = false;
1023                                    sib.red = true;
1024                                    rotateLeft(sib);
1025                                    sib = (xp = x.parent) == null ? null : xp.left;
1026                                }
1027                                if (sib != null) {
1028                                    sib.red = (xp == null) ? false : xp.red;
1029                                    if ((sl = sib.left) != null)
1030                                        sl.red = false;
1031                                }
1032                                if (xp != null) {
1033                                    xp.red = false;
1034                                    rotateRight(xp);
1035                                }
1036                                x = root;
1037                            }
1038                        }
1039                    }
1040                }
1041            }
1042            if (p == replacement && (pp = p.parent) != null) {
1043                if (p == pp.left) // detach pointers
1044                    pp.left = null;
1045                else if (p == pp.right)
1046                    pp.right = null;
1047                p.parent = null;
1048            }
1049        }
641      }
642  
643 <    /* ---------------- Collision reduction methods -------------- */
643 >    /* ---------------- Static utilities -------------- */
644  
645      /**
646 <     * Spreads higher bits to lower, and also forces top 2 bits to 0.
647 <     * Because the table uses power-of-two masking, sets of hashes
648 <     * that vary only in bits above the current mask will always
649 <     * collide. (Among known examples are sets of Float keys holding
650 <     * consecutive whole numbers in small tables.)  To counter this,
651 <     * we apply a transform that spreads the impact of higher bits
646 >     * Spreads (XORs) higher bits of hash to lower and also forces top
647 >     * bit to 0. Because the table uses power-of-two masking, sets of
648 >     * hashes that vary only in bits above the current mask will
649 >     * always collide. (Among known examples are sets of Float keys
650 >     * holding consecutive whole numbers in small tables.)  So we
651 >     * apply a transform that spreads the impact of higher bits
652       * downward. There is a tradeoff between speed, utility, and
653       * quality of bit-spreading. Because many common sets of hashes
654 <     * are already reasonably distributed across bits (so don't benefit
655 <     * from spreading), and because we use trees to handle large sets
656 <     * of collisions in bins, we don't need excessively high quality.
654 >     * are already reasonably distributed (so don't benefit from
655 >     * spreading), and because we use trees to handle large sets of
656 >     * collisions in bins, we just XOR some shifted bits in the
657 >     * cheapest possible way to reduce systematic lossage, as well as
658 >     * to incorporate impact of the highest bits that would otherwise
659 >     * never be used in index calculations because of table bounds.
660       */
661 <    private static final int spread(int h) {
662 <        h ^= (h >>> 18) ^ (h >>> 12);
1069 <        return (h ^ (h >>> 10)) & HASH_BITS;
661 >    static final int spread(int h) {
662 >        return (h ^ (h >>> 16)) & HASH_BITS;
663      }
664  
665      /**
666 <     * Replaces a list bin with a tree bin. Call only when locked.
667 <     * Fails to replace if the given key is non-comparable or table
1075 <     * is, or needs, resizing.
666 >     * Returns a power of two table size for the given desired capacity.
667 >     * See Hackers Delight, sec 3.2
668       */
669 <    private final void replaceWithTreeBin(Node[] tab, int index, Object key) {
670 <        if ((key instanceof Comparable) &&
671 <            (tab.length >= MAXIMUM_CAPACITY || counter.sum() < (long)sizeCtl)) {
672 <            TreeBin t = new TreeBin();
673 <            for (Node e = tabAt(tab, index); e != null; e = e.next)
674 <                t.putTreeNode(e.hash & HASH_BITS, e.key, e.val);
675 <            setTabAt(tab, index, new Node(MOVED, t, null, null));
676 <        }
669 >    private static final int tableSizeFor(int c) {
670 >        int n = c - 1;
671 >        n |= n >>> 1;
672 >        n |= n >>> 2;
673 >        n |= n >>> 4;
674 >        n |= n >>> 8;
675 >        n |= n >>> 16;
676 >        return (n < 0) ? 1 : (n >= MAXIMUM_CAPACITY) ? MAXIMUM_CAPACITY : n + 1;
677      }
678  
679 <    /* ---------------- Internal access and update methods -------------- */
680 <
681 <    /** Implementation for get and containsKey */
682 <    private final Object internalGet(Object k) {
683 <        int h = spread(k.hashCode());
684 <        retry: for (Node[] tab = table; tab != null;) {
685 <            Node e, p; Object ek, ev; int eh;      // locals to read fields once
686 <            for (e = tabAt(tab, (tab.length - 1) & h); e != null; e = e.next) {
687 <                if ((eh = e.hash) == MOVED) {
688 <                    if ((ek = e.key) instanceof TreeBin)  // search TreeBin
689 <                        return ((TreeBin)ek).getValue(h, k);
690 <                    else {                        // restart with new table
691 <                        tab = (Node[])ek;
692 <                        continue retry;
693 <                    }
679 >    /**
680 >     * Returns x's Class if it is of the form "class C implements
681 >     * Comparable<C>", else null.
682 >     */
683 >    static Class<?> comparableClassFor(Object x) {
684 >        if (x instanceof Comparable) {
685 >            Class<?> c; Type[] ts, as; Type t; ParameterizedType p;
686 >            if ((c = x.getClass()) == String.class) // bypass checks
687 >                return c;
688 >            if ((ts = c.getGenericInterfaces()) != null) {
689 >                for (int i = 0; i < ts.length; ++i) {
690 >                    if (((t = ts[i]) instanceof ParameterizedType) &&
691 >                        ((p = (ParameterizedType)t).getRawType() ==
692 >                         Comparable.class) &&
693 >                        (as = p.getActualTypeArguments()) != null &&
694 >                        as.length == 1 && as[0] == c) // type arg is c
695 >                        return c;
696                  }
1103                else if ((eh & HASH_BITS) == h && (ev = e.val) != null &&
1104                         ((ek = e.key) == k || k.equals(ek)))
1105                    return ev;
697              }
1107            break;
698          }
699          return null;
700      }
701  
702      /**
703 <     * Implementation for the four public remove/replace methods:
704 <     * Replaces node value with v, conditional upon match of cv if
1115 <     * non-null.  If resulting value is null, delete.
703 >     * Returns k.compareTo(x) if x matches kc (k's screened comparable
704 >     * class), else 0.
705       */
706 <    private final Object internalReplace(Object k, Object v, Object cv) {
707 <        int h = spread(k.hashCode());
708 <        Object oldVal = null;
709 <        for (Node[] tab = table;;) {
1121 <            Node f; int i, fh; Object fk;
1122 <            if (tab == null ||
1123 <                (f = tabAt(tab, i = (tab.length - 1) & h)) == null)
1124 <                break;
1125 <            else if ((fh = f.hash) == MOVED) {
1126 <                if ((fk = f.key) instanceof TreeBin) {
1127 <                    TreeBin t = (TreeBin)fk;
1128 <                    boolean validated = false;
1129 <                    boolean deleted = false;
1130 <                    t.acquire(0);
1131 <                    try {
1132 <                        if (tabAt(tab, i) == f) {
1133 <                            validated = true;
1134 <                            TreeNode p = t.getTreeNode(h, k, t.root);
1135 <                            if (p != null) {
1136 <                                Object pv = p.val;
1137 <                                if (cv == null || cv == pv || cv.equals(pv)) {
1138 <                                    oldVal = pv;
1139 <                                    if ((p.val = v) == null) {
1140 <                                        deleted = true;
1141 <                                        t.deleteTreeNode(p);
1142 <                                    }
1143 <                                }
1144 <                            }
1145 <                        }
1146 <                    } finally {
1147 <                        t.release(0);
1148 <                    }
1149 <                    if (validated) {
1150 <                        if (deleted)
1151 <                            counter.add(-1L);
1152 <                        break;
1153 <                    }
1154 <                }
1155 <                else
1156 <                    tab = (Node[])fk;
1157 <            }
1158 <            else if ((fh & HASH_BITS) != h && f.next == null) // precheck
1159 <                break;                          // rules out possible existence
1160 <            else if ((fh & LOCKED) != 0) {
1161 <                checkForResize();               // try resizing if can't get lock
1162 <                f.tryAwaitLock(tab, i);
1163 <            }
1164 <            else if (f.casHash(fh, fh | LOCKED)) {
1165 <                boolean validated = false;
1166 <                boolean deleted = false;
1167 <                try {
1168 <                    if (tabAt(tab, i) == f) {
1169 <                        validated = true;
1170 <                        for (Node e = f, pred = null;;) {
1171 <                            Object ek, ev;
1172 <                            if ((e.hash & HASH_BITS) == h &&
1173 <                                ((ev = e.val) != null) &&
1174 <                                ((ek = e.key) == k || k.equals(ek))) {
1175 <                                if (cv == null || cv == ev || cv.equals(ev)) {
1176 <                                    oldVal = ev;
1177 <                                    if ((e.val = v) == null) {
1178 <                                        deleted = true;
1179 <                                        Node en = e.next;
1180 <                                        if (pred != null)
1181 <                                            pred.next = en;
1182 <                                        else
1183 <                                            setTabAt(tab, i, en);
1184 <                                    }
1185 <                                }
1186 <                                break;
1187 <                            }
1188 <                            pred = e;
1189 <                            if ((e = e.next) == null)
1190 <                                break;
1191 <                        }
1192 <                    }
1193 <                } finally {
1194 <                    if (!f.casHash(fh | LOCKED, fh)) {
1195 <                        f.hash = fh;
1196 <                        synchronized (f) { f.notifyAll(); };
1197 <                    }
1198 <                }
1199 <                if (validated) {
1200 <                    if (deleted)
1201 <                        counter.add(-1L);
1202 <                    break;
1203 <                }
1204 <            }
1205 <        }
1206 <        return oldVal;
706 >    @SuppressWarnings({"rawtypes","unchecked"}) // for cast to Comparable
707 >    static int compareComparables(Class<?> kc, Object k, Object x) {
708 >        return (x == null || x.getClass() != kc ? 0 :
709 >                ((Comparable)k).compareTo(x));
710      }
711  
712 <    /*
1210 <     * Internal versions of the six insertion methods, each a
1211 <     * little more complicated than the last. All have
1212 <     * the same basic structure as the first (internalPut):
1213 <     *  1. If table uninitialized, create
1214 <     *  2. If bin empty, try to CAS new node
1215 <     *  3. If bin stale, use new table
1216 <     *  4. if bin converted to TreeBin, validate and relay to TreeBin methods
1217 <     *  5. Lock and validate; if valid, scan and add or update
1218 <     *
1219 <     * The others interweave other checks and/or alternative actions:
1220 <     *  * Plain put checks for and performs resize after insertion.
1221 <     *  * putIfAbsent prescans for mapping without lock (and fails to add
1222 <     *    if present), which also makes pre-emptive resize checks worthwhile.
1223 <     *  * computeIfAbsent extends form used in putIfAbsent with additional
1224 <     *    mechanics to deal with, calls, potential exceptions and null
1225 <     *    returns from function call.
1226 <     *  * compute uses the same function-call mechanics, but without
1227 <     *    the prescans
1228 <     *  * merge acts as putIfAbsent in the absent case, but invokes the
1229 <     *    update function if present
1230 <     *  * putAll attempts to pre-allocate enough table space
1231 <     *    and more lazily performs count updates and checks.
1232 <     *
1233 <     * Someday when details settle down a bit more, it might be worth
1234 <     * some factoring to reduce sprawl.
1235 <     */
1236 <
1237 <    /** Implementation for put */
1238 <    private final Object internalPut(Object k, Object v) {
1239 <        int h = spread(k.hashCode());
1240 <        int count = 0;
1241 <        for (Node[] tab = table;;) {
1242 <            int i; Node f; int fh; Object fk;
1243 <            if (tab == null)
1244 <                tab = initTable();
1245 <            else if ((f = tabAt(tab, i = (tab.length - 1) & h)) == null) {
1246 <                if (casTabAt(tab, i, null, new Node(h, k, v, null)))
1247 <                    break;                   // no lock when adding to empty bin
1248 <            }
1249 <            else if ((fh = f.hash) == MOVED) {
1250 <                if ((fk = f.key) instanceof TreeBin) {
1251 <                    TreeBin t = (TreeBin)fk;
1252 <                    Object oldVal = null;
1253 <                    t.acquire(0);
1254 <                    try {
1255 <                        if (tabAt(tab, i) == f) {
1256 <                            count = 2;
1257 <                            TreeNode p = t.putTreeNode(h, k, v);
1258 <                            if (p != null) {
1259 <                                oldVal = p.val;
1260 <                                p.val = v;
1261 <                            }
1262 <                        }
1263 <                    } finally {
1264 <                        t.release(0);
1265 <                    }
1266 <                    if (count != 0) {
1267 <                        if (oldVal != null)
1268 <                            return oldVal;
1269 <                        break;
1270 <                    }
1271 <                }
1272 <                else
1273 <                    tab = (Node[])fk;
1274 <            }
1275 <            else if ((fh & LOCKED) != 0) {
1276 <                checkForResize();
1277 <                f.tryAwaitLock(tab, i);
1278 <            }
1279 <            else if (f.casHash(fh, fh | LOCKED)) {
1280 <                Object oldVal = null;
1281 <                try {                        // needed in case equals() throws
1282 <                    if (tabAt(tab, i) == f) {
1283 <                        count = 1;
1284 <                        for (Node e = f;; ++count) {
1285 <                            Object ek, ev;
1286 <                            if ((e.hash & HASH_BITS) == h &&
1287 <                                (ev = e.val) != null &&
1288 <                                ((ek = e.key) == k || k.equals(ek))) {
1289 <                                oldVal = ev;
1290 <                                e.val = v;
1291 <                                break;
1292 <                            }
1293 <                            Node last = e;
1294 <                            if ((e = e.next) == null) {
1295 <                                last.next = new Node(h, k, v, null);
1296 <                                if (count >= TREE_THRESHOLD)
1297 <                                    replaceWithTreeBin(tab, i, k);
1298 <                                break;
1299 <                            }
1300 <                        }
1301 <                    }
1302 <                } finally {                  // unlock and signal if needed
1303 <                    if (!f.casHash(fh | LOCKED, fh)) {
1304 <                        f.hash = fh;
1305 <                        synchronized (f) { f.notifyAll(); };
1306 <                    }
1307 <                }
1308 <                if (count != 0) {
1309 <                    if (oldVal != null)
1310 <                        return oldVal;
1311 <                    if (tab.length <= 64)
1312 <                        count = 2;
1313 <                    break;
1314 <                }
1315 <            }
1316 <        }
1317 <        counter.add(1L);
1318 <        if (count > 1)
1319 <            checkForResize();
1320 <        return null;
1321 <    }
1322 <
1323 <    /** Implementation for putIfAbsent */
1324 <    private final Object internalPutIfAbsent(Object k, Object v) {
1325 <        int h = spread(k.hashCode());
1326 <        int count = 0;
1327 <        for (Node[] tab = table;;) {
1328 <            int i; Node f; int fh; Object fk, fv;
1329 <            if (tab == null)
1330 <                tab = initTable();
1331 <            else if ((f = tabAt(tab, i = (tab.length - 1) & h)) == null) {
1332 <                if (casTabAt(tab, i, null, new Node(h, k, v, null)))
1333 <                    break;
1334 <            }
1335 <            else if ((fh = f.hash) == MOVED) {
1336 <                if ((fk = f.key) instanceof TreeBin) {
1337 <                    TreeBin t = (TreeBin)fk;
1338 <                    Object oldVal = null;
1339 <                    t.acquire(0);
1340 <                    try {
1341 <                        if (tabAt(tab, i) == f) {
1342 <                            count = 2;
1343 <                            TreeNode p = t.putTreeNode(h, k, v);
1344 <                            if (p != null)
1345 <                                oldVal = p.val;
1346 <                        }
1347 <                    } finally {
1348 <                        t.release(0);
1349 <                    }
1350 <                    if (count != 0) {
1351 <                        if (oldVal != null)
1352 <                            return oldVal;
1353 <                        break;
1354 <                    }
1355 <                }
1356 <                else
1357 <                    tab = (Node[])fk;
1358 <            }
1359 <            else if ((fh & HASH_BITS) == h && (fv = f.val) != null &&
1360 <                     ((fk = f.key) == k || k.equals(fk)))
1361 <                return fv;
1362 <            else {
1363 <                Node g = f.next;
1364 <                if (g != null) { // at least 2 nodes -- search and maybe resize
1365 <                    for (Node e = g;;) {
1366 <                        Object ek, ev;
1367 <                        if ((e.hash & HASH_BITS) == h && (ev = e.val) != null &&
1368 <                            ((ek = e.key) == k || k.equals(ek)))
1369 <                            return ev;
1370 <                        if ((e = e.next) == null) {
1371 <                            checkForResize();
1372 <                            break;
1373 <                        }
1374 <                    }
1375 <                }
1376 <                if (((fh = f.hash) & LOCKED) != 0) {
1377 <                    checkForResize();
1378 <                    f.tryAwaitLock(tab, i);
1379 <                }
1380 <                else if (tabAt(tab, i) == f && f.casHash(fh, fh | LOCKED)) {
1381 <                    Object oldVal = null;
1382 <                    try {
1383 <                        if (tabAt(tab, i) == f) {
1384 <                            count = 1;
1385 <                            for (Node e = f;; ++count) {
1386 <                                Object ek, ev;
1387 <                                if ((e.hash & HASH_BITS) == h &&
1388 <                                    (ev = e.val) != null &&
1389 <                                    ((ek = e.key) == k || k.equals(ek))) {
1390 <                                    oldVal = ev;
1391 <                                    break;
1392 <                                }
1393 <                                Node last = e;
1394 <                                if ((e = e.next) == null) {
1395 <                                    last.next = new Node(h, k, v, null);
1396 <                                    if (count >= TREE_THRESHOLD)
1397 <                                        replaceWithTreeBin(tab, i, k);
1398 <                                    break;
1399 <                                }
1400 <                            }
1401 <                        }
1402 <                    } finally {
1403 <                        if (!f.casHash(fh | LOCKED, fh)) {
1404 <                            f.hash = fh;
1405 <                            synchronized (f) { f.notifyAll(); };
1406 <                        }
1407 <                    }
1408 <                    if (count != 0) {
1409 <                        if (oldVal != null)
1410 <                            return oldVal;
1411 <                        if (tab.length <= 64)
1412 <                            count = 2;
1413 <                        break;
1414 <                    }
1415 <                }
1416 <            }
1417 <        }
1418 <        counter.add(1L);
1419 <        if (count > 1)
1420 <            checkForResize();
1421 <        return null;
1422 <    }
712 >    /* ---------------- Table element access -------------- */
713  
714 <    /** Implementation for computeIfAbsent */
715 <    private final Object internalComputeIfAbsent(K k,
716 <                                                 Fun<? super K, ?> mf) {
717 <        int h = spread(k.hashCode());
718 <        Object val = null;
719 <        int count = 0;
720 <        for (Node[] tab = table;;) {
721 <            Node f; int i, fh; Object fk, fv;
722 <            if (tab == null)
723 <                tab = initTable();
724 <            else if ((f = tabAt(tab, i = (tab.length - 1) & h)) == null) {
725 <                Node node = new Node(fh = h | LOCKED, k, null, null);
726 <                if (casTabAt(tab, i, null, node)) {
727 <                    count = 1;
1438 <                    try {
1439 <                        if ((val = mf.apply(k)) != null)
1440 <                            node.val = val;
1441 <                    } finally {
1442 <                        if (val == null)
1443 <                            setTabAt(tab, i, null);
1444 <                        if (!node.casHash(fh, h)) {
1445 <                            node.hash = h;
1446 <                            synchronized (node) { node.notifyAll(); };
1447 <                        }
1448 <                    }
1449 <                }
1450 <                if (count != 0)
1451 <                    break;
1452 <            }
1453 <            else if ((fh = f.hash) == MOVED) {
1454 <                if ((fk = f.key) instanceof TreeBin) {
1455 <                    TreeBin t = (TreeBin)fk;
1456 <                    boolean added = false;
1457 <                    t.acquire(0);
1458 <                    try {
1459 <                        if (tabAt(tab, i) == f) {
1460 <                            count = 1;
1461 <                            TreeNode p = t.getTreeNode(h, k, t.root);
1462 <                            if (p != null)
1463 <                                val = p.val;
1464 <                            else if ((val = mf.apply(k)) != null) {
1465 <                                added = true;
1466 <                                count = 2;
1467 <                                t.putTreeNode(h, k, val);
1468 <                            }
1469 <                        }
1470 <                    } finally {
1471 <                        t.release(0);
1472 <                    }
1473 <                    if (count != 0) {
1474 <                        if (!added)
1475 <                            return val;
1476 <                        break;
1477 <                    }
1478 <                }
1479 <                else
1480 <                    tab = (Node[])fk;
1481 <            }
1482 <            else if ((fh & HASH_BITS) == h && (fv = f.val) != null &&
1483 <                     ((fk = f.key) == k || k.equals(fk)))
1484 <                return fv;
1485 <            else {
1486 <                Node g = f.next;
1487 <                if (g != null) {
1488 <                    for (Node e = g;;) {
1489 <                        Object ek, ev;
1490 <                        if ((e.hash & HASH_BITS) == h && (ev = e.val) != null &&
1491 <                            ((ek = e.key) == k || k.equals(ek)))
1492 <                            return ev;
1493 <                        if ((e = e.next) == null) {
1494 <                            checkForResize();
1495 <                            break;
1496 <                        }
1497 <                    }
1498 <                }
1499 <                if (((fh = f.hash) & LOCKED) != 0) {
1500 <                    checkForResize();
1501 <                    f.tryAwaitLock(tab, i);
1502 <                }
1503 <                else if (tabAt(tab, i) == f && f.casHash(fh, fh | LOCKED)) {
1504 <                    boolean added = false;
1505 <                    try {
1506 <                        if (tabAt(tab, i) == f) {
1507 <                            count = 1;
1508 <                            for (Node e = f;; ++count) {
1509 <                                Object ek, ev;
1510 <                                if ((e.hash & HASH_BITS) == h &&
1511 <                                    (ev = e.val) != null &&
1512 <                                    ((ek = e.key) == k || k.equals(ek))) {
1513 <                                    val = ev;
1514 <                                    break;
1515 <                                }
1516 <                                Node last = e;
1517 <                                if ((e = e.next) == null) {
1518 <                                    if ((val = mf.apply(k)) != null) {
1519 <                                        added = true;
1520 <                                        last.next = new Node(h, k, val, null);
1521 <                                        if (count >= TREE_THRESHOLD)
1522 <                                            replaceWithTreeBin(tab, i, k);
1523 <                                    }
1524 <                                    break;
1525 <                                }
1526 <                            }
1527 <                        }
1528 <                    } finally {
1529 <                        if (!f.casHash(fh | LOCKED, fh)) {
1530 <                            f.hash = fh;
1531 <                            synchronized (f) { f.notifyAll(); };
1532 <                        }
1533 <                    }
1534 <                    if (count != 0) {
1535 <                        if (!added)
1536 <                            return val;
1537 <                        if (tab.length <= 64)
1538 <                            count = 2;
1539 <                        break;
1540 <                    }
1541 <                }
1542 <            }
1543 <        }
1544 <        if (val != null) {
1545 <            counter.add(1L);
1546 <            if (count > 1)
1547 <                checkForResize();
1548 <        }
1549 <        return val;
1550 <    }
714 >    /*
715 >     * Volatile access methods are used for table elements as well as
716 >     * elements of in-progress next table while resizing.  All uses of
717 >     * the tab arguments must be null checked by callers.  All callers
718 >     * also paranoically precheck that tab's length is not zero (or an
719 >     * equivalent check), thus ensuring that any index argument taking
720 >     * the form of a hash value anded with (length - 1) is a valid
721 >     * index.  Note that, to be correct wrt arbitrary concurrency
722 >     * errors by users, these checks must operate on local variables,
723 >     * which accounts for some odd-looking inline assignments below.
724 >     * Note that calls to setTabAt always occur within locked regions,
725 >     * and so do not need full volatile semantics, but still require
726 >     * ordering to maintain concurrent readability.
727 >     */
728  
1552    /** Implementation for compute */
729      @SuppressWarnings("unchecked")
730 <    private final Object internalCompute(K k, boolean onlyIfPresent,
731 <                                             BiFun<? super K, ? super V, ? extends V> mf) {
1556 <        int h = spread(k.hashCode());
1557 <        Object val = null;
1558 <        int delta = 0;
1559 <        int count = 0;
1560 <        for (Node[] tab = table;;) {
1561 <            Node f; int i, fh; Object fk;
1562 <            if (tab == null)
1563 <                tab = initTable();
1564 <            else if ((f = tabAt(tab, i = (tab.length - 1) & h)) == null) {
1565 <                if (onlyIfPresent)
1566 <                    break;
1567 <                Node node = new Node(fh = h | LOCKED, k, null, null);
1568 <                if (casTabAt(tab, i, null, node)) {
1569 <                    try {
1570 <                        count = 1;
1571 <                        if ((val = mf.apply(k, null)) != null) {
1572 <                            node.val = val;
1573 <                            delta = 1;
1574 <                        }
1575 <                    } finally {
1576 <                        if (delta == 0)
1577 <                            setTabAt(tab, i, null);
1578 <                        if (!node.casHash(fh, h)) {
1579 <                            node.hash = h;
1580 <                            synchronized (node) { node.notifyAll(); };
1581 <                        }
1582 <                    }
1583 <                }
1584 <                if (count != 0)
1585 <                    break;
1586 <            }
1587 <            else if ((fh = f.hash) == MOVED) {
1588 <                if ((fk = f.key) instanceof TreeBin) {
1589 <                    TreeBin t = (TreeBin)fk;
1590 <                    t.acquire(0);
1591 <                    try {
1592 <                        if (tabAt(tab, i) == f) {
1593 <                            count = 1;
1594 <                            TreeNode p = t.getTreeNode(h, k, t.root);
1595 <                            Object pv = (p == null) ? null : p.val;
1596 <                            if ((val = mf.apply(k, (V)pv)) != null) {
1597 <                                if (p != null)
1598 <                                    p.val = val;
1599 <                                else {
1600 <                                    count = 2;
1601 <                                    delta = 1;
1602 <                                    t.putTreeNode(h, k, val);
1603 <                                }
1604 <                            }
1605 <                            else if (p != null) {
1606 <                                delta = -1;
1607 <                                t.deleteTreeNode(p);
1608 <                            }
1609 <                        }
1610 <                    } finally {
1611 <                        t.release(0);
1612 <                    }
1613 <                    if (count != 0)
1614 <                        break;
1615 <                }
1616 <                else
1617 <                    tab = (Node[])fk;
1618 <            }
1619 <            else if ((fh & LOCKED) != 0) {
1620 <                checkForResize();
1621 <                f.tryAwaitLock(tab, i);
1622 <            }
1623 <            else if (f.casHash(fh, fh | LOCKED)) {
1624 <                try {
1625 <                    if (tabAt(tab, i) == f) {
1626 <                        count = 1;
1627 <                        for (Node e = f, pred = null;; ++count) {
1628 <                            Object ek, ev;
1629 <                            if ((e.hash & HASH_BITS) == h &&
1630 <                                (ev = e.val) != null &&
1631 <                                ((ek = e.key) == k || k.equals(ek))) {
1632 <                                val = mf.apply(k, (V)ev);
1633 <                                if (val != null)
1634 <                                    e.val = val;
1635 <                                else {
1636 <                                    delta = -1;
1637 <                                    Node en = e.next;
1638 <                                    if (pred != null)
1639 <                                        pred.next = en;
1640 <                                    else
1641 <                                        setTabAt(tab, i, en);
1642 <                                }
1643 <                                break;
1644 <                            }
1645 <                            pred = e;
1646 <                            if ((e = e.next) == null) {
1647 <                                if (!onlyIfPresent && (val = mf.apply(k, null)) != null) {
1648 <                                    pred.next = new Node(h, k, val, null);
1649 <                                    delta = 1;
1650 <                                    if (count >= TREE_THRESHOLD)
1651 <                                        replaceWithTreeBin(tab, i, k);
1652 <                                }
1653 <                                break;
1654 <                            }
1655 <                        }
1656 <                    }
1657 <                } finally {
1658 <                    if (!f.casHash(fh | LOCKED, fh)) {
1659 <                        f.hash = fh;
1660 <                        synchronized (f) { f.notifyAll(); };
1661 <                    }
1662 <                }
1663 <                if (count != 0) {
1664 <                    if (tab.length <= 64)
1665 <                        count = 2;
1666 <                    break;
1667 <                }
1668 <            }
1669 <        }
1670 <        if (delta != 0) {
1671 <            counter.add((long)delta);
1672 <            if (count > 1)
1673 <                checkForResize();
1674 <        }
1675 <        return val;
730 >    static final <K,V> Node<K,V> tabAt(Node<K,V>[] tab, int i) {
731 >        return (Node<K,V>)U.getObjectVolatile(tab, ((long)i << ASHIFT) + ABASE);
732      }
733  
734 <    /** Implementation for merge */
735 <    @SuppressWarnings("unchecked")
736 <    private final Object internalMerge(K k, V v,
1681 <                                       BiFun<? super V, ? super V, ? extends V> mf) {
1682 <        int h = spread(k.hashCode());
1683 <        Object val = null;
1684 <        int delta = 0;
1685 <        int count = 0;
1686 <        for (Node[] tab = table;;) {
1687 <            int i; Node f; int fh; Object fk, fv;
1688 <            if (tab == null)
1689 <                tab = initTable();
1690 <            else if ((f = tabAt(tab, i = (tab.length - 1) & h)) == null) {
1691 <                if (casTabAt(tab, i, null, new Node(h, k, v, null))) {
1692 <                    delta = 1;
1693 <                    val = v;
1694 <                    break;
1695 <                }
1696 <            }
1697 <            else if ((fh = f.hash) == MOVED) {
1698 <                if ((fk = f.key) instanceof TreeBin) {
1699 <                    TreeBin t = (TreeBin)fk;
1700 <                    t.acquire(0);
1701 <                    try {
1702 <                        if (tabAt(tab, i) == f) {
1703 <                            count = 1;
1704 <                            TreeNode p = t.getTreeNode(h, k, t.root);
1705 <                            val = (p == null) ? v : mf.apply((V)p.val, v);
1706 <                            if (val != null) {
1707 <                                if (p != null)
1708 <                                    p.val = val;
1709 <                                else {
1710 <                                    count = 2;
1711 <                                    delta = 1;
1712 <                                    t.putTreeNode(h, k, val);
1713 <                                }
1714 <                            }
1715 <                            else if (p != null) {
1716 <                                delta = -1;
1717 <                                t.deleteTreeNode(p);
1718 <                            }
1719 <                        }
1720 <                    } finally {
1721 <                        t.release(0);
1722 <                    }
1723 <                    if (count != 0)
1724 <                        break;
1725 <                }
1726 <                else
1727 <                    tab = (Node[])fk;
1728 <            }
1729 <            else if ((fh & LOCKED) != 0) {
1730 <                checkForResize();
1731 <                f.tryAwaitLock(tab, i);
1732 <            }
1733 <            else if (f.casHash(fh, fh | LOCKED)) {
1734 <                try {
1735 <                    if (tabAt(tab, i) == f) {
1736 <                        count = 1;
1737 <                        for (Node e = f, pred = null;; ++count) {
1738 <                            Object ek, ev;
1739 <                            if ((e.hash & HASH_BITS) == h &&
1740 <                                (ev = e.val) != null &&
1741 <                                ((ek = e.key) == k || k.equals(ek))) {
1742 <                                val = mf.apply(v, (V)ev);
1743 <                                if (val != null)
1744 <                                    e.val = val;
1745 <                                else {
1746 <                                    delta = -1;
1747 <                                    Node en = e.next;
1748 <                                    if (pred != null)
1749 <                                        pred.next = en;
1750 <                                    else
1751 <                                        setTabAt(tab, i, en);
1752 <                                }
1753 <                                break;
1754 <                            }
1755 <                            pred = e;
1756 <                            if ((e = e.next) == null) {
1757 <                                val = v;
1758 <                                pred.next = new Node(h, k, val, null);
1759 <                                delta = 1;
1760 <                                if (count >= TREE_THRESHOLD)
1761 <                                    replaceWithTreeBin(tab, i, k);
1762 <                                break;
1763 <                            }
1764 <                        }
1765 <                    }
1766 <                } finally {
1767 <                    if (!f.casHash(fh | LOCKED, fh)) {
1768 <                        f.hash = fh;
1769 <                        synchronized (f) { f.notifyAll(); };
1770 <                    }
1771 <                }
1772 <                if (count != 0) {
1773 <                    if (tab.length <= 64)
1774 <                        count = 2;
1775 <                    break;
1776 <                }
1777 <            }
1778 <        }
1779 <        if (delta != 0) {
1780 <            counter.add((long)delta);
1781 <            if (count > 1)
1782 <                checkForResize();
1783 <        }
1784 <        return val;
734 >    static final <K,V> boolean casTabAt(Node<K,V>[] tab, int i,
735 >                                        Node<K,V> c, Node<K,V> v) {
736 >        return U.compareAndSwapObject(tab, ((long)i << ASHIFT) + ABASE, c, v);
737      }
738 <
739 <    /** Implementation for putAll */
740 <    private final void internalPutAll(Map<?, ?> m) {
1789 <        tryPresize(m.size());
1790 <        long delta = 0L;     // number of uncommitted additions
1791 <        boolean npe = false; // to throw exception on exit for nulls
1792 <        try {                // to clean up counts on other exceptions
1793 <            for (Map.Entry<?, ?> entry : m.entrySet()) {
1794 <                Object k, v;
1795 <                if (entry == null || (k = entry.getKey()) == null ||
1796 <                    (v = entry.getValue()) == null) {
1797 <                    npe = true;
1798 <                    break;
1799 <                }
1800 <                int h = spread(k.hashCode());
1801 <                for (Node[] tab = table;;) {
1802 <                    int i; Node f; int fh; Object fk;
1803 <                    if (tab == null)
1804 <                        tab = initTable();
1805 <                    else if ((f = tabAt(tab, i = (tab.length - 1) & h)) == null){
1806 <                        if (casTabAt(tab, i, null, new Node(h, k, v, null))) {
1807 <                            ++delta;
1808 <                            break;
1809 <                        }
1810 <                    }
1811 <                    else if ((fh = f.hash) == MOVED) {
1812 <                        if ((fk = f.key) instanceof TreeBin) {
1813 <                            TreeBin t = (TreeBin)fk;
1814 <                            boolean validated = false;
1815 <                            t.acquire(0);
1816 <                            try {
1817 <                                if (tabAt(tab, i) == f) {
1818 <                                    validated = true;
1819 <                                    TreeNode p = t.getTreeNode(h, k, t.root);
1820 <                                    if (p != null)
1821 <                                        p.val = v;
1822 <                                    else {
1823 <                                        t.putTreeNode(h, k, v);
1824 <                                        ++delta;
1825 <                                    }
1826 <                                }
1827 <                            } finally {
1828 <                                t.release(0);
1829 <                            }
1830 <                            if (validated)
1831 <                                break;
1832 <                        }
1833 <                        else
1834 <                            tab = (Node[])fk;
1835 <                    }
1836 <                    else if ((fh & LOCKED) != 0) {
1837 <                        counter.add(delta);
1838 <                        delta = 0L;
1839 <                        checkForResize();
1840 <                        f.tryAwaitLock(tab, i);
1841 <                    }
1842 <                    else if (f.casHash(fh, fh | LOCKED)) {
1843 <                        int count = 0;
1844 <                        try {
1845 <                            if (tabAt(tab, i) == f) {
1846 <                                count = 1;
1847 <                                for (Node e = f;; ++count) {
1848 <                                    Object ek, ev;
1849 <                                    if ((e.hash & HASH_BITS) == h &&
1850 <                                        (ev = e.val) != null &&
1851 <                                        ((ek = e.key) == k || k.equals(ek))) {
1852 <                                        e.val = v;
1853 <                                        break;
1854 <                                    }
1855 <                                    Node last = e;
1856 <                                    if ((e = e.next) == null) {
1857 <                                        ++delta;
1858 <                                        last.next = new Node(h, k, v, null);
1859 <                                        if (count >= TREE_THRESHOLD)
1860 <                                            replaceWithTreeBin(tab, i, k);
1861 <                                        break;
1862 <                                    }
1863 <                                }
1864 <                            }
1865 <                        } finally {
1866 <                            if (!f.casHash(fh | LOCKED, fh)) {
1867 <                                f.hash = fh;
1868 <                                synchronized (f) { f.notifyAll(); };
1869 <                            }
1870 <                        }
1871 <                        if (count != 0) {
1872 <                            if (count > 1) {
1873 <                                counter.add(delta);
1874 <                                delta = 0L;
1875 <                                checkForResize();
1876 <                            }
1877 <                            break;
1878 <                        }
1879 <                    }
1880 <                }
1881 <            }
1882 <        } finally {
1883 <            if (delta != 0)
1884 <                counter.add(delta);
1885 <        }
1886 <        if (npe)
1887 <            throw new NullPointerException();
738 >    
739 >    static final <K,V> void setTabAt(Node<K,V>[] tab, int i, Node<K,V> v) {
740 >        U.putOrderedObject(tab, ((long)i << ASHIFT) + ABASE, v);
741      }
742 <
743 <    /* ---------------- Table Initialization and Resizing -------------- */
742 >    
743 >    /* ---------------- Fields -------------- */
744  
745      /**
746 <     * Returns a power of two table size for the given desired capacity.
747 <     * See Hackers Delight, sec 3.2
746 >     * The array of bins. Lazily initialized upon first insertion.
747 >     * Size is always a power of two. Accessed directly by iterators.
748       */
749 <    private static final int tableSizeFor(int c) {
1897 <        int n = c - 1;
1898 <        n |= n >>> 1;
1899 <        n |= n >>> 2;
1900 <        n |= n >>> 4;
1901 <        n |= n >>> 8;
1902 <        n |= n >>> 16;
1903 <        return (n < 0) ? 1 : (n >= MAXIMUM_CAPACITY) ? MAXIMUM_CAPACITY : n + 1;
1904 <    }
749 >    transient volatile Node<K,V>[] table;
750  
751      /**
752 <     * Initializes table, using the size recorded in sizeCtl.
752 >     * The next table to use; non-null only while resizing.
753       */
754 <    private final Node[] initTable() {
1910 <        Node[] tab; int sc;
1911 <        while ((tab = table) == null) {
1912 <            if ((sc = sizeCtl) < 0)
1913 <                Thread.yield(); // lost initialization race; just spin
1914 <            else if (UNSAFE.compareAndSwapInt(this, sizeCtlOffset, sc, -1)) {
1915 <                try {
1916 <                    if ((tab = table) == null) {
1917 <                        int n = (sc > 0) ? sc : DEFAULT_CAPACITY;
1918 <                        tab = table = new Node[n];
1919 <                        sc = n - (n >>> 2);
1920 <                    }
1921 <                } finally {
1922 <                    sizeCtl = sc;
1923 <                }
1924 <                break;
1925 <            }
1926 <        }
1927 <        return tab;
1928 <    }
1929 <
1930 <    /**
1931 <     * If table is too small and not already resizing, creates next
1932 <     * table and transfers bins.  Rechecks occupancy after a transfer
1933 <     * to see if another resize is already needed because resizings
1934 <     * are lagging additions.
1935 <     */
1936 <    private final void checkForResize() {
1937 <        Node[] tab; int n, sc;
1938 <        while ((tab = table) != null &&
1939 <               (n = tab.length) < MAXIMUM_CAPACITY &&
1940 <               (sc = sizeCtl) >= 0 && counter.sum() >= (long)sc &&
1941 <               UNSAFE.compareAndSwapInt(this, sizeCtlOffset, sc, -1)) {
1942 <            try {
1943 <                if (tab == table) {
1944 <                    table = rebuild(tab);
1945 <                    sc = (n << 1) - (n >>> 1);
1946 <                }
1947 <            } finally {
1948 <                sizeCtl = sc;
1949 <            }
1950 <        }
1951 <    }
754 >    private transient volatile Node<K,V>[] nextTable;
755  
756      /**
757 <     * Tries to presize table to accommodate the given number of elements.
758 <     *
759 <     * @param size number of elements (doesn't need to be perfectly accurate)
757 >     * Base counter value, used mainly when there is no contention,
758 >     * but also as a fallback during table initialization
759 >     * races. Updated via CAS.
760       */
761 <    private final void tryPresize(int size) {
1959 <        int c = (size >= (MAXIMUM_CAPACITY >>> 1)) ? MAXIMUM_CAPACITY :
1960 <            tableSizeFor(size + (size >>> 1) + 1);
1961 <        int sc;
1962 <        while ((sc = sizeCtl) >= 0) {
1963 <            Node[] tab = table; int n;
1964 <            if (tab == null || (n = tab.length) == 0) {
1965 <                n = (sc > c) ? sc : c;
1966 <                if (UNSAFE.compareAndSwapInt(this, sizeCtlOffset, sc, -1)) {
1967 <                    try {
1968 <                        if (table == tab) {
1969 <                            table = new Node[n];
1970 <                            sc = n - (n >>> 2);
1971 <                        }
1972 <                    } finally {
1973 <                        sizeCtl = sc;
1974 <                    }
1975 <                }
1976 <            }
1977 <            else if (c <= sc || n >= MAXIMUM_CAPACITY)
1978 <                break;
1979 <            else if (UNSAFE.compareAndSwapInt(this, sizeCtlOffset, sc, -1)) {
1980 <                try {
1981 <                    if (table == tab) {
1982 <                        table = rebuild(tab);
1983 <                        sc = (n << 1) - (n >>> 1);
1984 <                    }
1985 <                } finally {
1986 <                    sizeCtl = sc;
1987 <                }
1988 <            }
1989 <        }
1990 <    }
761 >    private transient volatile long baseCount;
762  
763 <    /*
764 <     * Moves and/or copies the nodes in each bin to new table. See
765 <     * above for explanation.
766 <     *
767 <     * @return the new table
763 >    /**
764 >     * Table initialization and resizing control.  When negative, the
765 >     * table is being initialized or resized: -1 for initialization,
766 >     * else -(1 + the number of active resizing threads).  Otherwise,
767 >     * when table is null, holds the initial table size to use upon
768 >     * creation, or 0 for default. After initialization, holds the
769 >     * next element count value upon which to resize the table.
770       */
771 <    private static final Node[] rebuild(Node[] tab) {
1999 <        int n = tab.length;
2000 <        Node[] nextTab = new Node[n << 1];
2001 <        Node fwd = new Node(MOVED, nextTab, null, null);
2002 <        int[] buffer = null;       // holds bins to revisit; null until needed
2003 <        Node rev = null;           // reverse forwarder; null until needed
2004 <        int nbuffered = 0;         // the number of bins in buffer list
2005 <        int bufferIndex = 0;       // buffer index of current buffered bin
2006 <        int bin = n - 1;           // current non-buffered bin or -1 if none
2007 <
2008 <        for (int i = bin;;) {      // start upwards sweep
2009 <            int fh; Node f;
2010 <            if ((f = tabAt(tab, i)) == null) {
2011 <                if (bin >= 0) {    // no lock needed (or available)
2012 <                    if (!casTabAt(tab, i, f, fwd))
2013 <                        continue;
2014 <                }
2015 <                else {             // transiently use a locked forwarding node
2016 <                    Node g = new Node(MOVED|LOCKED, nextTab, null, null);
2017 <                    if (!casTabAt(tab, i, f, g))
2018 <                        continue;
2019 <                    setTabAt(nextTab, i, null);
2020 <                    setTabAt(nextTab, i + n, null);
2021 <                    setTabAt(tab, i, fwd);
2022 <                    if (!g.casHash(MOVED|LOCKED, MOVED)) {
2023 <                        g.hash = MOVED;
2024 <                        synchronized (g) { g.notifyAll(); }
2025 <                    }
2026 <                }
2027 <            }
2028 <            else if ((fh = f.hash) == MOVED) {
2029 <                Object fk = f.key;
2030 <                if (fk instanceof TreeBin) {
2031 <                    TreeBin t = (TreeBin)fk;
2032 <                    boolean validated = false;
2033 <                    t.acquire(0);
2034 <                    try {
2035 <                        if (tabAt(tab, i) == f) {
2036 <                            validated = true;
2037 <                            splitTreeBin(nextTab, i, t);
2038 <                            setTabAt(tab, i, fwd);
2039 <                        }
2040 <                    } finally {
2041 <                        t.release(0);
2042 <                    }
2043 <                    if (!validated)
2044 <                        continue;
2045 <                }
2046 <            }
2047 <            else if ((fh & LOCKED) == 0 && f.casHash(fh, fh|LOCKED)) {
2048 <                boolean validated = false;
2049 <                try {              // split to lo and hi lists; copying as needed
2050 <                    if (tabAt(tab, i) == f) {
2051 <                        validated = true;
2052 <                        splitBin(nextTab, i, f);
2053 <                        setTabAt(tab, i, fwd);
2054 <                    }
2055 <                } finally {
2056 <                    if (!f.casHash(fh | LOCKED, fh)) {
2057 <                        f.hash = fh;
2058 <                        synchronized (f) { f.notifyAll(); };
2059 <                    }
2060 <                }
2061 <                if (!validated)
2062 <                    continue;
2063 <            }
2064 <            else {
2065 <                if (buffer == null) // initialize buffer for revisits
2066 <                    buffer = new int[TRANSFER_BUFFER_SIZE];
2067 <                if (bin < 0 && bufferIndex > 0) {
2068 <                    int j = buffer[--bufferIndex];
2069 <                    buffer[bufferIndex] = i;
2070 <                    i = j;         // swap with another bin
2071 <                    continue;
2072 <                }
2073 <                if (bin < 0 || nbuffered >= TRANSFER_BUFFER_SIZE) {
2074 <                    f.tryAwaitLock(tab, i);
2075 <                    continue;      // no other options -- block
2076 <                }
2077 <                if (rev == null)   // initialize reverse-forwarder
2078 <                    rev = new Node(MOVED, tab, null, null);
2079 <                if (tabAt(tab, i) != f || (f.hash & LOCKED) == 0)
2080 <                    continue;      // recheck before adding to list
2081 <                buffer[nbuffered++] = i;
2082 <                setTabAt(nextTab, i, rev);     // install place-holders
2083 <                setTabAt(nextTab, i + n, rev);
2084 <            }
2085 <
2086 <            if (bin > 0)
2087 <                i = --bin;
2088 <            else if (buffer != null && nbuffered > 0) {
2089 <                bin = -1;
2090 <                i = buffer[bufferIndex = --nbuffered];
2091 <            }
2092 <            else
2093 <                return nextTab;
2094 <        }
2095 <    }
771 >    private transient volatile int sizeCtl;
772  
773      /**
774 <     * Splits a normal bin with list headed by e into lo and hi parts;
2099 <     * installs in given table.
774 >     * The next table index (plus one) to split while resizing.
775       */
776 <    private static void splitBin(Node[] nextTab, int i, Node e) {
2102 <        int bit = nextTab.length >>> 1; // bit to split on
2103 <        int runBit = e.hash & bit;
2104 <        Node lastRun = e, lo = null, hi = null;
2105 <        for (Node p = e.next; p != null; p = p.next) {
2106 <            int b = p.hash & bit;
2107 <            if (b != runBit) {
2108 <                runBit = b;
2109 <                lastRun = p;
2110 <            }
2111 <        }
2112 <        if (runBit == 0)
2113 <            lo = lastRun;
2114 <        else
2115 <            hi = lastRun;
2116 <        for (Node p = e; p != lastRun; p = p.next) {
2117 <            int ph = p.hash & HASH_BITS;
2118 <            Object pk = p.key, pv = p.val;
2119 <            if ((ph & bit) == 0)
2120 <                lo = new Node(ph, pk, pv, lo);
2121 <            else
2122 <                hi = new Node(ph, pk, pv, hi);
2123 <        }
2124 <        setTabAt(nextTab, i, lo);
2125 <        setTabAt(nextTab, i + bit, hi);
2126 <    }
776 >    private transient volatile int transferIndex;
777  
778      /**
779 <     * Splits a tree bin into lo and hi parts; installs in given table.
779 >     * The least available table index to split while resizing.
780       */
781 <    private static void splitTreeBin(Node[] nextTab, int i, TreeBin t) {
2132 <        int bit = nextTab.length >>> 1;
2133 <        TreeBin lt = new TreeBin();
2134 <        TreeBin ht = new TreeBin();
2135 <        int lc = 0, hc = 0;
2136 <        for (Node e = t.first; e != null; e = e.next) {
2137 <            int h = e.hash & HASH_BITS;
2138 <            Object k = e.key, v = e.val;
2139 <            if ((h & bit) == 0) {
2140 <                ++lc;
2141 <                lt.putTreeNode(h, k, v);
2142 <            }
2143 <            else {
2144 <                ++hc;
2145 <                ht.putTreeNode(h, k, v);
2146 <            }
2147 <        }
2148 <        Node ln, hn; // throw away trees if too small
2149 <        if (lc <= (TREE_THRESHOLD >>> 1)) {
2150 <            ln = null;
2151 <            for (Node p = lt.first; p != null; p = p.next)
2152 <                ln = new Node(p.hash, p.key, p.val, ln);
2153 <        }
2154 <        else
2155 <            ln = new Node(MOVED, lt, null, null);
2156 <        setTabAt(nextTab, i, ln);
2157 <        if (hc <= (TREE_THRESHOLD >>> 1)) {
2158 <            hn = null;
2159 <            for (Node p = ht.first; p != null; p = p.next)
2160 <                hn = new Node(p.hash, p.key, p.val, hn);
2161 <        }
2162 <        else
2163 <            hn = new Node(MOVED, ht, null, null);
2164 <        setTabAt(nextTab, i + bit, hn);
2165 <    }
781 >    private transient volatile int transferOrigin;
782  
783      /**
784 <     * Implementation for clear. Steps through each bin, removing all
2169 <     * nodes.
784 >     * Spinlock (locked via CAS) used when resizing and/or creating CounterCells.
785       */
786 <    private final void internalClear() {
2172 <        long delta = 0L; // negative number of deletions
2173 <        int i = 0;
2174 <        Node[] tab = table;
2175 <        while (tab != null && i < tab.length) {
2176 <            int fh; Object fk;
2177 <            Node f = tabAt(tab, i);
2178 <            if (f == null)
2179 <                ++i;
2180 <            else if ((fh = f.hash) == MOVED) {
2181 <                if ((fk = f.key) instanceof TreeBin) {
2182 <                    TreeBin t = (TreeBin)fk;
2183 <                    t.acquire(0);
2184 <                    try {
2185 <                        if (tabAt(tab, i) == f) {
2186 <                            for (Node p = t.first; p != null; p = p.next) {
2187 <                                p.val = null;
2188 <                                --delta;
2189 <                            }
2190 <                            t.first = null;
2191 <                            t.root = null;
2192 <                            ++i;
2193 <                        }
2194 <                    } finally {
2195 <                        t.release(0);
2196 <                    }
2197 <                }
2198 <                else
2199 <                    tab = (Node[])fk;
2200 <            }
2201 <            else if ((fh & LOCKED) != 0) {
2202 <                counter.add(delta); // opportunistically update count
2203 <                delta = 0L;
2204 <                f.tryAwaitLock(tab, i);
2205 <            }
2206 <            else if (f.casHash(fh, fh | LOCKED)) {
2207 <                try {
2208 <                    if (tabAt(tab, i) == f) {
2209 <                        for (Node e = f; e != null; e = e.next) {
2210 <                            e.val = null;
2211 <                            --delta;
2212 <                        }
2213 <                        setTabAt(tab, i, null);
2214 <                        ++i;
2215 <                    }
2216 <                } finally {
2217 <                    if (!f.casHash(fh | LOCKED, fh)) {
2218 <                        f.hash = fh;
2219 <                        synchronized (f) { f.notifyAll(); };
2220 <                    }
2221 <                }
2222 <            }
2223 <        }
2224 <        if (delta != 0)
2225 <            counter.add(delta);
2226 <    }
2227 <
2228 <    /* ----------------Table Traversal -------------- */
786 >    private transient volatile int cellsBusy;
787  
788      /**
789 <     * Encapsulates traversal for methods such as containsValue; also
2232 <     * serves as a base class for other iterators and bulk tasks.
2233 <     *
2234 <     * At each step, the iterator snapshots the key ("nextKey") and
2235 <     * value ("nextVal") of a valid node (i.e., one that, at point of
2236 <     * snapshot, has a non-null user value). Because val fields can
2237 <     * change (including to null, indicating deletion), field nextVal
2238 <     * might not be accurate at point of use, but still maintains the
2239 <     * weak consistency property of holding a value that was once
2240 <     * valid.
2241 <     *
2242 <     * Internal traversals directly access these fields, as in:
2243 <     * {@code while (it.advance() != null) { process(it.nextKey); }}
2244 <     *
2245 <     * Exported iterators must track whether the iterator has advanced
2246 <     * (in hasNext vs next) (by setting/checking/nulling field
2247 <     * nextVal), and then extract key, value, or key-value pairs as
2248 <     * return values of next().
2249 <     *
2250 <     * The iterator visits once each still-valid node that was
2251 <     * reachable upon iterator construction. It might miss some that
2252 <     * were added to a bin after the bin was visited, which is OK wrt
2253 <     * consistency guarantees. Maintaining this property in the face
2254 <     * of possible ongoing resizes requires a fair amount of
2255 <     * bookkeeping state that is difficult to optimize away amidst
2256 <     * volatile accesses.  Even so, traversal maintains reasonable
2257 <     * throughput.
2258 <     *
2259 <     * Normally, iteration proceeds bin-by-bin traversing lists.
2260 <     * However, if the table has been resized, then all future steps
2261 <     * must traverse both the bin at the current index as well as at
2262 <     * (index + baseSize); and so on for further resizings. To
2263 <     * paranoically cope with potential sharing by users of iterators
2264 <     * across threads, iteration terminates if a bounds checks fails
2265 <     * for a table read.
2266 <     *
2267 <     * This class extends ForkJoinTask to streamline parallel
2268 <     * iteration in bulk operations (see BulkTask). This adds only an
2269 <     * int of space overhead, which is close enough to negligible in
2270 <     * cases where it is not needed to not worry about it.  Because
2271 <     * ForkJoinTask is Serializable, but iterators need not be, we
2272 <     * need to add warning suppressions.
789 >     * Table of counter cells. When non-null, size is a power of 2.
790       */
791 <    @SuppressWarnings("serial")
2275 <    static class Traverser<K,V,R> extends ForkJoinTask<R> {
2276 <        final ConcurrentHashMapV8<K, V> map;
2277 <        Node next;           // the next entry to use
2278 <        Node last;           // the last entry used
2279 <        Object nextKey;      // cached key field of next
2280 <        Object nextVal;      // cached val field of next
2281 <        Node[] tab;          // current table; updated if resized
2282 <        int index;           // index of bin to use next
2283 <        int baseIndex;       // current index of initial table
2284 <        int baseLimit;       // index bound for initial table
2285 <        final int baseSize;  // initial table size
2286 <
2287 <        /** Creates iterator for all entries in the table. */
2288 <        Traverser(ConcurrentHashMapV8<K, V> map) {
2289 <            this.tab = (this.map = map).table;
2290 <            baseLimit = baseSize = (tab == null) ? 0 : tab.length;
2291 <        }
2292 <
2293 <        /** Creates iterator for split() methods */
2294 <        Traverser(Traverser<K,V,?> it, boolean split) {
2295 <            this.map = it.map;
2296 <            this.tab = it.tab;
2297 <            this.baseSize = it.baseSize;
2298 <            int lo = it.baseIndex;
2299 <            int hi = this.baseLimit = it.baseLimit;
2300 <            int i;
2301 <            if (split) // adjust parent
2302 <                i = it.baseLimit = (lo + hi + 1) >>> 1;
2303 <            else       // clone parent
2304 <                i = lo;
2305 <            this.index = this.baseIndex = i;
2306 <        }
2307 <
2308 <        /**
2309 <         * Advances next; returns nextVal or null if terminated.
2310 <         * See above for explanation.
2311 <         */
2312 <        final Object advance() {
2313 <            Node e = last = next;
2314 <            Object ev = null;
2315 <            outer: do {
2316 <                if (e != null)                  // advance past used/skipped node
2317 <                    e = e.next;
2318 <                while (e == null) {             // get to next non-null bin
2319 <                    Node[] t; int b, i, n; Object ek; // checks must use locals
2320 <                    if ((b = baseIndex) >= baseLimit || (i = index) < 0 ||
2321 <                        (t = tab) == null || i >= (n = t.length))
2322 <                        break outer;
2323 <                    else if ((e = tabAt(t, i)) != null && e.hash == MOVED) {
2324 <                        if ((ek = e.key) instanceof TreeBin)
2325 <                            e = ((TreeBin)ek).first;
2326 <                        else {
2327 <                            tab = (Node[])ek;
2328 <                            continue;           // restarts due to null val
2329 <                        }
2330 <                    }                           // visit upper slots if present
2331 <                    index = (i += baseSize) < n ? i : (baseIndex = b + 1);
2332 <                }
2333 <                nextKey = e.key;
2334 <            } while ((ev = e.val) == null);    // skip deleted or special nodes
2335 <            next = e;
2336 <            return nextVal = ev;
2337 <        }
791 >    private transient volatile CounterCell[] counterCells;
792  
793 <        public final void remove() {
794 <            if (nextVal == null && last == null)
795 <                advance();
796 <            Node e = last;
2343 <            if (e == null)
2344 <                throw new IllegalStateException();
2345 <            last = null;
2346 <            map.remove(e.key);
2347 <        }
2348 <
2349 <        public final boolean hasNext() {
2350 <            return nextVal != null || advance() != null;
2351 <        }
793 >    // views
794 >    private transient KeySetView<K,V> keySet;
795 >    private transient ValuesView<K,V> values;
796 >    private transient EntrySetView<K,V> entrySet;
797  
2353        public final boolean hasMoreElements() { return hasNext(); }
2354        public final void setRawResult(Object x) { }
2355        public R getRawResult() { return null; }
2356        public boolean exec() { return true; }
2357    }
798  
799      /* ---------------- Public operations -------------- */
800  
# Line 2362 | Line 802 | public class ConcurrentHashMapV8<K, V>
802       * Creates a new, empty map with the default initial table size (16).
803       */
804      public ConcurrentHashMapV8() {
2365        this.counter = new LongAdder();
805      }
806  
807      /**
# Line 2381 | Line 820 | public class ConcurrentHashMapV8<K, V>
820          int cap = ((initialCapacity >= (MAXIMUM_CAPACITY >>> 1)) ?
821                     MAXIMUM_CAPACITY :
822                     tableSizeFor(initialCapacity + (initialCapacity >>> 1) + 1));
2384        this.counter = new LongAdder();
823          this.sizeCtl = cap;
824      }
825  
# Line 2391 | Line 829 | public class ConcurrentHashMapV8<K, V>
829       * @param m the map
830       */
831      public ConcurrentHashMapV8(Map<? extends K, ? extends V> m) {
2394        this.counter = new LongAdder();
832          this.sizeCtl = DEFAULT_CAPACITY;
833 <        internalPutAll(m);
833 >        putAll(m);
834      }
835  
836      /**
# Line 2434 | Line 871 | public class ConcurrentHashMapV8<K, V>
871       * nonpositive
872       */
873      public ConcurrentHashMapV8(int initialCapacity,
874 <                               float loadFactor, int concurrencyLevel) {
874 >                             float loadFactor, int concurrencyLevel) {
875          if (!(loadFactor > 0.0f) || initialCapacity < 0 || concurrencyLevel <= 0)
876              throw new IllegalArgumentException();
877          if (initialCapacity < concurrencyLevel)   // Use at least as many bins
# Line 2442 | Line 879 | public class ConcurrentHashMapV8<K, V>
879          long size = (long)(1.0 + (long)initialCapacity / loadFactor);
880          int cap = (size >= (long)MAXIMUM_CAPACITY) ?
881              MAXIMUM_CAPACITY : tableSizeFor((int)size);
2445        this.counter = new LongAdder();
882          this.sizeCtl = cap;
883      }
884  
885 <    /**
2450 <     * {@inheritDoc}
2451 <     */
2452 <    public boolean isEmpty() {
2453 <        return counter.sum() <= 0L; // ignore transient negative values
2454 <    }
885 >    // Original (since JDK1.2) Map methods
886  
887      /**
888       * {@inheritDoc}
889       */
890      public int size() {
891 <        long n = counter.sum();
891 >        long n = sumCount();
892          return ((n < 0L) ? 0 :
893                  (n > (long)Integer.MAX_VALUE) ? Integer.MAX_VALUE :
894                  (int)n);
895      }
896  
897      /**
898 <     * Returns the number of mappings. This method should be used
2468 <     * instead of {@link #size} because a ConcurrentHashMap may
2469 <     * contain more mappings than can be represented as an int. The
2470 <     * value returned is a snapshot; the actual count may differ if
2471 <     * there are ongoing concurrent insertions or removals.
2472 <     *
2473 <     * @return the number of mappings
898 >     * {@inheritDoc}
899       */
900 <    public long mappingCount() {
901 <        long n = counter.sum();
2477 <        return (n < 0L) ? 0L : n; // ignore transient negative values
900 >    public boolean isEmpty() {
901 >        return sumCount() <= 0L; // ignore transient negative values
902      }
903  
904      /**
# Line 2488 | Line 912 | public class ConcurrentHashMapV8<K, V>
912       *
913       * @throws NullPointerException if the specified key is null
914       */
2491    @SuppressWarnings("unchecked")
915      public V get(Object key) {
916 <        if (key == null)
917 <            throw new NullPointerException();
918 <        return (V)internalGet(key);
916 >        Node<K,V>[] tab; Node<K,V> e, p; int n, eh; K ek;
917 >        int h = spread(key.hashCode());
918 >        if ((tab = table) != null && (n = tab.length) > 0 &&
919 >            (e = tabAt(tab, (n - 1) & h)) != null) {
920 >            if ((eh = e.hash) == h) {
921 >                if ((ek = e.key) == key || (ek != null && key.equals(ek)))
922 >                    return e.val;
923 >            }
924 >            else if (eh < 0)
925 >                return (p = e.find(h, key)) != null ? p.val : null;
926 >            while ((e = e.next) != null) {
927 >                if (e.hash == h &&
928 >                    ((ek = e.key) == key || (ek != null && key.equals(ek))))
929 >                    return e.val;
930 >            }
931 >        }
932 >        return null;
933      }
934  
935      /**
936       * Tests if the specified object is a key in this table.
937       *
938 <     * @param  key   possible key
938 >     * @param  key possible key
939       * @return {@code true} if and only if the specified object
940       *         is a key in this table, as determined by the
941       *         {@code equals} method; {@code false} otherwise
942       * @throws NullPointerException if the specified key is null
943       */
944      public boolean containsKey(Object key) {
945 <        if (key == null)
2509 <            throw new NullPointerException();
2510 <        return internalGet(key) != null;
945 >        return get(key) != null;
946      }
947  
948      /**
# Line 2523 | Line 958 | public class ConcurrentHashMapV8<K, V>
958      public boolean containsValue(Object value) {
959          if (value == null)
960              throw new NullPointerException();
961 <        Object v;
962 <        Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
963 <        while ((v = it.advance()) != null) {
964 <            if (v == value || value.equals(v))
965 <                return true;
961 >        Node<K,V>[] t;
962 >        if ((t = table) != null) {
963 >            Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length);
964 >            for (Node<K,V> p; (p = it.advance()) != null; ) {
965 >                V v;
966 >                if ((v = p.val) == value || (v != null && value.equals(v)))
967 >                    return true;
968 >            }
969          }
970          return false;
971      }
972  
973      /**
2536     * Legacy method testing if some key maps into the specified value
2537     * in this table.  This method is identical in functionality to
2538     * {@link #containsValue}, and exists solely to ensure
2539     * full compatibility with class {@link java.util.Hashtable},
2540     * which supported this method prior to introduction of the
2541     * Java Collections framework.
2542     *
2543     * @param  value a value to search for
2544     * @return {@code true} if and only if some key maps to the
2545     *         {@code value} argument in this table as
2546     *         determined by the {@code equals} method;
2547     *         {@code false} otherwise
2548     * @throws NullPointerException if the specified value is null
2549     */
2550    public boolean contains(Object value) {
2551        return containsValue(value);
2552    }
2553
2554    /**
974       * Maps the specified key to the specified value in this table.
975       * Neither the key nor the value can be null.
976       *
977 <     * <p> The value can be retrieved by calling the {@code get} method
977 >     * <p>The value can be retrieved by calling the {@code get} method
978       * with a key that is equal to the original key.
979       *
980       * @param key key with which the specified value is to be associated
# Line 2564 | Line 983 | public class ConcurrentHashMapV8<K, V>
983       *         {@code null} if there was no mapping for {@code key}
984       * @throws NullPointerException if the specified key or value is null
985       */
2567    @SuppressWarnings("unchecked")
986      public V put(K key, V value) {
987 <        if (key == null || value == null)
2570 <            throw new NullPointerException();
2571 <        return (V)internalPut(key, value);
987 >        return putVal(key, value, false);
988      }
989  
990 <    /**
991 <     * {@inheritDoc}
992 <     *
993 <     * @return the previous value associated with the specified key,
994 <     *         or {@code null} if there was no mapping for the key
995 <     * @throws NullPointerException if the specified key or value is null
996 <     */
997 <    @SuppressWarnings("unchecked")
998 <    public V putIfAbsent(K key, V value) {
999 <        if (key == null || value == null)
1000 <            throw new NullPointerException();
1001 <        return (V)internalPutIfAbsent(key, value);
990 >    /** Implementation for put and putIfAbsent */
991 >    final V putVal(K key, V value, boolean onlyIfAbsent) {
992 >        if (key == null || value == null) throw new NullPointerException();
993 >        int hash = spread(key.hashCode());
994 >        int binCount = 0;
995 >        for (Node<K,V>[] tab = table;;) {
996 >            Node<K,V> f; int n, i, fh;
997 >            if (tab == null || (n = tab.length) == 0)
998 >                tab = initTable();
999 >            else if ((f = tabAt(tab, i = (n - 1) & hash)) == null) {
1000 >                if (casTabAt(tab, i, null,
1001 >                             new Node<K,V>(hash, key, value, null)))
1002 >                    break;                   // no lock when adding to empty bin
1003 >            }
1004 >            else if ((fh = f.hash) == MOVED)
1005 >                tab = helpTransfer(tab, f);
1006 >            else {
1007 >                V oldVal = null;
1008 >                synchronized (f) {
1009 >                    if (tabAt(tab, i) == f) {
1010 >                        if (fh >= 0) {
1011 >                            binCount = 1;
1012 >                            for (Node<K,V> e = f;; ++binCount) {
1013 >                                K ek;
1014 >                                if (e.hash == hash &&
1015 >                                    ((ek = e.key) == key ||
1016 >                                     (ek != null && key.equals(ek)))) {
1017 >                                    oldVal = e.val;
1018 >                                    if (!onlyIfAbsent)
1019 >                                        e.val = value;
1020 >                                    break;
1021 >                                }
1022 >                                Node<K,V> pred = e;
1023 >                                if ((e = e.next) == null) {
1024 >                                    pred.next = new Node<K,V>(hash, key,
1025 >                                                              value, null);
1026 >                                    break;
1027 >                                }
1028 >                            }
1029 >                        }
1030 >                        else if (f instanceof TreeBin) {
1031 >                            Node<K,V> p;
1032 >                            binCount = 2;
1033 >                            if ((p = ((TreeBin<K,V>)f).putTreeVal(hash, key,
1034 >                                                           value)) != null) {
1035 >                                oldVal = p.val;
1036 >                                if (!onlyIfAbsent)
1037 >                                    p.val = value;
1038 >                            }
1039 >                        }
1040 >                    }
1041 >                }
1042 >                if (binCount != 0) {
1043 >                    if (binCount >= TREEIFY_THRESHOLD)
1044 >                        treeifyBin(tab, i);
1045 >                    if (oldVal != null)
1046 >                        return oldVal;
1047 >                    break;
1048 >                }
1049 >            }
1050 >        }
1051 >        addCount(1L, binCount);
1052 >        return null;
1053      }
1054  
1055      /**
# Line 2593 | Line 1060 | public class ConcurrentHashMapV8<K, V>
1060       * @param m mappings to be stored in this map
1061       */
1062      public void putAll(Map<? extends K, ? extends V> m) {
1063 <        internalPutAll(m);
1064 <    }
1065 <
2599 <    /**
2600 <     * If the specified key is not already associated with a value,
2601 <     * computes its value using the given mappingFunction and enters
2602 <     * it into the map unless null.  This is equivalent to
2603 <     * <pre> {@code
2604 <     * if (map.containsKey(key))
2605 <     *   return map.get(key);
2606 <     * value = mappingFunction.apply(key);
2607 <     * if (value != null)
2608 <     *   map.put(key, value);
2609 <     * return value;}</pre>
2610 <     *
2611 <     * except that the action is performed atomically.  If the
2612 <     * function returns {@code null} no mapping is recorded. If the
2613 <     * function itself throws an (unchecked) exception, the exception
2614 <     * is rethrown to its caller, and no mapping is recorded.  Some
2615 <     * attempted update operations on this map by other threads may be
2616 <     * blocked while computation is in progress, so the computation
2617 <     * should be short and simple, and must not attempt to update any
2618 <     * other mappings of this Map. The most appropriate usage is to
2619 <     * construct a new object serving as an initial mapped value, or
2620 <     * memoized result, as in:
2621 <     *
2622 <     *  <pre> {@code
2623 <     * map.computeIfAbsent(key, new Fun<K, V>() {
2624 <     *   public V map(K k) { return new Value(f(k)); }});}</pre>
2625 <     *
2626 <     * @param key key with which the specified value is to be associated
2627 <     * @param mappingFunction the function to compute a value
2628 <     * @return the current (existing or computed) value associated with
2629 <     *         the specified key, or null if the computed value is null.
2630 <     * @throws NullPointerException if the specified key or mappingFunction
2631 <     *         is null
2632 <     * @throws IllegalStateException if the computation detectably
2633 <     *         attempts a recursive update to this map that would
2634 <     *         otherwise never complete
2635 <     * @throws RuntimeException or Error if the mappingFunction does so,
2636 <     *         in which case the mapping is left unestablished
2637 <     */
2638 <    @SuppressWarnings("unchecked")
2639 <    public V computeIfAbsent(K key, Fun<? super K, ? extends V> mappingFunction) {
2640 <        if (key == null || mappingFunction == null)
2641 <            throw new NullPointerException();
2642 <        return (V)internalComputeIfAbsent(key, mappingFunction);
2643 <    }
2644 <
2645 <    /**
2646 <     * If the given key is present, computes a new mapping value given a key and
2647 <     * its current mapped value. This is equivalent to
2648 <     *  <pre> {@code
2649 <     *   if (map.containsKey(key)) {
2650 <     *     value = remappingFunction.apply(key, map.get(key));
2651 <     *     if (value != null)
2652 <     *       map.put(key, value);
2653 <     *     else
2654 <     *       map.remove(key);
2655 <     *   }
2656 <     * }</pre>
2657 <     *
2658 <     * except that the action is performed atomically.  If the
2659 <     * function returns {@code null}, the mapping is removed.  If the
2660 <     * function itself throws an (unchecked) exception, the exception
2661 <     * is rethrown to its caller, and the current mapping is left
2662 <     * unchanged.  Some attempted update operations on this map by
2663 <     * other threads may be blocked while computation is in progress,
2664 <     * so the computation should be short and simple, and must not
2665 <     * attempt to update any other mappings of this Map. For example,
2666 <     * to either create or append new messages to a value mapping:
2667 <     *
2668 <     * @param key key with which the specified value is to be associated
2669 <     * @param remappingFunction the function to compute a value
2670 <     * @return the new value associated with the specified key, or null if none
2671 <     * @throws NullPointerException if the specified key or remappingFunction
2672 <     *         is null
2673 <     * @throws IllegalStateException if the computation detectably
2674 <     *         attempts a recursive update to this map that would
2675 <     *         otherwise never complete
2676 <     * @throws RuntimeException or Error if the remappingFunction does so,
2677 <     *         in which case the mapping is unchanged
2678 <     */
2679 <    @SuppressWarnings("unchecked")
2680 <    public V computeIfPresent(K key, BiFun<? super K, ? super V, ? extends V> remappingFunction) {
2681 <        if (key == null || remappingFunction == null)
2682 <            throw new NullPointerException();
2683 <        return (V)internalCompute(key, true, remappingFunction);
2684 <    }
2685 <
2686 <    /**
2687 <     * Computes a new mapping value given a key and
2688 <     * its current mapped value (or {@code null} if there is no current
2689 <     * mapping). This is equivalent to
2690 <     *  <pre> {@code
2691 <     *   value = remappingFunction.apply(key, map.get(key));
2692 <     *   if (value != null)
2693 <     *     map.put(key, value);
2694 <     *   else
2695 <     *     map.remove(key);
2696 <     * }</pre>
2697 <     *
2698 <     * except that the action is performed atomically.  If the
2699 <     * function returns {@code null}, the mapping is removed.  If the
2700 <     * function itself throws an (unchecked) exception, the exception
2701 <     * is rethrown to its caller, and the current mapping is left
2702 <     * unchanged.  Some attempted update operations on this map by
2703 <     * other threads may be blocked while computation is in progress,
2704 <     * so the computation should be short and simple, and must not
2705 <     * attempt to update any other mappings of this Map. For example,
2706 <     * to either create or append new messages to a value mapping:
2707 <     *
2708 <     * <pre> {@code
2709 <     * Map<Key, String> map = ...;
2710 <     * final String msg = ...;
2711 <     * map.compute(key, new BiFun<Key, String, String>() {
2712 <     *   public String apply(Key k, String v) {
2713 <     *    return (v == null) ? msg : v + msg;});}}</pre>
2714 <     *
2715 <     * @param key key with which the specified value is to be associated
2716 <     * @param remappingFunction the function to compute a value
2717 <     * @return the new value associated with the specified key, or null if none
2718 <     * @throws NullPointerException if the specified key or remappingFunction
2719 <     *         is null
2720 <     * @throws IllegalStateException if the computation detectably
2721 <     *         attempts a recursive update to this map that would
2722 <     *         otherwise never complete
2723 <     * @throws RuntimeException or Error if the remappingFunction does so,
2724 <     *         in which case the mapping is unchanged
2725 <     */
2726 <    @SuppressWarnings("unchecked")
2727 <    public V compute(K key, BiFun<? super K, ? super V, ? extends V> remappingFunction) {
2728 <        if (key == null || remappingFunction == null)
2729 <            throw new NullPointerException();
2730 <        return (V)internalCompute(key, false, remappingFunction);
2731 <    }
2732 <
2733 <    /**
2734 <     * If the specified key is not already associated
2735 <     * with a value, associate it with the given value.
2736 <     * Otherwise, replace the value with the results of
2737 <     * the given remapping function. This is equivalent to:
2738 <     *  <pre> {@code
2739 <     *   if (!map.containsKey(key))
2740 <     *     map.put(value);
2741 <     *   else {
2742 <     *     newValue = remappingFunction.apply(map.get(key), value);
2743 <     *     if (value != null)
2744 <     *       map.put(key, value);
2745 <     *     else
2746 <     *       map.remove(key);
2747 <     *   }
2748 <     * }</pre>
2749 <     * except that the action is performed atomically.  If the
2750 <     * function returns {@code null}, the mapping is removed.  If the
2751 <     * function itself throws an (unchecked) exception, the exception
2752 <     * is rethrown to its caller, and the current mapping is left
2753 <     * unchanged.  Some attempted update operations on this map by
2754 <     * other threads may be blocked while computation is in progress,
2755 <     * so the computation should be short and simple, and must not
2756 <     * attempt to update any other mappings of this Map.
2757 <     */
2758 <    @SuppressWarnings("unchecked")
2759 <    public V merge(K key, V value, BiFun<? super V, ? super V, ? extends V> remappingFunction) {
2760 <        if (key == null || value == null || remappingFunction == null)
2761 <            throw new NullPointerException();
2762 <        return (V)internalMerge(key, value, remappingFunction);
1063 >        tryPresize(m.size());
1064 >        for (Map.Entry<? extends K, ? extends V> e : m.entrySet())
1065 >            putVal(e.getKey(), e.getValue(), false);
1066      }
1067  
1068      /**
# Line 2771 | Line 1074 | public class ConcurrentHashMapV8<K, V>
1074       *         {@code null} if there was no mapping for {@code key}
1075       * @throws NullPointerException if the specified key is null
1076       */
1077 <    @SuppressWarnings("unchecked")
1078 <        public V remove(Object key) {
2776 <        if (key == null)
2777 <            throw new NullPointerException();
2778 <        return (V)internalReplace(key, null, null);
2779 <    }
2780 <
2781 <    /**
2782 <     * {@inheritDoc}
2783 <     *
2784 <     * @throws NullPointerException if the specified key is null
2785 <     */
2786 <    public boolean remove(Object key, Object value) {
2787 <        if (key == null)
2788 <            throw new NullPointerException();
2789 <        if (value == null)
2790 <            return false;
2791 <        return internalReplace(key, null, value) != null;
2792 <    }
2793 <
2794 <    /**
2795 <     * {@inheritDoc}
2796 <     *
2797 <     * @throws NullPointerException if any of the arguments are null
2798 <     */
2799 <    public boolean replace(K key, V oldValue, V newValue) {
2800 <        if (key == null || oldValue == null || newValue == null)
2801 <            throw new NullPointerException();
2802 <        return internalReplace(key, newValue, oldValue) != null;
1077 >    public V remove(Object key) {
1078 >        return replaceNode(key, null, null);
1079      }
1080  
1081      /**
1082 <     * {@inheritDoc}
1083 <     *
1084 <     * @return the previous value associated with the specified key,
2809 <     *         or {@code null} if there was no mapping for the key
2810 <     * @throws NullPointerException if the specified key or value is null
1082 >     * Implementation for the four public remove/replace methods:
1083 >     * Replaces node value with v, conditional upon match of cv if
1084 >     * non-null.  If resulting value is null, delete.
1085       */
1086 <    @SuppressWarnings("unchecked")
1087 <        public V replace(K key, V value) {
1088 <        if (key == null || value == null)
1089 <            throw new NullPointerException();
1090 <        return (V)internalReplace(key, value, null);
1086 >    final V replaceNode(Object key, V value, Object cv) {
1087 >        int hash = spread(key.hashCode());
1088 >        for (Node<K,V>[] tab = table;;) {
1089 >            Node<K,V> f; int n, i, fh;
1090 >            if (tab == null || (n = tab.length) == 0 ||
1091 >                (f = tabAt(tab, i = (n - 1) & hash)) == null)
1092 >                break;
1093 >            else if ((fh = f.hash) == MOVED)
1094 >                tab = helpTransfer(tab, f);
1095 >            else {
1096 >                V oldVal = null;
1097 >                boolean validated = false;
1098 >                synchronized (f) {
1099 >                    if (tabAt(tab, i) == f) {
1100 >                        if (fh >= 0) {
1101 >                            validated = true;
1102 >                            for (Node<K,V> e = f, pred = null;;) {
1103 >                                K ek;
1104 >                                if (e.hash == hash &&
1105 >                                    ((ek = e.key) == key ||
1106 >                                     (ek != null && key.equals(ek)))) {
1107 >                                    V ev = e.val;
1108 >                                    if (cv == null || cv == ev ||
1109 >                                        (ev != null && cv.equals(ev))) {
1110 >                                        oldVal = ev;
1111 >                                        if (value != null)
1112 >                                            e.val = value;
1113 >                                        else if (pred != null)
1114 >                                            pred.next = e.next;
1115 >                                        else
1116 >                                            setTabAt(tab, i, e.next);
1117 >                                    }
1118 >                                    break;
1119 >                                }
1120 >                                pred = e;
1121 >                                if ((e = e.next) == null)
1122 >                                    break;
1123 >                            }
1124 >                        }
1125 >                        else if (f instanceof TreeBin) {
1126 >                            validated = true;
1127 >                            TreeBin<K,V> t = (TreeBin<K,V>)f;
1128 >                            TreeNode<K,V> r, p;
1129 >                            if ((r = t.root) != null &&
1130 >                                (p = r.findTreeNode(hash, key, null)) != null) {
1131 >                                V pv = p.val;
1132 >                                if (cv == null || cv == pv ||
1133 >                                    (pv != null && cv.equals(pv))) {
1134 >                                    oldVal = pv;
1135 >                                    if (value != null)
1136 >                                        p.val = value;
1137 >                                    else if (t.removeTreeNode(p))
1138 >                                        setTabAt(tab, i, untreeify(t.first));
1139 >                                }
1140 >                            }
1141 >                        }
1142 >                    }
1143 >                }
1144 >                if (validated) {
1145 >                    if (oldVal != null) {
1146 >                        if (value == null)
1147 >                            addCount(-1L, -1);
1148 >                        return oldVal;
1149 >                    }
1150 >                    break;
1151 >                }
1152 >            }
1153 >        }
1154 >        return null;
1155      }
1156  
1157      /**
1158       * Removes all of the mappings from this map.
1159       */
1160      public void clear() {
1161 <        internalClear();
1161 >        long delta = 0L; // negative number of deletions
1162 >        int i = 0;
1163 >        Node<K,V>[] tab = table;
1164 >        while (tab != null && i < tab.length) {
1165 >            int fh;
1166 >            Node<K,V> f = tabAt(tab, i);
1167 >            if (f == null)
1168 >                ++i;
1169 >            else if ((fh = f.hash) == MOVED) {
1170 >                tab = helpTransfer(tab, f);
1171 >                i = 0; // restart
1172 >            }
1173 >            else {
1174 >                synchronized (f) {
1175 >                    if (tabAt(tab, i) == f) {
1176 >                        Node<K,V> p = (fh >= 0 ? f :
1177 >                                       (f instanceof TreeBin) ?
1178 >                                       ((TreeBin<K,V>)f).first : null);
1179 >                        while (p != null) {
1180 >                            --delta;
1181 >                            p = p.next;
1182 >                        }
1183 >                        setTabAt(tab, i++, null);
1184 >                    }
1185 >                }
1186 >            }
1187 >        }
1188 >        if (delta != 0L)
1189 >            addCount(delta, -1);
1190      }
1191  
1192      /**
1193       * Returns a {@link Set} view of the keys contained in this map.
1194       * The set is backed by the map, so changes to the map are
1195 <     * reflected in the set, and vice-versa.  The set supports element
1195 >     * reflected in the set, and vice-versa. The set supports element
1196       * removal, which removes the corresponding mapping from this map,
1197       * via the {@code Iterator.remove}, {@code Set.remove},
1198       * {@code removeAll}, {@code retainAll}, and {@code clear}
# Line 2838 | Line 1204 | public class ConcurrentHashMapV8<K, V>
1204       * and guarantees to traverse elements as they existed upon
1205       * construction of the iterator, and may (but is not guaranteed to)
1206       * reflect any modifications subsequent to construction.
1207 +     *
1208 +     * @return the set view
1209       */
1210 <    public Set<K> keySet() {
1211 <        KeySet<K,V> ks = keySet;
1212 <        return (ks != null) ? ks : (keySet = new KeySet<K,V>(this));
1210 >    public KeySetView<K,V> keySet() {
1211 >        KeySetView<K,V> ks;
1212 >        return (ks = keySet) != null ? ks : (keySet = new KeySetView<K,V>(this, null));
1213      }
1214  
1215      /**
# Line 2859 | Line 1227 | public class ConcurrentHashMapV8<K, V>
1227       * and guarantees to traverse elements as they existed upon
1228       * construction of the iterator, and may (but is not guaranteed to)
1229       * reflect any modifications subsequent to construction.
1230 +     *
1231 +     * @return the collection view
1232       */
1233      public Collection<V> values() {
1234 <        Values<K,V> vs = values;
1235 <        return (vs != null) ? vs : (values = new Values<K,V>(this));
1234 >        ValuesView<K,V> vs;
1235 >        return (vs = values) != null ? vs : (values = new ValuesView<K,V>(this));
1236      }
1237  
1238      /**
# Line 2872 | Line 1242 | public class ConcurrentHashMapV8<K, V>
1242       * removal, which removes the corresponding mapping from the map,
1243       * via the {@code Iterator.remove}, {@code Set.remove},
1244       * {@code removeAll}, {@code retainAll}, and {@code clear}
1245 <     * operations.  It does not support the {@code add} or
2876 <     * {@code addAll} operations.
1245 >     * operations.
1246       *
1247       * <p>The view's {@code iterator} is a "weakly consistent" iterator
1248       * that will never throw {@link ConcurrentModificationException},
1249       * and guarantees to traverse elements as they existed upon
1250       * construction of the iterator, and may (but is not guaranteed to)
1251       * reflect any modifications subsequent to construction.
2883     */
2884    public Set<Map.Entry<K,V>> entrySet() {
2885        EntrySet<K,V> es = entrySet;
2886        return (es != null) ? es : (entrySet = new EntrySet<K,V>(this));
2887    }
2888
2889    /**
2890     * Returns an enumeration of the keys in this table.
1252       *
1253 <     * @return an enumeration of the keys in this table
2893 <     * @see #keySet()
1253 >     * @return the set view
1254       */
1255 <    public Enumeration<K> keys() {
1256 <        return new KeyIterator<K,V>(this);
1257 <    }
2898 <
2899 <    /**
2900 <     * Returns an enumeration of the values in this table.
2901 <     *
2902 <     * @return an enumeration of the values in this table
2903 <     * @see #values()
2904 <     */
2905 <    public Enumeration<V> elements() {
2906 <        return new ValueIterator<K,V>(this);
2907 <    }
2908 <
2909 <    /**
2910 <     * Returns a partitionable iterator of the keys in this map.
2911 <     *
2912 <     * @return a partitionable iterator of the keys in this map
2913 <     */
2914 <    public Spliterator<K> keySpliterator() {
2915 <        return new KeyIterator<K,V>(this);
2916 <    }
2917 <
2918 <    /**
2919 <     * Returns a partitionable iterator of the values in this map.
2920 <     *
2921 <     * @return a partitionable iterator of the values in this map
2922 <     */
2923 <    public Spliterator<V> valueSpliterator() {
2924 <        return new ValueIterator<K,V>(this);
2925 <    }
2926 <
2927 <    /**
2928 <     * Returns a partitionable iterator of the entries in this map.
2929 <     *
2930 <     * @return a partitionable iterator of the entries in this map
2931 <     */
2932 <    public Spliterator<Map.Entry<K,V>> entrySpliterator() {
2933 <        return new EntryIterator<K,V>(this);
1255 >    public Set<Map.Entry<K,V>> entrySet() {
1256 >        EntrySetView<K,V> es;
1257 >        return (es = entrySet) != null ? es : (entrySet = new EntrySetView<K,V>(this));
1258      }
1259  
1260      /**
# Line 2942 | Line 1266 | public class ConcurrentHashMapV8<K, V>
1266       */
1267      public int hashCode() {
1268          int h = 0;
1269 <        Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
1270 <        Object v;
1271 <        while ((v = it.advance()) != null) {
1272 <            h += it.nextKey.hashCode() ^ v.hashCode();
1269 >        Node<K,V>[] t;
1270 >        if ((t = table) != null) {
1271 >            Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length);
1272 >            for (Node<K,V> p; (p = it.advance()) != null; )
1273 >                h += p.key.hashCode() ^ p.val.hashCode();
1274          }
1275          return h;
1276      }
# Line 2962 | Line 1287 | public class ConcurrentHashMapV8<K, V>
1287       * @return a string representation of this map
1288       */
1289      public String toString() {
1290 <        Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
1290 >        Node<K,V>[] t;
1291 >        int f = (t = table) == null ? 0 : t.length;
1292 >        Traverser<K,V> it = new Traverser<K,V>(t, f, 0, f);
1293          StringBuilder sb = new StringBuilder();
1294          sb.append('{');
1295 <        Object v;
1296 <        if ((v = it.advance()) != null) {
1295 >        Node<K,V> p;
1296 >        if ((p = it.advance()) != null) {
1297              for (;;) {
1298 <                Object k = it.nextKey;
1298 >                K k = p.key;
1299 >                V v = p.val;
1300                  sb.append(k == this ? "(this Map)" : k);
1301                  sb.append('=');
1302                  sb.append(v == this ? "(this Map)" : v);
1303 <                if ((v = it.advance()) == null)
1303 >                if ((p = it.advance()) == null)
1304                      break;
1305                  sb.append(',').append(' ');
1306              }
# Line 2995 | Line 1323 | public class ConcurrentHashMapV8<K, V>
1323              if (!(o instanceof Map))
1324                  return false;
1325              Map<?,?> m = (Map<?,?>) o;
1326 <            Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
1327 <            Object val;
1328 <            while ((val = it.advance()) != null) {
1329 <                Object v = m.get(it.nextKey);
1326 >            Node<K,V>[] t;
1327 >            int f = (t = table) == null ? 0 : t.length;
1328 >            Traverser<K,V> it = new Traverser<K,V>(t, f, 0, f);
1329 >            for (Node<K,V> p; (p = it.advance()) != null; ) {
1330 >                V val = p.val;
1331 >                Object v = m.get(p.key);
1332                  if (v == null || (v != val && !v.equals(val)))
1333                      return false;
1334              }
# Line 3006 | Line 1336 | public class ConcurrentHashMapV8<K, V>
1336                  Object mk, mv, v;
1337                  if ((mk = e.getKey()) == null ||
1338                      (mv = e.getValue()) == null ||
1339 <                    (v = internalGet(mk)) == null ||
1339 >                    (v = get(mk)) == null ||
1340                      (mv != v && !mv.equals(v)))
1341                      return false;
1342              }
# Line 3014 | Line 1344 | public class ConcurrentHashMapV8<K, V>
1344          return true;
1345      }
1346  
1347 <    /* ----------------Iterators -------------- */
1347 >    /**
1348 >     * Stripped-down version of helper class used in previous version,
1349 >     * declared for the sake of serialization compatibility
1350 >     */
1351 >    static class Segment<K,V> extends ReentrantLock implements Serializable {
1352 >        private static final long serialVersionUID = 2249069246763182397L;
1353 >        final float loadFactor;
1354 >        Segment(float lf) { this.loadFactor = lf; }
1355 >    }
1356  
1357 <    @SuppressWarnings("serial")
1358 <    static final class KeyIterator<K,V> extends Traverser<K,V,Object>
1359 <        implements Spliterator<K>, Enumeration<K> {
1360 <        KeyIterator(ConcurrentHashMapV8<K, V> map) { super(map); }
1361 <        KeyIterator(Traverser<K,V,Object> it, boolean split) {
1362 <            super(it, split);
1363 <        }
1364 <        public KeyIterator<K,V> split() {
1365 <            if (last != null || (next != null && nextVal == null))
1366 <                throw new IllegalStateException();
1367 <            return new KeyIterator<K,V>(this, true);
1368 <        }
1369 <        @SuppressWarnings("unchecked")
1370 <            public final K next() {
1371 <            if (nextVal == null && advance() == null)
1372 <                throw new NoSuchElementException();
1373 <            Object k = nextKey;
1374 <            nextVal = null;
1375 <            return (K) k;
1357 >    /**
1358 >     * Saves the state of the {@code ConcurrentHashMapV8} instance to a
1359 >     * stream (i.e., serializes it).
1360 >     * @param s the stream
1361 >     * @serialData
1362 >     * the key (Object) and value (Object)
1363 >     * for each key-value mapping, followed by a null pair.
1364 >     * The key-value mappings are emitted in no particular order.
1365 >     */
1366 >    private void writeObject(java.io.ObjectOutputStream s)
1367 >        throws java.io.IOException {
1368 >        // For serialization compatibility
1369 >        // Emulate segment calculation from previous version of this class
1370 >        int sshift = 0;
1371 >        int ssize = 1;
1372 >        while (ssize < DEFAULT_CONCURRENCY_LEVEL) {
1373 >            ++sshift;
1374 >            ssize <<= 1;
1375 >        }
1376 >        int segmentShift = 32 - sshift;
1377 >        int segmentMask = ssize - 1;
1378 >        @SuppressWarnings("unchecked") Segment<K,V>[] segments = (Segment<K,V>[])
1379 >            new Segment<?,?>[DEFAULT_CONCURRENCY_LEVEL];
1380 >        for (int i = 0; i < segments.length; ++i)
1381 >            segments[i] = new Segment<K,V>(LOAD_FACTOR);
1382 >        s.putFields().put("segments", segments);
1383 >        s.putFields().put("segmentShift", segmentShift);
1384 >        s.putFields().put("segmentMask", segmentMask);
1385 >        s.writeFields();
1386 >
1387 >        Node<K,V>[] t;
1388 >        if ((t = table) != null) {
1389 >            Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length);
1390 >            for (Node<K,V> p; (p = it.advance()) != null; ) {
1391 >                s.writeObject(p.key);
1392 >                s.writeObject(p.val);
1393 >            }
1394          }
1395 <
1396 <        public final K nextElement() { return next(); }
1395 >        s.writeObject(null);
1396 >        s.writeObject(null);
1397 >        segments = null; // throw away
1398      }
1399  
1400 <    @SuppressWarnings("serial")
1401 <    static final class ValueIterator<K,V> extends Traverser<K,V,Object>
1402 <        implements Spliterator<V>, Enumeration<V> {
1403 <        ValueIterator(ConcurrentHashMapV8<K, V> map) { super(map); }
1404 <        ValueIterator(Traverser<K,V,Object> it, boolean split) {
1405 <            super(it, split);
1406 <        }
1407 <        public ValueIterator<K,V> split() {
1408 <            if (last != null || (next != null && nextVal == null))
1409 <                throw new IllegalStateException();
1410 <            return new ValueIterator<K,V>(this, true);
1400 >    /**
1401 >     * Reconstitutes the instance from a stream (that is, deserializes it).
1402 >     * @param s the stream
1403 >     */
1404 >    private void readObject(java.io.ObjectInputStream s)
1405 >        throws java.io.IOException, ClassNotFoundException {
1406 >        /*
1407 >         * To improve performance in typical cases, we create nodes
1408 >         * while reading, then place in table once size is known.
1409 >         * However, we must also validate uniqueness and deal with
1410 >         * overpopulated bins while doing so, which requires
1411 >         * specialized versions of putVal mechanics.
1412 >         */
1413 >        sizeCtl = -1; // force exclusion for table construction
1414 >        s.defaultReadObject();
1415 >        long size = 0L;
1416 >        Node<K,V> p = null;
1417 >        for (;;) {
1418 >            @SuppressWarnings("unchecked") K k = (K) s.readObject();
1419 >            @SuppressWarnings("unchecked") V v = (V) s.readObject();
1420 >            if (k != null && v != null) {
1421 >                p = new Node<K,V>(spread(k.hashCode()), k, v, p);
1422 >                ++size;
1423 >            }
1424 >            else
1425 >                break;
1426          }
1427 <
1428 <        @SuppressWarnings("unchecked")
1429 <            public final V next() {
1430 <            Object v;
1431 <            if ((v = nextVal) == null && (v = advance()) == null)
1432 <                throw new NoSuchElementException();
1433 <            nextVal = null;
1434 <            return (V) v;
1427 >        if (size == 0L)
1428 >            sizeCtl = 0;
1429 >        else {
1430 >            int n;
1431 >            if (size >= (long)(MAXIMUM_CAPACITY >>> 1))
1432 >                n = MAXIMUM_CAPACITY;
1433 >            else {
1434 >                int sz = (int)size;
1435 >                n = tableSizeFor(sz + (sz >>> 1) + 1);
1436 >            }
1437 >            @SuppressWarnings({"rawtypes","unchecked"})
1438 >                Node<K,V>[] tab = (Node<K,V>[])new Node[n];
1439 >            int mask = n - 1;
1440 >            long added = 0L;
1441 >            while (p != null) {
1442 >                boolean insertAtFront;
1443 >                Node<K,V> next = p.next, first;
1444 >                int h = p.hash, j = h & mask;
1445 >                if ((first = tabAt(tab, j)) == null)
1446 >                    insertAtFront = true;
1447 >                else {
1448 >                    K k = p.key;
1449 >                    if (first.hash < 0) {
1450 >                        TreeBin<K,V> t = (TreeBin<K,V>)first;
1451 >                        if (t.putTreeVal(h, k, p.val) == null)
1452 >                            ++added;
1453 >                        insertAtFront = false;
1454 >                    }
1455 >                    else {
1456 >                        int binCount = 0;
1457 >                        insertAtFront = true;
1458 >                        Node<K,V> q; K qk;
1459 >                        for (q = first; q != null; q = q.next) {
1460 >                            if (q.hash == h &&
1461 >                                ((qk = q.key) == k ||
1462 >                                 (qk != null && k.equals(qk)))) {
1463 >                                insertAtFront = false;
1464 >                                break;
1465 >                            }
1466 >                            ++binCount;
1467 >                        }
1468 >                        if (insertAtFront && binCount >= TREEIFY_THRESHOLD) {
1469 >                            insertAtFront = false;
1470 >                            ++added;
1471 >                            p.next = first;
1472 >                            TreeNode<K,V> hd = null, tl = null;
1473 >                            for (q = p; q != null; q = q.next) {
1474 >                                TreeNode<K,V> t = new TreeNode<K,V>
1475 >                                    (q.hash, q.key, q.val, null, null);
1476 >                                if ((t.prev = tl) == null)
1477 >                                    hd = t;
1478 >                                else
1479 >                                    tl.next = t;
1480 >                                tl = t;
1481 >                            }
1482 >                            setTabAt(tab, j, new TreeBin<K,V>(hd));
1483 >                        }
1484 >                    }
1485 >                }
1486 >                if (insertAtFront) {
1487 >                    ++added;
1488 >                    p.next = first;
1489 >                    setTabAt(tab, j, p);
1490 >                }
1491 >                p = next;
1492 >            }
1493 >            table = tab;
1494 >            sizeCtl = n - (n >>> 2);
1495 >            baseCount = added;
1496          }
3064
3065        public final V nextElement() { return next(); }
1497      }
1498  
1499 <    @SuppressWarnings("serial")
3069 <    static final class EntryIterator<K,V> extends Traverser<K,V,Object>
3070 <        implements Spliterator<Map.Entry<K,V>> {
3071 <        EntryIterator(ConcurrentHashMapV8<K, V> map) { super(map); }
3072 <        EntryIterator(Traverser<K,V,Object> it, boolean split) {
3073 <            super(it, split);
3074 <        }
3075 <        public EntryIterator<K,V> split() {
3076 <            if (last != null || (next != null && nextVal == null))
3077 <                throw new IllegalStateException();
3078 <            return new EntryIterator<K,V>(this, true);
3079 <        }
1499 >    // ConcurrentMap methods
1500  
1501 <        @SuppressWarnings("unchecked")
1502 <            public final Map.Entry<K,V> next() {
1503 <            Object v;
1504 <            if ((v = nextVal) == null && (v = advance()) == null)
1505 <                throw new NoSuchElementException();
1506 <            Object k = nextKey;
1507 <            nextVal = null;
1508 <            return new MapEntry<K,V>((K)k, (V)v, map);
1509 <        }
1501 >    /**
1502 >     * {@inheritDoc}
1503 >     *
1504 >     * @return the previous value associated with the specified key,
1505 >     *         or {@code null} if there was no mapping for the key
1506 >     * @throws NullPointerException if the specified key or value is null
1507 >     */
1508 >    public V putIfAbsent(K key, V value) {
1509 >        return putVal(key, value, true);
1510      }
1511  
1512      /**
1513 <     * Exported Entry for iterators
1513 >     * {@inheritDoc}
1514 >     *
1515 >     * @throws NullPointerException if the specified key is null
1516       */
1517 <    static final class MapEntry<K,V> implements Map.Entry<K, V> {
1518 <        final K key; // non-null
1519 <        V val;       // non-null
1520 <        final ConcurrentHashMapV8<K, V> map;
3099 <        MapEntry(K key, V val, ConcurrentHashMapV8<K, V> map) {
3100 <            this.key = key;
3101 <            this.val = val;
3102 <            this.map = map;
3103 <        }
3104 <        public final K getKey()       { return key; }
3105 <        public final V getValue()     { return val; }
3106 <        public final int hashCode()   { return key.hashCode() ^ val.hashCode(); }
3107 <        public final String toString(){ return key + "=" + val; }
3108 <
3109 <        public final boolean equals(Object o) {
3110 <            Object k, v; Map.Entry<?,?> e;
3111 <            return ((o instanceof Map.Entry) &&
3112 <                    (k = (e = (Map.Entry<?,?>)o).getKey()) != null &&
3113 <                    (v = e.getValue()) != null &&
3114 <                    (k == key || k.equals(key)) &&
3115 <                    (v == val || v.equals(val)));
3116 <        }
3117 <
3118 <        /**
3119 <         * Sets our entry's value and writes through to the map. The
3120 <         * value to return is somewhat arbitrary here. Since we do not
3121 <         * necessarily track asynchronous changes, the most recent
3122 <         * "previous" value could be different from what we return (or
3123 <         * could even have been removed in which case the put will
3124 <         * re-establish). We do not and cannot guarantee more.
3125 <         */
3126 <        public final V setValue(V value) {
3127 <            if (value == null) throw new NullPointerException();
3128 <            V v = val;
3129 <            val = value;
3130 <            map.put(key, value);
3131 <            return v;
3132 <        }
1517 >    public boolean remove(Object key, Object value) {
1518 >        if (key == null)
1519 >            throw new NullPointerException();
1520 >        return value != null && replaceNode(key, null, value) != null;
1521      }
1522  
1523 <    /* ----------------Views -------------- */
1523 >    /**
1524 >     * {@inheritDoc}
1525 >     *
1526 >     * @throws NullPointerException if any of the arguments are null
1527 >     */
1528 >    public boolean replace(K key, V oldValue, V newValue) {
1529 >        if (key == null || oldValue == null || newValue == null)
1530 >            throw new NullPointerException();
1531 >        return replaceNode(key, newValue, oldValue) != null;
1532 >    }
1533  
1534      /**
1535 <     * Base class for views.
1535 >     * {@inheritDoc}
1536 >     *
1537 >     * @return the previous value associated with the specified key,
1538 >     *         or {@code null} if there was no mapping for the key
1539 >     * @throws NullPointerException if the specified key or value is null
1540       */
1541 <    static abstract class CHMView<K, V> {
1542 <        final ConcurrentHashMapV8<K, V> map;
1543 <        CHMView(ConcurrentHashMapV8<K, V> map)  { this.map = map; }
1544 <        public final int size()                 { return map.size(); }
1545 <        public final boolean isEmpty()          { return map.isEmpty(); }
3145 <        public final void clear()               { map.clear(); }
1541 >    public V replace(K key, V value) {
1542 >        if (key == null || value == null)
1543 >            throw new NullPointerException();
1544 >        return replaceNode(key, value, null);
1545 >    }
1546  
1547 <        // implementations below rely on concrete classes supplying these
3148 <        abstract public Iterator<?> iterator();
3149 <        abstract public boolean contains(Object o);
3150 <        abstract public boolean remove(Object o);
1547 >    // Overrides of JDK8+ Map extension method defaults
1548  
1549 <        private static final String oomeMsg = "Required array size too large";
1549 >    /**
1550 >     * Returns the value to which the specified key is mapped, or the
1551 >     * given default value if this map contains no mapping for the
1552 >     * key.
1553 >     *
1554 >     * @param key the key whose associated value is to be returned
1555 >     * @param defaultValue the value to return if this map contains
1556 >     * no mapping for the given key
1557 >     * @return the mapping for the key, if present; else the default value
1558 >     * @throws NullPointerException if the specified key is null
1559 >     */
1560 >    public V getOrDefault(Object key, V defaultValue) {
1561 >        V v;
1562 >        return (v = get(key)) == null ? defaultValue : v;
1563 >    }
1564 >
1565 >    public void forEach(BiAction<? super K, ? super V> action) {
1566 >        if (action == null) throw new NullPointerException();
1567 >        Node<K,V>[] t;
1568 >        if ((t = table) != null) {
1569 >            Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length);
1570 >            for (Node<K,V> p; (p = it.advance()) != null; ) {
1571 >                action.apply(p.key, p.val);
1572 >            }
1573 >        }
1574 >    }
1575  
1576 <        public final Object[] toArray() {
1577 <            long sz = map.mappingCount();
1578 <            if (sz > (long)(MAX_ARRAY_SIZE))
1579 <                throw new OutOfMemoryError(oomeMsg);
1580 <            int n = (int)sz;
1581 <            Object[] r = new Object[n];
1582 <            int i = 0;
1583 <            Iterator<?> it = iterator();
1584 <            while (it.hasNext()) {
1585 <                if (i == n) {
1586 <                    if (n >= MAX_ARRAY_SIZE)
1587 <                        throw new OutOfMemoryError(oomeMsg);
1588 <                    if (n >= MAX_ARRAY_SIZE - (MAX_ARRAY_SIZE >>> 1) - 1)
1589 <                        n = MAX_ARRAY_SIZE;
3168 <                    else
3169 <                        n += (n >>> 1) + 1;
3170 <                    r = Arrays.copyOf(r, n);
1576 >    public void replaceAll(BiFun<? super K, ? super V, ? extends V> function) {
1577 >        if (function == null) throw new NullPointerException();
1578 >        Node<K,V>[] t;
1579 >        if ((t = table) != null) {
1580 >            Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length);
1581 >            for (Node<K,V> p; (p = it.advance()) != null; ) {
1582 >                V oldValue = p.val;
1583 >                for (K key = p.key;;) {
1584 >                    V newValue = function.apply(key, oldValue);
1585 >                    if (newValue == null)
1586 >                        throw new NullPointerException();
1587 >                    if (replaceNode(key, newValue, oldValue) != null ||
1588 >                        (oldValue = get(key)) == null)
1589 >                        break;
1590                  }
3172                r[i++] = it.next();
1591              }
3174            return (i == n) ? r : Arrays.copyOf(r, i);
1592          }
1593 +    }
1594  
1595 <        @SuppressWarnings("unchecked")
1596 <            public final <T> T[] toArray(T[] a) {
1597 <            long sz = map.mappingCount();
1598 <            if (sz > (long)(MAX_ARRAY_SIZE))
1599 <                throw new OutOfMemoryError(oomeMsg);
1600 <            int m = (int)sz;
1601 <            T[] r = (a.length >= m) ? a :
1602 <                (T[])java.lang.reflect.Array
1603 <                .newInstance(a.getClass().getComponentType(), m);
1604 <            int n = r.length;
1605 <            int i = 0;
1606 <            Iterator<?> it = iterator();
1607 <            while (it.hasNext()) {
1608 <                if (i == n) {
1609 <                    if (n >= MAX_ARRAY_SIZE)
1610 <                        throw new OutOfMemoryError(oomeMsg);
1611 <                    if (n >= MAX_ARRAY_SIZE - (MAX_ARRAY_SIZE >>> 1) - 1)
1612 <                        n = MAX_ARRAY_SIZE;
1613 <                    else
1614 <                        n += (n >>> 1) + 1;
1615 <                    r = Arrays.copyOf(r, n);
1595 >    /**
1596 >     * If the specified key is not already associated with a value,
1597 >     * attempts to compute its value using the given mapping function
1598 >     * and enters it into this map unless {@code null}.  The entire
1599 >     * method invocation is performed atomically, so the function is
1600 >     * applied at most once per key.  Some attempted update operations
1601 >     * on this map by other threads may be blocked while computation
1602 >     * is in progress, so the computation should be short and simple,
1603 >     * and must not attempt to update any other mappings of this map.
1604 >     *
1605 >     * @param key key with which the specified value is to be associated
1606 >     * @param mappingFunction the function to compute a value
1607 >     * @return the current (existing or computed) value associated with
1608 >     *         the specified key, or null if the computed value is null
1609 >     * @throws NullPointerException if the specified key or mappingFunction
1610 >     *         is null
1611 >     * @throws IllegalStateException if the computation detectably
1612 >     *         attempts a recursive update to this map that would
1613 >     *         otherwise never complete
1614 >     * @throws RuntimeException or Error if the mappingFunction does so,
1615 >     *         in which case the mapping is left unestablished
1616 >     */
1617 >    public V computeIfAbsent(K key, Fun<? super K, ? extends V> mappingFunction) {
1618 >        if (key == null || mappingFunction == null)
1619 >            throw new NullPointerException();
1620 >        int h = spread(key.hashCode());
1621 >        V val = null;
1622 >        int binCount = 0;
1623 >        for (Node<K,V>[] tab = table;;) {
1624 >            Node<K,V> f; int n, i, fh;
1625 >            if (tab == null || (n = tab.length) == 0)
1626 >                tab = initTable();
1627 >            else if ((f = tabAt(tab, i = (n - 1) & h)) == null) {
1628 >                Node<K,V> r = new ReservationNode<K,V>();
1629 >                synchronized (r) {
1630 >                    if (casTabAt(tab, i, null, r)) {
1631 >                        binCount = 1;
1632 >                        Node<K,V> node = null;
1633 >                        try {
1634 >                            if ((val = mappingFunction.apply(key)) != null)
1635 >                                node = new Node<K,V>(h, key, val, null);
1636 >                        } finally {
1637 >                            setTabAt(tab, i, node);
1638 >                        }
1639 >                    }
1640                  }
1641 <                r[i++] = (T)it.next();
1641 >                if (binCount != 0)
1642 >                    break;
1643              }
1644 <            if (a == r && i < n) {
1645 <                r[i] = null; // null-terminate
1646 <                return r;
1644 >            else if ((fh = f.hash) == MOVED)
1645 >                tab = helpTransfer(tab, f);
1646 >            else {
1647 >                boolean added = false;
1648 >                synchronized (f) {
1649 >                    if (tabAt(tab, i) == f) {
1650 >                        if (fh >= 0) {
1651 >                            binCount = 1;
1652 >                            for (Node<K,V> e = f;; ++binCount) {
1653 >                                K ek; V ev;
1654 >                                if (e.hash == h &&
1655 >                                    ((ek = e.key) == key ||
1656 >                                     (ek != null && key.equals(ek)))) {
1657 >                                    val = e.val;
1658 >                                    break;
1659 >                                }
1660 >                                Node<K,V> pred = e;
1661 >                                if ((e = e.next) == null) {
1662 >                                    if ((val = mappingFunction.apply(key)) != null) {
1663 >                                        added = true;
1664 >                                        pred.next = new Node<K,V>(h, key, val, null);
1665 >                                    }
1666 >                                    break;
1667 >                                }
1668 >                            }
1669 >                        }
1670 >                        else if (f instanceof TreeBin) {
1671 >                            binCount = 2;
1672 >                            TreeBin<K,V> t = (TreeBin<K,V>)f;
1673 >                            TreeNode<K,V> r, p;
1674 >                            if ((r = t.root) != null &&
1675 >                                (p = r.findTreeNode(h, key, null)) != null)
1676 >                                val = p.val;
1677 >                            else if ((val = mappingFunction.apply(key)) != null) {
1678 >                                added = true;
1679 >                                t.putTreeVal(h, key, val);
1680 >                            }
1681 >                        }
1682 >                    }
1683 >                }
1684 >                if (binCount != 0) {
1685 >                    if (binCount >= TREEIFY_THRESHOLD)
1686 >                        treeifyBin(tab, i);
1687 >                    if (!added)
1688 >                        return val;
1689 >                    break;
1690 >                }
1691              }
3205            return (i == n) ? r : Arrays.copyOf(r, i);
3206        }
3207
3208        public final int hashCode() {
3209            int h = 0;
3210            for (Iterator<?> it = iterator(); it.hasNext();)
3211                h += it.next().hashCode();
3212            return h;
1692          }
1693 +        if (val != null)
1694 +            addCount(1L, binCount);
1695 +        return val;
1696 +    }
1697  
1698 <        public final String toString() {
1699 <            StringBuilder sb = new StringBuilder();
1700 <            sb.append('[');
1701 <            Iterator<?> it = iterator();
1702 <            if (it.hasNext()) {
1703 <                for (;;) {
1704 <                    Object e = it.next();
1705 <                    sb.append(e == this ? "(this Collection)" : e);
1706 <                    if (!it.hasNext())
1707 <                        break;
1708 <                    sb.append(',').append(' ');
1698 >    /**
1699 >     * If the value for the specified key is present, attempts to
1700 >     * compute a new mapping given the key and its current mapped
1701 >     * value.  The entire method invocation is performed atomically.
1702 >     * Some attempted update operations on this map by other threads
1703 >     * may be blocked while computation is in progress, so the
1704 >     * computation should be short and simple, and must not attempt to
1705 >     * update any other mappings of this map.
1706 >     *
1707 >     * @param key key with which a value may be associated
1708 >     * @param remappingFunction the function to compute a value
1709 >     * @return the new value associated with the specified key, or null if none
1710 >     * @throws NullPointerException if the specified key or remappingFunction
1711 >     *         is null
1712 >     * @throws IllegalStateException if the computation detectably
1713 >     *         attempts a recursive update to this map that would
1714 >     *         otherwise never complete
1715 >     * @throws RuntimeException or Error if the remappingFunction does so,
1716 >     *         in which case the mapping is unchanged
1717 >     */
1718 >    public V computeIfPresent(K key, BiFun<? super K, ? super V, ? extends V> remappingFunction) {
1719 >        if (key == null || remappingFunction == null)
1720 >            throw new NullPointerException();
1721 >        int h = spread(key.hashCode());
1722 >        V val = null;
1723 >        int delta = 0;
1724 >        int binCount = 0;
1725 >        for (Node<K,V>[] tab = table;;) {
1726 >            Node<K,V> f; int n, i, fh;
1727 >            if (tab == null || (n = tab.length) == 0)
1728 >                tab = initTable();
1729 >            else if ((f = tabAt(tab, i = (n - 1) & h)) == null)
1730 >                break;
1731 >            else if ((fh = f.hash) == MOVED)
1732 >                tab = helpTransfer(tab, f);
1733 >            else {
1734 >                synchronized (f) {
1735 >                    if (tabAt(tab, i) == f) {
1736 >                        if (fh >= 0) {
1737 >                            binCount = 1;
1738 >                            for (Node<K,V> e = f, pred = null;; ++binCount) {
1739 >                                K ek;
1740 >                                if (e.hash == h &&
1741 >                                    ((ek = e.key) == key ||
1742 >                                     (ek != null && key.equals(ek)))) {
1743 >                                    val = remappingFunction.apply(key, e.val);
1744 >                                    if (val != null)
1745 >                                        e.val = val;
1746 >                                    else {
1747 >                                        delta = -1;
1748 >                                        Node<K,V> en = e.next;
1749 >                                        if (pred != null)
1750 >                                            pred.next = en;
1751 >                                        else
1752 >                                            setTabAt(tab, i, en);
1753 >                                    }
1754 >                                    break;
1755 >                                }
1756 >                                pred = e;
1757 >                                if ((e = e.next) == null)
1758 >                                    break;
1759 >                            }
1760 >                        }
1761 >                        else if (f instanceof TreeBin) {
1762 >                            binCount = 2;
1763 >                            TreeBin<K,V> t = (TreeBin<K,V>)f;
1764 >                            TreeNode<K,V> r, p;
1765 >                            if ((r = t.root) != null &&
1766 >                                (p = r.findTreeNode(h, key, null)) != null) {
1767 >                                val = remappingFunction.apply(key, p.val);
1768 >                                if (val != null)
1769 >                                    p.val = val;
1770 >                                else {
1771 >                                    delta = -1;
1772 >                                    if (t.removeTreeNode(p))
1773 >                                        setTabAt(tab, i, untreeify(t.first));
1774 >                                }
1775 >                            }
1776 >                        }
1777 >                    }
1778                  }
1779 +                if (binCount != 0)
1780 +                    break;
1781              }
3228            return sb.append(']').toString();
1782          }
1783 +        if (delta != 0)
1784 +            addCount((long)delta, binCount);
1785 +        return val;
1786 +    }
1787  
1788 <        public final boolean containsAll(Collection<?> c) {
1789 <            if (c != this) {
1790 <                for (Iterator<?> it = c.iterator(); it.hasNext();) {
1791 <                    Object e = it.next();
1792 <                    if (e == null || !contains(e))
1793 <                        return false;
1788 >    /**
1789 >     * Attempts to compute a mapping for the specified key and its
1790 >     * current mapped value (or {@code null} if there is no current
1791 >     * mapping). The entire method invocation is performed atomically.
1792 >     * Some attempted update operations on this map by other threads
1793 >     * may be blocked while computation is in progress, so the
1794 >     * computation should be short and simple, and must not attempt to
1795 >     * update any other mappings of this Map.
1796 >     *
1797 >     * @param key key with which the specified value is to be associated
1798 >     * @param remappingFunction the function to compute a value
1799 >     * @return the new value associated with the specified key, or null if none
1800 >     * @throws NullPointerException if the specified key or remappingFunction
1801 >     *         is null
1802 >     * @throws IllegalStateException if the computation detectably
1803 >     *         attempts a recursive update to this map that would
1804 >     *         otherwise never complete
1805 >     * @throws RuntimeException or Error if the remappingFunction does so,
1806 >     *         in which case the mapping is unchanged
1807 >     */
1808 >    public V compute(K key,
1809 >                     BiFun<? super K, ? super V, ? extends V> remappingFunction) {
1810 >        if (key == null || remappingFunction == null)
1811 >            throw new NullPointerException();
1812 >        int h = spread(key.hashCode());
1813 >        V val = null;
1814 >        int delta = 0;
1815 >        int binCount = 0;
1816 >        for (Node<K,V>[] tab = table;;) {
1817 >            Node<K,V> f; int n, i, fh;
1818 >            if (tab == null || (n = tab.length) == 0)
1819 >                tab = initTable();
1820 >            else if ((f = tabAt(tab, i = (n - 1) & h)) == null) {
1821 >                Node<K,V> r = new ReservationNode<K,V>();
1822 >                synchronized (r) {
1823 >                    if (casTabAt(tab, i, null, r)) {
1824 >                        binCount = 1;
1825 >                        Node<K,V> node = null;
1826 >                        try {
1827 >                            if ((val = remappingFunction.apply(key, null)) != null) {
1828 >                                delta = 1;
1829 >                                node = new Node<K,V>(h, key, val, null);
1830 >                            }
1831 >                        } finally {
1832 >                            setTabAt(tab, i, node);
1833 >                        }
1834 >                    }
1835                  }
1836 +                if (binCount != 0)
1837 +                    break;
1838              }
1839 <            return true;
1840 <        }
1841 <
1842 <        public final boolean removeAll(Collection<?> c) {
1843 <            boolean modified = false;
1844 <            for (Iterator<?> it = iterator(); it.hasNext();) {
1845 <                if (c.contains(it.next())) {
1846 <                    it.remove();
1847 <                    modified = true;
1839 >            else if ((fh = f.hash) == MOVED)
1840 >                tab = helpTransfer(tab, f);
1841 >            else {
1842 >                synchronized (f) {
1843 >                    if (tabAt(tab, i) == f) {
1844 >                        if (fh >= 0) {
1845 >                            binCount = 1;
1846 >                            for (Node<K,V> e = f, pred = null;; ++binCount) {
1847 >                                K ek;
1848 >                                if (e.hash == h &&
1849 >                                    ((ek = e.key) == key ||
1850 >                                     (ek != null && key.equals(ek)))) {
1851 >                                    val = remappingFunction.apply(key, e.val);
1852 >                                    if (val != null)
1853 >                                        e.val = val;
1854 >                                    else {
1855 >                                        delta = -1;
1856 >                                        Node<K,V> en = e.next;
1857 >                                        if (pred != null)
1858 >                                            pred.next = en;
1859 >                                        else
1860 >                                            setTabAt(tab, i, en);
1861 >                                    }
1862 >                                    break;
1863 >                                }
1864 >                                pred = e;
1865 >                                if ((e = e.next) == null) {
1866 >                                    val = remappingFunction.apply(key, null);
1867 >                                    if (val != null) {
1868 >                                        delta = 1;
1869 >                                        pred.next =
1870 >                                            new Node<K,V>(h, key, val, null);
1871 >                                    }
1872 >                                    break;
1873 >                                }
1874 >                            }
1875 >                        }
1876 >                        else if (f instanceof TreeBin) {
1877 >                            binCount = 1;
1878 >                            TreeBin<K,V> t = (TreeBin<K,V>)f;
1879 >                            TreeNode<K,V> r, p;
1880 >                            if ((r = t.root) != null)
1881 >                                p = r.findTreeNode(h, key, null);
1882 >                            else
1883 >                                p = null;
1884 >                            V pv = (p == null) ? null : p.val;
1885 >                            val = remappingFunction.apply(key, pv);
1886 >                            if (val != null) {
1887 >                                if (p != null)
1888 >                                    p.val = val;
1889 >                                else {
1890 >                                    delta = 1;
1891 >                                    t.putTreeVal(h, key, val);
1892 >                                }
1893 >                            }
1894 >                            else if (p != null) {
1895 >                                delta = -1;
1896 >                                if (t.removeTreeNode(p))
1897 >                                    setTabAt(tab, i, untreeify(t.first));
1898 >                            }
1899 >                        }
1900 >                    }
1901 >                }
1902 >                if (binCount != 0) {
1903 >                    if (binCount >= TREEIFY_THRESHOLD)
1904 >                        treeifyBin(tab, i);
1905 >                    break;
1906                  }
1907              }
3250            return modified;
1908          }
1909 +        if (delta != 0)
1910 +            addCount((long)delta, binCount);
1911 +        return val;
1912 +    }
1913  
1914 <        public final boolean retainAll(Collection<?> c) {
1915 <            boolean modified = false;
1916 <            for (Iterator<?> it = iterator(); it.hasNext();) {
1917 <                if (!c.contains(it.next())) {
1918 <                    it.remove();
1919 <                    modified = true;
1914 >    /**
1915 >     * If the specified key is not already associated with a
1916 >     * (non-null) value, associates it with the given value.
1917 >     * Otherwise, replaces the value with the results of the given
1918 >     * remapping function, or removes if {@code null}. The entire
1919 >     * method invocation is performed atomically.  Some attempted
1920 >     * update operations on this map by other threads may be blocked
1921 >     * while computation is in progress, so the computation should be
1922 >     * short and simple, and must not attempt to update any other
1923 >     * mappings of this Map.
1924 >     *
1925 >     * @param key key with which the specified value is to be associated
1926 >     * @param value the value to use if absent
1927 >     * @param remappingFunction the function to recompute a value if present
1928 >     * @return the new value associated with the specified key, or null if none
1929 >     * @throws NullPointerException if the specified key or the
1930 >     *         remappingFunction is null
1931 >     * @throws RuntimeException or Error if the remappingFunction does so,
1932 >     *         in which case the mapping is unchanged
1933 >     */
1934 >    public V merge(K key, V value, BiFun<? super V, ? super V, ? extends V> remappingFunction) {
1935 >        if (key == null || value == null || remappingFunction == null)
1936 >            throw new NullPointerException();
1937 >        int h = spread(key.hashCode());
1938 >        V val = null;
1939 >        int delta = 0;
1940 >        int binCount = 0;
1941 >        for (Node<K,V>[] tab = table;;) {
1942 >            Node<K,V> f; int n, i, fh;
1943 >            if (tab == null || (n = tab.length) == 0)
1944 >                tab = initTable();
1945 >            else if ((f = tabAt(tab, i = (n - 1) & h)) == null) {
1946 >                if (casTabAt(tab, i, null, new Node<K,V>(h, key, value, null))) {
1947 >                    delta = 1;
1948 >                    val = value;
1949 >                    break;
1950 >                }
1951 >            }
1952 >            else if ((fh = f.hash) == MOVED)
1953 >                tab = helpTransfer(tab, f);
1954 >            else {
1955 >                synchronized (f) {
1956 >                    if (tabAt(tab, i) == f) {
1957 >                        if (fh >= 0) {
1958 >                            binCount = 1;
1959 >                            for (Node<K,V> e = f, pred = null;; ++binCount) {
1960 >                                K ek;
1961 >                                if (e.hash == h &&
1962 >                                    ((ek = e.key) == key ||
1963 >                                     (ek != null && key.equals(ek)))) {
1964 >                                    val = remappingFunction.apply(e.val, value);
1965 >                                    if (val != null)
1966 >                                        e.val = val;
1967 >                                    else {
1968 >                                        delta = -1;
1969 >                                        Node<K,V> en = e.next;
1970 >                                        if (pred != null)
1971 >                                            pred.next = en;
1972 >                                        else
1973 >                                            setTabAt(tab, i, en);
1974 >                                    }
1975 >                                    break;
1976 >                                }
1977 >                                pred = e;
1978 >                                if ((e = e.next) == null) {
1979 >                                    delta = 1;
1980 >                                    val = value;
1981 >                                    pred.next =
1982 >                                        new Node<K,V>(h, key, val, null);
1983 >                                    break;
1984 >                                }
1985 >                            }
1986 >                        }
1987 >                        else if (f instanceof TreeBin) {
1988 >                            binCount = 2;
1989 >                            TreeBin<K,V> t = (TreeBin<K,V>)f;
1990 >                            TreeNode<K,V> r = t.root;
1991 >                            TreeNode<K,V> p = (r == null) ? null :
1992 >                                r.findTreeNode(h, key, null);
1993 >                            val = (p == null) ? value :
1994 >                                remappingFunction.apply(p.val, value);
1995 >                            if (val != null) {
1996 >                                if (p != null)
1997 >                                    p.val = val;
1998 >                                else {
1999 >                                    delta = 1;
2000 >                                    t.putTreeVal(h, key, val);
2001 >                                }
2002 >                            }
2003 >                            else if (p != null) {
2004 >                                delta = -1;
2005 >                                if (t.removeTreeNode(p))
2006 >                                    setTabAt(tab, i, untreeify(t.first));
2007 >                            }
2008 >                        }
2009 >                    }
2010 >                }
2011 >                if (binCount != 0) {
2012 >                    if (binCount >= TREEIFY_THRESHOLD)
2013 >                        treeifyBin(tab, i);
2014 >                    break;
2015                  }
2016              }
3261            return modified;
2017          }
2018 +        if (delta != 0)
2019 +            addCount((long)delta, binCount);
2020 +        return val;
2021 +    }
2022  
2023 +    // Hashtable legacy methods
2024 +
2025 +    /**
2026 +     * Legacy method testing if some key maps into the specified value
2027 +     * in this table.  This method is identical in functionality to
2028 +     * {@link #containsValue(Object)}, and exists solely to ensure
2029 +     * full compatibility with class {@link java.util.Hashtable},
2030 +     * which supported this method prior to introduction of the
2031 +     * Java Collections framework.
2032 +     *
2033 +     * @param  value a value to search for
2034 +     * @return {@code true} if and only if some key maps to the
2035 +     *         {@code value} argument in this table as
2036 +     *         determined by the {@code equals} method;
2037 +     *         {@code false} otherwise
2038 +     * @throws NullPointerException if the specified value is null
2039 +     */
2040 +    @Deprecated public boolean contains(Object value) {
2041 +        return containsValue(value);
2042      }
2043  
2044 <    static final class KeySet<K,V> extends CHMView<K,V> implements Set<K> {
2045 <        KeySet(ConcurrentHashMapV8<K, V> map)  {
2046 <            super(map);
2047 <        }
2048 <        public final boolean contains(Object o) { return map.containsKey(o); }
2049 <        public final boolean remove(Object o)   { return map.remove(o) != null; }
2050 <        public final Iterator<K> iterator() {
2051 <            return new KeyIterator<K,V>(map);
2052 <        }
2053 <        public final boolean add(K e) {
3276 <            throw new UnsupportedOperationException();
3277 <        }
3278 <        public final boolean addAll(Collection<? extends K> c) {
3279 <            throw new UnsupportedOperationException();
3280 <        }
3281 <        public boolean equals(Object o) {
3282 <            Set<?> c;
3283 <            return ((o instanceof Set) &&
3284 <                    ((c = (Set<?>)o) == this ||
3285 <                     (containsAll(c) && c.containsAll(this))));
3286 <        }
2044 >    /**
2045 >     * Returns an enumeration of the keys in this table.
2046 >     *
2047 >     * @return an enumeration of the keys in this table
2048 >     * @see #keySet()
2049 >     */
2050 >    public Enumeration<K> keys() {
2051 >        Node<K,V>[] t;
2052 >        int f = (t = table) == null ? 0 : t.length;
2053 >        return new KeyIterator<K,V>(t, f, 0, f, this);
2054      }
2055  
2056 +    /**
2057 +     * Returns an enumeration of the values in this table.
2058 +     *
2059 +     * @return an enumeration of the values in this table
2060 +     * @see #values()
2061 +     */
2062 +    public Enumeration<V> elements() {
2063 +        Node<K,V>[] t;
2064 +        int f = (t = table) == null ? 0 : t.length;
2065 +        return new ValueIterator<K,V>(t, f, 0, f, this);
2066 +    }
2067  
2068 <    static final class Values<K,V> extends CHMView<K,V>
3291 <        implements Collection<V> {
3292 <        Values(ConcurrentHashMapV8<K, V> map)   { super(map); }
3293 <        public final boolean contains(Object o) { return map.containsValue(o); }
3294 <        public final boolean remove(Object o) {
3295 <            if (o != null) {
3296 <                Iterator<V> it = new ValueIterator<K,V>(map);
3297 <                while (it.hasNext()) {
3298 <                    if (o.equals(it.next())) {
3299 <                        it.remove();
3300 <                        return true;
3301 <                    }
3302 <                }
3303 <            }
3304 <            return false;
3305 <        }
3306 <        public final Iterator<V> iterator() {
3307 <            return new ValueIterator<K,V>(map);
3308 <        }
3309 <        public final boolean add(V e) {
3310 <            throw new UnsupportedOperationException();
3311 <        }
3312 <        public final boolean addAll(Collection<? extends V> c) {
3313 <            throw new UnsupportedOperationException();
3314 <        }
2068 >    // ConcurrentHashMapV8-only methods
2069  
2070 +    /**
2071 +     * Returns the number of mappings. This method should be used
2072 +     * instead of {@link #size} because a ConcurrentHashMapV8 may
2073 +     * contain more mappings than can be represented as an int. The
2074 +     * value returned is an estimate; the actual count may differ if
2075 +     * there are concurrent insertions or removals.
2076 +     *
2077 +     * @return the number of mappings
2078 +     * @since 1.8
2079 +     */
2080 +    public long mappingCount() {
2081 +        long n = sumCount();
2082 +        return (n < 0L) ? 0L : n; // ignore transient negative values
2083      }
2084  
2085 <    static final class EntrySet<K,V> extends CHMView<K,V>
2086 <        implements Set<Map.Entry<K,V>> {
2087 <        EntrySet(ConcurrentHashMapV8<K, V> map) { super(map); }
2088 <        public final boolean contains(Object o) {
2089 <            Object k, v, r; Map.Entry<?,?> e;
2090 <            return ((o instanceof Map.Entry) &&
2091 <                    (k = (e = (Map.Entry<?,?>)o).getKey()) != null &&
2092 <                    (r = map.get(k)) != null &&
2093 <                    (v = e.getValue()) != null &&
2094 <                    (v == r || v.equals(r)));
3328 <        }
3329 <        public final boolean remove(Object o) {
3330 <            Object k, v; Map.Entry<?,?> e;
3331 <            return ((o instanceof Map.Entry) &&
3332 <                    (k = (e = (Map.Entry<?,?>)o).getKey()) != null &&
3333 <                    (v = e.getValue()) != null &&
3334 <                    map.remove(k, v));
3335 <        }
3336 <        public final Iterator<Map.Entry<K,V>> iterator() {
3337 <            return new EntryIterator<K,V>(map);
3338 <        }
3339 <        public final boolean add(Entry<K,V> e) {
3340 <            throw new UnsupportedOperationException();
3341 <        }
3342 <        public final boolean addAll(Collection<? extends Entry<K,V>> c) {
3343 <            throw new UnsupportedOperationException();
3344 <        }
3345 <        public boolean equals(Object o) {
3346 <            Set<?> c;
3347 <            return ((o instanceof Set) &&
3348 <                    ((c = (Set<?>)o) == this ||
3349 <                     (containsAll(c) && c.containsAll(this))));
3350 <        }
2085 >    /**
2086 >     * Creates a new {@link Set} backed by a ConcurrentHashMapV8
2087 >     * from the given type to {@code Boolean.TRUE}.
2088 >     *
2089 >     * @return the new set
2090 >     * @since 1.8
2091 >     */
2092 >    public static <K> KeySetView<K,Boolean> newKeySet() {
2093 >        return new KeySetView<K,Boolean>
2094 >            (new ConcurrentHashMapV8<K,Boolean>(), Boolean.TRUE);
2095      }
2096  
2097 <    /* ---------------- Serialization Support -------------- */
2097 >    /**
2098 >     * Creates a new {@link Set} backed by a ConcurrentHashMapV8
2099 >     * from the given type to {@code Boolean.TRUE}.
2100 >     *
2101 >     * @param initialCapacity The implementation performs internal
2102 >     * sizing to accommodate this many elements.
2103 >     * @throws IllegalArgumentException if the initial capacity of
2104 >     * elements is negative
2105 >     * @return the new set
2106 >     * @since 1.8
2107 >     */
2108 >    public static <K> KeySetView<K,Boolean> newKeySet(int initialCapacity) {
2109 >        return new KeySetView<K,Boolean>
2110 >            (new ConcurrentHashMapV8<K,Boolean>(initialCapacity), Boolean.TRUE);
2111 >    }
2112  
2113      /**
2114 <     * Stripped-down version of helper class used in previous version,
2115 <     * declared for the sake of serialization compatibility
2114 >     * Returns a {@link Set} view of the keys in this map, using the
2115 >     * given common mapped value for any additions (i.e., {@link
2116 >     * Collection#add} and {@link Collection#addAll(Collection)}).
2117 >     * This is of course only appropriate if it is acceptable to use
2118 >     * the same value for all additions from this view.
2119 >     *
2120 >     * @param mappedValue the mapped value to use for any additions
2121 >     * @return the set view
2122 >     * @throws NullPointerException if the mappedValue is null
2123       */
2124 <    static class Segment<K,V> implements Serializable {
2125 <        private static final long serialVersionUID = 2249069246763182397L;
2126 <        final float loadFactor;
2127 <        Segment(float lf) { this.loadFactor = lf; }
2124 >    public KeySetView<K,V> keySet(V mappedValue) {
2125 >        if (mappedValue == null)
2126 >            throw new NullPointerException();
2127 >        return new KeySetView<K,V>(this, mappedValue);
2128      }
2129  
2130 +    /* ---------------- Special Nodes -------------- */
2131 +
2132      /**
2133 <     * Saves the state of the {@code ConcurrentHashMapV8} instance to a
3367 <     * stream (i.e., serializes it).
3368 <     * @param s the stream
3369 <     * @serialData
3370 <     * the key (Object) and value (Object)
3371 <     * for each key-value mapping, followed by a null pair.
3372 <     * The key-value mappings are emitted in no particular order.
2133 >     * A node inserted at head of bins during transfer operations.
2134       */
2135 <    @SuppressWarnings("unchecked")
2136 <        private void writeObject(java.io.ObjectOutputStream s)
2137 <        throws java.io.IOException {
2138 <        if (segments == null) { // for serialization compatibility
2139 <            segments = (Segment<K,V>[])
2140 <                new Segment<?,?>[DEFAULT_CONCURRENCY_LEVEL];
2141 <            for (int i = 0; i < segments.length; ++i)
2142 <                segments[i] = new Segment<K,V>(LOAD_FACTOR);
2143 <        }
2144 <        s.defaultWriteObject();
2145 <        Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
2146 <        Object v;
2147 <        while ((v = it.advance()) != null) {
2148 <            s.writeObject(it.nextKey);
2149 <            s.writeObject(v);
2135 >    static final class ForwardingNode<K,V> extends Node<K,V> {
2136 >        final Node<K,V>[] nextTable;
2137 >        ForwardingNode(Node<K,V>[] tab) {
2138 >            super(MOVED, null, null, null);
2139 >            this.nextTable = tab;
2140 >        }
2141 >
2142 >        Node<K,V> find(int h, Object k) {
2143 >            Node<K,V> e; int n;
2144 >            Node<K,V>[] tab = nextTable;
2145 >            if (k != null && tab != null && (n = tab.length) > 0 &&
2146 >                (e = tabAt(tab, (n - 1) & h)) != null) {
2147 >                do {
2148 >                    int eh; K ek;
2149 >                    if ((eh = e.hash) == h &&
2150 >                        ((ek = e.key) == k || (ek != null && k.equals(ek))))
2151 >                        return e;
2152 >                    if (eh < 0)
2153 >                        return e.find(h, k);
2154 >                } while ((e = e.next) != null);
2155 >            }
2156 >            return null;
2157          }
3390        s.writeObject(null);
3391        s.writeObject(null);
3392        segments = null; // throw away
2158      }
2159  
2160      /**
2161 <     * Reconstitutes the instance from a stream (that is, deserializes it).
3397 <     * @param s the stream
2161 >     * A place-holder node used in computeIfAbsent and compute
2162       */
2163 <    @SuppressWarnings("unchecked")
2164 <        private void readObject(java.io.ObjectInputStream s)
2165 <        throws java.io.IOException, ClassNotFoundException {
2166 <        s.defaultReadObject();
3403 <        this.segments = null; // unneeded
3404 <        // initialize transient final field
3405 <        UNSAFE.putObjectVolatile(this, counterOffset, new LongAdder());
2163 >    static final class ReservationNode<K,V> extends Node<K,V> {
2164 >        ReservationNode() {
2165 >            super(RESERVED, null, null, null);
2166 >        }
2167  
2168 <        // Create all nodes, then place in table once size is known
2169 <        long size = 0L;
3409 <        Node p = null;
3410 <        for (;;) {
3411 <            K k = (K) s.readObject();
3412 <            V v = (V) s.readObject();
3413 <            if (k != null && v != null) {
3414 <                int h = spread(k.hashCode());
3415 <                p = new Node(h, k, v, p);
3416 <                ++size;
3417 <            }
3418 <            else
3419 <                break;
2168 >        Node<K,V> find(int h, Object k) {
2169 >            return null;
2170          }
2171 <        if (p != null) {
2172 <            boolean init = false;
2173 <            int n;
2174 <            if (size >= (long)(MAXIMUM_CAPACITY >>> 1))
2175 <                n = MAXIMUM_CAPACITY;
2176 <            else {
2177 <                int sz = (int)size;
2178 <                n = tableSizeFor(sz + (sz >>> 1) + 1);
2179 <            }
2180 <            int sc = sizeCtl;
2181 <            boolean collide = false;
2182 <            if (n > sc &&
2183 <                UNSAFE.compareAndSwapInt(this, sizeCtlOffset, sc, -1)) {
2171 >    }
2172 >
2173 >    /* ---------------- Table Initialization and Resizing -------------- */
2174 >
2175 >    /**
2176 >     * Initializes table, using the size recorded in sizeCtl.
2177 >     */
2178 >    private final Node<K,V>[] initTable() {
2179 >        Node<K,V>[] tab; int sc;
2180 >        while ((tab = table) == null || tab.length == 0) {
2181 >            if ((sc = sizeCtl) < 0)
2182 >                Thread.yield(); // lost initialization race; just spin
2183 >            else if (U.compareAndSwapInt(this, SIZECTL, sc, -1)) {
2184                  try {
2185 <                    if (table == null) {
2186 <                        init = true;
2187 <                        Node[] tab = new Node[n];
2188 <                        int mask = n - 1;
2189 <                        while (p != null) {
3440 <                            int j = p.hash & mask;
3441 <                            Node next = p.next;
3442 <                            Node q = p.next = tabAt(tab, j);
3443 <                            setTabAt(tab, j, p);
3444 <                            if (!collide && q != null && q.hash == p.hash)
3445 <                                collide = true;
3446 <                            p = next;
3447 <                        }
3448 <                        table = tab;
3449 <                        counter.add(size);
2185 >                    if ((tab = table) == null || tab.length == 0) {
2186 >                        int n = (sc > 0) ? sc : DEFAULT_CAPACITY;
2187 >                        @SuppressWarnings({"rawtypes","unchecked"})
2188 >                            Node<K,V>[] nt = (Node<K,V>[])new Node[n];
2189 >                        table = tab = nt;
2190                          sc = n - (n >>> 2);
2191                      }
2192                  } finally {
2193                      sizeCtl = sc;
2194                  }
2195 <                if (collide) { // rescan and convert to TreeBins
3456 <                    Node[] tab = table;
3457 <                    for (int i = 0; i < tab.length; ++i) {
3458 <                        int c = 0;
3459 <                        for (Node e = tabAt(tab, i); e != null; e = e.next) {
3460 <                            if (++c > TREE_THRESHOLD &&
3461 <                                (e.key instanceof Comparable)) {
3462 <                                replaceWithTreeBin(tab, i, e.key);
3463 <                                break;
3464 <                            }
3465 <                        }
3466 <                    }
3467 <                }
2195 >                break;
2196              }
2197 <            if (!init) { // Can only happen if unsafely published.
2198 <                while (p != null) {
2199 <                    internalPut(p.key, p.val);
2200 <                    p = p.next;
2197 >        }
2198 >        return tab;
2199 >    }
2200 >
2201 >    /**
2202 >     * Adds to count, and if table is too small and not already
2203 >     * resizing, initiates transfer. If already resizing, helps
2204 >     * perform transfer if work is available.  Rechecks occupancy
2205 >     * after a transfer to see if another resize is already needed
2206 >     * because resizings are lagging additions.
2207 >     *
2208 >     * @param x the count to add
2209 >     * @param check if <0, don't check resize, if <= 1 only check if uncontended
2210 >     */
2211 >    private final void addCount(long x, int check) {
2212 >        CounterCell[] as; long b, s;
2213 >        if ((as = counterCells) != null ||
2214 >            !U.compareAndSwapLong(this, BASECOUNT, b = baseCount, s = b + x)) {
2215 >            CounterHashCode hc; CounterCell a; long v; int m;
2216 >            boolean uncontended = true;
2217 >            if ((hc = threadCounterHashCode.get()) == null ||
2218 >                as == null || (m = as.length - 1) < 0 ||
2219 >                (a = as[m & hc.code]) == null ||
2220 >                !(uncontended =
2221 >                  U.compareAndSwapLong(a, CELLVALUE, v = a.value, v + x))) {
2222 >                fullAddCount(x, hc, uncontended);
2223 >                return;
2224 >            }
2225 >            if (check <= 1)
2226 >                return;
2227 >            s = sumCount();
2228 >        }
2229 >        if (check >= 0) {
2230 >            Node<K,V>[] tab, nt; int sc;
2231 >            while (s >= (long)(sc = sizeCtl) && (tab = table) != null &&
2232 >                   tab.length < MAXIMUM_CAPACITY) {
2233 >                if (sc < 0) {
2234 >                    if (sc == -1 || transferIndex <= transferOrigin ||
2235 >                        (nt = nextTable) == null)
2236 >                        break;
2237 >                    if (U.compareAndSwapInt(this, SIZECTL, sc, sc - 1))
2238 >                        transfer(tab, nt);
2239                  }
2240 +                else if (U.compareAndSwapInt(this, SIZECTL, sc, -2))
2241 +                    transfer(tab, null);
2242 +                s = sumCount();
2243              }
2244          }
2245      }
2246  
2247 +    /**
2248 +     * Helps transfer if a resize is in progress.
2249 +     */
2250 +    final Node<K,V>[] helpTransfer(Node<K,V>[] tab, Node<K,V> f) {
2251 +        Node<K,V>[] nextTab; int sc;
2252 +        if ((f instanceof ForwardingNode) &&
2253 +            (nextTab = ((ForwardingNode<K,V>)f).nextTable) != null) {
2254 +            if (nextTab == nextTable && tab == table &&
2255 +                transferIndex > transferOrigin && (sc = sizeCtl) < -1 &&
2256 +                U.compareAndSwapInt(this, SIZECTL, sc, sc - 1))
2257 +                transfer(tab, nextTab);
2258 +            return nextTab;
2259 +        }
2260 +        return table;
2261 +    }
2262  
2263 <    // -------------------------------------------------------
2263 >    /**
2264 >     * Tries to presize table to accommodate the given number of elements.
2265 >     *
2266 >     * @param size number of elements (doesn't need to be perfectly accurate)
2267 >     */
2268 >    private final void tryPresize(int size) {
2269 >        int c = (size >= (MAXIMUM_CAPACITY >>> 1)) ? MAXIMUM_CAPACITY :
2270 >            tableSizeFor(size + (size >>> 1) + 1);
2271 >        int sc;
2272 >        while ((sc = sizeCtl) >= 0) {
2273 >            Node<K,V>[] tab = table; int n;
2274 >            if (tab == null || (n = tab.length) == 0) {
2275 >                n = (sc > c) ? sc : c;
2276 >                if (U.compareAndSwapInt(this, SIZECTL, sc, -1)) {
2277 >                    try {
2278 >                        if (table == tab) {
2279 >                            @SuppressWarnings({"rawtypes","unchecked"})
2280 >                                Node<K,V>[] nt = (Node<K,V>[])new Node[n];
2281 >                            table = nt;
2282 >                            sc = n - (n >>> 2);
2283 >                        }
2284 >                    } finally {
2285 >                        sizeCtl = sc;
2286 >                    }
2287 >                }
2288 >            }
2289 >            else if (c <= sc || n >= MAXIMUM_CAPACITY)
2290 >                break;
2291 >            else if (tab == table &&
2292 >                     U.compareAndSwapInt(this, SIZECTL, sc, -2))
2293 >                transfer(tab, null);
2294 >        }
2295 >    }
2296  
2297 <    // Sams
2298 <    /** Interface describing a void action of one argument */
2299 <    public interface Action<A> { void apply(A a); }
2300 <    /** Interface describing a void action of two arguments */
2301 <    public interface BiAction<A,B> { void apply(A a, B b); }
2302 <    /** Interface describing a function of one argument */
2303 <    public interface Fun<A,T> { T apply(A a); }
2304 <    /** Interface describing a function of two arguments */
2305 <    public interface BiFun<A,B,T> { T apply(A a, B b); }
2306 <    /** Interface describing a function of no arguments */
2307 <    public interface Generator<T> { T apply(); }
2308 <    /** Interface describing a function mapping its argument to a double */
2309 <    public interface ObjectToDouble<A> { double apply(A a); }
2310 <    /** Interface describing a function mapping its argument to a long */
2311 <    public interface ObjectToLong<A> { long apply(A a); }
2312 <    /** Interface describing a function mapping its argument to an int */
2313 <    public interface ObjectToInt<A> {int apply(A a); }
2314 <    /** Interface describing a function mapping two arguments to a double */
2315 <    public interface ObjectByObjectToDouble<A,B> { double apply(A a, B b); }
2316 <    /** Interface describing a function mapping two arguments to a long */
2317 <    public interface ObjectByObjectToLong<A,B> { long apply(A a, B b); }
2318 <    /** Interface describing a function mapping two arguments to an int */
2319 <    public interface ObjectByObjectToInt<A,B> {int apply(A a, B b); }
2320 <    /** Interface describing a function mapping a double to a double */
2321 <    public interface DoubleToDouble { double apply(double a); }
2322 <    /** Interface describing a function mapping a long to a long */
2323 <    public interface LongToLong { long apply(long a); }
2324 <    /** Interface describing a function mapping an int to an int */
2325 <    public interface IntToInt { int apply(int a); }
2326 <    /** Interface describing a function mapping two doubles to a double */
2327 <    public interface DoubleByDoubleToDouble { double apply(double a, double b); }
2328 <    /** Interface describing a function mapping two longs to a long */
2329 <    public interface LongByLongToLong { long apply(long a, long b); }
2330 <    /** Interface describing a function mapping two ints to an int */
2331 <    public interface IntByIntToInt { int apply(int a, int b); }
2297 >    /**
2298 >     * Moves and/or copies the nodes in each bin to new table. See
2299 >     * above for explanation.
2300 >     */
2301 >    private final void transfer(Node<K,V>[] tab, Node<K,V>[] nextTab) {
2302 >        int n = tab.length, stride;
2303 >        if ((stride = (NCPU > 1) ? (n >>> 3) / NCPU : n) < MIN_TRANSFER_STRIDE)
2304 >            stride = MIN_TRANSFER_STRIDE; // subdivide range
2305 >        if (nextTab == null) {            // initiating
2306 >            try {
2307 >                @SuppressWarnings({"rawtypes","unchecked"})
2308 >                    Node<K,V>[] nt = (Node<K,V>[])new Node[n << 1];
2309 >                nextTab = nt;
2310 >            } catch (Throwable ex) {      // try to cope with OOME
2311 >                sizeCtl = Integer.MAX_VALUE;
2312 >                return;
2313 >            }
2314 >            nextTable = nextTab;
2315 >            transferOrigin = n;
2316 >            transferIndex = n;
2317 >            ForwardingNode<K,V> rev = new ForwardingNode<K,V>(tab);
2318 >            for (int k = n; k > 0;) {    // progressively reveal ready slots
2319 >                int nextk = (k > stride) ? k - stride : 0;
2320 >                for (int m = nextk; m < k; ++m)
2321 >                    nextTab[m] = rev;
2322 >                for (int m = n + nextk; m < n + k; ++m)
2323 >                    nextTab[m] = rev;
2324 >                U.putOrderedInt(this, TRANSFERORIGIN, k = nextk);
2325 >            }
2326 >        }
2327 >        int nextn = nextTab.length;
2328 >        ForwardingNode<K,V> fwd = new ForwardingNode<K,V>(nextTab);
2329 >        boolean advance = true;
2330 >        for (int i = 0, bound = 0;;) {
2331 >            int nextIndex, nextBound, fh; Node<K,V> f;
2332 >            while (advance) {
2333 >                if (--i >= bound)
2334 >                    advance = false;
2335 >                else if ((nextIndex = transferIndex) <= transferOrigin) {
2336 >                    i = -1;
2337 >                    advance = false;
2338 >                }
2339 >                else if (U.compareAndSwapInt
2340 >                         (this, TRANSFERINDEX, nextIndex,
2341 >                          nextBound = (nextIndex > stride ?
2342 >                                       nextIndex - stride : 0))) {
2343 >                    bound = nextBound;
2344 >                    i = nextIndex - 1;
2345 >                    advance = false;
2346 >                }
2347 >            }
2348 >            if (i < 0 || i >= n || i + n >= nextn) {
2349 >                for (int sc;;) {
2350 >                    if (U.compareAndSwapInt(this, SIZECTL, sc = sizeCtl, ++sc)) {
2351 >                        if (sc == -1) {
2352 >                            nextTable = null;
2353 >                            table = nextTab;
2354 >                            sizeCtl = (n << 1) - (n >>> 1);
2355 >                        }
2356 >                        return;
2357 >                    }
2358 >                }
2359 >            }
2360 >            else if ((f = tabAt(tab, i)) == null) {
2361 >                if (casTabAt(tab, i, null, fwd)) {
2362 >                    setTabAt(nextTab, i, null);
2363 >                    setTabAt(nextTab, i + n, null);
2364 >                    advance = true;
2365 >                }
2366 >            }
2367 >            else if ((fh = f.hash) == MOVED)
2368 >                advance = true; // already processed
2369 >            else {
2370 >                synchronized (f) {
2371 >                    if (tabAt(tab, i) == f) {
2372 >                        Node<K,V> ln, hn;
2373 >                        if (fh >= 0) {
2374 >                            int runBit = fh & n;
2375 >                            Node<K,V> lastRun = f;
2376 >                            for (Node<K,V> p = f.next; p != null; p = p.next) {
2377 >                                int b = p.hash & n;
2378 >                                if (b != runBit) {
2379 >                                    runBit = b;
2380 >                                    lastRun = p;
2381 >                                }
2382 >                            }
2383 >                            if (runBit == 0) {
2384 >                                ln = lastRun;
2385 >                                hn = null;
2386 >                            }
2387 >                            else {
2388 >                                hn = lastRun;
2389 >                                ln = null;
2390 >                            }
2391 >                            for (Node<K,V> p = f; p != lastRun; p = p.next) {
2392 >                                int ph = p.hash; K pk = p.key; V pv = p.val;
2393 >                                if ((ph & n) == 0)
2394 >                                    ln = new Node<K,V>(ph, pk, pv, ln);
2395 >                                else
2396 >                                    hn = new Node<K,V>(ph, pk, pv, hn);
2397 >                            }
2398 >                        }
2399 >                        else if (f instanceof TreeBin) {
2400 >                            TreeBin<K,V> t = (TreeBin<K,V>)f;
2401 >                            TreeNode<K,V> lo = null, loTail = null;
2402 >                            TreeNode<K,V> hi = null, hiTail = null;
2403 >                            int lc = 0, hc = 0;
2404 >                            for (Node<K,V> e = t.first; e != null; e = e.next) {
2405 >                                int h = e.hash;
2406 >                                TreeNode<K,V> p = new TreeNode<K,V>
2407 >                                    (h, e.key, e.val, null, null);
2408 >                                if ((h & n) == 0) {
2409 >                                    if ((p.prev = loTail) == null)
2410 >                                        lo = p;
2411 >                                    else
2412 >                                        loTail.next = p;
2413 >                                    loTail = p;
2414 >                                    ++lc;
2415 >                                }
2416 >                                else {
2417 >                                    if ((p.prev = hiTail) == null)
2418 >                                        hi = p;
2419 >                                    else
2420 >                                        hiTail.next = p;
2421 >                                    hiTail = p;
2422 >                                    ++hc;
2423 >                                }
2424 >                            }
2425 >                            ln = (lc <= UNTREEIFY_THRESHOLD ?  untreeify(lo) :
2426 >                                  (hc != 0) ? new TreeBin<K,V>(lo) : t);
2427 >                            hn = (hc <= UNTREEIFY_THRESHOLD ? untreeify(hi) :
2428 >                                  (lc != 0) ? new TreeBin<K,V>(hi) : t);
2429 >                        }
2430 >                        else
2431 >                            ln = hn = null;
2432 >                        setTabAt(nextTab, i, ln);
2433 >                        setTabAt(nextTab, i + n, hn);
2434 >                        setTabAt(tab, i, fwd);
2435 >                        advance = true;
2436 >                    }
2437 >                }
2438 >            }
2439 >        }
2440 >    }
2441  
2442 +    /* ---------------- Conversion from/to TreeBins -------------- */
2443  
2444 <    // -------------------------------------------------------
2444 >    /**
2445 >     * Replaces all linked nodes in bin at given index unless table is
2446 >     * too small, in which case resizes instead.
2447 >     */
2448 >    private final void treeifyBin(Node<K,V>[] tab, int index) {
2449 >        Node<K,V> b; int n, sc;
2450 >        if (tab != null) {
2451 >            if ((n = tab.length) < MIN_TREEIFY_CAPACITY) {
2452 >                if (tab == table && (sc = sizeCtl) >= 0 &&
2453 >                    U.compareAndSwapInt(this, SIZECTL, sc, -2))
2454 >                    transfer(tab, null);
2455 >            }
2456 >            else if ((b = tabAt(tab, index)) != null) {
2457 >                synchronized (b) {
2458 >                    if (tabAt(tab, index) == b) {
2459 >                        TreeNode<K,V> hd = null, tl = null;
2460 >                        for (Node<K,V> e = b; e != null; e = e.next) {
2461 >                            TreeNode<K,V> p =
2462 >                                new TreeNode<K,V>(e.hash, e.key, e.val,
2463 >                                                  null, null);
2464 >                            if ((p.prev = tl) == null)
2465 >                                hd = p;
2466 >                            else
2467 >                                tl.next = p;
2468 >                            tl = p;
2469 >                        }
2470 >                        setTabAt(tab, index, new TreeBin<K,V>(hd));
2471 >                    }
2472 >                }
2473 >            }
2474 >        }
2475 >    }
2476  
2477      /**
2478 <     * Returns an extended {@link Parallel} view of this map using the
3522 <     * given executor for bulk parallel operations.
3523 <     *
3524 <     * @param executor the executor
3525 <     * @return a parallel view
2478 >     * Returns a list on non-TreeNodes replacing those in given list
2479       */
2480 <    public Parallel parallel(ForkJoinPool executor)  {
2481 <        return new Parallel(executor);
2480 >    static <K,V> Node<K,V> untreeify(Node<K,V> b) {
2481 >        Node<K,V> hd = null, tl = null;
2482 >        for (Node<K,V> q = b; q != null; q = q.next) {
2483 >            Node<K,V> p = new Node<K,V>(q.hash, q.key, q.val, null);
2484 >            if (tl == null)
2485 >                hd = p;
2486 >            else
2487 >                tl.next = p;
2488 >            tl = p;
2489 >        }
2490 >        return hd;
2491      }
2492  
2493 +    /* ---------------- TreeNodes -------------- */
2494 +
2495      /**
2496 <     * An extended view of a ConcurrentHashMap supporting bulk
3533 <     * parallel operations. These operations are designed to be
3534 <     * safely, and often sensibly, applied even with maps that are
3535 <     * being concurrently updated by other threads; for example, when
3536 <     * computing a snapshot summary of the values in a shared
3537 <     * registry.  There are three kinds of operation, each with four
3538 <     * forms, accepting functions with Keys, Values, Entries, and
3539 <     * (Key, Value) arguments and/or return values. Because the
3540 <     * elements of a ConcurrentHashMap are not ordered in any
3541 <     * particular way, and may be processed in different orders in
3542 <     * different parallel executions, the correctness of supplied
3543 <     * functions should not depend on any ordering, or on any other
3544 <     * objects or values that may transiently change while computation
3545 <     * is in progress; and except for forEach actions, should ideally
3546 <     * be side-effect-free.
3547 <     *
3548 <     * <ul>
3549 <     * <li> forEach: Perform a given action on each element.
3550 <     * A variant form applies a given transformation on each element
3551 <     * before performing the action.</li>
3552 <     *
3553 <     * <li> search: Return the first available non-null result of
3554 <     * applying a given function on each element; skipping further
3555 <     * search when a result is found.</li>
3556 <     *
3557 <     * <li> reduce: Accumulate each element.  The supplied reduction
3558 <     * function cannot rely on ordering (more formally, it should be
3559 <     * both associative and commutative).  There are five variants:
3560 <     *
3561 <     * <ul>
3562 <     *
3563 <     * <li> Plain reductions. (There is not a form of this method for
3564 <     * (key, value) function arguments since there is no corresponding
3565 <     * return type.)</li>
3566 <     *
3567 <     * <li> Mapped reductions that accumulate the results of a given
3568 <     * function applied to each element.</li>
3569 <     *
3570 <     * <li> Reductions to scalar doubles, longs, and ints, using a
3571 <     * given basis value.</li>
3572 <     *
3573 <     * </li>
3574 <     * </ul>
3575 <     * </ul>
3576 <     *
3577 <     * <p>The concurrency properties of the bulk operations follow
3578 <     * from those of ConcurrentHashMap: Any non-null result returned
3579 <     * from {@code get(key)} and related access methods bears a
3580 <     * happens-before relation with the associated insertion or
3581 <     * update.  The result of any bulk operation reflects the
3582 <     * composition of these per-element relations (but is not
3583 <     * necessarily atomic with respect to the map as a whole unless it
3584 <     * is somehow known to be quiescent).  Conversely, because keys
3585 <     * and values in the map are never null, null serves as a reliable
3586 <     * atomic indicator of the current lack of any result.  To
3587 <     * maintain this property, null serves as an implicit basis for
3588 <     * all non-scalar reduction operations. For the double, long, and
3589 <     * int versions, the basis should be one that, when combined with
3590 <     * any other value, returns that other value (more formally, it
3591 <     * should be the identity element for the reduction). Most common
3592 <     * reductions have these properties; for example, computing a sum
3593 <     * with basis 0 or a minimum with basis MAX_VALUE.
3594 <     *
3595 <     * <p>Search and transformation functions provided as arguments
3596 <     * should similarly return null to indicate the lack of any result
3597 <     * (in which case it is not used). In the case of mapped
3598 <     * reductions, this also enables transformations to serve as
3599 <     * filters, returning null (or, in the case of primitive
3600 <     * specializations, the identity basis) if the element should not
3601 <     * be combined. You can create compound transformations and
3602 <     * filterings by composing them yourself under this "null means
3603 <     * there is nothing there now" rule before using them in search or
3604 <     * reduce operations.
3605 <     *
3606 <     * <p>Methods accepting and/or returning Entry arguments maintain
3607 <     * key-value associations. They may be useful for example when
3608 <     * finding the key for the greatest value. Note that "plain" Entry
3609 <     * arguments can be supplied using {@code new
3610 <     * AbstractMap.SimpleEntry(k,v)}.
3611 <     *
3612 <     * <p> Bulk operations may complete abruptly, throwing an
3613 <     * exception encountered in the application of a supplied
3614 <     * function. Bear in mind when handling such exceptions that other
3615 <     * concurrently executing functions could also have thrown
3616 <     * exceptions, or would have done so if the first exception had
3617 <     * not occurred.
3618 <     *
3619 <     * <p>Parallel speedups compared to sequential processing are
3620 <     * common but not guaranteed.  Operations involving brief
3621 <     * functions on small maps may execute more slowly than sequential
3622 <     * loops if the underlying work to parallelize the computation is
3623 <     * more expensive than the computation itself. Similarly,
3624 <     * parallelization may not lead to much actual parallelism if all
3625 <     * processors are busy performing unrelated tasks.
3626 <     *
3627 <     * <p> All arguments to all task methods must be non-null.
3628 <     *
3629 <     * <p><em>jsr166e note: During transition, this class
3630 <     * uses nested functional interfaces with different names but the
3631 <     * same forms as those expected for JDK8.<em>
2496 >     * Nodes for use in TreeBins
2497       */
2498 <    public class Parallel {
2499 <        final ForkJoinPool fjp;
2498 >    static final class TreeNode<K,V> extends Node<K,V> {
2499 >        TreeNode<K,V> parent;  // red-black tree links
2500 >        TreeNode<K,V> left;
2501 >        TreeNode<K,V> right;
2502 >        TreeNode<K,V> prev;    // needed to unlink next upon deletion
2503 >        boolean red;
2504  
2505 <        /**
2506 <         * Returns an extended view of this map using the given
2507 <         * executor for bulk parallel operations.
2508 <         *
3640 <         * @param executor the executor
3641 <         */
3642 <        public Parallel(ForkJoinPool executor)  {
3643 <            this.fjp = executor;
2505 >        TreeNode(int hash, K key, V val, Node<K,V> next,
2506 >                 TreeNode<K,V> parent) {
2507 >            super(hash, key, val, next);
2508 >            this.parent = parent;
2509          }
2510  
2511 <        /**
2512 <         * Performs the given action for each (key, value).
3648 <         *
3649 <         * @param action the action
3650 <         */
3651 <        public void forEach(BiAction<K,V> action) {
3652 <            fjp.invoke(ForkJoinTasks.forEach
3653 <                       (ConcurrentHashMapV8.this, action));
2511 >        Node<K,V> find(int h, Object k) {
2512 >            return findTreeNode(h, k, null);
2513          }
2514  
2515          /**
2516 <         * Performs the given action for each non-null transformation
2517 <         * of each (key, value).
3659 <         *
3660 <         * @param transformer a function returning the transformation
3661 <         * for an element, or null of there is no transformation (in
3662 <         * which case the action is not applied).
3663 <         * @param action the action
2516 >         * Returns the TreeNode (or null if not found) for the given key
2517 >         * starting at given root.
2518           */
2519 <        public <U> void forEach(BiFun<? super K, ? super V, ? extends U> transformer,
2520 <                                Action<U> action) {
2521 <            fjp.invoke(ForkJoinTasks.forEach
2522 <                       (ConcurrentHashMapV8.this, transformer, action));
2519 >        final TreeNode<K,V> findTreeNode(int h, Object k, Class<?> kc) {
2520 >            if (k != null) {
2521 >                TreeNode<K,V> p = this;
2522 >                do  {
2523 >                    int ph, dir; K pk; TreeNode<K,V> q;
2524 >                    TreeNode<K,V> pl = p.left, pr = p.right;
2525 >                    if ((ph = p.hash) > h)
2526 >                        p = pl;
2527 >                    else if (ph < h)
2528 >                        p = pr;
2529 >                    else if ((pk = p.key) == k || (pk != null && k.equals(pk)))
2530 >                        return p;
2531 >                    else if (pl == null && pr == null)
2532 >                        break;
2533 >                    else if ((kc != null ||
2534 >                              (kc = comparableClassFor(k)) != null) &&
2535 >                             (dir = compareComparables(kc, k, pk)) != 0)
2536 >                        p = (dir < 0) ? pl : pr;
2537 >                    else if (pl == null)
2538 >                        p = pr;
2539 >                    else if (pr == null ||
2540 >                             (q = pr.findTreeNode(h, k, kc)) == null)
2541 >                        p = pl;
2542 >                    else
2543 >                        return q;
2544 >                } while (p != null);
2545 >            }
2546 >            return null;
2547          }
2548 +    }
2549  
2550 <        /**
3672 <         * Returns a non-null result from applying the given search
3673 <         * function on each (key, value), or null if none.  Upon
3674 <         * success, further element processing is suppressed and the
3675 <         * results of any other parallel invocations of the search
3676 <         * function are ignored.
3677 <         *
3678 <         * @param searchFunction a function returning a non-null
3679 <         * result on success, else null
3680 <         * @return a non-null result from applying the given search
3681 <         * function on each (key, value), or null if none
3682 <         */
3683 <        public <U> U search(BiFun<? super K, ? super V, ? extends U> searchFunction) {
3684 <            return fjp.invoke(ForkJoinTasks.search
3685 <                              (ConcurrentHashMapV8.this, searchFunction));
3686 <        }
2550 >    /* ---------------- TreeBins -------------- */
2551  
2552 <        /**
2553 <         * Returns the result of accumulating the given transformation
2554 <         * of all (key, value) pairs using the given reducer to
2555 <         * combine values, or null if none.
2556 <         *
2557 <         * @param transformer a function returning the transformation
2558 <         * for an element, or null of there is no transformation (in
2559 <         * which case it is not combined).
2560 <         * @param reducer a commutative associative combining function
2561 <         * @return the result of accumulating the given transformation
2562 <         * of all (key, value) pairs
2563 <         */
2564 <        public <U> U reduce(BiFun<? super K, ? super V, ? extends U> transformer,
2565 <                            BiFun<? super U, ? super U, ? extends U> reducer) {
2566 <            return fjp.invoke(ForkJoinTasks.reduce
2567 <                              (ConcurrentHashMapV8.this, transformer, reducer));
2552 >    /**
2553 >     * TreeNodes used at the heads of bins. TreeBins do not hold user
2554 >     * keys or values, but instead point to list of TreeNodes and
2555 >     * their root. They also maintain a parasitic read-write lock
2556 >     * forcing writers (who hold bin lock) to wait for readers (who do
2557 >     * not) to complete before tree restructuring operations.
2558 >     */
2559 >    static final class TreeBin<K,V> extends Node<K,V> {
2560 >        TreeNode<K,V> root;
2561 >        volatile TreeNode<K,V> first;
2562 >        volatile Thread waiter;
2563 >        volatile int lockState;
2564 >        // values for lockState
2565 >        static final int WRITER = 1; // set while holding write lock
2566 >        static final int WAITER = 2; // set when waiting for write lock
2567 >        static final int READER = 4; // increment value for setting read lock
2568 >
2569 >        /**
2570 >         * Creates bin with initial set of nodes headed by b.
2571 >         */
2572 >        TreeBin(TreeNode<K,V> b) {
2573 >            super(TREEBIN, null, null, null);
2574 >            this.first = b;
2575 >            TreeNode<K,V> r = null;
2576 >            for (TreeNode<K,V> x = b, next; x != null; x = next) {
2577 >                next = (TreeNode<K,V>)x.next;
2578 >                x.left = x.right = null;
2579 >                if (r == null) {
2580 >                    x.parent = null;
2581 >                    x.red = false;
2582 >                    r = x;
2583 >                }
2584 >                else {
2585 >                    Object key = x.key;
2586 >                    int hash = x.hash;
2587 >                    Class<?> kc = null;
2588 >                    for (TreeNode<K,V> p = r;;) {
2589 >                        int dir, ph;
2590 >                        if ((ph = p.hash) > hash)
2591 >                            dir = -1;
2592 >                        else if (ph < hash)
2593 >                            dir = 1;
2594 >                        else if ((kc != null ||
2595 >                                  (kc = comparableClassFor(key)) != null))
2596 >                            dir = compareComparables(kc, key, p.key);
2597 >                        else
2598 >                            dir = 0;
2599 >                        TreeNode<K,V> xp = p;
2600 >                        if ((p = (dir <= 0) ? p.left : p.right) == null) {
2601 >                            x.parent = xp;
2602 >                            if (dir <= 0)
2603 >                                xp.left = x;
2604 >                            else
2605 >                                xp.right = x;
2606 >                            r = balanceInsertion(r, x);
2607 >                            break;
2608 >                        }
2609 >                    }
2610 >                }
2611 >            }
2612 >            this.root = r;
2613          }
2614  
2615          /**
2616 <         * Returns the result of accumulating the given transformation
3708 <         * of all (key, value) pairs using the given reducer to
3709 <         * combine values, and the given basis as an identity value.
3710 <         *
3711 <         * @param transformer a function returning the transformation
3712 <         * for an element
3713 <         * @param basis the identity (initial default value) for the reduction
3714 <         * @param reducer a commutative associative combining function
3715 <         * @return the result of accumulating the given transformation
3716 <         * of all (key, value) pairs
2616 >         * Acquires write lock for tree restructuring
2617           */
2618 <        public double reduceToDouble(ObjectByObjectToDouble<? super K, ? super V> transformer,
2619 <                                     double basis,
2620 <                                     DoubleByDoubleToDouble reducer) {
3721 <            return fjp.invoke(ForkJoinTasks.reduceToDouble
3722 <                              (ConcurrentHashMapV8.this, transformer, basis, reducer));
2618 >        private final void lockRoot() {
2619 >            if (!U.compareAndSwapInt(this, LOCKSTATE, 0, WRITER))
2620 >                contendedLock(); // offload to separate method
2621          }
2622  
2623          /**
2624 <         * Returns the result of accumulating the given transformation
3727 <         * of all (key, value) pairs using the given reducer to
3728 <         * combine values, and the given basis as an identity value.
3729 <         *
3730 <         * @param transformer a function returning the transformation
3731 <         * for an element
3732 <         * @param basis the identity (initial default value) for the reduction
3733 <         * @param reducer a commutative associative combining function
3734 <         * @return the result of accumulating the given transformation
3735 <         * of all (key, value) pairs
2624 >         * Releases write lock for tree restructuring
2625           */
2626 <        public long reduceToLong(ObjectByObjectToLong<? super K, ? super V> transformer,
2627 <                                 long basis,
3739 <                                 LongByLongToLong reducer) {
3740 <            return fjp.invoke(ForkJoinTasks.reduceToLong
3741 <                              (ConcurrentHashMapV8.this, transformer, basis, reducer));
2626 >        private final void unlockRoot() {
2627 >            lockState = 0;
2628          }
2629  
2630          /**
2631 <         * Returns the result of accumulating the given transformation
3746 <         * of all (key, value) pairs using the given reducer to
3747 <         * combine values, and the given basis as an identity value.
3748 <         *
3749 <         * @param transformer a function returning the transformation
3750 <         * for an element
3751 <         * @param basis the identity (initial default value) for the reduction
3752 <         * @param reducer a commutative associative combining function
3753 <         * @return the result of accumulating the given transformation
3754 <         * of all (key, value) pairs
2631 >         * Possibly blocks awaiting root lock
2632           */
2633 <        public int reduceToInt(ObjectByObjectToInt<? super K, ? super V> transformer,
2634 <                               int basis,
2635 <                               IntByIntToInt reducer) {
2636 <            return fjp.invoke(ForkJoinTasks.reduceToInt
2637 <                              (ConcurrentHashMapV8.this, transformer, basis, reducer));
2633 >        private final void contendedLock() {
2634 >            boolean waiting = false;
2635 >            for (int s;;) {
2636 >                if (((s = lockState) & WRITER) == 0) {
2637 >                    if (U.compareAndSwapInt(this, LOCKSTATE, s, WRITER)) {
2638 >                        if (waiting)
2639 >                            waiter = null;
2640 >                        return;
2641 >                    }
2642 >                }
2643 >                else if ((s | WAITER) == 0) {
2644 >                    if (U.compareAndSwapInt(this, LOCKSTATE, s, s | WAITER)) {
2645 >                        waiting = true;
2646 >                        waiter = Thread.currentThread();
2647 >                    }
2648 >                }
2649 >                else if (waiting)
2650 >                    LockSupport.park(this);
2651 >            }
2652          }
2653  
2654          /**
2655 <         * Performs the given action for each key.
2656 <         *
2657 <         * @param action the action
2655 >         * Returns matching node or null if none. Tries to search
2656 >         * using tree compareisons from root, but continues linear
2657 >         * search when lock not available.
2658           */
2659 <        public void forEachKey(Action<K> action) {
2660 <            fjp.invoke(ForkJoinTasks.forEachKey
2661 <                       (ConcurrentHashMapV8.this, action));
2659 >        final Node<K,V> find(int h, Object k) {
2660 >            if (k != null) {
2661 >                for (Node<K,V> e = first; e != null; e = e.next) {
2662 >                    int s; K ek;
2663 >                    if (((s = lockState) & (WAITER|WRITER)) != 0) {
2664 >                        if (e.hash == h &&
2665 >                            ((ek = e.key) == k || (ek != null && k.equals(ek))))
2666 >                            return e;
2667 >                    }
2668 >                    else if (U.compareAndSwapInt(this, LOCKSTATE, s,
2669 >                                                 s + READER)) {
2670 >                        TreeNode<K,V> r, p;
2671 >                        try {
2672 >                            p = ((r = root) == null ? null :
2673 >                                 r.findTreeNode(h, k, null));
2674 >                        } finally {
2675 >                            Thread w;
2676 >                            int ls;
2677 >                            do {} while (!U.compareAndSwapInt
2678 >                                         (this, LOCKSTATE,
2679 >                                          ls = lockState, ls - READER));
2680 >                            if (ls == (READER|WAITER) && (w = waiter) != null)
2681 >                                LockSupport.unpark(w);
2682 >                        }
2683 >                        return p;
2684 >                    }
2685 >                }
2686 >            }
2687 >            return null;
2688          }
2689  
2690          /**
2691 <         * Performs the given action for each non-null transformation
2692 <         * of each key.
3776 <         *
3777 <         * @param transformer a function returning the transformation
3778 <         * for an element, or null of there is no transformation (in
3779 <         * which case the action is not applied).
3780 <         * @param action the action
2691 >         * Finds or adds a node.
2692 >         * @return null if added
2693           */
2694 <        public <U> void forEachKey(Fun<? super K, ? extends U> transformer,
2695 <                                   Action<U> action) {
2696 <            fjp.invoke(ForkJoinTasks.forEachKey
2697 <                       (ConcurrentHashMapV8.this, transformer, action));
2694 >        final TreeNode<K,V> putTreeVal(int h, K k, V v) {
2695 >            Class<?> kc = null;
2696 >            for (TreeNode<K,V> p = root;;) {
2697 >                int dir, ph; K pk; TreeNode<K,V> q, pr;
2698 >                if (p == null) {
2699 >                    first = root = new TreeNode<K,V>(h, k, v, null, null);
2700 >                    break;
2701 >                }
2702 >                else if ((ph = p.hash) > h)
2703 >                    dir = -1;
2704 >                else if (ph < h)
2705 >                    dir = 1;
2706 >                else if ((pk = p.key) == k || (pk != null && k.equals(pk)))
2707 >                    return p;
2708 >                else if ((kc == null &&
2709 >                          (kc = comparableClassFor(k)) == null) ||
2710 >                         (dir = compareComparables(kc, k, pk)) == 0) {
2711 >                    if (p.left == null)
2712 >                        dir = 1;
2713 >                    else if ((pr = p.right) == null ||
2714 >                             (q = pr.findTreeNode(h, k, kc)) == null)
2715 >                        dir = -1;
2716 >                    else
2717 >                        return q;
2718 >                }
2719 >                TreeNode<K,V> xp = p;
2720 >                if ((p = (dir < 0) ? p.left : p.right) == null) {
2721 >                    TreeNode<K,V> x, f = first;
2722 >                    first = x = new TreeNode<K,V>(h, k, v, f, xp);
2723 >                    if (f != null)
2724 >                        f.prev = x;
2725 >                    if (dir < 0)
2726 >                        xp.left = x;
2727 >                    else
2728 >                        xp.right = x;
2729 >                    if (!xp.red)
2730 >                        x.red = true;
2731 >                    else {
2732 >                        lockRoot();
2733 >                        try {
2734 >                            root = balanceInsertion(root, x);
2735 >                        } finally {
2736 >                            unlockRoot();
2737 >                        }
2738 >                    }
2739 >                    break;
2740 >                }
2741 >            }
2742 >            assert checkInvariants(root);
2743 >            return null;
2744          }
2745  
2746          /**
2747 <         * Returns a non-null result from applying the given search
2748 <         * function on each key, or null if none. Upon success,
2749 <         * further element processing is suppressed and the results of
2750 <         * any other parallel invocations of the search function are
2751 <         * ignored.
2747 >         * Removes the given node, that must be present before this
2748 >         * call.  This is messier than typical red-black deletion code
2749 >         * because we cannot swap the contents of an interior node
2750 >         * with a leaf successor that is pinned by "next" pointers
2751 >         * that are accessible independently of lock. So instead we
2752 >         * swap the tree linkages.
2753           *
2754 <         * @param searchFunction a function returning a non-null
3796 <         * result on success, else null
3797 <         * @return a non-null result from applying the given search
3798 <         * function on each key, or null if none
2754 >         * @return true if now too small so should be untreeified.
2755           */
2756 <        public <U> U searchKeys(Fun<? super K, ? extends U> searchFunction) {
2757 <            return fjp.invoke(ForkJoinTasks.searchKeys
2758 <                              (ConcurrentHashMapV8.this, searchFunction));
2756 >        final boolean removeTreeNode(TreeNode<K,V> p) {
2757 >            TreeNode<K,V> next = (TreeNode<K,V>)p.next;
2758 >            TreeNode<K,V> pred = p.prev;  // unlink traversal pointers
2759 >            TreeNode<K,V> r, rl;
2760 >            if (pred == null)
2761 >                first = next;
2762 >            else
2763 >                pred.next = next;
2764 >            if (next != null)
2765 >                next.prev = pred;
2766 >            if (first == null) {
2767 >                root = null;
2768 >                return true;
2769 >            }
2770 >            if ((r = root) == null || r.right == null || // too small
2771 >                (rl = r.left) == null || rl.left == null)
2772 >                return true;
2773 >            lockRoot();
2774 >            try {
2775 >                TreeNode<K,V> replacement;
2776 >                TreeNode<K,V> pl = p.left;
2777 >                TreeNode<K,V> pr = p.right;
2778 >                if (pl != null && pr != null) {
2779 >                    TreeNode<K,V> s = pr, sl;
2780 >                    while ((sl = s.left) != null) // find successor
2781 >                        s = sl;
2782 >                    boolean c = s.red; s.red = p.red; p.red = c; // swap colors
2783 >                    TreeNode<K,V> sr = s.right;
2784 >                    TreeNode<K,V> pp = p.parent;
2785 >                    if (s == pr) { // p was s's direct parent
2786 >                        p.parent = s;
2787 >                        s.right = p;
2788 >                    }
2789 >                    else {
2790 >                        TreeNode<K,V> sp = s.parent;
2791 >                        if ((p.parent = sp) != null) {
2792 >                            if (s == sp.left)
2793 >                                sp.left = p;
2794 >                            else
2795 >                                sp.right = p;
2796 >                        }
2797 >                        if ((s.right = pr) != null)
2798 >                            pr.parent = s;
2799 >                    }
2800 >                    p.left = null;
2801 >                    if ((p.right = sr) != null)
2802 >                        sr.parent = p;
2803 >                    if ((s.left = pl) != null)
2804 >                        pl.parent = s;
2805 >                    if ((s.parent = pp) == null)
2806 >                        r = s;
2807 >                    else if (p == pp.left)
2808 >                        pp.left = s;
2809 >                    else
2810 >                        pp.right = s;
2811 >                    if (sr != null)
2812 >                        replacement = sr;
2813 >                    else
2814 >                        replacement = p;
2815 >                }
2816 >                else if (pl != null)
2817 >                    replacement = pl;
2818 >                else if (pr != null)
2819 >                    replacement = pr;
2820 >                else
2821 >                    replacement = p;
2822 >                if (replacement != p) {
2823 >                    TreeNode<K,V> pp = replacement.parent = p.parent;
2824 >                    if (pp == null)
2825 >                        r = replacement;
2826 >                    else if (p == pp.left)
2827 >                        pp.left = replacement;
2828 >                    else
2829 >                        pp.right = replacement;
2830 >                    p.left = p.right = p.parent = null;
2831 >                }
2832 >
2833 >                root = (p.red) ? r : balanceDeletion(r, replacement);
2834 >
2835 >                if (p == replacement) {  // detach pointers
2836 >                    TreeNode<K,V> pp;
2837 >                    if ((pp = p.parent) != null) {
2838 >                        if (p == pp.left)
2839 >                            pp.left = null;
2840 >                        else if (p == pp.right)
2841 >                            pp.right = null;
2842 >                        p.parent = null;
2843 >                    }
2844 >                }
2845 >            } finally {
2846 >                unlockRoot();
2847 >            }
2848 >            assert checkInvariants(root);
2849 >            return false;
2850          }
2851  
2852 <        /**
2853 <         * Returns the result of accumulating all keys using the given
2854 <         * reducer to combine values, or null if none.
2855 <         *
2856 <         * @param reducer a commutative associative combining function
2857 <         * @return the result of accumulating all keys using the given
2858 <         * reducer to combine values, or null if none
2859 <         */
2860 <        public K reduceKeys(BiFun<? super K, ? super K, ? extends K> reducer) {
2861 <            return fjp.invoke(ForkJoinTasks.reduceKeys
2862 <                              (ConcurrentHashMapV8.this, reducer));
2852 >        /* ------------------------------------------------------------ */
2853 >        // Red-black tree methods, all adapted from CLR
2854 >
2855 >        static <K,V> TreeNode<K,V> rotateLeft(TreeNode<K,V> root,
2856 >                                              TreeNode<K,V> p) {
2857 >            TreeNode<K,V> r, pp, rl;
2858 >            if (p != null && (r = p.right) != null) {
2859 >                if ((rl = p.right = r.left) != null)
2860 >                    rl.parent = p;
2861 >                if ((pp = r.parent = p.parent) == null)
2862 >                    (root = r).red = false;
2863 >                else if (pp.left == p)
2864 >                    pp.left = r;
2865 >                else
2866 >                    pp.right = r;
2867 >                r.left = p;
2868 >                p.parent = r;
2869 >            }
2870 >            return root;
2871          }
2872  
2873 <        /**
2874 <         * Returns the result of accumulating the given transformation
2875 <         * of all keys using the given reducer to combine values, or
2876 <         * null if none.
2877 <         *
2878 <         * @param transformer a function returning the transformation
2879 <         * for an element, or null of there is no transformation (in
2880 <         * which case it is not combined).
2881 <         * @param reducer a commutative associative combining function
2882 <         * @return the result of accumulating the given transformation
2883 <         * of all keys
2884 <         */
2885 <        public <U> U reduceKeys(Fun<? super K, ? extends U> transformer,
2886 <                                BiFun<? super U, ? super U, ? extends U> reducer) {
2887 <            return fjp.invoke(ForkJoinTasks.reduceKeys
2888 <                              (ConcurrentHashMapV8.this, transformer, reducer));
2873 >        static <K,V> TreeNode<K,V> rotateRight(TreeNode<K,V> root,
2874 >                                               TreeNode<K,V> p) {
2875 >            TreeNode<K,V> l, pp, lr;
2876 >            if (p != null && (l = p.left) != null) {
2877 >                if ((lr = p.left = l.right) != null)
2878 >                    lr.parent = p;
2879 >                if ((pp = l.parent = p.parent) == null)
2880 >                    (root = l).red = false;
2881 >                else if (pp.right == p)
2882 >                    pp.right = l;
2883 >                else
2884 >                    pp.left = l;
2885 >                l.right = p;
2886 >                p.parent = l;
2887 >            }
2888 >            return root;
2889          }
2890  
2891 <        /**
2892 <         * Returns the result of accumulating the given transformation
2893 <         * of all keys using the given reducer to combine values, and
2894 <         * the given basis as an identity value.
2895 <         *
2896 <         * @param transformer a function returning the transformation
2897 <         * for an element
2898 <         * @param basis the identity (initial default value) for the reduction
2899 <         * @param reducer a commutative associative combining function
2900 <         * @return  the result of accumulating the given transformation
2901 <         * of all keys
2902 <         */
2903 <        public double reduceKeysToDouble(ObjectToDouble<? super K> transformer,
2904 <                                         double basis,
2905 <                                         DoubleByDoubleToDouble reducer) {
2906 <            return fjp.invoke(ForkJoinTasks.reduceKeysToDouble
2907 <                              (ConcurrentHashMapV8.this, transformer, basis, reducer));
2891 >        static <K,V> TreeNode<K,V> balanceInsertion(TreeNode<K,V> root,
2892 >                                                    TreeNode<K,V> x) {
2893 >            x.red = true;
2894 >            for (TreeNode<K,V> xp, xpp, xppl, xppr;;) {
2895 >                if ((xp = x.parent) == null) {
2896 >                    x.red = false;
2897 >                    return x;
2898 >                }
2899 >                else if (!xp.red || (xpp = xp.parent) == null)
2900 >                    return root;
2901 >                if (xp == (xppl = xpp.left)) {
2902 >                    if ((xppr = xpp.right) != null && xppr.red) {
2903 >                        xppr.red = false;
2904 >                        xp.red = false;
2905 >                        xpp.red = true;
2906 >                        x = xpp;
2907 >                    }
2908 >                    else {
2909 >                        if (x == xp.right) {
2910 >                            root = rotateLeft(root, x = xp);
2911 >                            xpp = (xp = x.parent) == null ? null : xp.parent;
2912 >                        }
2913 >                        if (xp != null) {
2914 >                            xp.red = false;
2915 >                            if (xpp != null) {
2916 >                                xpp.red = true;
2917 >                                root = rotateRight(root, xpp);
2918 >                            }
2919 >                        }
2920 >                    }
2921 >                }
2922 >                else {
2923 >                    if (xppl != null && xppl.red) {
2924 >                        xppl.red = false;
2925 >                        xp.red = false;
2926 >                        xpp.red = true;
2927 >                        x = xpp;
2928 >                    }
2929 >                    else {
2930 >                        if (x == xp.left) {
2931 >                            root = rotateRight(root, x = xp);
2932 >                            xpp = (xp = x.parent) == null ? null : xp.parent;
2933 >                        }
2934 >                        if (xp != null) {
2935 >                            xp.red = false;
2936 >                            if (xpp != null) {
2937 >                                xpp.red = true;
2938 >                                root = rotateLeft(root, xpp);
2939 >                            }
2940 >                        }
2941 >                    }
2942 >                }
2943 >            }
2944          }
2945  
2946 +        static <K,V> TreeNode<K,V> balanceDeletion(TreeNode<K,V> root,
2947 +                                                   TreeNode<K,V> x) {
2948 +            for (TreeNode<K,V> xp, xpl, xpr;;)  {
2949 +                if (x == null || x == root)
2950 +                    return root;
2951 +                else if ((xp = x.parent) == null) {
2952 +                    x.red = false;
2953 +                    return x;
2954 +                }
2955 +                else if (x.red) {
2956 +                    x.red = false;
2957 +                    return root;
2958 +                }
2959 +                else if ((xpl = xp.left) == x) {
2960 +                    if ((xpr = xp.right) != null && xpr.red) {
2961 +                        xpr.red = false;
2962 +                        xp.red = true;
2963 +                        root = rotateLeft(root, xp);
2964 +                        xpr = (xp = x.parent) == null ? null : xp.right;
2965 +                    }
2966 +                    if (xpr == null)
2967 +                        x = xp;
2968 +                    else {
2969 +                        TreeNode<K,V> sl = xpr.left, sr = xpr.right;
2970 +                        if ((sr == null || !sr.red) &&
2971 +                            (sl == null || !sl.red)) {
2972 +                            xpr.red = true;
2973 +                            x = xp;
2974 +                        }
2975 +                        else {
2976 +                            if (sr == null || !sr.red) {
2977 +                                if (sl != null)
2978 +                                    sl.red = false;
2979 +                                xpr.red = true;
2980 +                                root = rotateRight(root, xpr);
2981 +                                xpr = (xp = x.parent) == null ?
2982 +                                    null : xp.right;
2983 +                            }
2984 +                            if (xpr != null) {
2985 +                                xpr.red = (xp == null) ? false : xp.red;
2986 +                                if ((sr = xpr.right) != null)
2987 +                                    sr.red = false;
2988 +                            }
2989 +                            if (xp != null) {
2990 +                                xp.red = false;
2991 +                                root = rotateLeft(root, xp);
2992 +                            }
2993 +                            x = root;
2994 +                        }
2995 +                    }
2996 +                }
2997 +                else { // symmetric
2998 +                    if (xpl != null && xpl.red) {
2999 +                        xpl.red = false;
3000 +                        xp.red = true;
3001 +                        root = rotateRight(root, xp);
3002 +                        xpl = (xp = x.parent) == null ? null : xp.left;
3003 +                    }
3004 +                    if (xpl == null)
3005 +                        x = xp;
3006 +                    else {
3007 +                        TreeNode<K,V> sl = xpl.left, sr = xpl.right;
3008 +                        if ((sl == null || !sl.red) &&
3009 +                            (sr == null || !sr.red)) {
3010 +                            xpl.red = true;
3011 +                            x = xp;
3012 +                        }
3013 +                        else {
3014 +                            if (sl == null || !sl.red) {
3015 +                                if (sr != null)
3016 +                                    sr.red = false;
3017 +                                xpl.red = true;
3018 +                                root = rotateLeft(root, xpl);
3019 +                                xpl = (xp = x.parent) == null ?
3020 +                                    null : xp.left;
3021 +                            }
3022 +                            if (xpl != null) {
3023 +                                xpl.red = (xp == null) ? false : xp.red;
3024 +                                if ((sl = xpl.left) != null)
3025 +                                    sl.red = false;
3026 +                            }
3027 +                            if (xp != null) {
3028 +                                xp.red = false;
3029 +                                root = rotateRight(root, xp);
3030 +                            }
3031 +                            x = root;
3032 +                        }
3033 +                    }
3034 +                }
3035 +            }
3036 +        }
3037          /**
3038 <         * Returns the result of accumulating the given transformation
3857 <         * of all keys using the given reducer to combine values, and
3858 <         * the given basis as an identity value.
3859 <         *
3860 <         * @param transformer a function returning the transformation
3861 <         * for an element
3862 <         * @param basis the identity (initial default value) for the reduction
3863 <         * @param reducer a commutative associative combining function
3864 <         * @return the result of accumulating the given transformation
3865 <         * of all keys
3038 >         * Recursive invariant check
3039           */
3040 <        public long reduceKeysToLong(ObjectToLong<? super K> transformer,
3041 <                                     long basis,
3042 <                                     LongByLongToLong reducer) {
3043 <            return fjp.invoke(ForkJoinTasks.reduceKeysToLong
3044 <                              (ConcurrentHashMapV8.this, transformer, basis, reducer));
3040 >        static <K,V> boolean checkInvariants(TreeNode<K,V> t) {
3041 >            TreeNode<K,V> tp = t.parent, tl = t.left, tr = t.right,
3042 >                tb = t.prev, tn = (TreeNode<K,V>)t.next;
3043 >            if (tb != null && tb.next != t)
3044 >                return false;
3045 >            if (tn != null && tn.prev != t)
3046 >                return false;
3047 >            if (tp != null && t != tp.left && t != tp.right)
3048 >                return false;
3049 >            if (tl != null && (tl.parent != t || tl.hash > t.hash))
3050 >                return false;
3051 >            if (tr != null && (tr.parent != t || tr.hash < t.hash))
3052 >                return false;
3053 >            if (t.red && tl != null && tl.red && tr != null && tr.red)
3054 >                return false;
3055 >            if (tl != null && !checkInvariants(tl))
3056 >                return false;
3057 >            if (tr != null && !checkInvariants(tr))
3058 >                return false;
3059 >            return true;
3060          }
3061  
3062 <        /**
3063 <         * Returns the result of accumulating the given transformation
3064 <         * of all keys using the given reducer to combine values, and
3065 <         * the given basis as an identity value.
3066 <         *
3067 <         * @param transformer a function returning the transformation
3068 <         * for an element
3069 <         * @param basis the identity (initial default value) for the reduction
3070 <         * @param reducer a commutative associative combining function
3071 <         * @return the result of accumulating the given transformation
3072 <         * of all keys
3885 <         */
3886 <        public int reduceKeysToInt(ObjectToInt<? super K> transformer,
3887 <                                   int basis,
3888 <                                   IntByIntToInt reducer) {
3889 <            return fjp.invoke(ForkJoinTasks.reduceKeysToInt
3890 <                              (ConcurrentHashMapV8.this, transformer, basis, reducer));
3062 >        private static final sun.misc.Unsafe U;
3063 >        private static final long LOCKSTATE;
3064 >        static {
3065 >            try {
3066 >                U = getUnsafe();
3067 >                Class<?> k = TreeBin.class;
3068 >                LOCKSTATE = U.objectFieldOffset
3069 >                    (k.getDeclaredField("lockState"));
3070 >            } catch (Exception e) {
3071 >                throw new Error(e);
3072 >            }
3073          }
3074 +    }
3075  
3076 <        /**
3077 <         * Performs the given action for each value.
3078 <         *
3079 <         * @param action the action
3080 <         */
3081 <        public void forEachValue(Action<V> action) {
3082 <            fjp.invoke(ForkJoinTasks.forEachValue
3083 <                       (ConcurrentHashMapV8.this, action));
3076 >    /* ----------------Table Traversal -------------- */
3077 >
3078 >    /**
3079 >     * Encapsulates traversal for methods such as containsValue; also
3080 >     * serves as a base class for other iterators and spliterators.
3081 >     *
3082 >     * Method advance visits once each still-valid node that was
3083 >     * reachable upon iterator construction. It might miss some that
3084 >     * were added to a bin after the bin was visited, which is OK wrt
3085 >     * consistency guarantees. Maintaining this property in the face
3086 >     * of possible ongoing resizes requires a fair amount of
3087 >     * bookkeeping state that is difficult to optimize away amidst
3088 >     * volatile accesses.  Even so, traversal maintains reasonable
3089 >     * throughput.
3090 >     *
3091 >     * Normally, iteration proceeds bin-by-bin traversing lists.
3092 >     * However, if the table has been resized, then all future steps
3093 >     * must traverse both the bin at the current index as well as at
3094 >     * (index + baseSize); and so on for further resizings. To
3095 >     * paranoically cope with potential sharing by users of iterators
3096 >     * across threads, iteration terminates if a bounds checks fails
3097 >     * for a table read.
3098 >     */
3099 >    static class Traverser<K,V> {
3100 >        Node<K,V>[] tab;        // current table; updated if resized
3101 >        Node<K,V> next;         // the next entry to use
3102 >        int index;              // index of bin to use next
3103 >        int baseIndex;          // current index of initial table
3104 >        int baseLimit;          // index bound for initial table
3105 >        final int baseSize;     // initial table size
3106 >
3107 >        Traverser(Node<K,V>[] tab, int size, int index, int limit) {
3108 >            this.tab = tab;
3109 >            this.baseSize = size;
3110 >            this.baseIndex = this.index = index;
3111 >            this.baseLimit = limit;
3112 >            this.next = null;
3113          }
3114  
3115          /**
3116 <         * Performs the given action for each non-null transformation
3117 <         * of each value.
3118 <         *
3119 <         * @param transformer a function returning the transformation
3120 <         * for an element, or null of there is no transformation (in
3121 <         * which case the action is not applied).
3122 <         */
3123 <        public <U> void forEachValue(Fun<? super V, ? extends U> transformer,
3124 <                                     Action<U> action) {
3125 <            fjp.invoke(ForkJoinTasks.forEachValue
3126 <                       (ConcurrentHashMapV8.this, transformer, action));
3116 >         * Advances if possible, returning next valid node, or null if none.
3117 >         */
3118 >        final Node<K,V> advance() {
3119 >            Node<K,V> e;
3120 >            if ((e = next) != null)
3121 >                e = e.next;
3122 >            for (;;) {
3123 >                Node<K,V>[] t; int i, n; K ek;  // must use locals in checks
3124 >                if (e != null)
3125 >                    return next = e;
3126 >                if (baseIndex >= baseLimit || (t = tab) == null ||
3127 >                    (n = t.length) <= (i = index) || i < 0)
3128 >                    return next = null;
3129 >                if ((e = tabAt(t, index)) != null && e.hash < 0) {
3130 >                    if (e instanceof ForwardingNode) {
3131 >                        tab = ((ForwardingNode<K,V>)e).nextTable;
3132 >                        e = null;
3133 >                        continue;
3134 >                    }
3135 >                    else if (e instanceof TreeBin)
3136 >                        e = ((TreeBin<K,V>)e).first;
3137 >                    else
3138 >                        e = null;
3139 >                }
3140 >                if ((index += baseSize) >= n)
3141 >                    index = ++baseIndex;    // visit upper slots if present
3142 >            }
3143          }
3144 +    }
3145  
3146 <        /**
3147 <         * Returns a non-null result from applying the given search
3148 <         * function on each value, or null if none.  Upon success,
3149 <         * further element processing is suppressed and the results of
3150 <         * any other parallel invocations of the search function are
3151 <         * ignored.
3152 <         *
3153 <         * @param searchFunction a function returning a non-null
3154 <         * result on success, else null
3155 <         * @return a non-null result from applying the given search
3156 <         * function on each value, or null if none
3157 <         *
3929 <         */
3930 <        public <U> U searchValues(Fun<? super V, ? extends U> searchFunction) {
3931 <            return fjp.invoke(ForkJoinTasks.searchValues
3932 <                              (ConcurrentHashMapV8.this, searchFunction));
3146 >    /**
3147 >     * Base of key, value, and entry Iterators. Adds fields to
3148 >     * Traverser to support iterator.remove
3149 >     */
3150 >    static class BaseIterator<K,V> extends Traverser<K,V> {
3151 >        final ConcurrentHashMapV8<K,V> map;
3152 >        Node<K,V> lastReturned;
3153 >        BaseIterator(Node<K,V>[] tab, int size, int index, int limit,
3154 >                    ConcurrentHashMapV8<K,V> map) {
3155 >            super(tab, size, index, limit);
3156 >            this.map = map;
3157 >            advance();
3158          }
3159  
3160 <        /**
3161 <         * Returns the result of accumulating all values using the
3162 <         * given reducer to combine values, or null if none.
3163 <         *
3164 <         * @param reducer a commutative associative combining function
3165 <         * @return  the result of accumulating all values
3166 <         */
3167 <        public V reduceValues(BiFun<? super V, ? super V, ? extends V> reducer) {
3168 <            return fjp.invoke(ForkJoinTasks.reduceValues
3944 <                              (ConcurrentHashMapV8.this, reducer));
3160 >        public final boolean hasNext() { return next != null; }
3161 >        public final boolean hasMoreElements() { return next != null; }
3162 >
3163 >        public final void remove() {
3164 >            Node<K,V> p;
3165 >            if ((p = lastReturned) == null)
3166 >                throw new IllegalStateException();
3167 >            lastReturned = null;
3168 >            map.replaceNode(p.key, null, null);
3169          }
3170 +    }
3171  
3172 <        /**
3173 <         * Returns the result of accumulating the given transformation
3174 <         * of all values using the given reducer to combine values, or
3175 <         * null if none.
3176 <         *
3952 <         * @param transformer a function returning the transformation
3953 <         * for an element, or null of there is no transformation (in
3954 <         * which case it is not combined).
3955 <         * @param reducer a commutative associative combining function
3956 <         * @return the result of accumulating the given transformation
3957 <         * of all values
3958 <         */
3959 <        public <U> U reduceValues(Fun<? super V, ? extends U> transformer,
3960 <                                  BiFun<? super U, ? super U, ? extends U> reducer) {
3961 <            return fjp.invoke(ForkJoinTasks.reduceValues
3962 <                              (ConcurrentHashMapV8.this, transformer, reducer));
3172 >    static final class KeyIterator<K,V> extends BaseIterator<K,V>
3173 >        implements Iterator<K>, Enumeration<K> {
3174 >        KeyIterator(Node<K,V>[] tab, int index, int size, int limit,
3175 >                    ConcurrentHashMapV8<K,V> map) {
3176 >            super(tab, index, size, limit, map);
3177          }
3178  
3179 <        /**
3180 <         * Returns the result of accumulating the given transformation
3181 <         * of all values using the given reducer to combine values,
3182 <         * and the given basis as an identity value.
3183 <         *
3184 <         * @param transformer a function returning the transformation
3185 <         * for an element
3186 <         * @param basis the identity (initial default value) for the reduction
3973 <         * @param reducer a commutative associative combining function
3974 <         * @return the result of accumulating the given transformation
3975 <         * of all values
3976 <         */
3977 <        public double reduceValuesToDouble(ObjectToDouble<? super V> transformer,
3978 <                                           double basis,
3979 <                                           DoubleByDoubleToDouble reducer) {
3980 <            return fjp.invoke(ForkJoinTasks.reduceValuesToDouble
3981 <                              (ConcurrentHashMapV8.this, transformer, basis, reducer));
3179 >        public final K next() {
3180 >            Node<K,V> p;
3181 >            if ((p = next) == null)
3182 >                throw new NoSuchElementException();
3183 >            K k = p.key;
3184 >            lastReturned = p;
3185 >            advance();
3186 >            return k;
3187          }
3188  
3189 <        /**
3190 <         * Returns the result of accumulating the given transformation
3191 <         * of all values using the given reducer to combine values,
3192 <         * and the given basis as an identity value.
3193 <         *
3194 <         * @param transformer a function returning the transformation
3195 <         * for an element
3196 <         * @param basis the identity (initial default value) for the reduction
3992 <         * @param reducer a commutative associative combining function
3993 <         * @return the result of accumulating the given transformation
3994 <         * of all values
3995 <         */
3996 <        public long reduceValuesToLong(ObjectToLong<? super V> transformer,
3997 <                                       long basis,
3998 <                                       LongByLongToLong reducer) {
3999 <            return fjp.invoke(ForkJoinTasks.reduceValuesToLong
4000 <                              (ConcurrentHashMapV8.this, transformer, basis, reducer));
3189 >        public final K nextElement() { return next(); }
3190 >    }
3191 >
3192 >    static final class ValueIterator<K,V> extends BaseIterator<K,V>
3193 >        implements Iterator<V>, Enumeration<V> {
3194 >        ValueIterator(Node<K,V>[] tab, int index, int size, int limit,
3195 >                      ConcurrentHashMapV8<K,V> map) {
3196 >            super(tab, index, size, limit, map);
3197          }
3198  
3199 <        /**
3200 <         * Returns the result of accumulating the given transformation
3201 <         * of all values using the given reducer to combine values,
3202 <         * and the given basis as an identity value.
3203 <         *
3204 <         * @param transformer a function returning the transformation
3205 <         * for an element
3206 <         * @param basis the identity (initial default value) for the reduction
4011 <         * @param reducer a commutative associative combining function
4012 <         * @return the result of accumulating the given transformation
4013 <         * of all values
4014 <         */
4015 <        public int reduceValuesToInt(ObjectToInt<? super V> transformer,
4016 <                                     int basis,
4017 <                                     IntByIntToInt reducer) {
4018 <            return fjp.invoke(ForkJoinTasks.reduceValuesToInt
4019 <                              (ConcurrentHashMapV8.this, transformer, basis, reducer));
3199 >        public final V next() {
3200 >            Node<K,V> p;
3201 >            if ((p = next) == null)
3202 >                throw new NoSuchElementException();
3203 >            V v = p.val;
3204 >            lastReturned = p;
3205 >            advance();
3206 >            return v;
3207          }
3208  
3209 <        /**
3210 <         * Performs the given action for each entry.
3211 <         *
3212 <         * @param action the action
3213 <         */
3214 <        public void forEachEntry(Action<Map.Entry<K,V>> action) {
3215 <            fjp.invoke(ForkJoinTasks.forEachEntry
3216 <                       (ConcurrentHashMapV8.this, action));
3209 >        public final V nextElement() { return next(); }
3210 >    }
3211 >
3212 >    static final class EntryIterator<K,V> extends BaseIterator<K,V>
3213 >        implements Iterator<Map.Entry<K,V>> {
3214 >        EntryIterator(Node<K,V>[] tab, int index, int size, int limit,
3215 >                      ConcurrentHashMapV8<K,V> map) {
3216 >            super(tab, index, size, limit, map);
3217          }
3218  
3219 <        /**
3220 <         * Performs the given action for each non-null transformation
3221 <         * of each entry.
3222 <         *
3223 <         * @param transformer a function returning the transformation
3224 <         * for an element, or null of there is no transformation (in
3225 <         * which case the action is not applied).
3226 <         * @param action the action
3227 <         */
4041 <        public <U> void forEachEntry(Fun<Map.Entry<K,V>, ? extends U> transformer,
4042 <                                     Action<U> action) {
4043 <            fjp.invoke(ForkJoinTasks.forEachEntry
4044 <                       (ConcurrentHashMapV8.this, transformer, action));
3219 >        public final Map.Entry<K,V> next() {
3220 >            Node<K,V> p;
3221 >            if ((p = next) == null)
3222 >                throw new NoSuchElementException();
3223 >            K k = p.key;
3224 >            V v = p.val;
3225 >            lastReturned = p;
3226 >            advance();
3227 >            return new MapEntry<K,V>(k, v, map);
3228          }
3229 +    }
3230  
3231 <        /**
3232 <         * Returns a non-null result from applying the given search
3233 <         * function on each entry, or null if none.  Upon success,
3234 <         * further element processing is suppressed and the results of
3235 <         * any other parallel invocations of the search function are
3236 <         * ignored.
3237 <         *
3238 <         * @param searchFunction a function returning a non-null
3239 <         * result on success, else null
3240 <         * @return a non-null result from applying the given search
3241 <         * function on each entry, or null if none
4058 <         */
4059 <        public <U> U searchEntries(Fun<Map.Entry<K,V>, ? extends U> searchFunction) {
4060 <            return fjp.invoke(ForkJoinTasks.searchEntries
4061 <                              (ConcurrentHashMapV8.this, searchFunction));
3231 >    /**
3232 >     * Exported Entry for EntryIterator
3233 >     */
3234 >    static final class MapEntry<K,V> implements Map.Entry<K,V> {
3235 >        final K key; // non-null
3236 >        V val;       // non-null
3237 >        final ConcurrentHashMapV8<K,V> map;
3238 >        MapEntry(K key, V val, ConcurrentHashMapV8<K,V> map) {
3239 >            this.key = key;
3240 >            this.val = val;
3241 >            this.map = map;
3242          }
3243 +        public K getKey()        { return key; }
3244 +        public V getValue()      { return val; }
3245 +        public int hashCode()    { return key.hashCode() ^ val.hashCode(); }
3246 +        public String toString() { return key + "=" + val; }
3247  
3248 <        /**
3249 <         * Returns the result of accumulating all entries using the
3250 <         * given reducer to combine values, or null if none.
3251 <         *
3252 <         * @param reducer a commutative associative combining function
3253 <         * @return the result of accumulating all entries
3254 <         */
4071 <        public Map.Entry<K,V> reduceEntries(BiFun<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer) {
4072 <            return fjp.invoke(ForkJoinTasks.reduceEntries
4073 <                              (ConcurrentHashMapV8.this, reducer));
3248 >        public boolean equals(Object o) {
3249 >            Object k, v; Map.Entry<?,?> e;
3250 >            return ((o instanceof Map.Entry) &&
3251 >                    (k = (e = (Map.Entry<?,?>)o).getKey()) != null &&
3252 >                    (v = e.getValue()) != null &&
3253 >                    (k == key || k.equals(key)) &&
3254 >                    (v == val || v.equals(val)));
3255          }
3256  
3257          /**
3258 <         * Returns the result of accumulating the given transformation
3259 <         * of all entries using the given reducer to combine values,
3260 <         * or null if none.
3261 <         *
3262 <         * @param transformer a function returning the transformation
3263 <         * for an element, or null of there is no transformation (in
4083 <         * which case it is not combined).
4084 <         * @param reducer a commutative associative combining function
4085 <         * @return the result of accumulating the given transformation
4086 <         * of all entries
3258 >         * Sets our entry's value and writes through to the map. The
3259 >         * value to return is somewhat arbitrary here. Since we do not
3260 >         * necessarily track asynchronous changes, the most recent
3261 >         * "previous" value could be different from what we return (or
3262 >         * could even have been removed, in which case the put will
3263 >         * re-establish). We do not and cannot guarantee more.
3264           */
3265 <        public <U> U reduceEntries(Fun<Map.Entry<K,V>, ? extends U> transformer,
3266 <                                   BiFun<? super U, ? super U, ? extends U> reducer) {
3267 <            return fjp.invoke(ForkJoinTasks.reduceEntries
3268 <                              (ConcurrentHashMapV8.this, transformer, reducer));
3265 >        public V setValue(V value) {
3266 >            if (value == null) throw new NullPointerException();
3267 >            V v = val;
3268 >            val = value;
3269 >            map.put(key, value);
3270 >            return v;
3271          }
3272 +    }
3273  
3274 <        /**
3275 <         * Returns the result of accumulating the given transformation
3276 <         * of all entries using the given reducer to combine values,
3277 <         * and the given basis as an identity value.
3278 <         *
3279 <         * @param transformer a function returning the transformation
3280 <         * for an element
3281 <         * @param basis the identity (initial default value) for the reduction
3282 <         * @param reducer a commutative associative combining function
3283 <         * @return the result of accumulating the given transformation
3284 <         * of all entries
3285 <         */
3286 <        public double reduceEntriesToDouble(ObjectToDouble<Map.Entry<K,V>> transformer,
3287 <                                            double basis,
4108 <                                            DoubleByDoubleToDouble reducer) {
4109 <            return fjp.invoke(ForkJoinTasks.reduceEntriesToDouble
4110 <                              (ConcurrentHashMapV8.this, transformer, basis, reducer));
3274 >    static final class KeySpliterator<K,V> extends Traverser<K,V>
3275 >        implements ConcurrentHashMapSpliterator<K> {
3276 >        long est;               // size estimate
3277 >        KeySpliterator(Node<K,V>[] tab, int size, int index, int limit,
3278 >                       long est) {
3279 >            super(tab, size, index, limit);
3280 >            this.est = est;
3281 >        }
3282 >
3283 >        public ConcurrentHashMapSpliterator<K> trySplit() {
3284 >            int i, f, h;
3285 >            return (h = ((i = baseIndex) + (f = baseLimit)) >>> 1) <= i ? null :
3286 >                new KeySpliterator<K,V>(tab, baseSize, baseLimit = h,
3287 >                                        f, est >>>= 1);
3288          }
3289  
3290 <        /**
3291 <         * Returns the result of accumulating the given transformation
3292 <         * of all entries using the given reducer to combine values,
3293 <         * and the given basis as an identity value.
4117 <         *
4118 <         * @param transformer a function returning the transformation
4119 <         * for an element
4120 <         * @param basis the identity (initial default value) for the reduction
4121 <         * @param reducer a commutative associative combining function
4122 <         * @return  the result of accumulating the given transformation
4123 <         * of all entries
4124 <         */
4125 <        public long reduceEntriesToLong(ObjectToLong<Map.Entry<K,V>> transformer,
4126 <                                        long basis,
4127 <                                        LongByLongToLong reducer) {
4128 <            return fjp.invoke(ForkJoinTasks.reduceEntriesToLong
4129 <                              (ConcurrentHashMapV8.this, transformer, basis, reducer));
3290 >        public void forEachRemaining(Action<? super K> action) {
3291 >            if (action == null) throw new NullPointerException();
3292 >            for (Node<K,V> p; (p = advance()) != null;)
3293 >                action.apply(p.key);
3294          }
3295  
3296 <        /**
3297 <         * Returns the result of accumulating the given transformation
3298 <         * of all entries using the given reducer to combine values,
3299 <         * and the given basis as an identity value.
3300 <         *
3301 <         * @param transformer a function returning the transformation
3302 <         * for an element
4139 <         * @param basis the identity (initial default value) for the reduction
4140 <         * @param reducer a commutative associative combining function
4141 <         * @return the result of accumulating the given transformation
4142 <         * of all entries
4143 <         */
4144 <        public int reduceEntriesToInt(ObjectToInt<Map.Entry<K,V>> transformer,
4145 <                                      int basis,
4146 <                                      IntByIntToInt reducer) {
4147 <            return fjp.invoke(ForkJoinTasks.reduceEntriesToInt
4148 <                              (ConcurrentHashMapV8.this, transformer, basis, reducer));
3296 >        public boolean tryAdvance(Action<? super K> action) {
3297 >            if (action == null) throw new NullPointerException();
3298 >            Node<K,V> p;
3299 >            if ((p = advance()) == null)
3300 >                return false;
3301 >            action.apply(p.key);
3302 >            return true;
3303          }
3304 +
3305 +        public long estimateSize() { return est; }
3306 +
3307      }
3308  
3309 <    // ---------------------------------------------------------------------
3309 >    static final class ValueSpliterator<K,V> extends Traverser<K,V>
3310 >        implements ConcurrentHashMapSpliterator<V> {
3311 >        long est;               // size estimate
3312 >        ValueSpliterator(Node<K,V>[] tab, int size, int index, int limit,
3313 >                         long est) {
3314 >            super(tab, size, index, limit);
3315 >            this.est = est;
3316 >        }
3317  
3318 <    /**
3319 <     * Predefined tasks for performing bulk parallel operations on
3320 <     * ConcurrentHashMaps. These tasks follow the forms and rules used
3321 <     * in class {@link Parallel}. Each method has the same name, but
3322 <     * returns a task rather than invoking it. These methods may be
3323 <     * useful in custom applications such as submitting a task without
4160 <     * waiting for completion, or combining with other tasks.
4161 <     */
4162 <    public static class ForkJoinTasks {
4163 <        private ForkJoinTasks() {}
3318 >        public ConcurrentHashMapSpliterator<V> trySplit() {
3319 >            int i, f, h;
3320 >            return (h = ((i = baseIndex) + (f = baseLimit)) >>> 1) <= i ? null :
3321 >                new ValueSpliterator<K,V>(tab, baseSize, baseLimit = h,
3322 >                                          f, est >>>= 1);
3323 >        }
3324  
3325 <        /**
4166 <         * Returns a task that when invoked, performs the given
4167 <         * action for each (key, value)
4168 <         *
4169 <         * @param map the map
4170 <         * @param action the action
4171 <         * @return the task
4172 <         */
4173 <        public static <K,V> ForkJoinTask<Void> forEach
4174 <            (ConcurrentHashMapV8<K,V> map,
4175 <             BiAction<K,V> action) {
3325 >        public void forEachRemaining(Action<? super V> action) {
3326              if (action == null) throw new NullPointerException();
3327 <            return new ForEachMappingTask<K,V>(map, action);
3327 >            for (Node<K,V> p; (p = advance()) != null;)
3328 >                action.apply(p.val);
3329          }
3330  
3331 <        /**
3332 <         * Returns a task that when invoked, performs the given
3333 <         * action for each non-null transformation of each (key, value)
3334 <         *
3335 <         * @param map the map
3336 <         * @param transformer a function returning the transformation
3337 <         * for an element, or null of there is no transformation (in
4187 <         * which case the action is not applied).
4188 <         * @param action the action
4189 <         * @return the task
4190 <         */
4191 <        public static <K,V,U> ForkJoinTask<Void> forEach
4192 <            (ConcurrentHashMapV8<K,V> map,
4193 <             BiFun<? super K, ? super V, ? extends U> transformer,
4194 <             Action<U> action) {
4195 <            if (transformer == null || action == null)
4196 <                throw new NullPointerException();
4197 <            return new ForEachTransformedMappingTask<K,V,U>
4198 <                (map, transformer, action);
3331 >        public boolean tryAdvance(Action<? super V> action) {
3332 >            if (action == null) throw new NullPointerException();
3333 >            Node<K,V> p;
3334 >            if ((p = advance()) == null)
3335 >                return false;
3336 >            action.apply(p.val);
3337 >            return true;
3338          }
3339  
3340 <        /**
3341 <         * Returns a task that when invoked, returns a non-null result
3342 <         * from applying the given search function on each (key,
3343 <         * value), or null if none. Upon success, further element
3344 <         * processing is suppressed and the results of any other
3345 <         * parallel invocations of the search function are ignored.
3346 <         *
3347 <         * @param map the map
3348 <         * @param searchFunction a function returning a non-null
3349 <         * result on success, else null
3350 <         * @return the task
3351 <         */
3352 <        public static <K,V,U> ForkJoinTask<U> search
4214 <            (ConcurrentHashMapV8<K,V> map,
4215 <             BiFun<? super K, ? super V, ? extends U> searchFunction) {
4216 <            if (searchFunction == null) throw new NullPointerException();
4217 <            return new SearchMappingsTask<K,V,U>
4218 <                (map, searchFunction,
4219 <                 new AtomicReference<U>());
3340 >        public long estimateSize() { return est; }
3341 >
3342 >    }
3343 >
3344 >    static final class EntrySpliterator<K,V> extends Traverser<K,V>
3345 >        implements ConcurrentHashMapSpliterator<Map.Entry<K,V>> {
3346 >        final ConcurrentHashMapV8<K,V> map; // To export MapEntry
3347 >        long est;               // size estimate
3348 >        EntrySpliterator(Node<K,V>[] tab, int size, int index, int limit,
3349 >                         long est, ConcurrentHashMapV8<K,V> map) {
3350 >            super(tab, size, index, limit);
3351 >            this.map = map;
3352 >            this.est = est;
3353          }
3354  
3355 <        /**
3356 <         * Returns a task that when invoked, returns the result of
3357 <         * accumulating the given transformation of all (key, value) pairs
3358 <         * using the given reducer to combine values, or null if none.
3359 <         *
4227 <         * @param map the map
4228 <         * @param transformer a function returning the transformation
4229 <         * for an element, or null of there is no transformation (in
4230 <         * which case it is not combined).
4231 <         * @param reducer a commutative associative combining function
4232 <         * @return the task
4233 <         */
4234 <        public static <K,V,U> ForkJoinTask<U> reduce
4235 <            (ConcurrentHashMapV8<K,V> map,
4236 <             BiFun<? super K, ? super V, ? extends U> transformer,
4237 <             BiFun<? super U, ? super U, ? extends U> reducer) {
4238 <            if (transformer == null || reducer == null)
4239 <                throw new NullPointerException();
4240 <            return new MapReduceMappingsTask<K,V,U>
4241 <                (map, transformer, reducer);
3355 >        public ConcurrentHashMapSpliterator<Map.Entry<K,V>> trySplit() {
3356 >            int i, f, h;
3357 >            return (h = ((i = baseIndex) + (f = baseLimit)) >>> 1) <= i ? null :
3358 >                new EntrySpliterator<K,V>(tab, baseSize, baseLimit = h,
3359 >                                          f, est >>>= 1, map);
3360          }
3361  
3362 <        /**
3363 <         * Returns a task that when invoked, returns the result of
3364 <         * accumulating the given transformation of all (key, value) pairs
3365 <         * using the given reducer to combine values, and the given
4248 <         * basis as an identity value.
4249 <         *
4250 <         * @param map the map
4251 <         * @param transformer a function returning the transformation
4252 <         * for an element
4253 <         * @param basis the identity (initial default value) for the reduction
4254 <         * @param reducer a commutative associative combining function
4255 <         * @return the task
4256 <         */
4257 <        public static <K,V> ForkJoinTask<Double> reduceToDouble
4258 <            (ConcurrentHashMapV8<K,V> map,
4259 <             ObjectByObjectToDouble<? super K, ? super V> transformer,
4260 <             double basis,
4261 <             DoubleByDoubleToDouble reducer) {
4262 <            if (transformer == null || reducer == null)
4263 <                throw new NullPointerException();
4264 <            return new MapReduceMappingsToDoubleTask<K,V>
4265 <                (map, transformer, basis, reducer);
3362 >        public void forEachRemaining(Action<? super Map.Entry<K,V>> action) {
3363 >            if (action == null) throw new NullPointerException();
3364 >            for (Node<K,V> p; (p = advance()) != null; )
3365 >                action.apply(new MapEntry<K,V>(p.key, p.val, map));
3366          }
3367  
3368 <        /**
3369 <         * Returns a task that when invoked, returns the result of
3370 <         * accumulating the given transformation of all (key, value) pairs
3371 <         * using the given reducer to combine values, and the given
3372 <         * basis as an identity value.
3373 <         *
3374 <         * @param map the map
4275 <         * @param transformer a function returning the transformation
4276 <         * for an element
4277 <         * @param basis the identity (initial default value) for the reduction
4278 <         * @param reducer a commutative associative combining function
4279 <         * @return the task
4280 <         */
4281 <        public static <K,V> ForkJoinTask<Long> reduceToLong
4282 <            (ConcurrentHashMapV8<K,V> map,
4283 <             ObjectByObjectToLong<? super K, ? super V> transformer,
4284 <             long basis,
4285 <             LongByLongToLong reducer) {
4286 <            if (transformer == null || reducer == null)
4287 <                throw new NullPointerException();
4288 <            return new MapReduceMappingsToLongTask<K,V>
4289 <                (map, transformer, basis, reducer);
3368 >        public boolean tryAdvance(Action<? super Map.Entry<K,V>> action) {
3369 >            if (action == null) throw new NullPointerException();
3370 >            Node<K,V> p;
3371 >            if ((p = advance()) == null)
3372 >                return false;
3373 >            action.apply(new MapEntry<K,V>(p.key, p.val, map));
3374 >            return true;
3375          }
3376  
3377 +        public long estimateSize() { return est; }
3378 +
3379 +    }
3380 +
3381 +    // Parallel bulk operations
3382 +
3383 +    /**
3384 +     * Computes initial batch value for bulk tasks. The returned value
3385 +     * is approximately exp2 of the number of times (minus one) to
3386 +     * split task by two before executing leaf action. This value is
3387 +     * faster to compute and more convenient to use as a guide to
3388 +     * splitting than is the depth, since it is used while dividing by
3389 +     * two anyway.
3390 +     */
3391 +    final int batchFor(long b) {
3392 +        long n;
3393 +        if (b == Long.MAX_VALUE || (n = sumCount()) <= 1L || n < b)
3394 +            return 0;
3395 +        int sp = ForkJoinPool.getCommonPoolParallelism() << 2; // slack of 4
3396 +        return (b <= 0L || (n /= b) >= sp) ? sp : (int)n;
3397 +    }
3398 +
3399 +    /**
3400 +     * Performs the given action for each (key, value).
3401 +     *
3402 +     * @param parallelismThreshold the (estimated) number of elements
3403 +     * needed for this operation to be executed in parallel
3404 +     * @param action the action
3405 +     * @since 1.8
3406 +     */
3407 +    public void forEach(long parallelismThreshold,
3408 +                        BiAction<? super K,? super V> action) {
3409 +        if (action == null) throw new NullPointerException();
3410 +        new ForEachMappingTask<K,V>
3411 +            (null, batchFor(parallelismThreshold), 0, 0, table,
3412 +             action).invoke();
3413 +    }
3414 +
3415 +    /**
3416 +     * Performs the given action for each non-null transformation
3417 +     * of each (key, value).
3418 +     *
3419 +     * @param parallelismThreshold the (estimated) number of elements
3420 +     * needed for this operation to be executed in parallel
3421 +     * @param transformer a function returning the transformation
3422 +     * for an element, or null if there is no transformation (in
3423 +     * which case the action is not applied)
3424 +     * @param action the action
3425 +     * @since 1.8
3426 +     */
3427 +    public <U> void forEach(long parallelismThreshold,
3428 +                            BiFun<? super K, ? super V, ? extends U> transformer,
3429 +                            Action<? super U> action) {
3430 +        if (transformer == null || action == null)
3431 +            throw new NullPointerException();
3432 +        new ForEachTransformedMappingTask<K,V,U>
3433 +            (null, batchFor(parallelismThreshold), 0, 0, table,
3434 +             transformer, action).invoke();
3435 +    }
3436 +
3437 +    /**
3438 +     * Returns a non-null result from applying the given search
3439 +     * function on each (key, value), or null if none.  Upon
3440 +     * success, further element processing is suppressed and the
3441 +     * results of any other parallel invocations of the search
3442 +     * function are ignored.
3443 +     *
3444 +     * @param parallelismThreshold the (estimated) number of elements
3445 +     * needed for this operation to be executed in parallel
3446 +     * @param searchFunction a function returning a non-null
3447 +     * result on success, else null
3448 +     * @return a non-null result from applying the given search
3449 +     * function on each (key, value), or null if none
3450 +     * @since 1.8
3451 +     */
3452 +    public <U> U search(long parallelismThreshold,
3453 +                        BiFun<? super K, ? super V, ? extends U> searchFunction) {
3454 +        if (searchFunction == null) throw new NullPointerException();
3455 +        return new SearchMappingsTask<K,V,U>
3456 +            (null, batchFor(parallelismThreshold), 0, 0, table,
3457 +             searchFunction, new AtomicReference<U>()).invoke();
3458 +    }
3459 +
3460 +    /**
3461 +     * Returns the result of accumulating the given transformation
3462 +     * of all (key, value) pairs using the given reducer to
3463 +     * combine values, or null if none.
3464 +     *
3465 +     * @param parallelismThreshold the (estimated) number of elements
3466 +     * needed for this operation to be executed in parallel
3467 +     * @param transformer a function returning the transformation
3468 +     * for an element, or null if there is no transformation (in
3469 +     * which case it is not combined)
3470 +     * @param reducer a commutative associative combining function
3471 +     * @return the result of accumulating the given transformation
3472 +     * of all (key, value) pairs
3473 +     * @since 1.8
3474 +     */
3475 +    public <U> U reduce(long parallelismThreshold,
3476 +                        BiFun<? super K, ? super V, ? extends U> transformer,
3477 +                        BiFun<? super U, ? super U, ? extends U> reducer) {
3478 +        if (transformer == null || reducer == null)
3479 +            throw new NullPointerException();
3480 +        return new MapReduceMappingsTask<K,V,U>
3481 +            (null, batchFor(parallelismThreshold), 0, 0, table,
3482 +             null, transformer, reducer).invoke();
3483 +    }
3484 +
3485 +    /**
3486 +     * Returns the result of accumulating the given transformation
3487 +     * of all (key, value) pairs using the given reducer to
3488 +     * combine values, and the given basis as an identity value.
3489 +     *
3490 +     * @param parallelismThreshold the (estimated) number of elements
3491 +     * needed for this operation to be executed in parallel
3492 +     * @param transformer a function returning the transformation
3493 +     * for an element
3494 +     * @param basis the identity (initial default value) for the reduction
3495 +     * @param reducer a commutative associative combining function
3496 +     * @return the result of accumulating the given transformation
3497 +     * of all (key, value) pairs
3498 +     * @since 1.8
3499 +     */
3500 +    public double reduceToDoubleIn(long parallelismThreshold,
3501 +                                   ObjectByObjectToDouble<? super K, ? super V> transformer,
3502 +                                   double basis,
3503 +                                   DoubleByDoubleToDouble reducer) {
3504 +        if (transformer == null || reducer == null)
3505 +            throw new NullPointerException();
3506 +        return new MapReduceMappingsToDoubleTask<K,V>
3507 +            (null, batchFor(parallelismThreshold), 0, 0, table,
3508 +             null, transformer, basis, reducer).invoke();
3509 +    }
3510 +
3511 +    /**
3512 +     * Returns the result of accumulating the given transformation
3513 +     * of all (key, value) pairs using the given reducer to
3514 +     * combine values, and the given basis as an identity value.
3515 +     *
3516 +     * @param parallelismThreshold the (estimated) number of elements
3517 +     * needed for this operation to be executed in parallel
3518 +     * @param transformer a function returning the transformation
3519 +     * for an element
3520 +     * @param basis the identity (initial default value) for the reduction
3521 +     * @param reducer a commutative associative combining function
3522 +     * @return the result of accumulating the given transformation
3523 +     * of all (key, value) pairs
3524 +     * @since 1.8
3525 +     */
3526 +    public long reduceToLong(long parallelismThreshold,
3527 +                             ObjectByObjectToLong<? super K, ? super V> transformer,
3528 +                             long basis,
3529 +                             LongByLongToLong reducer) {
3530 +        if (transformer == null || reducer == null)
3531 +            throw new NullPointerException();
3532 +        return new MapReduceMappingsToLongTask<K,V>
3533 +            (null, batchFor(parallelismThreshold), 0, 0, table,
3534 +             null, transformer, basis, reducer).invoke();
3535 +    }
3536 +
3537 +    /**
3538 +     * Returns the result of accumulating the given transformation
3539 +     * of all (key, value) pairs using the given reducer to
3540 +     * combine values, and the given basis as an identity value.
3541 +     *
3542 +     * @param parallelismThreshold the (estimated) number of elements
3543 +     * needed for this operation to be executed in parallel
3544 +     * @param transformer a function returning the transformation
3545 +     * for an element
3546 +     * @param basis the identity (initial default value) for the reduction
3547 +     * @param reducer a commutative associative combining function
3548 +     * @return the result of accumulating the given transformation
3549 +     * of all (key, value) pairs
3550 +     * @since 1.8
3551 +     */
3552 +    public int reduceToInt(long parallelismThreshold,
3553 +                           ObjectByObjectToInt<? super K, ? super V> transformer,
3554 +                           int basis,
3555 +                           IntByIntToInt reducer) {
3556 +        if (transformer == null || reducer == null)
3557 +            throw new NullPointerException();
3558 +        return new MapReduceMappingsToIntTask<K,V>
3559 +            (null, batchFor(parallelismThreshold), 0, 0, table,
3560 +             null, transformer, basis, reducer).invoke();
3561 +    }
3562 +
3563 +    /**
3564 +     * Performs the given action for each key.
3565 +     *
3566 +     * @param parallelismThreshold the (estimated) number of elements
3567 +     * needed for this operation to be executed in parallel
3568 +     * @param action the action
3569 +     * @since 1.8
3570 +     */
3571 +    public void forEachKey(long parallelismThreshold,
3572 +                           Action<? super K> action) {
3573 +        if (action == null) throw new NullPointerException();
3574 +        new ForEachKeyTask<K,V>
3575 +            (null, batchFor(parallelismThreshold), 0, 0, table,
3576 +             action).invoke();
3577 +    }
3578 +
3579 +    /**
3580 +     * Performs the given action for each non-null transformation
3581 +     * of each key.
3582 +     *
3583 +     * @param parallelismThreshold the (estimated) number of elements
3584 +     * needed for this operation to be executed in parallel
3585 +     * @param transformer a function returning the transformation
3586 +     * for an element, or null if there is no transformation (in
3587 +     * which case the action is not applied)
3588 +     * @param action the action
3589 +     * @since 1.8
3590 +     */
3591 +    public <U> void forEachKey(long parallelismThreshold,
3592 +                               Fun<? super K, ? extends U> transformer,
3593 +                               Action<? super U> action) {
3594 +        if (transformer == null || action == null)
3595 +            throw new NullPointerException();
3596 +        new ForEachTransformedKeyTask<K,V,U>
3597 +            (null, batchFor(parallelismThreshold), 0, 0, table,
3598 +             transformer, action).invoke();
3599 +    }
3600 +
3601 +    /**
3602 +     * Returns a non-null result from applying the given search
3603 +     * function on each key, or null if none. Upon success,
3604 +     * further element processing is suppressed and the results of
3605 +     * any other parallel invocations of the search function are
3606 +     * ignored.
3607 +     *
3608 +     * @param parallelismThreshold the (estimated) number of elements
3609 +     * needed for this operation to be executed in parallel
3610 +     * @param searchFunction a function returning a non-null
3611 +     * result on success, else null
3612 +     * @return a non-null result from applying the given search
3613 +     * function on each key, or null if none
3614 +     * @since 1.8
3615 +     */
3616 +    public <U> U searchKeys(long parallelismThreshold,
3617 +                            Fun<? super K, ? extends U> searchFunction) {
3618 +        if (searchFunction == null) throw new NullPointerException();
3619 +        return new SearchKeysTask<K,V,U>
3620 +            (null, batchFor(parallelismThreshold), 0, 0, table,
3621 +             searchFunction, new AtomicReference<U>()).invoke();
3622 +    }
3623 +
3624 +    /**
3625 +     * Returns the result of accumulating all keys using the given
3626 +     * reducer to combine values, or null if none.
3627 +     *
3628 +     * @param parallelismThreshold the (estimated) number of elements
3629 +     * needed for this operation to be executed in parallel
3630 +     * @param reducer a commutative associative combining function
3631 +     * @return the result of accumulating all keys using the given
3632 +     * reducer to combine values, or null if none
3633 +     * @since 1.8
3634 +     */
3635 +    public K reduceKeys(long parallelismThreshold,
3636 +                        BiFun<? super K, ? super K, ? extends K> reducer) {
3637 +        if (reducer == null) throw new NullPointerException();
3638 +        return new ReduceKeysTask<K,V>
3639 +            (null, batchFor(parallelismThreshold), 0, 0, table,
3640 +             null, reducer).invoke();
3641 +    }
3642 +
3643 +    /**
3644 +     * Returns the result of accumulating the given transformation
3645 +     * of all keys using the given reducer to combine values, or
3646 +     * null if none.
3647 +     *
3648 +     * @param parallelismThreshold the (estimated) number of elements
3649 +     * needed for this operation to be executed in parallel
3650 +     * @param transformer a function returning the transformation
3651 +     * for an element, or null if there is no transformation (in
3652 +     * which case it is not combined)
3653 +     * @param reducer a commutative associative combining function
3654 +     * @return the result of accumulating the given transformation
3655 +     * of all keys
3656 +     * @since 1.8
3657 +     */
3658 +    public <U> U reduceKeys(long parallelismThreshold,
3659 +                            Fun<? super K, ? extends U> transformer,
3660 +         BiFun<? super U, ? super U, ? extends U> reducer) {
3661 +        if (transformer == null || reducer == null)
3662 +            throw new NullPointerException();
3663 +        return new MapReduceKeysTask<K,V,U>
3664 +            (null, batchFor(parallelismThreshold), 0, 0, table,
3665 +             null, transformer, reducer).invoke();
3666 +    }
3667 +
3668 +    /**
3669 +     * Returns the result of accumulating the given transformation
3670 +     * of all keys using the given reducer to combine values, and
3671 +     * the given basis as an identity value.
3672 +     *
3673 +     * @param parallelismThreshold the (estimated) number of elements
3674 +     * needed for this operation to be executed in parallel
3675 +     * @param transformer a function returning the transformation
3676 +     * for an element
3677 +     * @param basis the identity (initial default value) for the reduction
3678 +     * @param reducer a commutative associative combining function
3679 +     * @return the result of accumulating the given transformation
3680 +     * of all keys
3681 +     * @since 1.8
3682 +     */
3683 +    public double reduceKeysToDouble(long parallelismThreshold,
3684 +                                     ObjectToDouble<? super K> transformer,
3685 +                                     double basis,
3686 +                                     DoubleByDoubleToDouble reducer) {
3687 +        if (transformer == null || reducer == null)
3688 +            throw new NullPointerException();
3689 +        return new MapReduceKeysToDoubleTask<K,V>
3690 +            (null, batchFor(parallelismThreshold), 0, 0, table,
3691 +             null, transformer, basis, reducer).invoke();
3692 +    }
3693 +
3694 +    /**
3695 +     * Returns the result of accumulating the given transformation
3696 +     * of all keys using the given reducer to combine values, and
3697 +     * the given basis as an identity value.
3698 +     *
3699 +     * @param parallelismThreshold the (estimated) number of elements
3700 +     * needed for this operation to be executed in parallel
3701 +     * @param transformer a function returning the transformation
3702 +     * for an element
3703 +     * @param basis the identity (initial default value) for the reduction
3704 +     * @param reducer a commutative associative combining function
3705 +     * @return the result of accumulating the given transformation
3706 +     * of all keys
3707 +     * @since 1.8
3708 +     */
3709 +    public long reduceKeysToLong(long parallelismThreshold,
3710 +                                 ObjectToLong<? super K> transformer,
3711 +                                 long basis,
3712 +                                 LongByLongToLong reducer) {
3713 +        if (transformer == null || reducer == null)
3714 +            throw new NullPointerException();
3715 +        return new MapReduceKeysToLongTask<K,V>
3716 +            (null, batchFor(parallelismThreshold), 0, 0, table,
3717 +             null, transformer, basis, reducer).invoke();
3718 +    }
3719 +
3720 +    /**
3721 +     * Returns the result of accumulating the given transformation
3722 +     * of all keys using the given reducer to combine values, and
3723 +     * the given basis as an identity value.
3724 +     *
3725 +     * @param parallelismThreshold the (estimated) number of elements
3726 +     * needed for this operation to be executed in parallel
3727 +     * @param transformer a function returning the transformation
3728 +     * for an element
3729 +     * @param basis the identity (initial default value) for the reduction
3730 +     * @param reducer a commutative associative combining function
3731 +     * @return the result of accumulating the given transformation
3732 +     * of all keys
3733 +     * @since 1.8
3734 +     */
3735 +    public int reduceKeysToInt(long parallelismThreshold,
3736 +                               ObjectToInt<? super K> transformer,
3737 +                               int basis,
3738 +                               IntByIntToInt reducer) {
3739 +        if (transformer == null || reducer == null)
3740 +            throw new NullPointerException();
3741 +        return new MapReduceKeysToIntTask<K,V>
3742 +            (null, batchFor(parallelismThreshold), 0, 0, table,
3743 +             null, transformer, basis, reducer).invoke();
3744 +    }
3745 +
3746 +    /**
3747 +     * Performs the given action for each value.
3748 +     *
3749 +     * @param parallelismThreshold the (estimated) number of elements
3750 +     * needed for this operation to be executed in parallel
3751 +     * @param action the action
3752 +     * @since 1.8
3753 +     */
3754 +    public void forEachValue(long parallelismThreshold,
3755 +                             Action<? super V> action) {
3756 +        if (action == null)
3757 +            throw new NullPointerException();
3758 +        new ForEachValueTask<K,V>
3759 +            (null, batchFor(parallelismThreshold), 0, 0, table,
3760 +             action).invoke();
3761 +    }
3762 +
3763 +    /**
3764 +     * Performs the given action for each non-null transformation
3765 +     * of each value.
3766 +     *
3767 +     * @param parallelismThreshold the (estimated) number of elements
3768 +     * needed for this operation to be executed in parallel
3769 +     * @param transformer a function returning the transformation
3770 +     * for an element, or null if there is no transformation (in
3771 +     * which case the action is not applied)
3772 +     * @param action the action
3773 +     * @since 1.8
3774 +     */
3775 +    public <U> void forEachValue(long parallelismThreshold,
3776 +                                 Fun<? super V, ? extends U> transformer,
3777 +                                 Action<? super U> action) {
3778 +        if (transformer == null || action == null)
3779 +            throw new NullPointerException();
3780 +        new ForEachTransformedValueTask<K,V,U>
3781 +            (null, batchFor(parallelismThreshold), 0, 0, table,
3782 +             transformer, action).invoke();
3783 +    }
3784 +
3785 +    /**
3786 +     * Returns a non-null result from applying the given search
3787 +     * function on each value, or null if none.  Upon success,
3788 +     * further element processing is suppressed and the results of
3789 +     * any other parallel invocations of the search function are
3790 +     * ignored.
3791 +     *
3792 +     * @param parallelismThreshold the (estimated) number of elements
3793 +     * needed for this operation to be executed in parallel
3794 +     * @param searchFunction a function returning a non-null
3795 +     * result on success, else null
3796 +     * @return a non-null result from applying the given search
3797 +     * function on each value, or null if none
3798 +     * @since 1.8
3799 +     */
3800 +    public <U> U searchValues(long parallelismThreshold,
3801 +                              Fun<? super V, ? extends U> searchFunction) {
3802 +        if (searchFunction == null) throw new NullPointerException();
3803 +        return new SearchValuesTask<K,V,U>
3804 +            (null, batchFor(parallelismThreshold), 0, 0, table,
3805 +             searchFunction, new AtomicReference<U>()).invoke();
3806 +    }
3807 +
3808 +    /**
3809 +     * Returns the result of accumulating all values using the
3810 +     * given reducer to combine values, or null if none.
3811 +     *
3812 +     * @param parallelismThreshold the (estimated) number of elements
3813 +     * needed for this operation to be executed in parallel
3814 +     * @param reducer a commutative associative combining function
3815 +     * @return the result of accumulating all values
3816 +     * @since 1.8
3817 +     */
3818 +    public V reduceValues(long parallelismThreshold,
3819 +                          BiFun<? super V, ? super V, ? extends V> reducer) {
3820 +        if (reducer == null) throw new NullPointerException();
3821 +        return new ReduceValuesTask<K,V>
3822 +            (null, batchFor(parallelismThreshold), 0, 0, table,
3823 +             null, reducer).invoke();
3824 +    }
3825 +
3826 +    /**
3827 +     * Returns the result of accumulating the given transformation
3828 +     * of all values using the given reducer to combine values, or
3829 +     * null if none.
3830 +     *
3831 +     * @param parallelismThreshold the (estimated) number of elements
3832 +     * needed for this operation to be executed in parallel
3833 +     * @param transformer a function returning the transformation
3834 +     * for an element, or null if there is no transformation (in
3835 +     * which case it is not combined)
3836 +     * @param reducer a commutative associative combining function
3837 +     * @return the result of accumulating the given transformation
3838 +     * of all values
3839 +     * @since 1.8
3840 +     */
3841 +    public <U> U reduceValues(long parallelismThreshold,
3842 +                              Fun<? super V, ? extends U> transformer,
3843 +                              BiFun<? super U, ? super U, ? extends U> reducer) {
3844 +        if (transformer == null || reducer == null)
3845 +            throw new NullPointerException();
3846 +        return new MapReduceValuesTask<K,V,U>
3847 +            (null, batchFor(parallelismThreshold), 0, 0, table,
3848 +             null, transformer, reducer).invoke();
3849 +    }
3850 +
3851 +    /**
3852 +     * Returns the result of accumulating the given transformation
3853 +     * of all values using the given reducer to combine values,
3854 +     * and the given basis as an identity value.
3855 +     *
3856 +     * @param parallelismThreshold the (estimated) number of elements
3857 +     * needed for this operation to be executed in parallel
3858 +     * @param transformer a function returning the transformation
3859 +     * for an element
3860 +     * @param basis the identity (initial default value) for the reduction
3861 +     * @param reducer a commutative associative combining function
3862 +     * @return the result of accumulating the given transformation
3863 +     * of all values
3864 +     * @since 1.8
3865 +     */
3866 +    public double reduceValuesToDouble(long parallelismThreshold,
3867 +                                       ObjectToDouble<? super V> transformer,
3868 +                                       double basis,
3869 +                                       DoubleByDoubleToDouble reducer) {
3870 +        if (transformer == null || reducer == null)
3871 +            throw new NullPointerException();
3872 +        return new MapReduceValuesToDoubleTask<K,V>
3873 +            (null, batchFor(parallelismThreshold), 0, 0, table,
3874 +             null, transformer, basis, reducer).invoke();
3875 +    }
3876 +
3877 +    /**
3878 +     * Returns the result of accumulating the given transformation
3879 +     * of all values using the given reducer to combine values,
3880 +     * and the given basis as an identity value.
3881 +     *
3882 +     * @param parallelismThreshold the (estimated) number of elements
3883 +     * needed for this operation to be executed in parallel
3884 +     * @param transformer a function returning the transformation
3885 +     * for an element
3886 +     * @param basis the identity (initial default value) for the reduction
3887 +     * @param reducer a commutative associative combining function
3888 +     * @return the result of accumulating the given transformation
3889 +     * of all values
3890 +     * @since 1.8
3891 +     */
3892 +    public long reduceValuesToLong(long parallelismThreshold,
3893 +                                   ObjectToLong<? super V> transformer,
3894 +                                   long basis,
3895 +                                   LongByLongToLong reducer) {
3896 +        if (transformer == null || reducer == null)
3897 +            throw new NullPointerException();
3898 +        return new MapReduceValuesToLongTask<K,V>
3899 +            (null, batchFor(parallelismThreshold), 0, 0, table,
3900 +             null, transformer, basis, reducer).invoke();
3901 +    }
3902 +
3903 +    /**
3904 +     * Returns the result of accumulating the given transformation
3905 +     * of all values using the given reducer to combine values,
3906 +     * and the given basis as an identity value.
3907 +     *
3908 +     * @param parallelismThreshold the (estimated) number of elements
3909 +     * needed for this operation to be executed in parallel
3910 +     * @param transformer a function returning the transformation
3911 +     * for an element
3912 +     * @param basis the identity (initial default value) for the reduction
3913 +     * @param reducer a commutative associative combining function
3914 +     * @return the result of accumulating the given transformation
3915 +     * of all values
3916 +     * @since 1.8
3917 +     */
3918 +    public int reduceValuesToInt(long parallelismThreshold,
3919 +                                 ObjectToInt<? super V> transformer,
3920 +                                 int basis,
3921 +                                 IntByIntToInt reducer) {
3922 +        if (transformer == null || reducer == null)
3923 +            throw new NullPointerException();
3924 +        return new MapReduceValuesToIntTask<K,V>
3925 +            (null, batchFor(parallelismThreshold), 0, 0, table,
3926 +             null, transformer, basis, reducer).invoke();
3927 +    }
3928 +
3929 +    /**
3930 +     * Performs the given action for each entry.
3931 +     *
3932 +     * @param parallelismThreshold the (estimated) number of elements
3933 +     * needed for this operation to be executed in parallel
3934 +     * @param action the action
3935 +     * @since 1.8
3936 +     */
3937 +    public void forEachEntry(long parallelismThreshold,
3938 +                             Action<? super Map.Entry<K,V>> action) {
3939 +        if (action == null) throw new NullPointerException();
3940 +        new ForEachEntryTask<K,V>(null, batchFor(parallelismThreshold), 0, 0, table,
3941 +                                  action).invoke();
3942 +    }
3943 +
3944 +    /**
3945 +     * Performs the given action for each non-null transformation
3946 +     * of each entry.
3947 +     *
3948 +     * @param parallelismThreshold the (estimated) number of elements
3949 +     * needed for this operation to be executed in parallel
3950 +     * @param transformer a function returning the transformation
3951 +     * for an element, or null if there is no transformation (in
3952 +     * which case the action is not applied)
3953 +     * @param action the action
3954 +     * @since 1.8
3955 +     */
3956 +    public <U> void forEachEntry(long parallelismThreshold,
3957 +                                 Fun<Map.Entry<K,V>, ? extends U> transformer,
3958 +                                 Action<? super U> action) {
3959 +        if (transformer == null || action == null)
3960 +            throw new NullPointerException();
3961 +        new ForEachTransformedEntryTask<K,V,U>
3962 +            (null, batchFor(parallelismThreshold), 0, 0, table,
3963 +             transformer, action).invoke();
3964 +    }
3965 +
3966 +    /**
3967 +     * Returns a non-null result from applying the given search
3968 +     * function on each entry, or null if none.  Upon success,
3969 +     * further element processing is suppressed and the results of
3970 +     * any other parallel invocations of the search function are
3971 +     * ignored.
3972 +     *
3973 +     * @param parallelismThreshold the (estimated) number of elements
3974 +     * needed for this operation to be executed in parallel
3975 +     * @param searchFunction a function returning a non-null
3976 +     * result on success, else null
3977 +     * @return a non-null result from applying the given search
3978 +     * function on each entry, or null if none
3979 +     * @since 1.8
3980 +     */
3981 +    public <U> U searchEntries(long parallelismThreshold,
3982 +                               Fun<Map.Entry<K,V>, ? extends U> searchFunction) {
3983 +        if (searchFunction == null) throw new NullPointerException();
3984 +        return new SearchEntriesTask<K,V,U>
3985 +            (null, batchFor(parallelismThreshold), 0, 0, table,
3986 +             searchFunction, new AtomicReference<U>()).invoke();
3987 +    }
3988 +
3989 +    /**
3990 +     * Returns the result of accumulating all entries using the
3991 +     * given reducer to combine values, or null if none.
3992 +     *
3993 +     * @param parallelismThreshold the (estimated) number of elements
3994 +     * needed for this operation to be executed in parallel
3995 +     * @param reducer a commutative associative combining function
3996 +     * @return the result of accumulating all entries
3997 +     * @since 1.8
3998 +     */
3999 +    public Map.Entry<K,V> reduceEntries(long parallelismThreshold,
4000 +                                        BiFun<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer) {
4001 +        if (reducer == null) throw new NullPointerException();
4002 +        return new ReduceEntriesTask<K,V>
4003 +            (null, batchFor(parallelismThreshold), 0, 0, table,
4004 +             null, reducer).invoke();
4005 +    }
4006 +
4007 +    /**
4008 +     * Returns the result of accumulating the given transformation
4009 +     * of all entries using the given reducer to combine values,
4010 +     * or null if none.
4011 +     *
4012 +     * @param parallelismThreshold the (estimated) number of elements
4013 +     * needed for this operation to be executed in parallel
4014 +     * @param transformer a function returning the transformation
4015 +     * for an element, or null if there is no transformation (in
4016 +     * which case it is not combined)
4017 +     * @param reducer a commutative associative combining function
4018 +     * @return the result of accumulating the given transformation
4019 +     * of all entries
4020 +     * @since 1.8
4021 +     */
4022 +    public <U> U reduceEntries(long parallelismThreshold,
4023 +                               Fun<Map.Entry<K,V>, ? extends U> transformer,
4024 +                               BiFun<? super U, ? super U, ? extends U> reducer) {
4025 +        if (transformer == null || reducer == null)
4026 +            throw new NullPointerException();
4027 +        return new MapReduceEntriesTask<K,V,U>
4028 +            (null, batchFor(parallelismThreshold), 0, 0, table,
4029 +             null, transformer, reducer).invoke();
4030 +    }
4031 +
4032 +    /**
4033 +     * Returns the result of accumulating the given transformation
4034 +     * of all entries using the given reducer to combine values,
4035 +     * and the given basis as an identity value.
4036 +     *
4037 +     * @param parallelismThreshold the (estimated) number of elements
4038 +     * needed for this operation to be executed in parallel
4039 +     * @param transformer a function returning the transformation
4040 +     * for an element
4041 +     * @param basis the identity (initial default value) for the reduction
4042 +     * @param reducer a commutative associative combining function
4043 +     * @return the result of accumulating the given transformation
4044 +     * of all entries
4045 +     * @since 1.8
4046 +     */
4047 +    public double reduceEntriesToDouble(long parallelismThreshold,
4048 +                                        ObjectToDouble<Map.Entry<K,V>> transformer,
4049 +                                        double basis,
4050 +                                        DoubleByDoubleToDouble reducer) {
4051 +        if (transformer == null || reducer == null)
4052 +            throw new NullPointerException();
4053 +        return new MapReduceEntriesToDoubleTask<K,V>
4054 +            (null, batchFor(parallelismThreshold), 0, 0, table,
4055 +             null, transformer, basis, reducer).invoke();
4056 +    }
4057 +
4058 +    /**
4059 +     * Returns the result of accumulating the given transformation
4060 +     * of all entries using the given reducer to combine values,
4061 +     * and the given basis as an identity value.
4062 +     *
4063 +     * @param parallelismThreshold the (estimated) number of elements
4064 +     * needed for this operation to be executed in parallel
4065 +     * @param transformer a function returning the transformation
4066 +     * for an element
4067 +     * @param basis the identity (initial default value) for the reduction
4068 +     * @param reducer a commutative associative combining function
4069 +     * @return the result of accumulating the given transformation
4070 +     * of all entries
4071 +     * @since 1.8
4072 +     */
4073 +    public long reduceEntriesToLong(long parallelismThreshold,
4074 +                                    ObjectToLong<Map.Entry<K,V>> transformer,
4075 +                                    long basis,
4076 +                                    LongByLongToLong reducer) {
4077 +        if (transformer == null || reducer == null)
4078 +            throw new NullPointerException();
4079 +        return new MapReduceEntriesToLongTask<K,V>
4080 +            (null, batchFor(parallelismThreshold), 0, 0, table,
4081 +             null, transformer, basis, reducer).invoke();
4082 +    }
4083 +
4084 +    /**
4085 +     * Returns the result of accumulating the given transformation
4086 +     * of all entries using the given reducer to combine values,
4087 +     * and the given basis as an identity value.
4088 +     *
4089 +     * @param parallelismThreshold the (estimated) number of elements
4090 +     * needed for this operation to be executed in parallel
4091 +     * @param transformer a function returning the transformation
4092 +     * for an element
4093 +     * @param basis the identity (initial default value) for the reduction
4094 +     * @param reducer a commutative associative combining function
4095 +     * @return the result of accumulating the given transformation
4096 +     * of all entries
4097 +     * @since 1.8
4098 +     */
4099 +    public int reduceEntriesToInt(long parallelismThreshold,
4100 +                                  ObjectToInt<Map.Entry<K,V>> transformer,
4101 +                                  int basis,
4102 +                                  IntByIntToInt reducer) {
4103 +        if (transformer == null || reducer == null)
4104 +            throw new NullPointerException();
4105 +        return new MapReduceEntriesToIntTask<K,V>
4106 +            (null, batchFor(parallelismThreshold), 0, 0, table,
4107 +             null, transformer, basis, reducer).invoke();
4108 +    }
4109 +
4110 +
4111 +    /* ----------------Views -------------- */
4112 +
4113 +    /**
4114 +     * Base class for views.
4115 +     */
4116 +    abstract static class CollectionView<K,V,E>
4117 +        implements Collection<E>, java.io.Serializable {
4118 +        private static final long serialVersionUID = 7249069246763182397L;
4119 +        final ConcurrentHashMapV8<K,V> map;
4120 +        CollectionView(ConcurrentHashMapV8<K,V> map)  { this.map = map; }
4121 +
4122          /**
4123 <         * Returns a task that when invoked, returns the result of
4294 <         * accumulating the given transformation of all (key, value) pairs
4295 <         * using the given reducer to combine values, and the given
4296 <         * basis as an identity value.
4123 >         * Returns the map backing this view.
4124           *
4125 <         * @param transformer a function returning the transformation
4299 <         * for an element
4300 <         * @param basis the identity (initial default value) for the reduction
4301 <         * @param reducer a commutative associative combining function
4302 <         * @return the task
4125 >         * @return the map backing this view
4126           */
4127 <        public static <K,V> ForkJoinTask<Integer> reduceToInt
4305 <            (ConcurrentHashMapV8<K,V> map,
4306 <             ObjectByObjectToInt<? super K, ? super V> transformer,
4307 <             int basis,
4308 <             IntByIntToInt reducer) {
4309 <            if (transformer == null || reducer == null)
4310 <                throw new NullPointerException();
4311 <            return new MapReduceMappingsToIntTask<K,V>
4312 <                (map, transformer, basis, reducer);
4313 <        }
4127 >        public ConcurrentHashMapV8<K,V> getMap() { return map; }
4128  
4129          /**
4130 <         * Returns a task that when invoked, performs the given action
4131 <         * for each key.
4318 <         *
4319 <         * @param map the map
4320 <         * @param action the action
4321 <         * @return the task
4130 >         * Removes all of the elements from this view, by removing all
4131 >         * the mappings from the map backing this view.
4132           */
4133 <        public static <K,V> ForkJoinTask<Void> forEachKey
4134 <            (ConcurrentHashMapV8<K,V> map,
4135 <             Action<K> action) {
4326 <            if (action == null) throw new NullPointerException();
4327 <            return new ForEachKeyTask<K,V>(map, action);
4328 <        }
4133 >        public final void clear()      { map.clear(); }
4134 >        public final int size()        { return map.size(); }
4135 >        public final boolean isEmpty() { return map.isEmpty(); }
4136  
4137 +        // implementations below rely on concrete classes supplying these
4138 +        // abstract methods
4139          /**
4140 <         * Returns a task that when invoked, performs the given action
4141 <         * for each non-null transformation of each key.
4142 <         *
4143 <         * @param map the map
4144 <         * @param transformer a function returning the transformation
4145 <         * for an element, or null of there is no transformation (in
4146 <         * which case the action is not applied).
4147 <         * @param action the action
4148 <         * @return the task
4149 <         */
4150 <        public static <K,V,U> ForkJoinTask<Void> forEachKey
4151 <            (ConcurrentHashMapV8<K,V> map,
4152 <             Fun<? super K, ? extends U> transformer,
4153 <             Action<U> action) {
4154 <            if (transformer == null || action == null)
4155 <                throw new NullPointerException();
4156 <            return new ForEachTransformedKeyTask<K,V,U>
4157 <                (map, transformer, action);
4140 >         * Returns a "weakly consistent" iterator that will never
4141 >         * throw {@link ConcurrentModificationException}, and
4142 >         * guarantees to traverse elements as they existed upon
4143 >         * construction of the iterator, and may (but is not
4144 >         * guaranteed to) reflect any modifications subsequent to
4145 >         * construction.
4146 >         */
4147 >        public abstract Iterator<E> iterator();
4148 >        public abstract boolean contains(Object o);
4149 >        public abstract boolean remove(Object o);
4150 >
4151 >        private static final String oomeMsg = "Required array size too large";
4152 >
4153 >        public final Object[] toArray() {
4154 >            long sz = map.mappingCount();
4155 >            if (sz > MAX_ARRAY_SIZE)
4156 >                throw new OutOfMemoryError(oomeMsg);
4157 >            int n = (int)sz;
4158 >            Object[] r = new Object[n];
4159 >            int i = 0;
4160 >            for (E e : this) {
4161 >                if (i == n) {
4162 >                    if (n >= MAX_ARRAY_SIZE)
4163 >                        throw new OutOfMemoryError(oomeMsg);
4164 >                    if (n >= MAX_ARRAY_SIZE - (MAX_ARRAY_SIZE >>> 1) - 1)
4165 >                        n = MAX_ARRAY_SIZE;
4166 >                    else
4167 >                        n += (n >>> 1) + 1;
4168 >                    r = Arrays.copyOf(r, n);
4169 >                }
4170 >                r[i++] = e;
4171 >            }
4172 >            return (i == n) ? r : Arrays.copyOf(r, i);
4173          }
4174  
4175 <        /**
4176 <         * Returns a task that when invoked, returns a non-null result
4177 <         * from applying the given search function on each key, or
4178 <         * null if none.  Upon success, further element processing is
4179 <         * suppressed and the results of any other parallel
4180 <         * invocations of the search function are ignored.
4181 <         *
4182 <         * @param map the map
4183 <         * @param searchFunction a function returning a non-null
4184 <         * result on success, else null
4185 <         * @return the task
4186 <         */
4187 <        public static <K,V,U> ForkJoinTask<U> searchKeys
4188 <            (ConcurrentHashMapV8<K,V> map,
4189 <             Fun<? super K, ? extends U> searchFunction) {
4190 <            if (searchFunction == null) throw new NullPointerException();
4191 <            return new SearchKeysTask<K,V,U>
4192 <                (map, searchFunction,
4193 <                 new AtomicReference<U>());
4175 >        @SuppressWarnings("unchecked")
4176 >        public final <T> T[] toArray(T[] a) {
4177 >            long sz = map.mappingCount();
4178 >            if (sz > MAX_ARRAY_SIZE)
4179 >                throw new OutOfMemoryError(oomeMsg);
4180 >            int m = (int)sz;
4181 >            T[] r = (a.length >= m) ? a :
4182 >                (T[])java.lang.reflect.Array
4183 >                .newInstance(a.getClass().getComponentType(), m);
4184 >            int n = r.length;
4185 >            int i = 0;
4186 >            for (E e : this) {
4187 >                if (i == n) {
4188 >                    if (n >= MAX_ARRAY_SIZE)
4189 >                        throw new OutOfMemoryError(oomeMsg);
4190 >                    if (n >= MAX_ARRAY_SIZE - (MAX_ARRAY_SIZE >>> 1) - 1)
4191 >                        n = MAX_ARRAY_SIZE;
4192 >                    else
4193 >                        n += (n >>> 1) + 1;
4194 >                    r = Arrays.copyOf(r, n);
4195 >                }
4196 >                r[i++] = (T)e;
4197 >            }
4198 >            if (a == r && i < n) {
4199 >                r[i] = null; // null-terminate
4200 >                return r;
4201 >            }
4202 >            return (i == n) ? r : Arrays.copyOf(r, i);
4203          }
4204  
4205          /**
4206 <         * Returns a task that when invoked, returns the result of
4207 <         * accumulating all keys using the given reducer to combine
4208 <         * values, or null if none.
4206 >         * Returns a string representation of this collection.
4207 >         * The string representation consists of the string representations
4208 >         * of the collection's elements in the order they are returned by
4209 >         * its iterator, enclosed in square brackets ({@code "[]"}).
4210 >         * Adjacent elements are separated by the characters {@code ", "}
4211 >         * (comma and space).  Elements are converted to strings as by
4212 >         * {@link String#valueOf(Object)}.
4213           *
4214 <         * @param map the map
4378 <         * @param reducer a commutative associative combining function
4379 <         * @return the task
4214 >         * @return a string representation of this collection
4215           */
4216 <        public static <K,V> ForkJoinTask<K> reduceKeys
4217 <            (ConcurrentHashMapV8<K,V> map,
4218 <             BiFun<? super K, ? super K, ? extends K> reducer) {
4219 <            if (reducer == null) throw new NullPointerException();
4220 <            return new ReduceKeysTask<K,V>
4221 <                (map, reducer);
4216 >        public final String toString() {
4217 >            StringBuilder sb = new StringBuilder();
4218 >            sb.append('[');
4219 >            Iterator<E> it = iterator();
4220 >            if (it.hasNext()) {
4221 >                for (;;) {
4222 >                    Object e = it.next();
4223 >                    sb.append(e == this ? "(this Collection)" : e);
4224 >                    if (!it.hasNext())
4225 >                        break;
4226 >                    sb.append(',').append(' ');
4227 >                }
4228 >            }
4229 >            return sb.append(']').toString();
4230          }
4231  
4232 <        /**
4233 <         * Returns a task that when invoked, returns the result of
4234 <         * accumulating the given transformation of all keys using the given
4235 <         * reducer to combine values, or null if none.
4236 <         *
4237 <         * @param map the map
4238 <         * @param transformer a function returning the transformation
4239 <         * for an element, or null of there is no transformation (in
4397 <         * which case it is not combined).
4398 <         * @param reducer a commutative associative combining function
4399 <         * @return the task
4400 <         */
4401 <        public static <K,V,U> ForkJoinTask<U> reduceKeys
4402 <            (ConcurrentHashMapV8<K,V> map,
4403 <             Fun<? super K, ? extends U> transformer,
4404 <             BiFun<? super U, ? super U, ? extends U> reducer) {
4405 <            if (transformer == null || reducer == null)
4406 <                throw new NullPointerException();
4407 <            return new MapReduceKeysTask<K,V,U>
4408 <                (map, transformer, reducer);
4232 >        public final boolean containsAll(Collection<?> c) {
4233 >            if (c != this) {
4234 >                for (Object e : c) {
4235 >                    if (e == null || !contains(e))
4236 >                        return false;
4237 >                }
4238 >            }
4239 >            return true;
4240          }
4241  
4242 <        /**
4243 <         * Returns a task that when invoked, returns the result of
4244 <         * accumulating the given transformation of all keys using the given
4245 <         * reducer to combine values, and the given basis as an
4246 <         * identity value.
4247 <         *
4248 <         * @param map the map
4249 <         * @param transformer a function returning the transformation
4250 <         * for an element
4420 <         * @param basis the identity (initial default value) for the reduction
4421 <         * @param reducer a commutative associative combining function
4422 <         * @return the task
4423 <         */
4424 <        public static <K,V> ForkJoinTask<Double> reduceKeysToDouble
4425 <            (ConcurrentHashMapV8<K,V> map,
4426 <             ObjectToDouble<? super K> transformer,
4427 <             double basis,
4428 <             DoubleByDoubleToDouble reducer) {
4429 <            if (transformer == null || reducer == null)
4430 <                throw new NullPointerException();
4431 <            return new MapReduceKeysToDoubleTask<K,V>
4432 <                (map, transformer, basis, reducer);
4242 >        public final boolean removeAll(Collection<?> c) {
4243 >            boolean modified = false;
4244 >            for (Iterator<E> it = iterator(); it.hasNext();) {
4245 >                if (c.contains(it.next())) {
4246 >                    it.remove();
4247 >                    modified = true;
4248 >                }
4249 >            }
4250 >            return modified;
4251          }
4252  
4253 <        /**
4254 <         * Returns a task that when invoked, returns the result of
4255 <         * accumulating the given transformation of all keys using the given
4256 <         * reducer to combine values, and the given basis as an
4257 <         * identity value.
4258 <         *
4259 <         * @param map the map
4260 <         * @param transformer a function returning the transformation
4261 <         * for an element
4444 <         * @param basis the identity (initial default value) for the reduction
4445 <         * @param reducer a commutative associative combining function
4446 <         * @return the task
4447 <         */
4448 <        public static <K,V> ForkJoinTask<Long> reduceKeysToLong
4449 <            (ConcurrentHashMapV8<K,V> map,
4450 <             ObjectToLong<? super K> transformer,
4451 <             long basis,
4452 <             LongByLongToLong reducer) {
4453 <            if (transformer == null || reducer == null)
4454 <                throw new NullPointerException();
4455 <            return new MapReduceKeysToLongTask<K,V>
4456 <                (map, transformer, basis, reducer);
4253 >        public final boolean retainAll(Collection<?> c) {
4254 >            boolean modified = false;
4255 >            for (Iterator<E> it = iterator(); it.hasNext();) {
4256 >                if (!c.contains(it.next())) {
4257 >                    it.remove();
4258 >                    modified = true;
4259 >                }
4260 >            }
4261 >            return modified;
4262          }
4263  
4264 <        /**
4265 <         * Returns a task that when invoked, returns the result of
4266 <         * accumulating the given transformation of all keys using the given
4267 <         * reducer to combine values, and the given basis as an
4268 <         * identity value.
4269 <         *
4270 <         * @param map the map
4271 <         * @param transformer a function returning the transformation
4272 <         * for an element
4273 <         * @param basis the identity (initial default value) for the reduction
4274 <         * @param reducer a commutative associative combining function
4275 <         * @return the task
4276 <         */
4277 <        public static <K,V> ForkJoinTask<Integer> reduceKeysToInt
4278 <            (ConcurrentHashMapV8<K,V> map,
4279 <             ObjectToInt<? super K> transformer,
4280 <             int basis,
4281 <             IntByIntToInt reducer) {
4282 <            if (transformer == null || reducer == null)
4283 <                throw new NullPointerException();
4479 <            return new MapReduceKeysToIntTask<K,V>
4480 <                (map, transformer, basis, reducer);
4264 >    }
4265 >
4266 >    /**
4267 >     * A view of a ConcurrentHashMapV8 as a {@link Set} of keys, in
4268 >     * which additions may optionally be enabled by mapping to a
4269 >     * common value.  This class cannot be directly instantiated.
4270 >     * See {@link #keySet() keySet()},
4271 >     * {@link #keySet(Object) keySet(V)},
4272 >     * {@link #newKeySet() newKeySet()},
4273 >     * {@link #newKeySet(int) newKeySet(int)}.
4274 >     *
4275 >     * @since 1.8
4276 >     */
4277 >    public static class KeySetView<K,V> extends CollectionView<K,V,K>
4278 >        implements Set<K>, java.io.Serializable {
4279 >        private static final long serialVersionUID = 7249069246763182397L;
4280 >        private final V value;
4281 >        KeySetView(ConcurrentHashMapV8<K,V> map, V value) {  // non-public
4282 >            super(map);
4283 >            this.value = value;
4284          }
4285  
4286          /**
4287 <         * Returns a task that when invoked, performs the given action
4288 <         * for each value.
4287 >         * Returns the default mapped value for additions,
4288 >         * or {@code null} if additions are not supported.
4289           *
4290 <         * @param map the map
4291 <         * @param action the action
4290 >         * @return the default mapped value for additions, or {@code null}
4291 >         * if not supported
4292           */
4293 <        public static <K,V> ForkJoinTask<Void> forEachValue
4491 <            (ConcurrentHashMapV8<K,V> map,
4492 <             Action<V> action) {
4493 <            if (action == null) throw new NullPointerException();
4494 <            return new ForEachValueTask<K,V>(map, action);
4495 <        }
4293 >        public V getMappedValue() { return value; }
4294  
4295          /**
4296 <         * Returns a task that when invoked, performs the given action
4297 <         * for each non-null transformation of each value.
4500 <         *
4501 <         * @param map the map
4502 <         * @param transformer a function returning the transformation
4503 <         * for an element, or null of there is no transformation (in
4504 <         * which case the action is not applied).
4505 <         * @param action the action
4296 >         * {@inheritDoc}
4297 >         * @throws NullPointerException if the specified key is null
4298           */
4299 <        public static <K,V,U> ForkJoinTask<Void> forEachValue
4508 <            (ConcurrentHashMapV8<K,V> map,
4509 <             Fun<? super V, ? extends U> transformer,
4510 <             Action<U> action) {
4511 <            if (transformer == null || action == null)
4512 <                throw new NullPointerException();
4513 <            return new ForEachTransformedValueTask<K,V,U>
4514 <                (map, transformer, action);
4515 <        }
4299 >        public boolean contains(Object o) { return map.containsKey(o); }
4300  
4301          /**
4302 <         * Returns a task that when invoked, returns a non-null result
4303 <         * from applying the given search function on each value, or
4304 <         * null if none.  Upon success, further element processing is
4521 <         * suppressed and the results of any other parallel
4522 <         * invocations of the search function are ignored.
4523 <         *
4524 <         * @param map the map
4525 <         * @param searchFunction a function returning a non-null
4526 <         * result on success, else null
4527 <         * @return the task
4302 >         * Removes the key from this map view, by removing the key (and its
4303 >         * corresponding value) from the backing map.  This method does
4304 >         * nothing if the key is not in the map.
4305           *
4306 +         * @param  o the key to be removed from the backing map
4307 +         * @return {@code true} if the backing map contained the specified key
4308 +         * @throws NullPointerException if the specified key is null
4309           */
4310 <        public static <K,V,U> ForkJoinTask<U> searchValues
4531 <            (ConcurrentHashMapV8<K,V> map,
4532 <             Fun<? super V, ? extends U> searchFunction) {
4533 <            if (searchFunction == null) throw new NullPointerException();
4534 <            return new SearchValuesTask<K,V,U>
4535 <                (map, searchFunction,
4536 <                 new AtomicReference<U>());
4537 <        }
4310 >        public boolean remove(Object o) { return map.remove(o) != null; }
4311  
4312          /**
4313 <         * Returns a task that when invoked, returns the result of
4541 <         * accumulating all values using the given reducer to combine
4542 <         * values, or null if none.
4543 <         *
4544 <         * @param map the map
4545 <         * @param reducer a commutative associative combining function
4546 <         * @return the task
4313 >         * @return an iterator over the keys of the backing map
4314           */
4315 <        public static <K,V> ForkJoinTask<V> reduceValues
4316 <            (ConcurrentHashMapV8<K,V> map,
4317 <             BiFun<? super V, ? super V, ? extends V> reducer) {
4318 <            if (reducer == null) throw new NullPointerException();
4319 <            return new ReduceValuesTask<K,V>
4553 <                (map, reducer);
4315 >        public Iterator<K> iterator() {
4316 >            Node<K,V>[] t;
4317 >            ConcurrentHashMapV8<K,V> m = map;
4318 >            int f = (t = m.table) == null ? 0 : t.length;
4319 >            return new KeyIterator<K,V>(t, f, 0, f, m);
4320          }
4321  
4322          /**
4323 <         * Returns a task that when invoked, returns the result of
4324 <         * accumulating the given transformation of all values using the
4559 <         * given reducer to combine values, or null if none.
4323 >         * Adds the specified key to this set view by mapping the key to
4324 >         * the default mapped value in the backing map, if defined.
4325           *
4326 <         * @param map the map
4327 <         * @param transformer a function returning the transformation
4328 <         * for an element, or null of there is no transformation (in
4329 <         * which case it is not combined).
4330 <         * @param reducer a commutative associative combining function
4566 <         * @return the task
4326 >         * @param e key to be added
4327 >         * @return {@code true} if this set changed as a result of the call
4328 >         * @throws NullPointerException if the specified key is null
4329 >         * @throws UnsupportedOperationException if no default mapped value
4330 >         * for additions was provided
4331           */
4332 <        public static <K,V,U> ForkJoinTask<U> reduceValues
4333 <            (ConcurrentHashMapV8<K,V> map,
4334 <             Fun<? super V, ? extends U> transformer,
4335 <             BiFun<? super U, ? super U, ? extends U> reducer) {
4336 <            if (transformer == null || reducer == null)
4573 <                throw new NullPointerException();
4574 <            return new MapReduceValuesTask<K,V,U>
4575 <                (map, transformer, reducer);
4332 >        public boolean add(K e) {
4333 >            V v;
4334 >            if ((v = value) == null)
4335 >                throw new UnsupportedOperationException();
4336 >            return map.putVal(e, v, true) == null;
4337          }
4338  
4339          /**
4340 <         * Returns a task that when invoked, returns the result of
4341 <         * accumulating the given transformation of all values using the
4581 <         * given reducer to combine values, and the given basis as an
4582 <         * identity value.
4340 >         * Adds all of the elements in the specified collection to this set,
4341 >         * as if by calling {@link #add} on each one.
4342           *
4343 <         * @param map the map
4344 <         * @param transformer a function returning the transformation
4345 <         * for an element
4346 <         * @param basis the identity (initial default value) for the reduction
4347 <         * @param reducer a commutative associative combining function
4348 <         * @return the task
4343 >         * @param c the elements to be inserted into this set
4344 >         * @return {@code true} if this set changed as a result of the call
4345 >         * @throws NullPointerException if the collection or any of its
4346 >         * elements are {@code null}
4347 >         * @throws UnsupportedOperationException if no default mapped value
4348 >         * for additions was provided
4349           */
4350 <        public static <K,V> ForkJoinTask<Double> reduceValuesToDouble
4351 <            (ConcurrentHashMapV8<K,V> map,
4352 <             ObjectToDouble<? super V> transformer,
4353 <             double basis,
4354 <             DoubleByDoubleToDouble reducer) {
4355 <            if (transformer == null || reducer == null)
4356 <                throw new NullPointerException();
4357 <            return new MapReduceValuesToDoubleTask<K,V>
4358 <                (map, transformer, basis, reducer);
4350 >        public boolean addAll(Collection<? extends K> c) {
4351 >            boolean added = false;
4352 >            V v;
4353 >            if ((v = value) == null)
4354 >                throw new UnsupportedOperationException();
4355 >            for (K e : c) {
4356 >                if (map.putVal(e, v, true) == null)
4357 >                    added = true;
4358 >            }
4359 >            return added;
4360          }
4361  
4362 <        /**
4363 <         * Returns a task that when invoked, returns the result of
4364 <         * accumulating the given transformation of all values using the
4365 <         * given reducer to combine values, and the given basis as an
4366 <         * identity value.
4607 <         *
4608 <         * @param map the map
4609 <         * @param transformer a function returning the transformation
4610 <         * for an element
4611 <         * @param basis the identity (initial default value) for the reduction
4612 <         * @param reducer a commutative associative combining function
4613 <         * @return the task
4614 <         */
4615 <        public static <K,V> ForkJoinTask<Long> reduceValuesToLong
4616 <            (ConcurrentHashMapV8<K,V> map,
4617 <             ObjectToLong<? super V> transformer,
4618 <             long basis,
4619 <             LongByLongToLong reducer) {
4620 <            if (transformer == null || reducer == null)
4621 <                throw new NullPointerException();
4622 <            return new MapReduceValuesToLongTask<K,V>
4623 <                (map, transformer, basis, reducer);
4362 >        public int hashCode() {
4363 >            int h = 0;
4364 >            for (K e : this)
4365 >                h += e.hashCode();
4366 >            return h;
4367          }
4368  
4369 <        /**
4370 <         * Returns a task that when invoked, returns the result of
4371 <         * accumulating the given transformation of all values using the
4372 <         * given reducer to combine values, and the given basis as an
4373 <         * identity value.
4631 <         *
4632 <         * @param map the map
4633 <         * @param transformer a function returning the transformation
4634 <         * for an element
4635 <         * @param basis the identity (initial default value) for the reduction
4636 <         * @param reducer a commutative associative combining function
4637 <         * @return the task
4638 <         */
4639 <        public static <K,V> ForkJoinTask<Integer> reduceValuesToInt
4640 <            (ConcurrentHashMapV8<K,V> map,
4641 <             ObjectToInt<? super V> transformer,
4642 <             int basis,
4643 <             IntByIntToInt reducer) {
4644 <            if (transformer == null || reducer == null)
4645 <                throw new NullPointerException();
4646 <            return new MapReduceValuesToIntTask<K,V>
4647 <                (map, transformer, basis, reducer);
4369 >        public boolean equals(Object o) {
4370 >            Set<?> c;
4371 >            return ((o instanceof Set) &&
4372 >                    ((c = (Set<?>)o) == this ||
4373 >                     (containsAll(c) && c.containsAll(this))));
4374          }
4375  
4376 <        /**
4377 <         * Returns a task that when invoked, perform the given action
4378 <         * for each entry.
4379 <         *
4380 <         * @param map the map
4381 <         * @param action the action
4656 <         */
4657 <        public static <K,V> ForkJoinTask<Void> forEachEntry
4658 <            (ConcurrentHashMapV8<K,V> map,
4659 <             Action<Map.Entry<K,V>> action) {
4660 <            if (action == null) throw new NullPointerException();
4661 <            return new ForEachEntryTask<K,V>(map, action);
4376 >        public ConcurrentHashMapSpliterator<K> spliterator() {
4377 >            Node<K,V>[] t;
4378 >            ConcurrentHashMapV8<K,V> m = map;
4379 >            long n = m.sumCount();
4380 >            int f = (t = m.table) == null ? 0 : t.length;
4381 >            return new KeySpliterator<K,V>(t, f, 0, f, n < 0L ? 0L : n);
4382          }
4383  
4384 <        /**
4385 <         * Returns a task that when invoked, perform the given action
4386 <         * for each non-null transformation of each entry.
4387 <         *
4388 <         * @param map the map
4389 <         * @param transformer a function returning the transformation
4390 <         * for an element, or null of there is no transformation (in
4391 <         * which case the action is not applied).
4672 <         * @param action the action
4673 <         */
4674 <        public static <K,V,U> ForkJoinTask<Void> forEachEntry
4675 <            (ConcurrentHashMapV8<K,V> map,
4676 <             Fun<Map.Entry<K,V>, ? extends U> transformer,
4677 <             Action<U> action) {
4678 <            if (transformer == null || action == null)
4679 <                throw new NullPointerException();
4680 <            return new ForEachTransformedEntryTask<K,V,U>
4681 <                (map, transformer, action);
4384 >        public void forEach(Action<? super K> action) {
4385 >            if (action == null) throw new NullPointerException();
4386 >            Node<K,V>[] t;
4387 >            if ((t = map.table) != null) {
4388 >                Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length);
4389 >                for (Node<K,V> p; (p = it.advance()) != null; )
4390 >                    action.apply(p.key);
4391 >            }
4392          }
4393 +    }
4394  
4395 <        /**
4396 <         * Returns a task that when invoked, returns a non-null result
4397 <         * from applying the given search function on each entry, or
4398 <         * null if none.  Upon success, further element processing is
4399 <         * suppressed and the results of any other parallel
4400 <         * invocations of the search function are ignored.
4401 <         *
4402 <         * @param map the map
4403 <         * @param searchFunction a function returning a non-null
4404 <         * result on success, else null
4405 <         * @return the task
4695 <         *
4696 <         */
4697 <        public static <K,V,U> ForkJoinTask<U> searchEntries
4698 <            (ConcurrentHashMapV8<K,V> map,
4699 <             Fun<Map.Entry<K,V>, ? extends U> searchFunction) {
4700 <            if (searchFunction == null) throw new NullPointerException();
4701 <            return new SearchEntriesTask<K,V,U>
4702 <                (map, searchFunction,
4703 <                 new AtomicReference<U>());
4395 >    /**
4396 >     * A view of a ConcurrentHashMapV8 as a {@link Collection} of
4397 >     * values, in which additions are disabled. This class cannot be
4398 >     * directly instantiated. See {@link #values()}.
4399 >     */
4400 >    static final class ValuesView<K,V> extends CollectionView<K,V,V>
4401 >        implements Collection<V>, java.io.Serializable {
4402 >        private static final long serialVersionUID = 2249069246763182397L;
4403 >        ValuesView(ConcurrentHashMapV8<K,V> map) { super(map); }
4404 >        public final boolean contains(Object o) {
4405 >            return map.containsValue(o);
4406          }
4407  
4408 <        /**
4409 <         * Returns a task that when invoked, returns the result of
4410 <         * accumulating all entries using the given reducer to combine
4411 <         * values, or null if none.
4412 <         *
4413 <         * @param map the map
4414 <         * @param reducer a commutative associative combining function
4415 <         * @return the task
4416 <         */
4417 <        public static <K,V> ForkJoinTask<Map.Entry<K,V>> reduceEntries
4716 <            (ConcurrentHashMapV8<K,V> map,
4717 <             BiFun<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer) {
4718 <            if (reducer == null) throw new NullPointerException();
4719 <            return new ReduceEntriesTask<K,V>
4720 <                (map, reducer);
4408 >        public final boolean remove(Object o) {
4409 >            if (o != null) {
4410 >                for (Iterator<V> it = iterator(); it.hasNext();) {
4411 >                    if (o.equals(it.next())) {
4412 >                        it.remove();
4413 >                        return true;
4414 >                    }
4415 >                }
4416 >            }
4417 >            return false;
4418          }
4419  
4420 <        /**
4421 <         * Returns a task that when invoked, returns the result of
4422 <         * accumulating the given transformation of all entries using the
4423 <         * given reducer to combine values, or null if none.
4424 <         *
4728 <         * @param map the map
4729 <         * @param transformer a function returning the transformation
4730 <         * for an element, or null of there is no transformation (in
4731 <         * which case it is not combined).
4732 <         * @param reducer a commutative associative combining function
4733 <         * @return the task
4734 <         */
4735 <        public static <K,V,U> ForkJoinTask<U> reduceEntries
4736 <            (ConcurrentHashMapV8<K,V> map,
4737 <             Fun<Map.Entry<K,V>, ? extends U> transformer,
4738 <             BiFun<? super U, ? super U, ? extends U> reducer) {
4739 <            if (transformer == null || reducer == null)
4740 <                throw new NullPointerException();
4741 <            return new MapReduceEntriesTask<K,V,U>
4742 <                (map, transformer, reducer);
4420 >        public final Iterator<V> iterator() {
4421 >            ConcurrentHashMapV8<K,V> m = map;
4422 >            Node<K,V>[] t;
4423 >            int f = (t = m.table) == null ? 0 : t.length;
4424 >            return new ValueIterator<K,V>(t, f, 0, f, m);
4425          }
4426  
4427 <        /**
4428 <         * Returns a task that when invoked, returns the result of
4429 <         * accumulating the given transformation of all entries using the
4430 <         * given reducer to combine values, and the given basis as an
4431 <         * identity value.
4750 <         *
4751 <         * @param map the map
4752 <         * @param transformer a function returning the transformation
4753 <         * for an element
4754 <         * @param basis the identity (initial default value) for the reduction
4755 <         * @param reducer a commutative associative combining function
4756 <         * @return the task
4757 <         */
4758 <        public static <K,V> ForkJoinTask<Double> reduceEntriesToDouble
4759 <            (ConcurrentHashMapV8<K,V> map,
4760 <             ObjectToDouble<Map.Entry<K,V>> transformer,
4761 <             double basis,
4762 <             DoubleByDoubleToDouble reducer) {
4763 <            if (transformer == null || reducer == null)
4764 <                throw new NullPointerException();
4765 <            return new MapReduceEntriesToDoubleTask<K,V>
4766 <                (map, transformer, basis, reducer);
4427 >        public final boolean add(V e) {
4428 >            throw new UnsupportedOperationException();
4429 >        }
4430 >        public final boolean addAll(Collection<? extends V> c) {
4431 >            throw new UnsupportedOperationException();
4432          }
4433  
4434 <        /**
4435 <         * Returns a task that when invoked, returns the result of
4436 <         * accumulating the given transformation of all entries using the
4437 <         * given reducer to combine values, and the given basis as an
4438 <         * identity value.
4439 <         *
4775 <         * @param map the map
4776 <         * @param transformer a function returning the transformation
4777 <         * for an element
4778 <         * @param basis the identity (initial default value) for the reduction
4779 <         * @param reducer a commutative associative combining function
4780 <         * @return the task
4781 <         */
4782 <        public static <K,V> ForkJoinTask<Long> reduceEntriesToLong
4783 <            (ConcurrentHashMapV8<K,V> map,
4784 <             ObjectToLong<Map.Entry<K,V>> transformer,
4785 <             long basis,
4786 <             LongByLongToLong reducer) {
4787 <            if (transformer == null || reducer == null)
4788 <                throw new NullPointerException();
4789 <            return new MapReduceEntriesToLongTask<K,V>
4790 <                (map, transformer, basis, reducer);
4434 >        public ConcurrentHashMapSpliterator<V> spliterator() {
4435 >            Node<K,V>[] t;
4436 >            ConcurrentHashMapV8<K,V> m = map;
4437 >            long n = m.sumCount();
4438 >            int f = (t = m.table) == null ? 0 : t.length;
4439 >            return new ValueSpliterator<K,V>(t, f, 0, f, n < 0L ? 0L : n);
4440          }
4441  
4442 <        /**
4443 <         * Returns a task that when invoked, returns the result of
4444 <         * accumulating the given transformation of all entries using the
4445 <         * given reducer to combine values, and the given basis as an
4446 <         * identity value.
4447 <         *
4448 <         * @param map the map
4449 <         * @param transformer a function returning the transformation
4801 <         * for an element
4802 <         * @param basis the identity (initial default value) for the reduction
4803 <         * @param reducer a commutative associative combining function
4804 <         * @return the task
4805 <         */
4806 <        public static <K,V> ForkJoinTask<Integer> reduceEntriesToInt
4807 <            (ConcurrentHashMapV8<K,V> map,
4808 <             ObjectToInt<Map.Entry<K,V>> transformer,
4809 <             int basis,
4810 <             IntByIntToInt reducer) {
4811 <            if (transformer == null || reducer == null)
4812 <                throw new NullPointerException();
4813 <            return new MapReduceEntriesToIntTask<K,V>
4814 <                (map, transformer, basis, reducer);
4442 >        public void forEach(Action<? super V> action) {
4443 >            if (action == null) throw new NullPointerException();
4444 >            Node<K,V>[] t;
4445 >            if ((t = map.table) != null) {
4446 >                Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length);
4447 >                for (Node<K,V> p; (p = it.advance()) != null; )
4448 >                    action.apply(p.val);
4449 >            }
4450          }
4451      }
4452  
4818    // -------------------------------------------------------
4819
4453      /**
4454 <     * Base for FJ tasks for bulk operations. This adds a variant of
4455 <     * CountedCompleters and some split and merge bookkeeping to
4456 <     * iterator functionality. The forEach and reduce methods are
4824 <     * similar to those illustrated in CountedCompleter documentation,
4825 <     * except that bottom-up reduction completions perform them within
4826 <     * their compute methods. The search methods are like forEach
4827 <     * except they continually poll for success and exit early.  Also,
4828 <     * exceptions are handled in a simpler manner, by just trying to
4829 <     * complete root task exceptionally.
4454 >     * A view of a ConcurrentHashMapV8 as a {@link Set} of (key, value)
4455 >     * entries.  This class cannot be directly instantiated. See
4456 >     * {@link #entrySet()}.
4457       */
4458 <    @SuppressWarnings("serial")
4459 <    static abstract class BulkTask<K,V,R> extends Traverser<K,V,R> {
4460 <        final BulkTask<K,V,?> parent;  // completion target
4461 <        int batch;                     // split control
4835 <        int pending;                   // completion control
4458 >    static final class EntrySetView<K,V> extends CollectionView<K,V,Map.Entry<K,V>>
4459 >        implements Set<Map.Entry<K,V>>, java.io.Serializable {
4460 >        private static final long serialVersionUID = 2249069246763182397L;
4461 >        EntrySetView(ConcurrentHashMapV8<K,V> map) { super(map); }
4462  
4463 <        /** Constructor for root tasks */
4464 <        BulkTask(ConcurrentHashMapV8<K,V> map) {
4465 <            super(map);
4466 <            this.parent = null;
4467 <            this.batch = -1; // force call to batch() on execution
4463 >        public boolean contains(Object o) {
4464 >            Object k, v, r; Map.Entry<?,?> e;
4465 >            return ((o instanceof Map.Entry) &&
4466 >                    (k = (e = (Map.Entry<?,?>)o).getKey()) != null &&
4467 >                    (r = map.get(k)) != null &&
4468 >                    (v = e.getValue()) != null &&
4469 >                    (v == r || v.equals(r)));
4470          }
4471  
4472 <        /** Constructor for subtasks */
4473 <        BulkTask(BulkTask<K,V,?> parent, int batch, boolean split) {
4474 <            super(parent, split);
4475 <            this.parent = parent;
4476 <            this.batch = batch;
4472 >        public boolean remove(Object o) {
4473 >            Object k, v; Map.Entry<?,?> e;
4474 >            return ((o instanceof Map.Entry) &&
4475 >                    (k = (e = (Map.Entry<?,?>)o).getKey()) != null &&
4476 >                    (v = e.getValue()) != null &&
4477 >                    map.remove(k, v));
4478          }
4479  
4851        // FJ methods
4852
4480          /**
4481 <         * Propagates completion. Note that all reduce actions
4855 <         * bypass this method to combine while completing.
4481 >         * @return an iterator over the entries of the backing map
4482           */
4483 <        final void tryComplete() {
4484 <            BulkTask<K,V,?> a = this, s = a;
4485 <            for (int c;;) {
4486 <                if ((c = a.pending) == 0) {
4487 <                    if ((a = (s = a).parent) == null) {
4862 <                        s.quietlyComplete();
4863 <                        break;
4864 <                    }
4865 <                }
4866 <                else if (U.compareAndSwapInt(a, PENDING, c, c - 1))
4867 <                    break;
4868 <            }
4483 >        public Iterator<Map.Entry<K,V>> iterator() {
4484 >            ConcurrentHashMapV8<K,V> m = map;
4485 >            Node<K,V>[] t;
4486 >            int f = (t = m.table) == null ? 0 : t.length;
4487 >            return new EntryIterator<K,V>(t, f, 0, f, m);
4488          }
4489  
4490 <        /**
4491 <         * Forces root task to throw exception unless already complete.
4873 <         */
4874 <        final void tryAbortComputation(Throwable ex) {
4875 <            for (BulkTask<K,V,?> a = this;;) {
4876 <                BulkTask<K,V,?> p = a.parent;
4877 <                if (p == null) {
4878 <                    a.completeExceptionally(ex);
4879 <                    break;
4880 <                }
4881 <                a = p;
4882 <            }
4490 >        public boolean add(Entry<K,V> e) {
4491 >            return map.putVal(e.getKey(), e.getValue(), false) == null;
4492          }
4493  
4494 <        public final boolean exec() {
4495 <            try {
4496 <                compute();
4494 >        public boolean addAll(Collection<? extends Entry<K,V>> c) {
4495 >            boolean added = false;
4496 >            for (Entry<K,V> e : c) {
4497 >                if (add(e))
4498 >                    added = true;
4499              }
4500 <            catch (Throwable ex) {
4890 <                tryAbortComputation(ex);
4891 <            }
4892 <            return false;
4500 >            return added;
4501          }
4502  
4503 <        public abstract void compute();
4503 >        public final int hashCode() {
4504 >            int h = 0;
4505 >            Node<K,V>[] t;
4506 >            if ((t = map.table) != null) {
4507 >                Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length);
4508 >                for (Node<K,V> p; (p = it.advance()) != null; ) {
4509 >                    h += p.hashCode();
4510 >                }
4511 >            }
4512 >            return h;
4513 >        }
4514  
4515 <        // utilities
4515 >        public final boolean equals(Object o) {
4516 >            Set<?> c;
4517 >            return ((o instanceof Set) &&
4518 >                    ((c = (Set<?>)o) == this ||
4519 >                     (containsAll(c) && c.containsAll(this))));
4520 >        }
4521  
4522 <        /** CompareAndSet pending count */
4523 <        final boolean casPending(int cmp, int val) {
4524 <            return U.compareAndSwapInt(this, PENDING, cmp, val);
4522 >        public ConcurrentHashMapSpliterator<Map.Entry<K,V>> spliterator() {
4523 >            Node<K,V>[] t;
4524 >            ConcurrentHashMapV8<K,V> m = map;
4525 >            long n = m.sumCount();
4526 >            int f = (t = m.table) == null ? 0 : t.length;
4527 >            return new EntrySpliterator<K,V>(t, f, 0, f, n < 0L ? 0L : n, m);
4528          }
4529  
4530 <        /**
4531 <         * Returns approx exp2 of the number of times (minus one) to
4532 <         * split task by two before executing leaf action. This value
4533 <         * is faster to compute and more convenient to use as a guide
4534 <         * to splitting than is the depth, since it is used while
4535 <         * dividing by two anyway.
4536 <         */
4911 <        final int batch() {
4912 <            int b = batch;
4913 <            if (b < 0) {
4914 <                long n = map.counter.sum();
4915 <                int sp = getPool().getParallelism() << 3; // slack of 8
4916 <                b = batch = (n <= 0L) ? 0 : (n < (long)sp) ? (int)n : sp;
4530 >        public void forEach(Action<? super Map.Entry<K,V>> action) {
4531 >            if (action == null) throw new NullPointerException();
4532 >            Node<K,V>[] t;
4533 >            if ((t = map.table) != null) {
4534 >                Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length);
4535 >                for (Node<K,V> p; (p = it.advance()) != null; )
4536 >                    action.apply(new MapEntry<K,V>(p.key, p.val, map));
4537              }
4918            return b;
4538          }
4539  
4540 <        /**
4922 <         * Error message for hoisted null checks of functions
4923 <         */
4924 <        static final String NullFunctionMessage =
4925 <            "Unexpected null function";
4540 >    }
4541  
4542 <        /**
4543 <         * Returns exportable snapshot entry.
4544 <         */
4545 <        static <K,V> AbstractMap.SimpleEntry<K,V> entryFor(K k, V v) {
4546 <            return new AbstractMap.SimpleEntry<K,V>(k, v);
4542 >    // -------------------------------------------------------
4543 >
4544 >    /**
4545 >     * Base class for bulk tasks. Repeats some fields and code from
4546 >     * class Traverser, because we need to subclass CountedCompleter.
4547 >     */
4548 >    abstract static class BulkTask<K,V,R> extends CountedCompleter<R> {
4549 >        Node<K,V>[] tab;        // same as Traverser
4550 >        Node<K,V> next;
4551 >        int index;
4552 >        int baseIndex;
4553 >        int baseLimit;
4554 >        final int baseSize;
4555 >        int batch;              // split control
4556 >
4557 >        BulkTask(BulkTask<K,V,?> par, int b, int i, int f, Node<K,V>[] t) {
4558 >            super(par);
4559 >            this.batch = b;
4560 >            this.index = this.baseIndex = i;
4561 >            if ((this.tab = t) == null)
4562 >                this.baseSize = this.baseLimit = 0;
4563 >            else if (par == null)
4564 >                this.baseSize = this.baseLimit = t.length;
4565 >            else {
4566 >                this.baseLimit = f;
4567 >                this.baseSize = par.baseSize;
4568 >            }
4569          }
4570  
4571 <        // Unsafe mechanics
4572 <        private static final sun.misc.Unsafe U;
4573 <        private static final long PENDING;
4574 <        static {
4575 <            try {
4576 <                U = getUnsafe();
4577 <                PENDING = U.objectFieldOffset
4578 <                    (BulkTask.class.getDeclaredField("pending"));
4579 <            } catch (Exception e) {
4580 <                throw new Error(e);
4571 >        /**
4572 >         * Same as Traverser version
4573 >         */
4574 >        final Node<K,V> advance() {
4575 >            Node<K,V> e;
4576 >            if ((e = next) != null)
4577 >                e = e.next;
4578 >            for (;;) {
4579 >                Node<K,V>[] t; int i, n; K ek;  // must use locals in checks
4580 >                if (e != null)
4581 >                    return next = e;
4582 >                if (baseIndex >= baseLimit || (t = tab) == null ||
4583 >                    (n = t.length) <= (i = index) || i < 0)
4584 >                    return next = null;
4585 >                if ((e = tabAt(t, index)) != null && e.hash < 0) {
4586 >                    if (e instanceof ForwardingNode) {
4587 >                        tab = ((ForwardingNode<K,V>)e).nextTable;
4588 >                        e = null;
4589 >                        continue;
4590 >                    }
4591 >                    else if (e instanceof TreeBin)
4592 >                        e = ((TreeBin<K,V>)e).first;
4593 >                    else
4594 >                        e = null;
4595 >                }
4596 >                if ((index += baseSize) >= n)
4597 >                    index = ++baseIndex;    // visit upper slots if present
4598              }
4599          }
4600      }
# Line 4948 | Line 4602 | public class ConcurrentHashMapV8<K, V>
4602      /*
4603       * Task classes. Coded in a regular but ugly format/style to
4604       * simplify checks that each variant differs in the right way from
4605 <     * others.
4605 >     * others. The null screenings exist because compilers cannot tell
4606 >     * that we've already null-checked task arguments, so we force
4607 >     * simplest hoisted bypass to help avoid convoluted traps.
4608       */
4953
4609      @SuppressWarnings("serial")
4610      static final class ForEachKeyTask<K,V>
4611          extends BulkTask<K,V,Void> {
4612 <        final Action<K> action;
4612 >        final Action<? super K> action;
4613          ForEachKeyTask
4614 <            (ConcurrentHashMapV8<K,V> m,
4615 <             Action<K> action) {
4616 <            super(m);
4614 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
4615 >             Action<? super K> action) {
4616 >            super(p, b, i, f, t);
4617              this.action = action;
4618          }
4619 <        ForEachKeyTask
4620 <            (BulkTask<K,V,?> p, int b, boolean split,
4621 <             Action<K> action) {
4622 <            super(p, b, split);
4623 <            this.action = action;
4624 <        }
4625 <        @SuppressWarnings("unchecked") public final void compute() {
4626 <            final Action<K> action = this.action;
4627 <            if (action == null)
4628 <                throw new Error(NullFunctionMessage);
4629 <            int b = batch(), c;
4630 <            while (b > 1 && baseIndex != baseLimit) {
4631 <                do {} while (!casPending(c = pending, c+1));
4632 <                new ForEachKeyTask<K,V>(this, b >>>= 1, true, action).fork();
4978 <            }
4979 <            while (advance() != null)
4980 <                action.apply((K)nextKey);
4981 <            tryComplete();
4619 >        public final void compute() {
4620 >            final Action<? super K> action;
4621 >            if ((action = this.action) != null) {
4622 >                for (int i = baseIndex, f, h; batch > 0 &&
4623 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
4624 >                    addToPendingCount(1);
4625 >                    new ForEachKeyTask<K,V>
4626 >                        (this, batch >>>= 1, baseLimit = h, f, tab,
4627 >                         action).fork();
4628 >                }
4629 >                for (Node<K,V> p; (p = advance()) != null;)
4630 >                    action.apply(p.key);
4631 >                propagateCompletion();
4632 >            }
4633          }
4634      }
4635  
4636      @SuppressWarnings("serial")
4637      static final class ForEachValueTask<K,V>
4638          extends BulkTask<K,V,Void> {
4639 <        final Action<V> action;
4989 <        ForEachValueTask
4990 <            (ConcurrentHashMapV8<K,V> m,
4991 <             Action<V> action) {
4992 <            super(m);
4993 <            this.action = action;
4994 <        }
4639 >        final Action<? super V> action;
4640          ForEachValueTask
4641 <            (BulkTask<K,V,?> p, int b, boolean split,
4642 <             Action<V> action) {
4643 <            super(p, b, split);
4641 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
4642 >             Action<? super V> action) {
4643 >            super(p, b, i, f, t);
4644              this.action = action;
4645          }
4646 <        @SuppressWarnings("unchecked") public final void compute() {
4647 <            final Action<V> action = this.action;
4648 <            if (action == null)
4649 <                throw new Error(NullFunctionMessage);
4650 <            int b = batch(), c;
4651 <            while (b > 1 && baseIndex != baseLimit) {
4652 <                do {} while (!casPending(c = pending, c+1));
4653 <                new ForEachValueTask<K,V>(this, b >>>= 1, true, action).fork();
4654 <            }
4655 <            Object v;
4656 <            while ((v = advance()) != null)
4657 <                action.apply((V)v);
4658 <            tryComplete();
4646 >        public final void compute() {
4647 >            final Action<? super V> action;
4648 >            if ((action = this.action) != null) {
4649 >                for (int i = baseIndex, f, h; batch > 0 &&
4650 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
4651 >                    addToPendingCount(1);
4652 >                    new ForEachValueTask<K,V>
4653 >                        (this, batch >>>= 1, baseLimit = h, f, tab,
4654 >                         action).fork();
4655 >                }
4656 >                for (Node<K,V> p; (p = advance()) != null;)
4657 >                    action.apply(p.val);
4658 >                propagateCompletion();
4659 >            }
4660          }
4661      }
4662  
4663      @SuppressWarnings("serial")
4664      static final class ForEachEntryTask<K,V>
4665          extends BulkTask<K,V,Void> {
4666 <        final Action<Entry<K,V>> action;
5021 <        ForEachEntryTask
5022 <            (ConcurrentHashMapV8<K,V> m,
5023 <             Action<Entry<K,V>> action) {
5024 <            super(m);
5025 <            this.action = action;
5026 <        }
4666 >        final Action<? super Entry<K,V>> action;
4667          ForEachEntryTask
4668 <            (BulkTask<K,V,?> p, int b, boolean split,
4669 <             Action<Entry<K,V>> action) {
4670 <            super(p, b, split);
4668 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
4669 >             Action<? super Entry<K,V>> action) {
4670 >            super(p, b, i, f, t);
4671              this.action = action;
4672          }
4673 <        @SuppressWarnings("unchecked") public final void compute() {
4674 <            final Action<Entry<K,V>> action = this.action;
4675 <            if (action == null)
4676 <                throw new Error(NullFunctionMessage);
4677 <            int b = batch(), c;
4678 <            while (b > 1 && baseIndex != baseLimit) {
4679 <                do {} while (!casPending(c = pending, c+1));
4680 <                new ForEachEntryTask<K,V>(this, b >>>= 1, true, action).fork();
4681 <            }
4682 <            Object v;
4683 <            while ((v = advance()) != null)
4684 <                action.apply(entryFor((K)nextKey, (V)v));
4685 <            tryComplete();
4673 >        public final void compute() {
4674 >            final Action<? super Entry<K,V>> action;
4675 >            if ((action = this.action) != null) {
4676 >                for (int i = baseIndex, f, h; batch > 0 &&
4677 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
4678 >                    addToPendingCount(1);
4679 >                    new ForEachEntryTask<K,V>
4680 >                        (this, batch >>>= 1, baseLimit = h, f, tab,
4681 >                         action).fork();
4682 >                }
4683 >                for (Node<K,V> p; (p = advance()) != null; )
4684 >                    action.apply(p);
4685 >                propagateCompletion();
4686 >            }
4687          }
4688      }
4689  
4690      @SuppressWarnings("serial")
4691      static final class ForEachMappingTask<K,V>
4692          extends BulkTask<K,V,Void> {
4693 <        final BiAction<K,V> action;
4693 >        final BiAction<? super K, ? super V> action;
4694          ForEachMappingTask
4695 <            (ConcurrentHashMapV8<K,V> m,
4696 <             BiAction<K,V> action) {
4697 <            super(m);
4695 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
4696 >             BiAction<? super K,? super V> action) {
4697 >            super(p, b, i, f, t);
4698              this.action = action;
4699          }
4700 <        ForEachMappingTask
4701 <            (BulkTask<K,V,?> p, int b, boolean split,
4702 <             BiAction<K,V> action) {
4703 <            super(p, b, split);
4704 <            this.action = action;
4705 <        }
4706 <
4707 <        @SuppressWarnings("unchecked") public final void compute() {
4708 <            final BiAction<K,V> action = this.action;
4709 <            if (action == null)
4710 <                throw new Error(NullFunctionMessage);
4711 <            int b = batch(), c;
4712 <            while (b > 1 && baseIndex != baseLimit) {
4713 <                do {} while (!casPending(c = pending, c+1));
5073 <                new ForEachMappingTask<K,V>(this, b >>>= 1, true,
5074 <                                            action).fork();
5075 <            }
5076 <            Object v;
5077 <            while ((v = advance()) != null)
5078 <                action.apply((K)nextKey, (V)v);
5079 <            tryComplete();
4700 >        public final void compute() {
4701 >            final BiAction<? super K, ? super V> action;
4702 >            if ((action = this.action) != null) {
4703 >                for (int i = baseIndex, f, h; batch > 0 &&
4704 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
4705 >                    addToPendingCount(1);
4706 >                    new ForEachMappingTask<K,V>
4707 >                        (this, batch >>>= 1, baseLimit = h, f, tab,
4708 >                         action).fork();
4709 >                }
4710 >                for (Node<K,V> p; (p = advance()) != null; )
4711 >                    action.apply(p.key, p.val);
4712 >                propagateCompletion();
4713 >            }
4714          }
4715      }
4716  
# Line 5084 | Line 4718 | public class ConcurrentHashMapV8<K, V>
4718      static final class ForEachTransformedKeyTask<K,V,U>
4719          extends BulkTask<K,V,Void> {
4720          final Fun<? super K, ? extends U> transformer;
4721 <        final Action<U> action;
5088 <        ForEachTransformedKeyTask
5089 <            (ConcurrentHashMapV8<K,V> m,
5090 <             Fun<? super K, ? extends U> transformer,
5091 <             Action<U> action) {
5092 <            super(m);
5093 <            this.transformer = transformer;
5094 <            this.action = action;
5095 <
5096 <        }
4721 >        final Action<? super U> action;
4722          ForEachTransformedKeyTask
4723 <            (BulkTask<K,V,?> p, int b, boolean split,
4724 <             Fun<? super K, ? extends U> transformer,
4725 <             Action<U> action) {
4726 <            super(p, b, split);
4727 <            this.transformer = transformer;
4728 <            this.action = action;
4729 <        }
4730 <        @SuppressWarnings("unchecked") public final void compute() {
4731 <            final Fun<? super K, ? extends U> transformer =
4732 <                this.transformer;
4733 <            final Action<U> action = this.action;
4734 <            if (transformer == null || action == null)
4735 <                throw new Error(NullFunctionMessage);
4736 <            int b = batch(), c;
4737 <            while (b > 1 && baseIndex != baseLimit) {
4738 <                do {} while (!casPending(c = pending, c+1));
4739 <                new ForEachTransformedKeyTask<K,V,U>
4740 <                    (this, b >>>= 1, true, transformer, action).fork();
4741 <            }
4742 <            U u;
4743 <            while (advance() != null) {
4744 <                if ((u = transformer.apply((K)nextKey)) != null)
4745 <                    action.apply(u);
4723 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
4724 >             Fun<? super K, ? extends U> transformer, Action<? super U> action) {
4725 >            super(p, b, i, f, t);
4726 >            this.transformer = transformer; this.action = action;
4727 >        }
4728 >        public final void compute() {
4729 >            final Fun<? super K, ? extends U> transformer;
4730 >            final Action<? super U> action;
4731 >            if ((transformer = this.transformer) != null &&
4732 >                (action = this.action) != null) {
4733 >                for (int i = baseIndex, f, h; batch > 0 &&
4734 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
4735 >                    addToPendingCount(1);
4736 >                    new ForEachTransformedKeyTask<K,V,U>
4737 >                        (this, batch >>>= 1, baseLimit = h, f, tab,
4738 >                         transformer, action).fork();
4739 >                }
4740 >                for (Node<K,V> p; (p = advance()) != null; ) {
4741 >                    U u;
4742 >                    if ((u = transformer.apply(p.key)) != null)
4743 >                        action.apply(u);
4744 >                }
4745 >                propagateCompletion();
4746              }
5122            tryComplete();
4747          }
4748      }
4749  
# Line 5127 | Line 4751 | public class ConcurrentHashMapV8<K, V>
4751      static final class ForEachTransformedValueTask<K,V,U>
4752          extends BulkTask<K,V,Void> {
4753          final Fun<? super V, ? extends U> transformer;
4754 <        final Action<U> action;
4754 >        final Action<? super U> action;
4755          ForEachTransformedValueTask
4756 <            (ConcurrentHashMapV8<K,V> m,
4757 <             Fun<? super V, ? extends U> transformer,
4758 <             Action<U> action) {
4759 <            super(m);
4760 <            this.transformer = transformer;
4761 <            this.action = action;
4762 <
4763 <        }
4764 <        ForEachTransformedValueTask
4765 <            (BulkTask<K,V,?> p, int b, boolean split,
4766 <             Fun<? super V, ? extends U> transformer,
4767 <             Action<U> action) {
4768 <            super(p, b, split);
4769 <            this.transformer = transformer;
4770 <            this.action = action;
4771 <        }
4772 <        @SuppressWarnings("unchecked") public final void compute() {
4773 <            final Fun<? super V, ? extends U> transformer =
4774 <                this.transformer;
4775 <            final Action<U> action = this.action;
4776 <            if (transformer == null || action == null)
4777 <                throw new Error(NullFunctionMessage);
4778 <            int b = batch(), c;
5155 <            while (b > 1 && baseIndex != baseLimit) {
5156 <                do {} while (!casPending(c = pending, c+1));
5157 <                new ForEachTransformedValueTask<K,V,U>
5158 <                    (this, b >>>= 1, true, transformer, action).fork();
5159 <            }
5160 <            Object v; U u;
5161 <            while ((v = advance()) != null) {
5162 <                if ((u = transformer.apply((V)v)) != null)
5163 <                    action.apply(u);
4756 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
4757 >             Fun<? super V, ? extends U> transformer, Action<? super U> action) {
4758 >            super(p, b, i, f, t);
4759 >            this.transformer = transformer; this.action = action;
4760 >        }
4761 >        public final void compute() {
4762 >            final Fun<? super V, ? extends U> transformer;
4763 >            final Action<? super U> action;
4764 >            if ((transformer = this.transformer) != null &&
4765 >                (action = this.action) != null) {
4766 >                for (int i = baseIndex, f, h; batch > 0 &&
4767 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
4768 >                    addToPendingCount(1);
4769 >                    new ForEachTransformedValueTask<K,V,U>
4770 >                        (this, batch >>>= 1, baseLimit = h, f, tab,
4771 >                         transformer, action).fork();
4772 >                }
4773 >                for (Node<K,V> p; (p = advance()) != null; ) {
4774 >                    U u;
4775 >                    if ((u = transformer.apply(p.val)) != null)
4776 >                        action.apply(u);
4777 >                }
4778 >                propagateCompletion();
4779              }
5165            tryComplete();
4780          }
4781      }
4782  
# Line 5170 | Line 4784 | public class ConcurrentHashMapV8<K, V>
4784      static final class ForEachTransformedEntryTask<K,V,U>
4785          extends BulkTask<K,V,Void> {
4786          final Fun<Map.Entry<K,V>, ? extends U> transformer;
4787 <        final Action<U> action;
4787 >        final Action<? super U> action;
4788          ForEachTransformedEntryTask
4789 <            (ConcurrentHashMapV8<K,V> m,
4790 <             Fun<Map.Entry<K,V>, ? extends U> transformer,
4791 <             Action<U> action) {
4792 <            super(m);
4793 <            this.transformer = transformer;
4794 <            this.action = action;
4795 <
4796 <        }
4797 <        ForEachTransformedEntryTask
4798 <            (BulkTask<K,V,?> p, int b, boolean split,
4799 <             Fun<Map.Entry<K,V>, ? extends U> transformer,
4800 <             Action<U> action) {
4801 <            super(p, b, split);
4802 <            this.transformer = transformer;
4803 <            this.action = action;
4804 <        }
4805 <        @SuppressWarnings("unchecked") public final void compute() {
4806 <            final Fun<Map.Entry<K,V>, ? extends U> transformer =
4807 <                this.transformer;
4808 <            final Action<U> action = this.action;
4809 <            if (transformer == null || action == null)
4810 <                throw new Error(NullFunctionMessage);
4811 <            int b = batch(), c;
5198 <            while (b > 1 && baseIndex != baseLimit) {
5199 <                do {} while (!casPending(c = pending, c+1));
5200 <                new ForEachTransformedEntryTask<K,V,U>
5201 <                    (this, b >>>= 1, true, transformer, action).fork();
5202 <            }
5203 <            Object v; U u;
5204 <            while ((v = advance()) != null) {
5205 <                if ((u = transformer.apply(entryFor((K)nextKey, (V)v))) != null)
5206 <                    action.apply(u);
4789 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
4790 >             Fun<Map.Entry<K,V>, ? extends U> transformer, Action<? super U> action) {
4791 >            super(p, b, i, f, t);
4792 >            this.transformer = transformer; this.action = action;
4793 >        }
4794 >        public final void compute() {
4795 >            final Fun<Map.Entry<K,V>, ? extends U> transformer;
4796 >            final Action<? super U> action;
4797 >            if ((transformer = this.transformer) != null &&
4798 >                (action = this.action) != null) {
4799 >                for (int i = baseIndex, f, h; batch > 0 &&
4800 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
4801 >                    addToPendingCount(1);
4802 >                    new ForEachTransformedEntryTask<K,V,U>
4803 >                        (this, batch >>>= 1, baseLimit = h, f, tab,
4804 >                         transformer, action).fork();
4805 >                }
4806 >                for (Node<K,V> p; (p = advance()) != null; ) {
4807 >                    U u;
4808 >                    if ((u = transformer.apply(p)) != null)
4809 >                        action.apply(u);
4810 >                }
4811 >                propagateCompletion();
4812              }
5208            tryComplete();
4813          }
4814      }
4815  
# Line 5213 | Line 4817 | public class ConcurrentHashMapV8<K, V>
4817      static final class ForEachTransformedMappingTask<K,V,U>
4818          extends BulkTask<K,V,Void> {
4819          final BiFun<? super K, ? super V, ? extends U> transformer;
4820 <        final Action<U> action;
4820 >        final Action<? super U> action;
4821          ForEachTransformedMappingTask
4822 <            (ConcurrentHashMapV8<K,V> m,
4822 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
4823               BiFun<? super K, ? super V, ? extends U> transformer,
4824 <             Action<U> action) {
4825 <            super(m);
4826 <            this.transformer = transformer;
4827 <            this.action = action;
4828 <
4829 <        }
4830 <        ForEachTransformedMappingTask
4831 <            (BulkTask<K,V,?> p, int b, boolean split,
4832 <             BiFun<? super K, ? super V, ? extends U> transformer,
4833 <             Action<U> action) {
4834 <            super(p, b, split);
4835 <            this.transformer = transformer;
4836 <            this.action = action;
4837 <        }
4838 <        @SuppressWarnings("unchecked") public final void compute() {
4839 <            final BiFun<? super K, ? super V, ? extends U> transformer =
4840 <                this.transformer;
4841 <            final Action<U> action = this.action;
4842 <            if (transformer == null || action == null)
4843 <                throw new Error(NullFunctionMessage);
4844 <            int b = batch(), c;
4845 <            while (b > 1 && baseIndex != baseLimit) {
5242 <                do {} while (!casPending(c = pending, c+1));
5243 <                new ForEachTransformedMappingTask<K,V,U>
5244 <                    (this, b >>>= 1, true, transformer, action).fork();
5245 <            }
5246 <            Object v; U u;
5247 <            while ((v = advance()) != null) {
5248 <                if ((u = transformer.apply((K)nextKey, (V)v)) != null)
5249 <                    action.apply(u);
4824 >             Action<? super U> action) {
4825 >            super(p, b, i, f, t);
4826 >            this.transformer = transformer; this.action = action;
4827 >        }
4828 >        public final void compute() {
4829 >            final BiFun<? super K, ? super V, ? extends U> transformer;
4830 >            final Action<? super U> action;
4831 >            if ((transformer = this.transformer) != null &&
4832 >                (action = this.action) != null) {
4833 >                for (int i = baseIndex, f, h; batch > 0 &&
4834 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
4835 >                    addToPendingCount(1);
4836 >                    new ForEachTransformedMappingTask<K,V,U>
4837 >                        (this, batch >>>= 1, baseLimit = h, f, tab,
4838 >                         transformer, action).fork();
4839 >                }
4840 >                for (Node<K,V> p; (p = advance()) != null; ) {
4841 >                    U u;
4842 >                    if ((u = transformer.apply(p.key, p.val)) != null)
4843 >                        action.apply(u);
4844 >                }
4845 >                propagateCompletion();
4846              }
5251            tryComplete();
4847          }
4848      }
4849  
# Line 5258 | Line 4853 | public class ConcurrentHashMapV8<K, V>
4853          final Fun<? super K, ? extends U> searchFunction;
4854          final AtomicReference<U> result;
4855          SearchKeysTask
4856 <            (ConcurrentHashMapV8<K,V> m,
5262 <             Fun<? super K, ? extends U> searchFunction,
5263 <             AtomicReference<U> result) {
5264 <            super(m);
5265 <            this.searchFunction = searchFunction; this.result = result;
5266 <        }
5267 <        SearchKeysTask
5268 <            (BulkTask<K,V,?> p, int b, boolean split,
4856 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
4857               Fun<? super K, ? extends U> searchFunction,
4858               AtomicReference<U> result) {
4859 <            super(p, b, split);
4859 >            super(p, b, i, f, t);
4860              this.searchFunction = searchFunction; this.result = result;
4861          }
4862 <        @SuppressWarnings("unchecked") public final void compute() {
4863 <            AtomicReference<U> result = this.result;
4864 <            final Fun<? super K, ? extends U> searchFunction =
4865 <                this.searchFunction;
4866 <            if (searchFunction == null || result == null)
4867 <                throw new Error(NullFunctionMessage);
4868 <            int b = batch(), c;
4869 <            while (b > 1 && baseIndex != baseLimit && result.get() == null) {
4870 <                do {} while (!casPending(c = pending, c+1));
4871 <                new SearchKeysTask<K,V,U>(this, b >>>= 1, true,
4872 <                                          searchFunction, result).fork();
4873 <            }
4874 <            U u;
4875 <            while (result.get() == null && advance() != null) {
4876 <                if ((u = searchFunction.apply((K)nextKey)) != null) {
4877 <                    if (result.compareAndSet(null, u)) {
4878 <                        for (BulkTask<K,V,?> a = this, p;;) {
4879 <                            if ((p = a.parent) == null) {
4880 <                                a.quietlyComplete();
4881 <                                break;
4882 <                            }
4883 <                            a = p;
4884 <                        }
4862 >        public final U getRawResult() { return result.get(); }
4863 >        public final void compute() {
4864 >            final Fun<? super K, ? extends U> searchFunction;
4865 >            final AtomicReference<U> result;
4866 >            if ((searchFunction = this.searchFunction) != null &&
4867 >                (result = this.result) != null) {
4868 >                for (int i = baseIndex, f, h; batch > 0 &&
4869 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
4870 >                    if (result.get() != null)
4871 >                        return;
4872 >                    addToPendingCount(1);
4873 >                    new SearchKeysTask<K,V,U>
4874 >                        (this, batch >>>= 1, baseLimit = h, f, tab,
4875 >                         searchFunction, result).fork();
4876 >                }
4877 >                while (result.get() == null) {
4878 >                    U u;
4879 >                    Node<K,V> p;
4880 >                    if ((p = advance()) == null) {
4881 >                        propagateCompletion();
4882 >                        break;
4883 >                    }
4884 >                    if ((u = searchFunction.apply(p.key)) != null) {
4885 >                        if (result.compareAndSet(null, u))
4886 >                            quietlyCompleteRoot();
4887 >                        break;
4888                      }
5298                    break;
4889                  }
4890              }
5301            tryComplete();
4891          }
5303        public final U getRawResult() { return result.get(); }
4892      }
4893  
4894      @SuppressWarnings("serial")
# Line 5309 | Line 4897 | public class ConcurrentHashMapV8<K, V>
4897          final Fun<? super V, ? extends U> searchFunction;
4898          final AtomicReference<U> result;
4899          SearchValuesTask
4900 <            (ConcurrentHashMapV8<K,V> m,
4900 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
4901               Fun<? super V, ? extends U> searchFunction,
4902               AtomicReference<U> result) {
4903 <            super(m);
4903 >            super(p, b, i, f, t);
4904              this.searchFunction = searchFunction; this.result = result;
4905          }
4906 <        SearchValuesTask
4907 <            (BulkTask<K,V,?> p, int b, boolean split,
4908 <             Fun<? super V, ? extends U> searchFunction,
4909 <             AtomicReference<U> result) {
4910 <            super(p, b, split);
4911 <            this.searchFunction = searchFunction; this.result = result;
4912 <        }
4913 <        @SuppressWarnings("unchecked") public final void compute() {
4914 <            AtomicReference<U> result = this.result;
4915 <            final Fun<? super V, ? extends U> searchFunction =
4916 <                this.searchFunction;
4917 <            if (searchFunction == null || result == null)
4918 <                throw new Error(NullFunctionMessage);
4919 <            int b = batch(), c;
4920 <            while (b > 1 && baseIndex != baseLimit && result.get() == null) {
4921 <                do {} while (!casPending(c = pending, c+1));
4922 <                new SearchValuesTask<K,V,U>(this, b >>>= 1, true,
4923 <                                            searchFunction, result).fork();
4924 <            }
4925 <            Object v; U u;
4926 <            while (result.get() == null && (v = advance()) != null) {
4927 <                if ((u = searchFunction.apply((V)v)) != null) {
4928 <                    if (result.compareAndSet(null, u)) {
4929 <                        for (BulkTask<K,V,?> a = this, p;;) {
4930 <                            if ((p = a.parent) == null) {
4931 <                                a.quietlyComplete();
5344 <                                break;
5345 <                            }
5346 <                            a = p;
5347 <                        }
4906 >        public final U getRawResult() { return result.get(); }
4907 >        public final void compute() {
4908 >            final Fun<? super V, ? extends U> searchFunction;
4909 >            final AtomicReference<U> result;
4910 >            if ((searchFunction = this.searchFunction) != null &&
4911 >                (result = this.result) != null) {
4912 >                for (int i = baseIndex, f, h; batch > 0 &&
4913 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
4914 >                    if (result.get() != null)
4915 >                        return;
4916 >                    addToPendingCount(1);
4917 >                    new SearchValuesTask<K,V,U>
4918 >                        (this, batch >>>= 1, baseLimit = h, f, tab,
4919 >                         searchFunction, result).fork();
4920 >                }
4921 >                while (result.get() == null) {
4922 >                    U u;
4923 >                    Node<K,V> p;
4924 >                    if ((p = advance()) == null) {
4925 >                        propagateCompletion();
4926 >                        break;
4927 >                    }
4928 >                    if ((u = searchFunction.apply(p.val)) != null) {
4929 >                        if (result.compareAndSet(null, u))
4930 >                            quietlyCompleteRoot();
4931 >                        break;
4932                      }
5349                    break;
4933                  }
4934              }
5352            tryComplete();
4935          }
5354        public final U getRawResult() { return result.get(); }
4936      }
4937  
4938      @SuppressWarnings("serial")
# Line 5360 | Line 4941 | public class ConcurrentHashMapV8<K, V>
4941          final Fun<Entry<K,V>, ? extends U> searchFunction;
4942          final AtomicReference<U> result;
4943          SearchEntriesTask
4944 <            (ConcurrentHashMapV8<K,V> m,
4944 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
4945               Fun<Entry<K,V>, ? extends U> searchFunction,
4946               AtomicReference<U> result) {
4947 <            super(m);
4947 >            super(p, b, i, f, t);
4948              this.searchFunction = searchFunction; this.result = result;
4949          }
4950 <        SearchEntriesTask
4951 <            (BulkTask<K,V,?> p, int b, boolean split,
4952 <             Fun<Entry<K,V>, ? extends U> searchFunction,
4953 <             AtomicReference<U> result) {
4954 <            super(p, b, split);
4955 <            this.searchFunction = searchFunction; this.result = result;
4956 <        }
4957 <        @SuppressWarnings("unchecked") public final void compute() {
4958 <            AtomicReference<U> result = this.result;
4959 <            final Fun<Entry<K,V>, ? extends U> searchFunction =
4960 <                this.searchFunction;
4961 <            if (searchFunction == null || result == null)
4962 <                throw new Error(NullFunctionMessage);
4963 <            int b = batch(), c;
4964 <            while (b > 1 && baseIndex != baseLimit && result.get() == null) {
4965 <                do {} while (!casPending(c = pending, c+1));
4966 <                new SearchEntriesTask<K,V,U>(this, b >>>= 1, true,
4967 <                                             searchFunction, result).fork();
4968 <            }
4969 <            Object v; U u;
4970 <            while (result.get() == null && (v = advance()) != null) {
4971 <                if ((u = searchFunction.apply(entryFor((K)nextKey, (V)v))) != null) {
4972 <                    if (result.compareAndSet(null, u)) {
4973 <                        for (BulkTask<K,V,?> a = this, p;;) {
4974 <                            if ((p = a.parent) == null) {
4975 <                                a.quietlyComplete();
5395 <                                break;
5396 <                            }
5397 <                            a = p;
5398 <                        }
4950 >        public final U getRawResult() { return result.get(); }
4951 >        public final void compute() {
4952 >            final Fun<Entry<K,V>, ? extends U> searchFunction;
4953 >            final AtomicReference<U> result;
4954 >            if ((searchFunction = this.searchFunction) != null &&
4955 >                (result = this.result) != null) {
4956 >                for (int i = baseIndex, f, h; batch > 0 &&
4957 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
4958 >                    if (result.get() != null)
4959 >                        return;
4960 >                    addToPendingCount(1);
4961 >                    new SearchEntriesTask<K,V,U>
4962 >                        (this, batch >>>= 1, baseLimit = h, f, tab,
4963 >                         searchFunction, result).fork();
4964 >                }
4965 >                while (result.get() == null) {
4966 >                    U u;
4967 >                    Node<K,V> p;
4968 >                    if ((p = advance()) == null) {
4969 >                        propagateCompletion();
4970 >                        break;
4971 >                    }
4972 >                    if ((u = searchFunction.apply(p)) != null) {
4973 >                        if (result.compareAndSet(null, u))
4974 >                            quietlyCompleteRoot();
4975 >                        return;
4976                      }
5400                    break;
4977                  }
4978              }
5403            tryComplete();
4979          }
5405        public final U getRawResult() { return result.get(); }
4980      }
4981  
4982      @SuppressWarnings("serial")
# Line 5411 | Line 4985 | public class ConcurrentHashMapV8<K, V>
4985          final BiFun<? super K, ? super V, ? extends U> searchFunction;
4986          final AtomicReference<U> result;
4987          SearchMappingsTask
4988 <            (ConcurrentHashMapV8<K,V> m,
5415 <             BiFun<? super K, ? super V, ? extends U> searchFunction,
5416 <             AtomicReference<U> result) {
5417 <            super(m);
5418 <            this.searchFunction = searchFunction; this.result = result;
5419 <        }
5420 <        SearchMappingsTask
5421 <            (BulkTask<K,V,?> p, int b, boolean split,
4988 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
4989               BiFun<? super K, ? super V, ? extends U> searchFunction,
4990               AtomicReference<U> result) {
4991 <            super(p, b, split);
4991 >            super(p, b, i, f, t);
4992              this.searchFunction = searchFunction; this.result = result;
4993          }
4994 <        @SuppressWarnings("unchecked") public final void compute() {
4995 <            AtomicReference<U> result = this.result;
4996 <            final BiFun<? super K, ? super V, ? extends U> searchFunction =
4997 <                this.searchFunction;
4998 <            if (searchFunction == null || result == null)
4999 <                throw new Error(NullFunctionMessage);
5000 <            int b = batch(), c;
5001 <            while (b > 1 && baseIndex != baseLimit && result.get() == null) {
5002 <                do {} while (!casPending(c = pending, c+1));
5003 <                new SearchMappingsTask<K,V,U>(this, b >>>= 1, true,
5004 <                                              searchFunction, result).fork();
5005 <            }
5006 <            Object v; U u;
5007 <            while (result.get() == null && (v = advance()) != null) {
5008 <                if ((u = searchFunction.apply((K)nextKey, (V)v)) != null) {
5009 <                    if (result.compareAndSet(null, u)) {
5010 <                        for (BulkTask<K,V,?> a = this, p;;) {
5011 <                            if ((p = a.parent) == null) {
5012 <                                a.quietlyComplete();
5013 <                                break;
5014 <                            }
5015 <                            a = p;
5016 <                        }
4994 >        public final U getRawResult() { return result.get(); }
4995 >        public final void compute() {
4996 >            final BiFun<? super K, ? super V, ? extends U> searchFunction;
4997 >            final AtomicReference<U> result;
4998 >            if ((searchFunction = this.searchFunction) != null &&
4999 >                (result = this.result) != null) {
5000 >                for (int i = baseIndex, f, h; batch > 0 &&
5001 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5002 >                    if (result.get() != null)
5003 >                        return;
5004 >                    addToPendingCount(1);
5005 >                    new SearchMappingsTask<K,V,U>
5006 >                        (this, batch >>>= 1, baseLimit = h, f, tab,
5007 >                         searchFunction, result).fork();
5008 >                }
5009 >                while (result.get() == null) {
5010 >                    U u;
5011 >                    Node<K,V> p;
5012 >                    if ((p = advance()) == null) {
5013 >                        propagateCompletion();
5014 >                        break;
5015 >                    }
5016 >                    if ((u = searchFunction.apply(p.key, p.val)) != null) {
5017 >                        if (result.compareAndSet(null, u))
5018 >                            quietlyCompleteRoot();
5019 >                        break;
5020                      }
5451                    break;
5021                  }
5022              }
5454            tryComplete();
5023          }
5456        public final U getRawResult() { return result.get(); }
5024      }
5025  
5026      @SuppressWarnings("serial")
# Line 5461 | Line 5028 | public class ConcurrentHashMapV8<K, V>
5028          extends BulkTask<K,V,K> {
5029          final BiFun<? super K, ? super K, ? extends K> reducer;
5030          K result;
5031 <        ReduceKeysTask<K,V> sibling;
5465 <        ReduceKeysTask
5466 <            (ConcurrentHashMapV8<K,V> m,
5467 <             BiFun<? super K, ? super K, ? extends K> reducer) {
5468 <            super(m);
5469 <            this.reducer = reducer;
5470 <        }
5031 >        ReduceKeysTask<K,V> rights, nextRight;
5032          ReduceKeysTask
5033 <            (BulkTask<K,V,?> p, int b, boolean split,
5033 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5034 >             ReduceKeysTask<K,V> nextRight,
5035               BiFun<? super K, ? super K, ? extends K> reducer) {
5036 <            super(p, b, split);
5036 >            super(p, b, i, f, t); this.nextRight = nextRight;
5037              this.reducer = reducer;
5038          }
5039 <
5040 <        @SuppressWarnings("unchecked") public final void compute() {
5041 <            ReduceKeysTask<K,V> t = this;
5042 <            final BiFun<? super K, ? super K, ? extends K> reducer =
5043 <                this.reducer;
5044 <            if (reducer == null)
5045 <                throw new Error(NullFunctionMessage);
5046 <            int b = batch();
5047 <            while (b > 1 && t.baseIndex != t.baseLimit) {
5048 <                b >>>= 1;
5049 <                t.pending = 1;
5050 <                ReduceKeysTask<K,V> rt =
5051 <                    new ReduceKeysTask<K,V>
5052 <                    (t, b, true, reducer);
5053 <                t = new ReduceKeysTask<K,V>
5054 <                    (t, b, false, reducer);
5055 <                t.sibling = rt;
5056 <                rt.sibling = t;
5057 <                rt.fork();
5058 <            }
5059 <            K r = null;
5060 <            while (t.advance() != null) {
5061 <                K u = (K)t.nextKey;
5062 <                r = (r == null) ? u : reducer.apply(r, u);
5063 <            }
5064 <            t.result = r;
5065 <            for (;;) {
5066 <                int c; BulkTask<K,V,?> par; ReduceKeysTask<K,V> s, p; K u;
5067 <                if ((par = t.parent) == null ||
5506 <                    !(par instanceof ReduceKeysTask)) {
5507 <                    t.quietlyComplete();
5508 <                    break;
5509 <                }
5510 <                else if ((c = (p = (ReduceKeysTask<K,V>)par).pending) == 0) {
5511 <                    if ((s = t.sibling) != null && (u = s.result) != null)
5512 <                        r = (r == null) ? u : reducer.apply(r, u);
5513 <                    (t = p).result = r;
5039 >        public final K getRawResult() { return result; }
5040 >        public final void compute() {
5041 >            final BiFun<? super K, ? super K, ? extends K> reducer;
5042 >            if ((reducer = this.reducer) != null) {
5043 >                for (int i = baseIndex, f, h; batch > 0 &&
5044 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5045 >                    addToPendingCount(1);
5046 >                    (rights = new ReduceKeysTask<K,V>
5047 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
5048 >                      rights, reducer)).fork();
5049 >                }
5050 >                K r = null;
5051 >                for (Node<K,V> p; (p = advance()) != null; ) {
5052 >                    K u = p.key;
5053 >                    r = (r == null) ? u : u == null ? r : reducer.apply(r, u);
5054 >                }
5055 >                result = r;
5056 >                CountedCompleter<?> c;
5057 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5058 >                    @SuppressWarnings("unchecked") ReduceKeysTask<K,V>
5059 >                        t = (ReduceKeysTask<K,V>)c,
5060 >                        s = t.rights;
5061 >                    while (s != null) {
5062 >                        K tr, sr;
5063 >                        if ((sr = s.result) != null)
5064 >                            t.result = (((tr = t.result) == null) ? sr :
5065 >                                        reducer.apply(tr, sr));
5066 >                        s = t.rights = s.nextRight;
5067 >                    }
5068                  }
5515                else if (p.casPending(c, 0))
5516                    break;
5069              }
5070          }
5519        public final K getRawResult() { return result; }
5071      }
5072  
5073      @SuppressWarnings("serial")
# Line 5524 | Line 5075 | public class ConcurrentHashMapV8<K, V>
5075          extends BulkTask<K,V,V> {
5076          final BiFun<? super V, ? super V, ? extends V> reducer;
5077          V result;
5078 <        ReduceValuesTask<K,V> sibling;
5528 <        ReduceValuesTask
5529 <            (ConcurrentHashMapV8<K,V> m,
5530 <             BiFun<? super V, ? super V, ? extends V> reducer) {
5531 <            super(m);
5532 <            this.reducer = reducer;
5533 <        }
5078 >        ReduceValuesTask<K,V> rights, nextRight;
5079          ReduceValuesTask
5080 <            (BulkTask<K,V,?> p, int b, boolean split,
5080 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5081 >             ReduceValuesTask<K,V> nextRight,
5082               BiFun<? super V, ? super V, ? extends V> reducer) {
5083 <            super(p, b, split);
5083 >            super(p, b, i, f, t); this.nextRight = nextRight;
5084              this.reducer = reducer;
5085          }
5086 <
5087 <        @SuppressWarnings("unchecked") public final void compute() {
5088 <            ReduceValuesTask<K,V> t = this;
5089 <            final BiFun<? super V, ? super V, ? extends V> reducer =
5090 <                this.reducer;
5091 <            if (reducer == null)
5092 <                throw new Error(NullFunctionMessage);
5093 <            int b = batch();
5094 <            while (b > 1 && t.baseIndex != t.baseLimit) {
5095 <                b >>>= 1;
5096 <                t.pending = 1;
5097 <                ReduceValuesTask<K,V> rt =
5098 <                    new ReduceValuesTask<K,V>
5099 <                    (t, b, true, reducer);
5100 <                t = new ReduceValuesTask<K,V>
5101 <                    (t, b, false, reducer);
5102 <                t.sibling = rt;
5103 <                rt.sibling = t;
5104 <                rt.fork();
5105 <            }
5106 <            V r = null;
5107 <            Object v;
5108 <            while ((v = t.advance()) != null) {
5109 <                V u = (V)v;
5110 <                r = (r == null) ? u : reducer.apply(r, u);
5111 <            }
5112 <            t.result = r;
5113 <            for (;;) {
5114 <                int c; BulkTask<K,V,?> par; ReduceValuesTask<K,V> s, p; V u;
5569 <                if ((par = t.parent) == null ||
5570 <                    !(par instanceof ReduceValuesTask)) {
5571 <                    t.quietlyComplete();
5572 <                    break;
5573 <                }
5574 <                else if ((c = (p = (ReduceValuesTask<K,V>)par).pending) == 0) {
5575 <                    if ((s = t.sibling) != null && (u = s.result) != null)
5576 <                        r = (r == null) ? u : reducer.apply(r, u);
5577 <                    (t = p).result = r;
5086 >        public final V getRawResult() { return result; }
5087 >        public final void compute() {
5088 >            final BiFun<? super V, ? super V, ? extends V> reducer;
5089 >            if ((reducer = this.reducer) != null) {
5090 >                for (int i = baseIndex, f, h; batch > 0 &&
5091 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5092 >                    addToPendingCount(1);
5093 >                    (rights = new ReduceValuesTask<K,V>
5094 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
5095 >                      rights, reducer)).fork();
5096 >                }
5097 >                V r = null;
5098 >                for (Node<K,V> p; (p = advance()) != null; ) {
5099 >                    V v = p.val;
5100 >                    r = (r == null) ? v : reducer.apply(r, v);
5101 >                }
5102 >                result = r;
5103 >                CountedCompleter<?> c;
5104 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5105 >                    @SuppressWarnings("unchecked") ReduceValuesTask<K,V>
5106 >                        t = (ReduceValuesTask<K,V>)c,
5107 >                        s = t.rights;
5108 >                    while (s != null) {
5109 >                        V tr, sr;
5110 >                        if ((sr = s.result) != null)
5111 >                            t.result = (((tr = t.result) == null) ? sr :
5112 >                                        reducer.apply(tr, sr));
5113 >                        s = t.rights = s.nextRight;
5114 >                    }
5115                  }
5579                else if (p.casPending(c, 0))
5580                    break;
5116              }
5117          }
5583        public final V getRawResult() { return result; }
5118      }
5119  
5120      @SuppressWarnings("serial")
# Line 5588 | Line 5122 | public class ConcurrentHashMapV8<K, V>
5122          extends BulkTask<K,V,Map.Entry<K,V>> {
5123          final BiFun<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer;
5124          Map.Entry<K,V> result;
5125 <        ReduceEntriesTask<K,V> sibling;
5125 >        ReduceEntriesTask<K,V> rights, nextRight;
5126          ReduceEntriesTask
5127 <            (ConcurrentHashMapV8<K,V> m,
5127 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5128 >             ReduceEntriesTask<K,V> nextRight,
5129               BiFun<Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer) {
5130 <            super(m);
5130 >            super(p, b, i, f, t); this.nextRight = nextRight;
5131              this.reducer = reducer;
5132          }
5133 <        ReduceEntriesTask
5134 <            (BulkTask<K,V,?> p, int b, boolean split,
5135 <             BiFun<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer) {
5136 <            super(p, b, split);
5137 <            this.reducer = reducer;
5138 <        }
5139 <
5140 <        @SuppressWarnings("unchecked") public final void compute() {
5141 <            ReduceEntriesTask<K,V> t = this;
5142 <            final BiFun<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer =
5143 <                this.reducer;
5144 <            if (reducer == null)
5145 <                throw new Error(NullFunctionMessage);
5146 <            int b = batch();
5147 <            while (b > 1 && t.baseIndex != t.baseLimit) {
5148 <                b >>>= 1;
5149 <                t.pending = 1;
5150 <                ReduceEntriesTask<K,V> rt =
5151 <                    new ReduceEntriesTask<K,V>
5152 <                    (t, b, true, reducer);
5153 <                t = new ReduceEntriesTask<K,V>
5154 <                    (t, b, false, reducer);
5155 <                t.sibling = rt;
5156 <                rt.sibling = t;
5157 <                rt.fork();
5158 <            }
5159 <            Map.Entry<K,V> r = null;
5625 <            Object v;
5626 <            while ((v = t.advance()) != null) {
5627 <                Map.Entry<K,V> u = entryFor((K)t.nextKey, (V)v);
5628 <                r = (r == null) ? u : reducer.apply(r, u);
5629 <            }
5630 <            t.result = r;
5631 <            for (;;) {
5632 <                int c; BulkTask<K,V,?> par; ReduceEntriesTask<K,V> s, p;
5633 <                Map.Entry<K,V> u;
5634 <                if ((par = t.parent) == null ||
5635 <                    !(par instanceof ReduceEntriesTask)) {
5636 <                    t.quietlyComplete();
5637 <                    break;
5638 <                }
5639 <                else if ((c = (p = (ReduceEntriesTask<K,V>)par).pending) == 0) {
5640 <                    if ((s = t.sibling) != null && (u = s.result) != null)
5641 <                        r = (r == null) ? u : reducer.apply(r, u);
5642 <                    (t = p).result = r;
5133 >        public final Map.Entry<K,V> getRawResult() { return result; }
5134 >        public final void compute() {
5135 >            final BiFun<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer;
5136 >            if ((reducer = this.reducer) != null) {
5137 >                for (int i = baseIndex, f, h; batch > 0 &&
5138 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5139 >                    addToPendingCount(1);
5140 >                    (rights = new ReduceEntriesTask<K,V>
5141 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
5142 >                      rights, reducer)).fork();
5143 >                }
5144 >                Map.Entry<K,V> r = null;
5145 >                for (Node<K,V> p; (p = advance()) != null; )
5146 >                    r = (r == null) ? p : reducer.apply(r, p);
5147 >                result = r;
5148 >                CountedCompleter<?> c;
5149 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5150 >                    @SuppressWarnings("unchecked") ReduceEntriesTask<K,V>
5151 >                        t = (ReduceEntriesTask<K,V>)c,
5152 >                        s = t.rights;
5153 >                    while (s != null) {
5154 >                        Map.Entry<K,V> tr, sr;
5155 >                        if ((sr = s.result) != null)
5156 >                            t.result = (((tr = t.result) == null) ? sr :
5157 >                                        reducer.apply(tr, sr));
5158 >                        s = t.rights = s.nextRight;
5159 >                    }
5160                  }
5644                else if (p.casPending(c, 0))
5645                    break;
5161              }
5162          }
5648        public final Map.Entry<K,V> getRawResult() { return result; }
5163      }
5164  
5165      @SuppressWarnings("serial")
# Line 5654 | Line 5168 | public class ConcurrentHashMapV8<K, V>
5168          final Fun<? super K, ? extends U> transformer;
5169          final BiFun<? super U, ? super U, ? extends U> reducer;
5170          U result;
5171 <        MapReduceKeysTask<K,V,U> sibling;
5658 <        MapReduceKeysTask
5659 <            (ConcurrentHashMapV8<K,V> m,
5660 <             Fun<? super K, ? extends U> transformer,
5661 <             BiFun<? super U, ? super U, ? extends U> reducer) {
5662 <            super(m);
5663 <            this.transformer = transformer;
5664 <            this.reducer = reducer;
5665 <        }
5171 >        MapReduceKeysTask<K,V,U> rights, nextRight;
5172          MapReduceKeysTask
5173 <            (BulkTask<K,V,?> p, int b, boolean split,
5173 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5174 >             MapReduceKeysTask<K,V,U> nextRight,
5175               Fun<? super K, ? extends U> transformer,
5176               BiFun<? super U, ? super U, ? extends U> reducer) {
5177 <            super(p, b, split);
5177 >            super(p, b, i, f, t); this.nextRight = nextRight;
5178              this.transformer = transformer;
5179              this.reducer = reducer;
5180          }
5181 <        @SuppressWarnings("unchecked") public final void compute() {
5182 <            MapReduceKeysTask<K,V,U> t = this;
5183 <            final Fun<? super K, ? extends U> transformer =
5184 <                this.transformer;
5185 <            final BiFun<? super U, ? super U, ? extends U> reducer =
5186 <                this.reducer;
5187 <            if (transformer == null || reducer == null)
5188 <                throw new Error(NullFunctionMessage);
5189 <            int b = batch();
5190 <            while (b > 1 && t.baseIndex != t.baseLimit) {
5191 <                b >>>= 1;
5192 <                t.pending = 1;
5193 <                MapReduceKeysTask<K,V,U> rt =
5194 <                    new MapReduceKeysTask<K,V,U>
5195 <                    (t, b, true, transformer, reducer);
5196 <                t = new MapReduceKeysTask<K,V,U>
5197 <                    (t, b, false, transformer, reducer);
5691 <                t.sibling = rt;
5692 <                rt.sibling = t;
5693 <                rt.fork();
5694 <            }
5695 <            U r = null, u;
5696 <            while (t.advance() != null) {
5697 <                if ((u = transformer.apply((K)t.nextKey)) != null)
5698 <                    r = (r == null) ? u : reducer.apply(r, u);
5699 <            }
5700 <            t.result = r;
5701 <            for (;;) {
5702 <                int c; BulkTask<K,V,?> par; MapReduceKeysTask<K,V,U> s, p;
5703 <                if ((par = t.parent) == null ||
5704 <                    !(par instanceof MapReduceKeysTask)) {
5705 <                    t.quietlyComplete();
5706 <                    break;
5707 <                }
5708 <                else if ((c = (p = (MapReduceKeysTask<K,V,U>)par).pending) == 0) {
5709 <                    if ((s = t.sibling) != null && (u = s.result) != null)
5181 >        public final U getRawResult() { return result; }
5182 >        public final void compute() {
5183 >            final Fun<? super K, ? extends U> transformer;
5184 >            final BiFun<? super U, ? super U, ? extends U> reducer;
5185 >            if ((transformer = this.transformer) != null &&
5186 >                (reducer = this.reducer) != null) {
5187 >                for (int i = baseIndex, f, h; batch > 0 &&
5188 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5189 >                    addToPendingCount(1);
5190 >                    (rights = new MapReduceKeysTask<K,V,U>
5191 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
5192 >                      rights, transformer, reducer)).fork();
5193 >                }
5194 >                U r = null;
5195 >                for (Node<K,V> p; (p = advance()) != null; ) {
5196 >                    U u;
5197 >                    if ((u = transformer.apply(p.key)) != null)
5198                          r = (r == null) ? u : reducer.apply(r, u);
5711                    (t = p).result = r;
5199                  }
5200 <                else if (p.casPending(c, 0))
5201 <                    break;
5200 >                result = r;
5201 >                CountedCompleter<?> c;
5202 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5203 >                    @SuppressWarnings("unchecked") MapReduceKeysTask<K,V,U>
5204 >                        t = (MapReduceKeysTask<K,V,U>)c,
5205 >                        s = t.rights;
5206 >                    while (s != null) {
5207 >                        U tr, sr;
5208 >                        if ((sr = s.result) != null)
5209 >                            t.result = (((tr = t.result) == null) ? sr :
5210 >                                        reducer.apply(tr, sr));
5211 >                        s = t.rights = s.nextRight;
5212 >                    }
5213 >                }
5214              }
5215          }
5717        public final U getRawResult() { return result; }
5216      }
5217  
5218      @SuppressWarnings("serial")
# Line 5723 | Line 5221 | public class ConcurrentHashMapV8<K, V>
5221          final Fun<? super V, ? extends U> transformer;
5222          final BiFun<? super U, ? super U, ? extends U> reducer;
5223          U result;
5224 <        MapReduceValuesTask<K,V,U> sibling;
5224 >        MapReduceValuesTask<K,V,U> rights, nextRight;
5225          MapReduceValuesTask
5226 <            (ConcurrentHashMapV8<K,V> m,
5226 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5227 >             MapReduceValuesTask<K,V,U> nextRight,
5228               Fun<? super V, ? extends U> transformer,
5229               BiFun<? super U, ? super U, ? extends U> reducer) {
5230 <            super(m);
5230 >            super(p, b, i, f, t); this.nextRight = nextRight;
5231              this.transformer = transformer;
5232              this.reducer = reducer;
5233          }
5234 <        MapReduceValuesTask
5235 <            (BulkTask<K,V,?> p, int b, boolean split,
5236 <             Fun<? super V, ? extends U> transformer,
5237 <             BiFun<? super U, ? super U, ? extends U> reducer) {
5238 <            super(p, b, split);
5239 <            this.transformer = transformer;
5240 <            this.reducer = reducer;
5241 <        }
5242 <        @SuppressWarnings("unchecked") public final void compute() {
5243 <            MapReduceValuesTask<K,V,U> t = this;
5244 <            final Fun<? super V, ? extends U> transformer =
5245 <                this.transformer;
5246 <            final BiFun<? super U, ? super U, ? extends U> reducer =
5247 <                this.reducer;
5248 <            if (transformer == null || reducer == null)
5249 <                throw new Error(NullFunctionMessage);
5250 <            int b = batch();
5752 <            while (b > 1 && t.baseIndex != t.baseLimit) {
5753 <                b >>>= 1;
5754 <                t.pending = 1;
5755 <                MapReduceValuesTask<K,V,U> rt =
5756 <                    new MapReduceValuesTask<K,V,U>
5757 <                    (t, b, true, transformer, reducer);
5758 <                t = new MapReduceValuesTask<K,V,U>
5759 <                    (t, b, false, transformer, reducer);
5760 <                t.sibling = rt;
5761 <                rt.sibling = t;
5762 <                rt.fork();
5763 <            }
5764 <            U r = null, u;
5765 <            Object v;
5766 <            while ((v = t.advance()) != null) {
5767 <                if ((u = transformer.apply((V)v)) != null)
5768 <                    r = (r == null) ? u : reducer.apply(r, u);
5769 <            }
5770 <            t.result = r;
5771 <            for (;;) {
5772 <                int c; BulkTask<K,V,?> par; MapReduceValuesTask<K,V,U> s, p;
5773 <                if ((par = t.parent) == null ||
5774 <                    !(par instanceof MapReduceValuesTask)) {
5775 <                    t.quietlyComplete();
5776 <                    break;
5777 <                }
5778 <                else if ((c = (p = (MapReduceValuesTask<K,V,U>)par).pending) == 0) {
5779 <                    if ((s = t.sibling) != null && (u = s.result) != null)
5234 >        public final U getRawResult() { return result; }
5235 >        public final void compute() {
5236 >            final Fun<? super V, ? extends U> transformer;
5237 >            final BiFun<? super U, ? super U, ? extends U> reducer;
5238 >            if ((transformer = this.transformer) != null &&
5239 >                (reducer = this.reducer) != null) {
5240 >                for (int i = baseIndex, f, h; batch > 0 &&
5241 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5242 >                    addToPendingCount(1);
5243 >                    (rights = new MapReduceValuesTask<K,V,U>
5244 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
5245 >                      rights, transformer, reducer)).fork();
5246 >                }
5247 >                U r = null;
5248 >                for (Node<K,V> p; (p = advance()) != null; ) {
5249 >                    U u;
5250 >                    if ((u = transformer.apply(p.val)) != null)
5251                          r = (r == null) ? u : reducer.apply(r, u);
5781                    (t = p).result = r;
5252                  }
5253 <                else if (p.casPending(c, 0))
5254 <                    break;
5253 >                result = r;
5254 >                CountedCompleter<?> c;
5255 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5256 >                    @SuppressWarnings("unchecked") MapReduceValuesTask<K,V,U>
5257 >                        t = (MapReduceValuesTask<K,V,U>)c,
5258 >                        s = t.rights;
5259 >                    while (s != null) {
5260 >                        U tr, sr;
5261 >                        if ((sr = s.result) != null)
5262 >                            t.result = (((tr = t.result) == null) ? sr :
5263 >                                        reducer.apply(tr, sr));
5264 >                        s = t.rights = s.nextRight;
5265 >                    }
5266 >                }
5267              }
5268          }
5787        public final U getRawResult() { return result; }
5269      }
5270  
5271      @SuppressWarnings("serial")
# Line 5793 | Line 5274 | public class ConcurrentHashMapV8<K, V>
5274          final Fun<Map.Entry<K,V>, ? extends U> transformer;
5275          final BiFun<? super U, ? super U, ? extends U> reducer;
5276          U result;
5277 <        MapReduceEntriesTask<K,V,U> sibling;
5277 >        MapReduceEntriesTask<K,V,U> rights, nextRight;
5278          MapReduceEntriesTask
5279 <            (ConcurrentHashMapV8<K,V> m,
5279 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5280 >             MapReduceEntriesTask<K,V,U> nextRight,
5281               Fun<Map.Entry<K,V>, ? extends U> transformer,
5282               BiFun<? super U, ? super U, ? extends U> reducer) {
5283 <            super(m);
5283 >            super(p, b, i, f, t); this.nextRight = nextRight;
5284              this.transformer = transformer;
5285              this.reducer = reducer;
5286          }
5287 <        MapReduceEntriesTask
5288 <            (BulkTask<K,V,?> p, int b, boolean split,
5289 <             Fun<Map.Entry<K,V>, ? extends U> transformer,
5290 <             BiFun<? super U, ? super U, ? extends U> reducer) {
5291 <            super(p, b, split);
5292 <            this.transformer = transformer;
5293 <            this.reducer = reducer;
5294 <        }
5295 <        @SuppressWarnings("unchecked") public final void compute() {
5296 <            MapReduceEntriesTask<K,V,U> t = this;
5297 <            final Fun<Map.Entry<K,V>, ? extends U> transformer =
5298 <                this.transformer;
5299 <            final BiFun<? super U, ? super U, ? extends U> reducer =
5300 <                this.reducer;
5301 <            if (transformer == null || reducer == null)
5302 <                throw new Error(NullFunctionMessage);
5303 <            int b = batch();
5822 <            while (b > 1 && t.baseIndex != t.baseLimit) {
5823 <                b >>>= 1;
5824 <                t.pending = 1;
5825 <                MapReduceEntriesTask<K,V,U> rt =
5826 <                    new MapReduceEntriesTask<K,V,U>
5827 <                    (t, b, true, transformer, reducer);
5828 <                t = new MapReduceEntriesTask<K,V,U>
5829 <                    (t, b, false, transformer, reducer);
5830 <                t.sibling = rt;
5831 <                rt.sibling = t;
5832 <                rt.fork();
5833 <            }
5834 <            U r = null, u;
5835 <            Object v;
5836 <            while ((v = t.advance()) != null) {
5837 <                if ((u = transformer.apply(entryFor((K)t.nextKey, (V)v))) != null)
5838 <                    r = (r == null) ? u : reducer.apply(r, u);
5839 <            }
5840 <            t.result = r;
5841 <            for (;;) {
5842 <                int c; BulkTask<K,V,?> par; MapReduceEntriesTask<K,V,U> s, p;
5843 <                if ((par = t.parent) == null ||
5844 <                    !(par instanceof MapReduceEntriesTask)) {
5845 <                    t.quietlyComplete();
5846 <                    break;
5847 <                }
5848 <                else if ((c = (p = (MapReduceEntriesTask<K,V,U>)par).pending) == 0) {
5849 <                    if ((s = t.sibling) != null && (u = s.result) != null)
5287 >        public final U getRawResult() { return result; }
5288 >        public final void compute() {
5289 >            final Fun<Map.Entry<K,V>, ? extends U> transformer;
5290 >            final BiFun<? super U, ? super U, ? extends U> reducer;
5291 >            if ((transformer = this.transformer) != null &&
5292 >                (reducer = this.reducer) != null) {
5293 >                for (int i = baseIndex, f, h; batch > 0 &&
5294 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5295 >                    addToPendingCount(1);
5296 >                    (rights = new MapReduceEntriesTask<K,V,U>
5297 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
5298 >                      rights, transformer, reducer)).fork();
5299 >                }
5300 >                U r = null;
5301 >                for (Node<K,V> p; (p = advance()) != null; ) {
5302 >                    U u;
5303 >                    if ((u = transformer.apply(p)) != null)
5304                          r = (r == null) ? u : reducer.apply(r, u);
5851                    (t = p).result = r;
5305                  }
5306 <                else if (p.casPending(c, 0))
5307 <                    break;
5306 >                result = r;
5307 >                CountedCompleter<?> c;
5308 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5309 >                    @SuppressWarnings("unchecked") MapReduceEntriesTask<K,V,U>
5310 >                        t = (MapReduceEntriesTask<K,V,U>)c,
5311 >                        s = t.rights;
5312 >                    while (s != null) {
5313 >                        U tr, sr;
5314 >                        if ((sr = s.result) != null)
5315 >                            t.result = (((tr = t.result) == null) ? sr :
5316 >                                        reducer.apply(tr, sr));
5317 >                        s = t.rights = s.nextRight;
5318 >                    }
5319 >                }
5320              }
5321          }
5857        public final U getRawResult() { return result; }
5322      }
5323  
5324      @SuppressWarnings("serial")
# Line 5863 | Line 5327 | public class ConcurrentHashMapV8<K, V>
5327          final BiFun<? super K, ? super V, ? extends U> transformer;
5328          final BiFun<? super U, ? super U, ? extends U> reducer;
5329          U result;
5330 <        MapReduceMappingsTask<K,V,U> sibling;
5867 <        MapReduceMappingsTask
5868 <            (ConcurrentHashMapV8<K,V> m,
5869 <             BiFun<? super K, ? super V, ? extends U> transformer,
5870 <             BiFun<? super U, ? super U, ? extends U> reducer) {
5871 <            super(m);
5872 <            this.transformer = transformer;
5873 <            this.reducer = reducer;
5874 <        }
5330 >        MapReduceMappingsTask<K,V,U> rights, nextRight;
5331          MapReduceMappingsTask
5332 <            (BulkTask<K,V,?> p, int b, boolean split,
5332 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5333 >             MapReduceMappingsTask<K,V,U> nextRight,
5334               BiFun<? super K, ? super V, ? extends U> transformer,
5335               BiFun<? super U, ? super U, ? extends U> reducer) {
5336 <            super(p, b, split);
5336 >            super(p, b, i, f, t); this.nextRight = nextRight;
5337              this.transformer = transformer;
5338              this.reducer = reducer;
5339          }
5340 <        @SuppressWarnings("unchecked") public final void compute() {
5341 <            MapReduceMappingsTask<K,V,U> t = this;
5342 <            final BiFun<? super K, ? super V, ? extends U> transformer =
5343 <                this.transformer;
5344 <            final BiFun<? super U, ? super U, ? extends U> reducer =
5345 <                this.reducer;
5346 <            if (transformer == null || reducer == null)
5347 <                throw new Error(NullFunctionMessage);
5348 <            int b = batch();
5349 <            while (b > 1 && t.baseIndex != t.baseLimit) {
5350 <                b >>>= 1;
5351 <                t.pending = 1;
5352 <                MapReduceMappingsTask<K,V,U> rt =
5353 <                    new MapReduceMappingsTask<K,V,U>
5354 <                    (t, b, true, transformer, reducer);
5355 <                t = new MapReduceMappingsTask<K,V,U>
5356 <                    (t, b, false, transformer, reducer);
5900 <                t.sibling = rt;
5901 <                rt.sibling = t;
5902 <                rt.fork();
5903 <            }
5904 <            U r = null, u;
5905 <            Object v;
5906 <            while ((v = t.advance()) != null) {
5907 <                if ((u = transformer.apply((K)t.nextKey, (V)v)) != null)
5908 <                    r = (r == null) ? u : reducer.apply(r, u);
5909 <            }
5910 <            for (;;) {
5911 <                int c; BulkTask<K,V,?> par; MapReduceMappingsTask<K,V,U> s, p;
5912 <                if ((par = t.parent) == null ||
5913 <                    !(par instanceof MapReduceMappingsTask)) {
5914 <                    t.quietlyComplete();
5915 <                    break;
5916 <                }
5917 <                else if ((c = (p = (MapReduceMappingsTask<K,V,U>)par).pending) == 0) {
5918 <                    if ((s = t.sibling) != null && (u = s.result) != null)
5340 >        public final U getRawResult() { return result; }
5341 >        public final void compute() {
5342 >            final BiFun<? super K, ? super V, ? extends U> transformer;
5343 >            final BiFun<? super U, ? super U, ? extends U> reducer;
5344 >            if ((transformer = this.transformer) != null &&
5345 >                (reducer = this.reducer) != null) {
5346 >                for (int i = baseIndex, f, h; batch > 0 &&
5347 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5348 >                    addToPendingCount(1);
5349 >                    (rights = new MapReduceMappingsTask<K,V,U>
5350 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
5351 >                      rights, transformer, reducer)).fork();
5352 >                }
5353 >                U r = null;
5354 >                for (Node<K,V> p; (p = advance()) != null; ) {
5355 >                    U u;
5356 >                    if ((u = transformer.apply(p.key, p.val)) != null)
5357                          r = (r == null) ? u : reducer.apply(r, u);
5920                    (t = p).result = r;
5358                  }
5359 <                else if (p.casPending(c, 0))
5360 <                    break;
5359 >                result = r;
5360 >                CountedCompleter<?> c;
5361 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5362 >                    @SuppressWarnings("unchecked") MapReduceMappingsTask<K,V,U>
5363 >                        t = (MapReduceMappingsTask<K,V,U>)c,
5364 >                        s = t.rights;
5365 >                    while (s != null) {
5366 >                        U tr, sr;
5367 >                        if ((sr = s.result) != null)
5368 >                            t.result = (((tr = t.result) == null) ? sr :
5369 >                                        reducer.apply(tr, sr));
5370 >                        s = t.rights = s.nextRight;
5371 >                    }
5372 >                }
5373              }
5374          }
5926        public final U getRawResult() { return result; }
5375      }
5376  
5377      @SuppressWarnings("serial")
# Line 5933 | Line 5381 | public class ConcurrentHashMapV8<K, V>
5381          final DoubleByDoubleToDouble reducer;
5382          final double basis;
5383          double result;
5384 <        MapReduceKeysToDoubleTask<K,V> sibling;
5384 >        MapReduceKeysToDoubleTask<K,V> rights, nextRight;
5385          MapReduceKeysToDoubleTask
5386 <            (ConcurrentHashMapV8<K,V> m,
5386 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5387 >             MapReduceKeysToDoubleTask<K,V> nextRight,
5388               ObjectToDouble<? super K> transformer,
5389               double basis,
5390               DoubleByDoubleToDouble reducer) {
5391 <            super(m);
5391 >            super(p, b, i, f, t); this.nextRight = nextRight;
5392              this.transformer = transformer;
5393              this.basis = basis; this.reducer = reducer;
5394          }
5395 <        MapReduceKeysToDoubleTask
5396 <            (BulkTask<K,V,?> p, int b, boolean split,
5397 <             ObjectToDouble<? super K> transformer,
5398 <             double basis,
5399 <             DoubleByDoubleToDouble reducer) {
5400 <            super(p, b, split);
5401 <            this.transformer = transformer;
5402 <            this.basis = basis; this.reducer = reducer;
5403 <        }
5404 <        @SuppressWarnings("unchecked") public final void compute() {
5405 <            MapReduceKeysToDoubleTask<K,V> t = this;
5406 <            final ObjectToDouble<? super K> transformer =
5407 <                this.transformer;
5408 <            final DoubleByDoubleToDouble reducer = this.reducer;
5409 <            if (transformer == null || reducer == null)
5410 <                throw new Error(NullFunctionMessage);
5411 <            final double id = this.basis;
5412 <            int b = batch();
5413 <            while (b > 1 && t.baseIndex != t.baseLimit) {
5414 <                b >>>= 1;
5415 <                t.pending = 1;
5416 <                MapReduceKeysToDoubleTask<K,V> rt =
5417 <                    new MapReduceKeysToDoubleTask<K,V>
5418 <                    (t, b, true, transformer, id, reducer);
5419 <                t = new MapReduceKeysToDoubleTask<K,V>
5420 <                    (t, b, false, transformer, id, reducer);
5972 <                t.sibling = rt;
5973 <                rt.sibling = t;
5974 <                rt.fork();
5975 <            }
5976 <            double r = id;
5977 <            while (t.advance() != null)
5978 <                r = reducer.apply(r, transformer.apply((K)t.nextKey));
5979 <            t.result = r;
5980 <            for (;;) {
5981 <                int c; BulkTask<K,V,?> par; MapReduceKeysToDoubleTask<K,V> s, p;
5982 <                if ((par = t.parent) == null ||
5983 <                    !(par instanceof MapReduceKeysToDoubleTask)) {
5984 <                    t.quietlyComplete();
5985 <                    break;
5986 <                }
5987 <                else if ((c = (p = (MapReduceKeysToDoubleTask<K,V>)par).pending) == 0) {
5988 <                    if ((s = t.sibling) != null)
5989 <                        r = reducer.apply(r, s.result);
5990 <                    (t = p).result = r;
5395 >        public final Double getRawResult() { return result; }
5396 >        public final void compute() {
5397 >            final ObjectToDouble<? super K> transformer;
5398 >            final DoubleByDoubleToDouble reducer;
5399 >            if ((transformer = this.transformer) != null &&
5400 >                (reducer = this.reducer) != null) {
5401 >                double r = this.basis;
5402 >                for (int i = baseIndex, f, h; batch > 0 &&
5403 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5404 >                    addToPendingCount(1);
5405 >                    (rights = new MapReduceKeysToDoubleTask<K,V>
5406 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
5407 >                      rights, transformer, r, reducer)).fork();
5408 >                }
5409 >                for (Node<K,V> p; (p = advance()) != null; )
5410 >                    r = reducer.apply(r, transformer.apply(p.key));
5411 >                result = r;
5412 >                CountedCompleter<?> c;
5413 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5414 >                    @SuppressWarnings("unchecked") MapReduceKeysToDoubleTask<K,V>
5415 >                        t = (MapReduceKeysToDoubleTask<K,V>)c,
5416 >                        s = t.rights;
5417 >                    while (s != null) {
5418 >                        t.result = reducer.apply(t.result, s.result);
5419 >                        s = t.rights = s.nextRight;
5420 >                    }
5421                  }
5992                else if (p.casPending(c, 0))
5993                    break;
5422              }
5423          }
5996        public final Double getRawResult() { return result; }
5424      }
5425  
5426      @SuppressWarnings("serial")
# Line 6003 | Line 5430 | public class ConcurrentHashMapV8<K, V>
5430          final DoubleByDoubleToDouble reducer;
5431          final double basis;
5432          double result;
5433 <        MapReduceValuesToDoubleTask<K,V> sibling;
5433 >        MapReduceValuesToDoubleTask<K,V> rights, nextRight;
5434          MapReduceValuesToDoubleTask
5435 <            (ConcurrentHashMapV8<K,V> m,
5435 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5436 >             MapReduceValuesToDoubleTask<K,V> nextRight,
5437               ObjectToDouble<? super V> transformer,
5438               double basis,
5439               DoubleByDoubleToDouble reducer) {
5440 <            super(m);
5440 >            super(p, b, i, f, t); this.nextRight = nextRight;
5441              this.transformer = transformer;
5442              this.basis = basis; this.reducer = reducer;
5443          }
5444 <        MapReduceValuesToDoubleTask
5445 <            (BulkTask<K,V,?> p, int b, boolean split,
5446 <             ObjectToDouble<? super V> transformer,
5447 <             double basis,
5448 <             DoubleByDoubleToDouble reducer) {
5449 <            super(p, b, split);
5450 <            this.transformer = transformer;
5451 <            this.basis = basis; this.reducer = reducer;
5452 <        }
5453 <        @SuppressWarnings("unchecked") public final void compute() {
5454 <            MapReduceValuesToDoubleTask<K,V> t = this;
5455 <            final ObjectToDouble<? super V> transformer =
5456 <                this.transformer;
5457 <            final DoubleByDoubleToDouble reducer = this.reducer;
5458 <            if (transformer == null || reducer == null)
5459 <                throw new Error(NullFunctionMessage);
5460 <            final double id = this.basis;
5461 <            int b = batch();
5462 <            while (b > 1 && t.baseIndex != t.baseLimit) {
5463 <                b >>>= 1;
5464 <                t.pending = 1;
5465 <                MapReduceValuesToDoubleTask<K,V> rt =
5466 <                    new MapReduceValuesToDoubleTask<K,V>
5467 <                    (t, b, true, transformer, id, reducer);
5468 <                t = new MapReduceValuesToDoubleTask<K,V>
5469 <                    (t, b, false, transformer, id, reducer);
6042 <                t.sibling = rt;
6043 <                rt.sibling = t;
6044 <                rt.fork();
6045 <            }
6046 <            double r = id;
6047 <            Object v;
6048 <            while ((v = t.advance()) != null)
6049 <                r = reducer.apply(r, transformer.apply((V)v));
6050 <            t.result = r;
6051 <            for (;;) {
6052 <                int c; BulkTask<K,V,?> par; MapReduceValuesToDoubleTask<K,V> s, p;
6053 <                if ((par = t.parent) == null ||
6054 <                    !(par instanceof MapReduceValuesToDoubleTask)) {
6055 <                    t.quietlyComplete();
6056 <                    break;
6057 <                }
6058 <                else if ((c = (p = (MapReduceValuesToDoubleTask<K,V>)par).pending) == 0) {
6059 <                    if ((s = t.sibling) != null)
6060 <                        r = reducer.apply(r, s.result);
6061 <                    (t = p).result = r;
5444 >        public final Double getRawResult() { return result; }
5445 >        public final void compute() {
5446 >            final ObjectToDouble<? super V> transformer;
5447 >            final DoubleByDoubleToDouble reducer;
5448 >            if ((transformer = this.transformer) != null &&
5449 >                (reducer = this.reducer) != null) {
5450 >                double r = this.basis;
5451 >                for (int i = baseIndex, f, h; batch > 0 &&
5452 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5453 >                    addToPendingCount(1);
5454 >                    (rights = new MapReduceValuesToDoubleTask<K,V>
5455 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
5456 >                      rights, transformer, r, reducer)).fork();
5457 >                }
5458 >                for (Node<K,V> p; (p = advance()) != null; )
5459 >                    r = reducer.apply(r, transformer.apply(p.val));
5460 >                result = r;
5461 >                CountedCompleter<?> c;
5462 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5463 >                    @SuppressWarnings("unchecked") MapReduceValuesToDoubleTask<K,V>
5464 >                        t = (MapReduceValuesToDoubleTask<K,V>)c,
5465 >                        s = t.rights;
5466 >                    while (s != null) {
5467 >                        t.result = reducer.apply(t.result, s.result);
5468 >                        s = t.rights = s.nextRight;
5469 >                    }
5470                  }
6063                else if (p.casPending(c, 0))
6064                    break;
5471              }
5472          }
6067        public final Double getRawResult() { return result; }
5473      }
5474  
5475      @SuppressWarnings("serial")
# Line 6074 | Line 5479 | public class ConcurrentHashMapV8<K, V>
5479          final DoubleByDoubleToDouble reducer;
5480          final double basis;
5481          double result;
5482 <        MapReduceEntriesToDoubleTask<K,V> sibling;
6078 <        MapReduceEntriesToDoubleTask
6079 <            (ConcurrentHashMapV8<K,V> m,
6080 <             ObjectToDouble<Map.Entry<K,V>> transformer,
6081 <             double basis,
6082 <             DoubleByDoubleToDouble reducer) {
6083 <            super(m);
6084 <            this.transformer = transformer;
6085 <            this.basis = basis; this.reducer = reducer;
6086 <        }
5482 >        MapReduceEntriesToDoubleTask<K,V> rights, nextRight;
5483          MapReduceEntriesToDoubleTask
5484 <            (BulkTask<K,V,?> p, int b, boolean split,
5484 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5485 >             MapReduceEntriesToDoubleTask<K,V> nextRight,
5486               ObjectToDouble<Map.Entry<K,V>> transformer,
5487               double basis,
5488               DoubleByDoubleToDouble reducer) {
5489 <            super(p, b, split);
5489 >            super(p, b, i, f, t); this.nextRight = nextRight;
5490              this.transformer = transformer;
5491              this.basis = basis; this.reducer = reducer;
5492          }
5493 <        @SuppressWarnings("unchecked") public final void compute() {
5494 <            MapReduceEntriesToDoubleTask<K,V> t = this;
5495 <            final ObjectToDouble<Map.Entry<K,V>> transformer =
5496 <                this.transformer;
5497 <            final DoubleByDoubleToDouble reducer = this.reducer;
5498 <            if (transformer == null || reducer == null)
5499 <                throw new Error(NullFunctionMessage);
5500 <            final double id = this.basis;
5501 <            int b = batch();
5502 <            while (b > 1 && t.baseIndex != t.baseLimit) {
5503 <                b >>>= 1;
5504 <                t.pending = 1;
5505 <                MapReduceEntriesToDoubleTask<K,V> rt =
5506 <                    new MapReduceEntriesToDoubleTask<K,V>
5507 <                    (t, b, true, transformer, id, reducer);
5508 <                t = new MapReduceEntriesToDoubleTask<K,V>
5509 <                    (t, b, false, transformer, id, reducer);
5510 <                t.sibling = rt;
5511 <                rt.sibling = t;
5512 <                rt.fork();
5513 <            }
5514 <            double r = id;
5515 <            Object v;
5516 <            while ((v = t.advance()) != null)
5517 <                r = reducer.apply(r, transformer.apply(entryFor((K)t.nextKey, (V)v)));
5518 <            t.result = r;
6122 <            for (;;) {
6123 <                int c; BulkTask<K,V,?> par; MapReduceEntriesToDoubleTask<K,V> s, p;
6124 <                if ((par = t.parent) == null ||
6125 <                    !(par instanceof MapReduceEntriesToDoubleTask)) {
6126 <                    t.quietlyComplete();
6127 <                    break;
6128 <                }
6129 <                else if ((c = (p = (MapReduceEntriesToDoubleTask<K,V>)par).pending) == 0) {
6130 <                    if ((s = t.sibling) != null)
6131 <                        r = reducer.apply(r, s.result);
6132 <                    (t = p).result = r;
5493 >        public final Double getRawResult() { return result; }
5494 >        public final void compute() {
5495 >            final ObjectToDouble<Map.Entry<K,V>> transformer;
5496 >            final DoubleByDoubleToDouble reducer;
5497 >            if ((transformer = this.transformer) != null &&
5498 >                (reducer = this.reducer) != null) {
5499 >                double r = this.basis;
5500 >                for (int i = baseIndex, f, h; batch > 0 &&
5501 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5502 >                    addToPendingCount(1);
5503 >                    (rights = new MapReduceEntriesToDoubleTask<K,V>
5504 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
5505 >                      rights, transformer, r, reducer)).fork();
5506 >                }
5507 >                for (Node<K,V> p; (p = advance()) != null; )
5508 >                    r = reducer.apply(r, transformer.apply(p));
5509 >                result = r;
5510 >                CountedCompleter<?> c;
5511 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5512 >                    @SuppressWarnings("unchecked") MapReduceEntriesToDoubleTask<K,V>
5513 >                        t = (MapReduceEntriesToDoubleTask<K,V>)c,
5514 >                        s = t.rights;
5515 >                    while (s != null) {
5516 >                        t.result = reducer.apply(t.result, s.result);
5517 >                        s = t.rights = s.nextRight;
5518 >                    }
5519                  }
6134                else if (p.casPending(c, 0))
6135                    break;
5520              }
5521          }
6138        public final Double getRawResult() { return result; }
5522      }
5523  
5524      @SuppressWarnings("serial")
# Line 6145 | Line 5528 | public class ConcurrentHashMapV8<K, V>
5528          final DoubleByDoubleToDouble reducer;
5529          final double basis;
5530          double result;
5531 <        MapReduceMappingsToDoubleTask<K,V> sibling;
6149 <        MapReduceMappingsToDoubleTask
6150 <            (ConcurrentHashMapV8<K,V> m,
6151 <             ObjectByObjectToDouble<? super K, ? super V> transformer,
6152 <             double basis,
6153 <             DoubleByDoubleToDouble reducer) {
6154 <            super(m);
6155 <            this.transformer = transformer;
6156 <            this.basis = basis; this.reducer = reducer;
6157 <        }
5531 >        MapReduceMappingsToDoubleTask<K,V> rights, nextRight;
5532          MapReduceMappingsToDoubleTask
5533 <            (BulkTask<K,V,?> p, int b, boolean split,
5533 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5534 >             MapReduceMappingsToDoubleTask<K,V> nextRight,
5535               ObjectByObjectToDouble<? super K, ? super V> transformer,
5536               double basis,
5537               DoubleByDoubleToDouble reducer) {
5538 <            super(p, b, split);
5538 >            super(p, b, i, f, t); this.nextRight = nextRight;
5539              this.transformer = transformer;
5540              this.basis = basis; this.reducer = reducer;
5541          }
5542 <        @SuppressWarnings("unchecked") public final void compute() {
5543 <            MapReduceMappingsToDoubleTask<K,V> t = this;
5544 <            final ObjectByObjectToDouble<? super K, ? super V> transformer =
5545 <                this.transformer;
5546 <            final DoubleByDoubleToDouble reducer = this.reducer;
5547 <            if (transformer == null || reducer == null)
5548 <                throw new Error(NullFunctionMessage);
5549 <            final double id = this.basis;
5550 <            int b = batch();
5551 <            while (b > 1 && t.baseIndex != t.baseLimit) {
5552 <                b >>>= 1;
5553 <                t.pending = 1;
5554 <                MapReduceMappingsToDoubleTask<K,V> rt =
5555 <                    new MapReduceMappingsToDoubleTask<K,V>
5556 <                    (t, b, true, transformer, id, reducer);
5557 <                t = new MapReduceMappingsToDoubleTask<K,V>
5558 <                    (t, b, false, transformer, id, reducer);
5559 <                t.sibling = rt;
5560 <                rt.sibling = t;
5561 <                rt.fork();
5562 <            }
5563 <            double r = id;
5564 <            Object v;
5565 <            while ((v = t.advance()) != null)
5566 <                r = reducer.apply(r, transformer.apply((K)t.nextKey, (V)v));
5567 <            t.result = r;
6193 <            for (;;) {
6194 <                int c; BulkTask<K,V,?> par; MapReduceMappingsToDoubleTask<K,V> s, p;
6195 <                if ((par = t.parent) == null ||
6196 <                    !(par instanceof MapReduceMappingsToDoubleTask)) {
6197 <                    t.quietlyComplete();
6198 <                    break;
6199 <                }
6200 <                else if ((c = (p = (MapReduceMappingsToDoubleTask<K,V>)par).pending) == 0) {
6201 <                    if ((s = t.sibling) != null)
6202 <                        r = reducer.apply(r, s.result);
6203 <                    (t = p).result = r;
5542 >        public final Double getRawResult() { return result; }
5543 >        public final void compute() {
5544 >            final ObjectByObjectToDouble<? super K, ? super V> transformer;
5545 >            final DoubleByDoubleToDouble reducer;
5546 >            if ((transformer = this.transformer) != null &&
5547 >                (reducer = this.reducer) != null) {
5548 >                double r = this.basis;
5549 >                for (int i = baseIndex, f, h; batch > 0 &&
5550 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5551 >                    addToPendingCount(1);
5552 >                    (rights = new MapReduceMappingsToDoubleTask<K,V>
5553 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
5554 >                      rights, transformer, r, reducer)).fork();
5555 >                }
5556 >                for (Node<K,V> p; (p = advance()) != null; )
5557 >                    r = reducer.apply(r, transformer.apply(p.key, p.val));
5558 >                result = r;
5559 >                CountedCompleter<?> c;
5560 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5561 >                    @SuppressWarnings("unchecked") MapReduceMappingsToDoubleTask<K,V>
5562 >                        t = (MapReduceMappingsToDoubleTask<K,V>)c,
5563 >                        s = t.rights;
5564 >                    while (s != null) {
5565 >                        t.result = reducer.apply(t.result, s.result);
5566 >                        s = t.rights = s.nextRight;
5567 >                    }
5568                  }
6205                else if (p.casPending(c, 0))
6206                    break;
5569              }
5570          }
6209        public final Double getRawResult() { return result; }
5571      }
5572  
5573      @SuppressWarnings("serial")
# Line 6216 | Line 5577 | public class ConcurrentHashMapV8<K, V>
5577          final LongByLongToLong reducer;
5578          final long basis;
5579          long result;
5580 <        MapReduceKeysToLongTask<K,V> sibling;
5580 >        MapReduceKeysToLongTask<K,V> rights, nextRight;
5581          MapReduceKeysToLongTask
5582 <            (ConcurrentHashMapV8<K,V> m,
5582 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5583 >             MapReduceKeysToLongTask<K,V> nextRight,
5584               ObjectToLong<? super K> transformer,
5585               long basis,
5586               LongByLongToLong reducer) {
5587 <            super(m);
5587 >            super(p, b, i, f, t); this.nextRight = nextRight;
5588              this.transformer = transformer;
5589              this.basis = basis; this.reducer = reducer;
5590          }
5591 <        MapReduceKeysToLongTask
5592 <            (BulkTask<K,V,?> p, int b, boolean split,
5593 <             ObjectToLong<? super K> transformer,
5594 <             long basis,
5595 <             LongByLongToLong reducer) {
5596 <            super(p, b, split);
5597 <            this.transformer = transformer;
5598 <            this.basis = basis; this.reducer = reducer;
5599 <        }
5600 <        @SuppressWarnings("unchecked") public final void compute() {
5601 <            MapReduceKeysToLongTask<K,V> t = this;
5602 <            final ObjectToLong<? super K> transformer =
5603 <                this.transformer;
5604 <            final LongByLongToLong reducer = this.reducer;
5605 <            if (transformer == null || reducer == null)
5606 <                throw new Error(NullFunctionMessage);
5607 <            final long id = this.basis;
5608 <            int b = batch();
5609 <            while (b > 1 && t.baseIndex != t.baseLimit) {
5610 <                b >>>= 1;
5611 <                t.pending = 1;
5612 <                MapReduceKeysToLongTask<K,V> rt =
5613 <                    new MapReduceKeysToLongTask<K,V>
5614 <                    (t, b, true, transformer, id, reducer);
5615 <                t = new MapReduceKeysToLongTask<K,V>
5616 <                    (t, b, false, transformer, id, reducer);
6255 <                t.sibling = rt;
6256 <                rt.sibling = t;
6257 <                rt.fork();
6258 <            }
6259 <            long r = id;
6260 <            while (t.advance() != null)
6261 <                r = reducer.apply(r, transformer.apply((K)t.nextKey));
6262 <            t.result = r;
6263 <            for (;;) {
6264 <                int c; BulkTask<K,V,?> par; MapReduceKeysToLongTask<K,V> s, p;
6265 <                if ((par = t.parent) == null ||
6266 <                    !(par instanceof MapReduceKeysToLongTask)) {
6267 <                    t.quietlyComplete();
6268 <                    break;
6269 <                }
6270 <                else if ((c = (p = (MapReduceKeysToLongTask<K,V>)par).pending) == 0) {
6271 <                    if ((s = t.sibling) != null)
6272 <                        r = reducer.apply(r, s.result);
6273 <                    (t = p).result = r;
5591 >        public final Long getRawResult() { return result; }
5592 >        public final void compute() {
5593 >            final ObjectToLong<? super K> transformer;
5594 >            final LongByLongToLong reducer;
5595 >            if ((transformer = this.transformer) != null &&
5596 >                (reducer = this.reducer) != null) {
5597 >                long r = this.basis;
5598 >                for (int i = baseIndex, f, h; batch > 0 &&
5599 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5600 >                    addToPendingCount(1);
5601 >                    (rights = new MapReduceKeysToLongTask<K,V>
5602 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
5603 >                      rights, transformer, r, reducer)).fork();
5604 >                }
5605 >                for (Node<K,V> p; (p = advance()) != null; )
5606 >                    r = reducer.apply(r, transformer.apply(p.key));
5607 >                result = r;
5608 >                CountedCompleter<?> c;
5609 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5610 >                    @SuppressWarnings("unchecked") MapReduceKeysToLongTask<K,V>
5611 >                        t = (MapReduceKeysToLongTask<K,V>)c,
5612 >                        s = t.rights;
5613 >                    while (s != null) {
5614 >                        t.result = reducer.apply(t.result, s.result);
5615 >                        s = t.rights = s.nextRight;
5616 >                    }
5617                  }
6275                else if (p.casPending(c, 0))
6276                    break;
5618              }
5619          }
6279        public final Long getRawResult() { return result; }
5620      }
5621  
5622      @SuppressWarnings("serial")
# Line 6286 | Line 5626 | public class ConcurrentHashMapV8<K, V>
5626          final LongByLongToLong reducer;
5627          final long basis;
5628          long result;
5629 <        MapReduceValuesToLongTask<K,V> sibling;
6290 <        MapReduceValuesToLongTask
6291 <            (ConcurrentHashMapV8<K,V> m,
6292 <             ObjectToLong<? super V> transformer,
6293 <             long basis,
6294 <             LongByLongToLong reducer) {
6295 <            super(m);
6296 <            this.transformer = transformer;
6297 <            this.basis = basis; this.reducer = reducer;
6298 <        }
5629 >        MapReduceValuesToLongTask<K,V> rights, nextRight;
5630          MapReduceValuesToLongTask
5631 <            (BulkTask<K,V,?> p, int b, boolean split,
5631 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5632 >             MapReduceValuesToLongTask<K,V> nextRight,
5633               ObjectToLong<? super V> transformer,
5634               long basis,
5635               LongByLongToLong reducer) {
5636 <            super(p, b, split);
5636 >            super(p, b, i, f, t); this.nextRight = nextRight;
5637              this.transformer = transformer;
5638              this.basis = basis; this.reducer = reducer;
5639          }
5640 <        @SuppressWarnings("unchecked") public final void compute() {
5641 <            MapReduceValuesToLongTask<K,V> t = this;
5642 <            final ObjectToLong<? super V> transformer =
5643 <                this.transformer;
5644 <            final LongByLongToLong reducer = this.reducer;
5645 <            if (transformer == null || reducer == null)
5646 <                throw new Error(NullFunctionMessage);
5647 <            final long id = this.basis;
5648 <            int b = batch();
5649 <            while (b > 1 && t.baseIndex != t.baseLimit) {
5650 <                b >>>= 1;
5651 <                t.pending = 1;
5652 <                MapReduceValuesToLongTask<K,V> rt =
5653 <                    new MapReduceValuesToLongTask<K,V>
5654 <                    (t, b, true, transformer, id, reducer);
5655 <                t = new MapReduceValuesToLongTask<K,V>
5656 <                    (t, b, false, transformer, id, reducer);
5657 <                t.sibling = rt;
5658 <                rt.sibling = t;
5659 <                rt.fork();
5660 <            }
5661 <            long r = id;
5662 <            Object v;
5663 <            while ((v = t.advance()) != null)
5664 <                r = reducer.apply(r, transformer.apply((V)v));
5665 <            t.result = r;
6334 <            for (;;) {
6335 <                int c; BulkTask<K,V,?> par; MapReduceValuesToLongTask<K,V> s, p;
6336 <                if ((par = t.parent) == null ||
6337 <                    !(par instanceof MapReduceValuesToLongTask)) {
6338 <                    t.quietlyComplete();
6339 <                    break;
6340 <                }
6341 <                else if ((c = (p = (MapReduceValuesToLongTask<K,V>)par).pending) == 0) {
6342 <                    if ((s = t.sibling) != null)
6343 <                        r = reducer.apply(r, s.result);
6344 <                    (t = p).result = r;
5640 >        public final Long getRawResult() { return result; }
5641 >        public final void compute() {
5642 >            final ObjectToLong<? super V> transformer;
5643 >            final LongByLongToLong reducer;
5644 >            if ((transformer = this.transformer) != null &&
5645 >                (reducer = this.reducer) != null) {
5646 >                long r = this.basis;
5647 >                for (int i = baseIndex, f, h; batch > 0 &&
5648 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5649 >                    addToPendingCount(1);
5650 >                    (rights = new MapReduceValuesToLongTask<K,V>
5651 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
5652 >                      rights, transformer, r, reducer)).fork();
5653 >                }
5654 >                for (Node<K,V> p; (p = advance()) != null; )
5655 >                    r = reducer.apply(r, transformer.apply(p.val));
5656 >                result = r;
5657 >                CountedCompleter<?> c;
5658 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5659 >                    @SuppressWarnings("unchecked") MapReduceValuesToLongTask<K,V>
5660 >                        t = (MapReduceValuesToLongTask<K,V>)c,
5661 >                        s = t.rights;
5662 >                    while (s != null) {
5663 >                        t.result = reducer.apply(t.result, s.result);
5664 >                        s = t.rights = s.nextRight;
5665 >                    }
5666                  }
6346                else if (p.casPending(c, 0))
6347                    break;
5667              }
5668          }
6350        public final Long getRawResult() { return result; }
5669      }
5670  
5671      @SuppressWarnings("serial")
# Line 6357 | Line 5675 | public class ConcurrentHashMapV8<K, V>
5675          final LongByLongToLong reducer;
5676          final long basis;
5677          long result;
5678 <        MapReduceEntriesToLongTask<K,V> sibling;
5678 >        MapReduceEntriesToLongTask<K,V> rights, nextRight;
5679          MapReduceEntriesToLongTask
5680 <            (ConcurrentHashMapV8<K,V> m,
5680 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5681 >             MapReduceEntriesToLongTask<K,V> nextRight,
5682               ObjectToLong<Map.Entry<K,V>> transformer,
5683               long basis,
5684               LongByLongToLong reducer) {
5685 <            super(m);
5685 >            super(p, b, i, f, t); this.nextRight = nextRight;
5686              this.transformer = transformer;
5687              this.basis = basis; this.reducer = reducer;
5688          }
5689 <        MapReduceEntriesToLongTask
5690 <            (BulkTask<K,V,?> p, int b, boolean split,
5691 <             ObjectToLong<Map.Entry<K,V>> transformer,
5692 <             long basis,
5693 <             LongByLongToLong reducer) {
5694 <            super(p, b, split);
5695 <            this.transformer = transformer;
5696 <            this.basis = basis; this.reducer = reducer;
5697 <        }
5698 <        @SuppressWarnings("unchecked") public final void compute() {
5699 <            MapReduceEntriesToLongTask<K,V> t = this;
5700 <            final ObjectToLong<Map.Entry<K,V>> transformer =
5701 <                this.transformer;
5702 <            final LongByLongToLong reducer = this.reducer;
5703 <            if (transformer == null || reducer == null)
5704 <                throw new Error(NullFunctionMessage);
5705 <            final long id = this.basis;
5706 <            int b = batch();
5707 <            while (b > 1 && t.baseIndex != t.baseLimit) {
5708 <                b >>>= 1;
5709 <                t.pending = 1;
5710 <                MapReduceEntriesToLongTask<K,V> rt =
5711 <                    new MapReduceEntriesToLongTask<K,V>
5712 <                    (t, b, true, transformer, id, reducer);
5713 <                t = new MapReduceEntriesToLongTask<K,V>
5714 <                    (t, b, false, transformer, id, reducer);
6396 <                t.sibling = rt;
6397 <                rt.sibling = t;
6398 <                rt.fork();
6399 <            }
6400 <            long r = id;
6401 <            Object v;
6402 <            while ((v = t.advance()) != null)
6403 <                r = reducer.apply(r, transformer.apply(entryFor((K)t.nextKey, (V)v)));
6404 <            t.result = r;
6405 <            for (;;) {
6406 <                int c; BulkTask<K,V,?> par; MapReduceEntriesToLongTask<K,V> s, p;
6407 <                if ((par = t.parent) == null ||
6408 <                    !(par instanceof MapReduceEntriesToLongTask)) {
6409 <                    t.quietlyComplete();
6410 <                    break;
6411 <                }
6412 <                else if ((c = (p = (MapReduceEntriesToLongTask<K,V>)par).pending) == 0) {
6413 <                    if ((s = t.sibling) != null)
6414 <                        r = reducer.apply(r, s.result);
6415 <                    (t = p).result = r;
5689 >        public final Long getRawResult() { return result; }
5690 >        public final void compute() {
5691 >            final ObjectToLong<Map.Entry<K,V>> transformer;
5692 >            final LongByLongToLong reducer;
5693 >            if ((transformer = this.transformer) != null &&
5694 >                (reducer = this.reducer) != null) {
5695 >                long r = this.basis;
5696 >                for (int i = baseIndex, f, h; batch > 0 &&
5697 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5698 >                    addToPendingCount(1);
5699 >                    (rights = new MapReduceEntriesToLongTask<K,V>
5700 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
5701 >                      rights, transformer, r, reducer)).fork();
5702 >                }
5703 >                for (Node<K,V> p; (p = advance()) != null; )
5704 >                    r = reducer.apply(r, transformer.apply(p));
5705 >                result = r;
5706 >                CountedCompleter<?> c;
5707 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5708 >                    @SuppressWarnings("unchecked") MapReduceEntriesToLongTask<K,V>
5709 >                        t = (MapReduceEntriesToLongTask<K,V>)c,
5710 >                        s = t.rights;
5711 >                    while (s != null) {
5712 >                        t.result = reducer.apply(t.result, s.result);
5713 >                        s = t.rights = s.nextRight;
5714 >                    }
5715                  }
6417                else if (p.casPending(c, 0))
6418                    break;
5716              }
5717          }
6421        public final Long getRawResult() { return result; }
5718      }
5719  
5720      @SuppressWarnings("serial")
# Line 6428 | Line 5724 | public class ConcurrentHashMapV8<K, V>
5724          final LongByLongToLong reducer;
5725          final long basis;
5726          long result;
5727 <        MapReduceMappingsToLongTask<K,V> sibling;
6432 <        MapReduceMappingsToLongTask
6433 <            (ConcurrentHashMapV8<K,V> m,
6434 <             ObjectByObjectToLong<? super K, ? super V> transformer,
6435 <             long basis,
6436 <             LongByLongToLong reducer) {
6437 <            super(m);
6438 <            this.transformer = transformer;
6439 <            this.basis = basis; this.reducer = reducer;
6440 <        }
5727 >        MapReduceMappingsToLongTask<K,V> rights, nextRight;
5728          MapReduceMappingsToLongTask
5729 <            (BulkTask<K,V,?> p, int b, boolean split,
5729 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5730 >             MapReduceMappingsToLongTask<K,V> nextRight,
5731               ObjectByObjectToLong<? super K, ? super V> transformer,
5732               long basis,
5733               LongByLongToLong reducer) {
5734 <            super(p, b, split);
5734 >            super(p, b, i, f, t); this.nextRight = nextRight;
5735              this.transformer = transformer;
5736              this.basis = basis; this.reducer = reducer;
5737          }
5738 <        @SuppressWarnings("unchecked") public final void compute() {
5739 <            MapReduceMappingsToLongTask<K,V> t = this;
5740 <            final ObjectByObjectToLong<? super K, ? super V> transformer =
5741 <                this.transformer;
5742 <            final LongByLongToLong reducer = this.reducer;
5743 <            if (transformer == null || reducer == null)
5744 <                throw new Error(NullFunctionMessage);
5745 <            final long id = this.basis;
5746 <            int b = batch();
5747 <            while (b > 1 && t.baseIndex != t.baseLimit) {
5748 <                b >>>= 1;
5749 <                t.pending = 1;
5750 <                MapReduceMappingsToLongTask<K,V> rt =
5751 <                    new MapReduceMappingsToLongTask<K,V>
5752 <                    (t, b, true, transformer, id, reducer);
5753 <                t = new MapReduceMappingsToLongTask<K,V>
5754 <                    (t, b, false, transformer, id, reducer);
5755 <                t.sibling = rt;
5756 <                rt.sibling = t;
5757 <                rt.fork();
5758 <            }
5759 <            long r = id;
5760 <            Object v;
5761 <            while ((v = t.advance()) != null)
5762 <                r = reducer.apply(r, transformer.apply((K)t.nextKey, (V)v));
5763 <            t.result = r;
6476 <            for (;;) {
6477 <                int c; BulkTask<K,V,?> par; MapReduceMappingsToLongTask<K,V> s, p;
6478 <                if ((par = t.parent) == null ||
6479 <                    !(par instanceof MapReduceMappingsToLongTask)) {
6480 <                    t.quietlyComplete();
6481 <                    break;
6482 <                }
6483 <                else if ((c = (p = (MapReduceMappingsToLongTask<K,V>)par).pending) == 0) {
6484 <                    if ((s = t.sibling) != null)
6485 <                        r = reducer.apply(r, s.result);
6486 <                    (t = p).result = r;
5738 >        public final Long getRawResult() { return result; }
5739 >        public final void compute() {
5740 >            final ObjectByObjectToLong<? super K, ? super V> transformer;
5741 >            final LongByLongToLong reducer;
5742 >            if ((transformer = this.transformer) != null &&
5743 >                (reducer = this.reducer) != null) {
5744 >                long r = this.basis;
5745 >                for (int i = baseIndex, f, h; batch > 0 &&
5746 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5747 >                    addToPendingCount(1);
5748 >                    (rights = new MapReduceMappingsToLongTask<K,V>
5749 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
5750 >                      rights, transformer, r, reducer)).fork();
5751 >                }
5752 >                for (Node<K,V> p; (p = advance()) != null; )
5753 >                    r = reducer.apply(r, transformer.apply(p.key, p.val));
5754 >                result = r;
5755 >                CountedCompleter<?> c;
5756 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5757 >                    @SuppressWarnings("unchecked") MapReduceMappingsToLongTask<K,V>
5758 >                        t = (MapReduceMappingsToLongTask<K,V>)c,
5759 >                        s = t.rights;
5760 >                    while (s != null) {
5761 >                        t.result = reducer.apply(t.result, s.result);
5762 >                        s = t.rights = s.nextRight;
5763 >                    }
5764                  }
6488                else if (p.casPending(c, 0))
6489                    break;
5765              }
5766          }
6492        public final Long getRawResult() { return result; }
5767      }
5768  
5769      @SuppressWarnings("serial")
# Line 6499 | Line 5773 | public class ConcurrentHashMapV8<K, V>
5773          final IntByIntToInt reducer;
5774          final int basis;
5775          int result;
5776 <        MapReduceKeysToIntTask<K,V> sibling;
6503 <        MapReduceKeysToIntTask
6504 <            (ConcurrentHashMapV8<K,V> m,
6505 <             ObjectToInt<? super K> transformer,
6506 <             int basis,
6507 <             IntByIntToInt reducer) {
6508 <            super(m);
6509 <            this.transformer = transformer;
6510 <            this.basis = basis; this.reducer = reducer;
6511 <        }
5776 >        MapReduceKeysToIntTask<K,V> rights, nextRight;
5777          MapReduceKeysToIntTask
5778 <            (BulkTask<K,V,?> p, int b, boolean split,
5778 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5779 >             MapReduceKeysToIntTask<K,V> nextRight,
5780               ObjectToInt<? super K> transformer,
5781               int basis,
5782               IntByIntToInt reducer) {
5783 <            super(p, b, split);
5783 >            super(p, b, i, f, t); this.nextRight = nextRight;
5784              this.transformer = transformer;
5785              this.basis = basis; this.reducer = reducer;
5786          }
5787 <        @SuppressWarnings("unchecked") public final void compute() {
5788 <            MapReduceKeysToIntTask<K,V> t = this;
5789 <            final ObjectToInt<? super K> transformer =
5790 <                this.transformer;
5791 <            final IntByIntToInt reducer = this.reducer;
5792 <            if (transformer == null || reducer == null)
5793 <                throw new Error(NullFunctionMessage);
5794 <            final int id = this.basis;
5795 <            int b = batch();
5796 <            while (b > 1 && t.baseIndex != t.baseLimit) {
5797 <                b >>>= 1;
5798 <                t.pending = 1;
5799 <                MapReduceKeysToIntTask<K,V> rt =
5800 <                    new MapReduceKeysToIntTask<K,V>
5801 <                    (t, b, true, transformer, id, reducer);
5802 <                t = new MapReduceKeysToIntTask<K,V>
5803 <                    (t, b, false, transformer, id, reducer);
5804 <                t.sibling = rt;
5805 <                rt.sibling = t;
5806 <                rt.fork();
5807 <            }
5808 <            int r = id;
5809 <            while (t.advance() != null)
5810 <                r = reducer.apply(r, transformer.apply((K)t.nextKey));
5811 <            t.result = r;
5812 <            for (;;) {
6547 <                int c; BulkTask<K,V,?> par; MapReduceKeysToIntTask<K,V> s, p;
6548 <                if ((par = t.parent) == null ||
6549 <                    !(par instanceof MapReduceKeysToIntTask)) {
6550 <                    t.quietlyComplete();
6551 <                    break;
6552 <                }
6553 <                else if ((c = (p = (MapReduceKeysToIntTask<K,V>)par).pending) == 0) {
6554 <                    if ((s = t.sibling) != null)
6555 <                        r = reducer.apply(r, s.result);
6556 <                    (t = p).result = r;
5787 >        public final Integer getRawResult() { return result; }
5788 >        public final void compute() {
5789 >            final ObjectToInt<? super K> transformer;
5790 >            final IntByIntToInt reducer;
5791 >            if ((transformer = this.transformer) != null &&
5792 >                (reducer = this.reducer) != null) {
5793 >                int r = this.basis;
5794 >                for (int i = baseIndex, f, h; batch > 0 &&
5795 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5796 >                    addToPendingCount(1);
5797 >                    (rights = new MapReduceKeysToIntTask<K,V>
5798 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
5799 >                      rights, transformer, r, reducer)).fork();
5800 >                }
5801 >                for (Node<K,V> p; (p = advance()) != null; )
5802 >                    r = reducer.apply(r, transformer.apply(p.key));
5803 >                result = r;
5804 >                CountedCompleter<?> c;
5805 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5806 >                    @SuppressWarnings("unchecked") MapReduceKeysToIntTask<K,V>
5807 >                        t = (MapReduceKeysToIntTask<K,V>)c,
5808 >                        s = t.rights;
5809 >                    while (s != null) {
5810 >                        t.result = reducer.apply(t.result, s.result);
5811 >                        s = t.rights = s.nextRight;
5812 >                    }
5813                  }
6558                else if (p.casPending(c, 0))
6559                    break;
5814              }
5815          }
6562        public final Integer getRawResult() { return result; }
5816      }
5817  
5818      @SuppressWarnings("serial")
# Line 6569 | Line 5822 | public class ConcurrentHashMapV8<K, V>
5822          final IntByIntToInt reducer;
5823          final int basis;
5824          int result;
5825 <        MapReduceValuesToIntTask<K,V> sibling;
6573 <        MapReduceValuesToIntTask
6574 <            (ConcurrentHashMapV8<K,V> m,
6575 <             ObjectToInt<? super V> transformer,
6576 <             int basis,
6577 <             IntByIntToInt reducer) {
6578 <            super(m);
6579 <            this.transformer = transformer;
6580 <            this.basis = basis; this.reducer = reducer;
6581 <        }
5825 >        MapReduceValuesToIntTask<K,V> rights, nextRight;
5826          MapReduceValuesToIntTask
5827 <            (BulkTask<K,V,?> p, int b, boolean split,
5827 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5828 >             MapReduceValuesToIntTask<K,V> nextRight,
5829               ObjectToInt<? super V> transformer,
5830               int basis,
5831               IntByIntToInt reducer) {
5832 <            super(p, b, split);
5832 >            super(p, b, i, f, t); this.nextRight = nextRight;
5833              this.transformer = transformer;
5834              this.basis = basis; this.reducer = reducer;
5835          }
5836 <        @SuppressWarnings("unchecked") public final void compute() {
5837 <            MapReduceValuesToIntTask<K,V> t = this;
5838 <            final ObjectToInt<? super V> transformer =
5839 <                this.transformer;
5840 <            final IntByIntToInt reducer = this.reducer;
5841 <            if (transformer == null || reducer == null)
5842 <                throw new Error(NullFunctionMessage);
5843 <            final int id = this.basis;
5844 <            int b = batch();
5845 <            while (b > 1 && t.baseIndex != t.baseLimit) {
5846 <                b >>>= 1;
5847 <                t.pending = 1;
5848 <                MapReduceValuesToIntTask<K,V> rt =
5849 <                    new MapReduceValuesToIntTask<K,V>
5850 <                    (t, b, true, transformer, id, reducer);
5851 <                t = new MapReduceValuesToIntTask<K,V>
5852 <                    (t, b, false, transformer, id, reducer);
5853 <                t.sibling = rt;
5854 <                rt.sibling = t;
5855 <                rt.fork();
5856 <            }
5857 <            int r = id;
5858 <            Object v;
5859 <            while ((v = t.advance()) != null)
5860 <                r = reducer.apply(r, transformer.apply((V)v));
5861 <            t.result = r;
6617 <            for (;;) {
6618 <                int c; BulkTask<K,V,?> par; MapReduceValuesToIntTask<K,V> s, p;
6619 <                if ((par = t.parent) == null ||
6620 <                    !(par instanceof MapReduceValuesToIntTask)) {
6621 <                    t.quietlyComplete();
6622 <                    break;
6623 <                }
6624 <                else if ((c = (p = (MapReduceValuesToIntTask<K,V>)par).pending) == 0) {
6625 <                    if ((s = t.sibling) != null)
6626 <                        r = reducer.apply(r, s.result);
6627 <                    (t = p).result = r;
5836 >        public final Integer getRawResult() { return result; }
5837 >        public final void compute() {
5838 >            final ObjectToInt<? super V> transformer;
5839 >            final IntByIntToInt reducer;
5840 >            if ((transformer = this.transformer) != null &&
5841 >                (reducer = this.reducer) != null) {
5842 >                int r = this.basis;
5843 >                for (int i = baseIndex, f, h; batch > 0 &&
5844 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5845 >                    addToPendingCount(1);
5846 >                    (rights = new MapReduceValuesToIntTask<K,V>
5847 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
5848 >                      rights, transformer, r, reducer)).fork();
5849 >                }
5850 >                for (Node<K,V> p; (p = advance()) != null; )
5851 >                    r = reducer.apply(r, transformer.apply(p.val));
5852 >                result = r;
5853 >                CountedCompleter<?> c;
5854 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5855 >                    @SuppressWarnings("unchecked") MapReduceValuesToIntTask<K,V>
5856 >                        t = (MapReduceValuesToIntTask<K,V>)c,
5857 >                        s = t.rights;
5858 >                    while (s != null) {
5859 >                        t.result = reducer.apply(t.result, s.result);
5860 >                        s = t.rights = s.nextRight;
5861 >                    }
5862                  }
6629                else if (p.casPending(c, 0))
6630                    break;
5863              }
5864          }
6633        public final Integer getRawResult() { return result; }
5865      }
5866  
5867      @SuppressWarnings("serial")
# Line 6640 | Line 5871 | public class ConcurrentHashMapV8<K, V>
5871          final IntByIntToInt reducer;
5872          final int basis;
5873          int result;
5874 <        MapReduceEntriesToIntTask<K,V> sibling;
5874 >        MapReduceEntriesToIntTask<K,V> rights, nextRight;
5875          MapReduceEntriesToIntTask
5876 <            (ConcurrentHashMapV8<K,V> m,
5876 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5877 >             MapReduceEntriesToIntTask<K,V> nextRight,
5878               ObjectToInt<Map.Entry<K,V>> transformer,
5879               int basis,
5880               IntByIntToInt reducer) {
5881 <            super(m);
5881 >            super(p, b, i, f, t); this.nextRight = nextRight;
5882              this.transformer = transformer;
5883              this.basis = basis; this.reducer = reducer;
5884          }
5885 <        MapReduceEntriesToIntTask
5886 <            (BulkTask<K,V,?> p, int b, boolean split,
5887 <             ObjectToInt<Map.Entry<K,V>> transformer,
5888 <             int basis,
5889 <             IntByIntToInt reducer) {
5890 <            super(p, b, split);
5891 <            this.transformer = transformer;
5892 <            this.basis = basis; this.reducer = reducer;
5893 <        }
5894 <        @SuppressWarnings("unchecked") public final void compute() {
5895 <            MapReduceEntriesToIntTask<K,V> t = this;
5896 <            final ObjectToInt<Map.Entry<K,V>> transformer =
5897 <                this.transformer;
5898 <            final IntByIntToInt reducer = this.reducer;
5899 <            if (transformer == null || reducer == null)
5900 <                throw new Error(NullFunctionMessage);
5901 <            final int id = this.basis;
5902 <            int b = batch();
5903 <            while (b > 1 && t.baseIndex != t.baseLimit) {
5904 <                b >>>= 1;
5905 <                t.pending = 1;
5906 <                MapReduceEntriesToIntTask<K,V> rt =
5907 <                    new MapReduceEntriesToIntTask<K,V>
5908 <                    (t, b, true, transformer, id, reducer);
5909 <                t = new MapReduceEntriesToIntTask<K,V>
5910 <                    (t, b, false, transformer, id, reducer);
6679 <                t.sibling = rt;
6680 <                rt.sibling = t;
6681 <                rt.fork();
6682 <            }
6683 <            int r = id;
6684 <            Object v;
6685 <            while ((v = t.advance()) != null)
6686 <                r = reducer.apply(r, transformer.apply(entryFor((K)t.nextKey, (V)v)));
6687 <            t.result = r;
6688 <            for (;;) {
6689 <                int c; BulkTask<K,V,?> par; MapReduceEntriesToIntTask<K,V> s, p;
6690 <                if ((par = t.parent) == null ||
6691 <                    !(par instanceof MapReduceEntriesToIntTask)) {
6692 <                    t.quietlyComplete();
6693 <                    break;
6694 <                }
6695 <                else if ((c = (p = (MapReduceEntriesToIntTask<K,V>)par).pending) == 0) {
6696 <                    if ((s = t.sibling) != null)
6697 <                        r = reducer.apply(r, s.result);
6698 <                    (t = p).result = r;
5885 >        public final Integer getRawResult() { return result; }
5886 >        public final void compute() {
5887 >            final ObjectToInt<Map.Entry<K,V>> transformer;
5888 >            final IntByIntToInt reducer;
5889 >            if ((transformer = this.transformer) != null &&
5890 >                (reducer = this.reducer) != null) {
5891 >                int r = this.basis;
5892 >                for (int i = baseIndex, f, h; batch > 0 &&
5893 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5894 >                    addToPendingCount(1);
5895 >                    (rights = new MapReduceEntriesToIntTask<K,V>
5896 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
5897 >                      rights, transformer, r, reducer)).fork();
5898 >                }
5899 >                for (Node<K,V> p; (p = advance()) != null; )
5900 >                    r = reducer.apply(r, transformer.apply(p));
5901 >                result = r;
5902 >                CountedCompleter<?> c;
5903 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5904 >                    @SuppressWarnings("unchecked") MapReduceEntriesToIntTask<K,V>
5905 >                        t = (MapReduceEntriesToIntTask<K,V>)c,
5906 >                        s = t.rights;
5907 >                    while (s != null) {
5908 >                        t.result = reducer.apply(t.result, s.result);
5909 >                        s = t.rights = s.nextRight;
5910 >                    }
5911                  }
6700                else if (p.casPending(c, 0))
6701                    break;
5912              }
5913          }
6704        public final Integer getRawResult() { return result; }
5914      }
5915  
5916      @SuppressWarnings("serial")
# Line 6711 | Line 5920 | public class ConcurrentHashMapV8<K, V>
5920          final IntByIntToInt reducer;
5921          final int basis;
5922          int result;
5923 <        MapReduceMappingsToIntTask<K,V> sibling;
5923 >        MapReduceMappingsToIntTask<K,V> rights, nextRight;
5924          MapReduceMappingsToIntTask
5925 <            (ConcurrentHashMapV8<K,V> m,
5925 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5926 >             MapReduceMappingsToIntTask<K,V> nextRight,
5927               ObjectByObjectToInt<? super K, ? super V> transformer,
5928               int basis,
5929               IntByIntToInt reducer) {
5930 <            super(m);
5930 >            super(p, b, i, f, t); this.nextRight = nextRight;
5931              this.transformer = transformer;
5932              this.basis = basis; this.reducer = reducer;
5933          }
5934 <        MapReduceMappingsToIntTask
5935 <            (BulkTask<K,V,?> p, int b, boolean split,
5936 <             ObjectByObjectToInt<? super K, ? super V> transformer,
5937 <             int basis,
5938 <             IntByIntToInt reducer) {
5939 <            super(p, b, split);
5940 <            this.transformer = transformer;
5941 <            this.basis = basis; this.reducer = reducer;
5934 >        public final Integer getRawResult() { return result; }
5935 >        public final void compute() {
5936 >            final ObjectByObjectToInt<? super K, ? super V> transformer;
5937 >            final IntByIntToInt reducer;
5938 >            if ((transformer = this.transformer) != null &&
5939 >                (reducer = this.reducer) != null) {
5940 >                int r = this.basis;
5941 >                for (int i = baseIndex, f, h; batch > 0 &&
5942 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5943 >                    addToPendingCount(1);
5944 >                    (rights = new MapReduceMappingsToIntTask<K,V>
5945 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
5946 >                      rights, transformer, r, reducer)).fork();
5947 >                }
5948 >                for (Node<K,V> p; (p = advance()) != null; )
5949 >                    r = reducer.apply(r, transformer.apply(p.key, p.val));
5950 >                result = r;
5951 >                CountedCompleter<?> c;
5952 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5953 >                    @SuppressWarnings("unchecked") MapReduceMappingsToIntTask<K,V>
5954 >                        t = (MapReduceMappingsToIntTask<K,V>)c,
5955 >                        s = t.rights;
5956 >                    while (s != null) {
5957 >                        t.result = reducer.apply(t.result, s.result);
5958 >                        s = t.rights = s.nextRight;
5959 >                    }
5960 >                }
5961 >            }
5962          }
5963 <        @SuppressWarnings("unchecked") public final void compute() {
5964 <            MapReduceMappingsToIntTask<K,V> t = this;
5965 <            final ObjectByObjectToInt<? super K, ? super V> transformer =
5966 <                this.transformer;
5967 <            final IntByIntToInt reducer = this.reducer;
5968 <            if (transformer == null || reducer == null)
5969 <                throw new Error(NullFunctionMessage);
5970 <            final int id = this.basis;
5971 <            int b = batch();
5972 <            while (b > 1 && t.baseIndex != t.baseLimit) {
5973 <                b >>>= 1;
5974 <                t.pending = 1;
5975 <                MapReduceMappingsToIntTask<K,V> rt =
5976 <                    new MapReduceMappingsToIntTask<K,V>
5977 <                    (t, b, true, transformer, id, reducer);
5978 <                t = new MapReduceMappingsToIntTask<K,V>
5979 <                    (t, b, false, transformer, id, reducer);
5980 <                t.sibling = rt;
5981 <                rt.sibling = t;
5982 <                rt.fork();
5983 <            }
5984 <            int r = id;
5985 <            Object v;
5986 <            while ((v = t.advance()) != null)
5987 <                r = reducer.apply(r, transformer.apply((K)t.nextKey, (V)v));
5988 <            t.result = r;
5989 <            for (;;) {
5990 <                int c; BulkTask<K,V,?> par; MapReduceMappingsToIntTask<K,V> s, p;
5991 <                if ((par = t.parent) == null ||
5992 <                    !(par instanceof MapReduceMappingsToIntTask)) {
5993 <                    t.quietlyComplete();
5963 >    }
5964 >
5965 >    /* ---------------- Counters -------------- */
5966 >
5967 >    // Adapted from LongAdder and Striped64.
5968 >    // See their internal docs for explanation.
5969 >
5970 >    // A padded cell for distributing counts
5971 >    static final class CounterCell {
5972 >        volatile long p0, p1, p2, p3, p4, p5, p6;
5973 >        volatile long value;
5974 >        volatile long q0, q1, q2, q3, q4, q5, q6;
5975 >        CounterCell(long x) { value = x; }
5976 >    }
5977 >
5978 >    /**
5979 >     * Holder for the thread-local hash code determining which
5980 >     * CounterCell to use. The code is initialized via the
5981 >     * counterHashCodeGenerator, but may be moved upon collisions.
5982 >     */
5983 >    static final class CounterHashCode {
5984 >        int code;
5985 >    }
5986 >
5987 >    /**
5988 >     * Generates initial value for per-thread CounterHashCodes
5989 >     */
5990 >    static final AtomicInteger counterHashCodeGenerator = new AtomicInteger();
5991 >
5992 >    /**
5993 >     * Increment for counterHashCodeGenerator. See class ThreadLocal
5994 >     * for explanation.
5995 >     */
5996 >    static final int SEED_INCREMENT = 0x61c88647;
5997 >
5998 >    /**
5999 >     * Per-thread counter hash codes. Shared across all instances.
6000 >     */
6001 >    static final ThreadLocal<CounterHashCode> threadCounterHashCode =
6002 >        new ThreadLocal<CounterHashCode>();
6003 >
6004 >
6005 >    final long sumCount() {
6006 >        CounterCell[] as = counterCells; CounterCell a;
6007 >        long sum = baseCount;
6008 >        if (as != null) {
6009 >            for (int i = 0; i < as.length; ++i) {
6010 >                if ((a = as[i]) != null)
6011 >                    sum += a.value;
6012 >            }
6013 >        }
6014 >        return sum;
6015 >    }
6016 >
6017 >    // See LongAdder version for explanation
6018 >    private final void fullAddCount(long x, CounterHashCode hc,
6019 >                                    boolean wasUncontended) {
6020 >        int h;
6021 >        if (hc == null) {
6022 >            hc = new CounterHashCode();
6023 >            int s = counterHashCodeGenerator.addAndGet(SEED_INCREMENT);
6024 >            h = hc.code = (s == 0) ? 1 : s; // Avoid zero
6025 >            threadCounterHashCode.set(hc);
6026 >        }
6027 >        else
6028 >            h = hc.code;
6029 >        boolean collide = false;                // True if last slot nonempty
6030 >        for (;;) {
6031 >            CounterCell[] as; CounterCell a; int n; long v;
6032 >            if ((as = counterCells) != null && (n = as.length) > 0) {
6033 >                if ((a = as[(n - 1) & h]) == null) {
6034 >                    if (cellsBusy == 0) {            // Try to attach new Cell
6035 >                        CounterCell r = new CounterCell(x); // Optimistic create
6036 >                        if (cellsBusy == 0 &&
6037 >                            U.compareAndSwapInt(this, CELLSBUSY, 0, 1)) {
6038 >                            boolean created = false;
6039 >                            try {               // Recheck under lock
6040 >                                CounterCell[] rs; int m, j;
6041 >                                if ((rs = counterCells) != null &&
6042 >                                    (m = rs.length) > 0 &&
6043 >                                    rs[j = (m - 1) & h] == null) {
6044 >                                    rs[j] = r;
6045 >                                    created = true;
6046 >                                }
6047 >                            } finally {
6048 >                                cellsBusy = 0;
6049 >                            }
6050 >                            if (created)
6051 >                                break;
6052 >                            continue;           // Slot is now non-empty
6053 >                        }
6054 >                    }
6055 >                    collide = false;
6056 >                }
6057 >                else if (!wasUncontended)       // CAS already known to fail
6058 >                    wasUncontended = true;      // Continue after rehash
6059 >                else if (U.compareAndSwapLong(a, CELLVALUE, v = a.value, v + x))
6060                      break;
6061 +                else if (counterCells != as || n >= NCPU)
6062 +                    collide = false;            // At max size or stale
6063 +                else if (!collide)
6064 +                    collide = true;
6065 +                else if (cellsBusy == 0 &&
6066 +                         U.compareAndSwapInt(this, CELLSBUSY, 0, 1)) {
6067 +                    try {
6068 +                        if (counterCells == as) {// Expand table unless stale
6069 +                            CounterCell[] rs = new CounterCell[n << 1];
6070 +                            for (int i = 0; i < n; ++i)
6071 +                                rs[i] = as[i];
6072 +                            counterCells = rs;
6073 +                        }
6074 +                    } finally {
6075 +                        cellsBusy = 0;
6076 +                    }
6077 +                    collide = false;
6078 +                    continue;                   // Retry with expanded table
6079                  }
6080 <                else if ((c = (p = (MapReduceMappingsToIntTask<K,V>)par).pending) == 0) {
6081 <                    if ((s = t.sibling) != null)
6082 <                        r = reducer.apply(r, s.result);
6083 <                    (t = p).result = r;
6080 >                h ^= h << 13;                   // Rehash
6081 >                h ^= h >>> 17;
6082 >                h ^= h << 5;
6083 >            }
6084 >            else if (cellsBusy == 0 && counterCells == as &&
6085 >                     U.compareAndSwapInt(this, CELLSBUSY, 0, 1)) {
6086 >                boolean init = false;
6087 >                try {                           // Initialize table
6088 >                    if (counterCells == as) {
6089 >                        CounterCell[] rs = new CounterCell[2];
6090 >                        rs[h & 1] = new CounterCell(x);
6091 >                        counterCells = rs;
6092 >                        init = true;
6093 >                    }
6094 >                } finally {
6095 >                    cellsBusy = 0;
6096                  }
6097 <                else if (p.casPending(c, 0))
6097 >                if (init)
6098                      break;
6099              }
6100 +            else if (U.compareAndSwapLong(this, BASECOUNT, v = baseCount, v + x))
6101 +                break;                          // Fall back on using base
6102          }
6103 <        public final Integer getRawResult() { return result; }
6103 >        hc.code = h;                            // Record index for next time
6104      }
6105  
6778
6106      // Unsafe mechanics
6107 <    private static final sun.misc.Unsafe UNSAFE;
6108 <    private static final long counterOffset;
6109 <    private static final long sizeCtlOffset;
6107 >    private static final sun.misc.Unsafe U;
6108 >    private static final long SIZECTL;
6109 >    private static final long TRANSFERINDEX;
6110 >    private static final long TRANSFERORIGIN;
6111 >    private static final long BASECOUNT;
6112 >    private static final long CELLSBUSY;
6113 >    private static final long CELLVALUE;
6114      private static final long ABASE;
6115      private static final int ASHIFT;
6116  
6117      static {
6787        int ss;
6118          try {
6119 <            UNSAFE = getUnsafe();
6119 >            U = getUnsafe();
6120              Class<?> k = ConcurrentHashMapV8.class;
6121 <            counterOffset = UNSAFE.objectFieldOffset
6792 <                (k.getDeclaredField("counter"));
6793 <            sizeCtlOffset = UNSAFE.objectFieldOffset
6121 >            SIZECTL = U.objectFieldOffset
6122                  (k.getDeclaredField("sizeCtl"));
6123 <            Class<?> sc = Node[].class;
6124 <            ABASE = UNSAFE.arrayBaseOffset(sc);
6125 <            ss = UNSAFE.arrayIndexScale(sc);
6123 >            TRANSFERINDEX = U.objectFieldOffset
6124 >                (k.getDeclaredField("transferIndex"));
6125 >            TRANSFERORIGIN = U.objectFieldOffset
6126 >                (k.getDeclaredField("transferOrigin"));
6127 >            BASECOUNT = U.objectFieldOffset
6128 >                (k.getDeclaredField("baseCount"));
6129 >            CELLSBUSY = U.objectFieldOffset
6130 >                (k.getDeclaredField("cellsBusy"));
6131 >            Class<?> ck = CounterCell.class;
6132 >            CELLVALUE = U.objectFieldOffset
6133 >                (ck.getDeclaredField("value"));
6134 >            Class<?> ak = Node[].class;
6135 >            ABASE = U.arrayBaseOffset(ak);
6136 >            int scale = U.arrayIndexScale(ak);
6137 >            if ((scale & (scale - 1)) != 0)
6138 >                throw new Error("data type scale not a power of two");
6139 >            ASHIFT = 31 - Integer.numberOfLeadingZeros(scale);
6140          } catch (Exception e) {
6141              throw new Error(e);
6142          }
6801        if ((ss & (ss-1)) != 0)
6802            throw new Error("data type scale not a power of two");
6803        ASHIFT = 31 - Integer.numberOfLeadingZeros(ss);
6143      }
6144  
6145      /**
# Line 6813 | Line 6152 | public class ConcurrentHashMapV8<K, V>
6152      private static sun.misc.Unsafe getUnsafe() {
6153          try {
6154              return sun.misc.Unsafe.getUnsafe();
6155 <        } catch (SecurityException se) {
6156 <            try {
6157 <                return java.security.AccessController.doPrivileged
6158 <                    (new java.security
6159 <                     .PrivilegedExceptionAction<sun.misc.Unsafe>() {
6160 <                        public sun.misc.Unsafe run() throws Exception {
6161 <                            java.lang.reflect.Field f = sun.misc
6162 <                                .Unsafe.class.getDeclaredField("theUnsafe");
6163 <                            f.setAccessible(true);
6164 <                            return (sun.misc.Unsafe) f.get(null);
6165 <                        }});
6166 <            } catch (java.security.PrivilegedActionException e) {
6167 <                throw new RuntimeException("Could not initialize intrinsics",
6168 <                                           e.getCause());
6169 <            }
6155 >        } catch (SecurityException tryReflectionInstead) {}
6156 >        try {
6157 >            return java.security.AccessController.doPrivileged
6158 >            (new java.security.PrivilegedExceptionAction<sun.misc.Unsafe>() {
6159 >                public sun.misc.Unsafe run() throws Exception {
6160 >                    Class<sun.misc.Unsafe> k = sun.misc.Unsafe.class;
6161 >                    for (java.lang.reflect.Field f : k.getDeclaredFields()) {
6162 >                        f.setAccessible(true);
6163 >                        Object x = f.get(null);
6164 >                        if (k.isInstance(x))
6165 >                            return k.cast(x);
6166 >                    }
6167 >                    throw new NoSuchFieldError("the Unsafe");
6168 >                }});
6169 >        } catch (java.security.PrivilegedActionException e) {
6170 >            throw new RuntimeException("Could not initialize intrinsics",
6171 >                                       e.getCause());
6172          }
6173      }
6174   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines