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.61 by dl, Thu Sep 13 10:41:37 2012 UTC vs.
Revision 1.110 by dl, Thu Jul 4 18:34:49 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
117 <     * portion of the elements, and so may be amenable to parallel
118 <     * execution.
119 <     *
120 <     * <p> This interface exports a subset of expected JDK8
121 <     * functionality.
122 <     *
123 <     * <p>Sample usage: Here is one (of the several) ways to compute
124 <     * the sum of the values held in a map using the ForkJoin
125 <     * framework. As illustrated here, Spliterators are well suited to
126 <     * designs in which a task repeatedly splits off half its work
127 <     * into forked subtasks until small enough to process directly,
128 <     * and then joins these subtasks. Variants of this style can also
129 <     * be used in completion-based designs.
130 <     *
131 <     * <pre>
132 <     * {@code ConcurrentHashMapV8<String, Long> m = ...
133 <     * // split as if have 8 * parallelism, for load balance
134 <     * int n = m.size();
135 <     * int p = aForkJoinPool.getParallelism() * 8;
136 <     * int split = (n < p)? n : p;
137 <     * long sum = aForkJoinPool.invoke(new SumValues(m.valueSpliterator(), split, null));
138 <     * // ...
139 <     * static class SumValues extends RecursiveTask<Long> {
140 <     *   final Spliterator<Long> s;
141 <     *   final int split;             // split while > 1
142 <     *   final SumValues nextJoin;    // records forked subtasks to join
143 <     *   SumValues(Spliterator<Long> s, int depth, SumValues nextJoin) {
144 <     *     this.s = s; this.depth = depth; this.nextJoin = nextJoin;
145 <     *   }
146 <     *   public Long compute() {
147 <     *     long sum = 0;
148 <     *     SumValues subtasks = null; // fork subtasks
149 <     *     for (int s = split >>> 1; s > 0; s >>>= 1)
150 <     *       (subtasks = new SumValues(s.split(), s, subtasks)).fork();
151 <     *     while (s.hasNext())        // directly process remaining elements
152 <     *       sum += s.next();
153 <     *     for (SumValues t = subtasks; t != null; t = t.nextJoin)
154 <     *       sum += t.join();         // collect subtask results
155 <     *     return sum;
156 <     *   }
157 <     * }
158 <     * }</pre>
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 Spliterator<T> extends Iterator<T> {
230 >    public static interface ConcurrentHashMapSpliterator<T> {
231          /**
232 <         * Returns a Spliterator covering approximately half of the
233 <         * elements, guaranteed not to overlap with those subsequently
234 <         * returned by this Spliterator.  After invoking this method,
235 <         * the current Spliterator will <em>not</em> produce any of
236 <         * the elements of the returned Spliterator, but the two
237 <         * Spliterators together will produce all of the elements that
238 <         * would have been produced by this Spliterator had this
239 <         * method not been called. The exact number of elements
240 <         * 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
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 <        Spliterator<T> split();
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 >     * insignificant.)
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 because 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     = -1; // hash for forwarding nodes
572 >    static final int TREEBIN   = -2; // hash for roots of trees
573 >    static final int RESERVED  = -3; // 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 negative hash 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 >        volatile 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 <         * The initial sanity check on tab and bounds is not currently
545 <         * necessary in the only usages of this method, but enables
546 <         * use in other future contexts.
547 <         */
548 <        final void tryAwaitLock(Node[] tab, int i) {
549 <            if (tab != null && i >= 0 && i < tab.length) { // sanity check
550 <                int r = ThreadLocalRandom.current().nextInt(); // randomize spins
551 <                int spins = MAX_SPINS, h;
552 <                while (tabAt(tab, i) == this && ((h = hash) & LOCKED) != 0) {
553 <                    if (spins >= 0) {
554 <                        r ^= r << 1; r ^= r >>> 3; r ^= r << 10; // xorshift
555 <                        if (r >= 0 && --spins == 0)
556 <                            Thread.yield();  // yield before block
557 <                    }
558 <                    else if (casHash(h, h | WAITING)) {
559 <                        synchronized (this) {
560 <                            if (tabAt(tab, i) == this &&
561 <                                (hash & WAITING) == WAITING) {
562 <                                try {
563 <                                    wait();
564 <                                } catch (InterruptedException ie) {
565 <                                    Thread.currentThread().interrupt();
566 <                                }
567 <                            }
568 <                            else
569 <                                notifyAll(); // possibly won race vs signaller
570 <                        }
571 <                        break;
572 <                    }
573 <                }
574 <            }
575 <        }
576 <
577 <        // Unsafe mechanics for casHash
578 <        private static final sun.misc.Unsafe UNSAFE;
579 <        private static final long hashOffset;
580 <
581 <        static {
582 <            try {
583 <                UNSAFE = getUnsafe();
584 <                Class<?> k = Node.class;
585 <                hashOffset = UNSAFE.objectFieldOffset
586 <                    (k.getDeclaredField("hash"));
587 <            } catch (Exception e) {
588 <                throw new Error(e);
589 <            }
590 <        }
591 <    }
592 <
593 <    /* ---------------- TreeBins -------------- */
594 <
595 <    /**
596 <     * Nodes for use in TreeBins
597 <     */
598 <    static final class TreeNode extends Node {
599 <        TreeNode parent;  // red-black tree links
600 <        TreeNode left;
601 <        TreeNode right;
602 <        TreeNode prev;    // needed to unlink next upon deletion
603 <        boolean red;
604 <
605 <        TreeNode(int hash, Object key, Object val, Node next, TreeNode parent) {
606 <            super(hash, key, val, next);
607 <            this.parent = parent;
608 <        }
609 <    }
610 <
611 <    /**
612 <     * A specialized form of red-black tree for use in bins
613 <     * whose size exceeds a threshold.
614 <     *
615 <     * TreeBins use a special form of comparison for search and
616 <     * related operations (which is the main reason we cannot use
617 <     * existing collections such as TreeMaps). TreeBins contain
618 <     * Comparable elements, but may contain others, as well as
619 <     * elements that are Comparable but not necessarily Comparable<T>
620 <     * for the same T, so we cannot invoke compareTo among them. To
621 <     * handle this, the tree is ordered primarily by hash value, then
622 <     * by getClass().getName() order, and then by Comparator order
623 <     * among elements of the same class.  On lookup at a node, if
624 <     * elements are not comparable or compare as 0, both left and
625 <     * right children may need to be searched in the case of tied hash
626 <     * values. (This corresponds to the full list search that would be
627 <     * necessary if all elements were non-Comparable and had tied
628 <     * hashes.)  The red-black balancing code is updated from
629 <     * pre-jdk-collections
630 <     * (http://gee.cs.oswego.edu/dl/classes/collections/RBCell.java)
631 <     * based in turn on Cormen, Leiserson, and Rivest "Introduction to
632 <     * Algorithms" (CLR).
633 <     *
634 <     * TreeBins also maintain a separate locking discipline than
635 <     * regular bins. Because they are forwarded via special MOVED
636 <     * nodes at bin heads (which can never change once established),
637 <     * we cannot use those nodes as locks. Instead, TreeBin
638 <     * extends AbstractQueuedSynchronizer to support a simple form of
639 <     * read-write lock. For update operations and table validation,
640 <     * the exclusive form of lock behaves in the same way as bin-head
641 <     * locks. However, lookups use shared read-lock mechanics to allow
642 <     * multiple readers in the absence of writers.  Additionally,
643 <     * these lookups do not ever block: While the lock is not
644 <     * available, they proceed along the slow traversal path (via
645 <     * next-pointers) until the lock becomes available or the list is
646 <     * exhausted, whichever comes first. (These cases are not fast,
647 <     * but maximize aggregate expected throughput.)  The AQS mechanics
648 <     * for doing this are straightforward.  The lock state is held as
649 <     * AQS getState().  Read counts are negative; the write count (1)
650 <     * is positive.  There are no signalling preferences among readers
651 <     * and writers. Since we don't need to export full Lock API, we
652 <     * just override the minimal AQS methods and use them directly.
653 <     */
654 <    static final class TreeBin extends AbstractQueuedSynchronizer {
655 <        private static final long serialVersionUID = 2249069246763182397L;
656 <        transient TreeNode root;  // root of tree
657 <        transient TreeNode first; // head of next-pointer list
658 <
659 <        /* AQS overrides */
660 <        public final boolean isHeldExclusively() { return getState() > 0; }
661 <        public final boolean tryAcquire(int ignore) {
662 <            if (compareAndSetState(0, 1)) {
663 <                setExclusiveOwnerThread(Thread.currentThread());
664 <                return true;
665 <            }
666 <            return false;
667 <        }
668 <        public final boolean tryRelease(int ignore) {
669 <            setExclusiveOwnerThread(null);
670 <            setState(0);
671 <            return true;
672 <        }
673 <        public final int tryAcquireShared(int ignore) {
674 <            for (int c;;) {
675 <                if ((c = getState()) > 0)
676 <                    return -1;
677 <                if (compareAndSetState(c, c -1))
678 <                    return 1;
679 <            }
680 <        }
681 <        public final boolean tryReleaseShared(int ignore) {
682 <            int c;
683 <            do {} while (!compareAndSetState(c = getState(), c + 1));
684 <            return c == -1;
685 <        }
686 <
687 <        /** From CLR */
688 <        private void rotateLeft(TreeNode p) {
689 <            if (p != null) {
690 <                TreeNode r = p.right, pp, rl;
691 <                if ((rl = p.right = r.left) != null)
692 <                    rl.parent = p;
693 <                if ((pp = r.parent = p.parent) == null)
694 <                    root = r;
695 <                else if (pp.left == p)
696 <                    pp.left = r;
697 <                else
698 <                    pp.right = r;
699 <                r.left = p;
700 <                p.parent = r;
701 <            }
702 <        }
703 <
704 <        /** From CLR */
705 <        private void rotateRight(TreeNode p) {
706 <            if (p != null) {
707 <                TreeNode l = p.left, pp, lr;
708 <                if ((lr = p.left = l.right) != null)
709 <                    lr.parent = p;
710 <                if ((pp = l.parent = p.parent) == null)
711 <                    root = l;
712 <                else if (pp.right == p)
713 <                    pp.right = l;
714 <                else
715 <                    pp.left = l;
716 <                l.right = p;
717 <                p.parent = l;
718 <            }
719 <        }
720 <
721 <        /**
722 <         * Returns the TreeNode (or null if not found) for the given key
723 <         * starting at given root.
724 <         */
725 <        @SuppressWarnings("unchecked") final TreeNode getTreeNode
726 <            (int h, Object k, TreeNode p) {
727 <            Class<?> c = k.getClass();
728 <            while (p != null) {
729 <                int dir, ph;  Object pk; Class<?> pc;
730 <                if ((ph = p.hash) == h) {
731 <                    if ((pk = p.key) == k || k.equals(pk))
732 <                        return p;
733 <                    if (c != (pc = pk.getClass()) ||
734 <                        !(k instanceof Comparable) ||
735 <                        (dir = ((Comparable)k).compareTo((Comparable)pk)) == 0) {
736 <                        dir = (c == pc) ? 0 : c.getName().compareTo(pc.getName());
737 <                        TreeNode r = null, s = null, pl, pr;
738 <                        if (dir >= 0) {
739 <                            if ((pl = p.left) != null && h <= pl.hash)
740 <                                s = pl;
741 <                        }
742 <                        else if ((pr = p.right) != null && h >= pr.hash)
743 <                            s = pr;
744 <                        if (s != null && (r = getTreeNode(h, k, s)) != null)
745 <                            return r;
746 <                    }
747 <                }
748 <                else
749 <                    dir = (h < ph) ? -1 : 1;
750 <                p = (dir > 0) ? p.right : p.left;
751 <            }
752 <            return null;
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;
762 <            int c = getState(); // Must read lock state first
763 <            for (Node e = first; e != null; e = e.next) {
764 <                if (c <= 0 && compareAndSetState(c, c - 1)) {
765 <                    try {
766 <                        r = getTreeNode(h, k, root);
767 <                    } finally {
768 <                        releaseShared(0);
769 <                    }
770 <                    break;
771 <                }
772 <                else if ((e.hash & HASH_BITS) == h && k.equals(e.key)) {
773 <                    r = e;
774 <                    break;
775 <                }
776 <                else
777 <                    c = getState();
778 <            }
779 <            return r == null ? null : r.val;
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.
784 <         * @return null if added
627 >         * Virtualized support for map.get(); overridden in subclasses.
628           */
629 <        @SuppressWarnings("unchecked") final TreeNode putTreeNode
630 <            (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) {
795 <                    if ((pk = p.key) == k || k.equals(pk))
796 <                        return p;
797 <                    if (c != (pc = pk.getClass()) ||
798 <                        !(k instanceof Comparable) ||
799 <                        (dir = ((Comparable)k).compareTo((Comparable)pk)) == 0) {
800 <                        dir = (c == pc) ? 0 : c.getName().compareTo(pc.getName());
801 <                        TreeNode r = null, s = null, pl, pr;
802 <                        if (dir >= 0) {
803 <                            if ((pl = p.left) != null && h <= pl.hash)
804 <                                s = pl;
805 <                        }
806 <                        else if ((pr = p.right) != null && h >= pr.hash)
807 <                            s = pr;
808 <                        if (s != null && (r = getTreeNode(h, k, s)) != null)
809 <                            return r;
810 <                    }
811 <                }
812 <                else
813 <                    dir = (h < ph) ? -1 : 1;
814 <                pp = (dir > 0) ? p.right : p.left;
815 <            }
816 <
817 <            TreeNode f = first;
818 <            TreeNode x = first = new TreeNode(h, k, v, f, p);
819 <            if (p == null)
820 <                root = x;
821 <            else { // attach and rebalance; adapted from CLR
822 <                TreeNode xp, xpp;
823 <                if (f != null)
824 <                    f.prev = x;
825 <                if (dir <= 0)
826 <                    p.left = x;
827 <                else
828 <                    p.right = x;
829 <                x.red = true;
830 <                while (x != null && (xp = x.parent) != null && xp.red &&
831 <                       (xpp = xp.parent) != null) {
832 <                    TreeNode xppl = xpp.left;
833 <                    if (xp == xppl) {
834 <                        TreeNode y = xpp.right;
835 <                        if (y != null && y.red) {
836 <                            y.red = false;
837 <                            xp.red = false;
838 <                            xpp.red = true;
839 <                            x = xpp;
840 <                        }
841 <                        else {
842 <                            if (x == xp.right) {
843 <                                rotateLeft(x = xp);
844 <                                xpp = (xp = x.parent) == null ? null : xp.parent;
845 <                            }
846 <                            if (xp != null) {
847 <                                xp.red = false;
848 <                                if (xpp != null) {
849 <                                    xpp.red = true;
850 <                                    rotateRight(xpp);
851 <                                }
852 <                            }
853 <                        }
854 <                    }
855 <                    else {
856 <                        TreeNode y = xppl;
857 <                        if (y != null && y.red) {
858 <                            y.red = false;
859 <                            xp.red = false;
860 <                            xpp.red = true;
861 <                            x = xpp;
862 <                        }
863 <                        else {
864 <                            if (x == xp.left) {
865 <                                rotateRight(x = xp);
866 <                                xpp = (xp = x.parent) == null ? null : xp.parent;
867 <                            }
868 <                            if (xp != null) {
869 <                                xp.red = false;
870 <                                if (xpp != null) {
871 <                                    xpp.red = true;
872 <                                    rotateLeft(xpp);
873 <                                }
874 <                            }
875 <                        }
876 <                    }
877 <                }
878 <                TreeNode r = root;
879 <                if (r != null && r.red)
880 <                    r.red = false;
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          }
884
885        /**
886         * Removes the given node, that must be present before this
887         * call.  This is messier than typical red-black deletion code
888         * because we cannot swap the contents of an interior node
889         * with a leaf successor that is pinned by "next" pointers
890         * that are accessible independently of lock. So instead we
891         * swap the tree linkages.
892         */
893        final void deleteTreeNode(TreeNode p) {
894            TreeNode next = (TreeNode)p.next; // unlink traversal pointers
895            TreeNode pred = p.prev;
896            if (pred == null)
897                first = next;
898            else
899                pred.next = next;
900            if (next != null)
901                next.prev = pred;
902            TreeNode replacement;
903            TreeNode pl = p.left;
904            TreeNode pr = p.right;
905            if (pl != null && pr != null) {
906                TreeNode s = pr, sl;
907                while ((sl = s.left) != null) // find successor
908                    s = sl;
909                boolean c = s.red; s.red = p.red; p.red = c; // swap colors
910                TreeNode sr = s.right;
911                TreeNode pp = p.parent;
912                if (s == pr) { // p was s's direct parent
913                    p.parent = s;
914                    s.right = p;
915                }
916                else {
917                    TreeNode sp = s.parent;
918                    if ((p.parent = sp) != null) {
919                        if (s == sp.left)
920                            sp.left = p;
921                        else
922                            sp.right = p;
923                    }
924                    if ((s.right = pr) != null)
925                        pr.parent = s;
926                }
927                p.left = null;
928                if ((p.right = sr) != null)
929                    sr.parent = p;
930                if ((s.left = pl) != null)
931                    pl.parent = s;
932                if ((s.parent = pp) == null)
933                    root = s;
934                else if (p == pp.left)
935                    pp.left = s;
936                else
937                    pp.right = s;
938                replacement = sr;
939            }
940            else
941                replacement = (pl != null) ? pl : pr;
942            TreeNode pp = p.parent;
943            if (replacement == null) {
944                if (pp == null) {
945                    root = null;
946                    return;
947                }
948                replacement = p;
949            }
950            else {
951                replacement.parent = pp;
952                if (pp == null)
953                    root = replacement;
954                else if (p == pp.left)
955                    pp.left = replacement;
956                else
957                    pp.right = replacement;
958                p.left = p.right = p.parent = null;
959            }
960            if (!p.red) { // rebalance, from CLR
961                TreeNode x = replacement;
962                while (x != null) {
963                    TreeNode xp, xpl;
964                    if (x.red || (xp = x.parent) == null) {
965                        x.red = false;
966                        break;
967                    }
968                    if (x == (xpl = xp.left)) {
969                        TreeNode sib = xp.right;
970                        if (sib != null && sib.red) {
971                            sib.red = false;
972                            xp.red = true;
973                            rotateLeft(xp);
974                            sib = (xp = x.parent) == null ? null : xp.right;
975                        }
976                        if (sib == null)
977                            x = xp;
978                        else {
979                            TreeNode sl = sib.left, sr = sib.right;
980                            if ((sr == null || !sr.red) &&
981                                (sl == null || !sl.red)) {
982                                sib.red = true;
983                                x = xp;
984                            }
985                            else {
986                                if (sr == null || !sr.red) {
987                                    if (sl != null)
988                                        sl.red = false;
989                                    sib.red = true;
990                                    rotateRight(sib);
991                                    sib = (xp = x.parent) == null ? null : xp.right;
992                                }
993                                if (sib != null) {
994                                    sib.red = (xp == null) ? false : xp.red;
995                                    if ((sr = sib.right) != null)
996                                        sr.red = false;
997                                }
998                                if (xp != null) {
999                                    xp.red = false;
1000                                    rotateLeft(xp);
1001                                }
1002                                x = root;
1003                            }
1004                        }
1005                    }
1006                    else { // symmetric
1007                        TreeNode sib = xpl;
1008                        if (sib != null && sib.red) {
1009                            sib.red = false;
1010                            xp.red = true;
1011                            rotateRight(xp);
1012                            sib = (xp = x.parent) == null ? null : xp.left;
1013                        }
1014                        if (sib == null)
1015                            x = xp;
1016                        else {
1017                            TreeNode sl = sib.left, sr = sib.right;
1018                            if ((sl == null || !sl.red) &&
1019                                (sr == null || !sr.red)) {
1020                                sib.red = true;
1021                                x = xp;
1022                            }
1023                            else {
1024                                if (sl == null || !sl.red) {
1025                                    if (sr != null)
1026                                        sr.red = false;
1027                                    sib.red = true;
1028                                    rotateLeft(sib);
1029                                    sib = (xp = x.parent) == null ? null : xp.left;
1030                                }
1031                                if (sib != null) {
1032                                    sib.red = (xp == null) ? false : xp.red;
1033                                    if ((sl = sib.left) != null)
1034                                        sl.red = false;
1035                                }
1036                                if (xp != null) {
1037                                    xp.red = false;
1038                                    rotateRight(xp);
1039                                }
1040                                x = root;
1041                            }
1042                        }
1043                    }
1044                }
1045            }
1046            if (p == replacement && (pp = p.parent) != null) {
1047                if (p == pp.left) // detach pointers
1048                    pp.left = null;
1049                else if (p == pp.right)
1050                    pp.right = null;
1051                p.parent = null;
1052            }
1053        }
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);
1073 <        return (h ^ (h >>> 10)) & HASH_BITS;
1074 <    }
1075 <
1076 <    /**
1077 <     * Replaces a list bin with a tree bin. Call only when locked.
1078 <     * Fails to replace if the given key is non-comparable or table
1079 <     * is, or needs, resizing.
1080 <     */
1081 <    private final void replaceWithTreeBin(Node[] tab, int index, Object key) {
1082 <        if ((key instanceof Comparable) &&
1083 <            (tab.length >= MAXIMUM_CAPACITY || counter.sum() < (long)sizeCtl)) {
1084 <            TreeBin t = new TreeBin();
1085 <            for (Node e = tabAt(tab, index); e != null; e = e.next)
1086 <                t.putTreeNode(e.hash & HASH_BITS, e.key, e.val);
1087 <            setTabAt(tab, index, new Node(MOVED, t, null, null));
1088 <        }
1089 <    }
1090 <
1091 <    /* ---------------- Internal access and update methods -------------- */
1092 <
1093 <    /** Implementation for get and containsKey */
1094 <    private final Object internalGet(Object k) {
1095 <        int h = spread(k.hashCode());
1096 <        retry: for (Node[] tab = table; tab != null;) {
1097 <            Node e, p; Object ek, ev; int eh;      // locals to read fields once
1098 <            for (e = tabAt(tab, (tab.length - 1) & h); e != null; e = e.next) {
1099 <                if ((eh = e.hash) == MOVED) {
1100 <                    if ((ek = e.key) instanceof TreeBin)  // search TreeBin
1101 <                        return ((TreeBin)ek).getValue(h, k);
1102 <                    else {                        // restart with new table
1103 <                        tab = (Node[])ek;
1104 <                        continue retry;
1105 <                    }
1106 <                }
1107 <                else if ((eh & HASH_BITS) == h && (ev = e.val) != null &&
1108 <                         ((ek = e.key) == k || k.equals(ek)))
1109 <                    return ev;
1110 <            }
1111 <            break;
1112 <        }
1113 <        return null;
1114 <    }
1115 <
1116 <    /**
1117 <     * Implementation for the four public remove/replace methods:
1118 <     * Replaces node value with v, conditional upon match of cv if
1119 <     * non-null.  If resulting value is null, delete.
1120 <     */
1121 <    private final Object internalReplace(Object k, Object v, Object cv) {
1122 <        int h = spread(k.hashCode());
1123 <        Object oldVal = null;
1124 <        for (Node[] tab = table;;) {
1125 <            Node f; int i, fh; Object fk;
1126 <            if (tab == null ||
1127 <                (f = tabAt(tab, i = (tab.length - 1) & h)) == null)
1128 <                break;
1129 <            else if ((fh = f.hash) == MOVED) {
1130 <                if ((fk = f.key) instanceof TreeBin) {
1131 <                    TreeBin t = (TreeBin)fk;
1132 <                    boolean validated = false;
1133 <                    boolean deleted = false;
1134 <                    t.acquire(0);
1135 <                    try {
1136 <                        if (tabAt(tab, i) == f) {
1137 <                            validated = true;
1138 <                            TreeNode p = t.getTreeNode(h, k, t.root);
1139 <                            if (p != null) {
1140 <                                Object pv = p.val;
1141 <                                if (cv == null || cv == pv || cv.equals(pv)) {
1142 <                                    oldVal = pv;
1143 <                                    if ((p.val = v) == null) {
1144 <                                        deleted = true;
1145 <                                        t.deleteTreeNode(p);
1146 <                                    }
1147 <                                }
1148 <                            }
1149 <                        }
1150 <                    } finally {
1151 <                        t.release(0);
1152 <                    }
1153 <                    if (validated) {
1154 <                        if (deleted)
1155 <                            counter.add(-1L);
1156 <                        break;
1157 <                    }
1158 <                }
1159 <                else
1160 <                    tab = (Node[])fk;
1161 <            }
1162 <            else if ((fh & HASH_BITS) != h && f.next == null) // precheck
1163 <                break;                          // rules out possible existence
1164 <            else if ((fh & LOCKED) != 0) {
1165 <                checkForResize();               // try resizing if can't get lock
1166 <                f.tryAwaitLock(tab, i);
1167 <            }
1168 <            else if (f.casHash(fh, fh | LOCKED)) {
1169 <                boolean validated = false;
1170 <                boolean deleted = false;
1171 <                try {
1172 <                    if (tabAt(tab, i) == f) {
1173 <                        validated = true;
1174 <                        for (Node e = f, pred = null;;) {
1175 <                            Object ek, ev;
1176 <                            if ((e.hash & HASH_BITS) == h &&
1177 <                                ((ev = e.val) != null) &&
1178 <                                ((ek = e.key) == k || k.equals(ek))) {
1179 <                                if (cv == null || cv == ev || cv.equals(ev)) {
1180 <                                    oldVal = ev;
1181 <                                    if ((e.val = v) == null) {
1182 <                                        deleted = true;
1183 <                                        Node en = e.next;
1184 <                                        if (pred != null)
1185 <                                            pred.next = en;
1186 <                                        else
1187 <                                            setTabAt(tab, i, en);
1188 <                                    }
1189 <                                }
1190 <                                break;
1191 <                            }
1192 <                            pred = e;
1193 <                            if ((e = e.next) == null)
1194 <                                break;
1195 <                        }
1196 <                    }
1197 <                } finally {
1198 <                    if (!f.casHash(fh | LOCKED, fh)) {
1199 <                        f.hash = fh;
1200 <                        synchronized (f) { f.notifyAll(); };
1201 <                    }
1202 <                }
1203 <                if (validated) {
1204 <                    if (deleted)
1205 <                        counter.add(-1L);
1206 <                    break;
1207 <                }
1208 <            }
1209 <        }
1210 <        return oldVal;
1211 <    }
1212 <
1213 <    /*
1214 <     * Internal versions of the six insertion methods, each a
1215 <     * little more complicated than the last. All have
1216 <     * the same basic structure as the first (internalPut):
1217 <     *  1. If table uninitialized, create
1218 <     *  2. If bin empty, try to CAS new node
1219 <     *  3. If bin stale, use new table
1220 <     *  4. if bin converted to TreeBin, validate and relay to TreeBin methods
1221 <     *  5. Lock and validate; if valid, scan and add or update
1222 <     *
1223 <     * The others interweave other checks and/or alternative actions:
1224 <     *  * Plain put checks for and performs resize after insertion.
1225 <     *  * putIfAbsent prescans for mapping without lock (and fails to add
1226 <     *    if present), which also makes pre-emptive resize checks worthwhile.
1227 <     *  * computeIfAbsent extends form used in putIfAbsent with additional
1228 <     *    mechanics to deal with, calls, potential exceptions and null
1229 <     *    returns from function call.
1230 <     *  * compute uses the same function-call mechanics, but without
1231 <     *    the prescans
1232 <     *  * merge acts as putIfAbsent in the absent case, but invokes the
1233 <     *    update function if present
1234 <     *  * putAll attempts to pre-allocate enough table space
1235 <     *    and more lazily performs count updates and checks.
1236 <     *
1237 <     * Someday when details settle down a bit more, it might be worth
1238 <     * some factoring to reduce sprawl.
1239 <     */
1240 <
1241 <    /** Implementation for put */
1242 <    private final Object internalPut(Object k, Object v) {
1243 <        int h = spread(k.hashCode());
1244 <        int count = 0;
1245 <        for (Node[] tab = table;;) {
1246 <            int i; Node f; int fh; Object fk;
1247 <            if (tab == null)
1248 <                tab = initTable();
1249 <            else if ((f = tabAt(tab, i = (tab.length - 1) & h)) == null) {
1250 <                if (casTabAt(tab, i, null, new Node(h, k, v, null)))
1251 <                    break;                   // no lock when adding to empty bin
1252 <            }
1253 <            else if ((fh = f.hash) == MOVED) {
1254 <                if ((fk = f.key) instanceof TreeBin) {
1255 <                    TreeBin t = (TreeBin)fk;
1256 <                    Object oldVal = null;
1257 <                    t.acquire(0);
1258 <                    try {
1259 <                        if (tabAt(tab, i) == f) {
1260 <                            count = 2;
1261 <                            TreeNode p = t.putTreeNode(h, k, v);
1262 <                            if (p != null) {
1263 <                                oldVal = p.val;
1264 <                                p.val = v;
1265 <                            }
1266 <                        }
1267 <                    } finally {
1268 <                        t.release(0);
1269 <                    }
1270 <                    if (count != 0) {
1271 <                        if (oldVal != null)
1272 <                            return oldVal;
1273 <                        break;
1274 <                    }
1275 <                }
1276 <                else
1277 <                    tab = (Node[])fk;
1278 <            }
1279 <            else if ((fh & LOCKED) != 0) {
1280 <                checkForResize();
1281 <                f.tryAwaitLock(tab, i);
1282 <            }
1283 <            else if (f.casHash(fh, fh | LOCKED)) {
1284 <                Object oldVal = null;
1285 <                try {                        // needed in case equals() throws
1286 <                    if (tabAt(tab, i) == f) {
1287 <                        count = 1;
1288 <                        for (Node e = f;; ++count) {
1289 <                            Object ek, ev;
1290 <                            if ((e.hash & HASH_BITS) == h &&
1291 <                                (ev = e.val) != null &&
1292 <                                ((ek = e.key) == k || k.equals(ek))) {
1293 <                                oldVal = ev;
1294 <                                e.val = v;
1295 <                                break;
1296 <                            }
1297 <                            Node last = e;
1298 <                            if ((e = e.next) == null) {
1299 <                                last.next = new Node(h, k, v, null);
1300 <                                if (count >= TREE_THRESHOLD)
1301 <                                    replaceWithTreeBin(tab, i, k);
1302 <                                break;
1303 <                            }
1304 <                        }
1305 <                    }
1306 <                } finally {                  // unlock and signal if needed
1307 <                    if (!f.casHash(fh | LOCKED, fh)) {
1308 <                        f.hash = fh;
1309 <                        synchronized (f) { f.notifyAll(); };
1310 <                    }
1311 <                }
1312 <                if (count != 0) {
1313 <                    if (oldVal != null)
1314 <                        return oldVal;
1315 <                    if (tab.length <= 64)
1316 <                        count = 2;
1317 <                    break;
1318 <                }
1319 <            }
1320 <        }
1321 <        counter.add(1L);
1322 <        if (count > 1)
1323 <            checkForResize();
1324 <        return null;
1325 <    }
1326 <
1327 <    /** Implementation for putIfAbsent */
1328 <    private final Object internalPutIfAbsent(Object k, Object v) {
1329 <        int h = spread(k.hashCode());
1330 <        int count = 0;
1331 <        for (Node[] tab = table;;) {
1332 <            int i; Node f; int fh; Object fk, fv;
1333 <            if (tab == null)
1334 <                tab = initTable();
1335 <            else if ((f = tabAt(tab, i = (tab.length - 1) & h)) == null) {
1336 <                if (casTabAt(tab, i, null, new Node(h, k, v, null)))
1337 <                    break;
1338 <            }
1339 <            else if ((fh = f.hash) == MOVED) {
1340 <                if ((fk = f.key) instanceof TreeBin) {
1341 <                    TreeBin t = (TreeBin)fk;
1342 <                    Object oldVal = null;
1343 <                    t.acquire(0);
1344 <                    try {
1345 <                        if (tabAt(tab, i) == f) {
1346 <                            count = 2;
1347 <                            TreeNode p = t.putTreeNode(h, k, v);
1348 <                            if (p != null)
1349 <                                oldVal = p.val;
1350 <                        }
1351 <                    } finally {
1352 <                        t.release(0);
1353 <                    }
1354 <                    if (count != 0) {
1355 <                        if (oldVal != null)
1356 <                            return oldVal;
1357 <                        break;
1358 <                    }
1359 <                }
1360 <                else
1361 <                    tab = (Node[])fk;
1362 <            }
1363 <            else if ((fh & HASH_BITS) == h && (fv = f.val) != null &&
1364 <                     ((fk = f.key) == k || k.equals(fk)))
1365 <                return fv;
1366 <            else {
1367 <                Node g = f.next;
1368 <                if (g != null) { // at least 2 nodes -- search and maybe resize
1369 <                    for (Node e = g;;) {
1370 <                        Object ek, ev;
1371 <                        if ((e.hash & HASH_BITS) == h && (ev = e.val) != null &&
1372 <                            ((ek = e.key) == k || k.equals(ek)))
1373 <                            return ev;
1374 <                        if ((e = e.next) == null) {
1375 <                            checkForResize();
1376 <                            break;
1377 <                        }
1378 <                    }
1379 <                }
1380 <                if (((fh = f.hash) & LOCKED) != 0) {
1381 <                    checkForResize();
1382 <                    f.tryAwaitLock(tab, i);
1383 <                }
1384 <                else if (tabAt(tab, i) == f && f.casHash(fh, fh | LOCKED)) {
1385 <                    Object oldVal = null;
1386 <                    try {
1387 <                        if (tabAt(tab, i) == f) {
1388 <                            count = 1;
1389 <                            for (Node e = f;; ++count) {
1390 <                                Object ek, ev;
1391 <                                if ((e.hash & HASH_BITS) == h &&
1392 <                                    (ev = e.val) != null &&
1393 <                                    ((ek = e.key) == k || k.equals(ek))) {
1394 <                                    oldVal = ev;
1395 <                                    break;
1396 <                                }
1397 <                                Node last = e;
1398 <                                if ((e = e.next) == null) {
1399 <                                    last.next = new Node(h, k, v, null);
1400 <                                    if (count >= TREE_THRESHOLD)
1401 <                                        replaceWithTreeBin(tab, i, k);
1402 <                                    break;
1403 <                                }
1404 <                            }
1405 <                        }
1406 <                    } finally {
1407 <                        if (!f.casHash(fh | LOCKED, fh)) {
1408 <                            f.hash = fh;
1409 <                            synchronized (f) { f.notifyAll(); };
1410 <                        }
1411 <                    }
1412 <                    if (count != 0) {
1413 <                        if (oldVal != null)
1414 <                            return oldVal;
1415 <                        if (tab.length <= 64)
1416 <                            count = 2;
1417 <                        break;
1418 <                    }
1419 <                }
1420 <            }
1421 <        }
1422 <        counter.add(1L);
1423 <        if (count > 1)
1424 <            checkForResize();
1425 <        return null;
1426 <    }
1427 <
1428 <    /** Implementation for computeIfAbsent */
1429 <    private final Object internalComputeIfAbsent(K k,
1430 <                                                 Fun<? super K, ?> mf) {
1431 <        int h = spread(k.hashCode());
1432 <        Object val = null;
1433 <        int count = 0;
1434 <        for (Node[] tab = table;;) {
1435 <            Node f; int i, fh; Object fk, fv;
1436 <            if (tab == null)
1437 <                tab = initTable();
1438 <            else if ((f = tabAt(tab, i = (tab.length - 1) & h)) == null) {
1439 <                Node node = new Node(fh = h | LOCKED, k, null, null);
1440 <                if (casTabAt(tab, i, null, node)) {
1441 <                    count = 1;
1442 <                    try {
1443 <                        if ((val = mf.apply(k)) != null)
1444 <                            node.val = val;
1445 <                    } finally {
1446 <                        if (val == null)
1447 <                            setTabAt(tab, i, null);
1448 <                        if (!node.casHash(fh, h)) {
1449 <                            node.hash = h;
1450 <                            synchronized (node) { node.notifyAll(); };
1451 <                        }
1452 <                    }
1453 <                }
1454 <                if (count != 0)
1455 <                    break;
1456 <            }
1457 <            else if ((fh = f.hash) == MOVED) {
1458 <                if ((fk = f.key) instanceof TreeBin) {
1459 <                    TreeBin t = (TreeBin)fk;
1460 <                    boolean added = false;
1461 <                    t.acquire(0);
1462 <                    try {
1463 <                        if (tabAt(tab, i) == f) {
1464 <                            count = 1;
1465 <                            TreeNode p = t.getTreeNode(h, k, t.root);
1466 <                            if (p != null)
1467 <                                val = p.val;
1468 <                            else if ((val = mf.apply(k)) != null) {
1469 <                                added = true;
1470 <                                count = 2;
1471 <                                t.putTreeNode(h, k, val);
1472 <                            }
1473 <                        }
1474 <                    } finally {
1475 <                        t.release(0);
1476 <                    }
1477 <                    if (count != 0) {
1478 <                        if (!added)
1479 <                            return val;
1480 <                        break;
1481 <                    }
1482 <                }
1483 <                else
1484 <                    tab = (Node[])fk;
1485 <            }
1486 <            else if ((fh & HASH_BITS) == h && (fv = f.val) != null &&
1487 <                     ((fk = f.key) == k || k.equals(fk)))
1488 <                return fv;
1489 <            else {
1490 <                Node g = f.next;
1491 <                if (g != null) {
1492 <                    for (Node e = g;;) {
1493 <                        Object ek, ev;
1494 <                        if ((e.hash & HASH_BITS) == h && (ev = e.val) != null &&
1495 <                            ((ek = e.key) == k || k.equals(ek)))
1496 <                            return ev;
1497 <                        if ((e = e.next) == null) {
1498 <                            checkForResize();
1499 <                            break;
1500 <                        }
1501 <                    }
1502 <                }
1503 <                if (((fh = f.hash) & LOCKED) != 0) {
1504 <                    checkForResize();
1505 <                    f.tryAwaitLock(tab, i);
1506 <                }
1507 <                else if (tabAt(tab, i) == f && f.casHash(fh, fh | LOCKED)) {
1508 <                    boolean added = false;
1509 <                    try {
1510 <                        if (tabAt(tab, i) == f) {
1511 <                            count = 1;
1512 <                            for (Node e = f;; ++count) {
1513 <                                Object ek, ev;
1514 <                                if ((e.hash & HASH_BITS) == h &&
1515 <                                    (ev = e.val) != null &&
1516 <                                    ((ek = e.key) == k || k.equals(ek))) {
1517 <                                    val = ev;
1518 <                                    break;
1519 <                                }
1520 <                                Node last = e;
1521 <                                if ((e = e.next) == null) {
1522 <                                    if ((val = mf.apply(k)) != null) {
1523 <                                        added = true;
1524 <                                        last.next = new Node(h, k, val, null);
1525 <                                        if (count >= TREE_THRESHOLD)
1526 <                                            replaceWithTreeBin(tab, i, k);
1527 <                                    }
1528 <                                    break;
1529 <                                }
1530 <                            }
1531 <                        }
1532 <                    } finally {
1533 <                        if (!f.casHash(fh | LOCKED, fh)) {
1534 <                            f.hash = fh;
1535 <                            synchronized (f) { f.notifyAll(); };
1536 <                        }
1537 <                    }
1538 <                    if (count != 0) {
1539 <                        if (!added)
1540 <                            return val;
1541 <                        if (tab.length <= 64)
1542 <                            count = 2;
1543 <                        break;
1544 <                    }
1545 <                }
1546 <            }
1547 <        }
1548 <        if (val != null) {
1549 <            counter.add(1L);
1550 <            if (count > 1)
1551 <                checkForResize();
1552 <        }
1553 <        return val;
1554 <    }
1555 <
1556 <    /** Implementation for compute */
1557 <    @SuppressWarnings("unchecked") private final Object internalCompute
1558 <        (K k, boolean onlyIfPresent, BiFun<? super K, ? super V, ? extends V> mf) {
1559 <        int h = spread(k.hashCode());
1560 <        Object val = null;
1561 <        int delta = 0;
1562 <        int count = 0;
1563 <        for (Node[] tab = table;;) {
1564 <            Node f; int i, fh; Object fk;
1565 <            if (tab == null)
1566 <                tab = initTable();
1567 <            else if ((f = tabAt(tab, i = (tab.length - 1) & h)) == null) {
1568 <                if (onlyIfPresent)
1569 <                    break;
1570 <                Node node = new Node(fh = h | LOCKED, k, null, null);
1571 <                if (casTabAt(tab, i, null, node)) {
1572 <                    try {
1573 <                        count = 1;
1574 <                        if ((val = mf.apply(k, null)) != null) {
1575 <                            node.val = val;
1576 <                            delta = 1;
1577 <                        }
1578 <                    } finally {
1579 <                        if (delta == 0)
1580 <                            setTabAt(tab, i, null);
1581 <                        if (!node.casHash(fh, h)) {
1582 <                            node.hash = h;
1583 <                            synchronized (node) { node.notifyAll(); };
1584 <                        }
1585 <                    }
1586 <                }
1587 <                if (count != 0)
1588 <                    break;
1589 <            }
1590 <            else if ((fh = f.hash) == MOVED) {
1591 <                if ((fk = f.key) instanceof TreeBin) {
1592 <                    TreeBin t = (TreeBin)fk;
1593 <                    t.acquire(0);
1594 <                    try {
1595 <                        if (tabAt(tab, i) == f) {
1596 <                            count = 1;
1597 <                            TreeNode p = t.getTreeNode(h, k, t.root);
1598 <                            Object pv = (p == null) ? null : p.val;
1599 <                            if ((val = mf.apply(k, (V)pv)) != null) {
1600 <                                if (p != null)
1601 <                                    p.val = val;
1602 <                                else {
1603 <                                    count = 2;
1604 <                                    delta = 1;
1605 <                                    t.putTreeNode(h, k, val);
1606 <                                }
1607 <                            }
1608 <                            else if (p != null) {
1609 <                                delta = -1;
1610 <                                t.deleteTreeNode(p);
1611 <                            }
1612 <                        }
1613 <                    } finally {
1614 <                        t.release(0);
1615 <                    }
1616 <                    if (count != 0)
1617 <                        break;
1618 <                }
1619 <                else
1620 <                    tab = (Node[])fk;
1621 <            }
1622 <            else if ((fh & LOCKED) != 0) {
1623 <                checkForResize();
1624 <                f.tryAwaitLock(tab, i);
1625 <            }
1626 <            else if (f.casHash(fh, fh | LOCKED)) {
1627 <                try {
1628 <                    if (tabAt(tab, i) == f) {
1629 <                        count = 1;
1630 <                        for (Node e = f, pred = null;; ++count) {
1631 <                            Object ek, ev;
1632 <                            if ((e.hash & HASH_BITS) == h &&
1633 <                                (ev = e.val) != null &&
1634 <                                ((ek = e.key) == k || k.equals(ek))) {
1635 <                                val = mf.apply(k, (V)ev);
1636 <                                if (val != null)
1637 <                                    e.val = val;
1638 <                                else {
1639 <                                    delta = -1;
1640 <                                    Node en = e.next;
1641 <                                    if (pred != null)
1642 <                                        pred.next = en;
1643 <                                    else
1644 <                                        setTabAt(tab, i, en);
1645 <                                }
1646 <                                break;
1647 <                            }
1648 <                            pred = e;
1649 <                            if ((e = e.next) == null) {
1650 <                                if (!onlyIfPresent && (val = mf.apply(k, null)) != null) {
1651 <                                    pred.next = new Node(h, k, val, null);
1652 <                                    delta = 1;
1653 <                                    if (count >= TREE_THRESHOLD)
1654 <                                        replaceWithTreeBin(tab, i, k);
1655 <                                }
1656 <                                break;
1657 <                            }
1658 <                        }
1659 <                    }
1660 <                } finally {
1661 <                    if (!f.casHash(fh | LOCKED, fh)) {
1662 <                        f.hash = fh;
1663 <                        synchronized (f) { f.notifyAll(); };
1664 <                    }
1665 <                }
1666 <                if (count != 0) {
1667 <                    if (tab.length <= 64)
1668 <                        count = 2;
1669 <                    break;
1670 <                }
1671 <            }
1672 <        }
1673 <        if (delta != 0) {
1674 <            counter.add((long)delta);
1675 <            if (count > 1)
1676 <                checkForResize();
1677 <        }
1678 <        return val;
1679 <    }
1680 <
1681 <    /** Implementation for merge */
1682 <    @SuppressWarnings("unchecked") private final Object internalMerge
1683 <        (K k, V v, BiFun<? super V, ? super V, ? extends V> mf) {
1684 <        int h = spread(k.hashCode());
1685 <        Object val = null;
1686 <        int delta = 0;
1687 <        int count = 0;
1688 <        for (Node[] tab = table;;) {
1689 <            int i; Node f; int fh; Object fk, fv;
1690 <            if (tab == null)
1691 <                tab = initTable();
1692 <            else if ((f = tabAt(tab, i = (tab.length - 1) & h)) == null) {
1693 <                if (casTabAt(tab, i, null, new Node(h, k, v, null))) {
1694 <                    delta = 1;
1695 <                    val = v;
1696 <                    break;
1697 <                }
1698 <            }
1699 <            else if ((fh = f.hash) == MOVED) {
1700 <                if ((fk = f.key) instanceof TreeBin) {
1701 <                    TreeBin t = (TreeBin)fk;
1702 <                    t.acquire(0);
1703 <                    try {
1704 <                        if (tabAt(tab, i) == f) {
1705 <                            count = 1;
1706 <                            TreeNode p = t.getTreeNode(h, k, t.root);
1707 <                            val = (p == null) ? v : mf.apply((V)p.val, v);
1708 <                            if (val != null) {
1709 <                                if (p != null)
1710 <                                    p.val = val;
1711 <                                else {
1712 <                                    count = 2;
1713 <                                    delta = 1;
1714 <                                    t.putTreeNode(h, k, val);
1715 <                                }
1716 <                            }
1717 <                            else if (p != null) {
1718 <                                delta = -1;
1719 <                                t.deleteTreeNode(p);
1720 <                            }
1721 <                        }
1722 <                    } finally {
1723 <                        t.release(0);
1724 <                    }
1725 <                    if (count != 0)
1726 <                        break;
1727 <                }
1728 <                else
1729 <                    tab = (Node[])fk;
1730 <            }
1731 <            else if ((fh & LOCKED) != 0) {
1732 <                checkForResize();
1733 <                f.tryAwaitLock(tab, i);
1734 <            }
1735 <            else if (f.casHash(fh, fh | LOCKED)) {
1736 <                try {
1737 <                    if (tabAt(tab, i) == f) {
1738 <                        count = 1;
1739 <                        for (Node e = f, pred = null;; ++count) {
1740 <                            Object ek, ev;
1741 <                            if ((e.hash & HASH_BITS) == h &&
1742 <                                (ev = e.val) != null &&
1743 <                                ((ek = e.key) == k || k.equals(ek))) {
1744 <                                val = mf.apply(v, (V)ev);
1745 <                                if (val != null)
1746 <                                    e.val = val;
1747 <                                else {
1748 <                                    delta = -1;
1749 <                                    Node en = e.next;
1750 <                                    if (pred != null)
1751 <                                        pred.next = en;
1752 <                                    else
1753 <                                        setTabAt(tab, i, en);
1754 <                                }
1755 <                                break;
1756 <                            }
1757 <                            pred = e;
1758 <                            if ((e = e.next) == null) {
1759 <                                val = v;
1760 <                                pred.next = new Node(h, k, val, null);
1761 <                                delta = 1;
1762 <                                if (count >= TREE_THRESHOLD)
1763 <                                    replaceWithTreeBin(tab, i, k);
1764 <                                break;
1765 <                            }
1766 <                        }
1767 <                    }
1768 <                } finally {
1769 <                    if (!f.casHash(fh | LOCKED, fh)) {
1770 <                        f.hash = fh;
1771 <                        synchronized (f) { f.notifyAll(); };
1772 <                    }
1773 <                }
1774 <                if (count != 0) {
1775 <                    if (tab.length <= 64)
1776 <                        count = 2;
1777 <                    break;
1778 <                }
1779 <            }
1780 <        }
1781 <        if (delta != 0) {
1782 <            counter.add((long)delta);
1783 <            if (count > 1)
1784 <                checkForResize();
1785 <        }
1786 <        return val;
1787 <    }
1788 <
1789 <    /** Implementation for putAll */
1790 <    private final void internalPutAll(Map<?, ?> m) {
1791 <        tryPresize(m.size());
1792 <        long delta = 0L;     // number of uncommitted additions
1793 <        boolean npe = false; // to throw exception on exit for nulls
1794 <        try {                // to clean up counts on other exceptions
1795 <            for (Map.Entry<?, ?> entry : m.entrySet()) {
1796 <                Object k, v;
1797 <                if (entry == null || (k = entry.getKey()) == null ||
1798 <                    (v = entry.getValue()) == null) {
1799 <                    npe = true;
1800 <                    break;
1801 <                }
1802 <                int h = spread(k.hashCode());
1803 <                for (Node[] tab = table;;) {
1804 <                    int i; Node f; int fh; Object fk;
1805 <                    if (tab == null)
1806 <                        tab = initTable();
1807 <                    else if ((f = tabAt(tab, i = (tab.length - 1) & h)) == null){
1808 <                        if (casTabAt(tab, i, null, new Node(h, k, v, null))) {
1809 <                            ++delta;
1810 <                            break;
1811 <                        }
1812 <                    }
1813 <                    else if ((fh = f.hash) == MOVED) {
1814 <                        if ((fk = f.key) instanceof TreeBin) {
1815 <                            TreeBin t = (TreeBin)fk;
1816 <                            boolean validated = false;
1817 <                            t.acquire(0);
1818 <                            try {
1819 <                                if (tabAt(tab, i) == f) {
1820 <                                    validated = true;
1821 <                                    TreeNode p = t.getTreeNode(h, k, t.root);
1822 <                                    if (p != null)
1823 <                                        p.val = v;
1824 <                                    else {
1825 <                                        t.putTreeNode(h, k, v);
1826 <                                        ++delta;
1827 <                                    }
1828 <                                }
1829 <                            } finally {
1830 <                                t.release(0);
1831 <                            }
1832 <                            if (validated)
1833 <                                break;
1834 <                        }
1835 <                        else
1836 <                            tab = (Node[])fk;
1837 <                    }
1838 <                    else if ((fh & LOCKED) != 0) {
1839 <                        counter.add(delta);
1840 <                        delta = 0L;
1841 <                        checkForResize();
1842 <                        f.tryAwaitLock(tab, i);
1843 <                    }
1844 <                    else if (f.casHash(fh, fh | LOCKED)) {
1845 <                        int count = 0;
1846 <                        try {
1847 <                            if (tabAt(tab, i) == f) {
1848 <                                count = 1;
1849 <                                for (Node e = f;; ++count) {
1850 <                                    Object ek, ev;
1851 <                                    if ((e.hash & HASH_BITS) == h &&
1852 <                                        (ev = e.val) != null &&
1853 <                                        ((ek = e.key) == k || k.equals(ek))) {
1854 <                                        e.val = v;
1855 <                                        break;
1856 <                                    }
1857 <                                    Node last = e;
1858 <                                    if ((e = e.next) == null) {
1859 <                                        ++delta;
1860 <                                        last.next = new Node(h, k, v, null);
1861 <                                        if (count >= TREE_THRESHOLD)
1862 <                                            replaceWithTreeBin(tab, i, k);
1863 <                                        break;
1864 <                                    }
1865 <                                }
1866 <                            }
1867 <                        } finally {
1868 <                            if (!f.casHash(fh | LOCKED, fh)) {
1869 <                                f.hash = fh;
1870 <                                synchronized (f) { f.notifyAll(); };
1871 <                            }
1872 <                        }
1873 <                        if (count != 0) {
1874 <                            if (count > 1) {
1875 <                                counter.add(delta);
1876 <                                delta = 0L;
1877 <                                checkForResize();
1878 <                            }
1879 <                            break;
1880 <                        }
1881 <                    }
1882 <                }
1883 <            }
1884 <        } finally {
1885 <            if (delta != 0)
1886 <                counter.add(delta);
1887 <        }
1888 <        if (npe)
1889 <            throw new NullPointerException();
661 >    static final int spread(int h) {
662 >        return (h ^ (h >>> 16)) & HASH_BITS;
663      }
664  
1892    /* ---------------- Table Initialization and Resizing -------------- */
1893
665      /**
666       * Returns a power of two table size for the given desired capacity.
667       * See Hackers Delight, sec 3.2
# Line 1906 | Line 677 | public class ConcurrentHashMapV8<K, V>
677      }
678  
679      /**
680 <     * Initializes table, using the size recorded in sizeCtl.
680 >     * Returns x's Class if it is of the form "class C implements
681 >     * Comparable<C>", else null.
682       */
683 <    private final Node[] initTable() {
684 <        Node[] tab; int sc;
685 <        while ((tab = table) == null) {
686 <            if ((sc = sizeCtl) < 0)
687 <                Thread.yield(); // lost initialization race; just spin
688 <            else if (UNSAFE.compareAndSwapInt(this, sizeCtlOffset, sc, -1)) {
689 <                try {
690 <                    if ((tab = table) == null) {
691 <                        int n = (sc > 0) ? sc : DEFAULT_CAPACITY;
692 <                        tab = table = new Node[n];
693 <                        sc = n - (n >>> 2);
694 <                    }
695 <                } finally {
1924 <                    sizeCtl = sc;
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                  }
1926                break;
1927            }
1928        }
1929        return tab;
1930    }
1931
1932    /**
1933     * If table is too small and not already resizing, creates next
1934     * table and transfers bins.  Rechecks occupancy after a transfer
1935     * to see if another resize is already needed because resizings
1936     * are lagging additions.
1937     */
1938    private final void checkForResize() {
1939        Node[] tab; int n, sc;
1940        while ((tab = table) != null &&
1941               (n = tab.length) < MAXIMUM_CAPACITY &&
1942               (sc = sizeCtl) >= 0 && counter.sum() >= (long)sc &&
1943               UNSAFE.compareAndSwapInt(this, sizeCtlOffset, sc, -1)) {
1944            try {
1945                if (tab == table) {
1946                    table = rebuild(tab);
1947                    sc = (n << 1) - (n >>> 1);
1948                }
1949            } finally {
1950                sizeCtl = sc;
697              }
698          }
699 +        return null;
700      }
701  
702      /**
703 <     * Tries to presize table to accommodate the given number of elements.
704 <     *
1958 <     * @param size number of elements (doesn't need to be perfectly accurate)
703 >     * Returns k.compareTo(x) if x matches kc (k's screened comparable
704 >     * class), else 0.
705       */
706 <    private final void tryPresize(int size) {
707 <        int c = (size >= (MAXIMUM_CAPACITY >>> 1)) ? MAXIMUM_CAPACITY :
708 <            tableSizeFor(size + (size >>> 1) + 1);
709 <        int sc;
1964 <        while ((sc = sizeCtl) >= 0) {
1965 <            Node[] tab = table; int n;
1966 <            if (tab == null || (n = tab.length) == 0) {
1967 <                n = (sc > c) ? sc : c;
1968 <                if (UNSAFE.compareAndSwapInt(this, sizeCtlOffset, sc, -1)) {
1969 <                    try {
1970 <                        if (table == tab) {
1971 <                            table = new Node[n];
1972 <                            sc = n - (n >>> 2);
1973 <                        }
1974 <                    } finally {
1975 <                        sizeCtl = sc;
1976 <                    }
1977 <                }
1978 <            }
1979 <            else if (c <= sc || n >= MAXIMUM_CAPACITY)
1980 <                break;
1981 <            else if (UNSAFE.compareAndSwapInt(this, sizeCtlOffset, sc, -1)) {
1982 <                try {
1983 <                    if (table == tab) {
1984 <                        table = rebuild(tab);
1985 <                        sc = (n << 1) - (n >>> 1);
1986 <                    }
1987 <                } finally {
1988 <                    sizeCtl = sc;
1989 <                }
1990 <            }
1991 <        }
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 +    /* ---------------- Table element access -------------- */
713 +
714      /*
715 <     * Moves and/or copies the nodes in each bin to new table. See
716 <     * above for explanation.
717 <     *
718 <     * @return the new table
719 <     */
720 <    private static final Node[] rebuild(Node[] tab) {
721 <        int n = tab.length;
722 <        Node[] nextTab = new Node[n << 1];
723 <        Node fwd = new Node(MOVED, nextTab, null, null);
724 <        int[] buffer = null;       // holds bins to revisit; null until needed
725 <        Node rev = null;           // reverse forwarder; null until needed
726 <        int nbuffered = 0;         // the number of bins in buffer list
727 <        int bufferIndex = 0;       // buffer index of current buffered bin
728 <        int bin = n - 1;           // current non-buffered bin or -1 if none
729 <
730 <        for (int i = bin;;) {      // start upwards sweep
731 <            int fh; Node f;
732 <            if ((f = tabAt(tab, i)) == null) {
733 <                if (bin >= 0) {    // Unbuffered; no lock needed (or available)
734 <                    if (!casTabAt(tab, i, f, fwd))
735 <                        continue;
736 <                }
737 <                else {             // transiently use a locked forwarding node
738 <                    Node g = new Node(MOVED|LOCKED, nextTab, null, null);
739 <                    if (!casTabAt(tab, i, f, g))
740 <                        continue;
741 <                    setTabAt(nextTab, i, null);
2022 <                    setTabAt(nextTab, i + n, null);
2023 <                    setTabAt(tab, i, fwd);
2024 <                    if (!g.casHash(MOVED|LOCKED, MOVED)) {
2025 <                        g.hash = MOVED;
2026 <                        synchronized (g) { g.notifyAll(); }
2027 <                    }
2028 <                }
2029 <            }
2030 <            else if ((fh = f.hash) == MOVED) {
2031 <                Object fk = f.key;
2032 <                if (fk instanceof TreeBin) {
2033 <                    TreeBin t = (TreeBin)fk;
2034 <                    boolean validated = false;
2035 <                    t.acquire(0);
2036 <                    try {
2037 <                        if (tabAt(tab, i) == f) {
2038 <                            validated = true;
2039 <                            splitTreeBin(nextTab, i, t);
2040 <                            setTabAt(tab, i, fwd);
2041 <                        }
2042 <                    } finally {
2043 <                        t.release(0);
2044 <                    }
2045 <                    if (!validated)
2046 <                        continue;
2047 <                }
2048 <            }
2049 <            else if ((fh & LOCKED) == 0 && f.casHash(fh, fh|LOCKED)) {
2050 <                boolean validated = false;
2051 <                try {              // split to lo and hi lists; copying as needed
2052 <                    if (tabAt(tab, i) == f) {
2053 <                        validated = true;
2054 <                        splitBin(nextTab, i, f);
2055 <                        setTabAt(tab, i, fwd);
2056 <                    }
2057 <                } finally {
2058 <                    if (!f.casHash(fh | LOCKED, fh)) {
2059 <                        f.hash = fh;
2060 <                        synchronized (f) { f.notifyAll(); };
2061 <                    }
2062 <                }
2063 <                if (!validated)
2064 <                    continue;
2065 <            }
2066 <            else {
2067 <                if (buffer == null) // initialize buffer for revisits
2068 <                    buffer = new int[TRANSFER_BUFFER_SIZE];
2069 <                if (bin < 0 && bufferIndex > 0) {
2070 <                    int j = buffer[--bufferIndex];
2071 <                    buffer[bufferIndex] = i;
2072 <                    i = j;         // swap with another bin
2073 <                    continue;
2074 <                }
2075 <                if (bin < 0 || nbuffered >= TRANSFER_BUFFER_SIZE) {
2076 <                    f.tryAwaitLock(tab, i);
2077 <                    continue;      // no other options -- block
2078 <                }
2079 <                if (rev == null)   // initialize reverse-forwarder
2080 <                    rev = new Node(MOVED, tab, null, null);
2081 <                if (tabAt(tab, i) != f || (f.hash & LOCKED) == 0)
2082 <                    continue;      // recheck before adding to list
2083 <                buffer[nbuffered++] = i;
2084 <                setTabAt(nextTab, i, rev);     // install place-holders
2085 <                setTabAt(nextTab, i + n, rev);
2086 <            }
2087 <
2088 <            if (bin > 0)
2089 <                i = --bin;
2090 <            else if (buffer != null && nbuffered > 0) {
2091 <                bin = -1;
2092 <                i = buffer[bufferIndex = --nbuffered];
2093 <            }
2094 <            else
2095 <                return nextTab;
2096 <        }
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 in principle require only release ordering, not need
726 >     * full volatile semantics, but are currently coded as volatile
727 >     * writes to be conservative.
728 >     */
729 >
730 >    @SuppressWarnings("unchecked")
731 >    static final <K,V> Node<K,V> tabAt(Node<K,V>[] tab, int i) {
732 >        return (Node<K,V>)U.getObjectVolatile(tab, ((long)i << ASHIFT) + ABASE);
733 >    }
734 >
735 >    static final <K,V> boolean casTabAt(Node<K,V>[] tab, int i,
736 >                                        Node<K,V> c, Node<K,V> v) {
737 >        return U.compareAndSwapObject(tab, ((long)i << ASHIFT) + ABASE, c, v);
738 >    }
739 >
740 >    static final <K,V> void setTabAt(Node<K,V>[] tab, int i, Node<K,V> v) {
741 >        U.putObjectVolatile(tab, ((long)i << ASHIFT) + ABASE, v);
742      }
743  
744 +    /* ---------------- Fields -------------- */
745 +
746      /**
747 <     * Splits a normal bin with list headed by e into lo and hi parts;
748 <     * installs in given table.
747 >     * The array of bins. Lazily initialized upon first insertion.
748 >     * Size is always a power of two. Accessed directly by iterators.
749       */
750 <    private static void splitBin(Node[] nextTab, int i, Node e) {
2104 <        int bit = nextTab.length >>> 1; // bit to split on
2105 <        int runBit = e.hash & bit;
2106 <        Node lastRun = e, lo = null, hi = null;
2107 <        for (Node p = e.next; p != null; p = p.next) {
2108 <            int b = p.hash & bit;
2109 <            if (b != runBit) {
2110 <                runBit = b;
2111 <                lastRun = p;
2112 <            }
2113 <        }
2114 <        if (runBit == 0)
2115 <            lo = lastRun;
2116 <        else
2117 <            hi = lastRun;
2118 <        for (Node p = e; p != lastRun; p = p.next) {
2119 <            int ph = p.hash & HASH_BITS;
2120 <            Object pk = p.key, pv = p.val;
2121 <            if ((ph & bit) == 0)
2122 <                lo = new Node(ph, pk, pv, lo);
2123 <            else
2124 <                hi = new Node(ph, pk, pv, hi);
2125 <        }
2126 <        setTabAt(nextTab, i, lo);
2127 <        setTabAt(nextTab, i + bit, hi);
2128 <    }
750 >    transient volatile Node<K,V>[] table;
751  
752      /**
753 <     * Splits a tree bin into lo and hi parts; installs in given table.
753 >     * The next table to use; non-null only while resizing.
754       */
755 <    private static void splitTreeBin(Node[] nextTab, int i, TreeBin t) {
2134 <        int bit = nextTab.length >>> 1;
2135 <        TreeBin lt = new TreeBin();
2136 <        TreeBin ht = new TreeBin();
2137 <        int lc = 0, hc = 0;
2138 <        for (Node e = t.first; e != null; e = e.next) {
2139 <            int h = e.hash & HASH_BITS;
2140 <            Object k = e.key, v = e.val;
2141 <            if ((h & bit) == 0) {
2142 <                ++lc;
2143 <                lt.putTreeNode(h, k, v);
2144 <            }
2145 <            else {
2146 <                ++hc;
2147 <                ht.putTreeNode(h, k, v);
2148 <            }
2149 <        }
2150 <        Node ln, hn; // throw away trees if too small
2151 <        if (lc <= (TREE_THRESHOLD >>> 1)) {
2152 <            ln = null;
2153 <            for (Node p = lt.first; p != null; p = p.next)
2154 <                ln = new Node(p.hash, p.key, p.val, ln);
2155 <        }
2156 <        else
2157 <            ln = new Node(MOVED, lt, null, null);
2158 <        setTabAt(nextTab, i, ln);
2159 <        if (hc <= (TREE_THRESHOLD >>> 1)) {
2160 <            hn = null;
2161 <            for (Node p = ht.first; p != null; p = p.next)
2162 <                hn = new Node(p.hash, p.key, p.val, hn);
2163 <        }
2164 <        else
2165 <            hn = new Node(MOVED, ht, null, null);
2166 <        setTabAt(nextTab, i + bit, hn);
2167 <    }
755 >    private transient volatile Node<K,V>[] nextTable;
756  
757      /**
758 <     * Implementation for clear. Steps through each bin, removing all
759 <     * nodes.
758 >     * Base counter value, used mainly when there is no contention,
759 >     * but also as a fallback during table initialization
760 >     * races. Updated via CAS.
761       */
762 <    private final void internalClear() {
2174 <        long delta = 0L; // negative number of deletions
2175 <        int i = 0;
2176 <        Node[] tab = table;
2177 <        while (tab != null && i < tab.length) {
2178 <            int fh; Object fk;
2179 <            Node f = tabAt(tab, i);
2180 <            if (f == null)
2181 <                ++i;
2182 <            else if ((fh = f.hash) == MOVED) {
2183 <                if ((fk = f.key) instanceof TreeBin) {
2184 <                    TreeBin t = (TreeBin)fk;
2185 <                    t.acquire(0);
2186 <                    try {
2187 <                        if (tabAt(tab, i) == f) {
2188 <                            for (Node p = t.first; p != null; p = p.next) {
2189 <                                if (p.val != null) { // (currently always true)
2190 <                                    p.val = null;
2191 <                                    --delta;
2192 <                                }
2193 <                            }
2194 <                            t.first = null;
2195 <                            t.root = null;
2196 <                            ++i;
2197 <                        }
2198 <                    } finally {
2199 <                        t.release(0);
2200 <                    }
2201 <                }
2202 <                else
2203 <                    tab = (Node[])fk;
2204 <            }
2205 <            else if ((fh & LOCKED) != 0) {
2206 <                counter.add(delta); // opportunistically update count
2207 <                delta = 0L;
2208 <                f.tryAwaitLock(tab, i);
2209 <            }
2210 <            else if (f.casHash(fh, fh | LOCKED)) {
2211 <                try {
2212 <                    if (tabAt(tab, i) == f) {
2213 <                        for (Node e = f; e != null; e = e.next) {
2214 <                            if (e.val != null) {  // (currently always true)
2215 <                                e.val = null;
2216 <                                --delta;
2217 <                            }
2218 <                        }
2219 <                        setTabAt(tab, i, null);
2220 <                        ++i;
2221 <                    }
2222 <                } finally {
2223 <                    if (!f.casHash(fh | LOCKED, fh)) {
2224 <                        f.hash = fh;
2225 <                        synchronized (f) { f.notifyAll(); };
2226 <                    }
2227 <                }
2228 <            }
2229 <        }
2230 <        if (delta != 0)
2231 <            counter.add(delta);
2232 <    }
762 >    private transient volatile long baseCount;
763  
764 <    /* ----------------Table Traversal -------------- */
764 >    /**
765 >     * Table initialization and resizing control.  When negative, the
766 >     * table is being initialized or resized: -1 for initialization,
767 >     * else -(1 + the number of active resizing threads).  Otherwise,
768 >     * when table is null, holds the initial table size to use upon
769 >     * creation, or 0 for default. After initialization, holds the
770 >     * next element count value upon which to resize the table.
771 >     */
772 >    private transient volatile int sizeCtl;
773  
774      /**
775 <     * Encapsulates traversal for methods such as containsValue; also
776 <     * serves as a base class for other iterators and bulk tasks.
777 <     *
2240 <     * At each step, the iterator snapshots the key ("nextKey") and
2241 <     * value ("nextVal") of a valid node (i.e., one that, at point of
2242 <     * snapshot, has a non-null user value). Because val fields can
2243 <     * change (including to null, indicating deletion), field nextVal
2244 <     * might not be accurate at point of use, but still maintains the
2245 <     * weak consistency property of holding a value that was once
2246 <     * valid.
2247 <     *
2248 <     * Internal traversals directly access these fields, as in:
2249 <     * {@code while (it.advance() != null) { process(it.nextKey); }}
2250 <     *
2251 <     * Exported iterators must track whether the iterator has advanced
2252 <     * (in hasNext vs next) (by setting/checking/nulling field
2253 <     * nextVal), and then extract key, value, or key-value pairs as
2254 <     * return values of next().
2255 <     *
2256 <     * The iterator visits once each still-valid node that was
2257 <     * reachable upon iterator construction. It might miss some that
2258 <     * were added to a bin after the bin was visited, which is OK wrt
2259 <     * consistency guarantees. Maintaining this property in the face
2260 <     * of possible ongoing resizes requires a fair amount of
2261 <     * bookkeeping state that is difficult to optimize away amidst
2262 <     * volatile accesses.  Even so, traversal maintains reasonable
2263 <     * throughput.
2264 <     *
2265 <     * Normally, iteration proceeds bin-by-bin traversing lists.
2266 <     * However, if the table has been resized, then all future steps
2267 <     * must traverse both the bin at the current index as well as at
2268 <     * (index + baseSize); and so on for further resizings. To
2269 <     * paranoically cope with potential sharing by users of iterators
2270 <     * across threads, iteration terminates if a bounds checks fails
2271 <     * for a table read.
2272 <     *
2273 <     * This class extends ForkJoinTask to streamline parallel
2274 <     * iteration in bulk operations (see BulkTask). This adds only an
2275 <     * int of space overhead, which is close enough to negligible in
2276 <     * cases where it is not needed to not worry about it.  Because
2277 <     * ForkJoinTask is Serializable, but iterators need not be, we
2278 <     * need to add warning suppressions.
2279 <     */
2280 <    @SuppressWarnings("serial") static class Traverser<K,V,R> extends ForkJoinTask<R> {
2281 <        final ConcurrentHashMapV8<K, V> map;
2282 <        Node next;           // the next entry to use
2283 <        Node last;           // the last entry used
2284 <        Object nextKey;      // cached key field of next
2285 <        Object nextVal;      // cached val field of next
2286 <        Node[] tab;          // current table; updated if resized
2287 <        int index;           // index of bin to use next
2288 <        int baseIndex;       // current index of initial table
2289 <        int baseLimit;       // index bound for initial table
2290 <        final int baseSize;  // initial table size
2291 <
2292 <        /** Creates iterator for all entries in the table. */
2293 <        Traverser(ConcurrentHashMapV8<K, V> map) {
2294 <            this.tab = (this.map = map).table;
2295 <            baseLimit = baseSize = (tab == null) ? 0 : tab.length;
2296 <        }
2297 <
2298 <        /** Creates iterator for split() methods */
2299 <        Traverser(Traverser<K,V,?> it) {
2300 <            this.map = it.map;
2301 <            this.tab = it.tab;
2302 <            this.baseSize = it.baseSize;
2303 <            it.baseLimit = this.index = this.baseIndex =
2304 <                ((this.baseLimit = it.baseLimit) + it.baseIndex + 1) >>> 1;
2305 <        }
775 >     * The next table index (plus one) to split while resizing.
776 >     */
777 >    private transient volatile int transferIndex;
778  
779 <        /**
780 <         * Advances next; returns nextVal or null if terminated.
781 <         * See above for explanation.
782 <         */
2311 <        final Object advance() {
2312 <            Node e = last = next;
2313 <            Object ev = null;
2314 <            outer: do {
2315 <                if (e != null)                  // advance past used/skipped node
2316 <                    e = e.next;
2317 <                while (e == null) {             // get to next non-null bin
2318 <                    Node[] t; int b, i, n; Object ek; // checks must use locals
2319 <                    if ((b = baseIndex) >= baseLimit || (i = index) < 0 ||
2320 <                        (t = tab) == null || i >= (n = t.length))
2321 <                        break outer;
2322 <                    else if ((e = tabAt(t, i)) != null && e.hash == MOVED) {
2323 <                        if ((ek = e.key) instanceof TreeBin)
2324 <                            e = ((TreeBin)ek).first;
2325 <                        else {
2326 <                            tab = (Node[])ek;
2327 <                            continue;           // restarts due to null val
2328 <                        }
2329 <                    }                           // visit upper slots if present
2330 <                    index = (i += baseSize) < n ? i : (baseIndex = b + 1);
2331 <                }
2332 <                nextKey = e.key;
2333 <            } while ((ev = e.val) == null);    // skip deleted or special nodes
2334 <            next = e;
2335 <            return nextVal = ev;
2336 <        }
779 >    /**
780 >     * The least available table index to split while resizing.
781 >     */
782 >    private transient volatile int transferOrigin;
783  
784 <        public final void remove() {
785 <            if (nextVal == null && last == null)
786 <                advance();
787 <            Node e = last;
2342 <            if (e == null)
2343 <                throw new IllegalStateException();
2344 <            last = null;
2345 <            map.remove(e.key);
2346 <        }
784 >    /**
785 >     * Spinlock (locked via CAS) used when resizing and/or creating CounterCells.
786 >     */
787 >    private transient volatile int cellsBusy;
788  
789 <        public final boolean hasNext() {
790 <            return nextVal != null || advance() != null;
791 <        }
789 >    /**
790 >     * Table of counter cells. When non-null, size is a power of 2.
791 >     */
792 >    private transient volatile CounterCell[] counterCells;
793 >
794 >    // views
795 >    private transient KeySetView<K,V> keySet;
796 >    private transient ValuesView<K,V> values;
797 >    private transient EntrySetView<K,V> entrySet;
798  
2352        public final boolean hasMoreElements() { return hasNext(); }
2353        public final void setRawResult(Object x) { }
2354        public R getRawResult() { return null; }
2355        public boolean exec() { return true; }
2356    }
799  
800      /* ---------------- Public operations -------------- */
801  
# Line 2361 | Line 803 | public class ConcurrentHashMapV8<K, V>
803       * Creates a new, empty map with the default initial table size (16).
804       */
805      public ConcurrentHashMapV8() {
2364        this.counter = new LongAdder();
806      }
807  
808      /**
# Line 2380 | Line 821 | public class ConcurrentHashMapV8<K, V>
821          int cap = ((initialCapacity >= (MAXIMUM_CAPACITY >>> 1)) ?
822                     MAXIMUM_CAPACITY :
823                     tableSizeFor(initialCapacity + (initialCapacity >>> 1) + 1));
2383        this.counter = new LongAdder();
824          this.sizeCtl = cap;
825      }
826  
# Line 2390 | Line 830 | public class ConcurrentHashMapV8<K, V>
830       * @param m the map
831       */
832      public ConcurrentHashMapV8(Map<? extends K, ? extends V> m) {
2393        this.counter = new LongAdder();
833          this.sizeCtl = DEFAULT_CAPACITY;
834 <        internalPutAll(m);
834 >        putAll(m);
835      }
836  
837      /**
# Line 2433 | Line 872 | public class ConcurrentHashMapV8<K, V>
872       * nonpositive
873       */
874      public ConcurrentHashMapV8(int initialCapacity,
875 <                               float loadFactor, int concurrencyLevel) {
875 >                             float loadFactor, int concurrencyLevel) {
876          if (!(loadFactor > 0.0f) || initialCapacity < 0 || concurrencyLevel <= 0)
877              throw new IllegalArgumentException();
878          if (initialCapacity < concurrencyLevel)   // Use at least as many bins
# Line 2441 | Line 880 | public class ConcurrentHashMapV8<K, V>
880          long size = (long)(1.0 + (long)initialCapacity / loadFactor);
881          int cap = (size >= (long)MAXIMUM_CAPACITY) ?
882              MAXIMUM_CAPACITY : tableSizeFor((int)size);
2444        this.counter = new LongAdder();
883          this.sizeCtl = cap;
884      }
885  
886 <    /**
2449 <     * {@inheritDoc}
2450 <     */
2451 <    public boolean isEmpty() {
2452 <        return counter.sum() <= 0L; // ignore transient negative values
2453 <    }
886 >    // Original (since JDK1.2) Map methods
887  
888      /**
889       * {@inheritDoc}
890       */
891      public int size() {
892 <        long n = counter.sum();
892 >        long n = sumCount();
893          return ((n < 0L) ? 0 :
894                  (n > (long)Integer.MAX_VALUE) ? Integer.MAX_VALUE :
895                  (int)n);
896      }
897  
898      /**
899 <     * Returns the number of mappings. This method should be used
2467 <     * instead of {@link #size} because a ConcurrentHashMap may
2468 <     * contain more mappings than can be represented as an int. The
2469 <     * value returned is a snapshot; the actual count may differ if
2470 <     * there are ongoing concurrent insertions or removals.
2471 <     *
2472 <     * @return the number of mappings
899 >     * {@inheritDoc}
900       */
901 <    public long mappingCount() {
902 <        long n = counter.sum();
2476 <        return (n < 0L) ? 0L : n; // ignore transient negative values
901 >    public boolean isEmpty() {
902 >        return sumCount() <= 0L; // ignore transient negative values
903      }
904  
905      /**
# Line 2487 | Line 913 | public class ConcurrentHashMapV8<K, V>
913       *
914       * @throws NullPointerException if the specified key is null
915       */
916 <    @SuppressWarnings("unchecked") public V get(Object key) {
917 <        if (key == null)
918 <            throw new NullPointerException();
919 <        return (V)internalGet(key);
916 >    public V get(Object key) {
917 >        Node<K,V>[] tab; Node<K,V> e, p; int n, eh; K ek;
918 >        int h = spread(key.hashCode());
919 >        if ((tab = table) != null && (n = tab.length) > 0 &&
920 >            (e = tabAt(tab, (n - 1) & h)) != null) {
921 >            if ((eh = e.hash) == h) {
922 >                if ((ek = e.key) == key || (ek != null && key.equals(ek)))
923 >                    return e.val;
924 >            }
925 >            else if (eh < 0)
926 >                return (p = e.find(h, key)) != null ? p.val : null;
927 >            while ((e = e.next) != null) {
928 >                if (e.hash == h &&
929 >                    ((ek = e.key) == key || (ek != null && key.equals(ek))))
930 >                    return e.val;
931 >            }
932 >        }
933 >        return null;
934      }
935  
936      /**
937       * Tests if the specified object is a key in this table.
938       *
939 <     * @param  key   possible key
939 >     * @param  key possible key
940       * @return {@code true} if and only if the specified object
941       *         is a key in this table, as determined by the
942       *         {@code equals} method; {@code false} otherwise
943       * @throws NullPointerException if the specified key is null
944       */
945      public boolean containsKey(Object key) {
946 <        if (key == null)
2507 <            throw new NullPointerException();
2508 <        return internalGet(key) != null;
946 >        return get(key) != null;
947      }
948  
949      /**
# Line 2521 | Line 959 | public class ConcurrentHashMapV8<K, V>
959      public boolean containsValue(Object value) {
960          if (value == null)
961              throw new NullPointerException();
962 <        Object v;
963 <        Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
964 <        while ((v = it.advance()) != null) {
965 <            if (v == value || value.equals(v))
966 <                return true;
962 >        Node<K,V>[] t;
963 >        if ((t = table) != null) {
964 >            Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length);
965 >            for (Node<K,V> p; (p = it.advance()) != null; ) {
966 >                V v;
967 >                if ((v = p.val) == value || (v != null && value.equals(v)))
968 >                    return true;
969 >            }
970          }
971          return false;
972      }
973  
974      /**
2534     * Legacy method testing if some key maps into the specified value
2535     * in this table.  This method is identical in functionality to
2536     * {@link #containsValue}, and exists solely to ensure
2537     * full compatibility with class {@link java.util.Hashtable},
2538     * which supported this method prior to introduction of the
2539     * Java Collections framework.
2540     *
2541     * @param  value a value to search for
2542     * @return {@code true} if and only if some key maps to the
2543     *         {@code value} argument in this table as
2544     *         determined by the {@code equals} method;
2545     *         {@code false} otherwise
2546     * @throws NullPointerException if the specified value is null
2547     */
2548    public boolean contains(Object value) {
2549        return containsValue(value);
2550    }
2551
2552    /**
975       * Maps the specified key to the specified value in this table.
976       * Neither the key nor the value can be null.
977       *
978 <     * <p> The value can be retrieved by calling the {@code get} method
978 >     * <p>The value can be retrieved by calling the {@code get} method
979       * with a key that is equal to the original key.
980       *
981       * @param key key with which the specified value is to be associated
# Line 2562 | Line 984 | public class ConcurrentHashMapV8<K, V>
984       *         {@code null} if there was no mapping for {@code key}
985       * @throws NullPointerException if the specified key or value is null
986       */
987 <    @SuppressWarnings("unchecked") public V put(K key, V value) {
988 <        if (key == null || value == null)
2567 <            throw new NullPointerException();
2568 <        return (V)internalPut(key, value);
987 >    public V put(K key, V value) {
988 >        return putVal(key, value, false);
989      }
990  
991 <    /**
992 <     * {@inheritDoc}
993 <     *
994 <     * @return the previous value associated with the specified key,
995 <     *         or {@code null} if there was no mapping for the key
996 <     * @throws NullPointerException if the specified key or value is null
997 <     */
998 <    @SuppressWarnings("unchecked") public V putIfAbsent(K key, V value) {
999 <        if (key == null || value == null)
1000 <            throw new NullPointerException();
1001 <        return (V)internalPutIfAbsent(key, value);
991 >    /** Implementation for put and putIfAbsent */
992 >    final V putVal(K key, V value, boolean onlyIfAbsent) {
993 >        if (key == null || value == null) throw new NullPointerException();
994 >        int hash = spread(key.hashCode());
995 >        int binCount = 0;
996 >        for (Node<K,V>[] tab = table;;) {
997 >            Node<K,V> f; int n, i, fh;
998 >            if (tab == null || (n = tab.length) == 0)
999 >                tab = initTable();
1000 >            else if ((f = tabAt(tab, i = (n - 1) & hash)) == null) {
1001 >                if (casTabAt(tab, i, null,
1002 >                             new Node<K,V>(hash, key, value, null)))
1003 >                    break;                   // no lock when adding to empty bin
1004 >            }
1005 >            else if ((fh = f.hash) == MOVED)
1006 >                tab = helpTransfer(tab, f);
1007 >            else {
1008 >                V oldVal = null;
1009 >                synchronized (f) {
1010 >                    if (tabAt(tab, i) == f) {
1011 >                        if (fh >= 0) {
1012 >                            binCount = 1;
1013 >                            for (Node<K,V> e = f;; ++binCount) {
1014 >                                K ek;
1015 >                                if (e.hash == hash &&
1016 >                                    ((ek = e.key) == key ||
1017 >                                     (ek != null && key.equals(ek)))) {
1018 >                                    oldVal = e.val;
1019 >                                    if (!onlyIfAbsent)
1020 >                                        e.val = value;
1021 >                                    break;
1022 >                                }
1023 >                                Node<K,V> pred = e;
1024 >                                if ((e = e.next) == null) {
1025 >                                    pred.next = new Node<K,V>(hash, key,
1026 >                                                              value, null);
1027 >                                    break;
1028 >                                }
1029 >                            }
1030 >                        }
1031 >                        else if (f instanceof TreeBin) {
1032 >                            Node<K,V> p;
1033 >                            binCount = 2;
1034 >                            if ((p = ((TreeBin<K,V>)f).putTreeVal(hash, key,
1035 >                                                           value)) != null) {
1036 >                                oldVal = p.val;
1037 >                                if (!onlyIfAbsent)
1038 >                                    p.val = value;
1039 >                            }
1040 >                        }
1041 >                    }
1042 >                }
1043 >                if (binCount != 0) {
1044 >                    if (binCount >= TREEIFY_THRESHOLD)
1045 >                        treeifyBin(tab, i);
1046 >                    if (oldVal != null)
1047 >                        return oldVal;
1048 >                    break;
1049 >                }
1050 >            }
1051 >        }
1052 >        addCount(1L, binCount);
1053 >        return null;
1054      }
1055  
1056      /**
# Line 2589 | Line 1061 | public class ConcurrentHashMapV8<K, V>
1061       * @param m mappings to be stored in this map
1062       */
1063      public void putAll(Map<? extends K, ? extends V> m) {
1064 <        internalPutAll(m);
1065 <    }
1066 <
2595 <    /**
2596 <     * If the specified key is not already associated with a value,
2597 <     * computes its value using the given mappingFunction and enters
2598 <     * it into the map unless null.  This is equivalent to
2599 <     * <pre> {@code
2600 <     * if (map.containsKey(key))
2601 <     *   return map.get(key);
2602 <     * value = mappingFunction.apply(key);
2603 <     * if (value != null)
2604 <     *   map.put(key, value);
2605 <     * return value;}</pre>
2606 <     *
2607 <     * except that the action is performed atomically.  If the
2608 <     * function returns {@code null} no mapping is recorded. If the
2609 <     * function itself throws an (unchecked) exception, the exception
2610 <     * is rethrown to its caller, and no mapping is recorded.  Some
2611 <     * attempted update operations on this map by other threads may be
2612 <     * blocked while computation is in progress, so the computation
2613 <     * should be short and simple, and must not attempt to update any
2614 <     * other mappings of this Map. The most appropriate usage is to
2615 <     * construct a new object serving as an initial mapped value, or
2616 <     * memoized result, as in:
2617 <     *
2618 <     *  <pre> {@code
2619 <     * map.computeIfAbsent(key, new Fun<K, V>() {
2620 <     *   public V map(K k) { return new Value(f(k)); }});}</pre>
2621 <     *
2622 <     * @param key key with which the specified value is to be associated
2623 <     * @param mappingFunction the function to compute a value
2624 <     * @return the current (existing or computed) value associated with
2625 <     *         the specified key, or null if the computed value is null.
2626 <     * @throws NullPointerException if the specified key or mappingFunction
2627 <     *         is null
2628 <     * @throws IllegalStateException if the computation detectably
2629 <     *         attempts a recursive update to this map that would
2630 <     *         otherwise never complete
2631 <     * @throws RuntimeException or Error if the mappingFunction does so,
2632 <     *         in which case the mapping is left unestablished
2633 <     */
2634 <    @SuppressWarnings("unchecked") public V computeIfAbsent
2635 <        (K key, Fun<? super K, ? extends V> mappingFunction) {
2636 <        if (key == null || mappingFunction == null)
2637 <            throw new NullPointerException();
2638 <        return (V)internalComputeIfAbsent(key, mappingFunction);
2639 <    }
2640 <
2641 <    /**
2642 <     * If the given key is present, computes a new mapping value given a key and
2643 <     * its current mapped value. This is equivalent to
2644 <     *  <pre> {@code
2645 <     *   if (map.containsKey(key)) {
2646 <     *     value = remappingFunction.apply(key, map.get(key));
2647 <     *     if (value != null)
2648 <     *       map.put(key, value);
2649 <     *     else
2650 <     *       map.remove(key);
2651 <     *   }
2652 <     * }</pre>
2653 <     *
2654 <     * except that the action is performed atomically.  If the
2655 <     * function returns {@code null}, the mapping is removed.  If the
2656 <     * function itself throws an (unchecked) exception, the exception
2657 <     * is rethrown to its caller, and the current mapping is left
2658 <     * unchanged.  Some attempted update operations on this map by
2659 <     * other threads may be blocked while computation is in progress,
2660 <     * so the computation should be short and simple, and must not
2661 <     * attempt to update any other mappings of this Map. For example,
2662 <     * to either create or append new messages to a value mapping:
2663 <     *
2664 <     * @param key key with which the specified value is to be associated
2665 <     * @param remappingFunction the function to compute a value
2666 <     * @return the new value associated with the specified key, or null if none
2667 <     * @throws NullPointerException if the specified key or remappingFunction
2668 <     *         is null
2669 <     * @throws IllegalStateException if the computation detectably
2670 <     *         attempts a recursive update to this map that would
2671 <     *         otherwise never complete
2672 <     * @throws RuntimeException or Error if the remappingFunction does so,
2673 <     *         in which case the mapping is unchanged
2674 <     */
2675 <    @SuppressWarnings("unchecked") public V computeIfPresent
2676 <        (K key, BiFun<? super K, ? super V, ? extends V> remappingFunction) {
2677 <        if (key == null || remappingFunction == null)
2678 <            throw new NullPointerException();
2679 <        return (V)internalCompute(key, true, remappingFunction);
2680 <    }
2681 <
2682 <    /**
2683 <     * Computes a new mapping value given a key and
2684 <     * its current mapped value (or {@code null} if there is no current
2685 <     * mapping). This is equivalent to
2686 <     *  <pre> {@code
2687 <     *   value = remappingFunction.apply(key, map.get(key));
2688 <     *   if (value != null)
2689 <     *     map.put(key, value);
2690 <     *   else
2691 <     *     map.remove(key);
2692 <     * }</pre>
2693 <     *
2694 <     * except that the action is performed atomically.  If the
2695 <     * function returns {@code null}, the mapping is removed.  If the
2696 <     * function itself throws an (unchecked) exception, the exception
2697 <     * is rethrown to its caller, and the current mapping is left
2698 <     * unchanged.  Some attempted update operations on this map by
2699 <     * other threads may be blocked while computation is in progress,
2700 <     * so the computation should be short and simple, and must not
2701 <     * attempt to update any other mappings of this Map. For example,
2702 <     * to either create or append new messages to a value mapping:
2703 <     *
2704 <     * <pre> {@code
2705 <     * Map<Key, String> map = ...;
2706 <     * final String msg = ...;
2707 <     * map.compute(key, new BiFun<Key, String, String>() {
2708 <     *   public String apply(Key k, String v) {
2709 <     *    return (v == null) ? msg : v + msg;});}}</pre>
2710 <     *
2711 <     * @param key key with which the specified value is to be associated
2712 <     * @param remappingFunction the function to compute a value
2713 <     * @return the new value associated with the specified key, or null if none
2714 <     * @throws NullPointerException if the specified key or remappingFunction
2715 <     *         is null
2716 <     * @throws IllegalStateException if the computation detectably
2717 <     *         attempts a recursive update to this map that would
2718 <     *         otherwise never complete
2719 <     * @throws RuntimeException or Error if the remappingFunction does so,
2720 <     *         in which case the mapping is unchanged
2721 <     */
2722 <    @SuppressWarnings("unchecked") public V compute
2723 <        (K key, BiFun<? super K, ? super V, ? extends V> remappingFunction) {
2724 <        if (key == null || remappingFunction == null)
2725 <            throw new NullPointerException();
2726 <        return (V)internalCompute(key, false, remappingFunction);
2727 <    }
2728 <
2729 <    /**
2730 <     * If the specified key is not already associated
2731 <     * with a value, associate it with the given value.
2732 <     * Otherwise, replace the value with the results of
2733 <     * the given remapping function. This is equivalent to:
2734 <     *  <pre> {@code
2735 <     *   if (!map.containsKey(key))
2736 <     *     map.put(value);
2737 <     *   else {
2738 <     *     newValue = remappingFunction.apply(map.get(key), value);
2739 <     *     if (value != null)
2740 <     *       map.put(key, value);
2741 <     *     else
2742 <     *       map.remove(key);
2743 <     *   }
2744 <     * }</pre>
2745 <     * except that the action is performed atomically.  If the
2746 <     * function returns {@code null}, the mapping is removed.  If the
2747 <     * function itself throws an (unchecked) exception, the exception
2748 <     * is rethrown to its caller, and the current mapping is left
2749 <     * unchanged.  Some attempted update operations on this map by
2750 <     * other threads may be blocked while computation is in progress,
2751 <     * so the computation should be short and simple, and must not
2752 <     * attempt to update any other mappings of this Map.
2753 <     */
2754 <    @SuppressWarnings("unchecked") public V merge
2755 <        (K key, V value, BiFun<? super V, ? super V, ? extends V> remappingFunction) {
2756 <        if (key == null || value == null || remappingFunction == null)
2757 <            throw new NullPointerException();
2758 <        return (V)internalMerge(key, value, remappingFunction);
1064 >        tryPresize(m.size());
1065 >        for (Map.Entry<? extends K, ? extends V> e : m.entrySet())
1066 >            putVal(e.getKey(), e.getValue(), false);
1067      }
1068  
1069      /**
# Line 2767 | Line 1075 | public class ConcurrentHashMapV8<K, V>
1075       *         {@code null} if there was no mapping for {@code key}
1076       * @throws NullPointerException if the specified key is null
1077       */
1078 <    @SuppressWarnings("unchecked") public V remove(Object key) {
1079 <        if (key == null)
2772 <            throw new NullPointerException();
2773 <        return (V)internalReplace(key, null, null);
1078 >    public V remove(Object key) {
1079 >        return replaceNode(key, null, null);
1080      }
1081  
1082      /**
1083 <     * {@inheritDoc}
1084 <     *
1085 <     * @throws NullPointerException if the specified key is null
2780 <     */
2781 <    public boolean remove(Object key, Object value) {
2782 <        if (key == null)
2783 <            throw new NullPointerException();
2784 <        if (value == null)
2785 <            return false;
2786 <        return internalReplace(key, null, value) != null;
2787 <    }
2788 <
2789 <    /**
2790 <     * {@inheritDoc}
2791 <     *
2792 <     * @throws NullPointerException if any of the arguments are null
2793 <     */
2794 <    public boolean replace(K key, V oldValue, V newValue) {
2795 <        if (key == null || oldValue == null || newValue == null)
2796 <            throw new NullPointerException();
2797 <        return internalReplace(key, newValue, oldValue) != null;
2798 <    }
2799 <
2800 <    /**
2801 <     * {@inheritDoc}
2802 <     *
2803 <     * @return the previous value associated with the specified key,
2804 <     *         or {@code null} if there was no mapping for the key
2805 <     * @throws NullPointerException if the specified key or value is null
1083 >     * Implementation for the four public remove/replace methods:
1084 >     * Replaces node value with v, conditional upon match of cv if
1085 >     * non-null.  If resulting value is null, delete.
1086       */
1087 <    @SuppressWarnings("unchecked") public V replace(K key, V value) {
1088 <        if (key == null || value == null)
1089 <            throw new NullPointerException();
1090 <        return (V)internalReplace(key, value, null);
1087 >    final V replaceNode(Object key, V value, Object cv) {
1088 >        int hash = spread(key.hashCode());
1089 >        for (Node<K,V>[] tab = table;;) {
1090 >            Node<K,V> f; int n, i, fh;
1091 >            if (tab == null || (n = tab.length) == 0 ||
1092 >                (f = tabAt(tab, i = (n - 1) & hash)) == null)
1093 >                break;
1094 >            else if ((fh = f.hash) == MOVED)
1095 >                tab = helpTransfer(tab, f);
1096 >            else {
1097 >                V oldVal = null;
1098 >                boolean validated = false;
1099 >                synchronized (f) {
1100 >                    if (tabAt(tab, i) == f) {
1101 >                        if (fh >= 0) {
1102 >                            validated = true;
1103 >                            for (Node<K,V> e = f, pred = null;;) {
1104 >                                K ek;
1105 >                                if (e.hash == hash &&
1106 >                                    ((ek = e.key) == key ||
1107 >                                     (ek != null && key.equals(ek)))) {
1108 >                                    V ev = e.val;
1109 >                                    if (cv == null || cv == ev ||
1110 >                                        (ev != null && cv.equals(ev))) {
1111 >                                        oldVal = ev;
1112 >                                        if (value != null)
1113 >                                            e.val = value;
1114 >                                        else if (pred != null)
1115 >                                            pred.next = e.next;
1116 >                                        else
1117 >                                            setTabAt(tab, i, e.next);
1118 >                                    }
1119 >                                    break;
1120 >                                }
1121 >                                pred = e;
1122 >                                if ((e = e.next) == null)
1123 >                                    break;
1124 >                            }
1125 >                        }
1126 >                        else if (f instanceof TreeBin) {
1127 >                            validated = true;
1128 >                            TreeBin<K,V> t = (TreeBin<K,V>)f;
1129 >                            TreeNode<K,V> r, p;
1130 >                            if ((r = t.root) != null &&
1131 >                                (p = r.findTreeNode(hash, key, null)) != null) {
1132 >                                V pv = p.val;
1133 >                                if (cv == null || cv == pv ||
1134 >                                    (pv != null && cv.equals(pv))) {
1135 >                                    oldVal = pv;
1136 >                                    if (value != null)
1137 >                                        p.val = value;
1138 >                                    else if (t.removeTreeNode(p))
1139 >                                        setTabAt(tab, i, untreeify(t.first));
1140 >                                }
1141 >                            }
1142 >                        }
1143 >                    }
1144 >                }
1145 >                if (validated) {
1146 >                    if (oldVal != null) {
1147 >                        if (value == null)
1148 >                            addCount(-1L, -1);
1149 >                        return oldVal;
1150 >                    }
1151 >                    break;
1152 >                }
1153 >            }
1154 >        }
1155 >        return null;
1156      }
1157  
1158      /**
1159       * Removes all of the mappings from this map.
1160       */
1161      public void clear() {
1162 <        internalClear();
1162 >        long delta = 0L; // negative number of deletions
1163 >        int i = 0;
1164 >        Node<K,V>[] tab = table;
1165 >        while (tab != null && i < tab.length) {
1166 >            int fh;
1167 >            Node<K,V> f = tabAt(tab, i);
1168 >            if (f == null)
1169 >                ++i;
1170 >            else if ((fh = f.hash) == MOVED) {
1171 >                tab = helpTransfer(tab, f);
1172 >                i = 0; // restart
1173 >            }
1174 >            else {
1175 >                synchronized (f) {
1176 >                    if (tabAt(tab, i) == f) {
1177 >                        Node<K,V> p = (fh >= 0 ? f :
1178 >                                       (f instanceof TreeBin) ?
1179 >                                       ((TreeBin<K,V>)f).first : null);
1180 >                        while (p != null) {
1181 >                            --delta;
1182 >                            p = p.next;
1183 >                        }
1184 >                        setTabAt(tab, i++, null);
1185 >                    }
1186 >                }
1187 >            }
1188 >        }
1189 >        if (delta != 0L)
1190 >            addCount(delta, -1);
1191      }
1192  
1193      /**
1194       * Returns a {@link Set} view of the keys contained in this map.
1195       * The set is backed by the map, so changes to the map are
1196 <     * reflected in the set, and vice-versa.  The set supports element
1196 >     * reflected in the set, and vice-versa. The set supports element
1197       * removal, which removes the corresponding mapping from this map,
1198       * via the {@code Iterator.remove}, {@code Set.remove},
1199       * {@code removeAll}, {@code retainAll}, and {@code clear}
# Line 2832 | Line 1205 | public class ConcurrentHashMapV8<K, V>
1205       * and guarantees to traverse elements as they existed upon
1206       * construction of the iterator, and may (but is not guaranteed to)
1207       * reflect any modifications subsequent to construction.
1208 +     *
1209 +     * @return the set view
1210       */
1211 <    public Set<K> keySet() {
1212 <        KeySet<K,V> ks = keySet;
1213 <        return (ks != null) ? ks : (keySet = new KeySet<K,V>(this));
1211 >    public KeySetView<K,V> keySet() {
1212 >        KeySetView<K,V> ks;
1213 >        return (ks = keySet) != null ? ks : (keySet = new KeySetView<K,V>(this, null));
1214      }
1215  
1216      /**
# Line 2853 | Line 1228 | public class ConcurrentHashMapV8<K, V>
1228       * and guarantees to traverse elements as they existed upon
1229       * construction of the iterator, and may (but is not guaranteed to)
1230       * reflect any modifications subsequent to construction.
1231 +     *
1232 +     * @return the collection view
1233       */
1234      public Collection<V> values() {
1235 <        Values<K,V> vs = values;
1236 <        return (vs != null) ? vs : (values = new Values<K,V>(this));
1235 >        ValuesView<K,V> vs;
1236 >        return (vs = values) != null ? vs : (values = new ValuesView<K,V>(this));
1237      }
1238  
1239      /**
# Line 2866 | Line 1243 | public class ConcurrentHashMapV8<K, V>
1243       * removal, which removes the corresponding mapping from the map,
1244       * via the {@code Iterator.remove}, {@code Set.remove},
1245       * {@code removeAll}, {@code retainAll}, and {@code clear}
1246 <     * operations.  It does not support the {@code add} or
2870 <     * {@code addAll} operations.
1246 >     * operations.
1247       *
1248       * <p>The view's {@code iterator} is a "weakly consistent" iterator
1249       * that will never throw {@link ConcurrentModificationException},
1250       * and guarantees to traverse elements as they existed upon
1251       * construction of the iterator, and may (but is not guaranteed to)
1252       * reflect any modifications subsequent to construction.
2877     */
2878    public Set<Map.Entry<K,V>> entrySet() {
2879        EntrySet<K,V> es = entrySet;
2880        return (es != null) ? es : (entrySet = new EntrySet<K,V>(this));
2881    }
2882
2883    /**
2884     * Returns an enumeration of the keys in this table.
2885     *
2886     * @return an enumeration of the keys in this table
2887     * @see #keySet()
2888     */
2889    public Enumeration<K> keys() {
2890        return new KeyIterator<K,V>(this);
2891    }
2892
2893    /**
2894     * Returns an enumeration of the values in this table.
1253       *
1254 <     * @return an enumeration of the values in this table
2897 <     * @see #values()
1254 >     * @return the set view
1255       */
1256 <    public Enumeration<V> elements() {
1257 <        return new ValueIterator<K,V>(this);
1258 <    }
2902 <
2903 <    /**
2904 <     * Returns a partitionable iterator of the keys in this map.
2905 <     *
2906 <     * @return a partitionable iterator of the keys in this map
2907 <     */
2908 <    public Spliterator<K> keySpliterator() {
2909 <        return new KeyIterator<K,V>(this);
2910 <    }
2911 <
2912 <    /**
2913 <     * Returns a partitionable iterator of the values in this map.
2914 <     *
2915 <     * @return a partitionable iterator of the values in this map
2916 <     */
2917 <    public Spliterator<V> valueSpliterator() {
2918 <        return new ValueIterator<K,V>(this);
2919 <    }
2920 <
2921 <    /**
2922 <     * Returns a partitionable iterator of the entries in this map.
2923 <     *
2924 <     * @return a partitionable iterator of the entries in this map
2925 <     */
2926 <    public Spliterator<Map.Entry<K,V>> entrySpliterator() {
2927 <        return new EntryIterator<K,V>(this);
1256 >    public Set<Map.Entry<K,V>> entrySet() {
1257 >        EntrySetView<K,V> es;
1258 >        return (es = entrySet) != null ? es : (entrySet = new EntrySetView<K,V>(this));
1259      }
1260  
1261      /**
# Line 2936 | Line 1267 | public class ConcurrentHashMapV8<K, V>
1267       */
1268      public int hashCode() {
1269          int h = 0;
1270 <        Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
1271 <        Object v;
1272 <        while ((v = it.advance()) != null) {
1273 <            h += it.nextKey.hashCode() ^ v.hashCode();
1270 >        Node<K,V>[] t;
1271 >        if ((t = table) != null) {
1272 >            Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length);
1273 >            for (Node<K,V> p; (p = it.advance()) != null; )
1274 >                h += p.key.hashCode() ^ p.val.hashCode();
1275          }
1276          return h;
1277      }
# Line 2956 | Line 1288 | public class ConcurrentHashMapV8<K, V>
1288       * @return a string representation of this map
1289       */
1290      public String toString() {
1291 <        Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
1291 >        Node<K,V>[] t;
1292 >        int f = (t = table) == null ? 0 : t.length;
1293 >        Traverser<K,V> it = new Traverser<K,V>(t, f, 0, f);
1294          StringBuilder sb = new StringBuilder();
1295          sb.append('{');
1296 <        Object v;
1297 <        if ((v = it.advance()) != null) {
1296 >        Node<K,V> p;
1297 >        if ((p = it.advance()) != null) {
1298              for (;;) {
1299 <                Object k = it.nextKey;
1299 >                K k = p.key;
1300 >                V v = p.val;
1301                  sb.append(k == this ? "(this Map)" : k);
1302                  sb.append('=');
1303                  sb.append(v == this ? "(this Map)" : v);
1304 <                if ((v = it.advance()) == null)
1304 >                if ((p = it.advance()) == null)
1305                      break;
1306                  sb.append(',').append(' ');
1307              }
# Line 2989 | Line 1324 | public class ConcurrentHashMapV8<K, V>
1324              if (!(o instanceof Map))
1325                  return false;
1326              Map<?,?> m = (Map<?,?>) o;
1327 <            Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
1328 <            Object val;
1329 <            while ((val = it.advance()) != null) {
1330 <                Object v = m.get(it.nextKey);
1327 >            Node<K,V>[] t;
1328 >            int f = (t = table) == null ? 0 : t.length;
1329 >            Traverser<K,V> it = new Traverser<K,V>(t, f, 0, f);
1330 >            for (Node<K,V> p; (p = it.advance()) != null; ) {
1331 >                V val = p.val;
1332 >                Object v = m.get(p.key);
1333                  if (v == null || (v != val && !v.equals(val)))
1334                      return false;
1335              }
# Line 3000 | Line 1337 | public class ConcurrentHashMapV8<K, V>
1337                  Object mk, mv, v;
1338                  if ((mk = e.getKey()) == null ||
1339                      (mv = e.getValue()) == null ||
1340 <                    (v = internalGet(mk)) == null ||
1340 >                    (v = get(mk)) == null ||
1341                      (mv != v && !mv.equals(v)))
1342                      return false;
1343              }
# Line 3008 | Line 1345 | public class ConcurrentHashMapV8<K, V>
1345          return true;
1346      }
1347  
1348 <    /* ----------------Iterators -------------- */
1348 >    /**
1349 >     * Stripped-down version of helper class used in previous version,
1350 >     * declared for the sake of serialization compatibility
1351 >     */
1352 >    static class Segment<K,V> extends ReentrantLock implements Serializable {
1353 >        private static final long serialVersionUID = 2249069246763182397L;
1354 >        final float loadFactor;
1355 >        Segment(float lf) { this.loadFactor = lf; }
1356 >    }
1357  
1358 <    @SuppressWarnings("serial") 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) {
1362 <            super(it);
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);
1368 <        }
1369 <        @SuppressWarnings("unchecked") public final K next() {
1370 <            if (nextVal == null && advance() == null)
1371 <                throw new NoSuchElementException();
1372 <            Object k = nextKey;
1373 <            nextVal = null;
1374 <            return (K) k;
1358 >    /**
1359 >     * Saves the state of the {@code ConcurrentHashMapV8} instance to a
1360 >     * stream (i.e., serializes it).
1361 >     * @param s the stream
1362 >     * @serialData
1363 >     * the key (Object) and value (Object)
1364 >     * for each key-value mapping, followed by a null pair.
1365 >     * The key-value mappings are emitted in no particular order.
1366 >     */
1367 >    private void writeObject(java.io.ObjectOutputStream s)
1368 >        throws java.io.IOException {
1369 >        // For serialization compatibility
1370 >        // Emulate segment calculation from previous version of this class
1371 >        int sshift = 0;
1372 >        int ssize = 1;
1373 >        while (ssize < DEFAULT_CONCURRENCY_LEVEL) {
1374 >            ++sshift;
1375 >            ssize <<= 1;
1376 >        }
1377 >        int segmentShift = 32 - sshift;
1378 >        int segmentMask = ssize - 1;
1379 >        @SuppressWarnings("unchecked") Segment<K,V>[] segments = (Segment<K,V>[])
1380 >            new Segment<?,?>[DEFAULT_CONCURRENCY_LEVEL];
1381 >        for (int i = 0; i < segments.length; ++i)
1382 >            segments[i] = new Segment<K,V>(LOAD_FACTOR);
1383 >        s.putFields().put("segments", segments);
1384 >        s.putFields().put("segmentShift", segmentShift);
1385 >        s.putFields().put("segmentMask", segmentMask);
1386 >        s.writeFields();
1387 >
1388 >        Node<K,V>[] t;
1389 >        if ((t = table) != null) {
1390 >            Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length);
1391 >            for (Node<K,V> p; (p = it.advance()) != null; ) {
1392 >                s.writeObject(p.key);
1393 >                s.writeObject(p.val);
1394 >            }
1395          }
1396 <
1397 <        public final K nextElement() { return next(); }
1396 >        s.writeObject(null);
1397 >        s.writeObject(null);
1398 >        segments = null; // throw away
1399      }
1400  
1401 <    @SuppressWarnings("serial") 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) {
1405 <            super(it);
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);
1401 >    /**
1402 >     * Reconstitutes the instance from a stream (that is, deserializes it).
1403 >     * @param s the stream
1404 >     */
1405 >    private void readObject(java.io.ObjectInputStream s)
1406 >        throws java.io.IOException, ClassNotFoundException {
1407 >        /*
1408 >         * To improve performance in typical cases, we create nodes
1409 >         * while reading, then place in table once size is known.
1410 >         * However, we must also validate uniqueness and deal with
1411 >         * overpopulated bins while doing so, which requires
1412 >         * specialized versions of putVal mechanics.
1413 >         */
1414 >        sizeCtl = -1; // force exclusion for table construction
1415 >        s.defaultReadObject();
1416 >        long size = 0L;
1417 >        Node<K,V> p = null;
1418 >        for (;;) {
1419 >            @SuppressWarnings("unchecked") K k = (K) s.readObject();
1420 >            @SuppressWarnings("unchecked") V v = (V) s.readObject();
1421 >            if (k != null && v != null) {
1422 >                p = new Node<K,V>(spread(k.hashCode()), k, v, p);
1423 >                ++size;
1424 >            }
1425 >            else
1426 >                break;
1427          }
1428 <
1429 <        @SuppressWarnings("unchecked") 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;
1428 >        if (size == 0L)
1429 >            sizeCtl = 0;
1430 >        else {
1431 >            int n;
1432 >            if (size >= (long)(MAXIMUM_CAPACITY >>> 1))
1433 >                n = MAXIMUM_CAPACITY;
1434 >            else {
1435 >                int sz = (int)size;
1436 >                n = tableSizeFor(sz + (sz >>> 1) + 1);
1437 >            }
1438 >            @SuppressWarnings({"rawtypes","unchecked"})
1439 >                Node<K,V>[] tab = (Node<K,V>[])new Node[n];
1440 >            int mask = n - 1;
1441 >            long added = 0L;
1442 >            while (p != null) {
1443 >                boolean insertAtFront;
1444 >                Node<K,V> next = p.next, first;
1445 >                int h = p.hash, j = h & mask;
1446 >                if ((first = tabAt(tab, j)) == null)
1447 >                    insertAtFront = true;
1448 >                else {
1449 >                    K k = p.key;
1450 >                    if (first.hash < 0) {
1451 >                        TreeBin<K,V> t = (TreeBin<K,V>)first;
1452 >                        if (t.putTreeVal(h, k, p.val) == null)
1453 >                            ++added;
1454 >                        insertAtFront = false;
1455 >                    }
1456 >                    else {
1457 >                        int binCount = 0;
1458 >                        insertAtFront = true;
1459 >                        Node<K,V> q; K qk;
1460 >                        for (q = first; q != null; q = q.next) {
1461 >                            if (q.hash == h &&
1462 >                                ((qk = q.key) == k ||
1463 >                                 (qk != null && k.equals(qk)))) {
1464 >                                insertAtFront = false;
1465 >                                break;
1466 >                            }
1467 >                            ++binCount;
1468 >                        }
1469 >                        if (insertAtFront && binCount >= TREEIFY_THRESHOLD) {
1470 >                            insertAtFront = false;
1471 >                            ++added;
1472 >                            p.next = first;
1473 >                            TreeNode<K,V> hd = null, tl = null;
1474 >                            for (q = p; q != null; q = q.next) {
1475 >                                TreeNode<K,V> t = new TreeNode<K,V>
1476 >                                    (q.hash, q.key, q.val, null, null);
1477 >                                if ((t.prev = tl) == null)
1478 >                                    hd = t;
1479 >                                else
1480 >                                    tl.next = t;
1481 >                                tl = t;
1482 >                            }
1483 >                            setTabAt(tab, j, new TreeBin<K,V>(hd));
1484 >                        }
1485 >                    }
1486 >                }
1487 >                if (insertAtFront) {
1488 >                    ++added;
1489 >                    p.next = first;
1490 >                    setTabAt(tab, j, p);
1491 >                }
1492 >                p = next;
1493 >            }
1494 >            table = tab;
1495 >            sizeCtl = n - (n >>> 2);
1496 >            baseCount = added;
1497          }
3054
3055        public final V nextElement() { return next(); }
1498      }
1499  
1500 <    @SuppressWarnings("serial") static final class EntryIterator<K,V> extends Traverser<K,V,Object>
3059 <        implements Spliterator<Map.Entry<K,V>> {
3060 <        EntryIterator(ConcurrentHashMapV8<K, V> map) { super(map); }
3061 <        EntryIterator(Traverser<K,V,Object> it) {
3062 <            super(it);
3063 <        }
3064 <        public EntryIterator<K,V> split() {
3065 <            if (last != null || (next != null && nextVal == null))
3066 <                throw new IllegalStateException();
3067 <            return new EntryIterator<K,V>(this);
3068 <        }
1500 >    // ConcurrentMap methods
1501  
1502 <        @SuppressWarnings("unchecked") 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 <        }
1502 >    /**
1503 >     * {@inheritDoc}
1504 >     *
1505 >     * @return the previous value associated with the specified key,
1506 >     *         or {@code null} if there was no mapping for the key
1507 >     * @throws NullPointerException if the specified key or value is null
1508 >     */
1509 >    public V putIfAbsent(K key, V value) {
1510 >        return putVal(key, value, true);
1511      }
1512  
1513      /**
1514 <     * Exported Entry for iterators
1514 >     * {@inheritDoc}
1515 >     *
1516 >     * @throws NullPointerException if the specified key is null
1517       */
1518 <    static final class MapEntry<K,V> implements Map.Entry<K, V> {
1519 <        final K key; // non-null
1520 <        V val;       // non-null
1521 <        final ConcurrentHashMapV8<K, V> map;
3087 <        MapEntry(K key, V val, ConcurrentHashMapV8<K, V> map) {
3088 <            this.key = key;
3089 <            this.val = val;
3090 <            this.map = map;
3091 <        }
3092 <        public final K getKey()       { return key; }
3093 <        public final V getValue()     { return val; }
3094 <        public final int hashCode()   { return key.hashCode() ^ val.hashCode(); }
3095 <        public final String toString(){ return key + "=" + val; }
3096 <
3097 <        public final boolean equals(Object o) {
3098 <            Object k, v; Map.Entry<?,?> e;
3099 <            return ((o instanceof Map.Entry) &&
3100 <                    (k = (e = (Map.Entry<?,?>)o).getKey()) != null &&
3101 <                    (v = e.getValue()) != null &&
3102 <                    (k == key || k.equals(key)) &&
3103 <                    (v == val || v.equals(val)));
3104 <        }
3105 <
3106 <        /**
3107 <         * Sets our entry's value and writes through to the map. The
3108 <         * value to return is somewhat arbitrary here. Since we do not
3109 <         * necessarily track asynchronous changes, the most recent
3110 <         * "previous" value could be different from what we return (or
3111 <         * could even have been removed in which case the put will
3112 <         * re-establish). We do not and cannot guarantee more.
3113 <         */
3114 <        public final V setValue(V value) {
3115 <            if (value == null) throw new NullPointerException();
3116 <            V v = val;
3117 <            val = value;
3118 <            map.put(key, value);
3119 <            return v;
3120 <        }
1518 >    public boolean remove(Object key, Object value) {
1519 >        if (key == null)
1520 >            throw new NullPointerException();
1521 >        return value != null && replaceNode(key, null, value) != null;
1522      }
1523  
1524 <    /* ----------------Views -------------- */
1524 >    /**
1525 >     * {@inheritDoc}
1526 >     *
1527 >     * @throws NullPointerException if any of the arguments are null
1528 >     */
1529 >    public boolean replace(K key, V oldValue, V newValue) {
1530 >        if (key == null || oldValue == null || newValue == null)
1531 >            throw new NullPointerException();
1532 >        return replaceNode(key, newValue, oldValue) != null;
1533 >    }
1534  
1535      /**
1536 <     * Base class for views.
1536 >     * {@inheritDoc}
1537 >     *
1538 >     * @return the previous value associated with the specified key,
1539 >     *         or {@code null} if there was no mapping for the key
1540 >     * @throws NullPointerException if the specified key or value is null
1541       */
1542 <    static abstract class CHMView<K, V> {
1543 <        final ConcurrentHashMapV8<K, V> map;
1544 <        CHMView(ConcurrentHashMapV8<K, V> map)  { this.map = map; }
1545 <        public final int size()                 { return map.size(); }
1546 <        public final boolean isEmpty()          { return map.isEmpty(); }
3133 <        public final void clear()               { map.clear(); }
1542 >    public V replace(K key, V value) {
1543 >        if (key == null || value == null)
1544 >            throw new NullPointerException();
1545 >        return replaceNode(key, value, null);
1546 >    }
1547  
1548 <        // implementations below rely on concrete classes supplying these
3136 <        abstract public Iterator<?> iterator();
3137 <        abstract public boolean contains(Object o);
3138 <        abstract public boolean remove(Object o);
1548 >    // Overrides of JDK8+ Map extension method defaults
1549  
1550 <        private static final String oomeMsg = "Required array size too large";
1550 >    /**
1551 >     * Returns the value to which the specified key is mapped, or the
1552 >     * given default value if this map contains no mapping for the
1553 >     * key.
1554 >     *
1555 >     * @param key the key whose associated value is to be returned
1556 >     * @param defaultValue the value to return if this map contains
1557 >     * no mapping for the given key
1558 >     * @return the mapping for the key, if present; else the default value
1559 >     * @throws NullPointerException if the specified key is null
1560 >     */
1561 >    public V getOrDefault(Object key, V defaultValue) {
1562 >        V v;
1563 >        return (v = get(key)) == null ? defaultValue : v;
1564 >    }
1565 >
1566 >    public void forEach(BiAction<? super K, ? super V> action) {
1567 >        if (action == null) throw new NullPointerException();
1568 >        Node<K,V>[] t;
1569 >        if ((t = table) != null) {
1570 >            Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length);
1571 >            for (Node<K,V> p; (p = it.advance()) != null; ) {
1572 >                action.apply(p.key, p.val);
1573 >            }
1574 >        }
1575 >    }
1576  
1577 <        public final Object[] toArray() {
1578 <            long sz = map.mappingCount();
1579 <            if (sz > (long)(MAX_ARRAY_SIZE))
1580 <                throw new OutOfMemoryError(oomeMsg);
1581 <            int n = (int)sz;
1582 <            Object[] r = new Object[n];
1583 <            int i = 0;
1584 <            Iterator<?> it = iterator();
1585 <            while (it.hasNext()) {
1586 <                if (i == n) {
1587 <                    if (n >= MAX_ARRAY_SIZE)
1588 <                        throw new OutOfMemoryError(oomeMsg);
1589 <                    if (n >= MAX_ARRAY_SIZE - (MAX_ARRAY_SIZE >>> 1) - 1)
1590 <                        n = MAX_ARRAY_SIZE;
3156 <                    else
3157 <                        n += (n >>> 1) + 1;
3158 <                    r = Arrays.copyOf(r, n);
1577 >    public void replaceAll(BiFun<? super K, ? super V, ? extends V> function) {
1578 >        if (function == null) throw new NullPointerException();
1579 >        Node<K,V>[] t;
1580 >        if ((t = table) != null) {
1581 >            Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length);
1582 >            for (Node<K,V> p; (p = it.advance()) != null; ) {
1583 >                V oldValue = p.val;
1584 >                for (K key = p.key;;) {
1585 >                    V newValue = function.apply(key, oldValue);
1586 >                    if (newValue == null)
1587 >                        throw new NullPointerException();
1588 >                    if (replaceNode(key, newValue, oldValue) != null ||
1589 >                        (oldValue = get(key)) == null)
1590 >                        break;
1591                  }
3160                r[i++] = it.next();
1592              }
3162            return (i == n) ? r : Arrays.copyOf(r, i);
1593          }
1594 +    }
1595  
1596 <        @SuppressWarnings("unchecked") 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);
1596 >    /**
1597 >     * If the specified key is not already associated with a value,
1598 >     * attempts to compute its value using the given mapping function
1599 >     * and enters it into this map unless {@code null}.  The entire
1600 >     * method invocation is performed atomically, so the function is
1601 >     * applied at most once per key.  Some attempted update operations
1602 >     * on this map by other threads may be blocked while computation
1603 >     * is in progress, so the computation should be short and simple,
1604 >     * and must not attempt to update any other mappings of this map.
1605 >     *
1606 >     * @param key key with which the specified value is to be associated
1607 >     * @param mappingFunction the function to compute a value
1608 >     * @return the current (existing or computed) value associated with
1609 >     *         the specified key, or null if the computed value is null
1610 >     * @throws NullPointerException if the specified key or mappingFunction
1611 >     *         is null
1612 >     * @throws IllegalStateException if the computation detectably
1613 >     *         attempts a recursive update to this map that would
1614 >     *         otherwise never complete
1615 >     * @throws RuntimeException or Error if the mappingFunction does so,
1616 >     *         in which case the mapping is left unestablished
1617 >     */
1618 >    public V computeIfAbsent(K key, Fun<? super K, ? extends V> mappingFunction) {
1619 >        if (key == null || mappingFunction == null)
1620 >            throw new NullPointerException();
1621 >        int h = spread(key.hashCode());
1622 >        V val = null;
1623 >        int binCount = 0;
1624 >        for (Node<K,V>[] tab = table;;) {
1625 >            Node<K,V> f; int n, i, fh;
1626 >            if (tab == null || (n = tab.length) == 0)
1627 >                tab = initTable();
1628 >            else if ((f = tabAt(tab, i = (n - 1) & h)) == null) {
1629 >                Node<K,V> r = new ReservationNode<K,V>();
1630 >                synchronized (r) {
1631 >                    if (casTabAt(tab, i, null, r)) {
1632 >                        binCount = 1;
1633 >                        Node<K,V> node = null;
1634 >                        try {
1635 >                            if ((val = mappingFunction.apply(key)) != null)
1636 >                                node = new Node<K,V>(h, key, val, null);
1637 >                        } finally {
1638 >                            setTabAt(tab, i, node);
1639 >                        }
1640 >                    }
1641                  }
1642 <                r[i++] = (T)it.next();
1642 >                if (binCount != 0)
1643 >                    break;
1644              }
1645 <            if (a == r && i < n) {
1646 <                r[i] = null; // null-terminate
1647 <                return r;
1645 >            else if ((fh = f.hash) == MOVED)
1646 >                tab = helpTransfer(tab, f);
1647 >            else {
1648 >                boolean added = false;
1649 >                synchronized (f) {
1650 >                    if (tabAt(tab, i) == f) {
1651 >                        if (fh >= 0) {
1652 >                            binCount = 1;
1653 >                            for (Node<K,V> e = f;; ++binCount) {
1654 >                                K ek; V ev;
1655 >                                if (e.hash == h &&
1656 >                                    ((ek = e.key) == key ||
1657 >                                     (ek != null && key.equals(ek)))) {
1658 >                                    val = e.val;
1659 >                                    break;
1660 >                                }
1661 >                                Node<K,V> pred = e;
1662 >                                if ((e = e.next) == null) {
1663 >                                    if ((val = mappingFunction.apply(key)) != null) {
1664 >                                        added = true;
1665 >                                        pred.next = new Node<K,V>(h, key, val, null);
1666 >                                    }
1667 >                                    break;
1668 >                                }
1669 >                            }
1670 >                        }
1671 >                        else if (f instanceof TreeBin) {
1672 >                            binCount = 2;
1673 >                            TreeBin<K,V> t = (TreeBin<K,V>)f;
1674 >                            TreeNode<K,V> r, p;
1675 >                            if ((r = t.root) != null &&
1676 >                                (p = r.findTreeNode(h, key, null)) != null)
1677 >                                val = p.val;
1678 >                            else if ((val = mappingFunction.apply(key)) != null) {
1679 >                                added = true;
1680 >                                t.putTreeVal(h, key, val);
1681 >                            }
1682 >                        }
1683 >                    }
1684 >                }
1685 >                if (binCount != 0) {
1686 >                    if (binCount >= TREEIFY_THRESHOLD)
1687 >                        treeifyBin(tab, i);
1688 >                    if (!added)
1689 >                        return val;
1690 >                    break;
1691 >                }
1692              }
3192            return (i == n) ? r : Arrays.copyOf(r, i);
3193        }
3194
3195        public final int hashCode() {
3196            int h = 0;
3197            for (Iterator<?> it = iterator(); it.hasNext();)
3198                h += it.next().hashCode();
3199            return h;
1693          }
1694 +        if (val != null)
1695 +            addCount(1L, binCount);
1696 +        return val;
1697 +    }
1698  
1699 <        public final String toString() {
1700 <            StringBuilder sb = new StringBuilder();
1701 <            sb.append('[');
1702 <            Iterator<?> it = iterator();
1703 <            if (it.hasNext()) {
1704 <                for (;;) {
1705 <                    Object e = it.next();
1706 <                    sb.append(e == this ? "(this Collection)" : e);
1707 <                    if (!it.hasNext())
1708 <                        break;
1709 <                    sb.append(',').append(' ');
1699 >    /**
1700 >     * If the value for the specified key is present, attempts to
1701 >     * compute a new mapping given the key and its current mapped
1702 >     * value.  The entire method invocation is performed atomically.
1703 >     * Some attempted update operations on this map by other threads
1704 >     * may be blocked while computation is in progress, so the
1705 >     * computation should be short and simple, and must not attempt to
1706 >     * update any other mappings of this map.
1707 >     *
1708 >     * @param key key with which a value may be associated
1709 >     * @param remappingFunction the function to compute a value
1710 >     * @return the new value associated with the specified key, or null if none
1711 >     * @throws NullPointerException if the specified key or remappingFunction
1712 >     *         is null
1713 >     * @throws IllegalStateException if the computation detectably
1714 >     *         attempts a recursive update to this map that would
1715 >     *         otherwise never complete
1716 >     * @throws RuntimeException or Error if the remappingFunction does so,
1717 >     *         in which case the mapping is unchanged
1718 >     */
1719 >    public V computeIfPresent(K key, BiFun<? super K, ? super V, ? extends V> remappingFunction) {
1720 >        if (key == null || remappingFunction == null)
1721 >            throw new NullPointerException();
1722 >        int h = spread(key.hashCode());
1723 >        V val = null;
1724 >        int delta = 0;
1725 >        int binCount = 0;
1726 >        for (Node<K,V>[] tab = table;;) {
1727 >            Node<K,V> f; int n, i, fh;
1728 >            if (tab == null || (n = tab.length) == 0)
1729 >                tab = initTable();
1730 >            else if ((f = tabAt(tab, i = (n - 1) & h)) == null)
1731 >                break;
1732 >            else if ((fh = f.hash) == MOVED)
1733 >                tab = helpTransfer(tab, f);
1734 >            else {
1735 >                synchronized (f) {
1736 >                    if (tabAt(tab, i) == f) {
1737 >                        if (fh >= 0) {
1738 >                            binCount = 1;
1739 >                            for (Node<K,V> e = f, pred = null;; ++binCount) {
1740 >                                K ek;
1741 >                                if (e.hash == h &&
1742 >                                    ((ek = e.key) == key ||
1743 >                                     (ek != null && key.equals(ek)))) {
1744 >                                    val = remappingFunction.apply(key, e.val);
1745 >                                    if (val != null)
1746 >                                        e.val = val;
1747 >                                    else {
1748 >                                        delta = -1;
1749 >                                        Node<K,V> en = e.next;
1750 >                                        if (pred != null)
1751 >                                            pred.next = en;
1752 >                                        else
1753 >                                            setTabAt(tab, i, en);
1754 >                                    }
1755 >                                    break;
1756 >                                }
1757 >                                pred = e;
1758 >                                if ((e = e.next) == null)
1759 >                                    break;
1760 >                            }
1761 >                        }
1762 >                        else if (f instanceof TreeBin) {
1763 >                            binCount = 2;
1764 >                            TreeBin<K,V> t = (TreeBin<K,V>)f;
1765 >                            TreeNode<K,V> r, p;
1766 >                            if ((r = t.root) != null &&
1767 >                                (p = r.findTreeNode(h, key, null)) != null) {
1768 >                                val = remappingFunction.apply(key, p.val);
1769 >                                if (val != null)
1770 >                                    p.val = val;
1771 >                                else {
1772 >                                    delta = -1;
1773 >                                    if (t.removeTreeNode(p))
1774 >                                        setTabAt(tab, i, untreeify(t.first));
1775 >                                }
1776 >                            }
1777 >                        }
1778 >                    }
1779                  }
1780 +                if (binCount != 0)
1781 +                    break;
1782              }
3215            return sb.append(']').toString();
1783          }
1784 +        if (delta != 0)
1785 +            addCount((long)delta, binCount);
1786 +        return val;
1787 +    }
1788  
1789 <        public final boolean containsAll(Collection<?> c) {
1790 <            if (c != this) {
1791 <                for (Iterator<?> it = c.iterator(); it.hasNext();) {
1792 <                    Object e = it.next();
1793 <                    if (e == null || !contains(e))
1794 <                        return false;
1789 >    /**
1790 >     * Attempts to compute a mapping for the specified key and its
1791 >     * current mapped value (or {@code null} if there is no current
1792 >     * mapping). The entire method invocation is performed atomically.
1793 >     * Some attempted update operations on this map by other threads
1794 >     * may be blocked while computation is in progress, so the
1795 >     * computation should be short and simple, and must not attempt to
1796 >     * update any other mappings of this Map.
1797 >     *
1798 >     * @param key key with which the specified value is to be associated
1799 >     * @param remappingFunction the function to compute a value
1800 >     * @return the new value associated with the specified key, or null if none
1801 >     * @throws NullPointerException if the specified key or remappingFunction
1802 >     *         is null
1803 >     * @throws IllegalStateException if the computation detectably
1804 >     *         attempts a recursive update to this map that would
1805 >     *         otherwise never complete
1806 >     * @throws RuntimeException or Error if the remappingFunction does so,
1807 >     *         in which case the mapping is unchanged
1808 >     */
1809 >    public V compute(K key,
1810 >                     BiFun<? super K, ? super V, ? extends V> remappingFunction) {
1811 >        if (key == null || remappingFunction == null)
1812 >            throw new NullPointerException();
1813 >        int h = spread(key.hashCode());
1814 >        V val = null;
1815 >        int delta = 0;
1816 >        int binCount = 0;
1817 >        for (Node<K,V>[] tab = table;;) {
1818 >            Node<K,V> f; int n, i, fh;
1819 >            if (tab == null || (n = tab.length) == 0)
1820 >                tab = initTable();
1821 >            else if ((f = tabAt(tab, i = (n - 1) & h)) == null) {
1822 >                Node<K,V> r = new ReservationNode<K,V>();
1823 >                synchronized (r) {
1824 >                    if (casTabAt(tab, i, null, r)) {
1825 >                        binCount = 1;
1826 >                        Node<K,V> node = null;
1827 >                        try {
1828 >                            if ((val = remappingFunction.apply(key, null)) != null) {
1829 >                                delta = 1;
1830 >                                node = new Node<K,V>(h, key, val, null);
1831 >                            }
1832 >                        } finally {
1833 >                            setTabAt(tab, i, node);
1834 >                        }
1835 >                    }
1836                  }
1837 +                if (binCount != 0)
1838 +                    break;
1839              }
1840 <            return true;
1841 <        }
1842 <
1843 <        public final boolean removeAll(Collection<?> c) {
1844 <            boolean modified = false;
1845 <            for (Iterator<?> it = iterator(); it.hasNext();) {
1846 <                if (c.contains(it.next())) {
1847 <                    it.remove();
1848 <                    modified = true;
1840 >            else if ((fh = f.hash) == MOVED)
1841 >                tab = helpTransfer(tab, f);
1842 >            else {
1843 >                synchronized (f) {
1844 >                    if (tabAt(tab, i) == f) {
1845 >                        if (fh >= 0) {
1846 >                            binCount = 1;
1847 >                            for (Node<K,V> e = f, pred = null;; ++binCount) {
1848 >                                K ek;
1849 >                                if (e.hash == h &&
1850 >                                    ((ek = e.key) == key ||
1851 >                                     (ek != null && key.equals(ek)))) {
1852 >                                    val = remappingFunction.apply(key, e.val);
1853 >                                    if (val != null)
1854 >                                        e.val = val;
1855 >                                    else {
1856 >                                        delta = -1;
1857 >                                        Node<K,V> en = e.next;
1858 >                                        if (pred != null)
1859 >                                            pred.next = en;
1860 >                                        else
1861 >                                            setTabAt(tab, i, en);
1862 >                                    }
1863 >                                    break;
1864 >                                }
1865 >                                pred = e;
1866 >                                if ((e = e.next) == null) {
1867 >                                    val = remappingFunction.apply(key, null);
1868 >                                    if (val != null) {
1869 >                                        delta = 1;
1870 >                                        pred.next =
1871 >                                            new Node<K,V>(h, key, val, null);
1872 >                                    }
1873 >                                    break;
1874 >                                }
1875 >                            }
1876 >                        }
1877 >                        else if (f instanceof TreeBin) {
1878 >                            binCount = 1;
1879 >                            TreeBin<K,V> t = (TreeBin<K,V>)f;
1880 >                            TreeNode<K,V> r, p;
1881 >                            if ((r = t.root) != null)
1882 >                                p = r.findTreeNode(h, key, null);
1883 >                            else
1884 >                                p = null;
1885 >                            V pv = (p == null) ? null : p.val;
1886 >                            val = remappingFunction.apply(key, pv);
1887 >                            if (val != null) {
1888 >                                if (p != null)
1889 >                                    p.val = val;
1890 >                                else {
1891 >                                    delta = 1;
1892 >                                    t.putTreeVal(h, key, val);
1893 >                                }
1894 >                            }
1895 >                            else if (p != null) {
1896 >                                delta = -1;
1897 >                                if (t.removeTreeNode(p))
1898 >                                    setTabAt(tab, i, untreeify(t.first));
1899 >                            }
1900 >                        }
1901 >                    }
1902 >                }
1903 >                if (binCount != 0) {
1904 >                    if (binCount >= TREEIFY_THRESHOLD)
1905 >                        treeifyBin(tab, i);
1906 >                    break;
1907                  }
1908              }
3237            return modified;
1909          }
1910 +        if (delta != 0)
1911 +            addCount((long)delta, binCount);
1912 +        return val;
1913 +    }
1914  
1915 <        public final boolean retainAll(Collection<?> c) {
1916 <            boolean modified = false;
1917 <            for (Iterator<?> it = iterator(); it.hasNext();) {
1918 <                if (!c.contains(it.next())) {
1919 <                    it.remove();
1920 <                    modified = true;
1915 >    /**
1916 >     * If the specified key is not already associated with a
1917 >     * (non-null) value, associates it with the given value.
1918 >     * Otherwise, replaces the value with the results of the given
1919 >     * remapping function, or removes if {@code null}. The entire
1920 >     * method invocation is performed atomically.  Some attempted
1921 >     * update operations on this map by other threads may be blocked
1922 >     * while computation is in progress, so the computation should be
1923 >     * short and simple, and must not attempt to update any other
1924 >     * mappings of this Map.
1925 >     *
1926 >     * @param key key with which the specified value is to be associated
1927 >     * @param value the value to use if absent
1928 >     * @param remappingFunction the function to recompute a value if present
1929 >     * @return the new value associated with the specified key, or null if none
1930 >     * @throws NullPointerException if the specified key or the
1931 >     *         remappingFunction is null
1932 >     * @throws RuntimeException or Error if the remappingFunction does so,
1933 >     *         in which case the mapping is unchanged
1934 >     */
1935 >    public V merge(K key, V value, BiFun<? super V, ? super V, ? extends V> remappingFunction) {
1936 >        if (key == null || value == null || remappingFunction == null)
1937 >            throw new NullPointerException();
1938 >        int h = spread(key.hashCode());
1939 >        V val = null;
1940 >        int delta = 0;
1941 >        int binCount = 0;
1942 >        for (Node<K,V>[] tab = table;;) {
1943 >            Node<K,V> f; int n, i, fh;
1944 >            if (tab == null || (n = tab.length) == 0)
1945 >                tab = initTable();
1946 >            else if ((f = tabAt(tab, i = (n - 1) & h)) == null) {
1947 >                if (casTabAt(tab, i, null, new Node<K,V>(h, key, value, null))) {
1948 >                    delta = 1;
1949 >                    val = value;
1950 >                    break;
1951 >                }
1952 >            }
1953 >            else if ((fh = f.hash) == MOVED)
1954 >                tab = helpTransfer(tab, f);
1955 >            else {
1956 >                synchronized (f) {
1957 >                    if (tabAt(tab, i) == f) {
1958 >                        if (fh >= 0) {
1959 >                            binCount = 1;
1960 >                            for (Node<K,V> e = f, pred = null;; ++binCount) {
1961 >                                K ek;
1962 >                                if (e.hash == h &&
1963 >                                    ((ek = e.key) == key ||
1964 >                                     (ek != null && key.equals(ek)))) {
1965 >                                    val = remappingFunction.apply(e.val, value);
1966 >                                    if (val != null)
1967 >                                        e.val = val;
1968 >                                    else {
1969 >                                        delta = -1;
1970 >                                        Node<K,V> en = e.next;
1971 >                                        if (pred != null)
1972 >                                            pred.next = en;
1973 >                                        else
1974 >                                            setTabAt(tab, i, en);
1975 >                                    }
1976 >                                    break;
1977 >                                }
1978 >                                pred = e;
1979 >                                if ((e = e.next) == null) {
1980 >                                    delta = 1;
1981 >                                    val = value;
1982 >                                    pred.next =
1983 >                                        new Node<K,V>(h, key, val, null);
1984 >                                    break;
1985 >                                }
1986 >                            }
1987 >                        }
1988 >                        else if (f instanceof TreeBin) {
1989 >                            binCount = 2;
1990 >                            TreeBin<K,V> t = (TreeBin<K,V>)f;
1991 >                            TreeNode<K,V> r = t.root;
1992 >                            TreeNode<K,V> p = (r == null) ? null :
1993 >                                r.findTreeNode(h, key, null);
1994 >                            val = (p == null) ? value :
1995 >                                remappingFunction.apply(p.val, value);
1996 >                            if (val != null) {
1997 >                                if (p != null)
1998 >                                    p.val = val;
1999 >                                else {
2000 >                                    delta = 1;
2001 >                                    t.putTreeVal(h, key, val);
2002 >                                }
2003 >                            }
2004 >                            else if (p != null) {
2005 >                                delta = -1;
2006 >                                if (t.removeTreeNode(p))
2007 >                                    setTabAt(tab, i, untreeify(t.first));
2008 >                            }
2009 >                        }
2010 >                    }
2011 >                }
2012 >                if (binCount != 0) {
2013 >                    if (binCount >= TREEIFY_THRESHOLD)
2014 >                        treeifyBin(tab, i);
2015 >                    break;
2016                  }
2017              }
3248            return modified;
2018          }
2019 +        if (delta != 0)
2020 +            addCount((long)delta, binCount);
2021 +        return val;
2022 +    }
2023  
2024 +    // Hashtable legacy methods
2025 +
2026 +    /**
2027 +     * Legacy method testing if some key maps into the specified value
2028 +     * in this table.  This method is identical in functionality to
2029 +     * {@link #containsValue(Object)}, and exists solely to ensure
2030 +     * full compatibility with class {@link java.util.Hashtable},
2031 +     * which supported this method prior to introduction of the
2032 +     * Java Collections framework.
2033 +     *
2034 +     * @param  value a value to search for
2035 +     * @return {@code true} if and only if some key maps to the
2036 +     *         {@code value} argument in this table as
2037 +     *         determined by the {@code equals} method;
2038 +     *         {@code false} otherwise
2039 +     * @throws NullPointerException if the specified value is null
2040 +     */
2041 +    @Deprecated public boolean contains(Object value) {
2042 +        return containsValue(value);
2043      }
2044  
2045 <    static final class KeySet<K,V> extends CHMView<K,V> implements Set<K> {
2046 <        KeySet(ConcurrentHashMapV8<K, V> map)  {
2047 <            super(map);
2048 <        }
2049 <        public final boolean contains(Object o) { return map.containsKey(o); }
2050 <        public final boolean remove(Object o)   { return map.remove(o) != null; }
2051 <        public final Iterator<K> iterator() {
2052 <            return new KeyIterator<K,V>(map);
2053 <        }
2054 <        public final boolean add(K e) {
3263 <            throw new UnsupportedOperationException();
3264 <        }
3265 <        public final boolean addAll(Collection<? extends K> c) {
3266 <            throw new UnsupportedOperationException();
3267 <        }
3268 <        public boolean equals(Object o) {
3269 <            Set<?> c;
3270 <            return ((o instanceof Set) &&
3271 <                    ((c = (Set<?>)o) == this ||
3272 <                     (containsAll(c) && c.containsAll(this))));
3273 <        }
2045 >    /**
2046 >     * Returns an enumeration of the keys in this table.
2047 >     *
2048 >     * @return an enumeration of the keys in this table
2049 >     * @see #keySet()
2050 >     */
2051 >    public Enumeration<K> keys() {
2052 >        Node<K,V>[] t;
2053 >        int f = (t = table) == null ? 0 : t.length;
2054 >        return new KeyIterator<K,V>(t, f, 0, f, this);
2055      }
2056  
2057 +    /**
2058 +     * Returns an enumeration of the values in this table.
2059 +     *
2060 +     * @return an enumeration of the values in this table
2061 +     * @see #values()
2062 +     */
2063 +    public Enumeration<V> elements() {
2064 +        Node<K,V>[] t;
2065 +        int f = (t = table) == null ? 0 : t.length;
2066 +        return new ValueIterator<K,V>(t, f, 0, f, this);
2067 +    }
2068  
2069 <    static final class Values<K,V> extends CHMView<K,V>
2070 <        implements Collection<V> {
2071 <        Values(ConcurrentHashMapV8<K, V> map)   { super(map); }
2072 <        public final boolean contains(Object o) { return map.containsValue(o); }
2073 <        public final boolean remove(Object o) {
2074 <            if (o != null) {
2075 <                Iterator<V> it = new ValueIterator<K,V>(map);
2076 <                while (it.hasNext()) {
2077 <                    if (o.equals(it.next())) {
2078 <                        it.remove();
2079 <                        return true;
2069 >    // ConcurrentHashMapV8-only methods
2070 >
2071 >    /**
2072 >     * Returns the number of mappings. This method should be used
2073 >     * instead of {@link #size} because a ConcurrentHashMapV8 may
2074 >     * contain more mappings than can be represented as an int. The
2075 >     * value returned is an estimate; the actual count may differ if
2076 >     * there are concurrent insertions or removals.
2077 >     *
2078 >     * @return the number of mappings
2079 >     * @since 1.8
2080 >     */
2081 >    public long mappingCount() {
2082 >        long n = sumCount();
2083 >        return (n < 0L) ? 0L : n; // ignore transient negative values
2084 >    }
2085 >
2086 >    /**
2087 >     * Creates a new {@link Set} backed by a ConcurrentHashMapV8
2088 >     * from the given type to {@code Boolean.TRUE}.
2089 >     *
2090 >     * @return the new set
2091 >     * @since 1.8
2092 >     */
2093 >    public static <K> KeySetView<K,Boolean> newKeySet() {
2094 >        return new KeySetView<K,Boolean>
2095 >            (new ConcurrentHashMapV8<K,Boolean>(), Boolean.TRUE);
2096 >    }
2097 >
2098 >    /**
2099 >     * Creates a new {@link Set} backed by a ConcurrentHashMapV8
2100 >     * from the given type to {@code Boolean.TRUE}.
2101 >     *
2102 >     * @param initialCapacity The implementation performs internal
2103 >     * sizing to accommodate this many elements.
2104 >     * @throws IllegalArgumentException if the initial capacity of
2105 >     * elements is negative
2106 >     * @return the new set
2107 >     * @since 1.8
2108 >     */
2109 >    public static <K> KeySetView<K,Boolean> newKeySet(int initialCapacity) {
2110 >        return new KeySetView<K,Boolean>
2111 >            (new ConcurrentHashMapV8<K,Boolean>(initialCapacity), Boolean.TRUE);
2112 >    }
2113 >
2114 >    /**
2115 >     * Returns a {@link Set} view of the keys in this map, using the
2116 >     * given common mapped value for any additions (i.e., {@link
2117 >     * Collection#add} and {@link Collection#addAll(Collection)}).
2118 >     * This is of course only appropriate if it is acceptable to use
2119 >     * the same value for all additions from this view.
2120 >     *
2121 >     * @param mappedValue the mapped value to use for any additions
2122 >     * @return the set view
2123 >     * @throws NullPointerException if the mappedValue is null
2124 >     */
2125 >    public KeySetView<K,V> keySet(V mappedValue) {
2126 >        if (mappedValue == null)
2127 >            throw new NullPointerException();
2128 >        return new KeySetView<K,V>(this, mappedValue);
2129 >    }
2130 >
2131 >    /* ---------------- Special Nodes -------------- */
2132 >
2133 >    /**
2134 >     * A node inserted at head of bins during transfer operations.
2135 >     */
2136 >    static final class ForwardingNode<K,V> extends Node<K,V> {
2137 >        final Node<K,V>[] nextTable;
2138 >        ForwardingNode(Node<K,V>[] tab) {
2139 >            super(MOVED, null, null, null);
2140 >            this.nextTable = tab;
2141 >        }
2142 >
2143 >        Node<K,V> find(int h, Object k) {
2144 >            // loop to avoid arbitrarily deep recursion on forwarding nodes
2145 >            outer: for (Node<K,V>[] tab = nextTable;;) {
2146 >                Node<K,V> e; int n;
2147 >                if (k == null || tab == null || (n = tab.length) == 0 ||
2148 >                    (e = tabAt(tab, (n - 1) & h)) == null)
2149 >                    return null;
2150 >                for (;;) {
2151 >                    int eh; K ek;
2152 >                    if ((eh = e.hash) == h &&
2153 >                        ((ek = e.key) == k || (ek != null && k.equals(ek))))
2154 >                        return e;
2155 >                    if (eh < 0) {
2156 >                        if (e instanceof ForwardingNode) {
2157 >                            tab = ((ForwardingNode<K,V>)e).nextTable;
2158 >                            continue outer;
2159 >                        }
2160 >                        else
2161 >                            return e.find(h, k);
2162                      }
2163 +                    if ((e = e.next) == null)
2164 +                        return null;
2165                  }
2166              }
3291            return false;
3292        }
3293        public final Iterator<V> iterator() {
3294            return new ValueIterator<K,V>(map);
3295        }
3296        public final boolean add(V e) {
3297            throw new UnsupportedOperationException();
2167          }
3299        public final boolean addAll(Collection<? extends V> c) {
3300            throw new UnsupportedOperationException();
3301        }
3302
2168      }
2169  
2170 <    static final class EntrySet<K,V> extends CHMView<K,V>
2171 <        implements Set<Map.Entry<K,V>> {
2172 <        EntrySet(ConcurrentHashMapV8<K, V> map) { super(map); }
2173 <        public final boolean contains(Object o) {
2174 <            Object k, v, r; Map.Entry<?,?> e;
2175 <            return ((o instanceof Map.Entry) &&
3311 <                    (k = (e = (Map.Entry<?,?>)o).getKey()) != null &&
3312 <                    (r = map.get(k)) != null &&
3313 <                    (v = e.getValue()) != null &&
3314 <                    (v == r || v.equals(r)));
3315 <        }
3316 <        public final boolean remove(Object o) {
3317 <            Object k, v; Map.Entry<?,?> e;
3318 <            return ((o instanceof Map.Entry) &&
3319 <                    (k = (e = (Map.Entry<?,?>)o).getKey()) != null &&
3320 <                    (v = e.getValue()) != null &&
3321 <                    map.remove(k, v));
3322 <        }
3323 <        public final Iterator<Map.Entry<K,V>> iterator() {
3324 <            return new EntryIterator<K,V>(map);
3325 <        }
3326 <        public final boolean add(Entry<K,V> e) {
3327 <            throw new UnsupportedOperationException();
3328 <        }
3329 <        public final boolean addAll(Collection<? extends Entry<K,V>> c) {
3330 <            throw new UnsupportedOperationException();
2170 >    /**
2171 >     * A place-holder node used in computeIfAbsent and compute
2172 >     */
2173 >    static final class ReservationNode<K,V> extends Node<K,V> {
2174 >        ReservationNode() {
2175 >            super(RESERVED, null, null, null);
2176          }
2177 <        public boolean equals(Object o) {
2178 <            Set<?> c;
2179 <            return ((o instanceof Set) &&
3335 <                    ((c = (Set<?>)o) == this ||
3336 <                     (containsAll(c) && c.containsAll(this))));
2177 >
2178 >        Node<K,V> find(int h, Object k) {
2179 >            return null;
2180          }
2181      }
2182  
2183 <    /* ---------------- Serialization Support -------------- */
2183 >    /* ---------------- Table Initialization and Resizing -------------- */
2184  
2185      /**
2186 <     * Stripped-down version of helper class used in previous version,
3344 <     * declared for the sake of serialization compatibility
2186 >     * Initializes table, using the size recorded in sizeCtl.
2187       */
2188 <    static class Segment<K,V> implements Serializable {
2189 <        private static final long serialVersionUID = 2249069246763182397L;
2190 <        final float loadFactor;
2191 <        Segment(float lf) { this.loadFactor = lf; }
2188 >    private final Node<K,V>[] initTable() {
2189 >        Node<K,V>[] tab; int sc;
2190 >        while ((tab = table) == null || tab.length == 0) {
2191 >            if ((sc = sizeCtl) < 0)
2192 >                Thread.yield(); // lost initialization race; just spin
2193 >            else if (U.compareAndSwapInt(this, SIZECTL, sc, -1)) {
2194 >                try {
2195 >                    if ((tab = table) == null || tab.length == 0) {
2196 >                        int n = (sc > 0) ? sc : DEFAULT_CAPACITY;
2197 >                        @SuppressWarnings({"rawtypes","unchecked"})
2198 >                            Node<K,V>[] nt = (Node<K,V>[])new Node[n];
2199 >                        table = tab = nt;
2200 >                        sc = n - (n >>> 2);
2201 >                    }
2202 >                } finally {
2203 >                    sizeCtl = sc;
2204 >                }
2205 >                break;
2206 >            }
2207 >        }
2208 >        return tab;
2209      }
2210  
2211      /**
2212 <     * Saves the state of the {@code ConcurrentHashMapV8} instance to a
2213 <     * stream (i.e., serializes it).
2214 <     * @param s the stream
2215 <     * @serialData
2216 <     * the key (Object) and value (Object)
2217 <     * for each key-value mapping, followed by a null pair.
2218 <     * The key-value mappings are emitted in no particular order.
2219 <     */
2220 <    @SuppressWarnings("unchecked") private void writeObject(java.io.ObjectOutputStream s)
2221 <        throws java.io.IOException {
2222 <        if (segments == null) { // for serialization compatibility
2223 <            segments = (Segment<K,V>[])
2224 <                new Segment<?,?>[DEFAULT_CONCURRENCY_LEVEL];
2225 <            for (int i = 0; i < segments.length; ++i)
2226 <                segments[i] = new Segment<K,V>(LOAD_FACTOR);
2227 <        }
2228 <        s.defaultWriteObject();
2229 <        Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
2230 <        Object v;
2231 <        while ((v = it.advance()) != null) {
2232 <            s.writeObject(it.nextKey);
2233 <            s.writeObject(v);
2212 >     * Adds to count, and if table is too small and not already
2213 >     * resizing, initiates transfer. If already resizing, helps
2214 >     * perform transfer if work is available.  Rechecks occupancy
2215 >     * after a transfer to see if another resize is already needed
2216 >     * because resizings are lagging additions.
2217 >     *
2218 >     * @param x the count to add
2219 >     * @param check if <0, don't check resize, if <= 1 only check if uncontended
2220 >     */
2221 >    private final void addCount(long x, int check) {
2222 >        CounterCell[] as; long b, s;
2223 >        if ((as = counterCells) != null ||
2224 >            !U.compareAndSwapLong(this, BASECOUNT, b = baseCount, s = b + x)) {
2225 >            CounterHashCode hc; CounterCell a; long v; int m;
2226 >            boolean uncontended = true;
2227 >            if ((hc = threadCounterHashCode.get()) == null ||
2228 >                as == null || (m = as.length - 1) < 0 ||
2229 >                (a = as[m & hc.code]) == null ||
2230 >                !(uncontended =
2231 >                  U.compareAndSwapLong(a, CELLVALUE, v = a.value, v + x))) {
2232 >                fullAddCount(x, hc, uncontended);
2233 >                return;
2234 >            }
2235 >            if (check <= 1)
2236 >                return;
2237 >            s = sumCount();
2238 >        }
2239 >        if (check >= 0) {
2240 >            Node<K,V>[] tab, nt; int sc;
2241 >            while (s >= (long)(sc = sizeCtl) && (tab = table) != null &&
2242 >                   tab.length < MAXIMUM_CAPACITY) {
2243 >                if (sc < 0) {
2244 >                    if (sc == -1 || transferIndex <= transferOrigin ||
2245 >                        (nt = nextTable) == null)
2246 >                        break;
2247 >                    if (U.compareAndSwapInt(this, SIZECTL, sc, sc - 1))
2248 >                        transfer(tab, nt);
2249 >                }
2250 >                else if (U.compareAndSwapInt(this, SIZECTL, sc, -2))
2251 >                    transfer(tab, null);
2252 >                s = sumCount();
2253 >            }
2254          }
3376        s.writeObject(null);
3377        s.writeObject(null);
3378        segments = null; // throw away
2255      }
2256  
2257      /**
2258 <     * Reconstitutes the instance from a stream (that is, deserializes it).
3383 <     * @param s the stream
2258 >     * Helps transfer if a resize is in progress.
2259       */
2260 <    @SuppressWarnings("unchecked") private void readObject(java.io.ObjectInputStream s)
2261 <        throws java.io.IOException, ClassNotFoundException {
2262 <        s.defaultReadObject();
2263 <        this.segments = null; // unneeded
2264 <        // initialize transient final field
2265 <        UNSAFE.putObjectVolatile(this, counterOffset, new LongAdder());
2260 >    final Node<K,V>[] helpTransfer(Node<K,V>[] tab, Node<K,V> f) {
2261 >        Node<K,V>[] nextTab; int sc;
2262 >        if ((f instanceof ForwardingNode) &&
2263 >            (nextTab = ((ForwardingNode<K,V>)f).nextTable) != null) {
2264 >            if (nextTab == nextTable && tab == table &&
2265 >                transferIndex > transferOrigin && (sc = sizeCtl) < -1 &&
2266 >                U.compareAndSwapInt(this, SIZECTL, sc, sc - 1))
2267 >                transfer(tab, nextTab);
2268 >            return nextTab;
2269 >        }
2270 >        return table;
2271 >    }
2272  
2273 <        // Create all nodes, then place in table once size is known
2274 <        long size = 0L;
2275 <        Node p = null;
2276 <        for (;;) {
2277 <            K k = (K) s.readObject();
2278 <            V v = (V) s.readObject();
2279 <            if (k != null && v != null) {
2280 <                int h = spread(k.hashCode());
2281 <                p = new Node(h, k, v, p);
2282 <                ++size;
2273 >    /**
2274 >     * Tries to presize table to accommodate the given number of elements.
2275 >     *
2276 >     * @param size number of elements (doesn't need to be perfectly accurate)
2277 >     */
2278 >    private final void tryPresize(int size) {
2279 >        int c = (size >= (MAXIMUM_CAPACITY >>> 1)) ? MAXIMUM_CAPACITY :
2280 >            tableSizeFor(size + (size >>> 1) + 1);
2281 >        int sc;
2282 >        while ((sc = sizeCtl) >= 0) {
2283 >            Node<K,V>[] tab = table; int n;
2284 >            if (tab == null || (n = tab.length) == 0) {
2285 >                n = (sc > c) ? sc : c;
2286 >                if (U.compareAndSwapInt(this, SIZECTL, sc, -1)) {
2287 >                    try {
2288 >                        if (table == tab) {
2289 >                            @SuppressWarnings({"rawtypes","unchecked"})
2290 >                                Node<K,V>[] nt = (Node<K,V>[])new Node[n];
2291 >                            table = nt;
2292 >                            sc = n - (n >>> 2);
2293 >                        }
2294 >                    } finally {
2295 >                        sizeCtl = sc;
2296 >                    }
2297 >                }
2298              }
2299 <            else
2299 >            else if (c <= sc || n >= MAXIMUM_CAPACITY)
2300                  break;
2301 +            else if (tab == table &&
2302 +                     U.compareAndSwapInt(this, SIZECTL, sc, -2))
2303 +                transfer(tab, null);
2304          }
2305 <        if (p != null) {
2306 <            boolean init = false;
2307 <            int n;
2308 <            if (size >= (long)(MAXIMUM_CAPACITY >>> 1))
2309 <                n = MAXIMUM_CAPACITY;
2310 <            else {
2311 <                int sz = (int)size;
2312 <                n = tableSizeFor(sz + (sz >>> 1) + 1);
2313 <            }
2314 <            int sc = sizeCtl;
2315 <            boolean collide = false;
2316 <            if (n > sc &&
2317 <                UNSAFE.compareAndSwapInt(this, sizeCtlOffset, sc, -1)) {
2318 <                try {
2319 <                    if (table == null) {
2320 <                        init = true;
2321 <                        Node[] tab = new Node[n];
2322 <                        int mask = n - 1;
2323 <                        while (p != null) {
2324 <                            int j = p.hash & mask;
2325 <                            Node next = p.next;
2326 <                            Node q = p.next = tabAt(tab, j);
2327 <                            setTabAt(tab, j, p);
2328 <                            if (!collide && q != null && q.hash == p.hash)
2329 <                                collide = true;
2330 <                            p = next;
2331 <                        }
2332 <                        table = tab;
2333 <                        counter.add(size);
2334 <                        sc = n - (n >>> 2);
2305 >    }
2306 >
2307 >    /**
2308 >     * Moves and/or copies the nodes in each bin to new table. See
2309 >     * above for explanation.
2310 >     */
2311 >    private final void transfer(Node<K,V>[] tab, Node<K,V>[] nextTab) {
2312 >        int n = tab.length, stride;
2313 >        if ((stride = (NCPU > 1) ? (n >>> 3) / NCPU : n) < MIN_TRANSFER_STRIDE)
2314 >            stride = MIN_TRANSFER_STRIDE; // subdivide range
2315 >        if (nextTab == null) {            // initiating
2316 >            try {
2317 >                @SuppressWarnings({"rawtypes","unchecked"})
2318 >                    Node<K,V>[] nt = (Node<K,V>[])new Node[n << 1];
2319 >                nextTab = nt;
2320 >            } catch (Throwable ex) {      // try to cope with OOME
2321 >                sizeCtl = Integer.MAX_VALUE;
2322 >                return;
2323 >            }
2324 >            nextTable = nextTab;
2325 >            transferOrigin = n;
2326 >            transferIndex = n;
2327 >            ForwardingNode<K,V> rev = new ForwardingNode<K,V>(tab);
2328 >            for (int k = n; k > 0;) {    // progressively reveal ready slots
2329 >                int nextk = (k > stride) ? k - stride : 0;
2330 >                for (int m = nextk; m < k; ++m)
2331 >                    nextTab[m] = rev;
2332 >                for (int m = n + nextk; m < n + k; ++m)
2333 >                    nextTab[m] = rev;
2334 >                U.putOrderedInt(this, TRANSFERORIGIN, k = nextk);
2335 >            }
2336 >        }
2337 >        int nextn = nextTab.length;
2338 >        ForwardingNode<K,V> fwd = new ForwardingNode<K,V>(nextTab);
2339 >        boolean advance = true;
2340 >        boolean finishing = false; // to ensure sweep before committing nextTab
2341 >        for (int i = 0, bound = 0;;) {
2342 >            int nextIndex, nextBound, fh; Node<K,V> f;
2343 >            while (advance) {
2344 >                if (--i >= bound || finishing)
2345 >                    advance = false;
2346 >                else if ((nextIndex = transferIndex) <= transferOrigin) {
2347 >                    i = -1;
2348 >                    advance = false;
2349 >                }
2350 >                else if (U.compareAndSwapInt
2351 >                         (this, TRANSFERINDEX, nextIndex,
2352 >                          nextBound = (nextIndex > stride ?
2353 >                                       nextIndex - stride : 0))) {
2354 >                    bound = nextBound;
2355 >                    i = nextIndex - 1;
2356 >                    advance = false;
2357 >                }
2358 >            }
2359 >            if (i < 0 || i >= n || i + n >= nextn) {
2360 >                if (finishing) {
2361 >                    nextTable = null;
2362 >                    table = nextTab;
2363 >                    sizeCtl = (n << 1) - (n >>> 1);
2364 >                    return;
2365 >                }
2366 >                for (int sc;;) {
2367 >                    if (U.compareAndSwapInt(this, SIZECTL, sc = sizeCtl, ++sc)) {
2368 >                        if (sc != -1)
2369 >                            return;
2370 >                        finishing = advance = true;
2371 >                        i = n; // recheck before commit
2372 >                        break;
2373                      }
3437                } finally {
3438                    sizeCtl = sc;
2374                  }
2375 <                if (collide) { // rescan and convert to TreeBins
2376 <                    Node[] tab = table;
2377 <                    for (int i = 0; i < tab.length; ++i) {
2378 <                        int c = 0;
2379 <                        for (Node e = tabAt(tab, i); e != null; e = e.next) {
2380 <                            if (++c > TREE_THRESHOLD &&
2381 <                                (e.key instanceof Comparable)) {
2382 <                                replaceWithTreeBin(tab, i, e.key);
2383 <                                break;
2375 >            }
2376 >            else if ((f = tabAt(tab, i)) == null) {
2377 >                if (casTabAt(tab, i, null, fwd)) {
2378 >                    setTabAt(nextTab, i, null);
2379 >                    setTabAt(nextTab, i + n, null);
2380 >                    advance = true;
2381 >                }
2382 >            }
2383 >            else if ((fh = f.hash) == MOVED)
2384 >                advance = true; // already processed
2385 >            else {
2386 >                synchronized (f) {
2387 >                    if (tabAt(tab, i) == f) {
2388 >                        Node<K,V> ln, hn;
2389 >                        if (fh >= 0) {
2390 >                            int runBit = fh & n;
2391 >                            Node<K,V> lastRun = f;
2392 >                            for (Node<K,V> p = f.next; p != null; p = p.next) {
2393 >                                int b = p.hash & n;
2394 >                                if (b != runBit) {
2395 >                                    runBit = b;
2396 >                                    lastRun = p;
2397 >                                }
2398 >                            }
2399 >                            if (runBit == 0) {
2400 >                                ln = lastRun;
2401 >                                hn = null;
2402                              }
2403 +                            else {
2404 +                                hn = lastRun;
2405 +                                ln = null;
2406 +                            }
2407 +                            for (Node<K,V> p = f; p != lastRun; p = p.next) {
2408 +                                int ph = p.hash; K pk = p.key; V pv = p.val;
2409 +                                if ((ph & n) == 0)
2410 +                                    ln = new Node<K,V>(ph, pk, pv, ln);
2411 +                                else
2412 +                                    hn = new Node<K,V>(ph, pk, pv, hn);
2413 +                            }
2414 +                            setTabAt(nextTab, i, ln);
2415 +                            setTabAt(nextTab, i + n, hn);
2416 +                            setTabAt(tab, i, fwd);
2417 +                            advance = true;
2418 +                        }
2419 +                        else if (f instanceof TreeBin) {
2420 +                            TreeBin<K,V> t = (TreeBin<K,V>)f;
2421 +                            TreeNode<K,V> lo = null, loTail = null;
2422 +                            TreeNode<K,V> hi = null, hiTail = null;
2423 +                            int lc = 0, hc = 0;
2424 +                            for (Node<K,V> e = t.first; e != null; e = e.next) {
2425 +                                int h = e.hash;
2426 +                                TreeNode<K,V> p = new TreeNode<K,V>
2427 +                                    (h, e.key, e.val, null, null);
2428 +                                if ((h & n) == 0) {
2429 +                                    if ((p.prev = loTail) == null)
2430 +                                        lo = p;
2431 +                                    else
2432 +                                        loTail.next = p;
2433 +                                    loTail = p;
2434 +                                    ++lc;
2435 +                                }
2436 +                                else {
2437 +                                    if ((p.prev = hiTail) == null)
2438 +                                        hi = p;
2439 +                                    else
2440 +                                        hiTail.next = p;
2441 +                                    hiTail = p;
2442 +                                    ++hc;
2443 +                                }
2444 +                            }
2445 +                            ln = (lc <= UNTREEIFY_THRESHOLD) ? untreeify(lo) :
2446 +                                (hc != 0) ? new TreeBin<K,V>(lo) : t;
2447 +                            hn = (hc <= UNTREEIFY_THRESHOLD) ? untreeify(hi) :
2448 +                                (lc != 0) ? new TreeBin<K,V>(hi) : t;
2449 +                            setTabAt(nextTab, i, ln);
2450 +                            setTabAt(nextTab, i + n, hn);
2451 +                            setTabAt(tab, i, fwd);
2452 +                            advance = true;
2453                          }
2454                      }
2455                  }
2456              }
3454            if (!init) { // Can only happen if unsafely published.
3455                while (p != null) {
3456                    internalPut(p.key, p.val);
3457                    p = p.next;
3458                }
3459            }
2457          }
2458      }
2459  
2460 +    /* ---------------- Conversion from/to TreeBins -------------- */
2461  
2462 <    // -------------------------------------------------------
2463 <
2464 <    // Sams
2465 <    /** Interface describing a void action of one argument */
2466 <    public interface Action<A> { void apply(A a); }
2467 <    /** Interface describing a void action of two arguments */
2468 <    public interface BiAction<A,B> { void apply(A a, B b); }
2469 <    /** Interface describing a function of one argument */
2470 <    public interface Fun<A,T> { T apply(A a); }
2471 <    /** Interface describing a function of two arguments */
2472 <    public interface BiFun<A,B,T> { T apply(A a, B b); }
2473 <    /** Interface describing a function of no arguments */
2474 <    public interface Generator<T> { T apply(); }
2475 <    /** Interface describing a function mapping its argument to a double */
2476 <    public interface ObjectToDouble<A> { double apply(A a); }
2477 <    /** Interface describing a function mapping its argument to a long */
2478 <    public interface ObjectToLong<A> { long apply(A a); }
2479 <    /** Interface describing a function mapping its argument to an int */
2480 <    public interface ObjectToInt<A> {int apply(A a); }
2481 <    /** Interface describing a function mapping two arguments to a double */
2482 <    public interface ObjectByObjectToDouble<A,B> { double apply(A a, B b); }
2483 <    /** Interface describing a function mapping two arguments to a long */
2484 <    public interface ObjectByObjectToLong<A,B> { long apply(A a, B b); }
2485 <    /** Interface describing a function mapping two arguments to an int */
2486 <    public interface ObjectByObjectToInt<A,B> {int apply(A a, B b); }
2487 <    /** Interface describing a function mapping a double to a double */
2488 <    public interface DoubleToDouble { double apply(double a); }
2489 <    /** Interface describing a function mapping a long to a long */
2490 <    public interface LongToLong { long apply(long a); }
2491 <    /** Interface describing a function mapping an int to an int */
2492 <    public interface IntToInt { int apply(int a); }
2493 <    /** Interface describing a function mapping two doubles to a double */
3496 <    public interface DoubleByDoubleToDouble { double apply(double a, double b); }
3497 <    /** Interface describing a function mapping two longs to a long */
3498 <    public interface LongByLongToLong { long apply(long a, long b); }
3499 <    /** Interface describing a function mapping two ints to an int */
3500 <    public interface IntByIntToInt { int apply(int a, int b); }
3501 <
3502 <
3503 <    // -------------------------------------------------------
2462 >    /**
2463 >     * Replaces all linked nodes in bin at given index unless table is
2464 >     * too small, in which case resizes instead.
2465 >     */
2466 >    private final void treeifyBin(Node<K,V>[] tab, int index) {
2467 >        Node<K,V> b; int n, sc;
2468 >        if (tab != null) {
2469 >            if ((n = tab.length) < MIN_TREEIFY_CAPACITY) {
2470 >                if (tab == table && (sc = sizeCtl) >= 0 &&
2471 >                    U.compareAndSwapInt(this, SIZECTL, sc, -2))
2472 >                    transfer(tab, null);
2473 >            }
2474 >            else if ((b = tabAt(tab, index)) != null && b.hash >= 0) {
2475 >                synchronized (b) {
2476 >                    if (tabAt(tab, index) == b) {
2477 >                        TreeNode<K,V> hd = null, tl = null;
2478 >                        for (Node<K,V> e = b; e != null; e = e.next) {
2479 >                            TreeNode<K,V> p =
2480 >                                new TreeNode<K,V>(e.hash, e.key, e.val,
2481 >                                                  null, null);
2482 >                            if ((p.prev = tl) == null)
2483 >                                hd = p;
2484 >                            else
2485 >                                tl.next = p;
2486 >                            tl = p;
2487 >                        }
2488 >                        setTabAt(tab, index, new TreeBin<K,V>(hd));
2489 >                    }
2490 >                }
2491 >            }
2492 >        }
2493 >    }
2494  
2495      /**
2496 <     * Returns an extended {@link Parallel} view of this map using the
3507 <     * given executor for bulk parallel operations.
3508 <     *
3509 <     * @param executor the executor
3510 <     * @return a parallel view
2496 >     * Returns a list on non-TreeNodes replacing those in given list.
2497       */
2498 <    public Parallel parallel(ForkJoinPool executor)  {
2499 <        return new Parallel(executor);
2498 >    static <K,V> Node<K,V> untreeify(Node<K,V> b) {
2499 >        Node<K,V> hd = null, tl = null;
2500 >        for (Node<K,V> q = b; q != null; q = q.next) {
2501 >            Node<K,V> p = new Node<K,V>(q.hash, q.key, q.val, null);
2502 >            if (tl == null)
2503 >                hd = p;
2504 >            else
2505 >                tl.next = p;
2506 >            tl = p;
2507 >        }
2508 >        return hd;
2509      }
2510  
2511 +    /* ---------------- TreeNodes -------------- */
2512 +
2513      /**
2514 <     * An extended view of a ConcurrentHashMap supporting bulk
3518 <     * parallel operations. These operations are designed to be
3519 <     * safely, and often sensibly, applied even with maps that are
3520 <     * being concurrently updated by other threads; for example, when
3521 <     * computing a snapshot summary of the values in a shared
3522 <     * registry.  There are three kinds of operation, each with four
3523 <     * forms, accepting functions with Keys, Values, Entries, and
3524 <     * (Key, Value) arguments and/or return values. Because the
3525 <     * elements of a ConcurrentHashMap are not ordered in any
3526 <     * particular way, and may be processed in different orders in
3527 <     * different parallel executions, the correctness of supplied
3528 <     * functions should not depend on any ordering, or on any other
3529 <     * objects or values that may transiently change while computation
3530 <     * is in progress; and except for forEach actions, should ideally
3531 <     * be side-effect-free.
3532 <     *
3533 <     * <ul>
3534 <     * <li> forEach: Perform a given action on each element.
3535 <     * A variant form applies a given transformation on each element
3536 <     * before performing the action.</li>
3537 <     *
3538 <     * <li> search: Return the first available non-null result of
3539 <     * applying a given function on each element; skipping further
3540 <     * search when a result is found.</li>
3541 <     *
3542 <     * <li> reduce: Accumulate each element.  The supplied reduction
3543 <     * function cannot rely on ordering (more formally, it should be
3544 <     * both associative and commutative).  There are five variants:
3545 <     *
3546 <     * <ul>
3547 <     *
3548 <     * <li> Plain reductions. (There is not a form of this method for
3549 <     * (key, value) function arguments since there is no corresponding
3550 <     * return type.)</li>
3551 <     *
3552 <     * <li> Mapped reductions that accumulate the results of a given
3553 <     * function applied to each element.</li>
3554 <     *
3555 <     * <li> Reductions to scalar doubles, longs, and ints, using a
3556 <     * given basis value.</li>
3557 <     *
3558 <     * </li>
3559 <     * </ul>
3560 <     * </ul>
3561 <     *
3562 <     * <p>The concurrency properties of the bulk operations follow
3563 <     * from those of ConcurrentHashMap: Any non-null result returned
3564 <     * from {@code get(key)} and related access methods bears a
3565 <     * happens-before relation with the associated insertion or
3566 <     * update.  The result of any bulk operation reflects the
3567 <     * composition of these per-element relations (but is not
3568 <     * necessarily atomic with respect to the map as a whole unless it
3569 <     * is somehow known to be quiescent).  Conversely, because keys
3570 <     * and values in the map are never null, null serves as a reliable
3571 <     * atomic indicator of the current lack of any result.  To
3572 <     * maintain this property, null serves as an implicit basis for
3573 <     * all non-scalar reduction operations. For the double, long, and
3574 <     * int versions, the basis should be one that, when combined with
3575 <     * any other value, returns that other value (more formally, it
3576 <     * should be the identity element for the reduction). Most common
3577 <     * reductions have these properties; for example, computing a sum
3578 <     * with basis 0 or a minimum with basis MAX_VALUE.
3579 <     *
3580 <     * <p>Search and transformation functions provided as arguments
3581 <     * should similarly return null to indicate the lack of any result
3582 <     * (in which case it is not used). In the case of mapped
3583 <     * reductions, this also enables transformations to serve as
3584 <     * filters, returning null (or, in the case of primitive
3585 <     * specializations, the identity basis) if the element should not
3586 <     * be combined. You can create compound transformations and
3587 <     * filterings by composing them yourself under this "null means
3588 <     * there is nothing there now" rule before using them in search or
3589 <     * reduce operations.
3590 <     *
3591 <     * <p>Methods accepting and/or returning Entry arguments maintain
3592 <     * key-value associations. They may be useful for example when
3593 <     * finding the key for the greatest value. Note that "plain" Entry
3594 <     * arguments can be supplied using {@code new
3595 <     * AbstractMap.SimpleEntry(k,v)}.
3596 <     *
3597 <     * <p> Bulk operations may complete abruptly, throwing an
3598 <     * exception encountered in the application of a supplied
3599 <     * function. Bear in mind when handling such exceptions that other
3600 <     * concurrently executing functions could also have thrown
3601 <     * exceptions, or would have done so if the first exception had
3602 <     * not occurred.
3603 <     *
3604 <     * <p>Parallel speedups compared to sequential processing are
3605 <     * common but not guaranteed.  Operations involving brief
3606 <     * functions on small maps may execute more slowly than sequential
3607 <     * loops if the underlying work to parallelize the computation is
3608 <     * more expensive than the computation itself. Similarly,
3609 <     * parallelization may not lead to much actual parallelism if all
3610 <     * processors are busy performing unrelated tasks.
3611 <     *
3612 <     * <p> All arguments to all task methods must be non-null.
3613 <     *
3614 <     * <p><em>jsr166e note: During transition, this class
3615 <     * uses nested functional interfaces with different names but the
3616 <     * same forms as those expected for JDK8.<em>
2514 >     * Nodes for use in TreeBins
2515       */
2516 <    public class Parallel {
2517 <        final ForkJoinPool fjp;
2516 >    static final class TreeNode<K,V> extends Node<K,V> {
2517 >        TreeNode<K,V> parent;  // red-black tree links
2518 >        TreeNode<K,V> left;
2519 >        TreeNode<K,V> right;
2520 >        TreeNode<K,V> prev;    // needed to unlink next upon deletion
2521 >        boolean red;
2522  
2523 <        /**
2524 <         * Returns an extended view of this map using the given
2525 <         * executor for bulk parallel operations.
2526 <         *
3625 <         * @param executor the executor
3626 <         */
3627 <        public Parallel(ForkJoinPool executor)  {
3628 <            this.fjp = executor;
2523 >        TreeNode(int hash, K key, V val, Node<K,V> next,
2524 >                 TreeNode<K,V> parent) {
2525 >            super(hash, key, val, next);
2526 >            this.parent = parent;
2527          }
2528  
2529 <        /**
2530 <         * Performs the given action for each (key, value).
3633 <         *
3634 <         * @param action the action
3635 <         */
3636 <        public void forEach(BiAction<K,V> action) {
3637 <            fjp.invoke(ForkJoinTasks.forEach
3638 <                       (ConcurrentHashMapV8.this, action));
2529 >        Node<K,V> find(int h, Object k) {
2530 >            return findTreeNode(h, k, null);
2531          }
2532  
2533          /**
2534 <         * Performs the given action for each non-null transformation
2535 <         * of each (key, value).
3644 <         *
3645 <         * @param transformer a function returning the transformation
3646 <         * for an element, or null of there is no transformation (in
3647 <         * which case the action is not applied).
3648 <         * @param action the action
2534 >         * Returns the TreeNode (or null if not found) for the given key
2535 >         * starting at given root.
2536           */
2537 <        public <U> void forEach(BiFun<? super K, ? super V, ? extends U> transformer,
2538 <                                Action<U> action) {
2539 <            fjp.invoke(ForkJoinTasks.forEach
2540 <                       (ConcurrentHashMapV8.this, transformer, action));
2537 >        final TreeNode<K,V> findTreeNode(int h, Object k, Class<?> kc) {
2538 >            if (k != null) {
2539 >                TreeNode<K,V> p = this;
2540 >                do  {
2541 >                    int ph, dir; K pk; TreeNode<K,V> q;
2542 >                    TreeNode<K,V> pl = p.left, pr = p.right;
2543 >                    if ((ph = p.hash) > h)
2544 >                        p = pl;
2545 >                    else if (ph < h)
2546 >                        p = pr;
2547 >                    else if ((pk = p.key) == k || (pk != null && k.equals(pk)))
2548 >                        return p;
2549 >                    else if (pl == null && pr == null)
2550 >                        break;
2551 >                    else if ((kc != null ||
2552 >                              (kc = comparableClassFor(k)) != null) &&
2553 >                             (dir = compareComparables(kc, k, pk)) != 0)
2554 >                        p = (dir < 0) ? pl : pr;
2555 >                    else if (pl == null)
2556 >                        p = pr;
2557 >                    else if (pr == null ||
2558 >                             (q = pr.findTreeNode(h, k, kc)) == null)
2559 >                        p = pl;
2560 >                    else
2561 >                        return q;
2562 >                } while (p != null);
2563 >            }
2564 >            return null;
2565          }
2566 +    }
2567  
2568 <        /**
2569 <         * Returns a non-null result from applying the given search
2570 <         * function on each (key, value), or null if none.  Upon
2571 <         * success, further element processing is suppressed and the
2572 <         * results of any other parallel invocations of the search
2573 <         * function are ignored.
2574 <         *
2575 <         * @param searchFunction a function returning a non-null
2576 <         * result on success, else null
2577 <         * @return a non-null result from applying the given search
2578 <         * function on each (key, value), or null if none
2579 <         */
2580 <        public <U> U search(BiFun<? super K, ? super V, ? extends U> searchFunction) {
2581 <            return fjp.invoke(ForkJoinTasks.search
2582 <                              (ConcurrentHashMapV8.this, searchFunction));
2568 >    /* ---------------- TreeBins -------------- */
2569 >
2570 >    /**
2571 >     * TreeNodes used at the heads of bins. TreeBins do not hold user
2572 >     * keys or values, but instead point to list of TreeNodes and
2573 >     * their root. They also maintain a parasitic read-write lock
2574 >     * forcing writers (who hold bin lock) to wait for readers (who do
2575 >     * not) to complete before tree restructuring operations.
2576 >     */
2577 >    static final class TreeBin<K,V> extends Node<K,V> {
2578 >        TreeNode<K,V> root;
2579 >        volatile TreeNode<K,V> first;
2580 >        volatile Thread waiter;
2581 >        volatile int lockState;
2582 >        // values for lockState
2583 >        static final int WRITER = 1; // set while holding write lock
2584 >        static final int WAITER = 2; // set when waiting for write lock
2585 >        static final int READER = 4; // increment value for setting read lock
2586 >
2587 >        /**
2588 >         * Creates bin with initial set of nodes headed by b.
2589 >         */
2590 >        TreeBin(TreeNode<K,V> b) {
2591 >            super(TREEBIN, null, null, null);
2592 >            this.first = b;
2593 >            TreeNode<K,V> r = null;
2594 >            for (TreeNode<K,V> x = b, next; x != null; x = next) {
2595 >                next = (TreeNode<K,V>)x.next;
2596 >                x.left = x.right = null;
2597 >                if (r == null) {
2598 >                    x.parent = null;
2599 >                    x.red = false;
2600 >                    r = x;
2601 >                }
2602 >                else {
2603 >                    Object key = x.key;
2604 >                    int hash = x.hash;
2605 >                    Class<?> kc = null;
2606 >                    for (TreeNode<K,V> p = r;;) {
2607 >                        int dir, ph;
2608 >                        if ((ph = p.hash) > hash)
2609 >                            dir = -1;
2610 >                        else if (ph < hash)
2611 >                            dir = 1;
2612 >                        else if ((kc != null ||
2613 >                                  (kc = comparableClassFor(key)) != null))
2614 >                            dir = compareComparables(kc, key, p.key);
2615 >                        else
2616 >                            dir = 0;
2617 >                        TreeNode<K,V> xp = p;
2618 >                        if ((p = (dir <= 0) ? p.left : p.right) == null) {
2619 >                            x.parent = xp;
2620 >                            if (dir <= 0)
2621 >                                xp.left = x;
2622 >                            else
2623 >                                xp.right = x;
2624 >                            r = balanceInsertion(r, x);
2625 >                            break;
2626 >                        }
2627 >                    }
2628 >                }
2629 >            }
2630 >            this.root = r;
2631          }
2632  
2633          /**
2634 <         * Returns the result of accumulating the given transformation
3675 <         * of all (key, value) pairs using the given reducer to
3676 <         * combine values, or null if none.
3677 <         *
3678 <         * @param transformer a function returning the transformation
3679 <         * for an element, or null of there is no transformation (in
3680 <         * which case it is not combined).
3681 <         * @param reducer a commutative associative combining function
3682 <         * @return the result of accumulating the given transformation
3683 <         * of all (key, value) pairs
2634 >         * Acquires write lock for tree restructuring.
2635           */
2636 <        public <U> U reduce(BiFun<? super K, ? super V, ? extends U> transformer,
2637 <                            BiFun<? super U, ? super U, ? extends U> reducer) {
2638 <            return fjp.invoke(ForkJoinTasks.reduce
3688 <                              (ConcurrentHashMapV8.this, transformer, reducer));
2636 >        private final void lockRoot() {
2637 >            if (!U.compareAndSwapInt(this, LOCKSTATE, 0, WRITER))
2638 >                contendedLock(); // offload to separate method
2639          }
2640  
2641          /**
2642 <         * Returns the result of accumulating the given transformation
3693 <         * of all (key, value) pairs using the given reducer to
3694 <         * combine values, and the given basis as an identity value.
3695 <         *
3696 <         * @param transformer a function returning the transformation
3697 <         * for an element
3698 <         * @param basis the identity (initial default value) for the reduction
3699 <         * @param reducer a commutative associative combining function
3700 <         * @return the result of accumulating the given transformation
3701 <         * of all (key, value) pairs
2642 >         * Releases write lock for tree restructuring.
2643           */
2644 <        public double reduceToDouble(ObjectByObjectToDouble<? super K, ? super V> transformer,
2645 <                                     double basis,
3705 <                                     DoubleByDoubleToDouble reducer) {
3706 <            return fjp.invoke(ForkJoinTasks.reduceToDouble
3707 <                              (ConcurrentHashMapV8.this, transformer, basis, reducer));
2644 >        private final void unlockRoot() {
2645 >            lockState = 0;
2646          }
2647  
2648          /**
2649 <         * Returns the result of accumulating the given transformation
3712 <         * of all (key, value) pairs using the given reducer to
3713 <         * combine values, and the given basis as an identity value.
3714 <         *
3715 <         * @param transformer a function returning the transformation
3716 <         * for an element
3717 <         * @param basis the identity (initial default value) for the reduction
3718 <         * @param reducer a commutative associative combining function
3719 <         * @return the result of accumulating the given transformation
3720 <         * of all (key, value) pairs
2649 >         * Possibly blocks awaiting root lock.
2650           */
2651 <        public long reduceToLong(ObjectByObjectToLong<? super K, ? super V> transformer,
2652 <                                 long basis,
2653 <                                 LongByLongToLong reducer) {
2654 <            return fjp.invoke(ForkJoinTasks.reduceToLong
2655 <                              (ConcurrentHashMapV8.this, transformer, basis, reducer));
2651 >        private final void contendedLock() {
2652 >            boolean waiting = false;
2653 >            for (int s;;) {
2654 >                if (((s = lockState) & WRITER) == 0) {
2655 >                    if (U.compareAndSwapInt(this, LOCKSTATE, s, WRITER)) {
2656 >                        if (waiting)
2657 >                            waiter = null;
2658 >                        return;
2659 >                    }
2660 >                }
2661 >                else if ((s | WAITER) == 0) {
2662 >                    if (U.compareAndSwapInt(this, LOCKSTATE, s, s | WAITER)) {
2663 >                        waiting = true;
2664 >                        waiter = Thread.currentThread();
2665 >                    }
2666 >                }
2667 >                else if (waiting)
2668 >                    LockSupport.park(this);
2669 >            }
2670          }
2671  
2672          /**
2673 <         * Returns the result of accumulating the given transformation
2674 <         * of all (key, value) pairs using the given reducer to
2675 <         * combine values, and the given basis as an identity value.
3733 <         *
3734 <         * @param transformer a function returning the transformation
3735 <         * for an element
3736 <         * @param basis the identity (initial default value) for the reduction
3737 <         * @param reducer a commutative associative combining function
3738 <         * @return the result of accumulating the given transformation
3739 <         * of all (key, value) pairs
2673 >         * Returns matching node or null if none. Tries to search
2674 >         * using tree comparisons from root, but continues linear
2675 >         * search when lock not available.
2676           */
2677 <        public int reduceToInt(ObjectByObjectToInt<? super K, ? super V> transformer,
2678 <                               int basis,
2679 <                               IntByIntToInt reducer) {
2680 <            return fjp.invoke(ForkJoinTasks.reduceToInt
2681 <                              (ConcurrentHashMapV8.this, transformer, basis, reducer));
2677 >        final Node<K,V> find(int h, Object k) {
2678 >            if (k != null) {
2679 >                for (Node<K,V> e = first; e != null; e = e.next) {
2680 >                    int s; K ek;
2681 >                    if (((s = lockState) & (WAITER|WRITER)) != 0) {
2682 >                        if (e.hash == h &&
2683 >                            ((ek = e.key) == k || (ek != null && k.equals(ek))))
2684 >                            return e;
2685 >                    }
2686 >                    else if (U.compareAndSwapInt(this, LOCKSTATE, s,
2687 >                                                 s + READER)) {
2688 >                        TreeNode<K,V> r, p;
2689 >                        try {
2690 >                            p = ((r = root) == null ? null :
2691 >                                 r.findTreeNode(h, k, null));
2692 >                        } finally {
2693 >                            Thread w;
2694 >                            int ls;
2695 >                            do {} while (!U.compareAndSwapInt
2696 >                                         (this, LOCKSTATE,
2697 >                                          ls = lockState, ls - READER));
2698 >                            if (ls == (READER|WAITER) && (w = waiter) != null)
2699 >                                LockSupport.unpark(w);
2700 >                        }
2701 >                        return p;
2702 >                    }
2703 >                }
2704 >            }
2705 >            return null;
2706          }
2707  
2708          /**
2709 <         * Performs the given action for each key.
2710 <         *
3751 <         * @param action the action
2709 >         * Finds or adds a node.
2710 >         * @return null if added
2711           */
2712 <        public void forEachKey(Action<K> action) {
2713 <            fjp.invoke(ForkJoinTasks.forEachKey
2714 <                       (ConcurrentHashMapV8.this, action));
2712 >        final TreeNode<K,V> putTreeVal(int h, K k, V v) {
2713 >            Class<?> kc = null;
2714 >            for (TreeNode<K,V> p = root;;) {
2715 >                int dir, ph; K pk; TreeNode<K,V> q, pr;
2716 >                if (p == null) {
2717 >                    first = root = new TreeNode<K,V>(h, k, v, null, null);
2718 >                    break;
2719 >                }
2720 >                else if ((ph = p.hash) > h)
2721 >                    dir = -1;
2722 >                else if (ph < h)
2723 >                    dir = 1;
2724 >                else if ((pk = p.key) == k || (pk != null && k.equals(pk)))
2725 >                    return p;
2726 >                else if ((kc == null &&
2727 >                          (kc = comparableClassFor(k)) == null) ||
2728 >                         (dir = compareComparables(kc, k, pk)) == 0) {
2729 >                    if (p.left == null)
2730 >                        dir = 1;
2731 >                    else if ((pr = p.right) == null ||
2732 >                             (q = pr.findTreeNode(h, k, kc)) == null)
2733 >                        dir = -1;
2734 >                    else
2735 >                        return q;
2736 >                }
2737 >                TreeNode<K,V> xp = p;
2738 >                if ((p = (dir < 0) ? p.left : p.right) == null) {
2739 >                    TreeNode<K,V> x, f = first;
2740 >                    first = x = new TreeNode<K,V>(h, k, v, f, xp);
2741 >                    if (f != null)
2742 >                        f.prev = x;
2743 >                    if (dir < 0)
2744 >                        xp.left = x;
2745 >                    else
2746 >                        xp.right = x;
2747 >                    if (!xp.red)
2748 >                        x.red = true;
2749 >                    else {
2750 >                        lockRoot();
2751 >                        try {
2752 >                            root = balanceInsertion(root, x);
2753 >                        } finally {
2754 >                            unlockRoot();
2755 >                        }
2756 >                    }
2757 >                    break;
2758 >                }
2759 >            }
2760 >            assert checkInvariants(root);
2761 >            return null;
2762          }
2763  
2764          /**
2765 <         * Performs the given action for each non-null transformation
2766 <         * of each key.
2765 >         * Removes the given node, that must be present before this
2766 >         * call.  This is messier than typical red-black deletion code
2767 >         * because we cannot swap the contents of an interior node
2768 >         * with a leaf successor that is pinned by "next" pointers
2769 >         * that are accessible independently of lock. So instead we
2770 >         * swap the tree linkages.
2771           *
2772 <         * @param transformer a function returning the transformation
3763 <         * for an element, or null of there is no transformation (in
3764 <         * which case the action is not applied).
3765 <         * @param action the action
2772 >         * @return true if now too small, so should be untreeified
2773           */
2774 <        public <U> void forEachKey(Fun<? super K, ? extends U> transformer,
2775 <                                   Action<U> action) {
2776 <            fjp.invoke(ForkJoinTasks.forEachKey
2777 <                       (ConcurrentHashMapV8.this, transformer, action));
2774 >        final boolean removeTreeNode(TreeNode<K,V> p) {
2775 >            TreeNode<K,V> next = (TreeNode<K,V>)p.next;
2776 >            TreeNode<K,V> pred = p.prev;  // unlink traversal pointers
2777 >            TreeNode<K,V> r, rl;
2778 >            if (pred == null)
2779 >                first = next;
2780 >            else
2781 >                pred.next = next;
2782 >            if (next != null)
2783 >                next.prev = pred;
2784 >            if (first == null) {
2785 >                root = null;
2786 >                return true;
2787 >            }
2788 >            if ((r = root) == null || r.right == null || // too small
2789 >                (rl = r.left) == null || rl.left == null)
2790 >                return true;
2791 >            lockRoot();
2792 >            try {
2793 >                TreeNode<K,V> replacement;
2794 >                TreeNode<K,V> pl = p.left;
2795 >                TreeNode<K,V> pr = p.right;
2796 >                if (pl != null && pr != null) {
2797 >                    TreeNode<K,V> s = pr, sl;
2798 >                    while ((sl = s.left) != null) // find successor
2799 >                        s = sl;
2800 >                    boolean c = s.red; s.red = p.red; p.red = c; // swap colors
2801 >                    TreeNode<K,V> sr = s.right;
2802 >                    TreeNode<K,V> pp = p.parent;
2803 >                    if (s == pr) { // p was s's direct parent
2804 >                        p.parent = s;
2805 >                        s.right = p;
2806 >                    }
2807 >                    else {
2808 >                        TreeNode<K,V> sp = s.parent;
2809 >                        if ((p.parent = sp) != null) {
2810 >                            if (s == sp.left)
2811 >                                sp.left = p;
2812 >                            else
2813 >                                sp.right = p;
2814 >                        }
2815 >                        if ((s.right = pr) != null)
2816 >                            pr.parent = s;
2817 >                    }
2818 >                    p.left = null;
2819 >                    if ((p.right = sr) != null)
2820 >                        sr.parent = p;
2821 >                    if ((s.left = pl) != null)
2822 >                        pl.parent = s;
2823 >                    if ((s.parent = pp) == null)
2824 >                        r = s;
2825 >                    else if (p == pp.left)
2826 >                        pp.left = s;
2827 >                    else
2828 >                        pp.right = s;
2829 >                    if (sr != null)
2830 >                        replacement = sr;
2831 >                    else
2832 >                        replacement = p;
2833 >                }
2834 >                else if (pl != null)
2835 >                    replacement = pl;
2836 >                else if (pr != null)
2837 >                    replacement = pr;
2838 >                else
2839 >                    replacement = p;
2840 >                if (replacement != p) {
2841 >                    TreeNode<K,V> pp = replacement.parent = p.parent;
2842 >                    if (pp == null)
2843 >                        r = replacement;
2844 >                    else if (p == pp.left)
2845 >                        pp.left = replacement;
2846 >                    else
2847 >                        pp.right = replacement;
2848 >                    p.left = p.right = p.parent = null;
2849 >                }
2850 >
2851 >                root = (p.red) ? r : balanceDeletion(r, replacement);
2852 >
2853 >                if (p == replacement) {  // detach pointers
2854 >                    TreeNode<K,V> pp;
2855 >                    if ((pp = p.parent) != null) {
2856 >                        if (p == pp.left)
2857 >                            pp.left = null;
2858 >                        else if (p == pp.right)
2859 >                            pp.right = null;
2860 >                        p.parent = null;
2861 >                    }
2862 >                }
2863 >            } finally {
2864 >                unlockRoot();
2865 >            }
2866 >            assert checkInvariants(root);
2867 >            return false;
2868          }
2869  
2870 <        /**
2871 <         * Returns a non-null result from applying the given search
2872 <         * function on each key, or null if none. Upon success,
2873 <         * further element processing is suppressed and the results of
2874 <         * any other parallel invocations of the search function are
2875 <         * ignored.
2876 <         *
2877 <         * @param searchFunction a function returning a non-null
2878 <         * result on success, else null
2879 <         * @return a non-null result from applying the given search
2880 <         * function on each key, or null if none
2881 <         */
2882 <        public <U> U searchKeys(Fun<? super K, ? extends U> searchFunction) {
2883 <            return fjp.invoke(ForkJoinTasks.searchKeys
2884 <                              (ConcurrentHashMapV8.this, searchFunction));
2870 >        /* ------------------------------------------------------------ */
2871 >        // Red-black tree methods, all adapted from CLR
2872 >
2873 >        static <K,V> TreeNode<K,V> rotateLeft(TreeNode<K,V> root,
2874 >                                              TreeNode<K,V> p) {
2875 >            TreeNode<K,V> r, pp, rl;
2876 >            if (p != null && (r = p.right) != null) {
2877 >                if ((rl = p.right = r.left) != null)
2878 >                    rl.parent = p;
2879 >                if ((pp = r.parent = p.parent) == null)
2880 >                    (root = r).red = false;
2881 >                else if (pp.left == p)
2882 >                    pp.left = r;
2883 >                else
2884 >                    pp.right = r;
2885 >                r.left = p;
2886 >                p.parent = r;
2887 >            }
2888 >            return root;
2889          }
2890  
2891 <        /**
2892 <         * Returns the result of accumulating all keys using the given
2893 <         * reducer to combine values, or null if none.
2894 <         *
2895 <         * @param reducer a commutative associative combining function
2896 <         * @return the result of accumulating all keys using the given
2897 <         * reducer to combine values, or null if none
2898 <         */
2899 <        public K reduceKeys(BiFun<? super K, ? super K, ? extends K> reducer) {
2900 <            return fjp.invoke(ForkJoinTasks.reduceKeys
2901 <                              (ConcurrentHashMapV8.this, reducer));
2891 >        static <K,V> TreeNode<K,V> rotateRight(TreeNode<K,V> root,
2892 >                                               TreeNode<K,V> p) {
2893 >            TreeNode<K,V> l, pp, lr;
2894 >            if (p != null && (l = p.left) != null) {
2895 >                if ((lr = p.left = l.right) != null)
2896 >                    lr.parent = p;
2897 >                if ((pp = l.parent = p.parent) == null)
2898 >                    (root = l).red = false;
2899 >                else if (pp.right == p)
2900 >                    pp.right = l;
2901 >                else
2902 >                    pp.left = l;
2903 >                l.right = p;
2904 >                p.parent = l;
2905 >            }
2906 >            return root;
2907          }
2908  
2909 <        /**
2910 <         * Returns the result of accumulating the given transformation
2911 <         * of all keys using the given reducer to combine values, or
2912 <         * null if none.
2913 <         *
2914 <         * @param transformer a function returning the transformation
2915 <         * for an element, or null of there is no transformation (in
2916 <         * which case it is not combined).
2917 <         * @param reducer a commutative associative combining function
2918 <         * @return the result of accumulating the given transformation
2919 <         * of all keys
2920 <         */
2921 <        public <U> U reduceKeys(Fun<? super K, ? extends U> transformer,
2922 <                                BiFun<? super U, ? super U, ? extends U> reducer) {
2923 <            return fjp.invoke(ForkJoinTasks.reduceKeys
2924 <                              (ConcurrentHashMapV8.this, transformer, reducer));
2909 >        static <K,V> TreeNode<K,V> balanceInsertion(TreeNode<K,V> root,
2910 >                                                    TreeNode<K,V> x) {
2911 >            x.red = true;
2912 >            for (TreeNode<K,V> xp, xpp, xppl, xppr;;) {
2913 >                if ((xp = x.parent) == null) {
2914 >                    x.red = false;
2915 >                    return x;
2916 >                }
2917 >                else if (!xp.red || (xpp = xp.parent) == null)
2918 >                    return root;
2919 >                if (xp == (xppl = xpp.left)) {
2920 >                    if ((xppr = xpp.right) != null && xppr.red) {
2921 >                        xppr.red = false;
2922 >                        xp.red = false;
2923 >                        xpp.red = true;
2924 >                        x = xpp;
2925 >                    }
2926 >                    else {
2927 >                        if (x == xp.right) {
2928 >                            root = rotateLeft(root, x = xp);
2929 >                            xpp = (xp = x.parent) == null ? null : xp.parent;
2930 >                        }
2931 >                        if (xp != null) {
2932 >                            xp.red = false;
2933 >                            if (xpp != null) {
2934 >                                xpp.red = true;
2935 >                                root = rotateRight(root, xpp);
2936 >                            }
2937 >                        }
2938 >                    }
2939 >                }
2940 >                else {
2941 >                    if (xppl != null && xppl.red) {
2942 >                        xppl.red = false;
2943 >                        xp.red = false;
2944 >                        xpp.red = true;
2945 >                        x = xpp;
2946 >                    }
2947 >                    else {
2948 >                        if (x == xp.left) {
2949 >                            root = rotateRight(root, x = xp);
2950 >                            xpp = (xp = x.parent) == null ? null : xp.parent;
2951 >                        }
2952 >                        if (xp != null) {
2953 >                            xp.red = false;
2954 >                            if (xpp != null) {
2955 >                                xpp.red = true;
2956 >                                root = rotateLeft(root, xpp);
2957 >                            }
2958 >                        }
2959 >                    }
2960 >                }
2961 >            }
2962          }
2963  
2964 <        /**
2965 <         * Returns the result of accumulating the given transformation
2966 <         * of all keys using the given reducer to combine values, and
2967 <         * the given basis as an identity value.
2968 <         *
2969 <         * @param transformer a function returning the transformation
2970 <         * for an element
2971 <         * @param basis the identity (initial default value) for the reduction
2972 <         * @param reducer a commutative associative combining function
2973 <         * @return  the result of accumulating the given transformation
2974 <         * of all keys
2975 <         */
2976 <        public double reduceKeysToDouble(ObjectToDouble<? super K> transformer,
2977 <                                         double basis,
2978 <                                         DoubleByDoubleToDouble reducer) {
2979 <            return fjp.invoke(ForkJoinTasks.reduceKeysToDouble
2980 <                              (ConcurrentHashMapV8.this, transformer, basis, reducer));
2964 >        static <K,V> TreeNode<K,V> balanceDeletion(TreeNode<K,V> root,
2965 >                                                   TreeNode<K,V> x) {
2966 >            for (TreeNode<K,V> xp, xpl, xpr;;)  {
2967 >                if (x == null || x == root)
2968 >                    return root;
2969 >                else if ((xp = x.parent) == null) {
2970 >                    x.red = false;
2971 >                    return x;
2972 >                }
2973 >                else if (x.red) {
2974 >                    x.red = false;
2975 >                    return root;
2976 >                }
2977 >                else if ((xpl = xp.left) == x) {
2978 >                    if ((xpr = xp.right) != null && xpr.red) {
2979 >                        xpr.red = false;
2980 >                        xp.red = true;
2981 >                        root = rotateLeft(root, xp);
2982 >                        xpr = (xp = x.parent) == null ? null : xp.right;
2983 >                    }
2984 >                    if (xpr == null)
2985 >                        x = xp;
2986 >                    else {
2987 >                        TreeNode<K,V> sl = xpr.left, sr = xpr.right;
2988 >                        if ((sr == null || !sr.red) &&
2989 >                            (sl == null || !sl.red)) {
2990 >                            xpr.red = true;
2991 >                            x = xp;
2992 >                        }
2993 >                        else {
2994 >                            if (sr == null || !sr.red) {
2995 >                                if (sl != null)
2996 >                                    sl.red = false;
2997 >                                xpr.red = true;
2998 >                                root = rotateRight(root, xpr);
2999 >                                xpr = (xp = x.parent) == null ?
3000 >                                    null : xp.right;
3001 >                            }
3002 >                            if (xpr != null) {
3003 >                                xpr.red = (xp == null) ? false : xp.red;
3004 >                                if ((sr = xpr.right) != null)
3005 >                                    sr.red = false;
3006 >                            }
3007 >                            if (xp != null) {
3008 >                                xp.red = false;
3009 >                                root = rotateLeft(root, xp);
3010 >                            }
3011 >                            x = root;
3012 >                        }
3013 >                    }
3014 >                }
3015 >                else { // symmetric
3016 >                    if (xpl != null && xpl.red) {
3017 >                        xpl.red = false;
3018 >                        xp.red = true;
3019 >                        root = rotateRight(root, xp);
3020 >                        xpl = (xp = x.parent) == null ? null : xp.left;
3021 >                    }
3022 >                    if (xpl == null)
3023 >                        x = xp;
3024 >                    else {
3025 >                        TreeNode<K,V> sl = xpl.left, sr = xpl.right;
3026 >                        if ((sl == null || !sl.red) &&
3027 >                            (sr == null || !sr.red)) {
3028 >                            xpl.red = true;
3029 >                            x = xp;
3030 >                        }
3031 >                        else {
3032 >                            if (sl == null || !sl.red) {
3033 >                                if (sr != null)
3034 >                                    sr.red = false;
3035 >                                xpl.red = true;
3036 >                                root = rotateLeft(root, xpl);
3037 >                                xpl = (xp = x.parent) == null ?
3038 >                                    null : xp.left;
3039 >                            }
3040 >                            if (xpl != null) {
3041 >                                xpl.red = (xp == null) ? false : xp.red;
3042 >                                if ((sl = xpl.left) != null)
3043 >                                    sl.red = false;
3044 >                            }
3045 >                            if (xp != null) {
3046 >                                xp.red = false;
3047 >                                root = rotateRight(root, xp);
3048 >                            }
3049 >                            x = root;
3050 >                        }
3051 >                    }
3052 >                }
3053 >            }
3054          }
3055  
3056          /**
3057 <         * Returns the result of accumulating the given transformation
3842 <         * of all keys using the given reducer to combine values, and
3843 <         * the given basis as an identity value.
3844 <         *
3845 <         * @param transformer a function returning the transformation
3846 <         * for an element
3847 <         * @param basis the identity (initial default value) for the reduction
3848 <         * @param reducer a commutative associative combining function
3849 <         * @return the result of accumulating the given transformation
3850 <         * of all keys
3057 >         * Recursive invariant check
3058           */
3059 <        public long reduceKeysToLong(ObjectToLong<? super K> transformer,
3060 <                                     long basis,
3061 <                                     LongByLongToLong reducer) {
3062 <            return fjp.invoke(ForkJoinTasks.reduceKeysToLong
3063 <                              (ConcurrentHashMapV8.this, transformer, basis, reducer));
3059 >        static <K,V> boolean checkInvariants(TreeNode<K,V> t) {
3060 >            TreeNode<K,V> tp = t.parent, tl = t.left, tr = t.right,
3061 >                tb = t.prev, tn = (TreeNode<K,V>)t.next;
3062 >            if (tb != null && tb.next != t)
3063 >                return false;
3064 >            if (tn != null && tn.prev != t)
3065 >                return false;
3066 >            if (tp != null && t != tp.left && t != tp.right)
3067 >                return false;
3068 >            if (tl != null && (tl.parent != t || tl.hash > t.hash))
3069 >                return false;
3070 >            if (tr != null && (tr.parent != t || tr.hash < t.hash))
3071 >                return false;
3072 >            if (t.red && tl != null && tl.red && tr != null && tr.red)
3073 >                return false;
3074 >            if (tl != null && !checkInvariants(tl))
3075 >                return false;
3076 >            if (tr != null && !checkInvariants(tr))
3077 >                return false;
3078 >            return true;
3079          }
3080  
3081 <        /**
3082 <         * Returns the result of accumulating the given transformation
3083 <         * of all keys using the given reducer to combine values, and
3084 <         * the given basis as an identity value.
3085 <         *
3086 <         * @param transformer a function returning the transformation
3087 <         * for an element
3088 <         * @param basis the identity (initial default value) for the reduction
3089 <         * @param reducer a commutative associative combining function
3090 <         * @return the result of accumulating the given transformation
3091 <         * of all keys
3870 <         */
3871 <        public int reduceKeysToInt(ObjectToInt<? super K> transformer,
3872 <                                   int basis,
3873 <                                   IntByIntToInt reducer) {
3874 <            return fjp.invoke(ForkJoinTasks.reduceKeysToInt
3875 <                              (ConcurrentHashMapV8.this, transformer, basis, reducer));
3081 >        private static final sun.misc.Unsafe U;
3082 >        private static final long LOCKSTATE;
3083 >        static {
3084 >            try {
3085 >                U = getUnsafe();
3086 >                Class<?> k = TreeBin.class;
3087 >                LOCKSTATE = U.objectFieldOffset
3088 >                    (k.getDeclaredField("lockState"));
3089 >            } catch (Exception e) {
3090 >                throw new Error(e);
3091 >            }
3092          }
3093 +    }
3094  
3095 <        /**
3096 <         * Performs the given action for each value.
3097 <         *
3098 <         * @param action the action
3099 <         */
3100 <        public void forEachValue(Action<V> action) {
3101 <            fjp.invoke(ForkJoinTasks.forEachValue
3102 <                       (ConcurrentHashMapV8.this, action));
3095 >    /* ----------------Table Traversal -------------- */
3096 >
3097 >    /**
3098 >     * Encapsulates traversal for methods such as containsValue; also
3099 >     * serves as a base class for other iterators and spliterators.
3100 >     *
3101 >     * Method advance visits once each still-valid node that was
3102 >     * reachable upon iterator construction. It might miss some that
3103 >     * were added to a bin after the bin was visited, which is OK wrt
3104 >     * consistency guarantees. Maintaining this property in the face
3105 >     * of possible ongoing resizes requires a fair amount of
3106 >     * bookkeeping state that is difficult to optimize away amidst
3107 >     * volatile accesses.  Even so, traversal maintains reasonable
3108 >     * throughput.
3109 >     *
3110 >     * Normally, iteration proceeds bin-by-bin traversing lists.
3111 >     * However, if the table has been resized, then all future steps
3112 >     * must traverse both the bin at the current index as well as at
3113 >     * (index + baseSize); and so on for further resizings. To
3114 >     * paranoically cope with potential sharing by users of iterators
3115 >     * across threads, iteration terminates if a bounds checks fails
3116 >     * for a table read.
3117 >     */
3118 >    static class Traverser<K,V> {
3119 >        Node<K,V>[] tab;        // current table; updated if resized
3120 >        Node<K,V> next;         // the next entry to use
3121 >        int index;              // index of bin to use next
3122 >        int baseIndex;          // current index of initial table
3123 >        int baseLimit;          // index bound for initial table
3124 >        final int baseSize;     // initial table size
3125 >
3126 >        Traverser(Node<K,V>[] tab, int size, int index, int limit) {
3127 >            this.tab = tab;
3128 >            this.baseSize = size;
3129 >            this.baseIndex = this.index = index;
3130 >            this.baseLimit = limit;
3131 >            this.next = null;
3132          }
3133  
3134          /**
3135 <         * Performs the given action for each non-null transformation
3136 <         * of each value.
3137 <         *
3138 <         * @param transformer a function returning the transformation
3139 <         * for an element, or null of there is no transformation (in
3140 <         * which case the action is not applied).
3141 <         */
3142 <        public <U> void forEachValue(Fun<? super V, ? extends U> transformer,
3143 <                                     Action<U> action) {
3144 <            fjp.invoke(ForkJoinTasks.forEachValue
3145 <                       (ConcurrentHashMapV8.this, transformer, action));
3135 >         * Advances if possible, returning next valid node, or null if none.
3136 >         */
3137 >        final Node<K,V> advance() {
3138 >            Node<K,V> e;
3139 >            if ((e = next) != null)
3140 >                e = e.next;
3141 >            for (;;) {
3142 >                Node<K,V>[] t; int i, n; K ek;  // must use locals in checks
3143 >                if (e != null)
3144 >                    return next = e;
3145 >                if (baseIndex >= baseLimit || (t = tab) == null ||
3146 >                    (n = t.length) <= (i = index) || i < 0)
3147 >                    return next = null;
3148 >                if ((e = tabAt(t, index)) != null && e.hash < 0) {
3149 >                    if (e instanceof ForwardingNode) {
3150 >                        tab = ((ForwardingNode<K,V>)e).nextTable;
3151 >                        e = null;
3152 >                        continue;
3153 >                    }
3154 >                    else if (e instanceof TreeBin)
3155 >                        e = ((TreeBin<K,V>)e).first;
3156 >                    else
3157 >                        e = null;
3158 >                }
3159 >                if ((index += baseSize) >= n)
3160 >                    index = ++baseIndex;    // visit upper slots if present
3161 >            }
3162          }
3163 +    }
3164  
3165 <        /**
3166 <         * Returns a non-null result from applying the given search
3167 <         * function on each value, or null if none.  Upon success,
3168 <         * further element processing is suppressed and the results of
3169 <         * any other parallel invocations of the search function are
3170 <         * ignored.
3171 <         *
3172 <         * @param searchFunction a function returning a non-null
3173 <         * result on success, else null
3174 <         * @return a non-null result from applying the given search
3175 <         * function on each value, or null if none
3176 <         *
3914 <         */
3915 <        public <U> U searchValues(Fun<? super V, ? extends U> searchFunction) {
3916 <            return fjp.invoke(ForkJoinTasks.searchValues
3917 <                              (ConcurrentHashMapV8.this, searchFunction));
3165 >    /**
3166 >     * Base of key, value, and entry Iterators. Adds fields to
3167 >     * Traverser to support iterator.remove.
3168 >     */
3169 >    static class BaseIterator<K,V> extends Traverser<K,V> {
3170 >        final ConcurrentHashMapV8<K,V> map;
3171 >        Node<K,V> lastReturned;
3172 >        BaseIterator(Node<K,V>[] tab, int size, int index, int limit,
3173 >                    ConcurrentHashMapV8<K,V> map) {
3174 >            super(tab, size, index, limit);
3175 >            this.map = map;
3176 >            advance();
3177          }
3178  
3179 <        /**
3180 <         * Returns the result of accumulating all values using the
3181 <         * given reducer to combine values, or null if none.
3182 <         *
3183 <         * @param reducer a commutative associative combining function
3184 <         * @return  the result of accumulating all values
3185 <         */
3186 <        public V reduceValues(BiFun<? super V, ? super V, ? extends V> reducer) {
3187 <            return fjp.invoke(ForkJoinTasks.reduceValues
3929 <                              (ConcurrentHashMapV8.this, reducer));
3179 >        public final boolean hasNext() { return next != null; }
3180 >        public final boolean hasMoreElements() { return next != null; }
3181 >
3182 >        public final void remove() {
3183 >            Node<K,V> p;
3184 >            if ((p = lastReturned) == null)
3185 >                throw new IllegalStateException();
3186 >            lastReturned = null;
3187 >            map.replaceNode(p.key, null, null);
3188          }
3189 +    }
3190  
3191 <        /**
3192 <         * Returns the result of accumulating the given transformation
3193 <         * of all values using the given reducer to combine values, or
3194 <         * null if none.
3195 <         *
3937 <         * @param transformer a function returning the transformation
3938 <         * for an element, or null of there is no transformation (in
3939 <         * which case it is not combined).
3940 <         * @param reducer a commutative associative combining function
3941 <         * @return the result of accumulating the given transformation
3942 <         * of all values
3943 <         */
3944 <        public <U> U reduceValues(Fun<? super V, ? extends U> transformer,
3945 <                                  BiFun<? super U, ? super U, ? extends U> reducer) {
3946 <            return fjp.invoke(ForkJoinTasks.reduceValues
3947 <                              (ConcurrentHashMapV8.this, transformer, reducer));
3191 >    static final class KeyIterator<K,V> extends BaseIterator<K,V>
3192 >        implements Iterator<K>, Enumeration<K> {
3193 >        KeyIterator(Node<K,V>[] tab, int index, int size, int limit,
3194 >                    ConcurrentHashMapV8<K,V> map) {
3195 >            super(tab, index, size, limit, map);
3196          }
3197  
3198 <        /**
3199 <         * Returns the result of accumulating the given transformation
3200 <         * of all values using the given reducer to combine values,
3201 <         * and the given basis as an identity value.
3202 <         *
3203 <         * @param transformer a function returning the transformation
3204 <         * for an element
3205 <         * @param basis the identity (initial default value) for the reduction
3958 <         * @param reducer a commutative associative combining function
3959 <         * @return the result of accumulating the given transformation
3960 <         * of all values
3961 <         */
3962 <        public double reduceValuesToDouble(ObjectToDouble<? super V> transformer,
3963 <                                           double basis,
3964 <                                           DoubleByDoubleToDouble reducer) {
3965 <            return fjp.invoke(ForkJoinTasks.reduceValuesToDouble
3966 <                              (ConcurrentHashMapV8.this, transformer, basis, reducer));
3198 >        public final K next() {
3199 >            Node<K,V> p;
3200 >            if ((p = next) == null)
3201 >                throw new NoSuchElementException();
3202 >            K k = p.key;
3203 >            lastReturned = p;
3204 >            advance();
3205 >            return k;
3206          }
3207  
3208 <        /**
3209 <         * Returns the result of accumulating the given transformation
3210 <         * of all values using the given reducer to combine values,
3211 <         * and the given basis as an identity value.
3212 <         *
3213 <         * @param transformer a function returning the transformation
3214 <         * for an element
3215 <         * @param basis the identity (initial default value) for the reduction
3977 <         * @param reducer a commutative associative combining function
3978 <         * @return the result of accumulating the given transformation
3979 <         * of all values
3980 <         */
3981 <        public long reduceValuesToLong(ObjectToLong<? super V> transformer,
3982 <                                       long basis,
3983 <                                       LongByLongToLong reducer) {
3984 <            return fjp.invoke(ForkJoinTasks.reduceValuesToLong
3985 <                              (ConcurrentHashMapV8.this, transformer, basis, reducer));
3208 >        public final K nextElement() { return next(); }
3209 >    }
3210 >
3211 >    static final class ValueIterator<K,V> extends BaseIterator<K,V>
3212 >        implements Iterator<V>, Enumeration<V> {
3213 >        ValueIterator(Node<K,V>[] tab, int index, int size, int limit,
3214 >                      ConcurrentHashMapV8<K,V> map) {
3215 >            super(tab, index, size, limit, map);
3216          }
3217  
3218 <        /**
3219 <         * Returns the result of accumulating the given transformation
3220 <         * of all values using the given reducer to combine values,
3221 <         * and the given basis as an identity value.
3222 <         *
3223 <         * @param transformer a function returning the transformation
3224 <         * for an element
3225 <         * @param basis the identity (initial default value) for the reduction
3996 <         * @param reducer a commutative associative combining function
3997 <         * @return the result of accumulating the given transformation
3998 <         * of all values
3999 <         */
4000 <        public int reduceValuesToInt(ObjectToInt<? super V> transformer,
4001 <                                     int basis,
4002 <                                     IntByIntToInt reducer) {
4003 <            return fjp.invoke(ForkJoinTasks.reduceValuesToInt
4004 <                              (ConcurrentHashMapV8.this, transformer, basis, reducer));
3218 >        public final V next() {
3219 >            Node<K,V> p;
3220 >            if ((p = next) == null)
3221 >                throw new NoSuchElementException();
3222 >            V v = p.val;
3223 >            lastReturned = p;
3224 >            advance();
3225 >            return v;
3226          }
3227  
3228 <        /**
3229 <         * Performs the given action for each entry.
3230 <         *
3231 <         * @param action the action
3232 <         */
3233 <        public void forEachEntry(Action<Map.Entry<K,V>> action) {
3234 <            fjp.invoke(ForkJoinTasks.forEachEntry
3235 <                       (ConcurrentHashMapV8.this, action));
3228 >        public final V nextElement() { return next(); }
3229 >    }
3230 >
3231 >    static final class EntryIterator<K,V> extends BaseIterator<K,V>
3232 >        implements Iterator<Map.Entry<K,V>> {
3233 >        EntryIterator(Node<K,V>[] tab, int index, int size, int limit,
3234 >                      ConcurrentHashMapV8<K,V> map) {
3235 >            super(tab, index, size, limit, map);
3236          }
3237  
3238 <        /**
3239 <         * Performs the given action for each non-null transformation
3240 <         * of each entry.
3241 <         *
3242 <         * @param transformer a function returning the transformation
3243 <         * for an element, or null of there is no transformation (in
3244 <         * which case the action is not applied).
3245 <         * @param action the action
3246 <         */
4026 <        public <U> void forEachEntry(Fun<Map.Entry<K,V>, ? extends U> transformer,
4027 <                                     Action<U> action) {
4028 <            fjp.invoke(ForkJoinTasks.forEachEntry
4029 <                       (ConcurrentHashMapV8.this, transformer, action));
3238 >        public final Map.Entry<K,V> next() {
3239 >            Node<K,V> p;
3240 >            if ((p = next) == null)
3241 >                throw new NoSuchElementException();
3242 >            K k = p.key;
3243 >            V v = p.val;
3244 >            lastReturned = p;
3245 >            advance();
3246 >            return new MapEntry<K,V>(k, v, map);
3247          }
3248 +    }
3249  
3250 <        /**
3251 <         * Returns a non-null result from applying the given search
3252 <         * function on each entry, or null if none.  Upon success,
3253 <         * further element processing is suppressed and the results of
3254 <         * any other parallel invocations of the search function are
3255 <         * ignored.
3256 <         *
3257 <         * @param searchFunction a function returning a non-null
3258 <         * result on success, else null
3259 <         * @return a non-null result from applying the given search
3260 <         * function on each entry, or null if none
4043 <         */
4044 <        public <U> U searchEntries(Fun<Map.Entry<K,V>, ? extends U> searchFunction) {
4045 <            return fjp.invoke(ForkJoinTasks.searchEntries
4046 <                              (ConcurrentHashMapV8.this, searchFunction));
3250 >    /**
3251 >     * Exported Entry for EntryIterator
3252 >     */
3253 >    static final class MapEntry<K,V> implements Map.Entry<K,V> {
3254 >        final K key; // non-null
3255 >        V val;       // non-null
3256 >        final ConcurrentHashMapV8<K,V> map;
3257 >        MapEntry(K key, V val, ConcurrentHashMapV8<K,V> map) {
3258 >            this.key = key;
3259 >            this.val = val;
3260 >            this.map = map;
3261          }
3262 +        public K getKey()        { return key; }
3263 +        public V getValue()      { return val; }
3264 +        public int hashCode()    { return key.hashCode() ^ val.hashCode(); }
3265 +        public String toString() { return key + "=" + val; }
3266  
3267 <        /**
3268 <         * Returns the result of accumulating all entries using the
3269 <         * given reducer to combine values, or null if none.
3270 <         *
3271 <         * @param reducer a commutative associative combining function
3272 <         * @return the result of accumulating all entries
3273 <         */
4056 <        public Map.Entry<K,V> reduceEntries(BiFun<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer) {
4057 <            return fjp.invoke(ForkJoinTasks.reduceEntries
4058 <                              (ConcurrentHashMapV8.this, reducer));
3267 >        public boolean equals(Object o) {
3268 >            Object k, v; Map.Entry<?,?> e;
3269 >            return ((o instanceof Map.Entry) &&
3270 >                    (k = (e = (Map.Entry<?,?>)o).getKey()) != null &&
3271 >                    (v = e.getValue()) != null &&
3272 >                    (k == key || k.equals(key)) &&
3273 >                    (v == val || v.equals(val)));
3274          }
3275  
3276          /**
3277 <         * Returns the result of accumulating the given transformation
3278 <         * of all entries using the given reducer to combine values,
3279 <         * or null if none.
3280 <         *
3281 <         * @param transformer a function returning the transformation
3282 <         * for an element, or null of there is no transformation (in
4068 <         * which case it is not combined).
4069 <         * @param reducer a commutative associative combining function
4070 <         * @return the result of accumulating the given transformation
4071 <         * of all entries
3277 >         * Sets our entry's value and writes through to the map. The
3278 >         * value to return is somewhat arbitrary here. Since we do not
3279 >         * necessarily track asynchronous changes, the most recent
3280 >         * "previous" value could be different from what we return (or
3281 >         * could even have been removed, in which case the put will
3282 >         * re-establish). We do not and cannot guarantee more.
3283           */
3284 <        public <U> U reduceEntries(Fun<Map.Entry<K,V>, ? extends U> transformer,
3285 <                                   BiFun<? super U, ? super U, ? extends U> reducer) {
3286 <            return fjp.invoke(ForkJoinTasks.reduceEntries
3287 <                              (ConcurrentHashMapV8.this, transformer, reducer));
3284 >        public V setValue(V value) {
3285 >            if (value == null) throw new NullPointerException();
3286 >            V v = val;
3287 >            val = value;
3288 >            map.put(key, value);
3289 >            return v;
3290          }
3291 +    }
3292  
3293 <        /**
3294 <         * Returns the result of accumulating the given transformation
3295 <         * of all entries using the given reducer to combine values,
3296 <         * and the given basis as an identity value.
3297 <         *
3298 <         * @param transformer a function returning the transformation
3299 <         * for an element
3300 <         * @param basis the identity (initial default value) for the reduction
3301 <         * @param reducer a commutative associative combining function
3302 <         * @return the result of accumulating the given transformation
3303 <         * of all entries
3304 <         */
3305 <        public double reduceEntriesToDouble(ObjectToDouble<Map.Entry<K,V>> transformer,
3306 <                                            double basis,
4093 <                                            DoubleByDoubleToDouble reducer) {
4094 <            return fjp.invoke(ForkJoinTasks.reduceEntriesToDouble
4095 <                              (ConcurrentHashMapV8.this, transformer, basis, reducer));
3293 >    static final class KeySpliterator<K,V> extends Traverser<K,V>
3294 >        implements ConcurrentHashMapSpliterator<K> {
3295 >        long est;               // size estimate
3296 >        KeySpliterator(Node<K,V>[] tab, int size, int index, int limit,
3297 >                       long est) {
3298 >            super(tab, size, index, limit);
3299 >            this.est = est;
3300 >        }
3301 >
3302 >        public ConcurrentHashMapSpliterator<K> trySplit() {
3303 >            int i, f, h;
3304 >            return (h = ((i = baseIndex) + (f = baseLimit)) >>> 1) <= i ? null :
3305 >                new KeySpliterator<K,V>(tab, baseSize, baseLimit = h,
3306 >                                        f, est >>>= 1);
3307          }
3308  
3309 <        /**
3310 <         * Returns the result of accumulating the given transformation
3311 <         * of all entries using the given reducer to combine values,
3312 <         * and the given basis as an identity value.
4102 <         *
4103 <         * @param transformer a function returning the transformation
4104 <         * for an element
4105 <         * @param basis the identity (initial default value) for the reduction
4106 <         * @param reducer a commutative associative combining function
4107 <         * @return  the result of accumulating the given transformation
4108 <         * of all entries
4109 <         */
4110 <        public long reduceEntriesToLong(ObjectToLong<Map.Entry<K,V>> transformer,
4111 <                                        long basis,
4112 <                                        LongByLongToLong reducer) {
4113 <            return fjp.invoke(ForkJoinTasks.reduceEntriesToLong
4114 <                              (ConcurrentHashMapV8.this, transformer, basis, reducer));
3309 >        public void forEachRemaining(Action<? super K> action) {
3310 >            if (action == null) throw new NullPointerException();
3311 >            for (Node<K,V> p; (p = advance()) != null;)
3312 >                action.apply(p.key);
3313          }
3314  
3315 <        /**
3316 <         * Returns the result of accumulating the given transformation
3317 <         * of all entries using the given reducer to combine values,
3318 <         * and the given basis as an identity value.
3319 <         *
3320 <         * @param transformer a function returning the transformation
3321 <         * for an element
4124 <         * @param basis the identity (initial default value) for the reduction
4125 <         * @param reducer a commutative associative combining function
4126 <         * @return the result of accumulating the given transformation
4127 <         * of all entries
4128 <         */
4129 <        public int reduceEntriesToInt(ObjectToInt<Map.Entry<K,V>> transformer,
4130 <                                      int basis,
4131 <                                      IntByIntToInt reducer) {
4132 <            return fjp.invoke(ForkJoinTasks.reduceEntriesToInt
4133 <                              (ConcurrentHashMapV8.this, transformer, basis, reducer));
3315 >        public boolean tryAdvance(Action<? super K> action) {
3316 >            if (action == null) throw new NullPointerException();
3317 >            Node<K,V> p;
3318 >            if ((p = advance()) == null)
3319 >                return false;
3320 >            action.apply(p.key);
3321 >            return true;
3322          }
3323 +
3324 +        public long estimateSize() { return est; }
3325 +
3326      }
3327  
3328 <    // ---------------------------------------------------------------------
3328 >    static final class ValueSpliterator<K,V> extends Traverser<K,V>
3329 >        implements ConcurrentHashMapSpliterator<V> {
3330 >        long est;               // size estimate
3331 >        ValueSpliterator(Node<K,V>[] tab, int size, int index, int limit,
3332 >                         long est) {
3333 >            super(tab, size, index, limit);
3334 >            this.est = est;
3335 >        }
3336  
3337 <    /**
3338 <     * Predefined tasks for performing bulk parallel operations on
3339 <     * ConcurrentHashMaps. These tasks follow the forms and rules used
3340 <     * in class {@link Parallel}. Each method has the same name, but
3341 <     * returns a task rather than invoking it. These methods may be
3342 <     * useful in custom applications such as submitting a task without
4145 <     * waiting for completion, or combining with other tasks.
4146 <     */
4147 <    public static class ForkJoinTasks {
4148 <        private ForkJoinTasks() {}
3337 >        public ConcurrentHashMapSpliterator<V> trySplit() {
3338 >            int i, f, h;
3339 >            return (h = ((i = baseIndex) + (f = baseLimit)) >>> 1) <= i ? null :
3340 >                new ValueSpliterator<K,V>(tab, baseSize, baseLimit = h,
3341 >                                          f, est >>>= 1);
3342 >        }
3343  
3344 <        /**
4151 <         * Returns a task that when invoked, performs the given
4152 <         * action for each (key, value)
4153 <         *
4154 <         * @param map the map
4155 <         * @param action the action
4156 <         * @return the task
4157 <         */
4158 <        public static <K,V> ForkJoinTask<Void> forEach
4159 <            (ConcurrentHashMapV8<K,V> map,
4160 <             BiAction<K,V> action) {
3344 >        public void forEachRemaining(Action<? super V> action) {
3345              if (action == null) throw new NullPointerException();
3346 <            return new ForEachMappingTask<K,V>(map, action);
3346 >            for (Node<K,V> p; (p = advance()) != null;)
3347 >                action.apply(p.val);
3348          }
3349  
3350 <        /**
3351 <         * Returns a task that when invoked, performs the given
3352 <         * action for each non-null transformation of each (key, value)
3353 <         *
3354 <         * @param map the map
3355 <         * @param transformer a function returning the transformation
3356 <         * for an element, or null of there is no transformation (in
4172 <         * which case the action is not applied).
4173 <         * @param action the action
4174 <         * @return the task
4175 <         */
4176 <        public static <K,V,U> ForkJoinTask<Void> forEach
4177 <            (ConcurrentHashMapV8<K,V> map,
4178 <             BiFun<? super K, ? super V, ? extends U> transformer,
4179 <             Action<U> action) {
4180 <            if (transformer == null || action == null)
4181 <                throw new NullPointerException();
4182 <            return new ForEachTransformedMappingTask<K,V,U>
4183 <                (map, transformer, action);
3350 >        public boolean tryAdvance(Action<? super V> action) {
3351 >            if (action == null) throw new NullPointerException();
3352 >            Node<K,V> p;
3353 >            if ((p = advance()) == null)
3354 >                return false;
3355 >            action.apply(p.val);
3356 >            return true;
3357          }
3358  
3359 <        /**
3360 <         * Returns a task that when invoked, returns a non-null result
3361 <         * from applying the given search function on each (key,
3362 <         * value), or null if none. Upon success, further element
3363 <         * processing is suppressed and the results of any other
3364 <         * parallel invocations of the search function are ignored.
3365 <         *
3366 <         * @param map the map
3367 <         * @param searchFunction a function returning a non-null
3368 <         * result on success, else null
3369 <         * @return the task
3370 <         */
3371 <        public static <K,V,U> ForkJoinTask<U> search
4199 <            (ConcurrentHashMapV8<K,V> map,
4200 <             BiFun<? super K, ? super V, ? extends U> searchFunction) {
4201 <            if (searchFunction == null) throw new NullPointerException();
4202 <            return new SearchMappingsTask<K,V,U>
4203 <                (map, searchFunction,
4204 <                 new AtomicReference<U>());
3359 >        public long estimateSize() { return est; }
3360 >
3361 >    }
3362 >
3363 >    static final class EntrySpliterator<K,V> extends Traverser<K,V>
3364 >        implements ConcurrentHashMapSpliterator<Map.Entry<K,V>> {
3365 >        final ConcurrentHashMapV8<K,V> map; // To export MapEntry
3366 >        long est;               // size estimate
3367 >        EntrySpliterator(Node<K,V>[] tab, int size, int index, int limit,
3368 >                         long est, ConcurrentHashMapV8<K,V> map) {
3369 >            super(tab, size, index, limit);
3370 >            this.map = map;
3371 >            this.est = est;
3372          }
3373  
3374 <        /**
3375 <         * Returns a task that when invoked, returns the result of
3376 <         * accumulating the given transformation of all (key, value) pairs
3377 <         * using the given reducer to combine values, or null if none.
3378 <         *
4212 <         * @param map the map
4213 <         * @param transformer a function returning the transformation
4214 <         * for an element, or null of there is no transformation (in
4215 <         * which case it is not combined).
4216 <         * @param reducer a commutative associative combining function
4217 <         * @return the task
4218 <         */
4219 <        public static <K,V,U> ForkJoinTask<U> reduce
4220 <            (ConcurrentHashMapV8<K,V> map,
4221 <             BiFun<? super K, ? super V, ? extends U> transformer,
4222 <             BiFun<? super U, ? super U, ? extends U> reducer) {
4223 <            if (transformer == null || reducer == null)
4224 <                throw new NullPointerException();
4225 <            return new MapReduceMappingsTask<K,V,U>
4226 <                (map, transformer, reducer);
3374 >        public ConcurrentHashMapSpliterator<Map.Entry<K,V>> trySplit() {
3375 >            int i, f, h;
3376 >            return (h = ((i = baseIndex) + (f = baseLimit)) >>> 1) <= i ? null :
3377 >                new EntrySpliterator<K,V>(tab, baseSize, baseLimit = h,
3378 >                                          f, est >>>= 1, map);
3379          }
3380  
3381 <        /**
3382 <         * Returns a task that when invoked, returns the result of
3383 <         * accumulating the given transformation of all (key, value) pairs
3384 <         * using the given reducer to combine values, and the given
4233 <         * basis as an identity value.
4234 <         *
4235 <         * @param map the map
4236 <         * @param transformer a function returning the transformation
4237 <         * for an element
4238 <         * @param basis the identity (initial default value) for the reduction
4239 <         * @param reducer a commutative associative combining function
4240 <         * @return the task
4241 <         */
4242 <        public static <K,V> ForkJoinTask<Double> reduceToDouble
4243 <            (ConcurrentHashMapV8<K,V> map,
4244 <             ObjectByObjectToDouble<? super K, ? super V> transformer,
4245 <             double basis,
4246 <             DoubleByDoubleToDouble reducer) {
4247 <            if (transformer == null || reducer == null)
4248 <                throw new NullPointerException();
4249 <            return new MapReduceMappingsToDoubleTask<K,V>
4250 <                (map, transformer, basis, reducer);
3381 >        public void forEachRemaining(Action<? super Map.Entry<K,V>> action) {
3382 >            if (action == null) throw new NullPointerException();
3383 >            for (Node<K,V> p; (p = advance()) != null; )
3384 >                action.apply(new MapEntry<K,V>(p.key, p.val, map));
3385          }
3386  
3387 <        /**
3388 <         * Returns a task that when invoked, returns the result of
3389 <         * accumulating the given transformation of all (key, value) pairs
3390 <         * using the given reducer to combine values, and the given
3391 <         * basis as an identity value.
3392 <         *
3393 <         * @param map the map
4260 <         * @param transformer a function returning the transformation
4261 <         * for an element
4262 <         * @param basis the identity (initial default value) for the reduction
4263 <         * @param reducer a commutative associative combining function
4264 <         * @return the task
4265 <         */
4266 <        public static <K,V> ForkJoinTask<Long> reduceToLong
4267 <            (ConcurrentHashMapV8<K,V> map,
4268 <             ObjectByObjectToLong<? super K, ? super V> transformer,
4269 <             long basis,
4270 <             LongByLongToLong reducer) {
4271 <            if (transformer == null || reducer == null)
4272 <                throw new NullPointerException();
4273 <            return new MapReduceMappingsToLongTask<K,V>
4274 <                (map, transformer, basis, reducer);
3387 >        public boolean tryAdvance(Action<? super Map.Entry<K,V>> action) {
3388 >            if (action == null) throw new NullPointerException();
3389 >            Node<K,V> p;
3390 >            if ((p = advance()) == null)
3391 >                return false;
3392 >            action.apply(new MapEntry<K,V>(p.key, p.val, map));
3393 >            return true;
3394          }
3395  
3396 +        public long estimateSize() { return est; }
3397 +
3398 +    }
3399 +
3400 +    // Parallel bulk operations
3401 +
3402 +    /**
3403 +     * Computes initial batch value for bulk tasks. The returned value
3404 +     * is approximately exp2 of the number of times (minus one) to
3405 +     * split task by two before executing leaf action. This value is
3406 +     * faster to compute and more convenient to use as a guide to
3407 +     * splitting than is the depth, since it is used while dividing by
3408 +     * two anyway.
3409 +     */
3410 +    final int batchFor(long b) {
3411 +        long n;
3412 +        if (b == Long.MAX_VALUE || (n = sumCount()) <= 1L || n < b)
3413 +            return 0;
3414 +        int sp = ForkJoinPool.getCommonPoolParallelism() << 2; // slack of 4
3415 +        return (b <= 0L || (n /= b) >= sp) ? sp : (int)n;
3416 +    }
3417 +
3418 +    /**
3419 +     * Performs the given action for each (key, value).
3420 +     *
3421 +     * @param parallelismThreshold the (estimated) number of elements
3422 +     * needed for this operation to be executed in parallel
3423 +     * @param action the action
3424 +     * @since 1.8
3425 +     */
3426 +    public void forEach(long parallelismThreshold,
3427 +                        BiAction<? super K,? super V> action) {
3428 +        if (action == null) throw new NullPointerException();
3429 +        new ForEachMappingTask<K,V>
3430 +            (null, batchFor(parallelismThreshold), 0, 0, table,
3431 +             action).invoke();
3432 +    }
3433 +
3434 +    /**
3435 +     * Performs the given action for each non-null transformation
3436 +     * of each (key, value).
3437 +     *
3438 +     * @param parallelismThreshold the (estimated) number of elements
3439 +     * needed for this operation to be executed in parallel
3440 +     * @param transformer a function returning the transformation
3441 +     * for an element, or null if there is no transformation (in
3442 +     * which case the action is not applied)
3443 +     * @param action the action
3444 +     * @since 1.8
3445 +     */
3446 +    public <U> void forEach(long parallelismThreshold,
3447 +                            BiFun<? super K, ? super V, ? extends U> transformer,
3448 +                            Action<? super U> action) {
3449 +        if (transformer == null || action == null)
3450 +            throw new NullPointerException();
3451 +        new ForEachTransformedMappingTask<K,V,U>
3452 +            (null, batchFor(parallelismThreshold), 0, 0, table,
3453 +             transformer, action).invoke();
3454 +    }
3455 +
3456 +    /**
3457 +     * Returns a non-null result from applying the given search
3458 +     * function on each (key, value), or null if none.  Upon
3459 +     * success, further element processing is suppressed and the
3460 +     * results of any other parallel invocations of the search
3461 +     * function are ignored.
3462 +     *
3463 +     * @param parallelismThreshold the (estimated) number of elements
3464 +     * needed for this operation to be executed in parallel
3465 +     * @param searchFunction a function returning a non-null
3466 +     * result on success, else null
3467 +     * @return a non-null result from applying the given search
3468 +     * function on each (key, value), or null if none
3469 +     * @since 1.8
3470 +     */
3471 +    public <U> U search(long parallelismThreshold,
3472 +                        BiFun<? super K, ? super V, ? extends U> searchFunction) {
3473 +        if (searchFunction == null) throw new NullPointerException();
3474 +        return new SearchMappingsTask<K,V,U>
3475 +            (null, batchFor(parallelismThreshold), 0, 0, table,
3476 +             searchFunction, new AtomicReference<U>()).invoke();
3477 +    }
3478 +
3479 +    /**
3480 +     * Returns the result of accumulating the given transformation
3481 +     * of all (key, value) pairs using the given reducer to
3482 +     * combine values, or null if none.
3483 +     *
3484 +     * @param parallelismThreshold the (estimated) number of elements
3485 +     * needed for this operation to be executed in parallel
3486 +     * @param transformer a function returning the transformation
3487 +     * for an element, or null if there is no transformation (in
3488 +     * which case it is not combined)
3489 +     * @param reducer a commutative associative combining function
3490 +     * @return the result of accumulating the given transformation
3491 +     * of all (key, value) pairs
3492 +     * @since 1.8
3493 +     */
3494 +    public <U> U reduce(long parallelismThreshold,
3495 +                        BiFun<? super K, ? super V, ? extends U> transformer,
3496 +                        BiFun<? super U, ? super U, ? extends U> reducer) {
3497 +        if (transformer == null || reducer == null)
3498 +            throw new NullPointerException();
3499 +        return new MapReduceMappingsTask<K,V,U>
3500 +            (null, batchFor(parallelismThreshold), 0, 0, table,
3501 +             null, transformer, reducer).invoke();
3502 +    }
3503 +
3504 +    /**
3505 +     * Returns the result of accumulating the given transformation
3506 +     * of all (key, value) pairs using the given reducer to
3507 +     * combine values, and the given basis as an identity value.
3508 +     *
3509 +     * @param parallelismThreshold the (estimated) number of elements
3510 +     * needed for this operation to be executed in parallel
3511 +     * @param transformer a function returning the transformation
3512 +     * for an element
3513 +     * @param basis the identity (initial default value) for the reduction
3514 +     * @param reducer a commutative associative combining function
3515 +     * @return the result of accumulating the given transformation
3516 +     * of all (key, value) pairs
3517 +     * @since 1.8
3518 +     */
3519 +    public double reduceToDouble(long parallelismThreshold,
3520 +                                 ObjectByObjectToDouble<? super K, ? super V> transformer,
3521 +                                 double basis,
3522 +                                 DoubleByDoubleToDouble reducer) {
3523 +        if (transformer == null || reducer == null)
3524 +            throw new NullPointerException();
3525 +        return new MapReduceMappingsToDoubleTask<K,V>
3526 +            (null, batchFor(parallelismThreshold), 0, 0, table,
3527 +             null, transformer, basis, reducer).invoke();
3528 +    }
3529 +
3530 +    /**
3531 +     * Returns the result of accumulating the given transformation
3532 +     * of all (key, value) pairs using the given reducer to
3533 +     * combine values, and the given basis as an identity value.
3534 +     *
3535 +     * @param parallelismThreshold the (estimated) number of elements
3536 +     * needed for this operation to be executed in parallel
3537 +     * @param transformer a function returning the transformation
3538 +     * for an element
3539 +     * @param basis the identity (initial default value) for the reduction
3540 +     * @param reducer a commutative associative combining function
3541 +     * @return the result of accumulating the given transformation
3542 +     * of all (key, value) pairs
3543 +     * @since 1.8
3544 +     */
3545 +    public long reduceToLong(long parallelismThreshold,
3546 +                             ObjectByObjectToLong<? super K, ? super V> transformer,
3547 +                             long basis,
3548 +                             LongByLongToLong reducer) {
3549 +        if (transformer == null || reducer == null)
3550 +            throw new NullPointerException();
3551 +        return new MapReduceMappingsToLongTask<K,V>
3552 +            (null, batchFor(parallelismThreshold), 0, 0, table,
3553 +             null, transformer, basis, reducer).invoke();
3554 +    }
3555 +
3556 +    /**
3557 +     * Returns the result of accumulating the given transformation
3558 +     * of all (key, value) pairs using the given reducer to
3559 +     * combine values, and the given basis as an identity value.
3560 +     *
3561 +     * @param parallelismThreshold the (estimated) number of elements
3562 +     * needed for this operation to be executed in parallel
3563 +     * @param transformer a function returning the transformation
3564 +     * for an element
3565 +     * @param basis the identity (initial default value) for the reduction
3566 +     * @param reducer a commutative associative combining function
3567 +     * @return the result of accumulating the given transformation
3568 +     * of all (key, value) pairs
3569 +     * @since 1.8
3570 +     */
3571 +    public int reduceToInt(long parallelismThreshold,
3572 +                           ObjectByObjectToInt<? super K, ? super V> transformer,
3573 +                           int basis,
3574 +                           IntByIntToInt reducer) {
3575 +        if (transformer == null || reducer == null)
3576 +            throw new NullPointerException();
3577 +        return new MapReduceMappingsToIntTask<K,V>
3578 +            (null, batchFor(parallelismThreshold), 0, 0, table,
3579 +             null, transformer, basis, reducer).invoke();
3580 +    }
3581 +
3582 +    /**
3583 +     * Performs the given action for each key.
3584 +     *
3585 +     * @param parallelismThreshold the (estimated) number of elements
3586 +     * needed for this operation to be executed in parallel
3587 +     * @param action the action
3588 +     * @since 1.8
3589 +     */
3590 +    public void forEachKey(long parallelismThreshold,
3591 +                           Action<? super K> action) {
3592 +        if (action == null) throw new NullPointerException();
3593 +        new ForEachKeyTask<K,V>
3594 +            (null, batchFor(parallelismThreshold), 0, 0, table,
3595 +             action).invoke();
3596 +    }
3597 +
3598 +    /**
3599 +     * Performs the given action for each non-null transformation
3600 +     * of each key.
3601 +     *
3602 +     * @param parallelismThreshold the (estimated) number of elements
3603 +     * needed for this operation to be executed in parallel
3604 +     * @param transformer a function returning the transformation
3605 +     * for an element, or null if there is no transformation (in
3606 +     * which case the action is not applied)
3607 +     * @param action the action
3608 +     * @since 1.8
3609 +     */
3610 +    public <U> void forEachKey(long parallelismThreshold,
3611 +                               Fun<? super K, ? extends U> transformer,
3612 +                               Action<? super U> action) {
3613 +        if (transformer == null || action == null)
3614 +            throw new NullPointerException();
3615 +        new ForEachTransformedKeyTask<K,V,U>
3616 +            (null, batchFor(parallelismThreshold), 0, 0, table,
3617 +             transformer, action).invoke();
3618 +    }
3619 +
3620 +    /**
3621 +     * Returns a non-null result from applying the given search
3622 +     * function on each key, or null if none. Upon success,
3623 +     * further element processing is suppressed and the results of
3624 +     * any other parallel invocations of the search function are
3625 +     * ignored.
3626 +     *
3627 +     * @param parallelismThreshold the (estimated) number of elements
3628 +     * needed for this operation to be executed in parallel
3629 +     * @param searchFunction a function returning a non-null
3630 +     * result on success, else null
3631 +     * @return a non-null result from applying the given search
3632 +     * function on each key, or null if none
3633 +     * @since 1.8
3634 +     */
3635 +    public <U> U searchKeys(long parallelismThreshold,
3636 +                            Fun<? super K, ? extends U> searchFunction) {
3637 +        if (searchFunction == null) throw new NullPointerException();
3638 +        return new SearchKeysTask<K,V,U>
3639 +            (null, batchFor(parallelismThreshold), 0, 0, table,
3640 +             searchFunction, new AtomicReference<U>()).invoke();
3641 +    }
3642 +
3643 +    /**
3644 +     * Returns the result of accumulating all keys using the given
3645 +     * reducer to combine values, or null if none.
3646 +     *
3647 +     * @param parallelismThreshold the (estimated) number of elements
3648 +     * needed for this operation to be executed in parallel
3649 +     * @param reducer a commutative associative combining function
3650 +     * @return the result of accumulating all keys using the given
3651 +     * reducer to combine values, or null if none
3652 +     * @since 1.8
3653 +     */
3654 +    public K reduceKeys(long parallelismThreshold,
3655 +                        BiFun<? super K, ? super K, ? extends K> reducer) {
3656 +        if (reducer == null) throw new NullPointerException();
3657 +        return new ReduceKeysTask<K,V>
3658 +            (null, batchFor(parallelismThreshold), 0, 0, table,
3659 +             null, reducer).invoke();
3660 +    }
3661 +
3662 +    /**
3663 +     * Returns the result of accumulating the given transformation
3664 +     * of all keys using the given reducer to combine values, or
3665 +     * null if none.
3666 +     *
3667 +     * @param parallelismThreshold the (estimated) number of elements
3668 +     * needed for this operation to be executed in parallel
3669 +     * @param transformer a function returning the transformation
3670 +     * for an element, or null if there is no transformation (in
3671 +     * which case it is not combined)
3672 +     * @param reducer a commutative associative combining function
3673 +     * @return the result of accumulating the given transformation
3674 +     * of all keys
3675 +     * @since 1.8
3676 +     */
3677 +    public <U> U reduceKeys(long parallelismThreshold,
3678 +                            Fun<? super K, ? extends U> transformer,
3679 +         BiFun<? super U, ? super U, ? extends U> reducer) {
3680 +        if (transformer == null || reducer == null)
3681 +            throw new NullPointerException();
3682 +        return new MapReduceKeysTask<K,V,U>
3683 +            (null, batchFor(parallelismThreshold), 0, 0, table,
3684 +             null, transformer, reducer).invoke();
3685 +    }
3686 +
3687 +    /**
3688 +     * Returns the result of accumulating the given transformation
3689 +     * of all keys using the given reducer to combine values, and
3690 +     * the given basis as an identity value.
3691 +     *
3692 +     * @param parallelismThreshold the (estimated) number of elements
3693 +     * needed for this operation to be executed in parallel
3694 +     * @param transformer a function returning the transformation
3695 +     * for an element
3696 +     * @param basis the identity (initial default value) for the reduction
3697 +     * @param reducer a commutative associative combining function
3698 +     * @return the result of accumulating the given transformation
3699 +     * of all keys
3700 +     * @since 1.8
3701 +     */
3702 +    public double reduceKeysToDouble(long parallelismThreshold,
3703 +                                     ObjectToDouble<? super K> transformer,
3704 +                                     double basis,
3705 +                                     DoubleByDoubleToDouble reducer) {
3706 +        if (transformer == null || reducer == null)
3707 +            throw new NullPointerException();
3708 +        return new MapReduceKeysToDoubleTask<K,V>
3709 +            (null, batchFor(parallelismThreshold), 0, 0, table,
3710 +             null, transformer, basis, reducer).invoke();
3711 +    }
3712 +
3713 +    /**
3714 +     * Returns the result of accumulating the given transformation
3715 +     * of all keys using the given reducer to combine values, and
3716 +     * the given basis as an identity value.
3717 +     *
3718 +     * @param parallelismThreshold the (estimated) number of elements
3719 +     * needed for this operation to be executed in parallel
3720 +     * @param transformer a function returning the transformation
3721 +     * for an element
3722 +     * @param basis the identity (initial default value) for the reduction
3723 +     * @param reducer a commutative associative combining function
3724 +     * @return the result of accumulating the given transformation
3725 +     * of all keys
3726 +     * @since 1.8
3727 +     */
3728 +    public long reduceKeysToLong(long parallelismThreshold,
3729 +                                 ObjectToLong<? super K> transformer,
3730 +                                 long basis,
3731 +                                 LongByLongToLong reducer) {
3732 +        if (transformer == null || reducer == null)
3733 +            throw new NullPointerException();
3734 +        return new MapReduceKeysToLongTask<K,V>
3735 +            (null, batchFor(parallelismThreshold), 0, 0, table,
3736 +             null, transformer, basis, reducer).invoke();
3737 +    }
3738 +
3739 +    /**
3740 +     * Returns the result of accumulating the given transformation
3741 +     * of all keys using the given reducer to combine values, and
3742 +     * the given basis as an identity value.
3743 +     *
3744 +     * @param parallelismThreshold the (estimated) number of elements
3745 +     * needed for this operation to be executed in parallel
3746 +     * @param transformer a function returning the transformation
3747 +     * for an element
3748 +     * @param basis the identity (initial default value) for the reduction
3749 +     * @param reducer a commutative associative combining function
3750 +     * @return the result of accumulating the given transformation
3751 +     * of all keys
3752 +     * @since 1.8
3753 +     */
3754 +    public int reduceKeysToInt(long parallelismThreshold,
3755 +                               ObjectToInt<? super K> transformer,
3756 +                               int basis,
3757 +                               IntByIntToInt reducer) {
3758 +        if (transformer == null || reducer == null)
3759 +            throw new NullPointerException();
3760 +        return new MapReduceKeysToIntTask<K,V>
3761 +            (null, batchFor(parallelismThreshold), 0, 0, table,
3762 +             null, transformer, basis, reducer).invoke();
3763 +    }
3764 +
3765 +    /**
3766 +     * Performs the given action for each value.
3767 +     *
3768 +     * @param parallelismThreshold the (estimated) number of elements
3769 +     * needed for this operation to be executed in parallel
3770 +     * @param action the action
3771 +     * @since 1.8
3772 +     */
3773 +    public void forEachValue(long parallelismThreshold,
3774 +                             Action<? super V> action) {
3775 +        if (action == null)
3776 +            throw new NullPointerException();
3777 +        new ForEachValueTask<K,V>
3778 +            (null, batchFor(parallelismThreshold), 0, 0, table,
3779 +             action).invoke();
3780 +    }
3781 +
3782 +    /**
3783 +     * Performs the given action for each non-null transformation
3784 +     * of each value.
3785 +     *
3786 +     * @param parallelismThreshold the (estimated) number of elements
3787 +     * needed for this operation to be executed in parallel
3788 +     * @param transformer a function returning the transformation
3789 +     * for an element, or null if there is no transformation (in
3790 +     * which case the action is not applied)
3791 +     * @param action the action
3792 +     * @since 1.8
3793 +     */
3794 +    public <U> void forEachValue(long parallelismThreshold,
3795 +                                 Fun<? super V, ? extends U> transformer,
3796 +                                 Action<? super U> action) {
3797 +        if (transformer == null || action == null)
3798 +            throw new NullPointerException();
3799 +        new ForEachTransformedValueTask<K,V,U>
3800 +            (null, batchFor(parallelismThreshold), 0, 0, table,
3801 +             transformer, action).invoke();
3802 +    }
3803 +
3804 +    /**
3805 +     * Returns a non-null result from applying the given search
3806 +     * function on each value, or null if none.  Upon success,
3807 +     * further element processing is suppressed and the results of
3808 +     * any other parallel invocations of the search function are
3809 +     * ignored.
3810 +     *
3811 +     * @param parallelismThreshold the (estimated) number of elements
3812 +     * needed for this operation to be executed in parallel
3813 +     * @param searchFunction a function returning a non-null
3814 +     * result on success, else null
3815 +     * @return a non-null result from applying the given search
3816 +     * function on each value, or null if none
3817 +     * @since 1.8
3818 +     */
3819 +    public <U> U searchValues(long parallelismThreshold,
3820 +                              Fun<? super V, ? extends U> searchFunction) {
3821 +        if (searchFunction == null) throw new NullPointerException();
3822 +        return new SearchValuesTask<K,V,U>
3823 +            (null, batchFor(parallelismThreshold), 0, 0, table,
3824 +             searchFunction, new AtomicReference<U>()).invoke();
3825 +    }
3826 +
3827 +    /**
3828 +     * Returns the result of accumulating all values using the
3829 +     * given reducer to combine values, or null if none.
3830 +     *
3831 +     * @param parallelismThreshold the (estimated) number of elements
3832 +     * needed for this operation to be executed in parallel
3833 +     * @param reducer a commutative associative combining function
3834 +     * @return the result of accumulating all values
3835 +     * @since 1.8
3836 +     */
3837 +    public V reduceValues(long parallelismThreshold,
3838 +                          BiFun<? super V, ? super V, ? extends V> reducer) {
3839 +        if (reducer == null) throw new NullPointerException();
3840 +        return new ReduceValuesTask<K,V>
3841 +            (null, batchFor(parallelismThreshold), 0, 0, table,
3842 +             null, reducer).invoke();
3843 +    }
3844 +
3845 +    /**
3846 +     * Returns the result of accumulating the given transformation
3847 +     * of all values using the given reducer to combine values, or
3848 +     * null if none.
3849 +     *
3850 +     * @param parallelismThreshold the (estimated) number of elements
3851 +     * needed for this operation to be executed in parallel
3852 +     * @param transformer a function returning the transformation
3853 +     * for an element, or null if there is no transformation (in
3854 +     * which case it is not combined)
3855 +     * @param reducer a commutative associative combining function
3856 +     * @return the result of accumulating the given transformation
3857 +     * of all values
3858 +     * @since 1.8
3859 +     */
3860 +    public <U> U reduceValues(long parallelismThreshold,
3861 +                              Fun<? super V, ? extends U> transformer,
3862 +                              BiFun<? super U, ? super U, ? extends U> reducer) {
3863 +        if (transformer == null || reducer == null)
3864 +            throw new NullPointerException();
3865 +        return new MapReduceValuesTask<K,V,U>
3866 +            (null, batchFor(parallelismThreshold), 0, 0, table,
3867 +             null, transformer, reducer).invoke();
3868 +    }
3869 +
3870 +    /**
3871 +     * Returns the result of accumulating the given transformation
3872 +     * of all values using the given reducer to combine values,
3873 +     * and the given basis as an identity value.
3874 +     *
3875 +     * @param parallelismThreshold the (estimated) number of elements
3876 +     * needed for this operation to be executed in parallel
3877 +     * @param transformer a function returning the transformation
3878 +     * for an element
3879 +     * @param basis the identity (initial default value) for the reduction
3880 +     * @param reducer a commutative associative combining function
3881 +     * @return the result of accumulating the given transformation
3882 +     * of all values
3883 +     * @since 1.8
3884 +     */
3885 +    public double reduceValuesToDouble(long parallelismThreshold,
3886 +                                       ObjectToDouble<? super V> transformer,
3887 +                                       double basis,
3888 +                                       DoubleByDoubleToDouble reducer) {
3889 +        if (transformer == null || reducer == null)
3890 +            throw new NullPointerException();
3891 +        return new MapReduceValuesToDoubleTask<K,V>
3892 +            (null, batchFor(parallelismThreshold), 0, 0, table,
3893 +             null, transformer, basis, reducer).invoke();
3894 +    }
3895 +
3896 +    /**
3897 +     * Returns the result of accumulating the given transformation
3898 +     * of all values using the given reducer to combine values,
3899 +     * and the given basis as an identity value.
3900 +     *
3901 +     * @param parallelismThreshold the (estimated) number of elements
3902 +     * needed for this operation to be executed in parallel
3903 +     * @param transformer a function returning the transformation
3904 +     * for an element
3905 +     * @param basis the identity (initial default value) for the reduction
3906 +     * @param reducer a commutative associative combining function
3907 +     * @return the result of accumulating the given transformation
3908 +     * of all values
3909 +     * @since 1.8
3910 +     */
3911 +    public long reduceValuesToLong(long parallelismThreshold,
3912 +                                   ObjectToLong<? super V> transformer,
3913 +                                   long basis,
3914 +                                   LongByLongToLong reducer) {
3915 +        if (transformer == null || reducer == null)
3916 +            throw new NullPointerException();
3917 +        return new MapReduceValuesToLongTask<K,V>
3918 +            (null, batchFor(parallelismThreshold), 0, 0, table,
3919 +             null, transformer, basis, reducer).invoke();
3920 +    }
3921 +
3922 +    /**
3923 +     * Returns the result of accumulating the given transformation
3924 +     * of all values using the given reducer to combine values,
3925 +     * and the given basis as an identity value.
3926 +     *
3927 +     * @param parallelismThreshold the (estimated) number of elements
3928 +     * needed for this operation to be executed in parallel
3929 +     * @param transformer a function returning the transformation
3930 +     * for an element
3931 +     * @param basis the identity (initial default value) for the reduction
3932 +     * @param reducer a commutative associative combining function
3933 +     * @return the result of accumulating the given transformation
3934 +     * of all values
3935 +     * @since 1.8
3936 +     */
3937 +    public int reduceValuesToInt(long parallelismThreshold,
3938 +                                 ObjectToInt<? super V> transformer,
3939 +                                 int basis,
3940 +                                 IntByIntToInt reducer) {
3941 +        if (transformer == null || reducer == null)
3942 +            throw new NullPointerException();
3943 +        return new MapReduceValuesToIntTask<K,V>
3944 +            (null, batchFor(parallelismThreshold), 0, 0, table,
3945 +             null, transformer, basis, reducer).invoke();
3946 +    }
3947 +
3948 +    /**
3949 +     * Performs the given action for each entry.
3950 +     *
3951 +     * @param parallelismThreshold the (estimated) number of elements
3952 +     * needed for this operation to be executed in parallel
3953 +     * @param action the action
3954 +     * @since 1.8
3955 +     */
3956 +    public void forEachEntry(long parallelismThreshold,
3957 +                             Action<? super Map.Entry<K,V>> action) {
3958 +        if (action == null) throw new NullPointerException();
3959 +        new ForEachEntryTask<K,V>(null, batchFor(parallelismThreshold), 0, 0, table,
3960 +                                  action).invoke();
3961 +    }
3962 +
3963 +    /**
3964 +     * Performs the given action for each non-null transformation
3965 +     * of each entry.
3966 +     *
3967 +     * @param parallelismThreshold the (estimated) number of elements
3968 +     * needed for this operation to be executed in parallel
3969 +     * @param transformer a function returning the transformation
3970 +     * for an element, or null if there is no transformation (in
3971 +     * which case the action is not applied)
3972 +     * @param action the action
3973 +     * @since 1.8
3974 +     */
3975 +    public <U> void forEachEntry(long parallelismThreshold,
3976 +                                 Fun<Map.Entry<K,V>, ? extends U> transformer,
3977 +                                 Action<? super U> action) {
3978 +        if (transformer == null || action == null)
3979 +            throw new NullPointerException();
3980 +        new ForEachTransformedEntryTask<K,V,U>
3981 +            (null, batchFor(parallelismThreshold), 0, 0, table,
3982 +             transformer, action).invoke();
3983 +    }
3984 +
3985 +    /**
3986 +     * Returns a non-null result from applying the given search
3987 +     * function on each entry, or null if none.  Upon success,
3988 +     * further element processing is suppressed and the results of
3989 +     * any other parallel invocations of the search function are
3990 +     * ignored.
3991 +     *
3992 +     * @param parallelismThreshold the (estimated) number of elements
3993 +     * needed for this operation to be executed in parallel
3994 +     * @param searchFunction a function returning a non-null
3995 +     * result on success, else null
3996 +     * @return a non-null result from applying the given search
3997 +     * function on each entry, or null if none
3998 +     * @since 1.8
3999 +     */
4000 +    public <U> U searchEntries(long parallelismThreshold,
4001 +                               Fun<Map.Entry<K,V>, ? extends U> searchFunction) {
4002 +        if (searchFunction == null) throw new NullPointerException();
4003 +        return new SearchEntriesTask<K,V,U>
4004 +            (null, batchFor(parallelismThreshold), 0, 0, table,
4005 +             searchFunction, new AtomicReference<U>()).invoke();
4006 +    }
4007 +
4008 +    /**
4009 +     * Returns the result of accumulating all entries using the
4010 +     * given reducer to combine values, 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 reducer a commutative associative combining function
4015 +     * @return the result of accumulating all entries
4016 +     * @since 1.8
4017 +     */
4018 +    public Map.Entry<K,V> reduceEntries(long parallelismThreshold,
4019 +                                        BiFun<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer) {
4020 +        if (reducer == null) throw new NullPointerException();
4021 +        return new ReduceEntriesTask<K,V>
4022 +            (null, batchFor(parallelismThreshold), 0, 0, table,
4023 +             null, reducer).invoke();
4024 +    }
4025 +
4026 +    /**
4027 +     * Returns the result of accumulating the given transformation
4028 +     * of all entries using the given reducer to combine values,
4029 +     * or null if none.
4030 +     *
4031 +     * @param parallelismThreshold the (estimated) number of elements
4032 +     * needed for this operation to be executed in parallel
4033 +     * @param transformer a function returning the transformation
4034 +     * for an element, or null if there is no transformation (in
4035 +     * which case it is not combined)
4036 +     * @param reducer a commutative associative combining function
4037 +     * @return the result of accumulating the given transformation
4038 +     * of all entries
4039 +     * @since 1.8
4040 +     */
4041 +    public <U> U reduceEntries(long parallelismThreshold,
4042 +                               Fun<Map.Entry<K,V>, ? extends U> transformer,
4043 +                               BiFun<? super U, ? super U, ? extends U> reducer) {
4044 +        if (transformer == null || reducer == null)
4045 +            throw new NullPointerException();
4046 +        return new MapReduceEntriesTask<K,V,U>
4047 +            (null, batchFor(parallelismThreshold), 0, 0, table,
4048 +             null, transformer, reducer).invoke();
4049 +    }
4050 +
4051 +    /**
4052 +     * Returns the result of accumulating the given transformation
4053 +     * of all entries using the given reducer to combine values,
4054 +     * and the given basis as an identity value.
4055 +     *
4056 +     * @param parallelismThreshold the (estimated) number of elements
4057 +     * needed for this operation to be executed in parallel
4058 +     * @param transformer a function returning the transformation
4059 +     * for an element
4060 +     * @param basis the identity (initial default value) for the reduction
4061 +     * @param reducer a commutative associative combining function
4062 +     * @return the result of accumulating the given transformation
4063 +     * of all entries
4064 +     * @since 1.8
4065 +     */
4066 +    public double reduceEntriesToDouble(long parallelismThreshold,
4067 +                                        ObjectToDouble<Map.Entry<K,V>> transformer,
4068 +                                        double basis,
4069 +                                        DoubleByDoubleToDouble reducer) {
4070 +        if (transformer == null || reducer == null)
4071 +            throw new NullPointerException();
4072 +        return new MapReduceEntriesToDoubleTask<K,V>
4073 +            (null, batchFor(parallelismThreshold), 0, 0, table,
4074 +             null, transformer, basis, reducer).invoke();
4075 +    }
4076 +
4077 +    /**
4078 +     * Returns the result of accumulating the given transformation
4079 +     * of all entries using the given reducer to combine values,
4080 +     * and the given basis as an identity value.
4081 +     *
4082 +     * @param parallelismThreshold the (estimated) number of elements
4083 +     * needed for this operation to be executed in parallel
4084 +     * @param transformer a function returning the transformation
4085 +     * for an element
4086 +     * @param basis the identity (initial default value) for the reduction
4087 +     * @param reducer a commutative associative combining function
4088 +     * @return the result of accumulating the given transformation
4089 +     * of all entries
4090 +     * @since 1.8
4091 +     */
4092 +    public long reduceEntriesToLong(long parallelismThreshold,
4093 +                                    ObjectToLong<Map.Entry<K,V>> transformer,
4094 +                                    long basis,
4095 +                                    LongByLongToLong reducer) {
4096 +        if (transformer == null || reducer == null)
4097 +            throw new NullPointerException();
4098 +        return new MapReduceEntriesToLongTask<K,V>
4099 +            (null, batchFor(parallelismThreshold), 0, 0, table,
4100 +             null, transformer, basis, reducer).invoke();
4101 +    }
4102 +
4103 +    /**
4104 +     * Returns the result of accumulating the given transformation
4105 +     * of all entries using the given reducer to combine values,
4106 +     * and the given basis as an identity value.
4107 +     *
4108 +     * @param parallelismThreshold the (estimated) number of elements
4109 +     * needed for this operation to be executed in parallel
4110 +     * @param transformer a function returning the transformation
4111 +     * for an element
4112 +     * @param basis the identity (initial default value) for the reduction
4113 +     * @param reducer a commutative associative combining function
4114 +     * @return the result of accumulating the given transformation
4115 +     * of all entries
4116 +     * @since 1.8
4117 +     */
4118 +    public int reduceEntriesToInt(long parallelismThreshold,
4119 +                                  ObjectToInt<Map.Entry<K,V>> transformer,
4120 +                                  int basis,
4121 +                                  IntByIntToInt reducer) {
4122 +        if (transformer == null || reducer == null)
4123 +            throw new NullPointerException();
4124 +        return new MapReduceEntriesToIntTask<K,V>
4125 +            (null, batchFor(parallelismThreshold), 0, 0, table,
4126 +             null, transformer, basis, reducer).invoke();
4127 +    }
4128 +
4129 +
4130 +    /* ----------------Views -------------- */
4131 +
4132 +    /**
4133 +     * Base class for views.
4134 +     */
4135 +    abstract static class CollectionView<K,V,E>
4136 +        implements Collection<E>, java.io.Serializable {
4137 +        private static final long serialVersionUID = 7249069246763182397L;
4138 +        final ConcurrentHashMapV8<K,V> map;
4139 +        CollectionView(ConcurrentHashMapV8<K,V> map)  { this.map = map; }
4140 +
4141          /**
4142 <         * Returns a task that when invoked, returns the result of
4279 <         * accumulating the given transformation of all (key, value) pairs
4280 <         * using the given reducer to combine values, and the given
4281 <         * basis as an identity value.
4142 >         * Returns the map backing this view.
4143           *
4144 <         * @param transformer a function returning the transformation
4284 <         * for an element
4285 <         * @param basis the identity (initial default value) for the reduction
4286 <         * @param reducer a commutative associative combining function
4287 <         * @return the task
4144 >         * @return the map backing this view
4145           */
4146 <        public static <K,V> ForkJoinTask<Integer> reduceToInt
4290 <            (ConcurrentHashMapV8<K,V> map,
4291 <             ObjectByObjectToInt<? super K, ? super V> transformer,
4292 <             int basis,
4293 <             IntByIntToInt reducer) {
4294 <            if (transformer == null || reducer == null)
4295 <                throw new NullPointerException();
4296 <            return new MapReduceMappingsToIntTask<K,V>
4297 <                (map, transformer, basis, reducer);
4298 <        }
4146 >        public ConcurrentHashMapV8<K,V> getMap() { return map; }
4147  
4148          /**
4149 <         * Returns a task that when invoked, performs the given action
4150 <         * for each key.
4303 <         *
4304 <         * @param map the map
4305 <         * @param action the action
4306 <         * @return the task
4149 >         * Removes all of the elements from this view, by removing all
4150 >         * the mappings from the map backing this view.
4151           */
4152 <        public static <K,V> ForkJoinTask<Void> forEachKey
4153 <            (ConcurrentHashMapV8<K,V> map,
4154 <             Action<K> action) {
4311 <            if (action == null) throw new NullPointerException();
4312 <            return new ForEachKeyTask<K,V>(map, action);
4313 <        }
4152 >        public final void clear()      { map.clear(); }
4153 >        public final int size()        { return map.size(); }
4154 >        public final boolean isEmpty() { return map.isEmpty(); }
4155  
4156 +        // implementations below rely on concrete classes supplying these
4157 +        // abstract methods
4158          /**
4159 <         * Returns a task that when invoked, performs the given action
4160 <         * for each non-null transformation of each key.
4161 <         *
4162 <         * @param map the map
4163 <         * @param transformer a function returning the transformation
4164 <         * for an element, or null of there is no transformation (in
4165 <         * which case the action is not applied).
4166 <         * @param action the action
4167 <         * @return the task
4168 <         */
4169 <        public static <K,V,U> ForkJoinTask<Void> forEachKey
4170 <            (ConcurrentHashMapV8<K,V> map,
4171 <             Fun<? super K, ? extends U> transformer,
4172 <             Action<U> action) {
4173 <            if (transformer == null || action == null)
4174 <                throw new NullPointerException();
4175 <            return new ForEachTransformedKeyTask<K,V,U>
4176 <                (map, transformer, action);
4159 >         * Returns a "weakly consistent" iterator that will never
4160 >         * throw {@link ConcurrentModificationException}, and
4161 >         * guarantees to traverse elements as they existed upon
4162 >         * construction of the iterator, and may (but is not
4163 >         * guaranteed to) reflect any modifications subsequent to
4164 >         * construction.
4165 >         */
4166 >        public abstract Iterator<E> iterator();
4167 >        public abstract boolean contains(Object o);
4168 >        public abstract boolean remove(Object o);
4169 >
4170 >        private static final String oomeMsg = "Required array size too large";
4171 >
4172 >        public final Object[] toArray() {
4173 >            long sz = map.mappingCount();
4174 >            if (sz > MAX_ARRAY_SIZE)
4175 >                throw new OutOfMemoryError(oomeMsg);
4176 >            int n = (int)sz;
4177 >            Object[] r = new Object[n];
4178 >            int i = 0;
4179 >            for (E e : this) {
4180 >                if (i == n) {
4181 >                    if (n >= MAX_ARRAY_SIZE)
4182 >                        throw new OutOfMemoryError(oomeMsg);
4183 >                    if (n >= MAX_ARRAY_SIZE - (MAX_ARRAY_SIZE >>> 1) - 1)
4184 >                        n = MAX_ARRAY_SIZE;
4185 >                    else
4186 >                        n += (n >>> 1) + 1;
4187 >                    r = Arrays.copyOf(r, n);
4188 >                }
4189 >                r[i++] = e;
4190 >            }
4191 >            return (i == n) ? r : Arrays.copyOf(r, i);
4192          }
4193  
4194 <        /**
4195 <         * Returns a task that when invoked, returns a non-null result
4196 <         * from applying the given search function on each key, or
4197 <         * null if none.  Upon success, further element processing is
4198 <         * suppressed and the results of any other parallel
4199 <         * invocations of the search function are ignored.
4200 <         *
4201 <         * @param map the map
4202 <         * @param searchFunction a function returning a non-null
4203 <         * result on success, else null
4204 <         * @return the task
4205 <         */
4206 <        public static <K,V,U> ForkJoinTask<U> searchKeys
4207 <            (ConcurrentHashMapV8<K,V> map,
4208 <             Fun<? super K, ? extends U> searchFunction) {
4209 <            if (searchFunction == null) throw new NullPointerException();
4210 <            return new SearchKeysTask<K,V,U>
4211 <                (map, searchFunction,
4212 <                 new AtomicReference<U>());
4194 >        @SuppressWarnings("unchecked")
4195 >        public final <T> T[] toArray(T[] a) {
4196 >            long sz = map.mappingCount();
4197 >            if (sz > MAX_ARRAY_SIZE)
4198 >                throw new OutOfMemoryError(oomeMsg);
4199 >            int m = (int)sz;
4200 >            T[] r = (a.length >= m) ? a :
4201 >                (T[])java.lang.reflect.Array
4202 >                .newInstance(a.getClass().getComponentType(), m);
4203 >            int n = r.length;
4204 >            int i = 0;
4205 >            for (E e : this) {
4206 >                if (i == n) {
4207 >                    if (n >= MAX_ARRAY_SIZE)
4208 >                        throw new OutOfMemoryError(oomeMsg);
4209 >                    if (n >= MAX_ARRAY_SIZE - (MAX_ARRAY_SIZE >>> 1) - 1)
4210 >                        n = MAX_ARRAY_SIZE;
4211 >                    else
4212 >                        n += (n >>> 1) + 1;
4213 >                    r = Arrays.copyOf(r, n);
4214 >                }
4215 >                r[i++] = (T)e;
4216 >            }
4217 >            if (a == r && i < n) {
4218 >                r[i] = null; // null-terminate
4219 >                return r;
4220 >            }
4221 >            return (i == n) ? r : Arrays.copyOf(r, i);
4222          }
4223  
4224          /**
4225 <         * Returns a task that when invoked, returns the result of
4226 <         * accumulating all keys using the given reducer to combine
4227 <         * values, or null if none.
4225 >         * Returns a string representation of this collection.
4226 >         * The string representation consists of the string representations
4227 >         * of the collection's elements in the order they are returned by
4228 >         * its iterator, enclosed in square brackets ({@code "[]"}).
4229 >         * Adjacent elements are separated by the characters {@code ", "}
4230 >         * (comma and space).  Elements are converted to strings as by
4231 >         * {@link String#valueOf(Object)}.
4232           *
4233 <         * @param map the map
4363 <         * @param reducer a commutative associative combining function
4364 <         * @return the task
4233 >         * @return a string representation of this collection
4234           */
4235 <        public static <K,V> ForkJoinTask<K> reduceKeys
4236 <            (ConcurrentHashMapV8<K,V> map,
4237 <             BiFun<? super K, ? super K, ? extends K> reducer) {
4238 <            if (reducer == null) throw new NullPointerException();
4239 <            return new ReduceKeysTask<K,V>
4240 <                (map, reducer);
4235 >        public final String toString() {
4236 >            StringBuilder sb = new StringBuilder();
4237 >            sb.append('[');
4238 >            Iterator<E> it = iterator();
4239 >            if (it.hasNext()) {
4240 >                for (;;) {
4241 >                    Object e = it.next();
4242 >                    sb.append(e == this ? "(this Collection)" : e);
4243 >                    if (!it.hasNext())
4244 >                        break;
4245 >                    sb.append(',').append(' ');
4246 >                }
4247 >            }
4248 >            return sb.append(']').toString();
4249          }
4250  
4251 <        /**
4252 <         * Returns a task that when invoked, returns the result of
4253 <         * accumulating the given transformation of all keys using the given
4254 <         * reducer to combine values, or null if none.
4255 <         *
4256 <         * @param map the map
4257 <         * @param transformer a function returning the transformation
4258 <         * for an element, or null of there is no transformation (in
4382 <         * which case it is not combined).
4383 <         * @param reducer a commutative associative combining function
4384 <         * @return the task
4385 <         */
4386 <        public static <K,V,U> ForkJoinTask<U> reduceKeys
4387 <            (ConcurrentHashMapV8<K,V> map,
4388 <             Fun<? super K, ? extends U> transformer,
4389 <             BiFun<? super U, ? super U, ? extends U> reducer) {
4390 <            if (transformer == null || reducer == null)
4391 <                throw new NullPointerException();
4392 <            return new MapReduceKeysTask<K,V,U>
4393 <                (map, transformer, reducer);
4251 >        public final boolean containsAll(Collection<?> c) {
4252 >            if (c != this) {
4253 >                for (Object e : c) {
4254 >                    if (e == null || !contains(e))
4255 >                        return false;
4256 >                }
4257 >            }
4258 >            return true;
4259          }
4260  
4261 <        /**
4262 <         * Returns a task that when invoked, returns the result of
4263 <         * accumulating the given transformation of all keys using the given
4264 <         * reducer to combine values, and the given basis as an
4265 <         * identity value.
4266 <         *
4267 <         * @param map the map
4268 <         * @param transformer a function returning the transformation
4269 <         * for an element
4405 <         * @param basis the identity (initial default value) for the reduction
4406 <         * @param reducer a commutative associative combining function
4407 <         * @return the task
4408 <         */
4409 <        public static <K,V> ForkJoinTask<Double> reduceKeysToDouble
4410 <            (ConcurrentHashMapV8<K,V> map,
4411 <             ObjectToDouble<? super K> transformer,
4412 <             double basis,
4413 <             DoubleByDoubleToDouble reducer) {
4414 <            if (transformer == null || reducer == null)
4415 <                throw new NullPointerException();
4416 <            return new MapReduceKeysToDoubleTask<K,V>
4417 <                (map, transformer, basis, reducer);
4261 >        public final boolean removeAll(Collection<?> c) {
4262 >            boolean modified = false;
4263 >            for (Iterator<E> it = iterator(); it.hasNext();) {
4264 >                if (c.contains(it.next())) {
4265 >                    it.remove();
4266 >                    modified = true;
4267 >                }
4268 >            }
4269 >            return modified;
4270          }
4271  
4272 <        /**
4273 <         * Returns a task that when invoked, returns the result of
4274 <         * accumulating the given transformation of all keys using the given
4275 <         * reducer to combine values, and the given basis as an
4276 <         * identity value.
4277 <         *
4278 <         * @param map the map
4279 <         * @param transformer a function returning the transformation
4280 <         * for an element
4429 <         * @param basis the identity (initial default value) for the reduction
4430 <         * @param reducer a commutative associative combining function
4431 <         * @return the task
4432 <         */
4433 <        public static <K,V> ForkJoinTask<Long> reduceKeysToLong
4434 <            (ConcurrentHashMapV8<K,V> map,
4435 <             ObjectToLong<? super K> transformer,
4436 <             long basis,
4437 <             LongByLongToLong reducer) {
4438 <            if (transformer == null || reducer == null)
4439 <                throw new NullPointerException();
4440 <            return new MapReduceKeysToLongTask<K,V>
4441 <                (map, transformer, basis, reducer);
4272 >        public final boolean retainAll(Collection<?> c) {
4273 >            boolean modified = false;
4274 >            for (Iterator<E> it = iterator(); it.hasNext();) {
4275 >                if (!c.contains(it.next())) {
4276 >                    it.remove();
4277 >                    modified = true;
4278 >                }
4279 >            }
4280 >            return modified;
4281          }
4282  
4283 <        /**
4284 <         * Returns a task that when invoked, returns the result of
4285 <         * accumulating the given transformation of all keys using the given
4286 <         * reducer to combine values, and the given basis as an
4287 <         * identity value.
4288 <         *
4289 <         * @param map the map
4290 <         * @param transformer a function returning the transformation
4291 <         * for an element
4292 <         * @param basis the identity (initial default value) for the reduction
4293 <         * @param reducer a commutative associative combining function
4294 <         * @return the task
4295 <         */
4296 <        public static <K,V> ForkJoinTask<Integer> reduceKeysToInt
4297 <            (ConcurrentHashMapV8<K,V> map,
4298 <             ObjectToInt<? super K> transformer,
4299 <             int basis,
4300 <             IntByIntToInt reducer) {
4301 <            if (transformer == null || reducer == null)
4302 <                throw new NullPointerException();
4464 <            return new MapReduceKeysToIntTask<K,V>
4465 <                (map, transformer, basis, reducer);
4283 >    }
4284 >
4285 >    /**
4286 >     * A view of a ConcurrentHashMapV8 as a {@link Set} of keys, in
4287 >     * which additions may optionally be enabled by mapping to a
4288 >     * common value.  This class cannot be directly instantiated.
4289 >     * See {@link #keySet() keySet()},
4290 >     * {@link #keySet(Object) keySet(V)},
4291 >     * {@link #newKeySet() newKeySet()},
4292 >     * {@link #newKeySet(int) newKeySet(int)}.
4293 >     *
4294 >     * @since 1.8
4295 >     */
4296 >    public static class KeySetView<K,V> extends CollectionView<K,V,K>
4297 >        implements Set<K>, java.io.Serializable {
4298 >        private static final long serialVersionUID = 7249069246763182397L;
4299 >        private final V value;
4300 >        KeySetView(ConcurrentHashMapV8<K,V> map, V value) {  // non-public
4301 >            super(map);
4302 >            this.value = value;
4303          }
4304  
4305          /**
4306 <         * Returns a task that when invoked, performs the given action
4307 <         * for each value.
4306 >         * Returns the default mapped value for additions,
4307 >         * or {@code null} if additions are not supported.
4308           *
4309 <         * @param map the map
4310 <         * @param action the action
4309 >         * @return the default mapped value for additions, or {@code null}
4310 >         * if not supported
4311           */
4312 <        public static <K,V> ForkJoinTask<Void> forEachValue
4476 <            (ConcurrentHashMapV8<K,V> map,
4477 <             Action<V> action) {
4478 <            if (action == null) throw new NullPointerException();
4479 <            return new ForEachValueTask<K,V>(map, action);
4480 <        }
4312 >        public V getMappedValue() { return value; }
4313  
4314          /**
4315 <         * Returns a task that when invoked, performs the given action
4316 <         * for each non-null transformation of each value.
4485 <         *
4486 <         * @param map the map
4487 <         * @param transformer a function returning the transformation
4488 <         * for an element, or null of there is no transformation (in
4489 <         * which case the action is not applied).
4490 <         * @param action the action
4315 >         * {@inheritDoc}
4316 >         * @throws NullPointerException if the specified key is null
4317           */
4318 <        public static <K,V,U> ForkJoinTask<Void> forEachValue
4493 <            (ConcurrentHashMapV8<K,V> map,
4494 <             Fun<? super V, ? extends U> transformer,
4495 <             Action<U> action) {
4496 <            if (transformer == null || action == null)
4497 <                throw new NullPointerException();
4498 <            return new ForEachTransformedValueTask<K,V,U>
4499 <                (map, transformer, action);
4500 <        }
4318 >        public boolean contains(Object o) { return map.containsKey(o); }
4319  
4320          /**
4321 <         * Returns a task that when invoked, returns a non-null result
4322 <         * from applying the given search function on each value, or
4323 <         * null if none.  Upon success, further element processing is
4506 <         * suppressed and the results of any other parallel
4507 <         * invocations of the search function are ignored.
4508 <         *
4509 <         * @param map the map
4510 <         * @param searchFunction a function returning a non-null
4511 <         * result on success, else null
4512 <         * @return the task
4321 >         * Removes the key from this map view, by removing the key (and its
4322 >         * corresponding value) from the backing map.  This method does
4323 >         * nothing if the key is not in the map.
4324           *
4325 +         * @param  o the key to be removed from the backing map
4326 +         * @return {@code true} if the backing map contained the specified key
4327 +         * @throws NullPointerException if the specified key is null
4328           */
4329 <        public static <K,V,U> ForkJoinTask<U> searchValues
4516 <            (ConcurrentHashMapV8<K,V> map,
4517 <             Fun<? super V, ? extends U> searchFunction) {
4518 <            if (searchFunction == null) throw new NullPointerException();
4519 <            return new SearchValuesTask<K,V,U>
4520 <                (map, searchFunction,
4521 <                 new AtomicReference<U>());
4522 <        }
4329 >        public boolean remove(Object o) { return map.remove(o) != null; }
4330  
4331          /**
4332 <         * Returns a task that when invoked, returns the result of
4526 <         * accumulating all values using the given reducer to combine
4527 <         * values, or null if none.
4528 <         *
4529 <         * @param map the map
4530 <         * @param reducer a commutative associative combining function
4531 <         * @return the task
4332 >         * @return an iterator over the keys of the backing map
4333           */
4334 <        public static <K,V> ForkJoinTask<V> reduceValues
4335 <            (ConcurrentHashMapV8<K,V> map,
4336 <             BiFun<? super V, ? super V, ? extends V> reducer) {
4337 <            if (reducer == null) throw new NullPointerException();
4338 <            return new ReduceValuesTask<K,V>
4538 <                (map, reducer);
4334 >        public Iterator<K> iterator() {
4335 >            Node<K,V>[] t;
4336 >            ConcurrentHashMapV8<K,V> m = map;
4337 >            int f = (t = m.table) == null ? 0 : t.length;
4338 >            return new KeyIterator<K,V>(t, f, 0, f, m);
4339          }
4340  
4341          /**
4342 <         * Returns a task that when invoked, returns the result of
4343 <         * accumulating the given transformation of all values using the
4544 <         * given reducer to combine values, or null if none.
4342 >         * Adds the specified key to this set view by mapping the key to
4343 >         * the default mapped value in the backing map, if defined.
4344           *
4345 <         * @param map the map
4346 <         * @param transformer a function returning the transformation
4347 <         * for an element, or null of there is no transformation (in
4348 <         * which case it is not combined).
4349 <         * @param reducer a commutative associative combining function
4551 <         * @return the task
4345 >         * @param e key to be added
4346 >         * @return {@code true} if this set changed as a result of the call
4347 >         * @throws NullPointerException if the specified key is null
4348 >         * @throws UnsupportedOperationException if no default mapped value
4349 >         * for additions was provided
4350           */
4351 <        public static <K,V,U> ForkJoinTask<U> reduceValues
4352 <            (ConcurrentHashMapV8<K,V> map,
4353 <             Fun<? super V, ? extends U> transformer,
4354 <             BiFun<? super U, ? super U, ? extends U> reducer) {
4355 <            if (transformer == null || reducer == null)
4558 <                throw new NullPointerException();
4559 <            return new MapReduceValuesTask<K,V,U>
4560 <                (map, transformer, reducer);
4351 >        public boolean add(K e) {
4352 >            V v;
4353 >            if ((v = value) == null)
4354 >                throw new UnsupportedOperationException();
4355 >            return map.putVal(e, v, true) == null;
4356          }
4357  
4358          /**
4359 <         * Returns a task that when invoked, returns the result of
4360 <         * accumulating the given transformation of all values using the
4566 <         * given reducer to combine values, and the given basis as an
4567 <         * identity value.
4359 >         * Adds all of the elements in the specified collection to this set,
4360 >         * as if by calling {@link #add} on each one.
4361           *
4362 <         * @param map the map
4363 <         * @param transformer a function returning the transformation
4364 <         * for an element
4365 <         * @param basis the identity (initial default value) for the reduction
4366 <         * @param reducer a commutative associative combining function
4367 <         * @return the task
4362 >         * @param c the elements to be inserted into this set
4363 >         * @return {@code true} if this set changed as a result of the call
4364 >         * @throws NullPointerException if the collection or any of its
4365 >         * elements are {@code null}
4366 >         * @throws UnsupportedOperationException if no default mapped value
4367 >         * for additions was provided
4368           */
4369 <        public static <K,V> ForkJoinTask<Double> reduceValuesToDouble
4370 <            (ConcurrentHashMapV8<K,V> map,
4371 <             ObjectToDouble<? super V> transformer,
4372 <             double basis,
4373 <             DoubleByDoubleToDouble reducer) {
4374 <            if (transformer == null || reducer == null)
4375 <                throw new NullPointerException();
4376 <            return new MapReduceValuesToDoubleTask<K,V>
4377 <                (map, transformer, basis, reducer);
4369 >        public boolean addAll(Collection<? extends K> c) {
4370 >            boolean added = false;
4371 >            V v;
4372 >            if ((v = value) == null)
4373 >                throw new UnsupportedOperationException();
4374 >            for (K e : c) {
4375 >                if (map.putVal(e, v, true) == null)
4376 >                    added = true;
4377 >            }
4378 >            return added;
4379          }
4380  
4381 <        /**
4382 <         * Returns a task that when invoked, returns the result of
4383 <         * accumulating the given transformation of all values using the
4384 <         * given reducer to combine values, and the given basis as an
4385 <         * identity value.
4592 <         *
4593 <         * @param map the map
4594 <         * @param transformer a function returning the transformation
4595 <         * for an element
4596 <         * @param basis the identity (initial default value) for the reduction
4597 <         * @param reducer a commutative associative combining function
4598 <         * @return the task
4599 <         */
4600 <        public static <K,V> ForkJoinTask<Long> reduceValuesToLong
4601 <            (ConcurrentHashMapV8<K,V> map,
4602 <             ObjectToLong<? super V> transformer,
4603 <             long basis,
4604 <             LongByLongToLong reducer) {
4605 <            if (transformer == null || reducer == null)
4606 <                throw new NullPointerException();
4607 <            return new MapReduceValuesToLongTask<K,V>
4608 <                (map, transformer, basis, reducer);
4381 >        public int hashCode() {
4382 >            int h = 0;
4383 >            for (K e : this)
4384 >                h += e.hashCode();
4385 >            return h;
4386          }
4387  
4388 <        /**
4389 <         * Returns a task that when invoked, returns the result of
4390 <         * accumulating the given transformation of all values using the
4391 <         * given reducer to combine values, and the given basis as an
4392 <         * identity value.
4616 <         *
4617 <         * @param map the map
4618 <         * @param transformer a function returning the transformation
4619 <         * for an element
4620 <         * @param basis the identity (initial default value) for the reduction
4621 <         * @param reducer a commutative associative combining function
4622 <         * @return the task
4623 <         */
4624 <        public static <K,V> ForkJoinTask<Integer> reduceValuesToInt
4625 <            (ConcurrentHashMapV8<K,V> map,
4626 <             ObjectToInt<? super V> transformer,
4627 <             int basis,
4628 <             IntByIntToInt reducer) {
4629 <            if (transformer == null || reducer == null)
4630 <                throw new NullPointerException();
4631 <            return new MapReduceValuesToIntTask<K,V>
4632 <                (map, transformer, basis, reducer);
4388 >        public boolean equals(Object o) {
4389 >            Set<?> c;
4390 >            return ((o instanceof Set) &&
4391 >                    ((c = (Set<?>)o) == this ||
4392 >                     (containsAll(c) && c.containsAll(this))));
4393          }
4394  
4395 <        /**
4396 <         * Returns a task that when invoked, perform the given action
4397 <         * for each entry.
4398 <         *
4399 <         * @param map the map
4400 <         * @param action the action
4641 <         */
4642 <        public static <K,V> ForkJoinTask<Void> forEachEntry
4643 <            (ConcurrentHashMapV8<K,V> map,
4644 <             Action<Map.Entry<K,V>> action) {
4645 <            if (action == null) throw new NullPointerException();
4646 <            return new ForEachEntryTask<K,V>(map, action);
4395 >        public ConcurrentHashMapSpliterator<K> spliterator() {
4396 >            Node<K,V>[] t;
4397 >            ConcurrentHashMapV8<K,V> m = map;
4398 >            long n = m.sumCount();
4399 >            int f = (t = m.table) == null ? 0 : t.length;
4400 >            return new KeySpliterator<K,V>(t, f, 0, f, n < 0L ? 0L : n);
4401          }
4402  
4403 <        /**
4404 <         * Returns a task that when invoked, perform the given action
4405 <         * for each non-null transformation of each entry.
4406 <         *
4407 <         * @param map the map
4408 <         * @param transformer a function returning the transformation
4409 <         * for an element, or null of there is no transformation (in
4410 <         * which case the action is not applied).
4657 <         * @param action the action
4658 <         */
4659 <        public static <K,V,U> ForkJoinTask<Void> forEachEntry
4660 <            (ConcurrentHashMapV8<K,V> map,
4661 <             Fun<Map.Entry<K,V>, ? extends U> transformer,
4662 <             Action<U> action) {
4663 <            if (transformer == null || action == null)
4664 <                throw new NullPointerException();
4665 <            return new ForEachTransformedEntryTask<K,V,U>
4666 <                (map, transformer, action);
4403 >        public void forEach(Action<? super K> action) {
4404 >            if (action == null) throw new NullPointerException();
4405 >            Node<K,V>[] t;
4406 >            if ((t = map.table) != null) {
4407 >                Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length);
4408 >                for (Node<K,V> p; (p = it.advance()) != null; )
4409 >                    action.apply(p.key);
4410 >            }
4411          }
4412 +    }
4413  
4414 <        /**
4415 <         * Returns a task that when invoked, returns a non-null result
4416 <         * from applying the given search function on each entry, or
4417 <         * null if none.  Upon success, further element processing is
4418 <         * suppressed and the results of any other parallel
4419 <         * invocations of the search function are ignored.
4420 <         *
4421 <         * @param map the map
4422 <         * @param searchFunction a function returning a non-null
4423 <         * result on success, else null
4424 <         * @return the task
4680 <         *
4681 <         */
4682 <        public static <K,V,U> ForkJoinTask<U> searchEntries
4683 <            (ConcurrentHashMapV8<K,V> map,
4684 <             Fun<Map.Entry<K,V>, ? extends U> searchFunction) {
4685 <            if (searchFunction == null) throw new NullPointerException();
4686 <            return new SearchEntriesTask<K,V,U>
4687 <                (map, searchFunction,
4688 <                 new AtomicReference<U>());
4414 >    /**
4415 >     * A view of a ConcurrentHashMapV8 as a {@link Collection} of
4416 >     * values, in which additions are disabled. This class cannot be
4417 >     * directly instantiated. See {@link #values()}.
4418 >     */
4419 >    static final class ValuesView<K,V> extends CollectionView<K,V,V>
4420 >        implements Collection<V>, java.io.Serializable {
4421 >        private static final long serialVersionUID = 2249069246763182397L;
4422 >        ValuesView(ConcurrentHashMapV8<K,V> map) { super(map); }
4423 >        public final boolean contains(Object o) {
4424 >            return map.containsValue(o);
4425          }
4426  
4427 <        /**
4428 <         * Returns a task that when invoked, returns the result of
4429 <         * accumulating all entries using the given reducer to combine
4430 <         * values, or null if none.
4431 <         *
4432 <         * @param map the map
4433 <         * @param reducer a commutative associative combining function
4434 <         * @return the task
4435 <         */
4436 <        public static <K,V> ForkJoinTask<Map.Entry<K,V>> reduceEntries
4701 <            (ConcurrentHashMapV8<K,V> map,
4702 <             BiFun<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer) {
4703 <            if (reducer == null) throw new NullPointerException();
4704 <            return new ReduceEntriesTask<K,V>
4705 <                (map, reducer);
4427 >        public final boolean remove(Object o) {
4428 >            if (o != null) {
4429 >                for (Iterator<V> it = iterator(); it.hasNext();) {
4430 >                    if (o.equals(it.next())) {
4431 >                        it.remove();
4432 >                        return true;
4433 >                    }
4434 >                }
4435 >            }
4436 >            return false;
4437          }
4438  
4439 <        /**
4440 <         * Returns a task that when invoked, returns the result of
4441 <         * accumulating the given transformation of all entries using the
4442 <         * given reducer to combine values, or null if none.
4443 <         *
4713 <         * @param map the map
4714 <         * @param transformer a function returning the transformation
4715 <         * for an element, or null of there is no transformation (in
4716 <         * which case it is not combined).
4717 <         * @param reducer a commutative associative combining function
4718 <         * @return the task
4719 <         */
4720 <        public static <K,V,U> ForkJoinTask<U> reduceEntries
4721 <            (ConcurrentHashMapV8<K,V> map,
4722 <             Fun<Map.Entry<K,V>, ? extends U> transformer,
4723 <             BiFun<? super U, ? super U, ? extends U> reducer) {
4724 <            if (transformer == null || reducer == null)
4725 <                throw new NullPointerException();
4726 <            return new MapReduceEntriesTask<K,V,U>
4727 <                (map, transformer, reducer);
4439 >        public final Iterator<V> iterator() {
4440 >            ConcurrentHashMapV8<K,V> m = map;
4441 >            Node<K,V>[] t;
4442 >            int f = (t = m.table) == null ? 0 : t.length;
4443 >            return new ValueIterator<K,V>(t, f, 0, f, m);
4444          }
4445  
4446 <        /**
4447 <         * Returns a task that when invoked, returns the result of
4448 <         * accumulating the given transformation of all entries using the
4449 <         * given reducer to combine values, and the given basis as an
4450 <         * identity value.
4735 <         *
4736 <         * @param map the map
4737 <         * @param transformer a function returning the transformation
4738 <         * for an element
4739 <         * @param basis the identity (initial default value) for the reduction
4740 <         * @param reducer a commutative associative combining function
4741 <         * @return the task
4742 <         */
4743 <        public static <K,V> ForkJoinTask<Double> reduceEntriesToDouble
4744 <            (ConcurrentHashMapV8<K,V> map,
4745 <             ObjectToDouble<Map.Entry<K,V>> transformer,
4746 <             double basis,
4747 <             DoubleByDoubleToDouble reducer) {
4748 <            if (transformer == null || reducer == null)
4749 <                throw new NullPointerException();
4750 <            return new MapReduceEntriesToDoubleTask<K,V>
4751 <                (map, transformer, basis, reducer);
4446 >        public final boolean add(V e) {
4447 >            throw new UnsupportedOperationException();
4448 >        }
4449 >        public final boolean addAll(Collection<? extends V> c) {
4450 >            throw new UnsupportedOperationException();
4451          }
4452  
4453 <        /**
4454 <         * Returns a task that when invoked, returns the result of
4455 <         * accumulating the given transformation of all entries using the
4456 <         * given reducer to combine values, and the given basis as an
4457 <         * identity value.
4458 <         *
4760 <         * @param map the map
4761 <         * @param transformer a function returning the transformation
4762 <         * for an element
4763 <         * @param basis the identity (initial default value) for the reduction
4764 <         * @param reducer a commutative associative combining function
4765 <         * @return the task
4766 <         */
4767 <        public static <K,V> ForkJoinTask<Long> reduceEntriesToLong
4768 <            (ConcurrentHashMapV8<K,V> map,
4769 <             ObjectToLong<Map.Entry<K,V>> transformer,
4770 <             long basis,
4771 <             LongByLongToLong reducer) {
4772 <            if (transformer == null || reducer == null)
4773 <                throw new NullPointerException();
4774 <            return new MapReduceEntriesToLongTask<K,V>
4775 <                (map, transformer, basis, reducer);
4453 >        public ConcurrentHashMapSpliterator<V> spliterator() {
4454 >            Node<K,V>[] t;
4455 >            ConcurrentHashMapV8<K,V> m = map;
4456 >            long n = m.sumCount();
4457 >            int f = (t = m.table) == null ? 0 : t.length;
4458 >            return new ValueSpliterator<K,V>(t, f, 0, f, n < 0L ? 0L : n);
4459          }
4460  
4461 <        /**
4462 <         * Returns a task that when invoked, returns the result of
4463 <         * accumulating the given transformation of all entries using the
4464 <         * given reducer to combine values, and the given basis as an
4465 <         * identity value.
4466 <         *
4467 <         * @param map the map
4468 <         * @param transformer a function returning the transformation
4786 <         * for an element
4787 <         * @param basis the identity (initial default value) for the reduction
4788 <         * @param reducer a commutative associative combining function
4789 <         * @return the task
4790 <         */
4791 <        public static <K,V> ForkJoinTask<Integer> reduceEntriesToInt
4792 <            (ConcurrentHashMapV8<K,V> map,
4793 <             ObjectToInt<Map.Entry<K,V>> transformer,
4794 <             int basis,
4795 <             IntByIntToInt reducer) {
4796 <            if (transformer == null || reducer == null)
4797 <                throw new NullPointerException();
4798 <            return new MapReduceEntriesToIntTask<K,V>
4799 <                (map, transformer, basis, reducer);
4461 >        public void forEach(Action<? super V> action) {
4462 >            if (action == null) throw new NullPointerException();
4463 >            Node<K,V>[] t;
4464 >            if ((t = map.table) != null) {
4465 >                Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length);
4466 >                for (Node<K,V> p; (p = it.advance()) != null; )
4467 >                    action.apply(p.val);
4468 >            }
4469          }
4470      }
4471  
4803    // -------------------------------------------------------
4804
4472      /**
4473 <     * Base for FJ tasks for bulk operations. This adds a variant of
4474 <     * CountedCompleters and some split and merge bookkeeping to
4475 <     * iterator functionality. The forEach and reduce methods are
4476 <     * similar to those illustrated in CountedCompleter documentation,
4477 <     * except that bottom-up reduction completions perform them within
4478 <     * their compute methods. The search methods are like forEach
4479 <     * except they continually poll for success and exit early.  Also,
4480 <     * exceptions are handled in a simpler manner, by just trying to
4814 <     * complete root task exceptionally.
4815 <     */
4816 <    @SuppressWarnings("serial") static abstract class BulkTask<K,V,R> extends Traverser<K,V,R> {
4817 <        final BulkTask<K,V,?> parent;  // completion target
4818 <        int batch;                     // split control
4819 <        int pending;                   // completion control
4473 >     * A view of a ConcurrentHashMapV8 as a {@link Set} of (key, value)
4474 >     * entries.  This class cannot be directly instantiated. See
4475 >     * {@link #entrySet()}.
4476 >     */
4477 >    static final class EntrySetView<K,V> extends CollectionView<K,V,Map.Entry<K,V>>
4478 >        implements Set<Map.Entry<K,V>>, java.io.Serializable {
4479 >        private static final long serialVersionUID = 2249069246763182397L;
4480 >        EntrySetView(ConcurrentHashMapV8<K,V> map) { super(map); }
4481  
4482 <        /** Constructor for root tasks */
4483 <        BulkTask(ConcurrentHashMapV8<K,V> map) {
4484 <            super(map);
4485 <            this.parent = null;
4486 <            this.batch = -1; // force call to batch() on execution
4482 >        public boolean contains(Object o) {
4483 >            Object k, v, r; Map.Entry<?,?> e;
4484 >            return ((o instanceof Map.Entry) &&
4485 >                    (k = (e = (Map.Entry<?,?>)o).getKey()) != null &&
4486 >                    (r = map.get(k)) != null &&
4487 >                    (v = e.getValue()) != null &&
4488 >                    (v == r || v.equals(r)));
4489          }
4490  
4491 <        /** Constructor for subtasks */
4492 <        BulkTask(BulkTask<K,V,?> parent, int batch) {
4493 <            super(parent);
4494 <            this.parent = parent;
4495 <            this.batch = batch;
4491 >        public boolean remove(Object o) {
4492 >            Object k, v; Map.Entry<?,?> e;
4493 >            return ((o instanceof Map.Entry) &&
4494 >                    (k = (e = (Map.Entry<?,?>)o).getKey()) != null &&
4495 >                    (v = e.getValue()) != null &&
4496 >                    map.remove(k, v));
4497          }
4498  
4835        // FJ methods
4836
4499          /**
4500 <         * Propagates completion. Note that all reduce actions
4839 <         * bypass this method to combine while completing.
4500 >         * @return an iterator over the entries of the backing map
4501           */
4502 <        final void tryComplete() {
4503 <            BulkTask<K,V,?> a = this, s = a;
4504 <            for (int c;;) {
4505 <                if ((c = a.pending) == 0) {
4506 <                    if ((a = (s = a).parent) == null) {
4507 <                        s.quietlyComplete();
4508 <                        break;
4509 <                    }
4510 <                }
4511 <                else if (U.compareAndSwapInt(a, PENDING, c, c - 1))
4512 <                    break;
4502 >        public Iterator<Map.Entry<K,V>> iterator() {
4503 >            ConcurrentHashMapV8<K,V> m = map;
4504 >            Node<K,V>[] t;
4505 >            int f = (t = m.table) == null ? 0 : t.length;
4506 >            return new EntryIterator<K,V>(t, f, 0, f, m);
4507 >        }
4508 >
4509 >        public boolean add(Entry<K,V> e) {
4510 >            return map.putVal(e.getKey(), e.getValue(), false) == null;
4511 >        }
4512 >
4513 >        public boolean addAll(Collection<? extends Entry<K,V>> c) {
4514 >            boolean added = false;
4515 >            for (Entry<K,V> e : c) {
4516 >                if (add(e))
4517 >                    added = true;
4518              }
4519 +            return added;
4520          }
4521  
4522 <        /**
4523 <         * Forces root task to complete.
4524 <         * @param ex if null, complete normally, else exceptionally
4525 <         * @return false to simplify use
4526 <         */
4527 <        final boolean tryCompleteComputation(Throwable ex) {
4528 <            for (BulkTask<K,V,?> a = this;;) {
4862 <                BulkTask<K,V,?> p = a.parent;
4863 <                if (p == null) {
4864 <                    if (ex != null)
4865 <                        a.completeExceptionally(ex);
4866 <                    else
4867 <                        a.quietlyComplete();
4868 <                    return false;
4522 >        public final int hashCode() {
4523 >            int h = 0;
4524 >            Node<K,V>[] t;
4525 >            if ((t = map.table) != null) {
4526 >                Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length);
4527 >                for (Node<K,V> p; (p = it.advance()) != null; ) {
4528 >                    h += p.hashCode();
4529                  }
4870                a = p;
4530              }
4531 +            return h;
4532          }
4533  
4534 <        /**
4535 <         * Version of tryCompleteComputation for function screening checks
4536 <         */
4537 <        final boolean abortOnNullFunction() {
4538 <            return tryCompleteComputation(new Error("Unexpected null function"));
4534 >        public final boolean equals(Object o) {
4535 >            Set<?> c;
4536 >            return ((o instanceof Set) &&
4537 >                    ((c = (Set<?>)o) == this ||
4538 >                     (containsAll(c) && c.containsAll(this))));
4539          }
4540  
4541 <        // utilities
4541 >        public ConcurrentHashMapSpliterator<Map.Entry<K,V>> spliterator() {
4542 >            Node<K,V>[] t;
4543 >            ConcurrentHashMapV8<K,V> m = map;
4544 >            long n = m.sumCount();
4545 >            int f = (t = m.table) == null ? 0 : t.length;
4546 >            return new EntrySpliterator<K,V>(t, f, 0, f, n < 0L ? 0L : n, m);
4547 >        }
4548  
4549 <        /** CompareAndSet pending count */
4550 <        final boolean casPending(int cmp, int val) {
4551 <            return U.compareAndSwapInt(this, PENDING, cmp, val);
4549 >        public void forEach(Action<? super Map.Entry<K,V>> action) {
4550 >            if (action == null) throw new NullPointerException();
4551 >            Node<K,V>[] t;
4552 >            if ((t = map.table) != null) {
4553 >                Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length);
4554 >                for (Node<K,V> p; (p = it.advance()) != null; )
4555 >                    action.apply(new MapEntry<K,V>(p.key, p.val, map));
4556 >            }
4557          }
4558  
4559 <        /**
4560 <         * Returns approx exp2 of the number of times (minus one) to
4561 <         * split task by two before executing leaf action. This value
4562 <         * is faster to compute and more convenient to use as a guide
4563 <         * to splitting than is the depth, since it is used while
4564 <         * dividing by two anyway.
4565 <         */
4566 <        final int batch() {
4567 <            int b = batch;
4568 <            if (b < 0) {
4569 <                long n = map.counter.sum();
4570 <                int sp = getPool().getParallelism() << 3; // slack of 8
4571 <                b = batch = (n <= 0L) ? 0 : (n < (long)sp) ? (int)n : sp;
4559 >    }
4560 >
4561 >    // -------------------------------------------------------
4562 >
4563 >    /**
4564 >     * Base class for bulk tasks. Repeats some fields and code from
4565 >     * class Traverser, because we need to subclass CountedCompleter.
4566 >     */
4567 >    abstract static class BulkTask<K,V,R> extends CountedCompleter<R> {
4568 >        Node<K,V>[] tab;        // same as Traverser
4569 >        Node<K,V> next;
4570 >        int index;
4571 >        int baseIndex;
4572 >        int baseLimit;
4573 >        final int baseSize;
4574 >        int batch;              // split control
4575 >
4576 >        BulkTask(BulkTask<K,V,?> par, int b, int i, int f, Node<K,V>[] t) {
4577 >            super(par);
4578 >            this.batch = b;
4579 >            this.index = this.baseIndex = i;
4580 >            if ((this.tab = t) == null)
4581 >                this.baseSize = this.baseLimit = 0;
4582 >            else if (par == null)
4583 >                this.baseSize = this.baseLimit = t.length;
4584 >            else {
4585 >                this.baseLimit = f;
4586 >                this.baseSize = par.baseSize;
4587              }
4902            return b;
4588          }
4589  
4590          /**
4591 <         * Returns exportable snapshot entry.
4591 >         * Same as Traverser version
4592           */
4593 <        static <K,V> AbstractMap.SimpleEntry<K,V> entryFor(K k, V v) {
4594 <            return new AbstractMap.SimpleEntry<K,V>(k, v);
4595 <        }
4596 <
4597 <        // Unsafe mechanics
4598 <        private static final sun.misc.Unsafe U;
4599 <        private static final long PENDING;
4600 <        static {
4601 <            try {
4602 <                U = getUnsafe();
4603 <                PENDING = U.objectFieldOffset
4604 <                    (BulkTask.class.getDeclaredField("pending"));
4605 <            } catch (Exception e) {
4606 <                throw new Error(e);
4593 >        final Node<K,V> advance() {
4594 >            Node<K,V> e;
4595 >            if ((e = next) != null)
4596 >                e = e.next;
4597 >            for (;;) {
4598 >                Node<K,V>[] t; int i, n; K ek;  // must use locals in checks
4599 >                if (e != null)
4600 >                    return next = e;
4601 >                if (baseIndex >= baseLimit || (t = tab) == null ||
4602 >                    (n = t.length) <= (i = index) || i < 0)
4603 >                    return next = null;
4604 >                if ((e = tabAt(t, index)) != null && e.hash < 0) {
4605 >                    if (e instanceof ForwardingNode) {
4606 >                        tab = ((ForwardingNode<K,V>)e).nextTable;
4607 >                        e = null;
4608 >                        continue;
4609 >                    }
4610 >                    else if (e instanceof TreeBin)
4611 >                        e = ((TreeBin<K,V>)e).first;
4612 >                    else
4613 >                        e = null;
4614 >                }
4615 >                if ((index += baseSize) >= n)
4616 >                    index = ++baseIndex;    // visit upper slots if present
4617              }
4618          }
4619      }
# Line 4926 | Line 4621 | public class ConcurrentHashMapV8<K, V>
4621      /*
4622       * Task classes. Coded in a regular but ugly format/style to
4623       * simplify checks that each variant differs in the right way from
4624 <     * others.
4624 >     * others. The null screenings exist because compilers cannot tell
4625 >     * that we've already null-checked task arguments, so we force
4626 >     * simplest hoisted bypass to help avoid convoluted traps.
4627       */
4628 <
4629 <    @SuppressWarnings("serial") static final class ForEachKeyTask<K,V>
4628 >    @SuppressWarnings("serial")
4629 >    static final class ForEachKeyTask<K,V>
4630          extends BulkTask<K,V,Void> {
4631 <        final Action<K> action;
4935 <        ForEachKeyTask
4936 <            (ConcurrentHashMapV8<K,V> m,
4937 <             Action<K> action) {
4938 <            super(m);
4939 <            this.action = action;
4940 <        }
4631 >        final Action<? super K> action;
4632          ForEachKeyTask
4633 <            (BulkTask<K,V,?> p, int b,
4634 <             Action<K> action) {
4635 <            super(p, b);
4633 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
4634 >             Action<? super K> action) {
4635 >            super(p, b, i, f, t);
4636              this.action = action;
4637          }
4638 <        @SuppressWarnings("unchecked") public final boolean exec() {
4639 <            final Action<K> action = this.action;
4640 <            if (action == null)
4641 <                return abortOnNullFunction();
4642 <            try {
4643 <                int b = batch(), c;
4644 <                while (b > 1 && baseIndex != baseLimit) {
4645 <                    do {} while (!casPending(c = pending, c+1));
4646 <                    new ForEachKeyTask<K,V>(this, b >>>= 1, action).fork();
4647 <                }
4648 <                while (advance() != null)
4649 <                    action.apply((K)nextKey);
4650 <                tryComplete();
4960 <            } catch (Throwable ex) {
4961 <                return tryCompleteComputation(ex);
4638 >        public final void compute() {
4639 >            final Action<? super K> action;
4640 >            if ((action = this.action) != null) {
4641 >                for (int i = baseIndex, f, h; batch > 0 &&
4642 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
4643 >                    addToPendingCount(1);
4644 >                    new ForEachKeyTask<K,V>
4645 >                        (this, batch >>>= 1, baseLimit = h, f, tab,
4646 >                         action).fork();
4647 >                }
4648 >                for (Node<K,V> p; (p = advance()) != null;)
4649 >                    action.apply(p.key);
4650 >                propagateCompletion();
4651              }
4963            return false;
4652          }
4653      }
4654  
4655 <    @SuppressWarnings("serial") static final class ForEachValueTask<K,V>
4655 >    @SuppressWarnings("serial")
4656 >    static final class ForEachValueTask<K,V>
4657          extends BulkTask<K,V,Void> {
4658 <        final Action<V> action;
4970 <        ForEachValueTask
4971 <            (ConcurrentHashMapV8<K,V> m,
4972 <             Action<V> action) {
4973 <            super(m);
4974 <            this.action = action;
4975 <        }
4658 >        final Action<? super V> action;
4659          ForEachValueTask
4660 <            (BulkTask<K,V,?> p, int b,
4661 <             Action<V> action) {
4662 <            super(p, b);
4660 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
4661 >             Action<? super V> action) {
4662 >            super(p, b, i, f, t);
4663              this.action = action;
4664          }
4665 <        @SuppressWarnings("unchecked") public final boolean exec() {
4666 <            final Action<V> action = this.action;
4667 <            if (action == null)
4668 <                return abortOnNullFunction();
4669 <            try {
4670 <                int b = batch(), c;
4671 <                while (b > 1 && baseIndex != baseLimit) {
4672 <                    do {} while (!casPending(c = pending, c+1));
4673 <                    new ForEachValueTask<K,V>(this, b >>>= 1, action).fork();
4674 <                }
4675 <                Object v;
4676 <                while ((v = advance()) != null)
4677 <                    action.apply((V)v);
4995 <                tryComplete();
4996 <            } catch (Throwable ex) {
4997 <                return tryCompleteComputation(ex);
4665 >        public final void compute() {
4666 >            final Action<? super V> action;
4667 >            if ((action = this.action) != null) {
4668 >                for (int i = baseIndex, f, h; batch > 0 &&
4669 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
4670 >                    addToPendingCount(1);
4671 >                    new ForEachValueTask<K,V>
4672 >                        (this, batch >>>= 1, baseLimit = h, f, tab,
4673 >                         action).fork();
4674 >                }
4675 >                for (Node<K,V> p; (p = advance()) != null;)
4676 >                    action.apply(p.val);
4677 >                propagateCompletion();
4678              }
4999            return false;
4679          }
4680      }
4681  
4682 <    @SuppressWarnings("serial") static final class ForEachEntryTask<K,V>
4682 >    @SuppressWarnings("serial")
4683 >    static final class ForEachEntryTask<K,V>
4684          extends BulkTask<K,V,Void> {
4685 <        final Action<Entry<K,V>> action;
5006 <        ForEachEntryTask
5007 <            (ConcurrentHashMapV8<K,V> m,
5008 <             Action<Entry<K,V>> action) {
5009 <            super(m);
5010 <            this.action = action;
5011 <        }
4685 >        final Action<? super Entry<K,V>> action;
4686          ForEachEntryTask
4687 <            (BulkTask<K,V,?> p, int b,
4688 <             Action<Entry<K,V>> action) {
4689 <            super(p, b);
4687 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
4688 >             Action<? super Entry<K,V>> action) {
4689 >            super(p, b, i, f, t);
4690              this.action = action;
4691          }
4692 <        @SuppressWarnings("unchecked") public final boolean exec() {
4693 <            final Action<Entry<K,V>> action = this.action;
4694 <            if (action == null)
4695 <                return abortOnNullFunction();
4696 <            try {
4697 <                int b = batch(), c;
4698 <                while (b > 1 && baseIndex != baseLimit) {
4699 <                    do {} while (!casPending(c = pending, c+1));
4700 <                    new ForEachEntryTask<K,V>(this, b >>>= 1, action).fork();
4701 <                }
4702 <                Object v;
4703 <                while ((v = advance()) != null)
4704 <                    action.apply(entryFor((K)nextKey, (V)v));
5031 <                tryComplete();
5032 <            } catch (Throwable ex) {
5033 <                return tryCompleteComputation(ex);
4692 >        public final void compute() {
4693 >            final Action<? super Entry<K,V>> action;
4694 >            if ((action = this.action) != null) {
4695 >                for (int i = baseIndex, f, h; batch > 0 &&
4696 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
4697 >                    addToPendingCount(1);
4698 >                    new ForEachEntryTask<K,V>
4699 >                        (this, batch >>>= 1, baseLimit = h, f, tab,
4700 >                         action).fork();
4701 >                }
4702 >                for (Node<K,V> p; (p = advance()) != null; )
4703 >                    action.apply(p);
4704 >                propagateCompletion();
4705              }
5035            return false;
4706          }
4707      }
4708  
4709 <    @SuppressWarnings("serial") static final class ForEachMappingTask<K,V>
4709 >    @SuppressWarnings("serial")
4710 >    static final class ForEachMappingTask<K,V>
4711          extends BulkTask<K,V,Void> {
4712 <        final BiAction<K,V> action;
4712 >        final BiAction<? super K, ? super V> action;
4713          ForEachMappingTask
4714 <            (ConcurrentHashMapV8<K,V> m,
4715 <             BiAction<K,V> action) {
4716 <            super(m);
4714 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
4715 >             BiAction<? super K,? super V> action) {
4716 >            super(p, b, i, f, t);
4717              this.action = action;
4718          }
4719 <        ForEachMappingTask
4720 <            (BulkTask<K,V,?> p, int b,
4721 <             BiAction<K,V> action) {
4722 <            super(p, b);
4723 <            this.action = action;
4724 <        }
4725 <
4726 <        @SuppressWarnings("unchecked") public final boolean exec() {
4727 <            final BiAction<K,V> action = this.action;
4728 <            if (action == null)
4729 <                return abortOnNullFunction();
4730 <            try {
4731 <                int b = batch(), c;
5061 <                while (b > 1 && baseIndex != baseLimit) {
5062 <                    do {} while (!casPending(c = pending, c+1));
5063 <                    new ForEachMappingTask<K,V>(this, b >>>= 1,
5064 <                                                action).fork();
5065 <                }
5066 <                Object v;
5067 <                while ((v = advance()) != null)
5068 <                    action.apply((K)nextKey, (V)v);
5069 <                tryComplete();
5070 <            } catch (Throwable ex) {
5071 <                return tryCompleteComputation(ex);
4719 >        public final void compute() {
4720 >            final BiAction<? super K, ? super V> action;
4721 >            if ((action = this.action) != null) {
4722 >                for (int i = baseIndex, f, h; batch > 0 &&
4723 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
4724 >                    addToPendingCount(1);
4725 >                    new ForEachMappingTask<K,V>
4726 >                        (this, batch >>>= 1, baseLimit = h, f, tab,
4727 >                         action).fork();
4728 >                }
4729 >                for (Node<K,V> p; (p = advance()) != null; )
4730 >                    action.apply(p.key, p.val);
4731 >                propagateCompletion();
4732              }
5073            return false;
4733          }
4734      }
4735  
4736 <    @SuppressWarnings("serial") static final class ForEachTransformedKeyTask<K,V,U>
4736 >    @SuppressWarnings("serial")
4737 >    static final class ForEachTransformedKeyTask<K,V,U>
4738          extends BulkTask<K,V,Void> {
4739          final Fun<? super K, ? extends U> transformer;
4740 <        final Action<U> action;
5081 <        ForEachTransformedKeyTask
5082 <            (ConcurrentHashMapV8<K,V> m,
5083 <             Fun<? super K, ? extends U> transformer,
5084 <             Action<U> action) {
5085 <            super(m);
5086 <            this.transformer = transformer;
5087 <            this.action = action;
5088 <
5089 <        }
4740 >        final Action<? super U> action;
4741          ForEachTransformedKeyTask
4742 <            (BulkTask<K,V,?> p, int b,
4743 <             Fun<? super K, ? extends U> transformer,
4744 <             Action<U> action) {
4745 <            super(p, b);
4746 <            this.transformer = transformer;
4747 <            this.action = action;
4748 <        }
4749 <        @SuppressWarnings("unchecked") public final boolean exec() {
4750 <            final Fun<? super K, ? extends U> transformer =
4751 <                this.transformer;
4752 <            final Action<U> action = this.action;
4753 <            if (transformer == null || action == null)
4754 <                return abortOnNullFunction();
5104 <            try {
5105 <                int b = batch(), c;
5106 <                while (b > 1 && baseIndex != baseLimit) {
5107 <                    do {} while (!casPending(c = pending, c+1));
4742 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
4743 >             Fun<? super K, ? extends U> transformer, Action<? super U> action) {
4744 >            super(p, b, i, f, t);
4745 >            this.transformer = transformer; this.action = action;
4746 >        }
4747 >        public final void compute() {
4748 >            final Fun<? super K, ? extends U> transformer;
4749 >            final Action<? super U> action;
4750 >            if ((transformer = this.transformer) != null &&
4751 >                (action = this.action) != null) {
4752 >                for (int i = baseIndex, f, h; batch > 0 &&
4753 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
4754 >                    addToPendingCount(1);
4755                      new ForEachTransformedKeyTask<K,V,U>
4756 <                        (this, b >>>= 1, transformer, action).fork();
4756 >                        (this, batch >>>= 1, baseLimit = h, f, tab,
4757 >                         transformer, action).fork();
4758                  }
4759 <                U u;
4760 <                while (advance() != null) {
4761 <                    if ((u = transformer.apply((K)nextKey)) != null)
4759 >                for (Node<K,V> p; (p = advance()) != null; ) {
4760 >                    U u;
4761 >                    if ((u = transformer.apply(p.key)) != null)
4762                          action.apply(u);
4763                  }
4764 <                tryComplete();
5117 <            } catch (Throwable ex) {
5118 <                return tryCompleteComputation(ex);
4764 >                propagateCompletion();
4765              }
5120            return false;
4766          }
4767      }
4768  
4769 <    @SuppressWarnings("serial") static final class ForEachTransformedValueTask<K,V,U>
4769 >    @SuppressWarnings("serial")
4770 >    static final class ForEachTransformedValueTask<K,V,U>
4771          extends BulkTask<K,V,Void> {
4772          final Fun<? super V, ? extends U> transformer;
4773 <        final Action<U> action;
4773 >        final Action<? super U> action;
4774          ForEachTransformedValueTask
4775 <            (ConcurrentHashMapV8<K,V> m,
4776 <             Fun<? super V, ? extends U> transformer,
4777 <             Action<U> action) {
4778 <            super(m);
4779 <            this.transformer = transformer;
4780 <            this.action = action;
4781 <
4782 <        }
4783 <        ForEachTransformedValueTask
4784 <            (BulkTask<K,V,?> p, int b,
4785 <             Fun<? super V, ? extends U> transformer,
4786 <             Action<U> action) {
4787 <            super(p, b);
5142 <            this.transformer = transformer;
5143 <            this.action = action;
5144 <        }
5145 <        @SuppressWarnings("unchecked") public final boolean exec() {
5146 <            final Fun<? super V, ? extends U> transformer =
5147 <                this.transformer;
5148 <            final Action<U> action = this.action;
5149 <            if (transformer == null || action == null)
5150 <                return abortOnNullFunction();
5151 <            try {
5152 <                int b = batch(), c;
5153 <                while (b > 1 && baseIndex != baseLimit) {
5154 <                    do {} while (!casPending(c = pending, c+1));
4775 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
4776 >             Fun<? super V, ? extends U> transformer, Action<? super U> action) {
4777 >            super(p, b, i, f, t);
4778 >            this.transformer = transformer; this.action = action;
4779 >        }
4780 >        public final void compute() {
4781 >            final Fun<? super V, ? extends U> transformer;
4782 >            final Action<? super U> action;
4783 >            if ((transformer = this.transformer) != null &&
4784 >                (action = this.action) != null) {
4785 >                for (int i = baseIndex, f, h; batch > 0 &&
4786 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
4787 >                    addToPendingCount(1);
4788                      new ForEachTransformedValueTask<K,V,U>
4789 <                        (this, b >>>= 1, transformer, action).fork();
4789 >                        (this, batch >>>= 1, baseLimit = h, f, tab,
4790 >                         transformer, action).fork();
4791                  }
4792 <                Object v; U u;
4793 <                while ((v = advance()) != null) {
4794 <                    if ((u = transformer.apply((V)v)) != null)
4792 >                for (Node<K,V> p; (p = advance()) != null; ) {
4793 >                    U u;
4794 >                    if ((u = transformer.apply(p.val)) != null)
4795                          action.apply(u);
4796                  }
4797 <                tryComplete();
5164 <            } catch (Throwable ex) {
5165 <                return tryCompleteComputation(ex);
4797 >                propagateCompletion();
4798              }
5167            return false;
4799          }
4800      }
4801  
4802 <    @SuppressWarnings("serial") static final class ForEachTransformedEntryTask<K,V,U>
4802 >    @SuppressWarnings("serial")
4803 >    static final class ForEachTransformedEntryTask<K,V,U>
4804          extends BulkTask<K,V,Void> {
4805          final Fun<Map.Entry<K,V>, ? extends U> transformer;
4806 <        final Action<U> action;
5175 <        ForEachTransformedEntryTask
5176 <            (ConcurrentHashMapV8<K,V> m,
5177 <             Fun<Map.Entry<K,V>, ? extends U> transformer,
5178 <             Action<U> action) {
5179 <            super(m);
5180 <            this.transformer = transformer;
5181 <            this.action = action;
5182 <
5183 <        }
4806 >        final Action<? super U> action;
4807          ForEachTransformedEntryTask
4808 <            (BulkTask<K,V,?> p, int b,
4809 <             Fun<Map.Entry<K,V>, ? extends U> transformer,
4810 <             Action<U> action) {
4811 <            super(p, b);
4812 <            this.transformer = transformer;
4813 <            this.action = action;
4814 <        }
4815 <        @SuppressWarnings("unchecked") public final boolean exec() {
4816 <            final Fun<Map.Entry<K,V>, ? extends U> transformer =
4817 <                this.transformer;
4818 <            final Action<U> action = this.action;
4819 <            if (transformer == null || action == null)
4820 <                return abortOnNullFunction();
5198 <            try {
5199 <                int b = batch(), c;
5200 <                while (b > 1 && baseIndex != baseLimit) {
5201 <                    do {} while (!casPending(c = pending, c+1));
4808 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
4809 >             Fun<Map.Entry<K,V>, ? extends U> transformer, Action<? super U> action) {
4810 >            super(p, b, i, f, t);
4811 >            this.transformer = transformer; this.action = action;
4812 >        }
4813 >        public final void compute() {
4814 >            final Fun<Map.Entry<K,V>, ? extends U> transformer;
4815 >            final Action<? super U> action;
4816 >            if ((transformer = this.transformer) != null &&
4817 >                (action = this.action) != null) {
4818 >                for (int i = baseIndex, f, h; batch > 0 &&
4819 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
4820 >                    addToPendingCount(1);
4821                      new ForEachTransformedEntryTask<K,V,U>
4822 <                        (this, b >>>= 1, transformer, action).fork();
4822 >                        (this, batch >>>= 1, baseLimit = h, f, tab,
4823 >                         transformer, action).fork();
4824                  }
4825 <                Object v; U u;
4826 <                while ((v = advance()) != null) {
4827 <                    if ((u = transformer.apply(entryFor((K)nextKey, (V)v))) != null)
4825 >                for (Node<K,V> p; (p = advance()) != null; ) {
4826 >                    U u;
4827 >                    if ((u = transformer.apply(p)) != null)
4828                          action.apply(u);
4829                  }
4830 <                tryComplete();
5211 <            } catch (Throwable ex) {
5212 <                return tryCompleteComputation(ex);
4830 >                propagateCompletion();
4831              }
5214            return false;
4832          }
4833      }
4834  
4835 <    @SuppressWarnings("serial") static final class ForEachTransformedMappingTask<K,V,U>
4835 >    @SuppressWarnings("serial")
4836 >    static final class ForEachTransformedMappingTask<K,V,U>
4837          extends BulkTask<K,V,Void> {
4838          final BiFun<? super K, ? super V, ? extends U> transformer;
4839 <        final Action<U> action;
4839 >        final Action<? super U> action;
4840          ForEachTransformedMappingTask
4841 <            (ConcurrentHashMapV8<K,V> m,
4841 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
4842               BiFun<? super K, ? super V, ? extends U> transformer,
4843 <             Action<U> action) {
4844 <            super(m);
4845 <            this.transformer = transformer;
4846 <            this.action = action;
4847 <
4848 <        }
4849 <        ForEachTransformedMappingTask
4850 <            (BulkTask<K,V,?> p, int b,
4851 <             BiFun<? super K, ? super V, ? extends U> transformer,
4852 <             Action<U> action) {
4853 <            super(p, b);
4854 <            this.transformer = transformer;
5237 <            this.action = action;
5238 <        }
5239 <        @SuppressWarnings("unchecked") public final boolean exec() {
5240 <            final BiFun<? super K, ? super V, ? extends U> transformer =
5241 <                this.transformer;
5242 <            final Action<U> action = this.action;
5243 <            if (transformer == null || action == null)
5244 <                return abortOnNullFunction();
5245 <            try {
5246 <                int b = batch(), c;
5247 <                while (b > 1 && baseIndex != baseLimit) {
5248 <                    do {} while (!casPending(c = pending, c+1));
4843 >             Action<? super U> action) {
4844 >            super(p, b, i, f, t);
4845 >            this.transformer = transformer; this.action = action;
4846 >        }
4847 >        public final void compute() {
4848 >            final BiFun<? super K, ? super V, ? extends U> transformer;
4849 >            final Action<? super U> action;
4850 >            if ((transformer = this.transformer) != null &&
4851 >                (action = this.action) != null) {
4852 >                for (int i = baseIndex, f, h; batch > 0 &&
4853 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
4854 >                    addToPendingCount(1);
4855                      new ForEachTransformedMappingTask<K,V,U>
4856 <                        (this, b >>>= 1, transformer, action).fork();
4856 >                        (this, batch >>>= 1, baseLimit = h, f, tab,
4857 >                         transformer, action).fork();
4858                  }
4859 <                Object v; U u;
4860 <                while ((v = advance()) != null) {
4861 <                    if ((u = transformer.apply((K)nextKey, (V)v)) != null)
4859 >                for (Node<K,V> p; (p = advance()) != null; ) {
4860 >                    U u;
4861 >                    if ((u = transformer.apply(p.key, p.val)) != null)
4862                          action.apply(u);
4863                  }
4864 <                tryComplete();
5258 <            } catch (Throwable ex) {
5259 <                return tryCompleteComputation(ex);
4864 >                propagateCompletion();
4865              }
5261            return false;
4866          }
4867      }
4868  
4869 <    @SuppressWarnings("serial") static final class SearchKeysTask<K,V,U>
4869 >    @SuppressWarnings("serial")
4870 >    static final class SearchKeysTask<K,V,U>
4871          extends BulkTask<K,V,U> {
4872          final Fun<? super K, ? extends U> searchFunction;
4873          final AtomicReference<U> result;
4874          SearchKeysTask
4875 <            (ConcurrentHashMapV8<K,V> m,
5271 <             Fun<? super K, ? extends U> searchFunction,
5272 <             AtomicReference<U> result) {
5273 <            super(m);
5274 <            this.searchFunction = searchFunction; this.result = result;
5275 <        }
5276 <        SearchKeysTask
5277 <            (BulkTask<K,V,?> p, int b,
4875 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
4876               Fun<? super K, ? extends U> searchFunction,
4877               AtomicReference<U> result) {
4878 <            super(p, b);
4878 >            super(p, b, i, f, t);
4879              this.searchFunction = searchFunction; this.result = result;
4880          }
4881 <        @SuppressWarnings("unchecked") public final boolean exec() {
4882 <            AtomicReference<U> result = this.result;
4883 <            final Fun<? super K, ? extends U> searchFunction =
4884 <                this.searchFunction;
4885 <            if (searchFunction == null || result == null)
4886 <                return abortOnNullFunction();
4887 <            try {
4888 <                int b = batch(), c;
4889 <                while (b > 1 && baseIndex != baseLimit && result.get() == null) {
4890 <                    do {} while (!casPending(c = pending, c+1));
4891 <                    new SearchKeysTask<K,V,U>(this, b >>>= 1,
4892 <                                              searchFunction, result).fork();
4893 <                }
4894 <                U u;
4895 <                while (result.get() == null && advance() != null) {
4896 <                    if ((u = searchFunction.apply((K)nextKey)) != null) {
4881 >        public final U getRawResult() { return result.get(); }
4882 >        public final void compute() {
4883 >            final Fun<? super K, ? extends U> searchFunction;
4884 >            final AtomicReference<U> result;
4885 >            if ((searchFunction = this.searchFunction) != null &&
4886 >                (result = this.result) != null) {
4887 >                for (int i = baseIndex, f, h; batch > 0 &&
4888 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
4889 >                    if (result.get() != null)
4890 >                        return;
4891 >                    addToPendingCount(1);
4892 >                    new SearchKeysTask<K,V,U>
4893 >                        (this, batch >>>= 1, baseLimit = h, f, tab,
4894 >                         searchFunction, result).fork();
4895 >                }
4896 >                while (result.get() == null) {
4897 >                    U u;
4898 >                    Node<K,V> p;
4899 >                    if ((p = advance()) == null) {
4900 >                        propagateCompletion();
4901 >                        break;
4902 >                    }
4903 >                    if ((u = searchFunction.apply(p.key)) != null) {
4904                          if (result.compareAndSet(null, u))
4905 <                            tryCompleteComputation(null);
4905 >                            quietlyCompleteRoot();
4906                          break;
4907                      }
4908                  }
5304                tryComplete();
5305            } catch (Throwable ex) {
5306                return tryCompleteComputation(ex);
4909              }
5308            return false;
4910          }
5310        public final U getRawResult() { return result.get(); }
4911      }
4912  
4913 <    @SuppressWarnings("serial") static final class SearchValuesTask<K,V,U>
4913 >    @SuppressWarnings("serial")
4914 >    static final class SearchValuesTask<K,V,U>
4915          extends BulkTask<K,V,U> {
4916          final Fun<? super V, ? extends U> searchFunction;
4917          final AtomicReference<U> result;
4918          SearchValuesTask
4919 <            (ConcurrentHashMapV8<K,V> m,
5319 <             Fun<? super V, ? extends U> searchFunction,
5320 <             AtomicReference<U> result) {
5321 <            super(m);
5322 <            this.searchFunction = searchFunction; this.result = result;
5323 <        }
5324 <        SearchValuesTask
5325 <            (BulkTask<K,V,?> p, int b,
4919 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
4920               Fun<? super V, ? extends U> searchFunction,
4921               AtomicReference<U> result) {
4922 <            super(p, b);
4922 >            super(p, b, i, f, t);
4923              this.searchFunction = searchFunction; this.result = result;
4924          }
4925 <        @SuppressWarnings("unchecked") public final boolean exec() {
4926 <            AtomicReference<U> result = this.result;
4927 <            final Fun<? super V, ? extends U> searchFunction =
4928 <                this.searchFunction;
4929 <            if (searchFunction == null || result == null)
4930 <                return abortOnNullFunction();
4931 <            try {
4932 <                int b = batch(), c;
4933 <                while (b > 1 && baseIndex != baseLimit && result.get() == null) {
4934 <                    do {} while (!casPending(c = pending, c+1));
4935 <                    new SearchValuesTask<K,V,U>(this, b >>>= 1,
4936 <                                                searchFunction, result).fork();
4937 <                }
4938 <                Object v; U u;
4939 <                while (result.get() == null && (v = advance()) != null) {
4940 <                    if ((u = searchFunction.apply((V)v)) != null) {
4925 >        public final U getRawResult() { return result.get(); }
4926 >        public final void compute() {
4927 >            final Fun<? super V, ? extends U> searchFunction;
4928 >            final AtomicReference<U> result;
4929 >            if ((searchFunction = this.searchFunction) != null &&
4930 >                (result = this.result) != null) {
4931 >                for (int i = baseIndex, f, h; batch > 0 &&
4932 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
4933 >                    if (result.get() != null)
4934 >                        return;
4935 >                    addToPendingCount(1);
4936 >                    new SearchValuesTask<K,V,U>
4937 >                        (this, batch >>>= 1, baseLimit = h, f, tab,
4938 >                         searchFunction, result).fork();
4939 >                }
4940 >                while (result.get() == null) {
4941 >                    U u;
4942 >                    Node<K,V> p;
4943 >                    if ((p = advance()) == null) {
4944 >                        propagateCompletion();
4945 >                        break;
4946 >                    }
4947 >                    if ((u = searchFunction.apply(p.val)) != null) {
4948                          if (result.compareAndSet(null, u))
4949 <                            tryCompleteComputation(null);
4949 >                            quietlyCompleteRoot();
4950                          break;
4951                      }
4952                  }
5352                tryComplete();
5353            } catch (Throwable ex) {
5354                return tryCompleteComputation(ex);
4953              }
5356            return false;
4954          }
5358        public final U getRawResult() { return result.get(); }
4955      }
4956  
4957 <    @SuppressWarnings("serial") static final class SearchEntriesTask<K,V,U>
4957 >    @SuppressWarnings("serial")
4958 >    static final class SearchEntriesTask<K,V,U>
4959          extends BulkTask<K,V,U> {
4960          final Fun<Entry<K,V>, ? extends U> searchFunction;
4961          final AtomicReference<U> result;
4962          SearchEntriesTask
4963 <            (ConcurrentHashMapV8<K,V> m,
5367 <             Fun<Entry<K,V>, ? extends U> searchFunction,
5368 <             AtomicReference<U> result) {
5369 <            super(m);
5370 <            this.searchFunction = searchFunction; this.result = result;
5371 <        }
5372 <        SearchEntriesTask
5373 <            (BulkTask<K,V,?> p, int b,
4963 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
4964               Fun<Entry<K,V>, ? extends U> searchFunction,
4965               AtomicReference<U> result) {
4966 <            super(p, b);
4966 >            super(p, b, i, f, t);
4967              this.searchFunction = searchFunction; this.result = result;
4968          }
4969 <        @SuppressWarnings("unchecked") public final boolean exec() {
4970 <            AtomicReference<U> result = this.result;
4971 <            final Fun<Entry<K,V>, ? extends U> searchFunction =
4972 <                this.searchFunction;
4973 <            if (searchFunction == null || result == null)
4974 <                return abortOnNullFunction();
4975 <            try {
4976 <                int b = batch(), c;
4977 <                while (b > 1 && baseIndex != baseLimit && result.get() == null) {
4978 <                    do {} while (!casPending(c = pending, c+1));
4979 <                    new SearchEntriesTask<K,V,U>(this, b >>>= 1,
4980 <                                                 searchFunction, result).fork();
4981 <                }
4982 <                Object v; U u;
4983 <                while (result.get() == null && (v = advance()) != null) {
4984 <                    if ((u = searchFunction.apply(entryFor((K)nextKey, (V)v))) != null) {
4985 <                        if (result.compareAndSet(null, u))
4986 <                            tryCompleteComputation(null);
4969 >        public final U getRawResult() { return result.get(); }
4970 >        public final void compute() {
4971 >            final Fun<Entry<K,V>, ? extends U> searchFunction;
4972 >            final AtomicReference<U> result;
4973 >            if ((searchFunction = this.searchFunction) != null &&
4974 >                (result = this.result) != null) {
4975 >                for (int i = baseIndex, f, h; batch > 0 &&
4976 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
4977 >                    if (result.get() != null)
4978 >                        return;
4979 >                    addToPendingCount(1);
4980 >                    new SearchEntriesTask<K,V,U>
4981 >                        (this, batch >>>= 1, baseLimit = h, f, tab,
4982 >                         searchFunction, result).fork();
4983 >                }
4984 >                while (result.get() == null) {
4985 >                    U u;
4986 >                    Node<K,V> p;
4987 >                    if ((p = advance()) == null) {
4988 >                        propagateCompletion();
4989                          break;
4990                      }
4991 +                    if ((u = searchFunction.apply(p)) != null) {
4992 +                        if (result.compareAndSet(null, u))
4993 +                            quietlyCompleteRoot();
4994 +                        return;
4995 +                    }
4996                  }
5400                tryComplete();
5401            } catch (Throwable ex) {
5402                return tryCompleteComputation(ex);
4997              }
5404            return false;
4998          }
5406        public final U getRawResult() { return result.get(); }
4999      }
5000  
5001 <    @SuppressWarnings("serial") static final class SearchMappingsTask<K,V,U>
5001 >    @SuppressWarnings("serial")
5002 >    static final class SearchMappingsTask<K,V,U>
5003          extends BulkTask<K,V,U> {
5004          final BiFun<? super K, ? super V, ? extends U> searchFunction;
5005          final AtomicReference<U> result;
5006          SearchMappingsTask
5007 <            (ConcurrentHashMapV8<K,V> m,
5007 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5008               BiFun<? super K, ? super V, ? extends U> searchFunction,
5009               AtomicReference<U> result) {
5010 <            super(m);
5010 >            super(p, b, i, f, t);
5011              this.searchFunction = searchFunction; this.result = result;
5012          }
5013 <        SearchMappingsTask
5014 <            (BulkTask<K,V,?> p, int b,
5015 <             BiFun<? super K, ? super V, ? extends U> searchFunction,
5016 <             AtomicReference<U> result) {
5017 <            super(p, b);
5018 <            this.searchFunction = searchFunction; this.result = result;
5019 <        }
5020 <        @SuppressWarnings("unchecked") public final boolean exec() {
5021 <            AtomicReference<U> result = this.result;
5022 <            final BiFun<? super K, ? super V, ? extends U> searchFunction =
5023 <                this.searchFunction;
5024 <            if (searchFunction == null || result == null)
5025 <                return abortOnNullFunction();
5026 <            try {
5027 <                int b = batch(), c;
5028 <                while (b > 1 && baseIndex != baseLimit && result.get() == null) {
5029 <                    do {} while (!casPending(c = pending, c+1));
5030 <                    new SearchMappingsTask<K,V,U>(this, b >>>= 1,
5031 <                                                  searchFunction, result).fork();
5032 <                }
5033 <                Object v; U u;
5034 <                while (result.get() == null && (v = advance()) != null) {
5035 <                    if ((u = searchFunction.apply((K)nextKey, (V)v)) != null) {
5013 >        public final U getRawResult() { return result.get(); }
5014 >        public final void compute() {
5015 >            final BiFun<? super K, ? super V, ? extends U> searchFunction;
5016 >            final AtomicReference<U> result;
5017 >            if ((searchFunction = this.searchFunction) != null &&
5018 >                (result = this.result) != null) {
5019 >                for (int i = baseIndex, f, h; batch > 0 &&
5020 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5021 >                    if (result.get() != null)
5022 >                        return;
5023 >                    addToPendingCount(1);
5024 >                    new SearchMappingsTask<K,V,U>
5025 >                        (this, batch >>>= 1, baseLimit = h, f, tab,
5026 >                         searchFunction, result).fork();
5027 >                }
5028 >                while (result.get() == null) {
5029 >                    U u;
5030 >                    Node<K,V> p;
5031 >                    if ((p = advance()) == null) {
5032 >                        propagateCompletion();
5033 >                        break;
5034 >                    }
5035 >                    if ((u = searchFunction.apply(p.key, p.val)) != null) {
5036                          if (result.compareAndSet(null, u))
5037 <                            tryCompleteComputation(null);
5037 >                            quietlyCompleteRoot();
5038                          break;
5039                      }
5040                  }
5448                tryComplete();
5449            } catch (Throwable ex) {
5450                return tryCompleteComputation(ex);
5041              }
5452            return false;
5042          }
5454        public final U getRawResult() { return result.get(); }
5043      }
5044  
5045 <    @SuppressWarnings("serial") static final class ReduceKeysTask<K,V>
5045 >    @SuppressWarnings("serial")
5046 >    static final class ReduceKeysTask<K,V>
5047          extends BulkTask<K,V,K> {
5048          final BiFun<? super K, ? super K, ? extends K> reducer;
5049          K result;
5050          ReduceKeysTask<K,V> rights, nextRight;
5051          ReduceKeysTask
5052 <            (ConcurrentHashMapV8<K,V> m,
5464 <             BiFun<? super K, ? super K, ? extends K> reducer) {
5465 <            super(m);
5466 <            this.reducer = reducer;
5467 <        }
5468 <        ReduceKeysTask
5469 <            (BulkTask<K,V,?> p, int b,
5052 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5053               ReduceKeysTask<K,V> nextRight,
5054               BiFun<? super K, ? super K, ? extends K> reducer) {
5055 <            super(p, b); this.nextRight = nextRight;
5055 >            super(p, b, i, f, t); this.nextRight = nextRight;
5056              this.reducer = reducer;
5057          }
5058 <
5059 <        @SuppressWarnings("unchecked") public final boolean exec() {
5060 <            final BiFun<? super K, ? super K, ? extends K> reducer =
5061 <                this.reducer;
5062 <            if (reducer == null)
5063 <                return abortOnNullFunction();
5064 <            try {
5482 <                for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
5483 <                    do {} while (!casPending(c = pending, c+1));
5058 >        public final K getRawResult() { return result; }
5059 >        public final void compute() {
5060 >            final BiFun<? super K, ? super K, ? extends K> reducer;
5061 >            if ((reducer = this.reducer) != null) {
5062 >                for (int i = baseIndex, f, h; batch > 0 &&
5063 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5064 >                    addToPendingCount(1);
5065                      (rights = new ReduceKeysTask<K,V>
5066 <                     (this, b >>>= 1, rights, reducer)).fork();
5066 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
5067 >                      rights, reducer)).fork();
5068                  }
5069                  K r = null;
5070 <                while (advance() != null) {
5071 <                    K u = (K)nextKey;
5072 <                    r = (r == null) ? u : reducer.apply(r, u);
5070 >                for (Node<K,V> p; (p = advance()) != null; ) {
5071 >                    K u = p.key;
5072 >                    r = (r == null) ? u : u == null ? r : reducer.apply(r, u);
5073                  }
5074                  result = r;
5075 <                for (ReduceKeysTask<K,V> t = this, s;;) {
5076 <                    int c; BulkTask<K,V,?> par; K tr, sr;
5077 <                    if ((c = t.pending) == 0) {
5078 <                        for (s = t.rights; s != null; s = t.rights = s.nextRight) {
5079 <                            if ((sr = s.result) != null)
5080 <                                t.result = (tr = t.result) == null? sr : reducer.apply(tr, sr);
5081 <                        }
5082 <                        if ((par = t.parent) == null ||
5083 <                            !(par instanceof ReduceKeysTask)) {
5084 <                            t.quietlyComplete();
5085 <                            break;
5504 <                        }
5505 <                        t = (ReduceKeysTask<K,V>)par;
5075 >                CountedCompleter<?> c;
5076 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5077 >                    @SuppressWarnings("unchecked") ReduceKeysTask<K,V>
5078 >                        t = (ReduceKeysTask<K,V>)c,
5079 >                        s = t.rights;
5080 >                    while (s != null) {
5081 >                        K tr, sr;
5082 >                        if ((sr = s.result) != null)
5083 >                            t.result = (((tr = t.result) == null) ? sr :
5084 >                                        reducer.apply(tr, sr));
5085 >                        s = t.rights = s.nextRight;
5086                      }
5507                    else if (t.casPending(c, c - 1))
5508                        break;
5087                  }
5510            } catch (Throwable ex) {
5511                return tryCompleteComputation(ex);
5088              }
5513            return false;
5089          }
5515        public final K getRawResult() { return result; }
5090      }
5091  
5092 <    @SuppressWarnings("serial") static final class ReduceValuesTask<K,V>
5092 >    @SuppressWarnings("serial")
5093 >    static final class ReduceValuesTask<K,V>
5094          extends BulkTask<K,V,V> {
5095          final BiFun<? super V, ? super V, ? extends V> reducer;
5096          V result;
5097          ReduceValuesTask<K,V> rights, nextRight;
5098          ReduceValuesTask
5099 <            (ConcurrentHashMapV8<K,V> m,
5525 <             BiFun<? super V, ? super V, ? extends V> reducer) {
5526 <            super(m);
5527 <            this.reducer = reducer;
5528 <        }
5529 <        ReduceValuesTask
5530 <            (BulkTask<K,V,?> p, int b,
5099 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5100               ReduceValuesTask<K,V> nextRight,
5101               BiFun<? super V, ? super V, ? extends V> reducer) {
5102 <            super(p, b); this.nextRight = nextRight;
5102 >            super(p, b, i, f, t); this.nextRight = nextRight;
5103              this.reducer = reducer;
5104          }
5105 <
5106 <        @SuppressWarnings("unchecked") public final boolean exec() {
5107 <            final BiFun<? super V, ? super V, ? extends V> reducer =
5108 <                this.reducer;
5109 <            if (reducer == null)
5110 <                return abortOnNullFunction();
5111 <            try {
5543 <                for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
5544 <                    do {} while (!casPending(c = pending, c+1));
5105 >        public final V getRawResult() { return result; }
5106 >        public final void compute() {
5107 >            final BiFun<? super V, ? super V, ? extends V> reducer;
5108 >            if ((reducer = this.reducer) != null) {
5109 >                for (int i = baseIndex, f, h; batch > 0 &&
5110 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5111 >                    addToPendingCount(1);
5112                      (rights = new ReduceValuesTask<K,V>
5113 <                     (this, b >>>= 1, rights, reducer)).fork();
5113 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
5114 >                      rights, reducer)).fork();
5115                  }
5116                  V r = null;
5117 <                Object v;
5118 <                while ((v = advance()) != null) {
5119 <                    V u = (V)v;
5552 <                    r = (r == null) ? u : reducer.apply(r, u);
5117 >                for (Node<K,V> p; (p = advance()) != null; ) {
5118 >                    V v = p.val;
5119 >                    r = (r == null) ? v : reducer.apply(r, v);
5120                  }
5121                  result = r;
5122 <                for (ReduceValuesTask<K,V> t = this, s;;) {
5123 <                    int c; BulkTask<K,V,?> par; V tr, sr;
5124 <                    if ((c = t.pending) == 0) {
5125 <                        for (s = t.rights; s != null; s = t.rights = s.nextRight) {
5126 <                            if ((sr = s.result) != null)
5127 <                                t.result = (tr = t.result) == null? sr : reducer.apply(tr, sr);
5128 <                        }
5129 <                        if ((par = t.parent) == null ||
5130 <                            !(par instanceof ReduceValuesTask)) {
5131 <                            t.quietlyComplete();
5132 <                            break;
5566 <                        }
5567 <                        t = (ReduceValuesTask<K,V>)par;
5122 >                CountedCompleter<?> c;
5123 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5124 >                    @SuppressWarnings("unchecked") ReduceValuesTask<K,V>
5125 >                        t = (ReduceValuesTask<K,V>)c,
5126 >                        s = t.rights;
5127 >                    while (s != null) {
5128 >                        V tr, sr;
5129 >                        if ((sr = s.result) != null)
5130 >                            t.result = (((tr = t.result) == null) ? sr :
5131 >                                        reducer.apply(tr, sr));
5132 >                        s = t.rights = s.nextRight;
5133                      }
5569                    else if (t.casPending(c, c - 1))
5570                        break;
5134                  }
5572            } catch (Throwable ex) {
5573                return tryCompleteComputation(ex);
5135              }
5575            return false;
5136          }
5577        public final V getRawResult() { return result; }
5137      }
5138  
5139 <    @SuppressWarnings("serial") static final class ReduceEntriesTask<K,V>
5139 >    @SuppressWarnings("serial")
5140 >    static final class ReduceEntriesTask<K,V>
5141          extends BulkTask<K,V,Map.Entry<K,V>> {
5142          final BiFun<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer;
5143          Map.Entry<K,V> result;
5144          ReduceEntriesTask<K,V> rights, nextRight;
5145          ReduceEntriesTask
5146 <            (ConcurrentHashMapV8<K,V> m,
5587 <             BiFun<Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer) {
5588 <            super(m);
5589 <            this.reducer = reducer;
5590 <        }
5591 <        ReduceEntriesTask
5592 <            (BulkTask<K,V,?> p, int b,
5146 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5147               ReduceEntriesTask<K,V> nextRight,
5148 <             BiFun<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer) {
5149 <            super(p, b); this.nextRight = nextRight;
5148 >             BiFun<Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer) {
5149 >            super(p, b, i, f, t); this.nextRight = nextRight;
5150              this.reducer = reducer;
5151          }
5152 <
5153 <        @SuppressWarnings("unchecked") public final boolean exec() {
5154 <            final BiFun<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer =
5155 <                this.reducer;
5156 <            if (reducer == null)
5157 <                return abortOnNullFunction();
5158 <            try {
5605 <                for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
5606 <                    do {} while (!casPending(c = pending, c+1));
5152 >        public final Map.Entry<K,V> getRawResult() { return result; }
5153 >        public final void compute() {
5154 >            final BiFun<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer;
5155 >            if ((reducer = this.reducer) != null) {
5156 >                for (int i = baseIndex, f, h; batch > 0 &&
5157 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5158 >                    addToPendingCount(1);
5159                      (rights = new ReduceEntriesTask<K,V>
5160 <                     (this, b >>>= 1, rights, reducer)).fork();
5160 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
5161 >                      rights, reducer)).fork();
5162                  }
5163                  Map.Entry<K,V> r = null;
5164 <                Object v;
5165 <                while ((v = advance()) != null) {
5613 <                    Map.Entry<K,V> u = entryFor((K)nextKey, (V)v);
5614 <                    r = (r == null) ? u : reducer.apply(r, u);
5615 <                }
5164 >                for (Node<K,V> p; (p = advance()) != null; )
5165 >                    r = (r == null) ? p : reducer.apply(r, p);
5166                  result = r;
5167 <                for (ReduceEntriesTask<K,V> t = this, s;;) {
5168 <                    int c; BulkTask<K,V,?> par; Map.Entry<K,V> tr, sr;
5169 <                    if ((c = t.pending) == 0) {
5170 <                        for (s = t.rights; s != null; s = t.rights = s.nextRight) {
5171 <                            if ((sr = s.result) != null)
5172 <                                t.result = (tr = t.result) == null? sr : reducer.apply(tr, sr);
5173 <                        }
5174 <                        if ((par = t.parent) == null ||
5175 <                            !(par instanceof ReduceEntriesTask)) {
5176 <                            t.quietlyComplete();
5177 <                            break;
5628 <                        }
5629 <                        t = (ReduceEntriesTask<K,V>)par;
5167 >                CountedCompleter<?> c;
5168 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5169 >                    @SuppressWarnings("unchecked") ReduceEntriesTask<K,V>
5170 >                        t = (ReduceEntriesTask<K,V>)c,
5171 >                        s = t.rights;
5172 >                    while (s != null) {
5173 >                        Map.Entry<K,V> tr, sr;
5174 >                        if ((sr = s.result) != null)
5175 >                            t.result = (((tr = t.result) == null) ? sr :
5176 >                                        reducer.apply(tr, sr));
5177 >                        s = t.rights = s.nextRight;
5178                      }
5631                    else if (t.casPending(c, c - 1))
5632                        break;
5179                  }
5634            } catch (Throwable ex) {
5635                return tryCompleteComputation(ex);
5180              }
5637            return false;
5181          }
5639        public final Map.Entry<K,V> getRawResult() { return result; }
5182      }
5183  
5184 <    @SuppressWarnings("serial") static final class MapReduceKeysTask<K,V,U>
5184 >    @SuppressWarnings("serial")
5185 >    static final class MapReduceKeysTask<K,V,U>
5186          extends BulkTask<K,V,U> {
5187          final Fun<? super K, ? extends U> transformer;
5188          final BiFun<? super U, ? super U, ? extends U> reducer;
5189          U result;
5190          MapReduceKeysTask<K,V,U> rights, nextRight;
5191          MapReduceKeysTask
5192 <            (ConcurrentHashMapV8<K,V> m,
5650 <             Fun<? super K, ? extends U> transformer,
5651 <             BiFun<? super U, ? super U, ? extends U> reducer) {
5652 <            super(m);
5653 <            this.transformer = transformer;
5654 <            this.reducer = reducer;
5655 <        }
5656 <        MapReduceKeysTask
5657 <            (BulkTask<K,V,?> p, int b,
5192 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5193               MapReduceKeysTask<K,V,U> nextRight,
5194               Fun<? super K, ? extends U> transformer,
5195               BiFun<? super U, ? super U, ? extends U> reducer) {
5196 <            super(p, b); this.nextRight = nextRight;
5196 >            super(p, b, i, f, t); this.nextRight = nextRight;
5197              this.transformer = transformer;
5198              this.reducer = reducer;
5199          }
5200 <        @SuppressWarnings("unchecked") public final boolean exec() {
5201 <            final Fun<? super K, ? extends U> transformer =
5202 <                this.transformer;
5203 <            final BiFun<? super U, ? super U, ? extends U> reducer =
5204 <                this.reducer;
5205 <            if (transformer == null || reducer == null)
5206 <                return abortOnNullFunction();
5207 <            try {
5208 <                for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
5674 <                    do {} while (!casPending(c = pending, c+1));
5200 >        public final U getRawResult() { return result; }
5201 >        public final void compute() {
5202 >            final Fun<? super K, ? extends U> transformer;
5203 >            final BiFun<? super U, ? super U, ? extends U> reducer;
5204 >            if ((transformer = this.transformer) != null &&
5205 >                (reducer = this.reducer) != null) {
5206 >                for (int i = baseIndex, f, h; batch > 0 &&
5207 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5208 >                    addToPendingCount(1);
5209                      (rights = new MapReduceKeysTask<K,V,U>
5210 <                     (this, b >>>= 1, rights, transformer, reducer)).fork();
5210 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
5211 >                      rights, transformer, reducer)).fork();
5212                  }
5213 <                U r = null, u;
5214 <                while (advance() != null) {
5215 <                    if ((u = transformer.apply((K)nextKey)) != null)
5213 >                U r = null;
5214 >                for (Node<K,V> p; (p = advance()) != null; ) {
5215 >                    U u;
5216 >                    if ((u = transformer.apply(p.key)) != null)
5217                          r = (r == null) ? u : reducer.apply(r, u);
5218                  }
5219                  result = r;
5220 <                for (MapReduceKeysTask<K,V,U> t = this, s;;) {
5221 <                    int c; BulkTask<K,V,?> par; U tr, sr;
5222 <                    if ((c = t.pending) == 0) {
5223 <                        for (s = t.rights; s != null; s = t.rights = s.nextRight) {
5224 <                            if ((sr = s.result) != null)
5225 <                                t.result = (tr = t.result) == null? sr : reducer.apply(tr, sr);
5226 <                        }
5227 <                        if ((par = t.parent) == null ||
5228 <                            !(par instanceof MapReduceKeysTask)) {
5229 <                            t.quietlyComplete();
5230 <                            break;
5695 <                        }
5696 <                        t = (MapReduceKeysTask<K,V,U>)par;
5220 >                CountedCompleter<?> c;
5221 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5222 >                    @SuppressWarnings("unchecked") MapReduceKeysTask<K,V,U>
5223 >                        t = (MapReduceKeysTask<K,V,U>)c,
5224 >                        s = t.rights;
5225 >                    while (s != null) {
5226 >                        U tr, sr;
5227 >                        if ((sr = s.result) != null)
5228 >                            t.result = (((tr = t.result) == null) ? sr :
5229 >                                        reducer.apply(tr, sr));
5230 >                        s = t.rights = s.nextRight;
5231                      }
5698                    else if (t.casPending(c, c - 1))
5699                        break;
5232                  }
5701            } catch (Throwable ex) {
5702                return tryCompleteComputation(ex);
5233              }
5704            return false;
5234          }
5706        public final U getRawResult() { return result; }
5235      }
5236  
5237 <    @SuppressWarnings("serial") static final class MapReduceValuesTask<K,V,U>
5237 >    @SuppressWarnings("serial")
5238 >    static final class MapReduceValuesTask<K,V,U>
5239          extends BulkTask<K,V,U> {
5240          final Fun<? super V, ? extends U> transformer;
5241          final BiFun<? super U, ? super U, ? extends U> reducer;
5242          U result;
5243          MapReduceValuesTask<K,V,U> rights, nextRight;
5244          MapReduceValuesTask
5245 <            (ConcurrentHashMapV8<K,V> m,
5717 <             Fun<? super V, ? extends U> transformer,
5718 <             BiFun<? super U, ? super U, ? extends U> reducer) {
5719 <            super(m);
5720 <            this.transformer = transformer;
5721 <            this.reducer = reducer;
5722 <        }
5723 <        MapReduceValuesTask
5724 <            (BulkTask<K,V,?> p, int b,
5245 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5246               MapReduceValuesTask<K,V,U> nextRight,
5247               Fun<? super V, ? extends U> transformer,
5248               BiFun<? super U, ? super U, ? extends U> reducer) {
5249 <            super(p, b); this.nextRight = nextRight;
5249 >            super(p, b, i, f, t); this.nextRight = nextRight;
5250              this.transformer = transformer;
5251              this.reducer = reducer;
5252          }
5253 <        @SuppressWarnings("unchecked") public final boolean exec() {
5254 <            final Fun<? super V, ? extends U> transformer =
5255 <                this.transformer;
5256 <            final BiFun<? super U, ? super U, ? extends U> reducer =
5257 <                this.reducer;
5258 <            if (transformer == null || reducer == null)
5259 <                return abortOnNullFunction();
5260 <            try {
5261 <                for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
5741 <                    do {} while (!casPending(c = pending, c+1));
5253 >        public final U getRawResult() { return result; }
5254 >        public final void compute() {
5255 >            final Fun<? super V, ? extends U> transformer;
5256 >            final BiFun<? super U, ? super U, ? extends U> reducer;
5257 >            if ((transformer = this.transformer) != null &&
5258 >                (reducer = this.reducer) != null) {
5259 >                for (int i = baseIndex, f, h; batch > 0 &&
5260 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5261 >                    addToPendingCount(1);
5262                      (rights = new MapReduceValuesTask<K,V,U>
5263 <                     (this, b >>>= 1, rights, transformer, reducer)).fork();
5263 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
5264 >                      rights, transformer, reducer)).fork();
5265                  }
5266 <                U r = null, u;
5267 <                Object v;
5268 <                while ((v = advance()) != null) {
5269 <                    if ((u = transformer.apply((V)v)) != null)
5266 >                U r = null;
5267 >                for (Node<K,V> p; (p = advance()) != null; ) {
5268 >                    U u;
5269 >                    if ((u = transformer.apply(p.val)) != null)
5270                          r = (r == null) ? u : reducer.apply(r, u);
5271                  }
5272                  result = r;
5273 <                for (MapReduceValuesTask<K,V,U> t = this, s;;) {
5274 <                    int c; BulkTask<K,V,?> par; U tr, sr;
5275 <                    if ((c = t.pending) == 0) {
5276 <                        for (s = t.rights; s != null; s = t.rights = s.nextRight) {
5277 <                            if ((sr = s.result) != null)
5278 <                                t.result = (tr = t.result) == null? sr : reducer.apply(tr, sr);
5279 <                        }
5280 <                        if ((par = t.parent) == null ||
5281 <                            !(par instanceof MapReduceValuesTask)) {
5282 <                            t.quietlyComplete();
5283 <                            break;
5763 <                        }
5764 <                        t = (MapReduceValuesTask<K,V,U>)par;
5273 >                CountedCompleter<?> c;
5274 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5275 >                    @SuppressWarnings("unchecked") MapReduceValuesTask<K,V,U>
5276 >                        t = (MapReduceValuesTask<K,V,U>)c,
5277 >                        s = t.rights;
5278 >                    while (s != null) {
5279 >                        U tr, sr;
5280 >                        if ((sr = s.result) != null)
5281 >                            t.result = (((tr = t.result) == null) ? sr :
5282 >                                        reducer.apply(tr, sr));
5283 >                        s = t.rights = s.nextRight;
5284                      }
5766                    else if (t.casPending(c, c - 1))
5767                        break;
5285                  }
5769            } catch (Throwable ex) {
5770                return tryCompleteComputation(ex);
5286              }
5772            return false;
5287          }
5774        public final U getRawResult() { return result; }
5288      }
5289  
5290 <    @SuppressWarnings("serial") static final class MapReduceEntriesTask<K,V,U>
5290 >    @SuppressWarnings("serial")
5291 >    static final class MapReduceEntriesTask<K,V,U>
5292          extends BulkTask<K,V,U> {
5293          final Fun<Map.Entry<K,V>, ? extends U> transformer;
5294          final BiFun<? super U, ? super U, ? extends U> reducer;
5295          U result;
5296          MapReduceEntriesTask<K,V,U> rights, nextRight;
5297          MapReduceEntriesTask
5298 <            (ConcurrentHashMapV8<K,V> m,
5785 <             Fun<Map.Entry<K,V>, ? extends U> transformer,
5786 <             BiFun<? super U, ? super U, ? extends U> reducer) {
5787 <            super(m);
5788 <            this.transformer = transformer;
5789 <            this.reducer = reducer;
5790 <        }
5791 <        MapReduceEntriesTask
5792 <            (BulkTask<K,V,?> p, int b,
5298 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5299               MapReduceEntriesTask<K,V,U> nextRight,
5300               Fun<Map.Entry<K,V>, ? extends U> transformer,
5301               BiFun<? super U, ? super U, ? extends U> reducer) {
5302 <            super(p, b); this.nextRight = nextRight;
5302 >            super(p, b, i, f, t); this.nextRight = nextRight;
5303              this.transformer = transformer;
5304              this.reducer = reducer;
5305          }
5306 <        @SuppressWarnings("unchecked") public final boolean exec() {
5307 <            final Fun<Map.Entry<K,V>, ? extends U> transformer =
5308 <                this.transformer;
5309 <            final BiFun<? super U, ? super U, ? extends U> reducer =
5310 <                this.reducer;
5311 <            if (transformer == null || reducer == null)
5312 <                return abortOnNullFunction();
5313 <            try {
5314 <                for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
5809 <                    do {} while (!casPending(c = pending, c+1));
5306 >        public final U getRawResult() { return result; }
5307 >        public final void compute() {
5308 >            final Fun<Map.Entry<K,V>, ? extends U> transformer;
5309 >            final BiFun<? super U, ? super U, ? extends U> reducer;
5310 >            if ((transformer = this.transformer) != null &&
5311 >                (reducer = this.reducer) != null) {
5312 >                for (int i = baseIndex, f, h; batch > 0 &&
5313 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5314 >                    addToPendingCount(1);
5315                      (rights = new MapReduceEntriesTask<K,V,U>
5316 <                     (this, b >>>= 1, rights, transformer, reducer)).fork();
5316 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
5317 >                      rights, transformer, reducer)).fork();
5318                  }
5319 <                U r = null, u;
5320 <                Object v;
5321 <                while ((v = advance()) != null) {
5322 <                    if ((u = transformer.apply(entryFor((K)nextKey, (V)v))) != null)
5319 >                U r = null;
5320 >                for (Node<K,V> p; (p = advance()) != null; ) {
5321 >                    U u;
5322 >                    if ((u = transformer.apply(p)) != null)
5323                          r = (r == null) ? u : reducer.apply(r, u);
5324                  }
5325                  result = r;
5326 <                for (MapReduceEntriesTask<K,V,U> t = this, s;;) {
5327 <                    int c; BulkTask<K,V,?> par; U tr, sr;
5328 <                    if ((c = t.pending) == 0) {
5329 <                        for (s = t.rights; s != null; s = t.rights = s.nextRight) {
5330 <                            if ((sr = s.result) != null)
5331 <                                t.result = (tr = t.result) == null? sr : reducer.apply(tr, sr);
5332 <                        }
5333 <                        if ((par = t.parent) == null ||
5334 <                            !(par instanceof MapReduceEntriesTask)) {
5335 <                            t.quietlyComplete();
5336 <                            break;
5831 <                        }
5832 <                        t = (MapReduceEntriesTask<K,V,U>)par;
5326 >                CountedCompleter<?> c;
5327 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5328 >                    @SuppressWarnings("unchecked") MapReduceEntriesTask<K,V,U>
5329 >                        t = (MapReduceEntriesTask<K,V,U>)c,
5330 >                        s = t.rights;
5331 >                    while (s != null) {
5332 >                        U tr, sr;
5333 >                        if ((sr = s.result) != null)
5334 >                            t.result = (((tr = t.result) == null) ? sr :
5335 >                                        reducer.apply(tr, sr));
5336 >                        s = t.rights = s.nextRight;
5337                      }
5834                    else if (t.casPending(c, c - 1))
5835                        break;
5338                  }
5837            } catch (Throwable ex) {
5838                return tryCompleteComputation(ex);
5339              }
5840            return false;
5340          }
5842        public final U getRawResult() { return result; }
5341      }
5342  
5343 <    @SuppressWarnings("serial") static final class MapReduceMappingsTask<K,V,U>
5343 >    @SuppressWarnings("serial")
5344 >    static final class MapReduceMappingsTask<K,V,U>
5345          extends BulkTask<K,V,U> {
5346          final BiFun<? super K, ? super V, ? extends U> transformer;
5347          final BiFun<? super U, ? super U, ? extends U> reducer;
5348          U result;
5349          MapReduceMappingsTask<K,V,U> rights, nextRight;
5350          MapReduceMappingsTask
5351 <            (ConcurrentHashMapV8<K,V> m,
5853 <             BiFun<? super K, ? super V, ? extends U> transformer,
5854 <             BiFun<? super U, ? super U, ? extends U> reducer) {
5855 <            super(m);
5856 <            this.transformer = transformer;
5857 <            this.reducer = reducer;
5858 <        }
5859 <        MapReduceMappingsTask
5860 <            (BulkTask<K,V,?> p, int b,
5351 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5352               MapReduceMappingsTask<K,V,U> nextRight,
5353               BiFun<? super K, ? super V, ? extends U> transformer,
5354               BiFun<? super U, ? super U, ? extends U> reducer) {
5355 <            super(p, b); this.nextRight = nextRight;
5355 >            super(p, b, i, f, t); this.nextRight = nextRight;
5356              this.transformer = transformer;
5357              this.reducer = reducer;
5358          }
5359 <        @SuppressWarnings("unchecked") public final boolean exec() {
5360 <            final BiFun<? super K, ? super V, ? extends U> transformer =
5361 <                this.transformer;
5362 <            final BiFun<? super U, ? super U, ? extends U> reducer =
5363 <                this.reducer;
5364 <            if (transformer == null || reducer == null)
5365 <                return abortOnNullFunction();
5366 <            try {
5367 <                for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
5877 <                    do {} while (!casPending(c = pending, c+1));
5359 >        public final U getRawResult() { return result; }
5360 >        public final void compute() {
5361 >            final BiFun<? super K, ? super V, ? extends U> transformer;
5362 >            final BiFun<? super U, ? super U, ? extends U> reducer;
5363 >            if ((transformer = this.transformer) != null &&
5364 >                (reducer = this.reducer) != null) {
5365 >                for (int i = baseIndex, f, h; batch > 0 &&
5366 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5367 >                    addToPendingCount(1);
5368                      (rights = new MapReduceMappingsTask<K,V,U>
5369 <                     (this, b >>>= 1, rights, transformer, reducer)).fork();
5369 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
5370 >                      rights, transformer, reducer)).fork();
5371                  }
5372 <                U r = null, u;
5373 <                Object v;
5374 <                while ((v = advance()) != null) {
5375 <                    if ((u = transformer.apply((K)nextKey, (V)v)) != null)
5372 >                U r = null;
5373 >                for (Node<K,V> p; (p = advance()) != null; ) {
5374 >                    U u;
5375 >                    if ((u = transformer.apply(p.key, p.val)) != null)
5376                          r = (r == null) ? u : reducer.apply(r, u);
5377                  }
5378                  result = r;
5379 <                for (MapReduceMappingsTask<K,V,U> t = this, s;;) {
5380 <                    int c; BulkTask<K,V,?> par; U tr, sr;
5381 <                    if ((c = t.pending) == 0) {
5382 <                        for (s = t.rights; s != null; s = t.rights = s.nextRight) {
5383 <                            if ((sr = s.result) != null)
5384 <                                t.result = (tr = t.result) == null? sr : reducer.apply(tr, sr);
5385 <                        }
5386 <                        if ((par = t.parent) == null ||
5387 <                            !(par instanceof MapReduceMappingsTask)) {
5388 <                            t.quietlyComplete();
5389 <                            break;
5899 <                        }
5900 <                        t = (MapReduceMappingsTask<K,V,U>)par;
5379 >                CountedCompleter<?> c;
5380 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5381 >                    @SuppressWarnings("unchecked") MapReduceMappingsTask<K,V,U>
5382 >                        t = (MapReduceMappingsTask<K,V,U>)c,
5383 >                        s = t.rights;
5384 >                    while (s != null) {
5385 >                        U tr, sr;
5386 >                        if ((sr = s.result) != null)
5387 >                            t.result = (((tr = t.result) == null) ? sr :
5388 >                                        reducer.apply(tr, sr));
5389 >                        s = t.rights = s.nextRight;
5390                      }
5902                    else if (t.casPending(c, c - 1))
5903                        break;
5391                  }
5905            } catch (Throwable ex) {
5906                return tryCompleteComputation(ex);
5392              }
5908            return false;
5393          }
5910        public final U getRawResult() { return result; }
5394      }
5395  
5396 <    @SuppressWarnings("serial") static final class MapReduceKeysToDoubleTask<K,V>
5396 >    @SuppressWarnings("serial")
5397 >    static final class MapReduceKeysToDoubleTask<K,V>
5398          extends BulkTask<K,V,Double> {
5399          final ObjectToDouble<? super K> transformer;
5400          final DoubleByDoubleToDouble reducer;
# Line 5918 | Line 5402 | public class ConcurrentHashMapV8<K, V>
5402          double result;
5403          MapReduceKeysToDoubleTask<K,V> rights, nextRight;
5404          MapReduceKeysToDoubleTask
5405 <            (ConcurrentHashMapV8<K,V> m,
5922 <             ObjectToDouble<? super K> transformer,
5923 <             double basis,
5924 <             DoubleByDoubleToDouble reducer) {
5925 <            super(m);
5926 <            this.transformer = transformer;
5927 <            this.basis = basis; this.reducer = reducer;
5928 <        }
5929 <        MapReduceKeysToDoubleTask
5930 <            (BulkTask<K,V,?> p, int b,
5405 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5406               MapReduceKeysToDoubleTask<K,V> nextRight,
5407               ObjectToDouble<? super K> transformer,
5408               double basis,
5409               DoubleByDoubleToDouble reducer) {
5410 <            super(p, b); this.nextRight = nextRight;
5410 >            super(p, b, i, f, t); this.nextRight = nextRight;
5411              this.transformer = transformer;
5412              this.basis = basis; this.reducer = reducer;
5413          }
5414 <        @SuppressWarnings("unchecked") public final boolean exec() {
5415 <            final ObjectToDouble<? super K> transformer =
5416 <                this.transformer;
5417 <            final DoubleByDoubleToDouble reducer = this.reducer;
5418 <            if (transformer == null || reducer == null)
5419 <                return abortOnNullFunction();
5420 <            try {
5421 <                final double id = this.basis;
5422 <                for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
5423 <                    do {} while (!casPending(c = pending, c+1));
5414 >        public final Double getRawResult() { return result; }
5415 >        public final void compute() {
5416 >            final ObjectToDouble<? super K> transformer;
5417 >            final DoubleByDoubleToDouble reducer;
5418 >            if ((transformer = this.transformer) != null &&
5419 >                (reducer = this.reducer) != null) {
5420 >                double r = this.basis;
5421 >                for (int i = baseIndex, f, h; batch > 0 &&
5422 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5423 >                    addToPendingCount(1);
5424                      (rights = new MapReduceKeysToDoubleTask<K,V>
5425 <                     (this, b >>>= 1, rights, transformer, id, reducer)).fork();
5425 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
5426 >                      rights, transformer, r, reducer)).fork();
5427                  }
5428 <                double r = id;
5429 <                while (advance() != null)
5954 <                    r = reducer.apply(r, transformer.apply((K)nextKey));
5428 >                for (Node<K,V> p; (p = advance()) != null; )
5429 >                    r = reducer.apply(r, transformer.apply(p.key));
5430                  result = r;
5431 <                for (MapReduceKeysToDoubleTask<K,V> t = this, s;;) {
5432 <                    int c; BulkTask<K,V,?> par;
5433 <                    if ((c = t.pending) == 0) {
5434 <                        for (s = t.rights; s != null; s = t.rights = s.nextRight) {
5435 <                            t.result = reducer.apply(t.result, s.result);
5436 <                        }
5437 <                        if ((par = t.parent) == null ||
5438 <                            !(par instanceof MapReduceKeysToDoubleTask)) {
5964 <                            t.quietlyComplete();
5965 <                            break;
5966 <                        }
5967 <                        t = (MapReduceKeysToDoubleTask<K,V>)par;
5431 >                CountedCompleter<?> c;
5432 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5433 >                    @SuppressWarnings("unchecked") MapReduceKeysToDoubleTask<K,V>
5434 >                        t = (MapReduceKeysToDoubleTask<K,V>)c,
5435 >                        s = t.rights;
5436 >                    while (s != null) {
5437 >                        t.result = reducer.apply(t.result, s.result);
5438 >                        s = t.rights = s.nextRight;
5439                      }
5969                    else if (t.casPending(c, c - 1))
5970                        break;
5440                  }
5972            } catch (Throwable ex) {
5973                return tryCompleteComputation(ex);
5441              }
5975            return false;
5442          }
5977        public final Double getRawResult() { return result; }
5443      }
5444  
5445 <    @SuppressWarnings("serial") static final class MapReduceValuesToDoubleTask<K,V>
5445 >    @SuppressWarnings("serial")
5446 >    static final class MapReduceValuesToDoubleTask<K,V>
5447          extends BulkTask<K,V,Double> {
5448          final ObjectToDouble<? super V> transformer;
5449          final DoubleByDoubleToDouble reducer;
# Line 5985 | Line 5451 | public class ConcurrentHashMapV8<K, V>
5451          double result;
5452          MapReduceValuesToDoubleTask<K,V> rights, nextRight;
5453          MapReduceValuesToDoubleTask
5454 <            (ConcurrentHashMapV8<K,V> m,
5989 <             ObjectToDouble<? super V> transformer,
5990 <             double basis,
5991 <             DoubleByDoubleToDouble reducer) {
5992 <            super(m);
5993 <            this.transformer = transformer;
5994 <            this.basis = basis; this.reducer = reducer;
5995 <        }
5996 <        MapReduceValuesToDoubleTask
5997 <            (BulkTask<K,V,?> p, int b,
5454 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5455               MapReduceValuesToDoubleTask<K,V> nextRight,
5456               ObjectToDouble<? super V> transformer,
5457               double basis,
5458               DoubleByDoubleToDouble reducer) {
5459 <            super(p, b); this.nextRight = nextRight;
5459 >            super(p, b, i, f, t); this.nextRight = nextRight;
5460              this.transformer = transformer;
5461              this.basis = basis; this.reducer = reducer;
5462          }
5463 <        @SuppressWarnings("unchecked") public final boolean exec() {
5464 <            final ObjectToDouble<? super V> transformer =
5465 <                this.transformer;
5466 <            final DoubleByDoubleToDouble reducer = this.reducer;
5467 <            if (transformer == null || reducer == null)
5468 <                return abortOnNullFunction();
5469 <            try {
5470 <                final double id = this.basis;
5471 <                for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
5472 <                    do {} while (!casPending(c = pending, c+1));
5463 >        public final Double getRawResult() { return result; }
5464 >        public final void compute() {
5465 >            final ObjectToDouble<? super V> transformer;
5466 >            final DoubleByDoubleToDouble reducer;
5467 >            if ((transformer = this.transformer) != null &&
5468 >                (reducer = this.reducer) != null) {
5469 >                double r = this.basis;
5470 >                for (int i = baseIndex, f, h; batch > 0 &&
5471 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5472 >                    addToPendingCount(1);
5473                      (rights = new MapReduceValuesToDoubleTask<K,V>
5474 <                     (this, b >>>= 1, rights, transformer, id, reducer)).fork();
5474 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
5475 >                      rights, transformer, r, reducer)).fork();
5476                  }
5477 <                double r = id;
5478 <                Object v;
6021 <                while ((v = advance()) != null)
6022 <                    r = reducer.apply(r, transformer.apply((V)v));
5477 >                for (Node<K,V> p; (p = advance()) != null; )
5478 >                    r = reducer.apply(r, transformer.apply(p.val));
5479                  result = r;
5480 <                for (MapReduceValuesToDoubleTask<K,V> t = this, s;;) {
5481 <                    int c; BulkTask<K,V,?> par;
5482 <                    if ((c = t.pending) == 0) {
5483 <                        for (s = t.rights; s != null; s = t.rights = s.nextRight) {
5484 <                            t.result = reducer.apply(t.result, s.result);
5485 <                        }
5486 <                        if ((par = t.parent) == null ||
5487 <                            !(par instanceof MapReduceValuesToDoubleTask)) {
6032 <                            t.quietlyComplete();
6033 <                            break;
6034 <                        }
6035 <                        t = (MapReduceValuesToDoubleTask<K,V>)par;
5480 >                CountedCompleter<?> c;
5481 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5482 >                    @SuppressWarnings("unchecked") MapReduceValuesToDoubleTask<K,V>
5483 >                        t = (MapReduceValuesToDoubleTask<K,V>)c,
5484 >                        s = t.rights;
5485 >                    while (s != null) {
5486 >                        t.result = reducer.apply(t.result, s.result);
5487 >                        s = t.rights = s.nextRight;
5488                      }
6037                    else if (t.casPending(c, c - 1))
6038                        break;
5489                  }
6040            } catch (Throwable ex) {
6041                return tryCompleteComputation(ex);
5490              }
6043            return false;
5491          }
6045        public final Double getRawResult() { return result; }
5492      }
5493  
5494 <    @SuppressWarnings("serial") static final class MapReduceEntriesToDoubleTask<K,V>
5494 >    @SuppressWarnings("serial")
5495 >    static final class MapReduceEntriesToDoubleTask<K,V>
5496          extends BulkTask<K,V,Double> {
5497          final ObjectToDouble<Map.Entry<K,V>> transformer;
5498          final DoubleByDoubleToDouble reducer;
# Line 6053 | Line 5500 | public class ConcurrentHashMapV8<K, V>
5500          double result;
5501          MapReduceEntriesToDoubleTask<K,V> rights, nextRight;
5502          MapReduceEntriesToDoubleTask
5503 <            (ConcurrentHashMapV8<K,V> m,
6057 <             ObjectToDouble<Map.Entry<K,V>> transformer,
6058 <             double basis,
6059 <             DoubleByDoubleToDouble reducer) {
6060 <            super(m);
6061 <            this.transformer = transformer;
6062 <            this.basis = basis; this.reducer = reducer;
6063 <        }
6064 <        MapReduceEntriesToDoubleTask
6065 <            (BulkTask<K,V,?> p, int b,
5503 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5504               MapReduceEntriesToDoubleTask<K,V> nextRight,
5505               ObjectToDouble<Map.Entry<K,V>> transformer,
5506               double basis,
5507               DoubleByDoubleToDouble reducer) {
5508 <            super(p, b); this.nextRight = nextRight;
5508 >            super(p, b, i, f, t); this.nextRight = nextRight;
5509              this.transformer = transformer;
5510              this.basis = basis; this.reducer = reducer;
5511          }
5512 <        @SuppressWarnings("unchecked") public final boolean exec() {
5513 <            final ObjectToDouble<Map.Entry<K,V>> transformer =
5514 <                this.transformer;
5515 <            final DoubleByDoubleToDouble reducer = this.reducer;
5516 <            if (transformer == null || reducer == null)
5517 <                return abortOnNullFunction();
5518 <            try {
5519 <                final double id = this.basis;
5520 <                for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
5521 <                    do {} while (!casPending(c = pending, c+1));
5512 >        public final Double getRawResult() { return result; }
5513 >        public final void compute() {
5514 >            final ObjectToDouble<Map.Entry<K,V>> transformer;
5515 >            final DoubleByDoubleToDouble reducer;
5516 >            if ((transformer = this.transformer) != null &&
5517 >                (reducer = this.reducer) != null) {
5518 >                double r = this.basis;
5519 >                for (int i = baseIndex, f, h; batch > 0 &&
5520 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5521 >                    addToPendingCount(1);
5522                      (rights = new MapReduceEntriesToDoubleTask<K,V>
5523 <                     (this, b >>>= 1, rights, transformer, id, reducer)).fork();
5523 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
5524 >                      rights, transformer, r, reducer)).fork();
5525                  }
5526 <                double r = id;
5527 <                Object v;
6089 <                while ((v = advance()) != null)
6090 <                    r = reducer.apply(r, transformer.apply(entryFor((K)nextKey, (V)v)));
5526 >                for (Node<K,V> p; (p = advance()) != null; )
5527 >                    r = reducer.apply(r, transformer.apply(p));
5528                  result = r;
5529 <                for (MapReduceEntriesToDoubleTask<K,V> t = this, s;;) {
5530 <                    int c; BulkTask<K,V,?> par;
5531 <                    if ((c = t.pending) == 0) {
5532 <                        for (s = t.rights; s != null; s = t.rights = s.nextRight) {
5533 <                            t.result = reducer.apply(t.result, s.result);
5534 <                        }
5535 <                        if ((par = t.parent) == null ||
5536 <                            !(par instanceof MapReduceEntriesToDoubleTask)) {
6100 <                            t.quietlyComplete();
6101 <                            break;
6102 <                        }
6103 <                        t = (MapReduceEntriesToDoubleTask<K,V>)par;
5529 >                CountedCompleter<?> c;
5530 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5531 >                    @SuppressWarnings("unchecked") MapReduceEntriesToDoubleTask<K,V>
5532 >                        t = (MapReduceEntriesToDoubleTask<K,V>)c,
5533 >                        s = t.rights;
5534 >                    while (s != null) {
5535 >                        t.result = reducer.apply(t.result, s.result);
5536 >                        s = t.rights = s.nextRight;
5537                      }
6105                    else if (t.casPending(c, c - 1))
6106                        break;
5538                  }
6108            } catch (Throwable ex) {
6109                return tryCompleteComputation(ex);
5539              }
6111            return false;
5540          }
6113        public final Double getRawResult() { return result; }
5541      }
5542  
5543 <    @SuppressWarnings("serial") static final class MapReduceMappingsToDoubleTask<K,V>
5543 >    @SuppressWarnings("serial")
5544 >    static final class MapReduceMappingsToDoubleTask<K,V>
5545          extends BulkTask<K,V,Double> {
5546          final ObjectByObjectToDouble<? super K, ? super V> transformer;
5547          final DoubleByDoubleToDouble reducer;
# Line 6121 | Line 5549 | public class ConcurrentHashMapV8<K, V>
5549          double result;
5550          MapReduceMappingsToDoubleTask<K,V> rights, nextRight;
5551          MapReduceMappingsToDoubleTask
5552 <            (ConcurrentHashMapV8<K,V> m,
6125 <             ObjectByObjectToDouble<? super K, ? super V> transformer,
6126 <             double basis,
6127 <             DoubleByDoubleToDouble reducer) {
6128 <            super(m);
6129 <            this.transformer = transformer;
6130 <            this.basis = basis; this.reducer = reducer;
6131 <        }
6132 <        MapReduceMappingsToDoubleTask
6133 <            (BulkTask<K,V,?> p, int b,
5552 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5553               MapReduceMappingsToDoubleTask<K,V> nextRight,
5554               ObjectByObjectToDouble<? super K, ? super V> transformer,
5555               double basis,
5556               DoubleByDoubleToDouble reducer) {
5557 <            super(p, b); this.nextRight = nextRight;
5557 >            super(p, b, i, f, t); this.nextRight = nextRight;
5558              this.transformer = transformer;
5559              this.basis = basis; this.reducer = reducer;
5560          }
5561 <        @SuppressWarnings("unchecked") public final boolean exec() {
5562 <            final ObjectByObjectToDouble<? super K, ? super V> transformer =
5563 <                this.transformer;
5564 <            final DoubleByDoubleToDouble reducer = this.reducer;
5565 <            if (transformer == null || reducer == null)
5566 <                return abortOnNullFunction();
5567 <            try {
5568 <                final double id = this.basis;
5569 <                for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
5570 <                    do {} while (!casPending(c = pending, c+1));
5561 >        public final Double getRawResult() { return result; }
5562 >        public final void compute() {
5563 >            final ObjectByObjectToDouble<? super K, ? super V> transformer;
5564 >            final DoubleByDoubleToDouble reducer;
5565 >            if ((transformer = this.transformer) != null &&
5566 >                (reducer = this.reducer) != null) {
5567 >                double r = this.basis;
5568 >                for (int i = baseIndex, f, h; batch > 0 &&
5569 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5570 >                    addToPendingCount(1);
5571                      (rights = new MapReduceMappingsToDoubleTask<K,V>
5572 <                     (this, b >>>= 1, rights, transformer, id, reducer)).fork();
5572 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
5573 >                      rights, transformer, r, reducer)).fork();
5574                  }
5575 <                double r = id;
5576 <                Object v;
6157 <                while ((v = advance()) != null)
6158 <                    r = reducer.apply(r, transformer.apply((K)nextKey, (V)v));
5575 >                for (Node<K,V> p; (p = advance()) != null; )
5576 >                    r = reducer.apply(r, transformer.apply(p.key, p.val));
5577                  result = r;
5578 <                for (MapReduceMappingsToDoubleTask<K,V> t = this, s;;) {
5579 <                    int c; BulkTask<K,V,?> par;
5580 <                    if ((c = t.pending) == 0) {
5581 <                        for (s = t.rights; s != null; s = t.rights = s.nextRight) {
5582 <                            t.result = reducer.apply(t.result, s.result);
5583 <                        }
5584 <                        if ((par = t.parent) == null ||
5585 <                            !(par instanceof MapReduceMappingsToDoubleTask)) {
6168 <                            t.quietlyComplete();
6169 <                            break;
6170 <                        }
6171 <                        t = (MapReduceMappingsToDoubleTask<K,V>)par;
5578 >                CountedCompleter<?> c;
5579 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5580 >                    @SuppressWarnings("unchecked") MapReduceMappingsToDoubleTask<K,V>
5581 >                        t = (MapReduceMappingsToDoubleTask<K,V>)c,
5582 >                        s = t.rights;
5583 >                    while (s != null) {
5584 >                        t.result = reducer.apply(t.result, s.result);
5585 >                        s = t.rights = s.nextRight;
5586                      }
6173                    else if (t.casPending(c, c - 1))
6174                        break;
5587                  }
6176            } catch (Throwable ex) {
6177                return tryCompleteComputation(ex);
5588              }
6179            return false;
5589          }
6181        public final Double getRawResult() { return result; }
5590      }
5591  
5592 <    @SuppressWarnings("serial") static final class MapReduceKeysToLongTask<K,V>
5592 >    @SuppressWarnings("serial")
5593 >    static final class MapReduceKeysToLongTask<K,V>
5594          extends BulkTask<K,V,Long> {
5595          final ObjectToLong<? super K> transformer;
5596          final LongByLongToLong reducer;
# Line 6189 | Line 5598 | public class ConcurrentHashMapV8<K, V>
5598          long result;
5599          MapReduceKeysToLongTask<K,V> rights, nextRight;
5600          MapReduceKeysToLongTask
5601 <            (ConcurrentHashMapV8<K,V> m,
6193 <             ObjectToLong<? super K> transformer,
6194 <             long basis,
6195 <             LongByLongToLong reducer) {
6196 <            super(m);
6197 <            this.transformer = transformer;
6198 <            this.basis = basis; this.reducer = reducer;
6199 <        }
6200 <        MapReduceKeysToLongTask
6201 <            (BulkTask<K,V,?> p, int b,
5601 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5602               MapReduceKeysToLongTask<K,V> nextRight,
5603               ObjectToLong<? super K> transformer,
5604               long basis,
5605               LongByLongToLong reducer) {
5606 <            super(p, b); this.nextRight = nextRight;
5606 >            super(p, b, i, f, t); this.nextRight = nextRight;
5607              this.transformer = transformer;
5608              this.basis = basis; this.reducer = reducer;
5609          }
5610 <        @SuppressWarnings("unchecked") public final boolean exec() {
5611 <            final ObjectToLong<? super K> transformer =
5612 <                this.transformer;
5613 <            final LongByLongToLong reducer = this.reducer;
5614 <            if (transformer == null || reducer == null)
5615 <                return abortOnNullFunction();
5616 <            try {
5617 <                final long id = this.basis;
5618 <                for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
5619 <                    do {} while (!casPending(c = pending, c+1));
5610 >        public final Long getRawResult() { return result; }
5611 >        public final void compute() {
5612 >            final ObjectToLong<? super K> transformer;
5613 >            final LongByLongToLong reducer;
5614 >            if ((transformer = this.transformer) != null &&
5615 >                (reducer = this.reducer) != null) {
5616 >                long r = this.basis;
5617 >                for (int i = baseIndex, f, h; batch > 0 &&
5618 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5619 >                    addToPendingCount(1);
5620                      (rights = new MapReduceKeysToLongTask<K,V>
5621 <                     (this, b >>>= 1, rights, transformer, id, reducer)).fork();
5621 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
5622 >                      rights, transformer, r, reducer)).fork();
5623                  }
5624 <                long r = id;
5625 <                while (advance() != null)
6225 <                    r = reducer.apply(r, transformer.apply((K)nextKey));
5624 >                for (Node<K,V> p; (p = advance()) != null; )
5625 >                    r = reducer.apply(r, transformer.apply(p.key));
5626                  result = r;
5627 <                for (MapReduceKeysToLongTask<K,V> t = this, s;;) {
5628 <                    int c; BulkTask<K,V,?> par;
5629 <                    if ((c = t.pending) == 0) {
5630 <                        for (s = t.rights; s != null; s = t.rights = s.nextRight) {
5631 <                            t.result = reducer.apply(t.result, s.result);
5632 <                        }
5633 <                        if ((par = t.parent) == null ||
5634 <                            !(par instanceof MapReduceKeysToLongTask)) {
6235 <                            t.quietlyComplete();
6236 <                            break;
6237 <                        }
6238 <                        t = (MapReduceKeysToLongTask<K,V>)par;
5627 >                CountedCompleter<?> c;
5628 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5629 >                    @SuppressWarnings("unchecked") MapReduceKeysToLongTask<K,V>
5630 >                        t = (MapReduceKeysToLongTask<K,V>)c,
5631 >                        s = t.rights;
5632 >                    while (s != null) {
5633 >                        t.result = reducer.apply(t.result, s.result);
5634 >                        s = t.rights = s.nextRight;
5635                      }
6240                    else if (t.casPending(c, c - 1))
6241                        break;
5636                  }
6243            } catch (Throwable ex) {
6244                return tryCompleteComputation(ex);
5637              }
6246            return false;
5638          }
6248        public final Long getRawResult() { return result; }
5639      }
5640  
5641 <    @SuppressWarnings("serial") static final class MapReduceValuesToLongTask<K,V>
5641 >    @SuppressWarnings("serial")
5642 >    static final class MapReduceValuesToLongTask<K,V>
5643          extends BulkTask<K,V,Long> {
5644          final ObjectToLong<? super V> transformer;
5645          final LongByLongToLong reducer;
# Line 6256 | Line 5647 | public class ConcurrentHashMapV8<K, V>
5647          long result;
5648          MapReduceValuesToLongTask<K,V> rights, nextRight;
5649          MapReduceValuesToLongTask
5650 <            (ConcurrentHashMapV8<K,V> m,
6260 <             ObjectToLong<? super V> transformer,
6261 <             long basis,
6262 <             LongByLongToLong reducer) {
6263 <            super(m);
6264 <            this.transformer = transformer;
6265 <            this.basis = basis; this.reducer = reducer;
6266 <        }
6267 <        MapReduceValuesToLongTask
6268 <            (BulkTask<K,V,?> p, int b,
5650 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5651               MapReduceValuesToLongTask<K,V> nextRight,
5652               ObjectToLong<? super V> transformer,
5653               long basis,
5654               LongByLongToLong reducer) {
5655 <            super(p, b); this.nextRight = nextRight;
5655 >            super(p, b, i, f, t); this.nextRight = nextRight;
5656              this.transformer = transformer;
5657              this.basis = basis; this.reducer = reducer;
5658          }
5659 <        @SuppressWarnings("unchecked") public final boolean exec() {
5660 <            final ObjectToLong<? super V> transformer =
5661 <                this.transformer;
5662 <            final LongByLongToLong reducer = this.reducer;
5663 <            if (transformer == null || reducer == null)
5664 <                return abortOnNullFunction();
5665 <            try {
5666 <                final long id = this.basis;
5667 <                for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
5668 <                    do {} while (!casPending(c = pending, c+1));
5659 >        public final Long getRawResult() { return result; }
5660 >        public final void compute() {
5661 >            final ObjectToLong<? super V> transformer;
5662 >            final LongByLongToLong reducer;
5663 >            if ((transformer = this.transformer) != null &&
5664 >                (reducer = this.reducer) != null) {
5665 >                long r = this.basis;
5666 >                for (int i = baseIndex, f, h; batch > 0 &&
5667 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5668 >                    addToPendingCount(1);
5669                      (rights = new MapReduceValuesToLongTask<K,V>
5670 <                     (this, b >>>= 1, rights, transformer, id, reducer)).fork();
5670 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
5671 >                      rights, transformer, r, reducer)).fork();
5672                  }
5673 <                long r = id;
5674 <                Object v;
6292 <                while ((v = advance()) != null)
6293 <                    r = reducer.apply(r, transformer.apply((V)v));
5673 >                for (Node<K,V> p; (p = advance()) != null; )
5674 >                    r = reducer.apply(r, transformer.apply(p.val));
5675                  result = r;
5676 <                for (MapReduceValuesToLongTask<K,V> t = this, s;;) {
5677 <                    int c; BulkTask<K,V,?> par;
5678 <                    if ((c = t.pending) == 0) {
5679 <                        for (s = t.rights; s != null; s = t.rights = s.nextRight) {
5680 <                            t.result = reducer.apply(t.result, s.result);
5681 <                        }
5682 <                        if ((par = t.parent) == null ||
5683 <                            !(par instanceof MapReduceValuesToLongTask)) {
6303 <                            t.quietlyComplete();
6304 <                            break;
6305 <                        }
6306 <                        t = (MapReduceValuesToLongTask<K,V>)par;
5676 >                CountedCompleter<?> c;
5677 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5678 >                    @SuppressWarnings("unchecked") MapReduceValuesToLongTask<K,V>
5679 >                        t = (MapReduceValuesToLongTask<K,V>)c,
5680 >                        s = t.rights;
5681 >                    while (s != null) {
5682 >                        t.result = reducer.apply(t.result, s.result);
5683 >                        s = t.rights = s.nextRight;
5684                      }
6308                    else if (t.casPending(c, c - 1))
6309                        break;
5685                  }
6311            } catch (Throwable ex) {
6312                return tryCompleteComputation(ex);
5686              }
6314            return false;
5687          }
6316        public final Long getRawResult() { return result; }
5688      }
5689  
5690 <    @SuppressWarnings("serial") static final class MapReduceEntriesToLongTask<K,V>
5690 >    @SuppressWarnings("serial")
5691 >    static final class MapReduceEntriesToLongTask<K,V>
5692          extends BulkTask<K,V,Long> {
5693          final ObjectToLong<Map.Entry<K,V>> transformer;
5694          final LongByLongToLong reducer;
# Line 6324 | Line 5696 | public class ConcurrentHashMapV8<K, V>
5696          long result;
5697          MapReduceEntriesToLongTask<K,V> rights, nextRight;
5698          MapReduceEntriesToLongTask
5699 <            (ConcurrentHashMapV8<K,V> m,
6328 <             ObjectToLong<Map.Entry<K,V>> transformer,
6329 <             long basis,
6330 <             LongByLongToLong reducer) {
6331 <            super(m);
6332 <            this.transformer = transformer;
6333 <            this.basis = basis; this.reducer = reducer;
6334 <        }
6335 <        MapReduceEntriesToLongTask
6336 <            (BulkTask<K,V,?> p, int b,
5699 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5700               MapReduceEntriesToLongTask<K,V> nextRight,
5701               ObjectToLong<Map.Entry<K,V>> transformer,
5702               long basis,
5703               LongByLongToLong reducer) {
5704 <            super(p, b); this.nextRight = nextRight;
5704 >            super(p, b, i, f, t); this.nextRight = nextRight;
5705              this.transformer = transformer;
5706              this.basis = basis; this.reducer = reducer;
5707          }
5708 <        @SuppressWarnings("unchecked") public final boolean exec() {
5709 <            final ObjectToLong<Map.Entry<K,V>> transformer =
5710 <                this.transformer;
5711 <            final LongByLongToLong reducer = this.reducer;
5712 <            if (transformer == null || reducer == null)
5713 <                return abortOnNullFunction();
5714 <            try {
5715 <                final long id = this.basis;
5716 <                for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
5717 <                    do {} while (!casPending(c = pending, c+1));
5708 >        public final Long getRawResult() { return result; }
5709 >        public final void compute() {
5710 >            final ObjectToLong<Map.Entry<K,V>> transformer;
5711 >            final LongByLongToLong reducer;
5712 >            if ((transformer = this.transformer) != null &&
5713 >                (reducer = this.reducer) != null) {
5714 >                long r = this.basis;
5715 >                for (int i = baseIndex, f, h; batch > 0 &&
5716 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5717 >                    addToPendingCount(1);
5718                      (rights = new MapReduceEntriesToLongTask<K,V>
5719 <                     (this, b >>>= 1, rights, transformer, id, reducer)).fork();
5719 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
5720 >                      rights, transformer, r, reducer)).fork();
5721                  }
5722 <                long r = id;
5723 <                Object v;
6360 <                while ((v = advance()) != null)
6361 <                    r = reducer.apply(r, transformer.apply(entryFor((K)nextKey, (V)v)));
5722 >                for (Node<K,V> p; (p = advance()) != null; )
5723 >                    r = reducer.apply(r, transformer.apply(p));
5724                  result = r;
5725 <                for (MapReduceEntriesToLongTask<K,V> t = this, s;;) {
5726 <                    int c; BulkTask<K,V,?> par;
5727 <                    if ((c = t.pending) == 0) {
5728 <                        for (s = t.rights; s != null; s = t.rights = s.nextRight) {
5729 <                            t.result = reducer.apply(t.result, s.result);
5730 <                        }
5731 <                        if ((par = t.parent) == null ||
5732 <                            !(par instanceof MapReduceEntriesToLongTask)) {
6371 <                            t.quietlyComplete();
6372 <                            break;
6373 <                        }
6374 <                        t = (MapReduceEntriesToLongTask<K,V>)par;
5725 >                CountedCompleter<?> c;
5726 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5727 >                    @SuppressWarnings("unchecked") MapReduceEntriesToLongTask<K,V>
5728 >                        t = (MapReduceEntriesToLongTask<K,V>)c,
5729 >                        s = t.rights;
5730 >                    while (s != null) {
5731 >                        t.result = reducer.apply(t.result, s.result);
5732 >                        s = t.rights = s.nextRight;
5733                      }
6376                    else if (t.casPending(c, c - 1))
6377                        break;
5734                  }
6379            } catch (Throwable ex) {
6380                return tryCompleteComputation(ex);
5735              }
6382            return false;
5736          }
6384        public final Long getRawResult() { return result; }
5737      }
5738  
5739 <    @SuppressWarnings("serial") static final class MapReduceMappingsToLongTask<K,V>
5739 >    @SuppressWarnings("serial")
5740 >    static final class MapReduceMappingsToLongTask<K,V>
5741          extends BulkTask<K,V,Long> {
5742          final ObjectByObjectToLong<? super K, ? super V> transformer;
5743          final LongByLongToLong reducer;
# Line 6392 | Line 5745 | public class ConcurrentHashMapV8<K, V>
5745          long result;
5746          MapReduceMappingsToLongTask<K,V> rights, nextRight;
5747          MapReduceMappingsToLongTask
5748 <            (ConcurrentHashMapV8<K,V> m,
6396 <             ObjectByObjectToLong<? super K, ? super V> transformer,
6397 <             long basis,
6398 <             LongByLongToLong reducer) {
6399 <            super(m);
6400 <            this.transformer = transformer;
6401 <            this.basis = basis; this.reducer = reducer;
6402 <        }
6403 <        MapReduceMappingsToLongTask
6404 <            (BulkTask<K,V,?> p, int b,
5748 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5749               MapReduceMappingsToLongTask<K,V> nextRight,
5750               ObjectByObjectToLong<? super K, ? super V> transformer,
5751               long basis,
5752               LongByLongToLong reducer) {
5753 <            super(p, b); this.nextRight = nextRight;
5753 >            super(p, b, i, f, t); this.nextRight = nextRight;
5754              this.transformer = transformer;
5755              this.basis = basis; this.reducer = reducer;
5756          }
5757 <        @SuppressWarnings("unchecked") public final boolean exec() {
5758 <            final ObjectByObjectToLong<? super K, ? super V> transformer =
5759 <                this.transformer;
5760 <            final LongByLongToLong reducer = this.reducer;
5761 <            if (transformer == null || reducer == null)
5762 <                return abortOnNullFunction();
5763 <            try {
5764 <                final long id = this.basis;
5765 <                for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
5766 <                    do {} while (!casPending(c = pending, c+1));
5757 >        public final Long getRawResult() { return result; }
5758 >        public final void compute() {
5759 >            final ObjectByObjectToLong<? super K, ? super V> transformer;
5760 >            final LongByLongToLong reducer;
5761 >            if ((transformer = this.transformer) != null &&
5762 >                (reducer = this.reducer) != null) {
5763 >                long r = this.basis;
5764 >                for (int i = baseIndex, f, h; batch > 0 &&
5765 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5766 >                    addToPendingCount(1);
5767                      (rights = new MapReduceMappingsToLongTask<K,V>
5768 <                     (this, b >>>= 1, rights, transformer, id, reducer)).fork();
5768 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
5769 >                      rights, transformer, r, reducer)).fork();
5770                  }
5771 <                long r = id;
5772 <                Object v;
6428 <                while ((v = advance()) != null)
6429 <                    r = reducer.apply(r, transformer.apply((K)nextKey, (V)v));
5771 >                for (Node<K,V> p; (p = advance()) != null; )
5772 >                    r = reducer.apply(r, transformer.apply(p.key, p.val));
5773                  result = r;
5774 <                for (MapReduceMappingsToLongTask<K,V> t = this, s;;) {
5775 <                    int c; BulkTask<K,V,?> par;
5776 <                    if ((c = t.pending) == 0) {
5777 <                        for (s = t.rights; s != null; s = t.rights = s.nextRight) {
5778 <                            t.result = reducer.apply(t.result, s.result);
5779 <                        }
5780 <                        if ((par = t.parent) == null ||
5781 <                            !(par instanceof MapReduceMappingsToLongTask)) {
6439 <                            t.quietlyComplete();
6440 <                            break;
6441 <                        }
6442 <                        t = (MapReduceMappingsToLongTask<K,V>)par;
5774 >                CountedCompleter<?> c;
5775 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5776 >                    @SuppressWarnings("unchecked") MapReduceMappingsToLongTask<K,V>
5777 >                        t = (MapReduceMappingsToLongTask<K,V>)c,
5778 >                        s = t.rights;
5779 >                    while (s != null) {
5780 >                        t.result = reducer.apply(t.result, s.result);
5781 >                        s = t.rights = s.nextRight;
5782                      }
6444                    else if (t.casPending(c, c - 1))
6445                        break;
5783                  }
6447            } catch (Throwable ex) {
6448                return tryCompleteComputation(ex);
5784              }
6450            return false;
5785          }
6452        public final Long getRawResult() { return result; }
5786      }
5787  
5788 <    @SuppressWarnings("serial") static final class MapReduceKeysToIntTask<K,V>
5788 >    @SuppressWarnings("serial")
5789 >    static final class MapReduceKeysToIntTask<K,V>
5790          extends BulkTask<K,V,Integer> {
5791          final ObjectToInt<? super K> transformer;
5792          final IntByIntToInt reducer;
# Line 6460 | Line 5794 | public class ConcurrentHashMapV8<K, V>
5794          int result;
5795          MapReduceKeysToIntTask<K,V> rights, nextRight;
5796          MapReduceKeysToIntTask
5797 <            (ConcurrentHashMapV8<K,V> m,
6464 <             ObjectToInt<? super K> transformer,
6465 <             int basis,
6466 <             IntByIntToInt reducer) {
6467 <            super(m);
6468 <            this.transformer = transformer;
6469 <            this.basis = basis; this.reducer = reducer;
6470 <        }
6471 <        MapReduceKeysToIntTask
6472 <            (BulkTask<K,V,?> p, int b,
5797 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5798               MapReduceKeysToIntTask<K,V> nextRight,
5799               ObjectToInt<? super K> transformer,
5800               int basis,
5801               IntByIntToInt reducer) {
5802 <            super(p, b); this.nextRight = nextRight;
5802 >            super(p, b, i, f, t); this.nextRight = nextRight;
5803              this.transformer = transformer;
5804              this.basis = basis; this.reducer = reducer;
5805          }
5806 <        @SuppressWarnings("unchecked") public final boolean exec() {
5807 <            final ObjectToInt<? super K> transformer =
5808 <                this.transformer;
5809 <            final IntByIntToInt reducer = this.reducer;
5810 <            if (transformer == null || reducer == null)
5811 <                return abortOnNullFunction();
5812 <            try {
5813 <                final int id = this.basis;
5814 <                for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
5815 <                    do {} while (!casPending(c = pending, c+1));
5806 >        public final Integer getRawResult() { return result; }
5807 >        public final void compute() {
5808 >            final ObjectToInt<? super K> transformer;
5809 >            final IntByIntToInt reducer;
5810 >            if ((transformer = this.transformer) != null &&
5811 >                (reducer = this.reducer) != null) {
5812 >                int r = this.basis;
5813 >                for (int i = baseIndex, f, h; batch > 0 &&
5814 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5815 >                    addToPendingCount(1);
5816                      (rights = new MapReduceKeysToIntTask<K,V>
5817 <                     (this, b >>>= 1, rights, transformer, id, reducer)).fork();
5817 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
5818 >                      rights, transformer, r, reducer)).fork();
5819                  }
5820 <                int r = id;
5821 <                while (advance() != null)
6496 <                    r = reducer.apply(r, transformer.apply((K)nextKey));
5820 >                for (Node<K,V> p; (p = advance()) != null; )
5821 >                    r = reducer.apply(r, transformer.apply(p.key));
5822                  result = r;
5823 <                for (MapReduceKeysToIntTask<K,V> t = this, s;;) {
5824 <                    int c; BulkTask<K,V,?> par;
5825 <                    if ((c = t.pending) == 0) {
5826 <                        for (s = t.rights; s != null; s = t.rights = s.nextRight) {
5827 <                            t.result = reducer.apply(t.result, s.result);
5828 <                        }
5829 <                        if ((par = t.parent) == null ||
5830 <                            !(par instanceof MapReduceKeysToIntTask)) {
6506 <                            t.quietlyComplete();
6507 <                            break;
6508 <                        }
6509 <                        t = (MapReduceKeysToIntTask<K,V>)par;
5823 >                CountedCompleter<?> c;
5824 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5825 >                    @SuppressWarnings("unchecked") MapReduceKeysToIntTask<K,V>
5826 >                        t = (MapReduceKeysToIntTask<K,V>)c,
5827 >                        s = t.rights;
5828 >                    while (s != null) {
5829 >                        t.result = reducer.apply(t.result, s.result);
5830 >                        s = t.rights = s.nextRight;
5831                      }
6511                    else if (t.casPending(c, c - 1))
6512                        break;
5832                  }
6514            } catch (Throwable ex) {
6515                return tryCompleteComputation(ex);
5833              }
6517            return false;
5834          }
6519        public final Integer getRawResult() { return result; }
5835      }
5836  
5837 <    @SuppressWarnings("serial") static final class MapReduceValuesToIntTask<K,V>
5837 >    @SuppressWarnings("serial")
5838 >    static final class MapReduceValuesToIntTask<K,V>
5839          extends BulkTask<K,V,Integer> {
5840          final ObjectToInt<? super V> transformer;
5841          final IntByIntToInt reducer;
# Line 6527 | Line 5843 | public class ConcurrentHashMapV8<K, V>
5843          int result;
5844          MapReduceValuesToIntTask<K,V> rights, nextRight;
5845          MapReduceValuesToIntTask
5846 <            (ConcurrentHashMapV8<K,V> m,
6531 <             ObjectToInt<? super V> transformer,
6532 <             int basis,
6533 <             IntByIntToInt reducer) {
6534 <            super(m);
6535 <            this.transformer = transformer;
6536 <            this.basis = basis; this.reducer = reducer;
6537 <        }
6538 <        MapReduceValuesToIntTask
6539 <            (BulkTask<K,V,?> p, int b,
5846 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5847               MapReduceValuesToIntTask<K,V> nextRight,
5848               ObjectToInt<? super V> transformer,
5849               int basis,
5850               IntByIntToInt reducer) {
5851 <            super(p, b); this.nextRight = nextRight;
5851 >            super(p, b, i, f, t); this.nextRight = nextRight;
5852              this.transformer = transformer;
5853              this.basis = basis; this.reducer = reducer;
5854          }
5855 <        @SuppressWarnings("unchecked") public final boolean exec() {
5856 <            final ObjectToInt<? super V> transformer =
5857 <                this.transformer;
5858 <            final IntByIntToInt reducer = this.reducer;
5859 <            if (transformer == null || reducer == null)
5860 <                return abortOnNullFunction();
5861 <            try {
5862 <                final int id = this.basis;
5863 <                for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
5864 <                    do {} while (!casPending(c = pending, c+1));
5855 >        public final Integer getRawResult() { return result; }
5856 >        public final void compute() {
5857 >            final ObjectToInt<? super V> transformer;
5858 >            final IntByIntToInt reducer;
5859 >            if ((transformer = this.transformer) != null &&
5860 >                (reducer = this.reducer) != null) {
5861 >                int r = this.basis;
5862 >                for (int i = baseIndex, f, h; batch > 0 &&
5863 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5864 >                    addToPendingCount(1);
5865                      (rights = new MapReduceValuesToIntTask<K,V>
5866 <                     (this, b >>>= 1, rights, transformer, id, reducer)).fork();
5866 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
5867 >                      rights, transformer, r, reducer)).fork();
5868                  }
5869 <                int r = id;
5870 <                Object v;
6563 <                while ((v = advance()) != null)
6564 <                    r = reducer.apply(r, transformer.apply((V)v));
5869 >                for (Node<K,V> p; (p = advance()) != null; )
5870 >                    r = reducer.apply(r, transformer.apply(p.val));
5871                  result = r;
5872 <                for (MapReduceValuesToIntTask<K,V> t = this, s;;) {
5873 <                    int c; BulkTask<K,V,?> par;
5874 <                    if ((c = t.pending) == 0) {
5875 <                        for (s = t.rights; s != null; s = t.rights = s.nextRight) {
5876 <                            t.result = reducer.apply(t.result, s.result);
5877 <                        }
5878 <                        if ((par = t.parent) == null ||
5879 <                            !(par instanceof MapReduceValuesToIntTask)) {
6574 <                            t.quietlyComplete();
6575 <                            break;
6576 <                        }
6577 <                        t = (MapReduceValuesToIntTask<K,V>)par;
5872 >                CountedCompleter<?> c;
5873 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5874 >                    @SuppressWarnings("unchecked") MapReduceValuesToIntTask<K,V>
5875 >                        t = (MapReduceValuesToIntTask<K,V>)c,
5876 >                        s = t.rights;
5877 >                    while (s != null) {
5878 >                        t.result = reducer.apply(t.result, s.result);
5879 >                        s = t.rights = s.nextRight;
5880                      }
6579                    else if (t.casPending(c, c - 1))
6580                        break;
5881                  }
6582            } catch (Throwable ex) {
6583                return tryCompleteComputation(ex);
5882              }
6585            return false;
5883          }
6587        public final Integer getRawResult() { return result; }
5884      }
5885  
5886 <    @SuppressWarnings("serial") static final class MapReduceEntriesToIntTask<K,V>
5886 >    @SuppressWarnings("serial")
5887 >    static final class MapReduceEntriesToIntTask<K,V>
5888          extends BulkTask<K,V,Integer> {
5889          final ObjectToInt<Map.Entry<K,V>> transformer;
5890          final IntByIntToInt reducer;
# Line 6595 | Line 5892 | public class ConcurrentHashMapV8<K, V>
5892          int result;
5893          MapReduceEntriesToIntTask<K,V> rights, nextRight;
5894          MapReduceEntriesToIntTask
5895 <            (ConcurrentHashMapV8<K,V> m,
6599 <             ObjectToInt<Map.Entry<K,V>> transformer,
6600 <             int basis,
6601 <             IntByIntToInt reducer) {
6602 <            super(m);
6603 <            this.transformer = transformer;
6604 <            this.basis = basis; this.reducer = reducer;
6605 <        }
6606 <        MapReduceEntriesToIntTask
6607 <            (BulkTask<K,V,?> p, int b,
5895 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5896               MapReduceEntriesToIntTask<K,V> nextRight,
5897               ObjectToInt<Map.Entry<K,V>> transformer,
5898               int basis,
5899               IntByIntToInt reducer) {
5900 <            super(p, b); this.nextRight = nextRight;
5900 >            super(p, b, i, f, t); this.nextRight = nextRight;
5901              this.transformer = transformer;
5902              this.basis = basis; this.reducer = reducer;
5903          }
5904 <        @SuppressWarnings("unchecked") public final boolean exec() {
5905 <            final ObjectToInt<Map.Entry<K,V>> transformer =
5906 <                this.transformer;
5907 <            final IntByIntToInt reducer = this.reducer;
5908 <            if (transformer == null || reducer == null)
5909 <                return abortOnNullFunction();
5910 <            try {
5911 <                final int id = this.basis;
5912 <                for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
5913 <                    do {} while (!casPending(c = pending, c+1));
5904 >        public final Integer getRawResult() { return result; }
5905 >        public final void compute() {
5906 >            final ObjectToInt<Map.Entry<K,V>> transformer;
5907 >            final IntByIntToInt reducer;
5908 >            if ((transformer = this.transformer) != null &&
5909 >                (reducer = this.reducer) != null) {
5910 >                int r = this.basis;
5911 >                for (int i = baseIndex, f, h; batch > 0 &&
5912 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5913 >                    addToPendingCount(1);
5914                      (rights = new MapReduceEntriesToIntTask<K,V>
5915 <                     (this, b >>>= 1, rights, transformer, id, reducer)).fork();
5915 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
5916 >                      rights, transformer, r, reducer)).fork();
5917                  }
5918 <                int r = id;
5919 <                Object v;
6631 <                while ((v = advance()) != null)
6632 <                    r = reducer.apply(r, transformer.apply(entryFor((K)nextKey, (V)v)));
5918 >                for (Node<K,V> p; (p = advance()) != null; )
5919 >                    r = reducer.apply(r, transformer.apply(p));
5920                  result = r;
5921 <                for (MapReduceEntriesToIntTask<K,V> t = this, s;;) {
5922 <                    int c; BulkTask<K,V,?> par;
5923 <                    if ((c = t.pending) == 0) {
5924 <                        for (s = t.rights; s != null; s = t.rights = s.nextRight) {
5925 <                            t.result = reducer.apply(t.result, s.result);
5926 <                        }
5927 <                        if ((par = t.parent) == null ||
5928 <                            !(par instanceof MapReduceEntriesToIntTask)) {
6642 <                            t.quietlyComplete();
6643 <                            break;
6644 <                        }
6645 <                        t = (MapReduceEntriesToIntTask<K,V>)par;
5921 >                CountedCompleter<?> c;
5922 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5923 >                    @SuppressWarnings("unchecked") MapReduceEntriesToIntTask<K,V>
5924 >                        t = (MapReduceEntriesToIntTask<K,V>)c,
5925 >                        s = t.rights;
5926 >                    while (s != null) {
5927 >                        t.result = reducer.apply(t.result, s.result);
5928 >                        s = t.rights = s.nextRight;
5929                      }
6647                    else if (t.casPending(c, c - 1))
6648                        break;
5930                  }
6650            } catch (Throwable ex) {
6651                return tryCompleteComputation(ex);
5931              }
6653            return false;
5932          }
6655        public final Integer getRawResult() { return result; }
5933      }
5934  
5935 <    @SuppressWarnings("serial") static final class MapReduceMappingsToIntTask<K,V>
5935 >    @SuppressWarnings("serial")
5936 >    static final class MapReduceMappingsToIntTask<K,V>
5937          extends BulkTask<K,V,Integer> {
5938          final ObjectByObjectToInt<? super K, ? super V> transformer;
5939          final IntByIntToInt reducer;
# Line 6663 | Line 5941 | public class ConcurrentHashMapV8<K, V>
5941          int result;
5942          MapReduceMappingsToIntTask<K,V> rights, nextRight;
5943          MapReduceMappingsToIntTask
5944 <            (ConcurrentHashMapV8<K,V> m,
6667 <             ObjectByObjectToInt<? super K, ? super V> transformer,
6668 <             int basis,
6669 <             IntByIntToInt reducer) {
6670 <            super(m);
6671 <            this.transformer = transformer;
6672 <            this.basis = basis; this.reducer = reducer;
6673 <        }
6674 <        MapReduceMappingsToIntTask
6675 <            (BulkTask<K,V,?> p, int b,
5944 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5945               MapReduceMappingsToIntTask<K,V> nextRight,
5946               ObjectByObjectToInt<? super K, ? super V> transformer,
5947               int basis,
5948               IntByIntToInt reducer) {
5949 <            super(p, b); this.nextRight = nextRight;
5949 >            super(p, b, i, f, t); this.nextRight = nextRight;
5950              this.transformer = transformer;
5951              this.basis = basis; this.reducer = reducer;
5952          }
5953 <        @SuppressWarnings("unchecked") public final boolean exec() {
5954 <            final ObjectByObjectToInt<? super K, ? super V> transformer =
5955 <                this.transformer;
5956 <            final IntByIntToInt reducer = this.reducer;
5957 <            if (transformer == null || reducer == null)
5958 <                return abortOnNullFunction();
5959 <            try {
5960 <                final int id = this.basis;
5961 <                for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
5962 <                    do {} while (!casPending(c = pending, c+1));
5953 >        public final Integer getRawResult() { return result; }
5954 >        public final void compute() {
5955 >            final ObjectByObjectToInt<? super K, ? super V> transformer;
5956 >            final IntByIntToInt reducer;
5957 >            if ((transformer = this.transformer) != null &&
5958 >                (reducer = this.reducer) != null) {
5959 >                int r = this.basis;
5960 >                for (int i = baseIndex, f, h; batch > 0 &&
5961 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5962 >                    addToPendingCount(1);
5963                      (rights = new MapReduceMappingsToIntTask<K,V>
5964 <                     (this, b >>>= 1, rights, transformer, id, reducer)).fork();
5964 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
5965 >                      rights, transformer, r, reducer)).fork();
5966                  }
5967 <                int r = id;
5968 <                Object v;
6699 <                while ((v = advance()) != null)
6700 <                    r = reducer.apply(r, transformer.apply((K)nextKey, (V)v));
5967 >                for (Node<K,V> p; (p = advance()) != null; )
5968 >                    r = reducer.apply(r, transformer.apply(p.key, p.val));
5969                  result = r;
5970 <                for (MapReduceMappingsToIntTask<K,V> t = this, s;;) {
5971 <                    int c; BulkTask<K,V,?> par;
5972 <                    if ((c = t.pending) == 0) {
5973 <                        for (s = t.rights; s != null; s = t.rights = s.nextRight) {
5974 <                            t.result = reducer.apply(t.result, s.result);
5970 >                CountedCompleter<?> c;
5971 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5972 >                    @SuppressWarnings("unchecked") MapReduceMappingsToIntTask<K,V>
5973 >                        t = (MapReduceMappingsToIntTask<K,V>)c,
5974 >                        s = t.rights;
5975 >                    while (s != null) {
5976 >                        t.result = reducer.apply(t.result, s.result);
5977 >                        s = t.rights = s.nextRight;
5978 >                    }
5979 >                }
5980 >            }
5981 >        }
5982 >    }
5983 >
5984 >    /* ---------------- Counters -------------- */
5985 >
5986 >    // Adapted from LongAdder and Striped64.
5987 >    // See their internal docs for explanation.
5988 >
5989 >    // A padded cell for distributing counts
5990 >    static final class CounterCell {
5991 >        volatile long p0, p1, p2, p3, p4, p5, p6;
5992 >        volatile long value;
5993 >        volatile long q0, q1, q2, q3, q4, q5, q6;
5994 >        CounterCell(long x) { value = x; }
5995 >    }
5996 >
5997 >    /**
5998 >     * Holder for the thread-local hash code determining which
5999 >     * CounterCell to use. The code is initialized via the
6000 >     * counterHashCodeGenerator, but may be moved upon collisions.
6001 >     */
6002 >    static final class CounterHashCode {
6003 >        int code;
6004 >    }
6005 >
6006 >    /**
6007 >     * Generates initial value for per-thread CounterHashCodes.
6008 >     */
6009 >    static final AtomicInteger counterHashCodeGenerator = new AtomicInteger();
6010 >
6011 >    /**
6012 >     * Increment for counterHashCodeGenerator. See class ThreadLocal
6013 >     * for explanation.
6014 >     */
6015 >    static final int SEED_INCREMENT = 0x61c88647;
6016 >
6017 >    /**
6018 >     * Per-thread counter hash codes. Shared across all instances.
6019 >     */
6020 >    static final ThreadLocal<CounterHashCode> threadCounterHashCode =
6021 >        new ThreadLocal<CounterHashCode>();
6022 >
6023 >
6024 >    final long sumCount() {
6025 >        CounterCell[] as = counterCells; CounterCell a;
6026 >        long sum = baseCount;
6027 >        if (as != null) {
6028 >            for (int i = 0; i < as.length; ++i) {
6029 >                if ((a = as[i]) != null)
6030 >                    sum += a.value;
6031 >            }
6032 >        }
6033 >        return sum;
6034 >    }
6035 >
6036 >    // See LongAdder version for explanation
6037 >    private final void fullAddCount(long x, CounterHashCode hc,
6038 >                                    boolean wasUncontended) {
6039 >        int h;
6040 >        if (hc == null) {
6041 >            hc = new CounterHashCode();
6042 >            int s = counterHashCodeGenerator.addAndGet(SEED_INCREMENT);
6043 >            h = hc.code = (s == 0) ? 1 : s; // Avoid zero
6044 >            threadCounterHashCode.set(hc);
6045 >        }
6046 >        else
6047 >            h = hc.code;
6048 >        boolean collide = false;                // True if last slot nonempty
6049 >        for (;;) {
6050 >            CounterCell[] as; CounterCell a; int n; long v;
6051 >            if ((as = counterCells) != null && (n = as.length) > 0) {
6052 >                if ((a = as[(n - 1) & h]) == null) {
6053 >                    if (cellsBusy == 0) {            // Try to attach new Cell
6054 >                        CounterCell r = new CounterCell(x); // Optimistic create
6055 >                        if (cellsBusy == 0 &&
6056 >                            U.compareAndSwapInt(this, CELLSBUSY, 0, 1)) {
6057 >                            boolean created = false;
6058 >                            try {               // Recheck under lock
6059 >                                CounterCell[] rs; int m, j;
6060 >                                if ((rs = counterCells) != null &&
6061 >                                    (m = rs.length) > 0 &&
6062 >                                    rs[j = (m - 1) & h] == null) {
6063 >                                    rs[j] = r;
6064 >                                    created = true;
6065 >                                }
6066 >                            } finally {
6067 >                                cellsBusy = 0;
6068 >                            }
6069 >                            if (created)
6070 >                                break;
6071 >                            continue;           // Slot is now non-empty
6072                          }
6073 <                        if ((par = t.parent) == null ||
6074 <                            !(par instanceof MapReduceMappingsToIntTask)) {
6075 <                            t.quietlyComplete();
6076 <                            break;
6073 >                    }
6074 >                    collide = false;
6075 >                }
6076 >                else if (!wasUncontended)       // CAS already known to fail
6077 >                    wasUncontended = true;      // Continue after rehash
6078 >                else if (U.compareAndSwapLong(a, CELLVALUE, v = a.value, v + x))
6079 >                    break;
6080 >                else if (counterCells != as || n >= NCPU)
6081 >                    collide = false;            // At max size or stale
6082 >                else if (!collide)
6083 >                    collide = true;
6084 >                else if (cellsBusy == 0 &&
6085 >                         U.compareAndSwapInt(this, CELLSBUSY, 0, 1)) {
6086 >                    try {
6087 >                        if (counterCells == as) {// Expand table unless stale
6088 >                            CounterCell[] rs = new CounterCell[n << 1];
6089 >                            for (int i = 0; i < n; ++i)
6090 >                                rs[i] = as[i];
6091 >                            counterCells = rs;
6092                          }
6093 <                        t = (MapReduceMappingsToIntTask<K,V>)par;
6093 >                    } finally {
6094 >                        cellsBusy = 0;
6095                      }
6096 <                    else if (t.casPending(c, c - 1))
6097 <                        break;
6096 >                    collide = false;
6097 >                    continue;                   // Retry with expanded table
6098                  }
6099 <            } catch (Throwable ex) {
6100 <                return tryCompleteComputation(ex);
6099 >                h ^= h << 13;                   // Rehash
6100 >                h ^= h >>> 17;
6101 >                h ^= h << 5;
6102 >            }
6103 >            else if (cellsBusy == 0 && counterCells == as &&
6104 >                     U.compareAndSwapInt(this, CELLSBUSY, 0, 1)) {
6105 >                boolean init = false;
6106 >                try {                           // Initialize table
6107 >                    if (counterCells == as) {
6108 >                        CounterCell[] rs = new CounterCell[2];
6109 >                        rs[h & 1] = new CounterCell(x);
6110 >                        counterCells = rs;
6111 >                        init = true;
6112 >                    }
6113 >                } finally {
6114 >                    cellsBusy = 0;
6115 >                }
6116 >                if (init)
6117 >                    break;
6118              }
6119 <            return false;
6119 >            else if (U.compareAndSwapLong(this, BASECOUNT, v = baseCount, v + x))
6120 >                break;                          // Fall back on using base
6121          }
6122 <        public final Integer getRawResult() { return result; }
6122 >        hc.code = h;                            // Record index for next time
6123      }
6124  
6726
6125      // Unsafe mechanics
6126 <    private static final sun.misc.Unsafe UNSAFE;
6127 <    private static final long counterOffset;
6128 <    private static final long sizeCtlOffset;
6126 >    private static final sun.misc.Unsafe U;
6127 >    private static final long SIZECTL;
6128 >    private static final long TRANSFERINDEX;
6129 >    private static final long TRANSFERORIGIN;
6130 >    private static final long BASECOUNT;
6131 >    private static final long CELLSBUSY;
6132 >    private static final long CELLVALUE;
6133      private static final long ABASE;
6134      private static final int ASHIFT;
6135  
6136      static {
6735        int ss;
6137          try {
6138 <            UNSAFE = getUnsafe();
6138 >            U = getUnsafe();
6139              Class<?> k = ConcurrentHashMapV8.class;
6140 <            counterOffset = UNSAFE.objectFieldOffset
6740 <                (k.getDeclaredField("counter"));
6741 <            sizeCtlOffset = UNSAFE.objectFieldOffset
6140 >            SIZECTL = U.objectFieldOffset
6141                  (k.getDeclaredField("sizeCtl"));
6142 <            Class<?> sc = Node[].class;
6143 <            ABASE = UNSAFE.arrayBaseOffset(sc);
6144 <            ss = UNSAFE.arrayIndexScale(sc);
6142 >            TRANSFERINDEX = U.objectFieldOffset
6143 >                (k.getDeclaredField("transferIndex"));
6144 >            TRANSFERORIGIN = U.objectFieldOffset
6145 >                (k.getDeclaredField("transferOrigin"));
6146 >            BASECOUNT = U.objectFieldOffset
6147 >                (k.getDeclaredField("baseCount"));
6148 >            CELLSBUSY = U.objectFieldOffset
6149 >                (k.getDeclaredField("cellsBusy"));
6150 >            Class<?> ck = CounterCell.class;
6151 >            CELLVALUE = U.objectFieldOffset
6152 >                (ck.getDeclaredField("value"));
6153 >            Class<?> ak = Node[].class;
6154 >            ABASE = U.arrayBaseOffset(ak);
6155 >            int scale = U.arrayIndexScale(ak);
6156 >            if ((scale & (scale - 1)) != 0)
6157 >                throw new Error("data type scale not a power of two");
6158 >            ASHIFT = 31 - Integer.numberOfLeadingZeros(scale);
6159          } catch (Exception e) {
6160              throw new Error(e);
6161          }
6749        if ((ss & (ss-1)) != 0)
6750            throw new Error("data type scale not a power of two");
6751        ASHIFT = 31 - Integer.numberOfLeadingZeros(ss);
6162      }
6163  
6164      /**
# Line 6761 | Line 6171 | public class ConcurrentHashMapV8<K, V>
6171      private static sun.misc.Unsafe getUnsafe() {
6172          try {
6173              return sun.misc.Unsafe.getUnsafe();
6174 <        } catch (SecurityException se) {
6175 <            try {
6176 <                return java.security.AccessController.doPrivileged
6177 <                    (new java.security
6178 <                     .PrivilegedExceptionAction<sun.misc.Unsafe>() {
6179 <                        public sun.misc.Unsafe run() throws Exception {
6180 <                            java.lang.reflect.Field f = sun.misc
6181 <                                .Unsafe.class.getDeclaredField("theUnsafe");
6182 <                            f.setAccessible(true);
6183 <                            return (sun.misc.Unsafe) f.get(null);
6184 <                        }});
6185 <            } catch (java.security.PrivilegedActionException e) {
6186 <                throw new RuntimeException("Could not initialize intrinsics",
6187 <                                           e.getCause());
6188 <            }
6174 >        } catch (SecurityException tryReflectionInstead) {}
6175 >        try {
6176 >            return java.security.AccessController.doPrivileged
6177 >            (new java.security.PrivilegedExceptionAction<sun.misc.Unsafe>() {
6178 >                public sun.misc.Unsafe run() throws Exception {
6179 >                    Class<sun.misc.Unsafe> k = sun.misc.Unsafe.class;
6180 >                    for (java.lang.reflect.Field f : k.getDeclaredFields()) {
6181 >                        f.setAccessible(true);
6182 >                        Object x = f.get(null);
6183 >                        if (k.isInstance(x))
6184 >                            return k.cast(x);
6185 >                    }
6186 >                    throw new NoSuchFieldError("the Unsafe");
6187 >                }});
6188 >        } catch (java.security.PrivilegedActionException e) {
6189 >            throw new RuntimeException("Could not initialize intrinsics",
6190 >                                       e.getCause());
6191          }
6192      }
6193   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines