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.57 by dl, Mon Aug 13 19:52:33 2012 UTC vs.
Revision 1.125 by jsr166, Sun Sep 13 16:28:14 2015 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.AbstractMap;
16   import java.util.Arrays;
14 import java.util.Map;
15 import java.util.Set;
17   import java.util.Collection;
18 < import java.util.AbstractMap;
19 < import java.util.AbstractSet;
19 < import java.util.AbstractCollection;
20 < import java.util.Hashtable;
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
48 < * onset.  For aggregate operations such as {@code putAll} and {@code
49 < * clear}, concurrent retrievals may reflect insertion or removal of
50 < * only some entries.  Similarly, Iterators and Enumerations return
51 < * elements reflecting the state of the hash table at some point at or
52 < * since the creation of the iterator/enumeration.  They do
53 < * <em>not</em> throw {@link ConcurrentModificationException}.
54 < * However, iterators are designed to be used by only one thread at a
55 < * time.  Bear in mind that the results of aggregate status methods
56 < * including {@code size}, {@code isEmpty}, and {@code containsValue}
57 < * are typically useful only when a map is not undergoing concurrent
58 < * updates in other threads.  Otherwise the results of these methods
59 < * reflect transient states that may be adequate for monitoring
60 < * or estimation purposes, but not for program control.
48 > * onset. (More formally, an update operation for a given key bears a
49 > * <em>happens-before</em> relation with any (non-null) retrieval for
50 > * that key reporting the updated value.)  For aggregate operations
51 > * such as {@code putAll} and {@code clear}, concurrent retrievals may
52 > * reflect insertion or removal of only some entries.  Similarly,
53 > * Iterators and Enumerations return elements reflecting the state of
54 > * the hash table at some point at or since the creation of the
55 > * iterator/enumeration.  They do <em>not</em> throw {@link
56 > * ConcurrentModificationException}.  However, iterators are designed
57 > * to be used by only one thread at a time.  Bear in mind that the
58 > * results of aggregate status methods including {@code size}, {@code
59 > * isEmpty}, and {@code containsValue} are typically useful only when
60 > * a map is not undergoing concurrent updates in other threads.
61 > * Otherwise the results of these methods reflect transient states
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 80 | 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.
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.
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.)
134 + *
135 + * <li>Mapped reductions that accumulate the results of a given
136 + * function applied to each element.
137 + *
138 + * <li>Reductions to scalar doubles, longs, and ints, using a
139 + * given basis value.
140 + *
141 + * </ul>
142 + * </ul>
143 + *
144 + * <p>These bulk operations accept a {@code parallelismThreshold}
145 + * argument. Methods proceed sequentially if the current map size is
146 + * estimated to be less than the given threshold. Using a value of
147 + * {@code Long.MAX_VALUE} suppresses all parallelism.  Using a value
148 + * of {@code 1} results in maximal parallelism by partitioning into
149 + * enough subtasks to fully utilize the {@link
150 + * ForkJoinPool#commonPool()} that is used for all parallel
151 + * computations. Normally, you would initially choose one of these
152 + * extreme values, and then measure performance of using in-between
153 + * values that trade off overhead versus throughput.
154 + *
155 + * <p>The concurrency properties of bulk operations follow
156 + * from those of ConcurrentHashMapV8: Any non-null result returned
157 + * from {@code get(key)} and related access methods bears a
158 + * happens-before relation with the associated insertion or
159 + * update.  The result of any bulk operation reflects the
160 + * composition of these per-element relations (but is not
161 + * necessarily atomic with respect to the map as a whole unless it
162 + * is somehow known to be quiescent).  Conversely, because keys
163 + * and values in the map are never null, null serves as a reliable
164 + * atomic indicator of the current lack of any result.  To
165 + * maintain this property, null serves as an implicit basis for
166 + * all non-scalar reduction operations. For the double, long, and
167 + * int versions, the basis should be one that, when combined with
168 + * any other value, returns that other value (more formally, it
169 + * should be the identity element for the reduction). Most common
170 + * reductions have these properties; for example, computing a sum
171 + * with basis 0 or a minimum with basis MAX_VALUE.
172 + *
173 + * <p>Search and transformation functions provided as arguments
174 + * should similarly return null to indicate the lack of any result
175 + * (in which case it is not used). In the case of mapped
176 + * reductions, this also enables transformations to serve as
177 + * filters, returning null (or, in the case of primitive
178 + * specializations, the identity basis) if the element should not
179 + * be combined. You can create compound transformations and
180 + * filterings by composing them yourself under this "null means
181 + * there is nothing there now" rule before using them in search or
182 + * reduce operations.
183 + *
184 + * <p>Methods accepting and/or returning Entry arguments maintain
185 + * key-value associations. They may be useful for example when
186 + * finding the key for the greatest value. Note that "plain" Entry
187 + * arguments can be supplied using {@code new
188 + * AbstractMap.SimpleEntry(k,v)}.
189 + *
190 + * <p>Bulk operations may complete abruptly, throwing an
191 + * exception encountered in the application of a supplied
192 + * function. Bear in mind when handling such exceptions that other
193 + * concurrently executing functions could also have thrown
194 + * exceptions, or would have done so if the first exception had
195 + * not occurred.
196 + *
197 + * <p>Speedups for parallel compared to sequential forms are common
198 + * but not guaranteed.  Parallel operations involving brief functions
199 + * on small maps may execute more slowly than sequential forms if the
200 + * underlying work to parallelize the computation is more expensive
201 + * than the computation itself.  Similarly, parallelization may not
202 + * lead to much actual parallelism if all processors are busy
203 + * performing unrelated tasks.
204 + *
205 + * <p>All arguments to all task methods must be non-null.
206 + *
207 + * <p><em>jsr166e note: During transition, this class
208 + * uses nested functional interfaces with different names but the
209 + * same forms as those expected for JDK8.</em>
210 + *
211   * <p>This class is a member of the
212   * <a href="{@docRoot}/../technotes/guides/collections/index.html">
213   * Java Collections Framework</a>.
214   *
96 * <p><em>jsr166e note: This class is a candidate replacement for
97 * java.util.concurrent.ConcurrentHashMap.  During transition, this
98 * class declares and uses nested functional interfaces with different
99 * names but the same forms as those expected for JDK8.<em>
100 *
215   * @since 1.5
216   * @author Doug Lea
217   * @param <K> the type of keys maintained by this map
218   * @param <V> the type of mapped values
219   */
220 < public class ConcurrentHashMapV8<K, V>
221 <    implements ConcurrentMap<K, V>, Serializable {
220 > public class ConcurrentHashMapV8<K,V> extends AbstractMap<K,V>
221 >    implements ConcurrentMap<K,V>, Serializable {
222      private static final long serialVersionUID = 7249069246763182397L;
223  
224      /**
225 <     * A partitionable iterator. A Spliterator can be traversed
226 <     * directly, but can also be partitioned (before traversal) by
227 <     * creating another Spliterator that covers a non-overlapping
114 <     * portion of the elements, and so may be amenable to parallel
115 <     * execution.
116 <     *
117 <     * <p> This interface exports a subset of expected JDK8
118 <     * functionality.
119 <     *
120 <     * <p>Sample usage: Here is one (of the several) ways to compute
121 <     * the sum of the values held in a map using the ForkJoin
122 <     * framework. As illustrated here, Spliterators are well suited to
123 <     * designs in which a task repeatedly splits off half its work
124 <     * into forked subtasks until small enough to process directly,
125 <     * and then joins these subtasks. Variants of this style can also
126 <     * be used in completion-based designs.
127 <     *
128 <     * <pre>
129 <     * {@code ConcurrentHashMapV8<String, Long> m = ...
130 <     * // split as if have 8 * parallelism, for load balance
131 <     * int n = m.size();
132 <     * int p = aForkJoinPool.getParallelism() * 8;
133 <     * int split = (n < p)? n : p;
134 <     * long sum = aForkJoinPool.invoke(new SumValues(m.valueSpliterator(), split, null));
135 <     * // ...
136 <     * static class SumValues extends RecursiveTask<Long> {
137 <     *   final Spliterator<Long> s;
138 <     *   final int split;             // split while > 1
139 <     *   final SumValues nextJoin;    // records forked subtasks to join
140 <     *   SumValues(Spliterator<Long> s, int depth, SumValues nextJoin) {
141 <     *     this.s = s; this.depth = depth; this.nextJoin = nextJoin;
142 <     *   }
143 <     *   public Long compute() {
144 <     *     long sum = 0;
145 <     *     SumValues subtasks = null; // fork subtasks
146 <     *     for (int s = split >>> 1; s > 0; s >>>= 1)
147 <     *       (subtasks = new SumValues(s.split(), s, subtasks)).fork();
148 <     *     while (s.hasNext())        // directly process remaining elements
149 <     *       sum += s.next();
150 <     *     for (SumValues t = subtasks; t != null; t = t.nextJoin)
151 <     *       sum += t.join();         // collect subtask results
152 <     *     return sum;
153 <     *   }
154 <     * }
155 <     * }</pre>
225 >     * An object for traversing and partitioning elements of a source.
226 >     * This interface provides a subset of the functionality of JDK8
227 >     * java.util.Spliterator.
228       */
229 <    public static interface Spliterator<T> extends Iterator<T> {
229 >    public static interface ConcurrentHashMapSpliterator<T> {
230          /**
231 <         * Returns a Spliterator covering approximately half of the
232 <         * elements, guaranteed not to overlap with those subsequently
233 <         * returned by this Spliterator.  After invoking this method,
234 <         * the current Spliterator will <em>not</em> produce any of
235 <         * the elements of the returned Spliterator, but the two
236 <         * Spliterators together will produce all of the elements that
237 <         * would have been produced by this Spliterator had this
238 <         * method not been called. The exact number of elements
239 <         * produced by the returned Spliterator is not guaranteed, and
168 <         * may be zero (i.e., with {@code hasNext()} reporting {@code
169 <         * false}) if this Spliterator cannot be further split.
170 <         *
171 <         * @return a Spliterator covering approximately half of the
172 <         * elements
173 <         * @throws IllegalStateException if this Spliterator has
174 <         * already commenced traversing elements
231 >         * If possible, returns a new spliterator covering
232 >         * approximately one half of the elements, which will not be
233 >         * covered by this spliterator. Returns null if cannot be
234 >         * split.
235 >         */
236 >        ConcurrentHashMapSpliterator<T> trySplit();
237 >        /**
238 >         * Returns an estimate of the number of elements covered by
239 >         * this Spliterator.
240           */
241 <        Spliterator<T> split();
241 >        long estimateSize();
242 >
243 >        /** Applies the action to each untraversed element */
244 >        void forEachRemaining(Action<? super T> action);
245 >        /** If an element remains, applies the action and returns true. */
246 >        boolean tryAdvance(Action<? super T> action);
247      }
248  
249 +    // Sams
250 +    /** Interface describing a void action of one argument */
251 +    public interface Action<A> { void apply(A a); }
252 +    /** Interface describing a void action of two arguments */
253 +    public interface BiAction<A,B> { void apply(A a, B b); }
254 +    /** Interface describing a function of one argument */
255 +    public interface Fun<A,T> { T apply(A a); }
256 +    /** Interface describing a function of two arguments */
257 +    public interface BiFun<A,B,T> { T apply(A a, B b); }
258 +    /** Interface describing a function mapping its argument to a double */
259 +    public interface ObjectToDouble<A> { double apply(A a); }
260 +    /** Interface describing a function mapping its argument to a long */
261 +    public interface ObjectToLong<A> { long apply(A a); }
262 +    /** Interface describing a function mapping its argument to an int */
263 +    public interface ObjectToInt<A> {int apply(A a); }
264 +    /** Interface describing a function mapping two arguments to a double */
265 +    public interface ObjectByObjectToDouble<A,B> { double apply(A a, B b); }
266 +    /** Interface describing a function mapping two arguments to a long */
267 +    public interface ObjectByObjectToLong<A,B> { long apply(A a, B b); }
268 +    /** Interface describing a function mapping two arguments to an int */
269 +    public interface ObjectByObjectToInt<A,B> {int apply(A a, B b); }
270 +    /** Interface describing a function mapping two doubles to a double */
271 +    public interface DoubleByDoubleToDouble { double apply(double a, double b); }
272 +    /** Interface describing a function mapping two longs to a long */
273 +    public interface LongByLongToLong { long apply(long a, long b); }
274 +    /** Interface describing a function mapping two ints to an int */
275 +    public interface IntByIntToInt { int apply(int a, int b); }
276 +
277 +
278      /*
279       * Overview:
280       *
# Line 186 | 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 205 | 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
214 <     * purposes -- they are available anyway because of addressing
215 <     * constraints.  As explained further below, these top bits are
216 <     * used as follows:
217 <     *  00 - Normal
218 <     *  01 - Locked
219 <     *  11 - Locked and may have a thread waiting for lock
220 <     *  10 - Node is a forwarding node
221 <     *
222 <     * The lower 30 bits of each Node's hash field contain a
223 <     * transformation of the key's hash code, except for forwarding
224 <     * nodes, for which the lower bits are zero (and so always have
225 <     * hash field == MOVED).
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 231 | 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
236 <     * construction, so we overlay these by using bits of the Node
237 <     * hash field for lock control (see above), and so normally use
238 <     * builtin monitors only for blocking and signalling using
239 <     * wait/notifyAll constructions. See Node.tryAwaitLock.
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,
247 <     * operations that only conditionally update may inspect nodes
248 <     * until the point of update. This is a converse of sorts to the
249 <     * 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 279 | 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
288 <     * Comparable.  These TreeBins use a balanced tree to hold nodes
289 <     * (a specialized form of red-black trees), bounding search time
290 <     * 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 298 | 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 array.
384 >     * However, rather than stalling, these other threads may proceed
385 >     * with insertions etc.  The use of TreeBins shields us from the
386 >     * 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. However, threads claim small
389 >     * blocks of indices to transfer (via field transferIndex) before
390 >     * doing so, reducing contention.  A generation stamp in field
391 >     * sizeCtl ensures that resizings do not overlap. Because we are
392 >     * using power-of-two expansion, the elements from each bin must
393 >     * either stay at same index, or move with a power of two
394 >     * offset. We eliminate unnecessary node creation by catching
395 >     * cases where old nodes can be reused because their next fields
396 >     * won't change.  On average, only about one-sixth of them need
397 >     * cloning when a table doubles. The nodes they replace will be
398 >     * garbage collectable as soon as they are no longer referenced by
399 >     * any reader thread that may be in the midst of concurrently
400 >     * traversing table.  Upon transfer, the old table bin contains
401 >     * only a special forwarding node (with hash field "MOVED") that
402 >     * contains the next table as its key. On encountering a
403 >     * forwarding node, access and update operations restart, using
404 >     * the new table.
405 >     *
406 >     * Each bin transfer requires its bin lock, which can stall
407 >     * waiting for locks while resizing. However, because other
408 >     * threads can join in and help resize rather than contend for
409 >     * locks, average aggregate waits become shorter as resizing
410 >     * progresses.  The transfer operation must also ensure that all
411 >     * accessible bins in both the old and new table are usable by any
412 >     * traversal.  This is arranged in part by proceeding from the
413 >     * last bin (table.length - 1) up towards the first.  Upon seeing
414 >     * a forwarding node, traversals (see class Traverser) arrange to
415 >     * move to the new table without revisiting nodes.  To ensure that
416 >     * no intervening nodes are skipped even when moved out of order,
417 >     * a stack (see class TableStack) is created on first encounter of
418 >     * a forwarding node during a traversal, to maintain its place if
419 >     * later processing the current table. The need for these
420 >     * save/restore mechanics is relatively rare, but when one
421 >     * forwarding node is encountered, typically many more will be.
422 >     * So Traversers use a simple caching scheme to avoid creating so
423 >     * many new TableStack nodes. (Thanks to Peter Levart for
424 >     * suggesting use of a stack here.)
425       *
426       * The traversal scheme also applies to partial traversals of
427       * ranges of bins (via an alternate Traverser constructor)
# Line 349 | Line 436 | public class ConcurrentHashMapV8<K, V>
436       * These cases attempt to override the initial capacity settings,
437       * but harmlessly fail to take effect in cases of races.
438       *
439 <     * The element count is maintained using a LongAdder, which avoids
440 <     * contention on updates but can encounter cache thrashing if read
441 <     * too frequently during concurrent access. To avoid reading so
442 <     * often, resizing is attempted either when a bin lock is
443 <     * contended, or upon adding to a bin already holding two or more
444 <     * nodes (checked before adding in the xIfAbsent methods, after
445 <     * adding in others). Under uniform hash distributions, the
446 <     * probability of this occurring at threshold is around 13%,
447 <     * meaning that only about 1 in 8 puts check threshold (and after
448 <     * resizing, many fewer do so). But this approximation has high
449 <     * variance for small table sizes, so we check on any collision
450 <     * for sizes <= 64. The bulk putAll operation further reduces
451 <     * contention by only committing count updates upon these size
452 <     * checks.
439 >     * The element count is maintained using a specialization of
440 >     * LongAdder. We need to incorporate a specialization rather than
441 >     * just use a LongAdder in order to access implicit
442 >     * contention-sensing that leads to creation of multiple
443 >     * CounterCells.  The counter mechanics avoid contention on
444 >     * updates but can encounter cache thrashing if read too
445 >     * frequently during concurrent access. To avoid reading so often,
446 >     * resizing under contention is attempted only upon adding to a
447 >     * bin already holding two or more nodes. Under uniform hash
448 >     * distributions, the probability of this occurring at threshold
449 >     * is around 13%, meaning that only about 1 in 8 puts check
450 >     * threshold (and after resizing, many fewer do so).
451 >     *
452 >     * TreeBins use a special form of comparison for search and
453 >     * related operations (which is the main reason we cannot use
454 >     * existing collections such as TreeMaps). TreeBins contain
455 >     * Comparable elements, but may contain others, as well as
456 >     * elements that are Comparable but not necessarily Comparable for
457 >     * the same T, so we cannot invoke compareTo among them. To handle
458 >     * this, the tree is ordered primarily by hash value, then by
459 >     * Comparable.compareTo order if applicable.  On lookup at a node,
460 >     * if elements are not comparable or compare as 0 then both left
461 >     * and right children may need to be searched in the case of tied
462 >     * hash values. (This corresponds to the full list search that
463 >     * would be necessary if all elements were non-Comparable and had
464 >     * tied hashes.) On insertion, to keep a total ordering (or as
465 >     * close as is required here) across rebalancings, we compare
466 >     * classes and identityHashCodes as tie-breakers. The red-black
467 >     * balancing code is updated from pre-jdk-collections
468 >     * (http://gee.cs.oswego.edu/dl/classes/collections/RBCell.java)
469 >     * based in turn on Cormen, Leiserson, and Rivest "Introduction to
470 >     * Algorithms" (CLR).
471 >     *
472 >     * TreeBins also require an additional locking mechanism.  While
473 >     * list traversal is always possible by readers even during
474 >     * updates, tree traversal is not, mainly because of tree-rotations
475 >     * that may change the root node and/or its linkages.  TreeBins
476 >     * include a simple read-write lock mechanism parasitic on the
477 >     * main bin-synchronization strategy: Structural adjustments
478 >     * associated with an insertion or removal are already bin-locked
479 >     * (and so cannot conflict with other writers) but must wait for
480 >     * ongoing readers to finish. Since there can be only one such
481 >     * waiter, we use a simple scheme using a single "waiter" field to
482 >     * block writers.  However, readers need never block.  If the root
483 >     * lock is held, they proceed along the slow traversal path (via
484 >     * next-pointers) until the lock becomes available or the list is
485 >     * exhausted, whichever comes first. These cases are not fast, but
486 >     * maximize aggregate expected throughput.
487       *
488       * Maintaining API and serialization compatibility with previous
489       * versions of this class introduces several oddities. Mainly: We
490 <     * leave untouched but unused constructor arguments refering to
490 >     * leave untouched but unused constructor arguments referring to
491       * concurrencyLevel. We accept a loadFactor constructor argument,
492       * but apply it only to initial table capacity (which is the only
493       * time that we can guarantee to honor it.) We also declare an
494       * unused "Segment" class that is instantiated in minimal form
495       * only when serializing.
496 +     *
497 +     * Also, solely for compatibility with previous versions of this
498 +     * class, it extends AbstractMap, even though all of its methods
499 +     * are overridden, so it is just useless baggage.
500 +     *
501 +     * This file is organized to make things a little easier to follow
502 +     * while reading than they might otherwise: First the main static
503 +     * declarations and utilities, then fields, then main public
504 +     * methods (with a few factorings of multiple public methods into
505 +     * internal ones), then sizing methods, trees, traversers, and
506 +     * bulk operations.
507       */
508  
509      /* ---------------- Constants -------------- */
# Line 413 | Line 545 | public class ConcurrentHashMapV8<K, V>
545      private static final float LOAD_FACTOR = 0.75f;
546  
547      /**
548 <     * The buffer size for skipped bins during transfers. The
549 <     * value is arbitrary but should be large enough to avoid
550 <     * most locking stalls during resizes.
548 >     * The bin count threshold for using a tree rather than list for a
549 >     * bin.  Bins are converted to trees when adding an element to a
550 >     * bin with at least this many nodes. The value must be greater
551 >     * than 2, and should be at least 8 to mesh with assumptions in
552 >     * tree removal about conversion back to plain bins upon
553 >     * shrinkage.
554       */
555 <    private static final int TRANSFER_BUFFER_SIZE = 32;
555 >    static final int TREEIFY_THRESHOLD = 8;
556  
557      /**
558 <     * The bin count threshold for using a tree rather than list for a
559 <     * bin.  The value reflects the approximate break-even point for
560 <     * using tree-based operations.
558 >     * The bin count threshold for untreeifying a (split) bin during a
559 >     * resize operation. Should be less than TREEIFY_THRESHOLD, and at
560 >     * most 6 to mesh with shrinkage detection under removal.
561       */
562 <    private static final int TREE_THRESHOLD = 8;
562 >    static final int UNTREEIFY_THRESHOLD = 6;
563  
564 <    /*
565 <     * Encodings for special uses of Node hash fields. See above for
566 <     * explanation.
564 >    /**
565 >     * The smallest table capacity for which bins may be treeified.
566 >     * (Otherwise the table is resized if too many nodes in a bin.)
567 >     * The value should be at least 4 * TREEIFY_THRESHOLD to avoid
568 >     * conflicts between resizing and treeification thresholds.
569       */
570 <    static final int MOVED     = 0x80000000; // hash field for forwarding nodes
434 <    static final int LOCKED    = 0x40000000; // set/tested only as a bit
435 <    static final int WAITING   = 0xc0000000; // both bits set/tested together
436 <    static final int HASH_BITS = 0x3fffffff; // usable bits of normal node hash
437 <
438 <    /* ---------------- Fields -------------- */
570 >    static final int MIN_TREEIFY_CAPACITY = 64;
571  
572      /**
573 <     * The array of bins. Lazily initialized upon first insertion.
574 <     * Size is always a power of two. Accessed directly by iterators.
573 >     * Minimum number of rebinnings per transfer step. Ranges are
574 >     * subdivided to allow multiple resizer threads.  This value
575 >     * serves as a lower bound to avoid resizers encountering
576 >     * excessive memory contention.  The value should be at least
577 >     * DEFAULT_CAPACITY.
578       */
579 <    transient volatile Node[] table;
579 >    private static final int MIN_TRANSFER_STRIDE = 16;
580  
581      /**
582 <     * The counter maintaining number of elements.
582 >     * The number of bits used for generation stamp in sizeCtl.
583 >     * Must be at least 6 for 32bit arrays.
584       */
585 <    private transient final LongAdder counter;
585 >    private static int RESIZE_STAMP_BITS = 16;
586  
587      /**
588 <     * Table initialization and resizing control.  When negative, the
589 <     * table is being initialized or resized. Otherwise, when table is
454 <     * null, holds the initial table size to use upon creation, or 0
455 <     * for default. After initialization, holds the next element count
456 <     * value upon which to resize the table.
588 >     * The maximum number of threads that can help resize.
589 >     * Must fit in 32 - RESIZE_STAMP_BITS bits.
590       */
591 <    private transient volatile int sizeCtl;
459 <
460 <    // views
461 <    private transient KeySet<K,V> keySet;
462 <    private transient Values<K,V> values;
463 <    private transient EntrySet<K,V> entrySet;
591 >    private static final int MAX_RESIZERS = (1 << (32 - RESIZE_STAMP_BITS)) - 1;
592  
593 <    /** For serialization compatibility. Null unless serialized; see below */
594 <    private Segment<K,V>[] segments;
595 <
596 <    /* ---------------- Table element access -------------- */
593 >    /**
594 >     * The bit shift for recording size stamp in sizeCtl.
595 >     */
596 >    private static final int RESIZE_STAMP_SHIFT = 32 - RESIZE_STAMP_BITS;
597  
598      /*
599 <     * Volatile access methods are used for table elements as well as
472 <     * elements of in-progress next table while resizing.  Uses are
473 <     * null checked by callers, and implicitly bounds-checked, relying
474 <     * on the invariants that tab arrays have non-zero size, and all
475 <     * indices are masked with (tab.length - 1) which is never
476 <     * negative and always less than length. Note that, to be correct
477 <     * wrt arbitrary concurrency errors by users, bounds checks must
478 <     * operate on local variables, which accounts for some odd-looking
479 <     * inline assignments below.
599 >     * Encodings for Node hash fields. See above for explanation.
600       */
601 <
602 <    static final Node tabAt(Node[] tab, int i) { // used by Iter
603 <        return (Node)UNSAFE.getObjectVolatile(tab, ((long)i<<ASHIFT)+ABASE);
604 <    }
605 <
606 <    private static final boolean casTabAt(Node[] tab, int i, Node c, Node v) {
607 <        return UNSAFE.compareAndSwapObject(tab, ((long)i<<ASHIFT)+ABASE, c, v);
608 <    }
609 <
610 <    private static final void setTabAt(Node[] tab, int i, Node v) {
611 <        UNSAFE.putObjectVolatile(tab, ((long)i<<ASHIFT)+ABASE, v);
612 <    }
601 >    static final int MOVED     = -1; // hash for forwarding nodes
602 >    static final int TREEBIN   = -2; // hash for roots of trees
603 >    static final int RESERVED  = -3; // hash for transient reservations
604 >    static final int HASH_BITS = 0x7fffffff; // usable bits of normal node hash
605 >
606 >    /** Number of CPUS, to place bounds on some sizings */
607 >    static final int NCPU = Runtime.getRuntime().availableProcessors();
608 >
609 >    /** For serialization compatibility. */
610 >    private static final ObjectStreamField[] serialPersistentFields = {
611 >        new ObjectStreamField("segments", Segment[].class),
612 >        new ObjectStreamField("segmentMask", Integer.TYPE),
613 >        new ObjectStreamField("segmentShift", Integer.TYPE)
614 >    };
615  
616      /* ---------------- Nodes -------------- */
617  
618      /**
619 <     * Key-value entry. Note that this is never exported out as a
620 <     * user-visible Map.Entry (see MapEntry below). Nodes with a hash
621 <     * field of MOVED are special, and do not contain user keys or
622 <     * values.  Otherwise, keys are never null, and null val fields
623 <     * indicate that a node is in the process of being deleted or
624 <     * created. For purposes of read-only access, a key may be read
625 <     * before a val, but can only be used after checking val to be
626 <     * non-null.
627 <     */
628 <    static class Node {
629 <        volatile int hash;
630 <        final Object key;
509 <        volatile Object val;
510 <        volatile Node next;
619 >     * Key-value entry.  This class is never exported out as a
620 >     * user-mutable Map.Entry (i.e., one supporting setValue; see
621 >     * MapEntry below), but can be used for read-only traversals used
622 >     * in bulk tasks.  Subclasses of Node with a negative hash field
623 >     * are special, and contain null keys and values (but are never
624 >     * exported).  Otherwise, keys and vals are never null.
625 >     */
626 >    static class Node<K,V> implements Map.Entry<K,V> {
627 >        final int hash;
628 >        final K key;
629 >        volatile V val;
630 >        volatile Node<K,V> next;
631  
632 <        Node(int hash, Object key, Object val, Node next) {
632 >        Node(int hash, K key, V val, Node<K,V> next) {
633              this.hash = hash;
634              this.key = key;
635              this.val = val;
636              this.next = next;
637          }
638  
639 <        /** CompareAndSet the hash field */
640 <        final boolean casHash(int cmp, int val) {
641 <            return UNSAFE.compareAndSwapInt(this, hashOffset, cmp, val);
642 <        }
643 <
644 <        /** The number of spins before blocking for a lock */
525 <        static final int MAX_SPINS =
526 <            Runtime.getRuntime().availableProcessors() > 1 ? 64 : 1;
527 <
528 <        /**
529 <         * Spins a while if LOCKED bit set and this node is the first
530 <         * of its bin, and then sets WAITING bits on hash field and
531 <         * blocks (once) if they are still set.  It is OK for this
532 <         * method to return even if lock is not available upon exit,
533 <         * which enables these simple single-wait mechanics.
534 <         *
535 <         * The corresponding signalling operation is performed within
536 <         * callers: Upon detecting that WAITING has been set when
537 <         * unlocking lock (via a failed CAS from non-waiting LOCKED
538 <         * state), unlockers acquire the sync lock and perform a
539 <         * notifyAll.
540 <         */
541 <        final void tryAwaitLock(Node[] tab, int i) {
542 <            if (tab != null && i >= 0 && i < tab.length) { // bounds check
543 <                int r = ThreadLocalRandom.current().nextInt(); // randomize spins
544 <                int spins = MAX_SPINS, h;
545 <                while (tabAt(tab, i) == this && ((h = hash) & LOCKED) != 0) {
546 <                    if (spins >= 0) {
547 <                        r ^= r << 1; r ^= r >>> 3; r ^= r << 10; // xorshift
548 <                        if (r >= 0 && --spins == 0)
549 <                            Thread.yield();  // yield before block
550 <                    }
551 <                    else if (casHash(h, h | WAITING)) {
552 <                        synchronized (this) {
553 <                            if (tabAt(tab, i) == this &&
554 <                                (hash & WAITING) == WAITING) {
555 <                                try {
556 <                                    wait();
557 <                                } catch (InterruptedException ie) {
558 <                                    Thread.currentThread().interrupt();
559 <                                }
560 <                            }
561 <                            else
562 <                                notifyAll(); // possibly won race vs signaller
563 <                        }
564 <                        break;
565 <                    }
566 <                }
567 <            }
568 <        }
569 <
570 <        // Unsafe mechanics for casHash
571 <        private static final sun.misc.Unsafe UNSAFE;
572 <        private static final long hashOffset;
573 <
574 <        static {
575 <            try {
576 <                UNSAFE = getUnsafe();
577 <                Class<?> k = Node.class;
578 <                hashOffset = UNSAFE.objectFieldOffset
579 <                    (k.getDeclaredField("hash"));
580 <            } catch (Exception e) {
581 <                throw new Error(e);
582 <            }
583 <        }
584 <    }
585 <
586 <    /* ---------------- TreeBins -------------- */
587 <
588 <    /**
589 <     * Nodes for use in TreeBins
590 <     */
591 <    static final class TreeNode extends Node {
592 <        TreeNode parent;  // red-black tree links
593 <        TreeNode left;
594 <        TreeNode right;
595 <        TreeNode prev;    // needed to unlink next upon deletion
596 <        boolean red;
597 <
598 <        TreeNode(int hash, Object key, Object val, Node next, TreeNode parent) {
599 <            super(hash, key, val, next);
600 <            this.parent = parent;
601 <        }
602 <    }
603 <
604 <    /**
605 <     * A specialized form of red-black tree for use in bins
606 <     * whose size exceeds a threshold.
607 <     *
608 <     * TreeBins use a special form of comparison for search and
609 <     * related operations (which is the main reason we cannot use
610 <     * existing collections such as TreeMaps). TreeBins contain
611 <     * Comparable elements, but may contain others, as well as
612 <     * elements that are Comparable but not necessarily Comparable<T>
613 <     * for the same T, so we cannot invoke compareTo among them. To
614 <     * handle this, the tree is ordered primarily by hash value, then
615 <     * by getClass().getName() order, and then by Comparator order
616 <     * among elements of the same class.  On lookup at a node, if
617 <     * elements are not comparable or compare as 0, both left and
618 <     * right children may need to be searched in the case of tied hash
619 <     * values. (This corresponds to the full list search that would be
620 <     * necessary if all elements were non-Comparable and had tied
621 <     * hashes.)  The red-black balancing code is updated from
622 <     * pre-jdk-collections
623 <     * (http://gee.cs.oswego.edu/dl/classes/collections/RBCell.java)
624 <     * based in turn on Cormen, Leiserson, and Rivest "Introduction to
625 <     * Algorithms" (CLR).
626 <     *
627 <     * TreeBins also maintain a separate locking discipline than
628 <     * regular bins. Because they are forwarded via special MOVED
629 <     * nodes at bin heads (which can never change once established),
630 <     * we cannot use those nodes as locks. Instead, TreeBin
631 <     * extends AbstractQueuedSynchronizer to support a simple form of
632 <     * read-write lock. For update operations and table validation,
633 <     * the exclusive form of lock behaves in the same way as bin-head
634 <     * locks. However, lookups use shared read-lock mechanics to allow
635 <     * multiple readers in the absence of writers.  Additionally,
636 <     * these lookups do not ever block: While the lock is not
637 <     * available, they proceed along the slow traversal path (via
638 <     * next-pointers) until the lock becomes available or the list is
639 <     * exhausted, whichever comes first. (These cases are not fast,
640 <     * but maximize aggregate expected throughput.)  The AQS mechanics
641 <     * for doing this are straightforward.  The lock state is held as
642 <     * AQS getState().  Read counts are negative; the write count (1)
643 <     * is positive.  There are no signalling preferences among readers
644 <     * and writers. Since we don't need to export full Lock API, we
645 <     * just override the minimal AQS methods and use them directly.
646 <     */
647 <    static final class TreeBin extends AbstractQueuedSynchronizer {
648 <        private static final long serialVersionUID = 2249069246763182397L;
649 <        transient TreeNode root;  // root of tree
650 <        transient TreeNode first; // head of next-pointer list
651 <
652 <        /* AQS overrides */
653 <        public final boolean isHeldExclusively() { return getState() > 0; }
654 <        public final boolean tryAcquire(int ignore) {
655 <            if (compareAndSetState(0, 1)) {
656 <                setExclusiveOwnerThread(Thread.currentThread());
657 <                return true;
658 <            }
659 <            return false;
660 <        }
661 <        public final boolean tryRelease(int ignore) {
662 <            setExclusiveOwnerThread(null);
663 <            setState(0);
664 <            return true;
665 <        }
666 <        public final int tryAcquireShared(int ignore) {
667 <            for (int c;;) {
668 <                if ((c = getState()) > 0)
669 <                    return -1;
670 <                if (compareAndSetState(c, c -1))
671 <                    return 1;
672 <            }
673 <        }
674 <        public final boolean tryReleaseShared(int ignore) {
675 <            int c;
676 <            do {} while (!compareAndSetState(c = getState(), c + 1));
677 <            return c == -1;
678 <        }
679 <
680 <        /** From CLR */
681 <        private void rotateLeft(TreeNode p) {
682 <            if (p != null) {
683 <                TreeNode r = p.right, pp, rl;
684 <                if ((rl = p.right = r.left) != null)
685 <                    rl.parent = p;
686 <                if ((pp = r.parent = p.parent) == null)
687 <                    root = r;
688 <                else if (pp.left == p)
689 <                    pp.left = r;
690 <                else
691 <                    pp.right = r;
692 <                r.left = p;
693 <                p.parent = r;
694 <            }
695 <        }
696 <
697 <        /** From CLR */
698 <        private void rotateRight(TreeNode p) {
699 <            if (p != null) {
700 <                TreeNode l = p.left, pp, lr;
701 <                if ((lr = p.left = l.right) != null)
702 <                    lr.parent = p;
703 <                if ((pp = l.parent = p.parent) == null)
704 <                    root = l;
705 <                else if (pp.right == p)
706 <                    pp.right = l;
707 <                else
708 <                    pp.left = l;
709 <                l.right = p;
710 <                p.parent = l;
711 <            }
712 <        }
713 <
714 <        /**
715 <         * Returns the TreeNode (or null if not found) for the given key
716 <         * starting at given root.
717 <         */
718 <        @SuppressWarnings("unchecked") // suppress Comparable cast warning
719 <            final TreeNode getTreeNode(int h, Object k, TreeNode p) {
720 <            Class<?> c = k.getClass();
721 <            while (p != null) {
722 <                int dir, ph;  Object pk; Class<?> pc;
723 <                if ((ph = p.hash) == h) {
724 <                    if ((pk = p.key) == k || k.equals(pk))
725 <                        return p;
726 <                    if (c != (pc = pk.getClass()) ||
727 <                        !(k instanceof Comparable) ||
728 <                        (dir = ((Comparable)k).compareTo((Comparable)pk)) == 0) {
729 <                        dir = (c == pc) ? 0 : c.getName().compareTo(pc.getName());
730 <                        TreeNode r = null, s = null, pl, pr;
731 <                        if (dir >= 0) {
732 <                            if ((pl = p.left) != null && h <= pl.hash)
733 <                                s = pl;
734 <                        }
735 <                        else if ((pr = p.right) != null && h >= pr.hash)
736 <                            s = pr;
737 <                        if (s != null && (r = getTreeNode(h, k, s)) != null)
738 <                            return r;
739 <                    }
740 <                }
741 <                else
742 <                    dir = (h < ph) ? -1 : 1;
743 <                p = (dir > 0) ? p.right : p.left;
744 <            }
745 <            return null;
639 >        public final K getKey()     { return key; }
640 >        public final V getValue()   { return val; }
641 >        public final int hashCode() { return key.hashCode() ^ val.hashCode(); }
642 >        public final String toString() { return key + "=" + val; }
643 >        public final V setValue(V value) {
644 >            throw new UnsupportedOperationException();
645          }
646  
647 <        /**
648 <         * Wrapper for getTreeNode used by CHM.get. Tries to obtain
649 <         * read-lock to call getTreeNode, but during failure to get
650 <         * lock, searches along next links.
651 <         */
652 <        final Object getValue(int h, Object k) {
653 <            Node r = null;
755 <            int c = getState(); // Must read lock state first
756 <            for (Node e = first; e != null; e = e.next) {
757 <                if (c <= 0 && compareAndSetState(c, c - 1)) {
758 <                    try {
759 <                        r = getTreeNode(h, k, root);
760 <                    } finally {
761 <                        releaseShared(0);
762 <                    }
763 <                    break;
764 <                }
765 <                else if ((e.hash & HASH_BITS) == h && k.equals(e.key)) {
766 <                    r = e;
767 <                    break;
768 <                }
769 <                else
770 <                    c = getState();
771 <            }
772 <            return r == null ? null : r.val;
647 >        public final boolean equals(Object o) {
648 >            Object k, v, u; Map.Entry<?,?> e;
649 >            return ((o instanceof Map.Entry) &&
650 >                    (k = (e = (Map.Entry<?,?>)o).getKey()) != null &&
651 >                    (v = e.getValue()) != null &&
652 >                    (k == key || k.equals(key)) &&
653 >                    (v == (u = val) || v.equals(u)));
654          }
655  
656          /**
657 <         * Finds or adds a node.
777 <         * @return null if added
657 >         * Virtualized support for map.get(); overridden in subclasses.
658           */
659 <        @SuppressWarnings("unchecked") // suppress Comparable cast warning
660 <            final TreeNode putTreeNode(int h, Object k, Object v) {
661 <            Class<?> c = k.getClass();
662 <            TreeNode pp = root, p = null;
663 <            int dir = 0;
664 <            while (pp != null) { // find existing node or leaf to insert at
665 <                int ph;  Object pk; Class<?> pc;
666 <                p = pp;
667 <                if ((ph = p.hash) == h) {
788 <                    if ((pk = p.key) == k || k.equals(pk))
789 <                        return p;
790 <                    if (c != (pc = pk.getClass()) ||
791 <                        !(k instanceof Comparable) ||
792 <                        (dir = ((Comparable)k).compareTo((Comparable)pk)) == 0) {
793 <                        dir = (c == pc) ? 0 : c.getName().compareTo(pc.getName());
794 <                        TreeNode r = null, s = null, pl, pr;
795 <                        if (dir >= 0) {
796 <                            if ((pl = p.left) != null && h <= pl.hash)
797 <                                s = pl;
798 <                        }
799 <                        else if ((pr = p.right) != null && h >= pr.hash)
800 <                            s = pr;
801 <                        if (s != null && (r = getTreeNode(h, k, s)) != null)
802 <                            return r;
803 <                    }
804 <                }
805 <                else
806 <                    dir = (h < ph) ? -1 : 1;
807 <                pp = (dir > 0) ? p.right : p.left;
808 <            }
809 <
810 <            TreeNode f = first;
811 <            TreeNode x = first = new TreeNode(h, k, v, f, p);
812 <            if (p == null)
813 <                root = x;
814 <            else { // attach and rebalance; adapted from CLR
815 <                TreeNode xp, xpp;
816 <                if (f != null)
817 <                    f.prev = x;
818 <                if (dir <= 0)
819 <                    p.left = x;
820 <                else
821 <                    p.right = x;
822 <                x.red = true;
823 <                while (x != null && (xp = x.parent) != null && xp.red &&
824 <                       (xpp = xp.parent) != null) {
825 <                    TreeNode xppl = xpp.left;
826 <                    if (xp == xppl) {
827 <                        TreeNode y = xpp.right;
828 <                        if (y != null && y.red) {
829 <                            y.red = false;
830 <                            xp.red = false;
831 <                            xpp.red = true;
832 <                            x = xpp;
833 <                        }
834 <                        else {
835 <                            if (x == xp.right) {
836 <                                rotateLeft(x = xp);
837 <                                xpp = (xp = x.parent) == null ? null : xp.parent;
838 <                            }
839 <                            if (xp != null) {
840 <                                xp.red = false;
841 <                                if (xpp != null) {
842 <                                    xpp.red = true;
843 <                                    rotateRight(xpp);
844 <                                }
845 <                            }
846 <                        }
847 <                    }
848 <                    else {
849 <                        TreeNode y = xppl;
850 <                        if (y != null && y.red) {
851 <                            y.red = false;
852 <                            xp.red = false;
853 <                            xpp.red = true;
854 <                            x = xpp;
855 <                        }
856 <                        else {
857 <                            if (x == xp.left) {
858 <                                rotateRight(x = xp);
859 <                                xpp = (xp = x.parent) == null ? null : xp.parent;
860 <                            }
861 <                            if (xp != null) {
862 <                                xp.red = false;
863 <                                if (xpp != null) {
864 <                                    xpp.red = true;
865 <                                    rotateLeft(xpp);
866 <                                }
867 <                            }
868 <                        }
869 <                    }
870 <                }
871 <                TreeNode r = root;
872 <                if (r != null && r.red)
873 <                    r.red = false;
659 >        Node<K,V> find(int h, Object k) {
660 >            Node<K,V> e = this;
661 >            if (k != null) {
662 >                do {
663 >                    K ek;
664 >                    if (e.hash == h &&
665 >                        ((ek = e.key) == k || (ek != null && k.equals(ek))))
666 >                        return e;
667 >                } while ((e = e.next) != null);
668              }
669              return null;
670          }
877
878        /**
879         * Removes the given node, that must be present before this
880         * call.  This is messier than typical red-black deletion code
881         * because we cannot swap the contents of an interior node
882         * with a leaf successor that is pinned by "next" pointers
883         * that are accessible independently of lock. So instead we
884         * swap the tree linkages.
885         */
886        final void deleteTreeNode(TreeNode p) {
887            TreeNode next = (TreeNode)p.next; // unlink traversal pointers
888            TreeNode pred = p.prev;
889            if (pred == null)
890                first = next;
891            else
892                pred.next = next;
893            if (next != null)
894                next.prev = pred;
895            TreeNode replacement;
896            TreeNode pl = p.left;
897            TreeNode pr = p.right;
898            if (pl != null && pr != null) {
899                TreeNode s = pr, sl;
900                while ((sl = s.left) != null) // find successor
901                    s = sl;
902                boolean c = s.red; s.red = p.red; p.red = c; // swap colors
903                TreeNode sr = s.right;
904                TreeNode pp = p.parent;
905                if (s == pr) { // p was s's direct parent
906                    p.parent = s;
907                    s.right = p;
908                }
909                else {
910                    TreeNode sp = s.parent;
911                    if ((p.parent = sp) != null) {
912                        if (s == sp.left)
913                            sp.left = p;
914                        else
915                            sp.right = p;
916                    }
917                    if ((s.right = pr) != null)
918                        pr.parent = s;
919                }
920                p.left = null;
921                if ((p.right = sr) != null)
922                    sr.parent = p;
923                if ((s.left = pl) != null)
924                    pl.parent = s;
925                if ((s.parent = pp) == null)
926                    root = s;
927                else if (p == pp.left)
928                    pp.left = s;
929                else
930                    pp.right = s;
931                replacement = sr;
932            }
933            else
934                replacement = (pl != null) ? pl : pr;
935            TreeNode pp = p.parent;
936            if (replacement == null) {
937                if (pp == null) {
938                    root = null;
939                    return;
940                }
941                replacement = p;
942            }
943            else {
944                replacement.parent = pp;
945                if (pp == null)
946                    root = replacement;
947                else if (p == pp.left)
948                    pp.left = replacement;
949                else
950                    pp.right = replacement;
951                p.left = p.right = p.parent = null;
952            }
953            if (!p.red) { // rebalance, from CLR
954                TreeNode x = replacement;
955                while (x != null) {
956                    TreeNode xp, xpl;
957                    if (x.red || (xp = x.parent) == null) {
958                        x.red = false;
959                        break;
960                    }
961                    if (x == (xpl = xp.left)) {
962                        TreeNode sib = xp.right;
963                        if (sib != null && sib.red) {
964                            sib.red = false;
965                            xp.red = true;
966                            rotateLeft(xp);
967                            sib = (xp = x.parent) == null ? null : xp.right;
968                        }
969                        if (sib == null)
970                            x = xp;
971                        else {
972                            TreeNode sl = sib.left, sr = sib.right;
973                            if ((sr == null || !sr.red) &&
974                                (sl == null || !sl.red)) {
975                                sib.red = true;
976                                x = xp;
977                            }
978                            else {
979                                if (sr == null || !sr.red) {
980                                    if (sl != null)
981                                        sl.red = false;
982                                    sib.red = true;
983                                    rotateRight(sib);
984                                    sib = (xp = x.parent) == null ? null : xp.right;
985                                }
986                                if (sib != null) {
987                                    sib.red = (xp == null) ? false : xp.red;
988                                    if ((sr = sib.right) != null)
989                                        sr.red = false;
990                                }
991                                if (xp != null) {
992                                    xp.red = false;
993                                    rotateLeft(xp);
994                                }
995                                x = root;
996                            }
997                        }
998                    }
999                    else { // symmetric
1000                        TreeNode sib = xpl;
1001                        if (sib != null && sib.red) {
1002                            sib.red = false;
1003                            xp.red = true;
1004                            rotateRight(xp);
1005                            sib = (xp = x.parent) == null ? null : xp.left;
1006                        }
1007                        if (sib == null)
1008                            x = xp;
1009                        else {
1010                            TreeNode sl = sib.left, sr = sib.right;
1011                            if ((sl == null || !sl.red) &&
1012                                (sr == null || !sr.red)) {
1013                                sib.red = true;
1014                                x = xp;
1015                            }
1016                            else {
1017                                if (sl == null || !sl.red) {
1018                                    if (sr != null)
1019                                        sr.red = false;
1020                                    sib.red = true;
1021                                    rotateLeft(sib);
1022                                    sib = (xp = x.parent) == null ? null : xp.left;
1023                                }
1024                                if (sib != null) {
1025                                    sib.red = (xp == null) ? false : xp.red;
1026                                    if ((sl = sib.left) != null)
1027                                        sl.red = false;
1028                                }
1029                                if (xp != null) {
1030                                    xp.red = false;
1031                                    rotateRight(xp);
1032                                }
1033                                x = root;
1034                            }
1035                        }
1036                    }
1037                }
1038            }
1039            if (p == replacement && (pp = p.parent) != null) {
1040                if (p == pp.left) // detach pointers
1041                    pp.left = null;
1042                else if (p == pp.right)
1043                    pp.right = null;
1044                p.parent = null;
1045            }
1046        }
671      }
672  
673 <    /* ---------------- Collision reduction methods -------------- */
673 >    /* ---------------- Static utilities -------------- */
674  
675      /**
676 <     * Spreads higher bits to lower, and also forces top 2 bits to 0.
677 <     * Because the table uses power-of-two masking, sets of hashes
678 <     * that vary only in bits above the current mask will always
679 <     * collide. (Among known examples are sets of Float keys holding
680 <     * consecutive whole numbers in small tables.)  To counter this,
681 <     * we apply a transform that spreads the impact of higher bits
676 >     * Spreads (XORs) higher bits of hash to lower and also forces top
677 >     * bit to 0. Because the table uses power-of-two masking, sets of
678 >     * hashes that vary only in bits above the current mask will
679 >     * always collide. (Among known examples are sets of Float keys
680 >     * holding consecutive whole numbers in small tables.)  So we
681 >     * apply a transform that spreads the impact of higher bits
682       * downward. There is a tradeoff between speed, utility, and
683       * quality of bit-spreading. Because many common sets of hashes
684 <     * are already reasonably distributed across bits (so don't benefit
685 <     * from spreading), and because we use trees to handle large sets
686 <     * of collisions in bins, we don't need excessively high quality.
684 >     * are already reasonably distributed (so don't benefit from
685 >     * spreading), and because we use trees to handle large sets of
686 >     * collisions in bins, we just XOR some shifted bits in the
687 >     * cheapest possible way to reduce systematic lossage, as well as
688 >     * to incorporate impact of the highest bits that would otherwise
689 >     * never be used in index calculations because of table bounds.
690       */
691 <    private static final int spread(int h) {
692 <        h ^= (h >>> 18) ^ (h >>> 12);
1066 <        return (h ^ (h >>> 10)) & HASH_BITS;
691 >    static final int spread(int h) {
692 >        return (h ^ (h >>> 16)) & HASH_BITS;
693      }
694  
695      /**
696 <     * Replaces a list bin with a tree bin. Call only when locked.
697 <     * Fails to replace if the given key is non-comparable or table
1072 <     * is, or needs, resizing.
696 >     * Returns a power of two table size for the given desired capacity.
697 >     * See Hackers Delight, sec 3.2
698       */
699 <    private final void replaceWithTreeBin(Node[] tab, int index, Object key) {
700 <        if ((key instanceof Comparable) &&
701 <            (tab.length >= MAXIMUM_CAPACITY || counter.sum() < (long)sizeCtl)) {
702 <            TreeBin t = new TreeBin();
703 <            for (Node e = tabAt(tab, index); e != null; e = e.next)
704 <                t.putTreeNode(e.hash & HASH_BITS, e.key, e.val);
705 <            setTabAt(tab, index, new Node(MOVED, t, null, null));
706 <        }
699 >    private static final int tableSizeFor(int c) {
700 >        int n = c - 1;
701 >        n |= n >>> 1;
702 >        n |= n >>> 2;
703 >        n |= n >>> 4;
704 >        n |= n >>> 8;
705 >        n |= n >>> 16;
706 >        return (n < 0) ? 1 : (n >= MAXIMUM_CAPACITY) ? MAXIMUM_CAPACITY : n + 1;
707      }
708  
709 <    /* ---------------- Internal access and update methods -------------- */
710 <
711 <    /** Implementation for get and containsKey */
712 <    private final Object internalGet(Object k) {
713 <        int h = spread(k.hashCode());
714 <        retry: for (Node[] tab = table; tab != null;) {
715 <            Node e, p; Object ek, ev; int eh;      // locals to read fields once
716 <            for (e = tabAt(tab, (tab.length - 1) & h); e != null; e = e.next) {
717 <                if ((eh = e.hash) == MOVED) {
718 <                    if ((ek = e.key) instanceof TreeBin)  // search TreeBin
719 <                        return ((TreeBin)ek).getValue(h, k);
720 <                    else {                        // restart with new table
721 <                        tab = (Node[])ek;
722 <                        continue retry;
723 <                    }
709 >    /**
710 >     * Returns x's Class if it is of the form "class C implements
711 >     * Comparable<C>", else null.
712 >     */
713 >    static Class<?> comparableClassFor(Object x) {
714 >        if (x instanceof Comparable) {
715 >            Class<?> c; Type[] ts, as; Type t; ParameterizedType p;
716 >            if ((c = x.getClass()) == String.class) // bypass checks
717 >                return c;
718 >            if ((ts = c.getGenericInterfaces()) != null) {
719 >                for (int i = 0; i < ts.length; ++i) {
720 >                    if (((t = ts[i]) instanceof ParameterizedType) &&
721 >                        ((p = (ParameterizedType)t).getRawType() ==
722 >                         Comparable.class) &&
723 >                        (as = p.getActualTypeArguments()) != null &&
724 >                        as.length == 1 && as[0] == c) // type arg is c
725 >                        return c;
726                  }
1100                else if ((eh & HASH_BITS) == h && (ev = e.val) != null &&
1101                         ((ek = e.key) == k || k.equals(ek)))
1102                    return ev;
727              }
1104            break;
728          }
729          return null;
730      }
731  
732      /**
733 <     * Implementation for the four public remove/replace methods:
734 <     * Replaces node value with v, conditional upon match of cv if
1112 <     * non-null.  If resulting value is null, delete.
733 >     * Returns k.compareTo(x) if x matches kc (k's screened comparable
734 >     * class), else 0.
735       */
736 <    private final Object internalReplace(Object k, Object v, Object cv) {
737 <        int h = spread(k.hashCode());
738 <        Object oldVal = null;
739 <        for (Node[] tab = table;;) {
1118 <            Node f; int i, fh; Object fk;
1119 <            if (tab == null ||
1120 <                (f = tabAt(tab, i = (tab.length - 1) & h)) == null)
1121 <                break;
1122 <            else if ((fh = f.hash) == MOVED) {
1123 <                if ((fk = f.key) instanceof TreeBin) {
1124 <                    TreeBin t = (TreeBin)fk;
1125 <                    boolean validated = false;
1126 <                    boolean deleted = false;
1127 <                    t.acquire(0);
1128 <                    try {
1129 <                        if (tabAt(tab, i) == f) {
1130 <                            validated = true;
1131 <                            TreeNode p = t.getTreeNode(h, k, t.root);
1132 <                            if (p != null) {
1133 <                                Object pv = p.val;
1134 <                                if (cv == null || cv == pv || cv.equals(pv)) {
1135 <                                    oldVal = pv;
1136 <                                    if ((p.val = v) == null) {
1137 <                                        deleted = true;
1138 <                                        t.deleteTreeNode(p);
1139 <                                    }
1140 <                                }
1141 <                            }
1142 <                        }
1143 <                    } finally {
1144 <                        t.release(0);
1145 <                    }
1146 <                    if (validated) {
1147 <                        if (deleted)
1148 <                            counter.add(-1L);
1149 <                        break;
1150 <                    }
1151 <                }
1152 <                else
1153 <                    tab = (Node[])fk;
1154 <            }
1155 <            else if ((fh & HASH_BITS) != h && f.next == null) // precheck
1156 <                break;                          // rules out possible existence
1157 <            else if ((fh & LOCKED) != 0) {
1158 <                checkForResize();               // try resizing if can't get lock
1159 <                f.tryAwaitLock(tab, i);
1160 <            }
1161 <            else if (f.casHash(fh, fh | LOCKED)) {
1162 <                boolean validated = false;
1163 <                boolean deleted = false;
1164 <                try {
1165 <                    if (tabAt(tab, i) == f) {
1166 <                        validated = true;
1167 <                        for (Node e = f, pred = null;;) {
1168 <                            Object ek, ev;
1169 <                            if ((e.hash & HASH_BITS) == h &&
1170 <                                ((ev = e.val) != null) &&
1171 <                                ((ek = e.key) == k || k.equals(ek))) {
1172 <                                if (cv == null || cv == ev || cv.equals(ev)) {
1173 <                                    oldVal = ev;
1174 <                                    if ((e.val = v) == null) {
1175 <                                        deleted = true;
1176 <                                        Node en = e.next;
1177 <                                        if (pred != null)
1178 <                                            pred.next = en;
1179 <                                        else
1180 <                                            setTabAt(tab, i, en);
1181 <                                    }
1182 <                                }
1183 <                                break;
1184 <                            }
1185 <                            pred = e;
1186 <                            if ((e = e.next) == null)
1187 <                                break;
1188 <                        }
1189 <                    }
1190 <                } finally {
1191 <                    if (!f.casHash(fh | LOCKED, fh)) {
1192 <                        f.hash = fh;
1193 <                        synchronized (f) { f.notifyAll(); };
1194 <                    }
1195 <                }
1196 <                if (validated) {
1197 <                    if (deleted)
1198 <                        counter.add(-1L);
1199 <                    break;
1200 <                }
1201 <            }
1202 <        }
1203 <        return oldVal;
736 >    @SuppressWarnings({"rawtypes","unchecked"}) // for cast to Comparable
737 >    static int compareComparables(Class<?> kc, Object k, Object x) {
738 >        return (x == null || x.getClass() != kc ? 0 :
739 >                ((Comparable)k).compareTo(x));
740      }
741  
742 <    /*
1207 <     * Internal versions of the five insertion methods, each a
1208 <     * little more complicated than the last. All have
1209 <     * the same basic structure as the first (internalPut):
1210 <     *  1. If table uninitialized, create
1211 <     *  2. If bin empty, try to CAS new node
1212 <     *  3. If bin stale, use new table
1213 <     *  4. if bin converted to TreeBin, validate and relay to TreeBin methods
1214 <     *  5. Lock and validate; if valid, scan and add or update
1215 <     *
1216 <     * The others interweave other checks and/or alternative actions:
1217 <     *  * Plain put checks for and performs resize after insertion.
1218 <     *  * putIfAbsent prescans for mapping without lock (and fails to add
1219 <     *    if present), which also makes pre-emptive resize checks worthwhile.
1220 <     *  * computeIfAbsent extends form used in putIfAbsent with additional
1221 <     *    mechanics to deal with, calls, potential exceptions and null
1222 <     *    returns from function call.
1223 <     *  * compute uses the same function-call mechanics, but without
1224 <     *    the prescans
1225 <     *  * putAll attempts to pre-allocate enough table space
1226 <     *    and more lazily performs count updates and checks.
1227 <     *
1228 <     * Someday when details settle down a bit more, it might be worth
1229 <     * some factoring to reduce sprawl.
1230 <     */
1231 <
1232 <    /** Implementation for put */
1233 <    private final Object internalPut(Object k, Object v) {
1234 <        int h = spread(k.hashCode());
1235 <        int count = 0;
1236 <        for (Node[] tab = table;;) {
1237 <            int i; Node f; int fh; Object fk;
1238 <            if (tab == null)
1239 <                tab = initTable();
1240 <            else if ((f = tabAt(tab, i = (tab.length - 1) & h)) == null) {
1241 <                if (casTabAt(tab, i, null, new Node(h, k, v, null)))
1242 <                    break;                   // no lock when adding to empty bin
1243 <            }
1244 <            else if ((fh = f.hash) == MOVED) {
1245 <                if ((fk = f.key) instanceof TreeBin) {
1246 <                    TreeBin t = (TreeBin)fk;
1247 <                    Object oldVal = null;
1248 <                    t.acquire(0);
1249 <                    try {
1250 <                        if (tabAt(tab, i) == f) {
1251 <                            count = 2;
1252 <                            TreeNode p = t.putTreeNode(h, k, v);
1253 <                            if (p != null) {
1254 <                                oldVal = p.val;
1255 <                                p.val = v;
1256 <                            }
1257 <                        }
1258 <                    } finally {
1259 <                        t.release(0);
1260 <                    }
1261 <                    if (count != 0) {
1262 <                        if (oldVal != null)
1263 <                            return oldVal;
1264 <                        break;
1265 <                    }
1266 <                }
1267 <                else
1268 <                    tab = (Node[])fk;
1269 <            }
1270 <            else if ((fh & LOCKED) != 0) {
1271 <                checkForResize();
1272 <                f.tryAwaitLock(tab, i);
1273 <            }
1274 <            else if (f.casHash(fh, fh | LOCKED)) {
1275 <                Object oldVal = null;
1276 <                try {                        // needed in case equals() throws
1277 <                    if (tabAt(tab, i) == f) {
1278 <                        count = 1;
1279 <                        for (Node e = f;; ++count) {
1280 <                            Object ek, ev;
1281 <                            if ((e.hash & HASH_BITS) == h &&
1282 <                                (ev = e.val) != null &&
1283 <                                ((ek = e.key) == k || k.equals(ek))) {
1284 <                                oldVal = ev;
1285 <                                e.val = v;
1286 <                                break;
1287 <                            }
1288 <                            Node last = e;
1289 <                            if ((e = e.next) == null) {
1290 <                                last.next = new Node(h, k, v, null);
1291 <                                if (count >= TREE_THRESHOLD)
1292 <                                    replaceWithTreeBin(tab, i, k);
1293 <                                break;
1294 <                            }
1295 <                        }
1296 <                    }
1297 <                } finally {                  // unlock and signal if needed
1298 <                    if (!f.casHash(fh | LOCKED, fh)) {
1299 <                        f.hash = fh;
1300 <                        synchronized (f) { f.notifyAll(); };
1301 <                    }
1302 <                }
1303 <                if (count != 0) {
1304 <                    if (oldVal != null)
1305 <                        return oldVal;
1306 <                    if (tab.length <= 64)
1307 <                        count = 2;
1308 <                    break;
1309 <                }
1310 <            }
1311 <        }
1312 <        counter.add(1L);
1313 <        if (count > 1)
1314 <            checkForResize();
1315 <        return null;
1316 <    }
1317 <
1318 <    /** Implementation for putIfAbsent */
1319 <    private final Object internalPutIfAbsent(Object k, Object v) {
1320 <        int h = spread(k.hashCode());
1321 <        int count = 0;
1322 <        for (Node[] tab = table;;) {
1323 <            int i; Node f; int fh; Object fk, fv;
1324 <            if (tab == null)
1325 <                tab = initTable();
1326 <            else if ((f = tabAt(tab, i = (tab.length - 1) & h)) == null) {
1327 <                if (casTabAt(tab, i, null, new Node(h, k, v, null)))
1328 <                    break;
1329 <            }
1330 <            else if ((fh = f.hash) == MOVED) {
1331 <                if ((fk = f.key) instanceof TreeBin) {
1332 <                    TreeBin t = (TreeBin)fk;
1333 <                    Object oldVal = null;
1334 <                    t.acquire(0);
1335 <                    try {
1336 <                        if (tabAt(tab, i) == f) {
1337 <                            count = 2;
1338 <                            TreeNode p = t.putTreeNode(h, k, v);
1339 <                            if (p != null)
1340 <                                oldVal = p.val;
1341 <                        }
1342 <                    } finally {
1343 <                        t.release(0);
1344 <                    }
1345 <                    if (count != 0) {
1346 <                        if (oldVal != null)
1347 <                            return oldVal;
1348 <                        break;
1349 <                    }
1350 <                }
1351 <                else
1352 <                    tab = (Node[])fk;
1353 <            }
1354 <            else if ((fh & HASH_BITS) == h && (fv = f.val) != null &&
1355 <                     ((fk = f.key) == k || k.equals(fk)))
1356 <                return fv;
1357 <            else {
1358 <                Node g = f.next;
1359 <                if (g != null) { // at least 2 nodes -- search and maybe resize
1360 <                    for (Node e = g;;) {
1361 <                        Object ek, ev;
1362 <                        if ((e.hash & HASH_BITS) == h && (ev = e.val) != null &&
1363 <                            ((ek = e.key) == k || k.equals(ek)))
1364 <                            return ev;
1365 <                        if ((e = e.next) == null) {
1366 <                            checkForResize();
1367 <                            break;
1368 <                        }
1369 <                    }
1370 <                }
1371 <                if (((fh = f.hash) & LOCKED) != 0) {
1372 <                    checkForResize();
1373 <                    f.tryAwaitLock(tab, i);
1374 <                }
1375 <                else if (tabAt(tab, i) == f && f.casHash(fh, fh | LOCKED)) {
1376 <                    Object oldVal = null;
1377 <                    try {
1378 <                        if (tabAt(tab, i) == f) {
1379 <                            count = 1;
1380 <                            for (Node e = f;; ++count) {
1381 <                                Object ek, ev;
1382 <                                if ((e.hash & HASH_BITS) == h &&
1383 <                                    (ev = e.val) != null &&
1384 <                                    ((ek = e.key) == k || k.equals(ek))) {
1385 <                                    oldVal = ev;
1386 <                                    break;
1387 <                                }
1388 <                                Node last = e;
1389 <                                if ((e = e.next) == null) {
1390 <                                    last.next = new Node(h, k, v, null);
1391 <                                    if (count >= TREE_THRESHOLD)
1392 <                                        replaceWithTreeBin(tab, i, k);
1393 <                                    break;
1394 <                                }
1395 <                            }
1396 <                        }
1397 <                    } finally {
1398 <                        if (!f.casHash(fh | LOCKED, fh)) {
1399 <                            f.hash = fh;
1400 <                            synchronized (f) { f.notifyAll(); };
1401 <                        }
1402 <                    }
1403 <                    if (count != 0) {
1404 <                        if (oldVal != null)
1405 <                            return oldVal;
1406 <                        if (tab.length <= 64)
1407 <                            count = 2;
1408 <                        break;
1409 <                    }
1410 <                }
1411 <            }
1412 <        }
1413 <        counter.add(1L);
1414 <        if (count > 1)
1415 <            checkForResize();
1416 <        return null;
1417 <    }
742 >    /* ---------------- Table element access -------------- */
743  
744 <    /** Implementation for computeIfAbsent */
745 <    private final Object internalComputeIfAbsent(K k,
746 <                                                 Fun<? super K, ?> mf) {
747 <        int h = spread(k.hashCode());
748 <        Object val = null;
749 <        int count = 0;
750 <        for (Node[] tab = table;;) {
751 <            Node f; int i, fh; Object fk, fv;
752 <            if (tab == null)
753 <                tab = initTable();
754 <            else if ((f = tabAt(tab, i = (tab.length - 1) & h)) == null) {
755 <                Node node = new Node(fh = h | LOCKED, k, null, null);
756 <                if (casTabAt(tab, i, null, node)) {
757 <                    count = 1;
758 <                    try {
1434 <                        if ((val = mf.apply(k)) != null)
1435 <                            node.val = val;
1436 <                    } finally {
1437 <                        if (val == null)
1438 <                            setTabAt(tab, i, null);
1439 <                        if (!node.casHash(fh, h)) {
1440 <                            node.hash = h;
1441 <                            synchronized (node) { node.notifyAll(); };
1442 <                        }
1443 <                    }
1444 <                }
1445 <                if (count != 0)
1446 <                    break;
1447 <            }
1448 <            else if ((fh = f.hash) == MOVED) {
1449 <                if ((fk = f.key) instanceof TreeBin) {
1450 <                    TreeBin t = (TreeBin)fk;
1451 <                    boolean added = false;
1452 <                    t.acquire(0);
1453 <                    try {
1454 <                        if (tabAt(tab, i) == f) {
1455 <                            count = 1;
1456 <                            TreeNode p = t.getTreeNode(h, k, t.root);
1457 <                            if (p != null)
1458 <                                val = p.val;
1459 <                            else if ((val = mf.apply(k)) != null) {
1460 <                                added = true;
1461 <                                count = 2;
1462 <                                t.putTreeNode(h, k, val);
1463 <                            }
1464 <                        }
1465 <                    } finally {
1466 <                        t.release(0);
1467 <                    }
1468 <                    if (count != 0) {
1469 <                        if (!added)
1470 <                            return val;
1471 <                        break;
1472 <                    }
1473 <                }
1474 <                else
1475 <                    tab = (Node[])fk;
1476 <            }
1477 <            else if ((fh & HASH_BITS) == h && (fv = f.val) != null &&
1478 <                     ((fk = f.key) == k || k.equals(fk)))
1479 <                return fv;
1480 <            else {
1481 <                Node g = f.next;
1482 <                if (g != null) {
1483 <                    for (Node e = g;;) {
1484 <                        Object ek, ev;
1485 <                        if ((e.hash & HASH_BITS) == h && (ev = e.val) != null &&
1486 <                            ((ek = e.key) == k || k.equals(ek)))
1487 <                            return ev;
1488 <                        if ((e = e.next) == null) {
1489 <                            checkForResize();
1490 <                            break;
1491 <                        }
1492 <                    }
1493 <                }
1494 <                if (((fh = f.hash) & LOCKED) != 0) {
1495 <                    checkForResize();
1496 <                    f.tryAwaitLock(tab, i);
1497 <                }
1498 <                else if (tabAt(tab, i) == f && f.casHash(fh, fh | LOCKED)) {
1499 <                    boolean added = false;
1500 <                    try {
1501 <                        if (tabAt(tab, i) == f) {
1502 <                            count = 1;
1503 <                            for (Node e = f;; ++count) {
1504 <                                Object ek, ev;
1505 <                                if ((e.hash & HASH_BITS) == h &&
1506 <                                    (ev = e.val) != null &&
1507 <                                    ((ek = e.key) == k || k.equals(ek))) {
1508 <                                    val = ev;
1509 <                                    break;
1510 <                                }
1511 <                                Node last = e;
1512 <                                if ((e = e.next) == null) {
1513 <                                    if ((val = mf.apply(k)) != null) {
1514 <                                        added = true;
1515 <                                        last.next = new Node(h, k, val, null);
1516 <                                        if (count >= TREE_THRESHOLD)
1517 <                                            replaceWithTreeBin(tab, i, k);
1518 <                                    }
1519 <                                    break;
1520 <                                }
1521 <                            }
1522 <                        }
1523 <                    } finally {
1524 <                        if (!f.casHash(fh | LOCKED, fh)) {
1525 <                            f.hash = fh;
1526 <                            synchronized (f) { f.notifyAll(); };
1527 <                        }
1528 <                    }
1529 <                    if (count != 0) {
1530 <                        if (!added)
1531 <                            return val;
1532 <                        if (tab.length <= 64)
1533 <                            count = 2;
1534 <                        break;
1535 <                    }
1536 <                }
1537 <            }
1538 <        }
1539 <        if (val != null) {
1540 <            counter.add(1L);
1541 <            if (count > 1)
1542 <                checkForResize();
1543 <        }
1544 <        return val;
1545 <    }
744 >    /*
745 >     * Volatile access methods are used for table elements as well as
746 >     * elements of in-progress next table while resizing.  All uses of
747 >     * the tab arguments must be null checked by callers.  All callers
748 >     * also paranoically precheck that tab's length is not zero (or an
749 >     * equivalent check), thus ensuring that any index argument taking
750 >     * the form of a hash value anded with (length - 1) is a valid
751 >     * index.  Note that, to be correct wrt arbitrary concurrency
752 >     * errors by users, these checks must operate on local variables,
753 >     * which accounts for some odd-looking inline assignments below.
754 >     * Note that calls to setTabAt always occur within locked regions,
755 >     * and so in principle require only release ordering, not
756 >     * full volatile semantics, but are currently coded as volatile
757 >     * writes to be conservative.
758 >     */
759  
1547    /** Implementation for compute */
760      @SuppressWarnings("unchecked")
761 <        private final Object internalCompute(K k, boolean onlyIfPresent,
762 <                                             BiFun<? super K, ? super V, ? extends V> mf) {
1551 <        int h = spread(k.hashCode());
1552 <        Object val = null;
1553 <        int delta = 0;
1554 <        int count = 0;
1555 <        for (Node[] tab = table;;) {
1556 <            Node f; int i, fh; Object fk;
1557 <            if (tab == null)
1558 <                tab = initTable();
1559 <            else if ((f = tabAt(tab, i = (tab.length - 1) & h)) == null) {
1560 <                if (onlyIfPresent)
1561 <                    break;
1562 <                Node node = new Node(fh = h | LOCKED, k, null, null);
1563 <                if (casTabAt(tab, i, null, node)) {
1564 <                    try {
1565 <                        count = 1;
1566 <                        if ((val = mf.apply(k, null)) != null) {
1567 <                            node.val = val;
1568 <                            delta = 1;
1569 <                        }
1570 <                    } finally {
1571 <                        if (delta == 0)
1572 <                            setTabAt(tab, i, null);
1573 <                        if (!node.casHash(fh, h)) {
1574 <                            node.hash = h;
1575 <                            synchronized (node) { node.notifyAll(); };
1576 <                        }
1577 <                    }
1578 <                }
1579 <                if (count != 0)
1580 <                    break;
1581 <            }
1582 <            else if ((fh = f.hash) == MOVED) {
1583 <                if ((fk = f.key) instanceof TreeBin) {
1584 <                    TreeBin t = (TreeBin)fk;
1585 <                    t.acquire(0);
1586 <                    try {
1587 <                        if (tabAt(tab, i) == f) {
1588 <                            count = 1;
1589 <                            TreeNode p = t.getTreeNode(h, k, t.root);
1590 <                            Object pv = (p == null) ? null : p.val;
1591 <                            if ((val = mf.apply(k, (V)pv)) != null) {
1592 <                                if (p != null)
1593 <                                    p.val = val;
1594 <                                else {
1595 <                                    count = 2;
1596 <                                    delta = 1;
1597 <                                    t.putTreeNode(h, k, val);
1598 <                                }
1599 <                            }
1600 <                            else if (p != null) {
1601 <                                delta = -1;
1602 <                                t.deleteTreeNode(p);
1603 <                            }
1604 <                        }
1605 <                    } finally {
1606 <                        t.release(0);
1607 <                    }
1608 <                    if (count != 0)
1609 <                        break;
1610 <                }
1611 <                else
1612 <                    tab = (Node[])fk;
1613 <            }
1614 <            else if ((fh & LOCKED) != 0) {
1615 <                checkForResize();
1616 <                f.tryAwaitLock(tab, i);
1617 <            }
1618 <            else if (f.casHash(fh, fh | LOCKED)) {
1619 <                try {
1620 <                    if (tabAt(tab, i) == f) {
1621 <                        count = 1;
1622 <                        for (Node e = f, pred = null;; ++count) {
1623 <                            Object ek, ev;
1624 <                            if ((e.hash & HASH_BITS) == h &&
1625 <                                (ev = e.val) != null &&
1626 <                                ((ek = e.key) == k || k.equals(ek))) {
1627 <                                val = mf.apply(k, (V)ev);
1628 <                                if (val != null)
1629 <                                    e.val = val;
1630 <                                else {
1631 <                                    delta = -1;
1632 <                                    Node en = e.next;
1633 <                                    if (pred != null)
1634 <                                        pred.next = en;
1635 <                                    else
1636 <                                        setTabAt(tab, i, en);
1637 <                                }
1638 <                                break;
1639 <                            }
1640 <                            pred = e;
1641 <                            if ((e = e.next) == null) {
1642 <                                if (!onlyIfPresent && (val = mf.apply(k, null)) != null) {
1643 <                                    pred.next = new Node(h, k, val, null);
1644 <                                    delta = 1;
1645 <                                    if (count >= TREE_THRESHOLD)
1646 <                                        replaceWithTreeBin(tab, i, k);
1647 <                                }
1648 <                                break;
1649 <                            }
1650 <                        }
1651 <                    }
1652 <                } finally {
1653 <                    if (!f.casHash(fh | LOCKED, fh)) {
1654 <                        f.hash = fh;
1655 <                        synchronized (f) { f.notifyAll(); };
1656 <                    }
1657 <                }
1658 <                if (count != 0) {
1659 <                    if (tab.length <= 64)
1660 <                        count = 2;
1661 <                    break;
1662 <                }
1663 <            }
1664 <        }
1665 <        if (delta != 0) {
1666 <            counter.add((long)delta);
1667 <            if (count > 1)
1668 <                checkForResize();
1669 <        }
1670 <        return val;
761 >    static final <K,V> Node<K,V> tabAt(Node<K,V>[] tab, int i) {
762 >        return (Node<K,V>)U.getObjectVolatile(tab, ((long)i << ASHIFT) + ABASE);
763      }
764  
765 <    private final Object internalMerge(K k, V v,
766 <                                       BiFun<? super V, ? super V, ? extends V> mf) {
767 <        int h = spread(k.hashCode());
1676 <        Object val = null;
1677 <        int delta = 0;
1678 <        int count = 0;
1679 <        for (Node[] tab = table;;) {
1680 <            int i; Node f; int fh; Object fk, fv;
1681 <            if (tab == null)
1682 <                tab = initTable();
1683 <            else if ((f = tabAt(tab, i = (tab.length - 1) & h)) == null) {
1684 <                if (casTabAt(tab, i, null, new Node(h, k, v, null))) {
1685 <                    delta = 1;
1686 <                    val = v;
1687 <                    break;
1688 <                }
1689 <            }
1690 <            else if ((fh = f.hash) == MOVED) {
1691 <                if ((fk = f.key) instanceof TreeBin) {
1692 <                    TreeBin t = (TreeBin)fk;
1693 <                    t.acquire(0);
1694 <                    try {
1695 <                        if (tabAt(tab, i) == f) {
1696 <                            count = 1;
1697 <                            TreeNode p = t.getTreeNode(h, k, t.root);
1698 <                            val = (p == null) ? v : mf.apply((V)p.val, v);
1699 <                            if (val != null) {
1700 <                                if (p != null)
1701 <                                    p.val = val;
1702 <                                else {
1703 <                                    count = 2;
1704 <                                    delta = 1;
1705 <                                    t.putTreeNode(h, k, val);
1706 <                                }
1707 <                            }
1708 <                            else if (p != null) {
1709 <                                delta = -1;
1710 <                                t.deleteTreeNode(p);
1711 <                            }
1712 <                        }
1713 <                    } finally {
1714 <                        t.release(0);
1715 <                    }
1716 <                    if (count != 0)
1717 <                        break;
1718 <                }
1719 <                else
1720 <                    tab = (Node[])fk;
1721 <            }
1722 <            else if ((fh & LOCKED) != 0) {
1723 <                checkForResize();
1724 <                f.tryAwaitLock(tab, i);
1725 <            }
1726 <            else if (f.casHash(fh, fh | LOCKED)) {
1727 <                try {
1728 <                    if (tabAt(tab, i) == f) {
1729 <                        count = 1;
1730 <                        for (Node e = f, pred = null;; ++count) {
1731 <                            Object ek, ev;
1732 <                            if ((e.hash & HASH_BITS) == h &&
1733 <                                (ev = e.val) != null &&
1734 <                                ((ek = e.key) == k || k.equals(ek))) {
1735 <                                val = mf.apply(v, (V)ev);
1736 <                                if (val != null)
1737 <                                    e.val = val;
1738 <                                else {
1739 <                                    delta = -1;
1740 <                                    Node en = e.next;
1741 <                                    if (pred != null)
1742 <                                        pred.next = en;
1743 <                                    else
1744 <                                        setTabAt(tab, i, en);
1745 <                                }
1746 <                                break;
1747 <                            }
1748 <                            pred = e;
1749 <                            if ((e = e.next) == null) {
1750 <                                val = v;
1751 <                                pred.next = new Node(h, k, val, null);
1752 <                                delta = 1;
1753 <                                if (count >= TREE_THRESHOLD)
1754 <                                    replaceWithTreeBin(tab, i, k);
1755 <                                break;
1756 <                            }
1757 <                        }
1758 <                    }
1759 <                } finally {
1760 <                    if (!f.casHash(fh | LOCKED, fh)) {
1761 <                        f.hash = fh;
1762 <                        synchronized (f) { f.notifyAll(); };
1763 <                    }
1764 <                }
1765 <                if (count != 0) {
1766 <                    if (tab.length <= 64)
1767 <                        count = 2;
1768 <                    break;
1769 <                }
1770 <            }
1771 <        }
1772 <        if (delta != 0) {
1773 <            counter.add((long)delta);
1774 <            if (count > 1)
1775 <                checkForResize();
1776 <        }
1777 <        return val;
765 >    static final <K,V> boolean casTabAt(Node<K,V>[] tab, int i,
766 >                                        Node<K,V> c, Node<K,V> v) {
767 >        return U.compareAndSwapObject(tab, ((long)i << ASHIFT) + ABASE, c, v);
768      }
769  
770 <    /** Implementation for putAll */
771 <    private final void internalPutAll(Map<?, ?> m) {
1782 <        tryPresize(m.size());
1783 <        long delta = 0L;     // number of uncommitted additions
1784 <        boolean npe = false; // to throw exception on exit for nulls
1785 <        try {                // to clean up counts on other exceptions
1786 <            for (Map.Entry<?, ?> entry : m.entrySet()) {
1787 <                Object k, v;
1788 <                if (entry == null || (k = entry.getKey()) == null ||
1789 <                    (v = entry.getValue()) == null) {
1790 <                    npe = true;
1791 <                    break;
1792 <                }
1793 <                int h = spread(k.hashCode());
1794 <                for (Node[] tab = table;;) {
1795 <                    int i; Node f; int fh; Object fk;
1796 <                    if (tab == null)
1797 <                        tab = initTable();
1798 <                    else if ((f = tabAt(tab, i = (tab.length - 1) & h)) == null){
1799 <                        if (casTabAt(tab, i, null, new Node(h, k, v, null))) {
1800 <                            ++delta;
1801 <                            break;
1802 <                        }
1803 <                    }
1804 <                    else if ((fh = f.hash) == MOVED) {
1805 <                        if ((fk = f.key) instanceof TreeBin) {
1806 <                            TreeBin t = (TreeBin)fk;
1807 <                            boolean validated = false;
1808 <                            t.acquire(0);
1809 <                            try {
1810 <                                if (tabAt(tab, i) == f) {
1811 <                                    validated = true;
1812 <                                    TreeNode p = t.getTreeNode(h, k, t.root);
1813 <                                    if (p != null)
1814 <                                        p.val = v;
1815 <                                    else {
1816 <                                        t.putTreeNode(h, k, v);
1817 <                                        ++delta;
1818 <                                    }
1819 <                                }
1820 <                            } finally {
1821 <                                t.release(0);
1822 <                            }
1823 <                            if (validated)
1824 <                                break;
1825 <                        }
1826 <                        else
1827 <                            tab = (Node[])fk;
1828 <                    }
1829 <                    else if ((fh & LOCKED) != 0) {
1830 <                        counter.add(delta);
1831 <                        delta = 0L;
1832 <                        checkForResize();
1833 <                        f.tryAwaitLock(tab, i);
1834 <                    }
1835 <                    else if (f.casHash(fh, fh | LOCKED)) {
1836 <                        int count = 0;
1837 <                        try {
1838 <                            if (tabAt(tab, i) == f) {
1839 <                                count = 1;
1840 <                                for (Node e = f;; ++count) {
1841 <                                    Object ek, ev;
1842 <                                    if ((e.hash & HASH_BITS) == h &&
1843 <                                        (ev = e.val) != null &&
1844 <                                        ((ek = e.key) == k || k.equals(ek))) {
1845 <                                        e.val = v;
1846 <                                        break;
1847 <                                    }
1848 <                                    Node last = e;
1849 <                                    if ((e = e.next) == null) {
1850 <                                        ++delta;
1851 <                                        last.next = new Node(h, k, v, null);
1852 <                                        if (count >= TREE_THRESHOLD)
1853 <                                            replaceWithTreeBin(tab, i, k);
1854 <                                        break;
1855 <                                    }
1856 <                                }
1857 <                            }
1858 <                        } finally {
1859 <                            if (!f.casHash(fh | LOCKED, fh)) {
1860 <                                f.hash = fh;
1861 <                                synchronized (f) { f.notifyAll(); };
1862 <                            }
1863 <                        }
1864 <                        if (count != 0) {
1865 <                            if (count > 1) {
1866 <                                counter.add(delta);
1867 <                                delta = 0L;
1868 <                                checkForResize();
1869 <                            }
1870 <                            break;
1871 <                        }
1872 <                    }
1873 <                }
1874 <            }
1875 <        } finally {
1876 <            if (delta != 0)
1877 <                counter.add(delta);
1878 <        }
1879 <        if (npe)
1880 <            throw new NullPointerException();
770 >    static final <K,V> void setTabAt(Node<K,V>[] tab, int i, Node<K,V> v) {
771 >        U.putObjectVolatile(tab, ((long)i << ASHIFT) + ABASE, v);
772      }
773  
774 <    /* ---------------- Table Initialization and Resizing -------------- */
774 >    /* ---------------- Fields -------------- */
775  
776      /**
777 <     * Returns a power of two table size for the given desired capacity.
778 <     * See Hackers Delight, sec 3.2
777 >     * The array of bins. Lazily initialized upon first insertion.
778 >     * Size is always a power of two. Accessed directly by iterators.
779       */
780 <    private static final int tableSizeFor(int c) {
1890 <        int n = c - 1;
1891 <        n |= n >>> 1;
1892 <        n |= n >>> 2;
1893 <        n |= n >>> 4;
1894 <        n |= n >>> 8;
1895 <        n |= n >>> 16;
1896 <        return (n < 0) ? 1 : (n >= MAXIMUM_CAPACITY) ? MAXIMUM_CAPACITY : n + 1;
1897 <    }
780 >    transient volatile Node<K,V>[] table;
781  
782      /**
783 <     * Initializes table, using the size recorded in sizeCtl.
783 >     * The next table to use; non-null only while resizing.
784       */
785 <    private final Node[] initTable() {
1903 <        Node[] tab; int sc;
1904 <        while ((tab = table) == null) {
1905 <            if ((sc = sizeCtl) < 0)
1906 <                Thread.yield(); // lost initialization race; just spin
1907 <            else if (UNSAFE.compareAndSwapInt(this, sizeCtlOffset, sc, -1)) {
1908 <                try {
1909 <                    if ((tab = table) == null) {
1910 <                        int n = (sc > 0) ? sc : DEFAULT_CAPACITY;
1911 <                        tab = table = new Node[n];
1912 <                        sc = n - (n >>> 2);
1913 <                    }
1914 <                } finally {
1915 <                    sizeCtl = sc;
1916 <                }
1917 <                break;
1918 <            }
1919 <        }
1920 <        return tab;
1921 <    }
1922 <
1923 <    /**
1924 <     * If table is too small and not already resizing, creates next
1925 <     * table and transfers bins.  Rechecks occupancy after a transfer
1926 <     * to see if another resize is already needed because resizings
1927 <     * are lagging additions.
1928 <     */
1929 <    private final void checkForResize() {
1930 <        Node[] tab; int n, sc;
1931 <        while ((tab = table) != null &&
1932 <               (n = tab.length) < MAXIMUM_CAPACITY &&
1933 <               (sc = sizeCtl) >= 0 && counter.sum() >= (long)sc &&
1934 <               UNSAFE.compareAndSwapInt(this, sizeCtlOffset, sc, -1)) {
1935 <            try {
1936 <                if (tab == table) {
1937 <                    table = rebuild(tab);
1938 <                    sc = (n << 1) - (n >>> 1);
1939 <                }
1940 <            } finally {
1941 <                sizeCtl = sc;
1942 <            }
1943 <        }
1944 <    }
785 >    private transient volatile Node<K,V>[] nextTable;
786  
787      /**
788 <     * Tries to presize table to accommodate the given number of elements.
789 <     *
790 <     * @param size number of elements (doesn't need to be perfectly accurate)
788 >     * Base counter value, used mainly when there is no contention,
789 >     * but also as a fallback during table initialization
790 >     * races. Updated via CAS.
791       */
792 <    private final void tryPresize(int size) {
1952 <        int c = (size >= (MAXIMUM_CAPACITY >>> 1)) ? MAXIMUM_CAPACITY :
1953 <            tableSizeFor(size + (size >>> 1) + 1);
1954 <        int sc;
1955 <        while ((sc = sizeCtl) >= 0) {
1956 <            Node[] tab = table; int n;
1957 <            if (tab == null || (n = tab.length) == 0) {
1958 <                n = (sc > c) ? sc : c;
1959 <                if (UNSAFE.compareAndSwapInt(this, sizeCtlOffset, sc, -1)) {
1960 <                    try {
1961 <                        if (table == tab) {
1962 <                            table = new Node[n];
1963 <                            sc = n - (n >>> 2);
1964 <                        }
1965 <                    } finally {
1966 <                        sizeCtl = sc;
1967 <                    }
1968 <                }
1969 <            }
1970 <            else if (c <= sc || n >= MAXIMUM_CAPACITY)
1971 <                break;
1972 <            else if (UNSAFE.compareAndSwapInt(this, sizeCtlOffset, sc, -1)) {
1973 <                try {
1974 <                    if (table == tab) {
1975 <                        table = rebuild(tab);
1976 <                        sc = (n << 1) - (n >>> 1);
1977 <                    }
1978 <                } finally {
1979 <                    sizeCtl = sc;
1980 <                }
1981 <            }
1982 <        }
1983 <    }
1984 <
1985 <    /*
1986 <     * Moves and/or copies the nodes in each bin to new table. See
1987 <     * above for explanation.
1988 <     *
1989 <     * @return the new table
1990 <     */
1991 <    private static final Node[] rebuild(Node[] tab) {
1992 <        int n = tab.length;
1993 <        Node[] nextTab = new Node[n << 1];
1994 <        Node fwd = new Node(MOVED, nextTab, null, null);
1995 <        int[] buffer = null;       // holds bins to revisit; null until needed
1996 <        Node rev = null;           // reverse forwarder; null until needed
1997 <        int nbuffered = 0;         // the number of bins in buffer list
1998 <        int bufferIndex = 0;       // buffer index of current buffered bin
1999 <        int bin = n - 1;           // current non-buffered bin or -1 if none
2000 <
2001 <        for (int i = bin;;) {      // start upwards sweep
2002 <            int fh; Node f;
2003 <            if ((f = tabAt(tab, i)) == null) {
2004 <                if (bin >= 0) {    // no lock needed (or available)
2005 <                    if (!casTabAt(tab, i, f, fwd))
2006 <                        continue;
2007 <                }
2008 <                else {             // transiently use a locked forwarding node
2009 <                    Node g = new Node(MOVED|LOCKED, nextTab, null, null);
2010 <                    if (!casTabAt(tab, i, f, g))
2011 <                        continue;
2012 <                    setTabAt(nextTab, i, null);
2013 <                    setTabAt(nextTab, i + n, null);
2014 <                    setTabAt(tab, i, fwd);
2015 <                    if (!g.casHash(MOVED|LOCKED, MOVED)) {
2016 <                        g.hash = MOVED;
2017 <                        synchronized (g) { g.notifyAll(); }
2018 <                    }
2019 <                }
2020 <            }
2021 <            else if ((fh = f.hash) == MOVED) {
2022 <                Object fk = f.key;
2023 <                if (fk instanceof TreeBin) {
2024 <                    TreeBin t = (TreeBin)fk;
2025 <                    boolean validated = false;
2026 <                    t.acquire(0);
2027 <                    try {
2028 <                        if (tabAt(tab, i) == f) {
2029 <                            validated = true;
2030 <                            splitTreeBin(nextTab, i, t);
2031 <                            setTabAt(tab, i, fwd);
2032 <                        }
2033 <                    } finally {
2034 <                        t.release(0);
2035 <                    }
2036 <                    if (!validated)
2037 <                        continue;
2038 <                }
2039 <            }
2040 <            else if ((fh & LOCKED) == 0 && f.casHash(fh, fh|LOCKED)) {
2041 <                boolean validated = false;
2042 <                try {              // split to lo and hi lists; copying as needed
2043 <                    if (tabAt(tab, i) == f) {
2044 <                        validated = true;
2045 <                        splitBin(nextTab, i, f);
2046 <                        setTabAt(tab, i, fwd);
2047 <                    }
2048 <                } finally {
2049 <                    if (!f.casHash(fh | LOCKED, fh)) {
2050 <                        f.hash = fh;
2051 <                        synchronized (f) { f.notifyAll(); };
2052 <                    }
2053 <                }
2054 <                if (!validated)
2055 <                    continue;
2056 <            }
2057 <            else {
2058 <                if (buffer == null) // initialize buffer for revisits
2059 <                    buffer = new int[TRANSFER_BUFFER_SIZE];
2060 <                if (bin < 0 && bufferIndex > 0) {
2061 <                    int j = buffer[--bufferIndex];
2062 <                    buffer[bufferIndex] = i;
2063 <                    i = j;         // swap with another bin
2064 <                    continue;
2065 <                }
2066 <                if (bin < 0 || nbuffered >= TRANSFER_BUFFER_SIZE) {
2067 <                    f.tryAwaitLock(tab, i);
2068 <                    continue;      // no other options -- block
2069 <                }
2070 <                if (rev == null)   // initialize reverse-forwarder
2071 <                    rev = new Node(MOVED, tab, null, null);
2072 <                if (tabAt(tab, i) != f || (f.hash & LOCKED) == 0)
2073 <                    continue;      // recheck before adding to list
2074 <                buffer[nbuffered++] = i;
2075 <                setTabAt(nextTab, i, rev);     // install place-holders
2076 <                setTabAt(nextTab, i + n, rev);
2077 <            }
2078 <
2079 <            if (bin > 0)
2080 <                i = --bin;
2081 <            else if (buffer != null && nbuffered > 0) {
2082 <                bin = -1;
2083 <                i = buffer[bufferIndex = --nbuffered];
2084 <            }
2085 <            else
2086 <                return nextTab;
2087 <        }
2088 <    }
792 >    private transient volatile long baseCount;
793  
794      /**
795 <     * Splits a normal bin with list headed by e into lo and hi parts;
796 <     * installs in given table.
795 >     * Table initialization and resizing control.  When negative, the
796 >     * table is being initialized or resized: -1 for initialization,
797 >     * else -(1 + the number of active resizing threads).  Otherwise,
798 >     * when table is null, holds the initial table size to use upon
799 >     * creation, or 0 for default. After initialization, holds the
800 >     * next element count value upon which to resize the table.
801       */
802 <    private static void splitBin(Node[] nextTab, int i, Node e) {
2095 <        int bit = nextTab.length >>> 1; // bit to split on
2096 <        int runBit = e.hash & bit;
2097 <        Node lastRun = e, lo = null, hi = null;
2098 <        for (Node p = e.next; p != null; p = p.next) {
2099 <            int b = p.hash & bit;
2100 <            if (b != runBit) {
2101 <                runBit = b;
2102 <                lastRun = p;
2103 <            }
2104 <        }
2105 <        if (runBit == 0)
2106 <            lo = lastRun;
2107 <        else
2108 <            hi = lastRun;
2109 <        for (Node p = e; p != lastRun; p = p.next) {
2110 <            int ph = p.hash & HASH_BITS;
2111 <            Object pk = p.key, pv = p.val;
2112 <            if ((ph & bit) == 0)
2113 <                lo = new Node(ph, pk, pv, lo);
2114 <            else
2115 <                hi = new Node(ph, pk, pv, hi);
2116 <        }
2117 <        setTabAt(nextTab, i, lo);
2118 <        setTabAt(nextTab, i + bit, hi);
2119 <    }
802 >    private transient volatile int sizeCtl;
803  
804      /**
805 <     * Splits a tree bin into lo and hi parts; installs in given table.
805 >     * The next table index (plus one) to split while resizing.
806       */
807 <    private static void splitTreeBin(Node[] nextTab, int i, TreeBin t) {
2125 <        int bit = nextTab.length >>> 1;
2126 <        TreeBin lt = new TreeBin();
2127 <        TreeBin ht = new TreeBin();
2128 <        int lc = 0, hc = 0;
2129 <        for (Node e = t.first; e != null; e = e.next) {
2130 <            int h = e.hash & HASH_BITS;
2131 <            Object k = e.key, v = e.val;
2132 <            if ((h & bit) == 0) {
2133 <                ++lc;
2134 <                lt.putTreeNode(h, k, v);
2135 <            }
2136 <            else {
2137 <                ++hc;
2138 <                ht.putTreeNode(h, k, v);
2139 <            }
2140 <        }
2141 <        Node ln, hn; // throw away trees if too small
2142 <        if (lc <= (TREE_THRESHOLD >>> 1)) {
2143 <            ln = null;
2144 <            for (Node p = lt.first; p != null; p = p.next)
2145 <                ln = new Node(p.hash, p.key, p.val, ln);
2146 <        }
2147 <        else
2148 <            ln = new Node(MOVED, lt, null, null);
2149 <        setTabAt(nextTab, i, ln);
2150 <        if (hc <= (TREE_THRESHOLD >>> 1)) {
2151 <            hn = null;
2152 <            for (Node p = ht.first; p != null; p = p.next)
2153 <                hn = new Node(p.hash, p.key, p.val, hn);
2154 <        }
2155 <        else
2156 <            hn = new Node(MOVED, ht, null, null);
2157 <        setTabAt(nextTab, i + bit, hn);
2158 <    }
807 >    private transient volatile int transferIndex;
808  
809      /**
810 <     * Implementation for clear. Steps through each bin, removing all
2162 <     * nodes.
810 >     * Spinlock (locked via CAS) used when resizing and/or creating CounterCells.
811       */
812 <    private final void internalClear() {
2165 <        long delta = 0L; // negative number of deletions
2166 <        int i = 0;
2167 <        Node[] tab = table;
2168 <        while (tab != null && i < tab.length) {
2169 <            int fh; Object fk;
2170 <            Node f = tabAt(tab, i);
2171 <            if (f == null)
2172 <                ++i;
2173 <            else if ((fh = f.hash) == MOVED) {
2174 <                if ((fk = f.key) instanceof TreeBin) {
2175 <                    TreeBin t = (TreeBin)fk;
2176 <                    t.acquire(0);
2177 <                    try {
2178 <                        if (tabAt(tab, i) == f) {
2179 <                            for (Node p = t.first; p != null; p = p.next) {
2180 <                                p.val = null;
2181 <                                --delta;
2182 <                            }
2183 <                            t.first = null;
2184 <                            t.root = null;
2185 <                            ++i;
2186 <                        }
2187 <                    } finally {
2188 <                        t.release(0);
2189 <                    }
2190 <                }
2191 <                else
2192 <                    tab = (Node[])fk;
2193 <            }
2194 <            else if ((fh & LOCKED) != 0) {
2195 <                counter.add(delta); // opportunistically update count
2196 <                delta = 0L;
2197 <                f.tryAwaitLock(tab, i);
2198 <            }
2199 <            else if (f.casHash(fh, fh | LOCKED)) {
2200 <                try {
2201 <                    if (tabAt(tab, i) == f) {
2202 <                        for (Node e = f; e != null; e = e.next) {
2203 <                            e.val = null;
2204 <                            --delta;
2205 <                        }
2206 <                        setTabAt(tab, i, null);
2207 <                        ++i;
2208 <                    }
2209 <                } finally {
2210 <                    if (!f.casHash(fh | LOCKED, fh)) {
2211 <                        f.hash = fh;
2212 <                        synchronized (f) { f.notifyAll(); };
2213 <                    }
2214 <                }
2215 <            }
2216 <        }
2217 <        if (delta != 0)
2218 <            counter.add(delta);
2219 <    }
2220 <
2221 <    /* ----------------Table Traversal -------------- */
812 >    private transient volatile int cellsBusy;
813  
814      /**
815 <     * Encapsulates traversal for methods such as containsValue; also
816 <     * serves as a base class for other iterators.
817 <     *
2227 <     * At each step, the iterator snapshots the key ("nextKey") and
2228 <     * value ("nextVal") of a valid node (i.e., one that, at point of
2229 <     * snapshot, has a non-null user value). Because val fields can
2230 <     * change (including to null, indicating deletion), field nextVal
2231 <     * might not be accurate at point of use, but still maintains the
2232 <     * weak consistency property of holding a value that was once
2233 <     * valid.
2234 <     *
2235 <     * Internal traversals directly access these fields, as in:
2236 <     * {@code while (it.advance() != null) { process(it.nextKey); }}
2237 <     *
2238 <     * Exported iterators must track whether the iterator has advanced
2239 <     * (in hasNext vs next) (by setting/checking/nulling field
2240 <     * nextVal), and then extract key, value, or key-value pairs as
2241 <     * return values of next().
2242 <     *
2243 <     * The iterator visits once each still-valid node that was
2244 <     * reachable upon iterator construction. It might miss some that
2245 <     * were added to a bin after the bin was visited, which is OK wrt
2246 <     * consistency guarantees. Maintaining this property in the face
2247 <     * of possible ongoing resizes requires a fair amount of
2248 <     * bookkeeping state that is difficult to optimize away amidst
2249 <     * volatile accesses.  Even so, traversal maintains reasonable
2250 <     * throughput.
2251 <     *
2252 <     * Normally, iteration proceeds bin-by-bin traversing lists.
2253 <     * However, if the table has been resized, then all future steps
2254 <     * must traverse both the bin at the current index as well as at
2255 <     * (index + baseSize); and so on for further resizings. To
2256 <     * paranoically cope with potential sharing by users of iterators
2257 <     * across threads, iteration terminates if a bounds checks fails
2258 <     * for a table read.
2259 <     *
2260 <     * This class extends ForkJoinTask to streamline parallel
2261 <     * iteration in bulk operations (see BulkTask). This adds only an
2262 <     * int of space overhead, which is close enough to negligible in
2263 <     * cases where it is not needed to not worry about it.
2264 <     */
2265 <    static class Traverser<K,V,R> extends ForkJoinTask<R> {
2266 <        final ConcurrentHashMapV8<K, V> map;
2267 <        Node next;           // the next entry to use
2268 <        Node last;           // the last entry used
2269 <        Object nextKey;      // cached key field of next
2270 <        Object nextVal;      // cached val field of next
2271 <        Node[] tab;          // current table; updated if resized
2272 <        int index;           // index of bin to use next
2273 <        int baseIndex;       // current index of initial table
2274 <        int baseLimit;       // index bound for initial table
2275 <        final int baseSize;  // initial table size
2276 <
2277 <        /** Creates iterator for all entries in the table. */
2278 <        Traverser(ConcurrentHashMapV8<K, V> map) {
2279 <            this.tab = (this.map = map).table;
2280 <            baseLimit = baseSize = (tab == null) ? 0 : tab.length;
2281 <        }
2282 <
2283 <        /** Creates iterator for split() methods */
2284 <        Traverser(Traverser<K,V,?> it, boolean split) {
2285 <            this.map = it.map;
2286 <            this.tab = it.tab;
2287 <            this.baseSize = it.baseSize;
2288 <            int lo = it.baseIndex;
2289 <            int hi = this.baseLimit = it.baseLimit;
2290 <            int i;
2291 <            if (split) // adjust parent
2292 <                i = it.baseLimit = (lo + hi + 1) >>> 1;
2293 <            else       // clone parent
2294 <                i = lo;
2295 <            this.index = this.baseIndex = i;
2296 <        }
2297 <
2298 <        /**
2299 <         * Advances next; returns nextVal or null if terminated.
2300 <         * See above for explanation.
2301 <         */
2302 <        final Object advance() {
2303 <            Node e = last = next;
2304 <            Object ev = null;
2305 <            outer: do {
2306 <                if (e != null)                  // advance past used/skipped node
2307 <                    e = e.next;
2308 <                while (e == null) {             // get to next non-null bin
2309 <                    Node[] t; int b, i, n; Object ek; // checks must use locals
2310 <                    if ((b = baseIndex) >= baseLimit || (i = index) < 0 ||
2311 <                        (t = tab) == null || i >= (n = t.length))
2312 <                        break outer;
2313 <                    else if ((e = tabAt(t, i)) != null && e.hash == MOVED) {
2314 <                        if ((ek = e.key) instanceof TreeBin)
2315 <                            e = ((TreeBin)ek).first;
2316 <                        else {
2317 <                            tab = (Node[])ek;
2318 <                            continue;           // restarts due to null val
2319 <                        }
2320 <                    }                           // visit upper slots if present
2321 <                    index = (i += baseSize) < n ? i : (baseIndex = b + 1);
2322 <                }
2323 <                nextKey = e.key;
2324 <            } while ((ev = e.val) == null);    // skip deleted or special nodes
2325 <            next = e;
2326 <            return nextVal = ev;
2327 <        }
2328 <
2329 <        public final void remove() {
2330 <            if (nextVal == null && last == null)
2331 <                advance();
2332 <            Node e = last;
2333 <            if (e == null)
2334 <                throw new IllegalStateException();
2335 <            last = null;
2336 <            map.remove(e.key);
2337 <        }
815 >     * Table of counter cells. When non-null, size is a power of 2.
816 >     */
817 >    private transient volatile CounterCell[] counterCells;
818  
819 <        public final boolean hasNext() {
820 <            return nextVal != null || advance() != null;
821 <        }
819 >    // views
820 >    private transient KeySetView<K,V> keySet;
821 >    private transient ValuesView<K,V> values;
822 >    private transient EntrySetView<K,V> entrySet;
823  
2343        public final boolean hasMoreElements() { return hasNext(); }
2344        public final void setRawResult(Object x) { }
2345        public R getRawResult() { return null; }
2346        public boolean exec() { return true; }
2347    }
824  
825      /* ---------------- Public operations -------------- */
826  
# Line 2352 | Line 828 | public class ConcurrentHashMapV8<K, V>
828       * Creates a new, empty map with the default initial table size (16).
829       */
830      public ConcurrentHashMapV8() {
2355        this.counter = new LongAdder();
831      }
832  
833      /**
# Line 2371 | Line 846 | public class ConcurrentHashMapV8<K, V>
846          int cap = ((initialCapacity >= (MAXIMUM_CAPACITY >>> 1)) ?
847                     MAXIMUM_CAPACITY :
848                     tableSizeFor(initialCapacity + (initialCapacity >>> 1) + 1));
2374        this.counter = new LongAdder();
849          this.sizeCtl = cap;
850      }
851  
# Line 2381 | Line 855 | public class ConcurrentHashMapV8<K, V>
855       * @param m the map
856       */
857      public ConcurrentHashMapV8(Map<? extends K, ? extends V> m) {
2384        this.counter = new LongAdder();
858          this.sizeCtl = DEFAULT_CAPACITY;
859 <        internalPutAll(m);
859 >        putAll(m);
860      }
861  
862      /**
# Line 2424 | Line 897 | public class ConcurrentHashMapV8<K, V>
897       * nonpositive
898       */
899      public ConcurrentHashMapV8(int initialCapacity,
900 <                               float loadFactor, int concurrencyLevel) {
900 >                             float loadFactor, int concurrencyLevel) {
901          if (!(loadFactor > 0.0f) || initialCapacity < 0 || concurrencyLevel <= 0)
902              throw new IllegalArgumentException();
903          if (initialCapacity < concurrencyLevel)   // Use at least as many bins
# Line 2432 | Line 905 | public class ConcurrentHashMapV8<K, V>
905          long size = (long)(1.0 + (long)initialCapacity / loadFactor);
906          int cap = (size >= (long)MAXIMUM_CAPACITY) ?
907              MAXIMUM_CAPACITY : tableSizeFor((int)size);
2435        this.counter = new LongAdder();
908          this.sizeCtl = cap;
909      }
910  
911 <    /**
2440 <     * {@inheritDoc}
2441 <     */
2442 <    public boolean isEmpty() {
2443 <        return counter.sum() <= 0L; // ignore transient negative values
2444 <    }
911 >    // Original (since JDK1.2) Map methods
912  
913      /**
914       * {@inheritDoc}
915       */
916      public int size() {
917 <        long n = counter.sum();
917 >        long n = sumCount();
918          return ((n < 0L) ? 0 :
919                  (n > (long)Integer.MAX_VALUE) ? Integer.MAX_VALUE :
920                  (int)n);
921      }
922  
923      /**
924 <     * Returns the number of mappings. This method should be used
2458 <     * instead of {@link #size} because a ConcurrentHashMap may
2459 <     * contain more mappings than can be represented as an int. The
2460 <     * value returned is a snapshot; the actual count may differ if
2461 <     * there are ongoing concurrent insertions of removals.
2462 <     *
2463 <     * @return the number of mappings
924 >     * {@inheritDoc}
925       */
926 <    public long mappingCount() {
927 <        long n = counter.sum();
2467 <        return (n < 0L) ? 0L : n;
926 >    public boolean isEmpty() {
927 >        return sumCount() <= 0L; // ignore transient negative values
928      }
929  
930      /**
# Line 2478 | Line 938 | public class ConcurrentHashMapV8<K, V>
938       *
939       * @throws NullPointerException if the specified key is null
940       */
941 <    @SuppressWarnings("unchecked")
942 <        public V get(Object key) {
943 <        if (key == null)
944 <            throw new NullPointerException();
945 <        return (V)internalGet(key);
941 >    public V get(Object key) {
942 >        Node<K,V>[] tab; Node<K,V> e, p; int n, eh; K ek;
943 >        int h = spread(key.hashCode());
944 >        if ((tab = table) != null && (n = tab.length) > 0 &&
945 >            (e = tabAt(tab, (n - 1) & h)) != null) {
946 >            if ((eh = e.hash) == h) {
947 >                if ((ek = e.key) == key || (ek != null && key.equals(ek)))
948 >                    return e.val;
949 >            }
950 >            else if (eh < 0)
951 >                return (p = e.find(h, key)) != null ? p.val : null;
952 >            while ((e = e.next) != null) {
953 >                if (e.hash == h &&
954 >                    ((ek = e.key) == key || (ek != null && key.equals(ek))))
955 >                    return e.val;
956 >            }
957 >        }
958 >        return null;
959      }
960  
961      /**
962       * Tests if the specified object is a key in this table.
963       *
964 <     * @param  key   possible key
964 >     * @param  key possible key
965       * @return {@code true} if and only if the specified object
966       *         is a key in this table, as determined by the
967       *         {@code equals} method; {@code false} otherwise
968       * @throws NullPointerException if the specified key is null
969       */
970      public boolean containsKey(Object key) {
971 <        if (key == null)
2499 <            throw new NullPointerException();
2500 <        return internalGet(key) != null;
971 >        return get(key) != null;
972      }
973  
974      /**
# Line 2513 | Line 984 | public class ConcurrentHashMapV8<K, V>
984      public boolean containsValue(Object value) {
985          if (value == null)
986              throw new NullPointerException();
987 <        Object v;
988 <        Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
989 <        while ((v = it.advance()) != null) {
990 <            if (v == value || value.equals(v))
991 <                return true;
987 >        Node<K,V>[] t;
988 >        if ((t = table) != null) {
989 >            Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length);
990 >            for (Node<K,V> p; (p = it.advance()) != null; ) {
991 >                V v;
992 >                if ((v = p.val) == value || (v != null && value.equals(v)))
993 >                    return true;
994 >            }
995          }
996          return false;
997      }
998  
999      /**
2526     * Legacy method testing if some key maps into the specified value
2527     * in this table.  This method is identical in functionality to
2528     * {@link #containsValue}, and exists solely to ensure
2529     * full compatibility with class {@link java.util.Hashtable},
2530     * which supported this method prior to introduction of the
2531     * Java Collections framework.
2532     *
2533     * @param  value a value to search for
2534     * @return {@code true} if and only if some key maps to the
2535     *         {@code value} argument in this table as
2536     *         determined by the {@code equals} method;
2537     *         {@code false} otherwise
2538     * @throws NullPointerException if the specified value is null
2539     */
2540    public boolean contains(Object value) {
2541        return containsValue(value);
2542    }
2543
2544    /**
1000       * Maps the specified key to the specified value in this table.
1001       * Neither the key nor the value can be null.
1002       *
1003 <     * <p> The value can be retrieved by calling the {@code get} method
1003 >     * <p>The value can be retrieved by calling the {@code get} method
1004       * with a key that is equal to the original key.
1005       *
1006       * @param key key with which the specified value is to be associated
# Line 2554 | Line 1009 | public class ConcurrentHashMapV8<K, V>
1009       *         {@code null} if there was no mapping for {@code key}
1010       * @throws NullPointerException if the specified key or value is null
1011       */
1012 <    @SuppressWarnings("unchecked")
1013 <        public V put(K key, V value) {
2559 <        if (key == null || value == null)
2560 <            throw new NullPointerException();
2561 <        return (V)internalPut(key, value);
1012 >    public V put(K key, V value) {
1013 >        return putVal(key, value, false);
1014      }
1015  
1016 <    /**
1017 <     * {@inheritDoc}
1018 <     *
1019 <     * @return the previous value associated with the specified key,
1020 <     *         or {@code null} if there was no mapping for the key
1021 <     * @throws NullPointerException if the specified key or value is null
1022 <     */
1023 <    @SuppressWarnings("unchecked")
1024 <        public V putIfAbsent(K key, V value) {
1025 <        if (key == null || value == null)
1026 <            throw new NullPointerException();
1027 <        return (V)internalPutIfAbsent(key, value);
1016 >    /** Implementation for put and putIfAbsent */
1017 >    final V putVal(K key, V value, boolean onlyIfAbsent) {
1018 >        if (key == null || value == null) throw new NullPointerException();
1019 >        int hash = spread(key.hashCode());
1020 >        int binCount = 0;
1021 >        for (Node<K,V>[] tab = table;;) {
1022 >            Node<K,V> f; int n, i, fh;
1023 >            if (tab == null || (n = tab.length) == 0)
1024 >                tab = initTable();
1025 >            else if ((f = tabAt(tab, i = (n - 1) & hash)) == null) {
1026 >                if (casTabAt(tab, i, null,
1027 >                             new Node<K,V>(hash, key, value, null)))
1028 >                    break;                   // no lock when adding to empty bin
1029 >            }
1030 >            else if ((fh = f.hash) == MOVED)
1031 >                tab = helpTransfer(tab, f);
1032 >            else {
1033 >                V oldVal = null;
1034 >                synchronized (f) {
1035 >                    if (tabAt(tab, i) == f) {
1036 >                        if (fh >= 0) {
1037 >                            binCount = 1;
1038 >                            for (Node<K,V> e = f;; ++binCount) {
1039 >                                K ek;
1040 >                                if (e.hash == hash &&
1041 >                                    ((ek = e.key) == key ||
1042 >                                     (ek != null && key.equals(ek)))) {
1043 >                                    oldVal = e.val;
1044 >                                    if (!onlyIfAbsent)
1045 >                                        e.val = value;
1046 >                                    break;
1047 >                                }
1048 >                                Node<K,V> pred = e;
1049 >                                if ((e = e.next) == null) {
1050 >                                    pred.next = new Node<K,V>(hash, key,
1051 >                                                              value, null);
1052 >                                    break;
1053 >                                }
1054 >                            }
1055 >                        }
1056 >                        else if (f instanceof TreeBin) {
1057 >                            Node<K,V> p;
1058 >                            binCount = 2;
1059 >                            if ((p = ((TreeBin<K,V>)f).putTreeVal(hash, key,
1060 >                                                           value)) != null) {
1061 >                                oldVal = p.val;
1062 >                                if (!onlyIfAbsent)
1063 >                                    p.val = value;
1064 >                            }
1065 >                        }
1066 >                    }
1067 >                }
1068 >                if (binCount != 0) {
1069 >                    if (binCount >= TREEIFY_THRESHOLD)
1070 >                        treeifyBin(tab, i);
1071 >                    if (oldVal != null)
1072 >                        return oldVal;
1073 >                    break;
1074 >                }
1075 >            }
1076 >        }
1077 >        addCount(1L, binCount);
1078 >        return null;
1079      }
1080  
1081      /**
# Line 2583 | Line 1086 | public class ConcurrentHashMapV8<K, V>
1086       * @param m mappings to be stored in this map
1087       */
1088      public void putAll(Map<? extends K, ? extends V> m) {
1089 <        internalPutAll(m);
1090 <    }
1091 <
2589 <    /**
2590 <     * If the specified key is not already associated with a value,
2591 <     * computes its value using the given mappingFunction and enters
2592 <     * it into the map unless null.  This is equivalent to
2593 <     * <pre> {@code
2594 <     * if (map.containsKey(key))
2595 <     *   return map.get(key);
2596 <     * value = mappingFunction.apply(key);
2597 <     * if (value != null)
2598 <     *   map.put(key, value);
2599 <     * return value;}</pre>
2600 <     *
2601 <     * except that the action is performed atomically.  If the
2602 <     * function returns {@code null} no mapping is recorded. If the
2603 <     * function itself throws an (unchecked) exception, the exception
2604 <     * is rethrown to its caller, and no mapping is recorded.  Some
2605 <     * attempted update operations on this map by other threads may be
2606 <     * blocked while computation is in progress, so the computation
2607 <     * should be short and simple, and must not attempt to update any
2608 <     * other mappings of this Map. The most appropriate usage is to
2609 <     * construct a new object serving as an initial mapped value, or
2610 <     * memoized result, as in:
2611 <     *
2612 <     *  <pre> {@code
2613 <     * map.computeIfAbsent(key, new Fun<K, V>() {
2614 <     *   public V map(K k) { return new Value(f(k)); }});}</pre>
2615 <     *
2616 <     * @param key key with which the specified value is to be associated
2617 <     * @param mappingFunction the function to compute a value
2618 <     * @return the current (existing or computed) value associated with
2619 <     *         the specified key, or null if the computed value is null.
2620 <     * @throws NullPointerException if the specified key or mappingFunction
2621 <     *         is null
2622 <     * @throws IllegalStateException if the computation detectably
2623 <     *         attempts a recursive update to this map that would
2624 <     *         otherwise never complete
2625 <     * @throws RuntimeException or Error if the mappingFunction does so,
2626 <     *         in which case the mapping is left unestablished
2627 <     */
2628 <    @SuppressWarnings("unchecked")
2629 <        public V computeIfAbsent(K key, Fun<? super K, ? extends V> mappingFunction) {
2630 <        if (key == null || mappingFunction == null)
2631 <            throw new NullPointerException();
2632 <        return (V)internalComputeIfAbsent(key, mappingFunction);
2633 <    }
2634 <
2635 <    /**
2636 <     * If the given key is present, computes a new mapping value given a key and
2637 <     * its current mapped value. This is equivalent to
2638 <     *  <pre> {@code
2639 <     *   if (map.containsKey(key)) {
2640 <     *     value = remappingFunction.apply(key, map.get(key));
2641 <     *     if (value != null)
2642 <     *       map.put(key, value);
2643 <     *     else
2644 <     *       map.remove(key);
2645 <     *   }
2646 <     * }</pre>
2647 <     *
2648 <     * except that the action is performed atomically.  If the
2649 <     * function returns {@code null}, the mapping is removed.  If the
2650 <     * function itself throws an (unchecked) exception, the exception
2651 <     * is rethrown to its caller, and the current mapping is left
2652 <     * unchanged.  Some attempted update operations on this map by
2653 <     * other threads may be blocked while computation is in progress,
2654 <     * so the computation should be short and simple, and must not
2655 <     * attempt to update any other mappings of this Map. For example,
2656 <     * to either create or append new messages to a value mapping:
2657 <     *
2658 <     * @param key key with which the specified value is to be associated
2659 <     * @param remappingFunction the function to compute a value
2660 <     * @return the new value associated with the specified key, or null if none
2661 <     * @throws NullPointerException if the specified key or remappingFunction
2662 <     *         is null
2663 <     * @throws IllegalStateException if the computation detectably
2664 <     *         attempts a recursive update to this map that would
2665 <     *         otherwise never complete
2666 <     * @throws RuntimeException or Error if the remappingFunction does so,
2667 <     *         in which case the mapping is unchanged
2668 <     */
2669 <    public V computeIfPresent(K key, BiFun<? super K, ? super V, ? extends V> remappingFunction) {
2670 <        if (key == null || remappingFunction == null)
2671 <            throw new NullPointerException();
2672 <        return (V)internalCompute(key, true, remappingFunction);
2673 <    }
2674 <
2675 <    /**
2676 <     * Computes a new mapping value given a key and
2677 <     * its current mapped value (or {@code null} if there is no current
2678 <     * mapping). This is equivalent to
2679 <     *  <pre> {@code
2680 <     *   value = remappingFunction.apply(key, map.get(key));
2681 <     *   if (value != null)
2682 <     *     map.put(key, value);
2683 <     *   else
2684 <     *     map.remove(key);
2685 <     * }</pre>
2686 <     *
2687 <     * except that the action is performed atomically.  If the
2688 <     * function returns {@code null}, the mapping is removed.  If the
2689 <     * function itself throws an (unchecked) exception, the exception
2690 <     * is rethrown to its caller, and the current mapping is left
2691 <     * unchanged.  Some attempted update operations on this map by
2692 <     * other threads may be blocked while computation is in progress,
2693 <     * so the computation should be short and simple, and must not
2694 <     * attempt to update any other mappings of this Map. For example,
2695 <     * to either create or append new messages to a value mapping:
2696 <     *
2697 <     * <pre> {@code
2698 <     * Map<Key, String> map = ...;
2699 <     * final String msg = ...;
2700 <     * map.compute(key, new BiFun<Key, String, String>() {
2701 <     *   public String apply(Key k, String v) {
2702 <     *    return (v == null) ? msg : v + msg;});}}</pre>
2703 <     *
2704 <     * @param key key with which the specified value is to be associated
2705 <     * @param remappingFunction the function to compute a value
2706 <     * @return the new value associated with the specified key, or null if none
2707 <     * @throws NullPointerException if the specified key or remappingFunction
2708 <     *         is null
2709 <     * @throws IllegalStateException if the computation detectably
2710 <     *         attempts a recursive update to this map that would
2711 <     *         otherwise never complete
2712 <     * @throws RuntimeException or Error if the remappingFunction does so,
2713 <     *         in which case the mapping is unchanged
2714 <     */
2715 <    //    @SuppressWarnings("unchecked")
2716 <    public V compute(K key, BiFun<? super K, ? super V, ? extends V> remappingFunction) {
2717 <        if (key == null || remappingFunction == null)
2718 <            throw new NullPointerException();
2719 <        return (V)internalCompute(key, false, remappingFunction);
2720 <    }
2721 <
2722 <    /**
2723 <     * If the specified key is not already associated
2724 <     * with a value, associate it with the given value.
2725 <     * Otherwise, replace the value with the results of
2726 <     * the given remapping function. This is equivalent to:
2727 <     *  <pre> {@code
2728 <     *   if (!map.containsKey(key))
2729 <     *     map.put(value);
2730 <     *   else {
2731 <     *     newValue = remappingFunction.apply(map.get(key), value);
2732 <     *     if (value != null)
2733 <     *       map.put(key, value);
2734 <     *     else
2735 <     *       map.remove(key);
2736 <     *   }
2737 <     * }</pre>
2738 <     * except that the action is performed atomically.  If the
2739 <     * function returns {@code null}, the mapping is removed.  If the
2740 <     * function itself throws an (unchecked) exception, the exception
2741 <     * is rethrown to its caller, and the current mapping is left
2742 <     * unchanged.  Some attempted update operations on this map by
2743 <     * other threads may be blocked while computation is in progress,
2744 <     * so the computation should be short and simple, and must not
2745 <     * attempt to update any other mappings of this Map.
2746 <     */
2747 <    //    @SuppressWarnings("unchecked")
2748 <    public V merge(K key, V value, BiFun<? super V, ? super V, ? extends V> remappingFunction) {
2749 <        if (key == null || value == null || remappingFunction == null)
2750 <            throw new NullPointerException();
2751 <        return (V)internalMerge(key, value, remappingFunction);
1089 >        tryPresize(m.size());
1090 >        for (Map.Entry<? extends K, ? extends V> e : m.entrySet())
1091 >            putVal(e.getKey(), e.getValue(), false);
1092      }
1093  
1094      /**
# Line 2760 | Line 1100 | public class ConcurrentHashMapV8<K, V>
1100       *         {@code null} if there was no mapping for {@code key}
1101       * @throws NullPointerException if the specified key is null
1102       */
1103 <    @SuppressWarnings("unchecked")
1104 <        public V remove(Object key) {
2765 <        if (key == null)
2766 <            throw new NullPointerException();
2767 <        return (V)internalReplace(key, null, null);
1103 >    public V remove(Object key) {
1104 >        return replaceNode(key, null, null);
1105      }
1106  
1107      /**
1108 <     * {@inheritDoc}
1109 <     *
1110 <     * @throws NullPointerException if the specified key is null
2774 <     */
2775 <    public boolean remove(Object key, Object value) {
2776 <        if (key == null)
2777 <            throw new NullPointerException();
2778 <        if (value == null)
2779 <            return false;
2780 <        return internalReplace(key, null, value) != null;
2781 <    }
2782 <
2783 <    /**
2784 <     * {@inheritDoc}
2785 <     *
2786 <     * @throws NullPointerException if any of the arguments are null
2787 <     */
2788 <    public boolean replace(K key, V oldValue, V newValue) {
2789 <        if (key == null || oldValue == null || newValue == null)
2790 <            throw new NullPointerException();
2791 <        return internalReplace(key, newValue, oldValue) != null;
2792 <    }
2793 <
2794 <    /**
2795 <     * {@inheritDoc}
2796 <     *
2797 <     * @return the previous value associated with the specified key,
2798 <     *         or {@code null} if there was no mapping for the key
2799 <     * @throws NullPointerException if the specified key or value is null
1108 >     * Implementation for the four public remove/replace methods:
1109 >     * Replaces node value with v, conditional upon match of cv if
1110 >     * non-null.  If resulting value is null, delete.
1111       */
1112 <    @SuppressWarnings("unchecked")
1113 <        public V replace(K key, V value) {
1114 <        if (key == null || value == null)
1115 <            throw new NullPointerException();
1116 <        return (V)internalReplace(key, value, null);
1112 >    final V replaceNode(Object key, V value, Object cv) {
1113 >        int hash = spread(key.hashCode());
1114 >        for (Node<K,V>[] tab = table;;) {
1115 >            Node<K,V> f; int n, i, fh;
1116 >            if (tab == null || (n = tab.length) == 0 ||
1117 >                (f = tabAt(tab, i = (n - 1) & hash)) == null)
1118 >                break;
1119 >            else if ((fh = f.hash) == MOVED)
1120 >                tab = helpTransfer(tab, f);
1121 >            else {
1122 >                V oldVal = null;
1123 >                boolean validated = false;
1124 >                synchronized (f) {
1125 >                    if (tabAt(tab, i) == f) {
1126 >                        if (fh >= 0) {
1127 >                            validated = true;
1128 >                            for (Node<K,V> e = f, pred = null;;) {
1129 >                                K ek;
1130 >                                if (e.hash == hash &&
1131 >                                    ((ek = e.key) == key ||
1132 >                                     (ek != null && key.equals(ek)))) {
1133 >                                    V ev = e.val;
1134 >                                    if (cv == null || cv == ev ||
1135 >                                        (ev != null && cv.equals(ev))) {
1136 >                                        oldVal = ev;
1137 >                                        if (value != null)
1138 >                                            e.val = value;
1139 >                                        else if (pred != null)
1140 >                                            pred.next = e.next;
1141 >                                        else
1142 >                                            setTabAt(tab, i, e.next);
1143 >                                    }
1144 >                                    break;
1145 >                                }
1146 >                                pred = e;
1147 >                                if ((e = e.next) == null)
1148 >                                    break;
1149 >                            }
1150 >                        }
1151 >                        else if (f instanceof TreeBin) {
1152 >                            validated = true;
1153 >                            TreeBin<K,V> t = (TreeBin<K,V>)f;
1154 >                            TreeNode<K,V> r, p;
1155 >                            if ((r = t.root) != null &&
1156 >                                (p = r.findTreeNode(hash, key, null)) != null) {
1157 >                                V pv = p.val;
1158 >                                if (cv == null || cv == pv ||
1159 >                                    (pv != null && cv.equals(pv))) {
1160 >                                    oldVal = pv;
1161 >                                    if (value != null)
1162 >                                        p.val = value;
1163 >                                    else if (t.removeTreeNode(p))
1164 >                                        setTabAt(tab, i, untreeify(t.first));
1165 >                                }
1166 >                            }
1167 >                        }
1168 >                    }
1169 >                }
1170 >                if (validated) {
1171 >                    if (oldVal != null) {
1172 >                        if (value == null)
1173 >                            addCount(-1L, -1);
1174 >                        return oldVal;
1175 >                    }
1176 >                    break;
1177 >                }
1178 >            }
1179 >        }
1180 >        return null;
1181      }
1182  
1183      /**
1184       * Removes all of the mappings from this map.
1185       */
1186      public void clear() {
1187 <        internalClear();
1187 >        long delta = 0L; // negative number of deletions
1188 >        int i = 0;
1189 >        Node<K,V>[] tab = table;
1190 >        while (tab != null && i < tab.length) {
1191 >            int fh;
1192 >            Node<K,V> f = tabAt(tab, i);
1193 >            if (f == null)
1194 >                ++i;
1195 >            else if ((fh = f.hash) == MOVED) {
1196 >                tab = helpTransfer(tab, f);
1197 >                i = 0; // restart
1198 >            }
1199 >            else {
1200 >                synchronized (f) {
1201 >                    if (tabAt(tab, i) == f) {
1202 >                        Node<K,V> p = (fh >= 0 ? f :
1203 >                                       (f instanceof TreeBin) ?
1204 >                                       ((TreeBin<K,V>)f).first : null);
1205 >                        while (p != null) {
1206 >                            --delta;
1207 >                            p = p.next;
1208 >                        }
1209 >                        setTabAt(tab, i++, null);
1210 >                    }
1211 >                }
1212 >            }
1213 >        }
1214 >        if (delta != 0L)
1215 >            addCount(delta, -1);
1216      }
1217  
1218      /**
1219       * Returns a {@link Set} view of the keys contained in this map.
1220       * The set is backed by the map, so changes to the map are
1221 <     * reflected in the set, and vice-versa.  The set supports element
1221 >     * reflected in the set, and vice-versa. The set supports element
1222       * removal, which removes the corresponding mapping from this map,
1223       * via the {@code Iterator.remove}, {@code Set.remove},
1224       * {@code removeAll}, {@code retainAll}, and {@code clear}
# Line 2827 | Line 1230 | public class ConcurrentHashMapV8<K, V>
1230       * and guarantees to traverse elements as they existed upon
1231       * construction of the iterator, and may (but is not guaranteed to)
1232       * reflect any modifications subsequent to construction.
1233 +     *
1234 +     * @return the set view
1235       */
1236 <    public Set<K> keySet() {
1237 <        KeySet<K,V> ks = keySet;
1238 <        return (ks != null) ? ks : (keySet = new KeySet<K,V>(this));
1236 >    public KeySetView<K,V> keySet() {
1237 >        KeySetView<K,V> ks;
1238 >        return (ks = keySet) != null ? ks : (keySet = new KeySetView<K,V>(this, null));
1239      }
1240  
1241      /**
# Line 2848 | Line 1253 | public class ConcurrentHashMapV8<K, V>
1253       * and guarantees to traverse elements as they existed upon
1254       * construction of the iterator, and may (but is not guaranteed to)
1255       * reflect any modifications subsequent to construction.
1256 +     *
1257 +     * @return the collection view
1258       */
1259      public Collection<V> values() {
1260 <        Values<K,V> vs = values;
1261 <        return (vs != null) ? vs : (values = new Values<K,V>(this));
1260 >        ValuesView<K,V> vs;
1261 >        return (vs = values) != null ? vs : (values = new ValuesView<K,V>(this));
1262      }
1263  
1264      /**
# Line 2861 | Line 1268 | public class ConcurrentHashMapV8<K, V>
1268       * removal, which removes the corresponding mapping from the map,
1269       * via the {@code Iterator.remove}, {@code Set.remove},
1270       * {@code removeAll}, {@code retainAll}, and {@code clear}
1271 <     * operations.  It does not support the {@code add} or
2865 <     * {@code addAll} operations.
1271 >     * operations.
1272       *
1273       * <p>The view's {@code iterator} is a "weakly consistent" iterator
1274       * that will never throw {@link ConcurrentModificationException},
1275       * and guarantees to traverse elements as they existed upon
1276       * construction of the iterator, and may (but is not guaranteed to)
1277       * reflect any modifications subsequent to construction.
2872     */
2873    public Set<Map.Entry<K,V>> entrySet() {
2874        EntrySet<K,V> es = entrySet;
2875        return (es != null) ? es : (entrySet = new EntrySet<K,V>(this));
2876    }
2877
2878    /**
2879     * Returns an enumeration of the keys in this table.
2880     *
2881     * @return an enumeration of the keys in this table
2882     * @see #keySet()
2883     */
2884    public Enumeration<K> keys() {
2885        return new KeyIterator<K,V>(this);
2886    }
2887
2888    /**
2889     * Returns an enumeration of the values in this table.
1278       *
1279 <     * @return an enumeration of the values in this table
2892 <     * @see #values()
1279 >     * @return the set view
1280       */
1281 <    public Enumeration<V> elements() {
1282 <        return new ValueIterator<K,V>(this);
1283 <    }
2897 <
2898 <    /**
2899 <     * Returns a partitionable iterator of the keys in this map.
2900 <     *
2901 <     * @return a partitionable iterator of the keys in this map
2902 <     */
2903 <    public Spliterator<K> keySpliterator() {
2904 <        return new KeyIterator<K,V>(this);
2905 <    }
2906 <
2907 <    /**
2908 <     * Returns a partitionable iterator of the values in this map.
2909 <     *
2910 <     * @return a partitionable iterator of the values in this map
2911 <     */
2912 <    public Spliterator<V> valueSpliterator() {
2913 <        return new ValueIterator<K,V>(this);
2914 <    }
2915 <
2916 <    /**
2917 <     * Returns a partitionable iterator of the entries in this map.
2918 <     *
2919 <     * @return a partitionable iterator of the entries in this map
2920 <     */
2921 <    public Spliterator<Map.Entry<K,V>> entrySpliterator() {
2922 <        return new EntryIterator<K,V>(this);
1281 >    public Set<Map.Entry<K,V>> entrySet() {
1282 >        EntrySetView<K,V> es;
1283 >        return (es = entrySet) != null ? es : (entrySet = new EntrySetView<K,V>(this));
1284      }
1285  
1286      /**
# Line 2931 | Line 1292 | public class ConcurrentHashMapV8<K, V>
1292       */
1293      public int hashCode() {
1294          int h = 0;
1295 <        Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
1296 <        Object v;
1297 <        while ((v = it.advance()) != null) {
1298 <            h += it.nextKey.hashCode() ^ v.hashCode();
1295 >        Node<K,V>[] t;
1296 >        if ((t = table) != null) {
1297 >            Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length);
1298 >            for (Node<K,V> p; (p = it.advance()) != null; )
1299 >                h += p.key.hashCode() ^ p.val.hashCode();
1300          }
1301          return h;
1302      }
# Line 2951 | Line 1313 | public class ConcurrentHashMapV8<K, V>
1313       * @return a string representation of this map
1314       */
1315      public String toString() {
1316 <        Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
1316 >        Node<K,V>[] t;
1317 >        int f = (t = table) == null ? 0 : t.length;
1318 >        Traverser<K,V> it = new Traverser<K,V>(t, f, 0, f);
1319          StringBuilder sb = new StringBuilder();
1320          sb.append('{');
1321 <        Object v;
1322 <        if ((v = it.advance()) != null) {
1321 >        Node<K,V> p;
1322 >        if ((p = it.advance()) != null) {
1323              for (;;) {
1324 <                Object k = it.nextKey;
1324 >                K k = p.key;
1325 >                V v = p.val;
1326                  sb.append(k == this ? "(this Map)" : k);
1327                  sb.append('=');
1328                  sb.append(v == this ? "(this Map)" : v);
1329 <                if ((v = it.advance()) == null)
1329 >                if ((p = it.advance()) == null)
1330                      break;
1331                  sb.append(',').append(' ');
1332              }
# Line 2984 | Line 1349 | public class ConcurrentHashMapV8<K, V>
1349              if (!(o instanceof Map))
1350                  return false;
1351              Map<?,?> m = (Map<?,?>) o;
1352 <            Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
1353 <            Object val;
1354 <            while ((val = it.advance()) != null) {
1355 <                Object v = m.get(it.nextKey);
1352 >            Node<K,V>[] t;
1353 >            int f = (t = table) == null ? 0 : t.length;
1354 >            Traverser<K,V> it = new Traverser<K,V>(t, f, 0, f);
1355 >            for (Node<K,V> p; (p = it.advance()) != null; ) {
1356 >                V val = p.val;
1357 >                Object v = m.get(p.key);
1358                  if (v == null || (v != val && !v.equals(val)))
1359                      return false;
1360              }
# Line 2995 | Line 1362 | public class ConcurrentHashMapV8<K, V>
1362                  Object mk, mv, v;
1363                  if ((mk = e.getKey()) == null ||
1364                      (mv = e.getValue()) == null ||
1365 <                    (v = internalGet(mk)) == null ||
1365 >                    (v = get(mk)) == null ||
1366                      (mv != v && !mv.equals(v)))
1367                      return false;
1368              }
# Line 3003 | Line 1370 | public class ConcurrentHashMapV8<K, V>
1370          return true;
1371      }
1372  
1373 <    /* ----------------Iterators -------------- */
1373 >    /**
1374 >     * Stripped-down version of helper class used in previous version,
1375 >     * declared for the sake of serialization compatibility
1376 >     */
1377 >    static class Segment<K,V> extends ReentrantLock implements Serializable {
1378 >        private static final long serialVersionUID = 2249069246763182397L;
1379 >        final float loadFactor;
1380 >        Segment(float lf) { this.loadFactor = lf; }
1381 >    }
1382  
1383 <    static final class KeyIterator<K,V> extends Traverser<K,V,Object>
1384 <        implements Spliterator<K>, Enumeration<K> {
1385 <        KeyIterator(ConcurrentHashMapV8<K, V> map) { super(map); }
1386 <        KeyIterator(Traverser<K,V,Object> it, boolean split) {
1387 <            super(it, split);
1388 <        }
1389 <        public KeyIterator<K,V> split() {
1390 <            if (last != null || (next != null && nextVal == null))
1391 <                throw new IllegalStateException();
1392 <            return new KeyIterator<K,V>(this, true);
1393 <        }
1394 <        @SuppressWarnings("unchecked")
1395 <            public final K next() {
1396 <            if (nextVal == null && advance() == null)
1397 <                throw new NoSuchElementException();
1398 <            Object k = nextKey;
1399 <            nextVal = null;
1400 <            return (K) k;
1383 >    /**
1384 >     * Saves the state of the {@code ConcurrentHashMapV8} instance to a
1385 >     * stream (i.e., serializes it).
1386 >     * @param s the stream
1387 >     * @throws java.io.IOException if an I/O error occurs
1388 >     * @serialData
1389 >     * the key (Object) and value (Object)
1390 >     * for each key-value mapping, followed by a null pair.
1391 >     * The key-value mappings are emitted in no particular order.
1392 >     */
1393 >    private void writeObject(java.io.ObjectOutputStream s)
1394 >        throws java.io.IOException {
1395 >        // For serialization compatibility
1396 >        // Emulate segment calculation from previous version of this class
1397 >        int sshift = 0;
1398 >        int ssize = 1;
1399 >        while (ssize < DEFAULT_CONCURRENCY_LEVEL) {
1400 >            ++sshift;
1401 >            ssize <<= 1;
1402 >        }
1403 >        int segmentShift = 32 - sshift;
1404 >        int segmentMask = ssize - 1;
1405 >        @SuppressWarnings("unchecked") Segment<K,V>[] segments = (Segment<K,V>[])
1406 >            new Segment<?,?>[DEFAULT_CONCURRENCY_LEVEL];
1407 >        for (int i = 0; i < segments.length; ++i)
1408 >            segments[i] = new Segment<K,V>(LOAD_FACTOR);
1409 >        s.putFields().put("segments", segments);
1410 >        s.putFields().put("segmentShift", segmentShift);
1411 >        s.putFields().put("segmentMask", segmentMask);
1412 >        s.writeFields();
1413 >
1414 >        Node<K,V>[] t;
1415 >        if ((t = table) != null) {
1416 >            Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length);
1417 >            for (Node<K,V> p; (p = it.advance()) != null; ) {
1418 >                s.writeObject(p.key);
1419 >                s.writeObject(p.val);
1420 >            }
1421          }
1422 <
1423 <        public final K nextElement() { return next(); }
1422 >        s.writeObject(null);
1423 >        s.writeObject(null);
1424 >        segments = null; // throw away
1425      }
1426  
1427 <    static final class ValueIterator<K,V> extends Traverser<K,V,Object>
1428 <        implements Spliterator<V>, Enumeration<V> {
1429 <        ValueIterator(ConcurrentHashMapV8<K, V> map) { super(map); }
1430 <        ValueIterator(Traverser<K,V,Object> it, boolean split) {
1431 <            super(it, split);
1432 <        }
1433 <        public ValueIterator<K,V> split() {
1434 <            if (last != null || (next != null && nextVal == null))
1435 <                throw new IllegalStateException();
1436 <            return new ValueIterator<K,V>(this, true);
1427 >    /**
1428 >     * Reconstitutes the instance from a stream (that is, deserializes it).
1429 >     * @param s the stream
1430 >     * @throws ClassNotFoundException if the class of a serialized object
1431 >     *         could not be found
1432 >     * @throws java.io.IOException if an I/O error occurs
1433 >     */
1434 >    private void readObject(java.io.ObjectInputStream s)
1435 >        throws java.io.IOException, ClassNotFoundException {
1436 >        /*
1437 >         * To improve performance in typical cases, we create nodes
1438 >         * while reading, then place in table once size is known.
1439 >         * However, we must also validate uniqueness and deal with
1440 >         * overpopulated bins while doing so, which requires
1441 >         * specialized versions of putVal mechanics.
1442 >         */
1443 >        sizeCtl = -1; // force exclusion for table construction
1444 >        s.defaultReadObject();
1445 >        long size = 0L;
1446 >        Node<K,V> p = null;
1447 >        for (;;) {
1448 >            @SuppressWarnings("unchecked") K k = (K) s.readObject();
1449 >            @SuppressWarnings("unchecked") V v = (V) s.readObject();
1450 >            if (k != null && v != null) {
1451 >                p = new Node<K,V>(spread(k.hashCode()), k, v, p);
1452 >                ++size;
1453 >            }
1454 >            else
1455 >                break;
1456          }
1457 <
1458 <        @SuppressWarnings("unchecked")
1459 <            public final V next() {
1460 <            Object v;
1461 <            if ((v = nextVal) == null && (v = advance()) == null)
1462 <                throw new NoSuchElementException();
1463 <            nextVal = null;
1464 <            return (V) v;
1457 >        if (size == 0L)
1458 >            sizeCtl = 0;
1459 >        else {
1460 >            int n;
1461 >            if (size >= (long)(MAXIMUM_CAPACITY >>> 1))
1462 >                n = MAXIMUM_CAPACITY;
1463 >            else {
1464 >                int sz = (int)size;
1465 >                n = tableSizeFor(sz + (sz >>> 1) + 1);
1466 >            }
1467 >            @SuppressWarnings("unchecked")
1468 >                Node<K,V>[] tab = (Node<K,V>[])new Node<?,?>[n];
1469 >            int mask = n - 1;
1470 >            long added = 0L;
1471 >            while (p != null) {
1472 >                boolean insertAtFront;
1473 >                Node<K,V> next = p.next, first;
1474 >                int h = p.hash, j = h & mask;
1475 >                if ((first = tabAt(tab, j)) == null)
1476 >                    insertAtFront = true;
1477 >                else {
1478 >                    K k = p.key;
1479 >                    if (first.hash < 0) {
1480 >                        TreeBin<K,V> t = (TreeBin<K,V>)first;
1481 >                        if (t.putTreeVal(h, k, p.val) == null)
1482 >                            ++added;
1483 >                        insertAtFront = false;
1484 >                    }
1485 >                    else {
1486 >                        int binCount = 0;
1487 >                        insertAtFront = true;
1488 >                        Node<K,V> q; K qk;
1489 >                        for (q = first; q != null; q = q.next) {
1490 >                            if (q.hash == h &&
1491 >                                ((qk = q.key) == k ||
1492 >                                 (qk != null && k.equals(qk)))) {
1493 >                                insertAtFront = false;
1494 >                                break;
1495 >                            }
1496 >                            ++binCount;
1497 >                        }
1498 >                        if (insertAtFront && binCount >= TREEIFY_THRESHOLD) {
1499 >                            insertAtFront = false;
1500 >                            ++added;
1501 >                            p.next = first;
1502 >                            TreeNode<K,V> hd = null, tl = null;
1503 >                            for (q = p; q != null; q = q.next) {
1504 >                                TreeNode<K,V> t = new TreeNode<K,V>
1505 >                                    (q.hash, q.key, q.val, null, null);
1506 >                                if ((t.prev = tl) == null)
1507 >                                    hd = t;
1508 >                                else
1509 >                                    tl.next = t;
1510 >                                tl = t;
1511 >                            }
1512 >                            setTabAt(tab, j, new TreeBin<K,V>(hd));
1513 >                        }
1514 >                    }
1515 >                }
1516 >                if (insertAtFront) {
1517 >                    ++added;
1518 >                    p.next = first;
1519 >                    setTabAt(tab, j, p);
1520 >                }
1521 >                p = next;
1522 >            }
1523 >            table = tab;
1524 >            sizeCtl = n - (n >>> 2);
1525 >            baseCount = added;
1526          }
3051
3052        public final V nextElement() { return next(); }
1527      }
1528  
1529 <    static final class EntryIterator<K,V> extends Traverser<K,V,Object>
3056 <        implements Spliterator<Map.Entry<K,V>> {
3057 <        EntryIterator(ConcurrentHashMapV8<K, V> map) { super(map); }
3058 <        EntryIterator(Traverser<K,V,Object> it, boolean split) {
3059 <            super(it, split);
3060 <        }
3061 <        public EntryIterator<K,V> split() {
3062 <            if (last != null || (next != null && nextVal == null))
3063 <                throw new IllegalStateException();
3064 <            return new EntryIterator<K,V>(this, true);
3065 <        }
1529 >    // ConcurrentMap methods
1530  
1531 <        @SuppressWarnings("unchecked")
1532 <            public final Map.Entry<K,V> next() {
1533 <            Object v;
1534 <            if ((v = nextVal) == null && (v = advance()) == null)
1535 <                throw new NoSuchElementException();
1536 <            Object k = nextKey;
1537 <            nextVal = null;
1538 <            return new MapEntry<K,V>((K)k, (V)v, map);
1539 <        }
1531 >    /**
1532 >     * {@inheritDoc}
1533 >     *
1534 >     * @return the previous value associated with the specified key,
1535 >     *         or {@code null} if there was no mapping for the key
1536 >     * @throws NullPointerException if the specified key or value is null
1537 >     */
1538 >    public V putIfAbsent(K key, V value) {
1539 >        return putVal(key, value, true);
1540      }
1541  
1542      /**
1543 <     * Exported Entry for iterators
1543 >     * {@inheritDoc}
1544 >     *
1545 >     * @throws NullPointerException if the specified key is null
1546       */
1547 <    static final class MapEntry<K,V> implements Map.Entry<K, V> {
1548 <        final K key; // non-null
1549 <        V val;       // non-null
1550 <        final ConcurrentHashMapV8<K, V> map;
3085 <        MapEntry(K key, V val, ConcurrentHashMapV8<K, V> map) {
3086 <            this.key = key;
3087 <            this.val = val;
3088 <            this.map = map;
3089 <        }
3090 <        public final K getKey()       { return key; }
3091 <        public final V getValue()     { return val; }
3092 <        public final int hashCode()   { return key.hashCode() ^ val.hashCode(); }
3093 <        public final String toString(){ return key + "=" + val; }
3094 <
3095 <        public final boolean equals(Object o) {
3096 <            Object k, v; Map.Entry<?,?> e;
3097 <            return ((o instanceof Map.Entry) &&
3098 <                    (k = (e = (Map.Entry<?,?>)o).getKey()) != null &&
3099 <                    (v = e.getValue()) != null &&
3100 <                    (k == key || k.equals(key)) &&
3101 <                    (v == val || v.equals(val)));
3102 <        }
3103 <
3104 <        /**
3105 <         * Sets our entry's value and writes through to the map. The
3106 <         * value to return is somewhat arbitrary here. Since we do not
3107 <         * necessarily track asynchronous changes, the most recent
3108 <         * "previous" value could be different from what we return (or
3109 <         * could even have been removed in which case the put will
3110 <         * re-establish). We do not and cannot guarantee more.
3111 <         */
3112 <        public final V setValue(V value) {
3113 <            if (value == null) throw new NullPointerException();
3114 <            V v = val;
3115 <            val = value;
3116 <            map.put(key, value);
3117 <            return v;
3118 <        }
1547 >    public boolean remove(Object key, Object value) {
1548 >        if (key == null)
1549 >            throw new NullPointerException();
1550 >        return value != null && replaceNode(key, null, value) != null;
1551      }
1552  
1553 <    /* ----------------Views -------------- */
1553 >    /**
1554 >     * {@inheritDoc}
1555 >     *
1556 >     * @throws NullPointerException if any of the arguments are null
1557 >     */
1558 >    public boolean replace(K key, V oldValue, V newValue) {
1559 >        if (key == null || oldValue == null || newValue == null)
1560 >            throw new NullPointerException();
1561 >        return replaceNode(key, newValue, oldValue) != null;
1562 >    }
1563  
1564      /**
1565 <     * Base class for views.
1565 >     * {@inheritDoc}
1566 >     *
1567 >     * @return the previous value associated with the specified key,
1568 >     *         or {@code null} if there was no mapping for the key
1569 >     * @throws NullPointerException if the specified key or value is null
1570       */
1571 <    static abstract class CHMView<K, V> {
1572 <        final ConcurrentHashMapV8<K, V> map;
1573 <        CHMView(ConcurrentHashMapV8<K, V> map)  { this.map = map; }
1574 <        public final int size()                 { return map.size(); }
1575 <        public final boolean isEmpty()          { return map.isEmpty(); }
3131 <        public final void clear()               { map.clear(); }
1571 >    public V replace(K key, V value) {
1572 >        if (key == null || value == null)
1573 >            throw new NullPointerException();
1574 >        return replaceNode(key, value, null);
1575 >    }
1576  
1577 <        // implementations below rely on concrete classes supplying these
3134 <        abstract public Iterator<?> iterator();
3135 <        abstract public boolean contains(Object o);
3136 <        abstract public boolean remove(Object o);
1577 >    // Overrides of JDK8+ Map extension method defaults
1578  
1579 <        private static final String oomeMsg = "Required array size too large";
1579 >    /**
1580 >     * Returns the value to which the specified key is mapped, or the
1581 >     * given default value if this map contains no mapping for the
1582 >     * key.
1583 >     *
1584 >     * @param key the key whose associated value is to be returned
1585 >     * @param defaultValue the value to return if this map contains
1586 >     * no mapping for the given key
1587 >     * @return the mapping for the key, if present; else the default value
1588 >     * @throws NullPointerException if the specified key is null
1589 >     */
1590 >    public V getOrDefault(Object key, V defaultValue) {
1591 >        V v;
1592 >        return (v = get(key)) == null ? defaultValue : v;
1593 >    }
1594 >
1595 >    public void forEach(BiAction<? super K, ? super V> action) {
1596 >        if (action == null) throw new NullPointerException();
1597 >        Node<K,V>[] t;
1598 >        if ((t = table) != null) {
1599 >            Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length);
1600 >            for (Node<K,V> p; (p = it.advance()) != null; ) {
1601 >                action.apply(p.key, p.val);
1602 >            }
1603 >        }
1604 >    }
1605  
1606 <        public final Object[] toArray() {
1607 <            long sz = map.mappingCount();
1608 <            if (sz > (long)(MAX_ARRAY_SIZE))
1609 <                throw new OutOfMemoryError(oomeMsg);
1610 <            int n = (int)sz;
1611 <            Object[] r = new Object[n];
1612 <            int i = 0;
1613 <            Iterator<?> it = iterator();
1614 <            while (it.hasNext()) {
1615 <                if (i == n) {
1616 <                    if (n >= MAX_ARRAY_SIZE)
1617 <                        throw new OutOfMemoryError(oomeMsg);
1618 <                    if (n >= MAX_ARRAY_SIZE - (MAX_ARRAY_SIZE >>> 1) - 1)
1619 <                        n = MAX_ARRAY_SIZE;
3154 <                    else
3155 <                        n += (n >>> 1) + 1;
3156 <                    r = Arrays.copyOf(r, n);
1606 >    public void replaceAll(BiFun<? super K, ? super V, ? extends V> function) {
1607 >        if (function == null) throw new NullPointerException();
1608 >        Node<K,V>[] t;
1609 >        if ((t = table) != null) {
1610 >            Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length);
1611 >            for (Node<K,V> p; (p = it.advance()) != null; ) {
1612 >                V oldValue = p.val;
1613 >                for (K key = p.key;;) {
1614 >                    V newValue = function.apply(key, oldValue);
1615 >                    if (newValue == null)
1616 >                        throw new NullPointerException();
1617 >                    if (replaceNode(key, newValue, oldValue) != null ||
1618 >                        (oldValue = get(key)) == null)
1619 >                        break;
1620                  }
3158                r[i++] = it.next();
1621              }
3160            return (i == n) ? r : Arrays.copyOf(r, i);
1622          }
1623 +    }
1624  
1625 <        @SuppressWarnings("unchecked")
1626 <            public final <T> T[] toArray(T[] a) {
1627 <            long sz = map.mappingCount();
1628 <            if (sz > (long)(MAX_ARRAY_SIZE))
1629 <                throw new OutOfMemoryError(oomeMsg);
1630 <            int m = (int)sz;
1631 <            T[] r = (a.length >= m) ? a :
1632 <                (T[])java.lang.reflect.Array
1633 <                .newInstance(a.getClass().getComponentType(), m);
1634 <            int n = r.length;
1635 <            int i = 0;
1636 <            Iterator<?> it = iterator();
1637 <            while (it.hasNext()) {
1638 <                if (i == n) {
1639 <                    if (n >= MAX_ARRAY_SIZE)
1640 <                        throw new OutOfMemoryError(oomeMsg);
1641 <                    if (n >= MAX_ARRAY_SIZE - (MAX_ARRAY_SIZE >>> 1) - 1)
1642 <                        n = MAX_ARRAY_SIZE;
1643 <                    else
1644 <                        n += (n >>> 1) + 1;
1645 <                    r = Arrays.copyOf(r, n);
1625 >    /**
1626 >     * If the specified key is not already associated with a value,
1627 >     * attempts to compute its value using the given mapping function
1628 >     * and enters it into this map unless {@code null}.  The entire
1629 >     * method invocation is performed atomically, so the function is
1630 >     * applied at most once per key.  Some attempted update operations
1631 >     * on this map by other threads may be blocked while computation
1632 >     * is in progress, so the computation should be short and simple,
1633 >     * and must not attempt to update any other mappings of this map.
1634 >     *
1635 >     * @param key key with which the specified value is to be associated
1636 >     * @param mappingFunction the function to compute a value
1637 >     * @return the current (existing or computed) value associated with
1638 >     *         the specified key, or null if the computed value is null
1639 >     * @throws NullPointerException if the specified key or mappingFunction
1640 >     *         is null
1641 >     * @throws IllegalStateException if the computation detectably
1642 >     *         attempts a recursive update to this map that would
1643 >     *         otherwise never complete
1644 >     * @throws RuntimeException or Error if the mappingFunction does so,
1645 >     *         in which case the mapping is left unestablished
1646 >     */
1647 >    public V computeIfAbsent(K key, Fun<? super K, ? extends V> mappingFunction) {
1648 >        if (key == null || mappingFunction == null)
1649 >            throw new NullPointerException();
1650 >        int h = spread(key.hashCode());
1651 >        V val = null;
1652 >        int binCount = 0;
1653 >        for (Node<K,V>[] tab = table;;) {
1654 >            Node<K,V> f; int n, i, fh;
1655 >            if (tab == null || (n = tab.length) == 0)
1656 >                tab = initTable();
1657 >            else if ((f = tabAt(tab, i = (n - 1) & h)) == null) {
1658 >                Node<K,V> r = new ReservationNode<K,V>();
1659 >                synchronized (r) {
1660 >                    if (casTabAt(tab, i, null, r)) {
1661 >                        binCount = 1;
1662 >                        Node<K,V> node = null;
1663 >                        try {
1664 >                            if ((val = mappingFunction.apply(key)) != null)
1665 >                                node = new Node<K,V>(h, key, val, null);
1666 >                        } finally {
1667 >                            setTabAt(tab, i, node);
1668 >                        }
1669 >                    }
1670                  }
1671 <                r[i++] = (T)it.next();
1671 >                if (binCount != 0)
1672 >                    break;
1673              }
1674 <            if (a == r && i < n) {
1675 <                r[i] = null; // null-terminate
1676 <                return r;
1674 >            else if ((fh = f.hash) == MOVED)
1675 >                tab = helpTransfer(tab, f);
1676 >            else {
1677 >                boolean added = false;
1678 >                synchronized (f) {
1679 >                    if (tabAt(tab, i) == f) {
1680 >                        if (fh >= 0) {
1681 >                            binCount = 1;
1682 >                            for (Node<K,V> e = f;; ++binCount) {
1683 >                                K ek; V ev;
1684 >                                if (e.hash == h &&
1685 >                                    ((ek = e.key) == key ||
1686 >                                     (ek != null && key.equals(ek)))) {
1687 >                                    val = e.val;
1688 >                                    break;
1689 >                                }
1690 >                                Node<K,V> pred = e;
1691 >                                if ((e = e.next) == null) {
1692 >                                    if ((val = mappingFunction.apply(key)) != null) {
1693 >                                        added = true;
1694 >                                        pred.next = new Node<K,V>(h, key, val, null);
1695 >                                    }
1696 >                                    break;
1697 >                                }
1698 >                            }
1699 >                        }
1700 >                        else if (f instanceof TreeBin) {
1701 >                            binCount = 2;
1702 >                            TreeBin<K,V> t = (TreeBin<K,V>)f;
1703 >                            TreeNode<K,V> r, p;
1704 >                            if ((r = t.root) != null &&
1705 >                                (p = r.findTreeNode(h, key, null)) != null)
1706 >                                val = p.val;
1707 >                            else if ((val = mappingFunction.apply(key)) != null) {
1708 >                                added = true;
1709 >                                t.putTreeVal(h, key, val);
1710 >                            }
1711 >                        }
1712 >                    }
1713 >                }
1714 >                if (binCount != 0) {
1715 >                    if (binCount >= TREEIFY_THRESHOLD)
1716 >                        treeifyBin(tab, i);
1717 >                    if (!added)
1718 >                        return val;
1719 >                    break;
1720 >                }
1721              }
3191            return (i == n) ? r : Arrays.copyOf(r, i);
3192        }
3193
3194        public final int hashCode() {
3195            int h = 0;
3196            for (Iterator<?> it = iterator(); it.hasNext();)
3197                h += it.next().hashCode();
3198            return h;
1722          }
1723 +        if (val != null)
1724 +            addCount(1L, binCount);
1725 +        return val;
1726 +    }
1727  
1728 <        public final String toString() {
1729 <            StringBuilder sb = new StringBuilder();
1730 <            sb.append('[');
1731 <            Iterator<?> it = iterator();
1732 <            if (it.hasNext()) {
1733 <                for (;;) {
1734 <                    Object e = it.next();
1735 <                    sb.append(e == this ? "(this Collection)" : e);
1736 <                    if (!it.hasNext())
1737 <                        break;
1738 <                    sb.append(',').append(' ');
1728 >    /**
1729 >     * If the value for the specified key is present, attempts to
1730 >     * compute a new mapping given the key and its current mapped
1731 >     * value.  The entire method invocation is performed atomically.
1732 >     * Some attempted update operations on this map by other threads
1733 >     * may be blocked while computation is in progress, so the
1734 >     * computation should be short and simple, and must not attempt to
1735 >     * update any other mappings of this map.
1736 >     *
1737 >     * @param key key with which a value may be associated
1738 >     * @param remappingFunction the function to compute a value
1739 >     * @return the new value associated with the specified key, or null if none
1740 >     * @throws NullPointerException if the specified key or remappingFunction
1741 >     *         is null
1742 >     * @throws IllegalStateException if the computation detectably
1743 >     *         attempts a recursive update to this map that would
1744 >     *         otherwise never complete
1745 >     * @throws RuntimeException or Error if the remappingFunction does so,
1746 >     *         in which case the mapping is unchanged
1747 >     */
1748 >    public V computeIfPresent(K key, BiFun<? super K, ? super V, ? extends V> remappingFunction) {
1749 >        if (key == null || remappingFunction == null)
1750 >            throw new NullPointerException();
1751 >        int h = spread(key.hashCode());
1752 >        V val = null;
1753 >        int delta = 0;
1754 >        int binCount = 0;
1755 >        for (Node<K,V>[] tab = table;;) {
1756 >            Node<K,V> f; int n, i, fh;
1757 >            if (tab == null || (n = tab.length) == 0)
1758 >                tab = initTable();
1759 >            else if ((f = tabAt(tab, i = (n - 1) & h)) == null)
1760 >                break;
1761 >            else if ((fh = f.hash) == MOVED)
1762 >                tab = helpTransfer(tab, f);
1763 >            else {
1764 >                synchronized (f) {
1765 >                    if (tabAt(tab, i) == f) {
1766 >                        if (fh >= 0) {
1767 >                            binCount = 1;
1768 >                            for (Node<K,V> e = f, pred = null;; ++binCount) {
1769 >                                K ek;
1770 >                                if (e.hash == h &&
1771 >                                    ((ek = e.key) == key ||
1772 >                                     (ek != null && key.equals(ek)))) {
1773 >                                    val = remappingFunction.apply(key, e.val);
1774 >                                    if (val != null)
1775 >                                        e.val = val;
1776 >                                    else {
1777 >                                        delta = -1;
1778 >                                        Node<K,V> en = e.next;
1779 >                                        if (pred != null)
1780 >                                            pred.next = en;
1781 >                                        else
1782 >                                            setTabAt(tab, i, en);
1783 >                                    }
1784 >                                    break;
1785 >                                }
1786 >                                pred = e;
1787 >                                if ((e = e.next) == null)
1788 >                                    break;
1789 >                            }
1790 >                        }
1791 >                        else if (f instanceof TreeBin) {
1792 >                            binCount = 2;
1793 >                            TreeBin<K,V> t = (TreeBin<K,V>)f;
1794 >                            TreeNode<K,V> r, p;
1795 >                            if ((r = t.root) != null &&
1796 >                                (p = r.findTreeNode(h, key, null)) != null) {
1797 >                                val = remappingFunction.apply(key, p.val);
1798 >                                if (val != null)
1799 >                                    p.val = val;
1800 >                                else {
1801 >                                    delta = -1;
1802 >                                    if (t.removeTreeNode(p))
1803 >                                        setTabAt(tab, i, untreeify(t.first));
1804 >                                }
1805 >                            }
1806 >                        }
1807 >                    }
1808                  }
1809 +                if (binCount != 0)
1810 +                    break;
1811              }
3214            return sb.append(']').toString();
1812          }
1813 +        if (delta != 0)
1814 +            addCount((long)delta, binCount);
1815 +        return val;
1816 +    }
1817  
1818 <        public final boolean containsAll(Collection<?> c) {
1819 <            if (c != this) {
1820 <                for (Iterator<?> it = c.iterator(); it.hasNext();) {
1821 <                    Object e = it.next();
1822 <                    if (e == null || !contains(e))
1823 <                        return false;
1818 >    /**
1819 >     * Attempts to compute a mapping for the specified key and its
1820 >     * current mapped value (or {@code null} if there is no current
1821 >     * mapping). The entire method invocation is performed atomically.
1822 >     * Some attempted update operations on this map by other threads
1823 >     * may be blocked while computation is in progress, so the
1824 >     * computation should be short and simple, and must not attempt to
1825 >     * update any other mappings of this Map.
1826 >     *
1827 >     * @param key key with which the specified value is to be associated
1828 >     * @param remappingFunction the function to compute a value
1829 >     * @return the new value associated with the specified key, or null if none
1830 >     * @throws NullPointerException if the specified key or remappingFunction
1831 >     *         is null
1832 >     * @throws IllegalStateException if the computation detectably
1833 >     *         attempts a recursive update to this map that would
1834 >     *         otherwise never complete
1835 >     * @throws RuntimeException or Error if the remappingFunction does so,
1836 >     *         in which case the mapping is unchanged
1837 >     */
1838 >    public V compute(K key,
1839 >                     BiFun<? super K, ? super V, ? extends V> remappingFunction) {
1840 >        if (key == null || remappingFunction == null)
1841 >            throw new NullPointerException();
1842 >        int h = spread(key.hashCode());
1843 >        V val = null;
1844 >        int delta = 0;
1845 >        int binCount = 0;
1846 >        for (Node<K,V>[] tab = table;;) {
1847 >            Node<K,V> f; int n, i, fh;
1848 >            if (tab == null || (n = tab.length) == 0)
1849 >                tab = initTable();
1850 >            else if ((f = tabAt(tab, i = (n - 1) & h)) == null) {
1851 >                Node<K,V> r = new ReservationNode<K,V>();
1852 >                synchronized (r) {
1853 >                    if (casTabAt(tab, i, null, r)) {
1854 >                        binCount = 1;
1855 >                        Node<K,V> node = null;
1856 >                        try {
1857 >                            if ((val = remappingFunction.apply(key, null)) != null) {
1858 >                                delta = 1;
1859 >                                node = new Node<K,V>(h, key, val, null);
1860 >                            }
1861 >                        } finally {
1862 >                            setTabAt(tab, i, node);
1863 >                        }
1864 >                    }
1865                  }
1866 +                if (binCount != 0)
1867 +                    break;
1868              }
1869 <            return true;
1870 <        }
1871 <
1872 <        public final boolean removeAll(Collection<?> c) {
1873 <            boolean modified = false;
1874 <            for (Iterator<?> it = iterator(); it.hasNext();) {
1875 <                if (c.contains(it.next())) {
1876 <                    it.remove();
1877 <                    modified = true;
1869 >            else if ((fh = f.hash) == MOVED)
1870 >                tab = helpTransfer(tab, f);
1871 >            else {
1872 >                synchronized (f) {
1873 >                    if (tabAt(tab, i) == f) {
1874 >                        if (fh >= 0) {
1875 >                            binCount = 1;
1876 >                            for (Node<K,V> e = f, pred = null;; ++binCount) {
1877 >                                K ek;
1878 >                                if (e.hash == h &&
1879 >                                    ((ek = e.key) == key ||
1880 >                                     (ek != null && key.equals(ek)))) {
1881 >                                    val = remappingFunction.apply(key, e.val);
1882 >                                    if (val != null)
1883 >                                        e.val = val;
1884 >                                    else {
1885 >                                        delta = -1;
1886 >                                        Node<K,V> en = e.next;
1887 >                                        if (pred != null)
1888 >                                            pred.next = en;
1889 >                                        else
1890 >                                            setTabAt(tab, i, en);
1891 >                                    }
1892 >                                    break;
1893 >                                }
1894 >                                pred = e;
1895 >                                if ((e = e.next) == null) {
1896 >                                    val = remappingFunction.apply(key, null);
1897 >                                    if (val != null) {
1898 >                                        delta = 1;
1899 >                                        pred.next =
1900 >                                            new Node<K,V>(h, key, val, null);
1901 >                                    }
1902 >                                    break;
1903 >                                }
1904 >                            }
1905 >                        }
1906 >                        else if (f instanceof TreeBin) {
1907 >                            binCount = 1;
1908 >                            TreeBin<K,V> t = (TreeBin<K,V>)f;
1909 >                            TreeNode<K,V> r, p;
1910 >                            if ((r = t.root) != null)
1911 >                                p = r.findTreeNode(h, key, null);
1912 >                            else
1913 >                                p = null;
1914 >                            V pv = (p == null) ? null : p.val;
1915 >                            val = remappingFunction.apply(key, pv);
1916 >                            if (val != null) {
1917 >                                if (p != null)
1918 >                                    p.val = val;
1919 >                                else {
1920 >                                    delta = 1;
1921 >                                    t.putTreeVal(h, key, val);
1922 >                                }
1923 >                            }
1924 >                            else if (p != null) {
1925 >                                delta = -1;
1926 >                                if (t.removeTreeNode(p))
1927 >                                    setTabAt(tab, i, untreeify(t.first));
1928 >                            }
1929 >                        }
1930 >                    }
1931 >                }
1932 >                if (binCount != 0) {
1933 >                    if (binCount >= TREEIFY_THRESHOLD)
1934 >                        treeifyBin(tab, i);
1935 >                    break;
1936                  }
1937              }
3236            return modified;
1938          }
1939 +        if (delta != 0)
1940 +            addCount((long)delta, binCount);
1941 +        return val;
1942 +    }
1943  
1944 <        public final boolean retainAll(Collection<?> c) {
1945 <            boolean modified = false;
1946 <            for (Iterator<?> it = iterator(); it.hasNext();) {
1947 <                if (!c.contains(it.next())) {
1948 <                    it.remove();
1949 <                    modified = true;
1944 >    /**
1945 >     * If the specified key is not already associated with a
1946 >     * (non-null) value, associates it with the given value.
1947 >     * Otherwise, replaces the value with the results of the given
1948 >     * remapping function, or removes if {@code null}. The entire
1949 >     * method invocation is performed atomically.  Some attempted
1950 >     * update operations on this map by other threads may be blocked
1951 >     * while computation is in progress, so the computation should be
1952 >     * short and simple, and must not attempt to update any other
1953 >     * mappings of this Map.
1954 >     *
1955 >     * @param key key with which the specified value is to be associated
1956 >     * @param value the value to use if absent
1957 >     * @param remappingFunction the function to recompute a value if present
1958 >     * @return the new value associated with the specified key, or null if none
1959 >     * @throws NullPointerException if the specified key or the
1960 >     *         remappingFunction is null
1961 >     * @throws RuntimeException or Error if the remappingFunction does so,
1962 >     *         in which case the mapping is unchanged
1963 >     */
1964 >    public V merge(K key, V value, BiFun<? super V, ? super V, ? extends V> remappingFunction) {
1965 >        if (key == null || value == null || remappingFunction == null)
1966 >            throw new NullPointerException();
1967 >        int h = spread(key.hashCode());
1968 >        V val = null;
1969 >        int delta = 0;
1970 >        int binCount = 0;
1971 >        for (Node<K,V>[] tab = table;;) {
1972 >            Node<K,V> f; int n, i, fh;
1973 >            if (tab == null || (n = tab.length) == 0)
1974 >                tab = initTable();
1975 >            else if ((f = tabAt(tab, i = (n - 1) & h)) == null) {
1976 >                if (casTabAt(tab, i, null, new Node<K,V>(h, key, value, null))) {
1977 >                    delta = 1;
1978 >                    val = value;
1979 >                    break;
1980 >                }
1981 >            }
1982 >            else if ((fh = f.hash) == MOVED)
1983 >                tab = helpTransfer(tab, f);
1984 >            else {
1985 >                synchronized (f) {
1986 >                    if (tabAt(tab, i) == f) {
1987 >                        if (fh >= 0) {
1988 >                            binCount = 1;
1989 >                            for (Node<K,V> e = f, pred = null;; ++binCount) {
1990 >                                K ek;
1991 >                                if (e.hash == h &&
1992 >                                    ((ek = e.key) == key ||
1993 >                                     (ek != null && key.equals(ek)))) {
1994 >                                    val = remappingFunction.apply(e.val, value);
1995 >                                    if (val != null)
1996 >                                        e.val = val;
1997 >                                    else {
1998 >                                        delta = -1;
1999 >                                        Node<K,V> en = e.next;
2000 >                                        if (pred != null)
2001 >                                            pred.next = en;
2002 >                                        else
2003 >                                            setTabAt(tab, i, en);
2004 >                                    }
2005 >                                    break;
2006 >                                }
2007 >                                pred = e;
2008 >                                if ((e = e.next) == null) {
2009 >                                    delta = 1;
2010 >                                    val = value;
2011 >                                    pred.next =
2012 >                                        new Node<K,V>(h, key, val, null);
2013 >                                    break;
2014 >                                }
2015 >                            }
2016 >                        }
2017 >                        else if (f instanceof TreeBin) {
2018 >                            binCount = 2;
2019 >                            TreeBin<K,V> t = (TreeBin<K,V>)f;
2020 >                            TreeNode<K,V> r = t.root;
2021 >                            TreeNode<K,V> p = (r == null) ? null :
2022 >                                r.findTreeNode(h, key, null);
2023 >                            val = (p == null) ? value :
2024 >                                remappingFunction.apply(p.val, value);
2025 >                            if (val != null) {
2026 >                                if (p != null)
2027 >                                    p.val = val;
2028 >                                else {
2029 >                                    delta = 1;
2030 >                                    t.putTreeVal(h, key, val);
2031 >                                }
2032 >                            }
2033 >                            else if (p != null) {
2034 >                                delta = -1;
2035 >                                if (t.removeTreeNode(p))
2036 >                                    setTabAt(tab, i, untreeify(t.first));
2037 >                            }
2038 >                        }
2039 >                    }
2040 >                }
2041 >                if (binCount != 0) {
2042 >                    if (binCount >= TREEIFY_THRESHOLD)
2043 >                        treeifyBin(tab, i);
2044 >                    break;
2045                  }
2046              }
3247            return modified;
2047          }
2048 +        if (delta != 0)
2049 +            addCount((long)delta, binCount);
2050 +        return val;
2051 +    }
2052 +
2053 +    // Hashtable legacy methods
2054 +
2055 +    /**
2056 +     * Legacy method testing if some key maps into the specified value
2057 +     * in this table.  This method is identical in functionality to
2058 +     * {@link #containsValue(Object)}, and exists solely to ensure
2059 +     * full compatibility with class {@link java.util.Hashtable},
2060 +     * which supported this method prior to introduction of the
2061 +     * Java Collections framework.
2062 +     *
2063 +     * @param  value a value to search for
2064 +     * @return {@code true} if and only if some key maps to the
2065 +     *         {@code value} argument in this table as
2066 +     *         determined by the {@code equals} method;
2067 +     *         {@code false} otherwise
2068 +     * @throws NullPointerException if the specified value is null
2069 +     */
2070 +    @Deprecated public boolean contains(Object value) {
2071 +        return containsValue(value);
2072 +    }
2073  
2074 +    /**
2075 +     * Returns an enumeration of the keys in this table.
2076 +     *
2077 +     * @return an enumeration of the keys in this table
2078 +     * @see #keySet()
2079 +     */
2080 +    public Enumeration<K> keys() {
2081 +        Node<K,V>[] t;
2082 +        int f = (t = table) == null ? 0 : t.length;
2083 +        return new KeyIterator<K,V>(t, f, 0, f, this);
2084      }
2085  
2086 <    static final class KeySet<K,V> extends CHMView<K,V> implements Set<K> {
2087 <        KeySet(ConcurrentHashMapV8<K, V> map)  {
2088 <            super(map);
2089 <        }
2090 <        public final boolean contains(Object o) { return map.containsKey(o); }
2091 <        public final boolean remove(Object o)   { return map.remove(o) != null; }
2092 <        public final Iterator<K> iterator() {
2093 <            return new KeyIterator<K,V>(map);
2094 <        }
2095 <        public final boolean add(K e) {
3262 <            throw new UnsupportedOperationException();
3263 <        }
3264 <        public final boolean addAll(Collection<? extends K> c) {
3265 <            throw new UnsupportedOperationException();
3266 <        }
3267 <        public boolean equals(Object o) {
3268 <            Set<?> c;
3269 <            return ((o instanceof Set) &&
3270 <                    ((c = (Set<?>)o) == this ||
3271 <                     (containsAll(c) && c.containsAll(this))));
3272 <        }
2086 >    /**
2087 >     * Returns an enumeration of the values in this table.
2088 >     *
2089 >     * @return an enumeration of the values in this table
2090 >     * @see #values()
2091 >     */
2092 >    public Enumeration<V> elements() {
2093 >        Node<K,V>[] t;
2094 >        int f = (t = table) == null ? 0 : t.length;
2095 >        return new ValueIterator<K,V>(t, f, 0, f, this);
2096      }
2097  
2098 +    // ConcurrentHashMapV8-only methods
2099  
2100 <    static final class Values<K,V> extends CHMView<K,V>
2101 <        implements Collection<V> {
2102 <        Values(ConcurrentHashMapV8<K, V> map)   { super(map); }
2103 <        public final boolean contains(Object o) { return map.containsValue(o); }
2104 <        public final boolean remove(Object o) {
2105 <            if (o != null) {
2106 <                Iterator<V> it = new ValueIterator<K,V>(map);
2107 <                while (it.hasNext()) {
2108 <                    if (o.equals(it.next())) {
2109 <                        it.remove();
2110 <                        return true;
2100 >    /**
2101 >     * Returns the number of mappings. This method should be used
2102 >     * instead of {@link #size} because a ConcurrentHashMapV8 may
2103 >     * contain more mappings than can be represented as an int. The
2104 >     * value returned is an estimate; the actual count may differ if
2105 >     * there are concurrent insertions or removals.
2106 >     *
2107 >     * @return the number of mappings
2108 >     * @since 1.8
2109 >     */
2110 >    public long mappingCount() {
2111 >        long n = sumCount();
2112 >        return (n < 0L) ? 0L : n; // ignore transient negative values
2113 >    }
2114 >
2115 >    /**
2116 >     * Creates a new {@link Set} backed by a ConcurrentHashMapV8
2117 >     * from the given type to {@code Boolean.TRUE}.
2118 >     *
2119 >     * @return the new set
2120 >     * @since 1.8
2121 >     */
2122 >    public static <K> KeySetView<K,Boolean> newKeySet() {
2123 >        return new KeySetView<K,Boolean>
2124 >            (new ConcurrentHashMapV8<K,Boolean>(), Boolean.TRUE);
2125 >    }
2126 >
2127 >    /**
2128 >     * Creates a new {@link Set} backed by a ConcurrentHashMapV8
2129 >     * from the given type to {@code Boolean.TRUE}.
2130 >     *
2131 >     * @param initialCapacity The implementation performs internal
2132 >     * sizing to accommodate this many elements.
2133 >     * @return the new set
2134 >     * @throws IllegalArgumentException if the initial capacity of
2135 >     * elements is negative
2136 >     * @since 1.8
2137 >     */
2138 >    public static <K> KeySetView<K,Boolean> newKeySet(int initialCapacity) {
2139 >        return new KeySetView<K,Boolean>
2140 >            (new ConcurrentHashMapV8<K,Boolean>(initialCapacity), Boolean.TRUE);
2141 >    }
2142 >
2143 >    /**
2144 >     * Returns a {@link Set} view of the keys in this map, using the
2145 >     * given common mapped value for any additions (i.e., {@link
2146 >     * Collection#add} and {@link Collection#addAll(Collection)}).
2147 >     * This is of course only appropriate if it is acceptable to use
2148 >     * the same value for all additions from this view.
2149 >     *
2150 >     * @param mappedValue the mapped value to use for any additions
2151 >     * @return the set view
2152 >     * @throws NullPointerException if the mappedValue is null
2153 >     */
2154 >    public KeySetView<K,V> keySet(V mappedValue) {
2155 >        if (mappedValue == null)
2156 >            throw new NullPointerException();
2157 >        return new KeySetView<K,V>(this, mappedValue);
2158 >    }
2159 >
2160 >    /* ---------------- Special Nodes -------------- */
2161 >
2162 >    /**
2163 >     * A node inserted at head of bins during transfer operations.
2164 >     */
2165 >    static final class ForwardingNode<K,V> extends Node<K,V> {
2166 >        final Node<K,V>[] nextTable;
2167 >        ForwardingNode(Node<K,V>[] tab) {
2168 >            super(MOVED, null, null, null);
2169 >            this.nextTable = tab;
2170 >        }
2171 >
2172 >        Node<K,V> find(int h, Object k) {
2173 >            // loop to avoid arbitrarily deep recursion on forwarding nodes
2174 >            outer: for (Node<K,V>[] tab = nextTable;;) {
2175 >                Node<K,V> e; int n;
2176 >                if (k == null || tab == null || (n = tab.length) == 0 ||
2177 >                    (e = tabAt(tab, (n - 1) & h)) == null)
2178 >                    return null;
2179 >                for (;;) {
2180 >                    int eh; K ek;
2181 >                    if ((eh = e.hash) == h &&
2182 >                        ((ek = e.key) == k || (ek != null && k.equals(ek))))
2183 >                        return e;
2184 >                    if (eh < 0) {
2185 >                        if (e instanceof ForwardingNode) {
2186 >                            tab = ((ForwardingNode<K,V>)e).nextTable;
2187 >                            continue outer;
2188 >                        }
2189 >                        else
2190 >                            return e.find(h, k);
2191                      }
2192 +                    if ((e = e.next) == null)
2193 +                        return null;
2194                  }
2195              }
3290            return false;
3291        }
3292        public final Iterator<V> iterator() {
3293            return new ValueIterator<K,V>(map);
3294        }
3295        public final boolean add(V e) {
3296            throw new UnsupportedOperationException();
3297        }
3298        public final boolean addAll(Collection<? extends V> c) {
3299            throw new UnsupportedOperationException();
2196          }
3301
2197      }
2198  
2199 <    static final class EntrySet<K,V> extends CHMView<K,V>
2200 <        implements Set<Map.Entry<K,V>> {
2201 <        EntrySet(ConcurrentHashMapV8<K, V> map) { super(map); }
2202 <        public final boolean contains(Object o) {
2203 <            Object k, v, r; Map.Entry<?,?> e;
2204 <            return ((o instanceof Map.Entry) &&
3310 <                    (k = (e = (Map.Entry<?,?>)o).getKey()) != null &&
3311 <                    (r = map.get(k)) != null &&
3312 <                    (v = e.getValue()) != null &&
3313 <                    (v == r || v.equals(r)));
3314 <        }
3315 <        public final boolean remove(Object o) {
3316 <            Object k, v; Map.Entry<?,?> e;
3317 <            return ((o instanceof Map.Entry) &&
3318 <                    (k = (e = (Map.Entry<?,?>)o).getKey()) != null &&
3319 <                    (v = e.getValue()) != null &&
3320 <                    map.remove(k, v));
3321 <        }
3322 <        public final Iterator<Map.Entry<K,V>> iterator() {
3323 <            return new EntryIterator<K,V>(map);
3324 <        }
3325 <        public final boolean add(Entry<K,V> e) {
3326 <            throw new UnsupportedOperationException();
3327 <        }
3328 <        public final boolean addAll(Collection<? extends Entry<K,V>> c) {
3329 <            throw new UnsupportedOperationException();
2199 >    /**
2200 >     * A place-holder node used in computeIfAbsent and compute
2201 >     */
2202 >    static final class ReservationNode<K,V> extends Node<K,V> {
2203 >        ReservationNode() {
2204 >            super(RESERVED, null, null, null);
2205          }
2206 <        public boolean equals(Object o) {
2207 <            Set<?> c;
2208 <            return ((o instanceof Set) &&
3334 <                    ((c = (Set<?>)o) == this ||
3335 <                     (containsAll(c) && c.containsAll(this))));
2206 >
2207 >        Node<K,V> find(int h, Object k) {
2208 >            return null;
2209          }
2210      }
2211  
2212 <    /* ---------------- Serialization Support -------------- */
2212 >    /* ---------------- Table Initialization and Resizing -------------- */
2213  
2214      /**
2215 <     * Stripped-down version of helper class used in previous version,
2216 <     * declared for the sake of serialization compatibility
2215 >     * Returns the stamp bits for resizing a table of size n.
2216 >     * Must be negative when shifted left by RESIZE_STAMP_SHIFT.
2217       */
2218 <    static class Segment<K,V> implements Serializable {
2219 <        private static final long serialVersionUID = 2249069246763182397L;
3347 <        final float loadFactor;
3348 <        Segment(float lf) { this.loadFactor = lf; }
2218 >    static final int resizeStamp(int n) {
2219 >        return Integer.numberOfLeadingZeros(n) | (1 << (RESIZE_STAMP_BITS - 1));
2220      }
2221  
2222      /**
2223 <     * Saves the state of the {@code ConcurrentHashMapV8} instance to a
3353 <     * stream (i.e., serializes it).
3354 <     * @param s the stream
3355 <     * @serialData
3356 <     * the key (Object) and value (Object)
3357 <     * for each key-value mapping, followed by a null pair.
3358 <     * The key-value mappings are emitted in no particular order.
2223 >     * Initializes table, using the size recorded in sizeCtl.
2224       */
2225 <    @SuppressWarnings("unchecked")
2226 <        private void writeObject(java.io.ObjectOutputStream s)
2227 <        throws java.io.IOException {
2228 <        if (segments == null) { // for serialization compatibility
2229 <            segments = (Segment<K,V>[])
2230 <                new Segment<?,?>[DEFAULT_CONCURRENCY_LEVEL];
2231 <            for (int i = 0; i < segments.length; ++i)
2232 <                segments[i] = new Segment<K,V>(LOAD_FACTOR);
2233 <        }
2234 <        s.defaultWriteObject();
2235 <        Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
2236 <        Object v;
2237 <        while ((v = it.advance()) != null) {
2238 <            s.writeObject(it.nextKey);
2239 <            s.writeObject(v);
2225 >    private final Node<K,V>[] initTable() {
2226 >        Node<K,V>[] tab; int sc;
2227 >        while ((tab = table) == null || tab.length == 0) {
2228 >            if ((sc = sizeCtl) < 0)
2229 >                Thread.yield(); // lost initialization race; just spin
2230 >            else if (U.compareAndSwapInt(this, SIZECTL, sc, -1)) {
2231 >                try {
2232 >                    if ((tab = table) == null || tab.length == 0) {
2233 >                        int n = (sc > 0) ? sc : DEFAULT_CAPACITY;
2234 >                        @SuppressWarnings("unchecked")
2235 >                        Node<K,V>[] nt = (Node<K,V>[])new Node<?,?>[n];
2236 >                        table = tab = nt;
2237 >                        sc = n - (n >>> 2);
2238 >                    }
2239 >                } finally {
2240 >                    sizeCtl = sc;
2241 >                }
2242 >                break;
2243 >            }
2244          }
2245 <        s.writeObject(null);
3377 <        s.writeObject(null);
3378 <        segments = null; // throw away
2245 >        return tab;
2246      }
2247  
2248      /**
2249 <     * Reconstitutes the instance from a stream (that is, deserializes it).
2250 <     * @param s the stream
2249 >     * Adds to count, and if table is too small and not already
2250 >     * resizing, initiates transfer. If already resizing, helps
2251 >     * perform transfer if work is available.  Rechecks occupancy
2252 >     * after a transfer to see if another resize is already needed
2253 >     * because resizings are lagging additions.
2254 >     *
2255 >     * @param x the count to add
2256 >     * @param check if <0, don't check resize, if <= 1 only check if uncontended
2257 >     */
2258 >    private final void addCount(long x, int check) {
2259 >        CounterCell[] as; long b, s;
2260 >        if ((as = counterCells) != null ||
2261 >            !U.compareAndSwapLong(this, BASECOUNT, b = baseCount, s = b + x)) {
2262 >            CounterHashCode hc; CounterCell a; long v; int m;
2263 >            boolean uncontended = true;
2264 >            if ((hc = threadCounterHashCode.get()) == null ||
2265 >                as == null || (m = as.length - 1) < 0 ||
2266 >                (a = as[m & hc.code]) == null ||
2267 >                !(uncontended =
2268 >                  U.compareAndSwapLong(a, CELLVALUE, v = a.value, v + x))) {
2269 >                fullAddCount(x, hc, uncontended);
2270 >                return;
2271 >            }
2272 >            if (check <= 1)
2273 >                return;
2274 >            s = sumCount();
2275 >        }
2276 >        if (check >= 0) {
2277 >            Node<K,V>[] tab, nt; int n, sc;
2278 >            while (s >= (long)(sc = sizeCtl) && (tab = table) != null &&
2279 >                   (n = tab.length) < MAXIMUM_CAPACITY) {
2280 >                int rs = resizeStamp(n);
2281 >                if (sc < 0) {
2282 >                    if ((sc >>> RESIZE_STAMP_SHIFT) != rs || sc == rs + 1 ||
2283 >                        sc == rs + MAX_RESIZERS || (nt = nextTable) == null ||
2284 >                        transferIndex <= 0)
2285 >                        break;
2286 >                    if (U.compareAndSwapInt(this, SIZECTL, sc, sc + 1))
2287 >                        transfer(tab, nt);
2288 >                }
2289 >                else if (U.compareAndSwapInt(this, SIZECTL, sc,
2290 >                                             (rs << RESIZE_STAMP_SHIFT) + 2))
2291 >                    transfer(tab, null);
2292 >                s = sumCount();
2293 >            }
2294 >        }
2295 >    }
2296 >
2297 >    /**
2298 >     * Helps transfer if a resize is in progress.
2299       */
2300 <    @SuppressWarnings("unchecked")
2301 <        private void readObject(java.io.ObjectInputStream s)
2302 <        throws java.io.IOException, ClassNotFoundException {
2303 <        s.defaultReadObject();
2304 <        this.segments = null; // unneeded
2305 <        // initialize transient final field
2306 <        UNSAFE.putObjectVolatile(this, counterOffset, new LongAdder());
2300 >    final Node<K,V>[] helpTransfer(Node<K,V>[] tab, Node<K,V> f) {
2301 >        Node<K,V>[] nextTab; int sc;
2302 >        if (tab != null && (f instanceof ForwardingNode) &&
2303 >            (nextTab = ((ForwardingNode<K,V>)f).nextTable) != null) {
2304 >            int rs = resizeStamp(tab.length);
2305 >            while (nextTab == nextTable && table == tab &&
2306 >                   (sc = sizeCtl) < 0) {
2307 >                if ((sc >>> RESIZE_STAMP_SHIFT) != rs || sc == rs + 1 ||
2308 >                    sc == rs + MAX_RESIZERS || transferIndex <= 0)
2309 >                    break;
2310 >                if (U.compareAndSwapInt(this, SIZECTL, sc, sc + 1)) {
2311 >                    transfer(tab, nextTab);
2312 >                    break;
2313 >                }
2314 >            }
2315 >            return nextTab;
2316 >        }
2317 >        return table;
2318 >    }
2319  
2320 <        // Create all nodes, then place in table once size is known
2321 <        long size = 0L;
2322 <        Node p = null;
2323 <        for (;;) {
2324 <            K k = (K) s.readObject();
2325 <            V v = (V) s.readObject();
2326 <            if (k != null && v != null) {
2327 <                int h = spread(k.hashCode());
2328 <                p = new Node(h, k, v, p);
2329 <                ++size;
2320 >    /**
2321 >     * Tries to presize table to accommodate the given number of elements.
2322 >     *
2323 >     * @param size number of elements (doesn't need to be perfectly accurate)
2324 >     */
2325 >    private final void tryPresize(int size) {
2326 >        int c = (size >= (MAXIMUM_CAPACITY >>> 1)) ? MAXIMUM_CAPACITY :
2327 >            tableSizeFor(size + (size >>> 1) + 1);
2328 >        int sc;
2329 >        while ((sc = sizeCtl) >= 0) {
2330 >            Node<K,V>[] tab = table; int n;
2331 >            if (tab == null || (n = tab.length) == 0) {
2332 >                n = (sc > c) ? sc : c;
2333 >                if (U.compareAndSwapInt(this, SIZECTL, sc, -1)) {
2334 >                    try {
2335 >                        if (table == tab) {
2336 >                            @SuppressWarnings("unchecked")
2337 >                            Node<K,V>[] nt = (Node<K,V>[])new Node<?,?>[n];
2338 >                            table = nt;
2339 >                            sc = n - (n >>> 2);
2340 >                        }
2341 >                    } finally {
2342 >                        sizeCtl = sc;
2343 >                    }
2344 >                }
2345              }
2346 <            else
2346 >            else if (c <= sc || n >= MAXIMUM_CAPACITY)
2347                  break;
2348 +            else if (tab == table) {
2349 +                int rs = resizeStamp(n);
2350 +                if (sc < 0) {
2351 +                    Node<K,V>[] nt;
2352 +                    if ((sc >>> RESIZE_STAMP_SHIFT) != rs || sc == rs + 1 ||
2353 +                        sc == rs + MAX_RESIZERS || (nt = nextTable) == null ||
2354 +                        transferIndex <= 0)
2355 +                        break;
2356 +                    if (U.compareAndSwapInt(this, SIZECTL, sc, sc + 1))
2357 +                        transfer(tab, nt);
2358 +                }
2359 +                else if (U.compareAndSwapInt(this, SIZECTL, sc,
2360 +                                             (rs << RESIZE_STAMP_SHIFT) + 2))
2361 +                    transfer(tab, null);
2362 +            }
2363          }
2364 <        if (p != null) {
2365 <            boolean init = false;
2366 <            int n;
2367 <            if (size >= (long)(MAXIMUM_CAPACITY >>> 1))
2368 <                n = MAXIMUM_CAPACITY;
2369 <            else {
2370 <                int sz = (int)size;
2371 <                n = tableSizeFor(sz + (sz >>> 1) + 1);
2364 >    }
2365 >
2366 >    /**
2367 >     * Moves and/or copies the nodes in each bin to new table. See
2368 >     * above for explanation.
2369 >     */
2370 >    private final void transfer(Node<K,V>[] tab, Node<K,V>[] nextTab) {
2371 >        int n = tab.length, stride;
2372 >        if ((stride = (NCPU > 1) ? (n >>> 3) / NCPU : n) < MIN_TRANSFER_STRIDE)
2373 >            stride = MIN_TRANSFER_STRIDE; // subdivide range
2374 >        if (nextTab == null) {            // initiating
2375 >            try {
2376 >                @SuppressWarnings("unchecked")
2377 >                Node<K,V>[] nt = (Node<K,V>[])new Node<?,?>[n << 1];
2378 >                nextTab = nt;
2379 >            } catch (Throwable ex) {      // try to cope with OOME
2380 >                sizeCtl = Integer.MAX_VALUE;
2381 >                return;
2382 >            }
2383 >            nextTable = nextTab;
2384 >            transferIndex = n;
2385 >        }
2386 >        int nextn = nextTab.length;
2387 >        ForwardingNode<K,V> fwd = new ForwardingNode<K,V>(nextTab);
2388 >        boolean advance = true;
2389 >        boolean finishing = false; // to ensure sweep before committing nextTab
2390 >        for (int i = 0, bound = 0;;) {
2391 >            Node<K,V> f; int fh;
2392 >            while (advance) {
2393 >                int nextIndex, nextBound;
2394 >                if (--i >= bound || finishing)
2395 >                    advance = false;
2396 >                else if ((nextIndex = transferIndex) <= 0) {
2397 >                    i = -1;
2398 >                    advance = false;
2399 >                }
2400 >                else if (U.compareAndSwapInt
2401 >                         (this, TRANSFERINDEX, nextIndex,
2402 >                          nextBound = (nextIndex > stride ?
2403 >                                       nextIndex - stride : 0))) {
2404 >                    bound = nextBound;
2405 >                    i = nextIndex - 1;
2406 >                    advance = false;
2407 >                }
2408 >            }
2409 >            if (i < 0 || i >= n || i + n >= nextn) {
2410 >                int sc;
2411 >                if (finishing) {
2412 >                    nextTable = null;
2413 >                    table = nextTab;
2414 >                    sizeCtl = (n << 1) - (n >>> 1);
2415 >                    return;
2416 >                }
2417 >                if (U.compareAndSwapInt(this, SIZECTL, sc = sizeCtl, sc - 1)) {
2418 >                    if ((sc - 2) != resizeStamp(n) << RESIZE_STAMP_SHIFT)
2419 >                        return;
2420 >                    finishing = advance = true;
2421 >                    i = n; // recheck before commit
2422 >                }
2423              }
2424 <            int sc = sizeCtl;
2425 <            boolean collide = false;
2426 <            if (n > sc &&
2427 <                UNSAFE.compareAndSwapInt(this, sizeCtlOffset, sc, -1)) {
2428 <                try {
2429 <                    if (table == null) {
2430 <                        init = true;
2431 <                        Node[] tab = new Node[n];
2432 <                        int mask = n - 1;
2433 <                        while (p != null) {
2434 <                            int j = p.hash & mask;
2435 <                            Node next = p.next;
2436 <                            Node q = p.next = tabAt(tab, j);
2437 <                            setTabAt(tab, j, p);
2438 <                            if (!collide && q != null && q.hash == p.hash)
2439 <                                collide = true;
2440 <                            p = next;
2424 >            else if ((f = tabAt(tab, i)) == null)
2425 >                advance = casTabAt(tab, i, null, fwd);
2426 >            else if ((fh = f.hash) == MOVED)
2427 >                advance = true; // already processed
2428 >            else {
2429 >                synchronized (f) {
2430 >                    if (tabAt(tab, i) == f) {
2431 >                        Node<K,V> ln, hn;
2432 >                        if (fh >= 0) {
2433 >                            int runBit = fh & n;
2434 >                            Node<K,V> lastRun = f;
2435 >                            for (Node<K,V> p = f.next; p != null; p = p.next) {
2436 >                                int b = p.hash & n;
2437 >                                if (b != runBit) {
2438 >                                    runBit = b;
2439 >                                    lastRun = p;
2440 >                                }
2441 >                            }
2442 >                            if (runBit == 0) {
2443 >                                ln = lastRun;
2444 >                                hn = null;
2445 >                            }
2446 >                            else {
2447 >                                hn = lastRun;
2448 >                                ln = null;
2449 >                            }
2450 >                            for (Node<K,V> p = f; p != lastRun; p = p.next) {
2451 >                                int ph = p.hash; K pk = p.key; V pv = p.val;
2452 >                                if ((ph & n) == 0)
2453 >                                    ln = new Node<K,V>(ph, pk, pv, ln);
2454 >                                else
2455 >                                    hn = new Node<K,V>(ph, pk, pv, hn);
2456 >                            }
2457 >                            setTabAt(nextTab, i, ln);
2458 >                            setTabAt(nextTab, i + n, hn);
2459 >                            setTabAt(tab, i, fwd);
2460 >                            advance = true;
2461                          }
2462 <                        table = tab;
2463 <                        counter.add(size);
2464 <                        sc = n - (n >>> 2);
2465 <                    }
2466 <                } finally {
2467 <                    sizeCtl = sc;
2468 <                }
2469 <                if (collide) { // rescan and convert to TreeBins
2470 <                    Node[] tab = table;
2471 <                    for (int i = 0; i < tab.length; ++i) {
2472 <                        int c = 0;
2473 <                        for (Node e = tabAt(tab, i); e != null; e = e.next) {
2474 <                            if (++c > TREE_THRESHOLD &&
2475 <                                (e.key instanceof Comparable)) {
2476 <                                replaceWithTreeBin(tab, i, e.key);
2477 <                                break;
2462 >                        else if (f instanceof TreeBin) {
2463 >                            TreeBin<K,V> t = (TreeBin<K,V>)f;
2464 >                            TreeNode<K,V> lo = null, loTail = null;
2465 >                            TreeNode<K,V> hi = null, hiTail = null;
2466 >                            int lc = 0, hc = 0;
2467 >                            for (Node<K,V> e = t.first; e != null; e = e.next) {
2468 >                                int h = e.hash;
2469 >                                TreeNode<K,V> p = new TreeNode<K,V>
2470 >                                    (h, e.key, e.val, null, null);
2471 >                                if ((h & n) == 0) {
2472 >                                    if ((p.prev = loTail) == null)
2473 >                                        lo = p;
2474 >                                    else
2475 >                                        loTail.next = p;
2476 >                                    loTail = p;
2477 >                                    ++lc;
2478 >                                }
2479 >                                else {
2480 >                                    if ((p.prev = hiTail) == null)
2481 >                                        hi = p;
2482 >                                    else
2483 >                                        hiTail.next = p;
2484 >                                    hiTail = p;
2485 >                                    ++hc;
2486 >                                }
2487                              }
2488 +                            ln = (lc <= UNTREEIFY_THRESHOLD) ? untreeify(lo) :
2489 +                                (hc != 0) ? new TreeBin<K,V>(lo) : t;
2490 +                            hn = (hc <= UNTREEIFY_THRESHOLD) ? untreeify(hi) :
2491 +                                (lc != 0) ? new TreeBin<K,V>(hi) : t;
2492 +                            setTabAt(nextTab, i, ln);
2493 +                            setTabAt(nextTab, i + n, hn);
2494 +                            setTabAt(tab, i, fwd);
2495 +                            advance = true;
2496                          }
2497                      }
2498                  }
2499              }
3455            if (!init) { // Can only happen if unsafely published.
3456                while (p != null) {
3457                    internalPut(p.key, p.val);
3458                    p = p.next;
3459                }
3460            }
2500          }
2501      }
2502  
2503 +    /* ---------------- Conversion from/to TreeBins -------------- */
2504  
2505 <    // -------------------------------------------------------
2506 <
2507 <    // Sams
2508 <    /** Interface describing a void action of one argument */
2509 <    public interface Action<A> { void apply(A a); }
2510 <    /** Interface describing a void action of two arguments */
2511 <    public interface BiAction<A,B> { void apply(A a, B b); }
2512 <    /** Interface describing a function of one argument */
2513 <    public interface Fun<A,T> { T apply(A a); }
2514 <    /** Interface describing a function of two arguments */
2515 <    public interface BiFun<A,B,T> { T apply(A a, B b); }
2516 <    /** Interface describing a function of no arguments */
2517 <    public interface Generator<T> { T apply(); }
2518 <    /** Interface describing a function mapping its argument to a double */
2519 <    public interface ObjectToDouble<A> { double apply(A a); }
2520 <    /** Interface describing a function mapping its argument to a long */
2521 <    public interface ObjectToLong<A> { long apply(A a); }
2522 <    /** Interface describing a function mapping its argument to an int */
2523 <    public interface ObjectToInt<A> {int apply(A a); }
2524 <    /** Interface describing a function mapping two arguments to a double */
2525 <    public interface ObjectByObjectToDouble<A,B> { double apply(A a, B b); }
2526 <    /** Interface describing a function mapping two arguments to a long */
2527 <    public interface ObjectByObjectToLong<A,B> { long apply(A a, B b); }
2528 <    /** Interface describing a function mapping two arguments to an int */
2529 <    public interface ObjectByObjectToInt<A,B> {int apply(A a, B b); }
2530 <    /** Interface describing a function mapping a double to a double */
2531 <    public interface DoubleToDouble { double apply(double a); }
2532 <    /** Interface describing a function mapping a long to a long */
2533 <    public interface LongToLong { long apply(long a); }
3494 <    /** Interface describing a function mapping an int to an int */
3495 <    public interface IntToInt { int apply(int a); }
3496 <    /** Interface describing a function mapping two doubles to a double */
3497 <    public interface DoubleByDoubleToDouble { double apply(double a, double b); }
3498 <    /** Interface describing a function mapping two longs to a long */
3499 <    public interface LongByLongToLong { long apply(long a, long b); }
3500 <    /** Interface describing a function mapping two ints to an int */
3501 <    public interface IntByIntToInt { int apply(int a, int b); }
3502 <
3503 <
3504 <    // -------------------------------------------------------
2505 >    /**
2506 >     * Replaces all linked nodes in bin at given index unless table is
2507 >     * too small, in which case resizes instead.
2508 >     */
2509 >    private final void treeifyBin(Node<K,V>[] tab, int index) {
2510 >        Node<K,V> b; int n, sc;
2511 >        if (tab != null) {
2512 >            if ((n = tab.length) < MIN_TREEIFY_CAPACITY)
2513 >                tryPresize(n << 1);
2514 >            else if ((b = tabAt(tab, index)) != null && b.hash >= 0) {
2515 >                synchronized (b) {
2516 >                    if (tabAt(tab, index) == b) {
2517 >                        TreeNode<K,V> hd = null, tl = null;
2518 >                        for (Node<K,V> e = b; e != null; e = e.next) {
2519 >                            TreeNode<K,V> p =
2520 >                                new TreeNode<K,V>(e.hash, e.key, e.val,
2521 >                                                  null, null);
2522 >                            if ((p.prev = tl) == null)
2523 >                                hd = p;
2524 >                            else
2525 >                                tl.next = p;
2526 >                            tl = p;
2527 >                        }
2528 >                        setTabAt(tab, index, new TreeBin<K,V>(hd));
2529 >                    }
2530 >                }
2531 >            }
2532 >        }
2533 >    }
2534  
2535      /**
2536 <     * Returns an extended {@link Parallel} view of this map using the
3508 <     * given executor for bulk parallel operations.
3509 <     *
3510 <     * @param executor the executor
3511 <     * @return a parallel view
2536 >     * Returns a list on non-TreeNodes replacing those in given list.
2537       */
2538 <    public Parallel parallel(ForkJoinPool executor)  {
2539 <        return new Parallel(executor);
2538 >    static <K,V> Node<K,V> untreeify(Node<K,V> b) {
2539 >        Node<K,V> hd = null, tl = null;
2540 >        for (Node<K,V> q = b; q != null; q = q.next) {
2541 >            Node<K,V> p = new Node<K,V>(q.hash, q.key, q.val, null);
2542 >            if (tl == null)
2543 >                hd = p;
2544 >            else
2545 >                tl.next = p;
2546 >            tl = p;
2547 >        }
2548 >        return hd;
2549      }
2550  
2551 +    /* ---------------- TreeNodes -------------- */
2552 +
2553      /**
2554 <     * An extended view of a ConcurrentHashMap supporting bulk
3519 <     * parallel operations. These operations are designed to be
3520 <     * safely, and often sensibly, applied even with maps that are
3521 <     * being concurrently updated by other threads; for example, when
3522 <     * computing a snapshot summary of the values in a shared
3523 <     * registry.  There are three kinds of operation, each with four
3524 <     * forms, accepting functions with Keys, Values, Entries, and
3525 <     * (Key, Value) arguments and/or return values. Because the
3526 <     * elements of a ConcurrentHashMap are not ordered in any
3527 <     * particular way, and may be processed in different orders in
3528 <     * different parallel executions, the correctness of supplied
3529 <     * functions should not depend on any ordering, or on any other
3530 <     * objects or values that may transiently change while computation
3531 <     * is in progress; and except for forEach actions, should ideally
3532 <     * be side-effect-free.
3533 <     *
3534 <     * <ul>
3535 <     * <li> forEach: Perform a given action on each element.
3536 <     * A variant form applies a given transformation on each element
3537 <     * before performing the action.</li>
3538 <     *
3539 <     * <li> search: Return the first available non-null result of
3540 <     * applying a given function on each element; skipping further
3541 <     * search when a result is found.</li>
3542 <     *
3543 <     * <li> reduce: Accumulate each element.  The supplied reduction
3544 <     * function cannot rely on ordering (more formally, it should be
3545 <     * both associative and commutative).  There are five variants:
3546 <     *
3547 <     * <ul>
3548 <     *
3549 <     * <li> Plain reductions. (There is not a form of this method for
3550 <     * (key, value) function arguments since there is no corresponding
3551 <     * return type.)</li>
3552 <     *
3553 <     * <li> Mapped reductions that accumulate the results of a given
3554 <     * function applied to each element.</li>
3555 <     *
3556 <     * <li> Reductions to scalar doubles, longs, and ints, using a
3557 <     * given basis value.</li>
3558 <     *
3559 <     * </li>
3560 <     * </ul>
3561 <     * </ul>
3562 <     *
3563 <     * <p>The concurrency properties of the bulk operations follow
3564 <     * from those of ConcurrentHashMap: Any non-null result returned
3565 <     * from {@code get(key)} and related access methods bears a
3566 <     * happens-before relation with the associated insertion or
3567 <     * update.  The result of any bulk operation reflects the
3568 <     * composition of these per-element relations (but is not
3569 <     * necessarily atomic with respect to the map as a whole unless it
3570 <     * is somehow known to be quiescent).  Conversely, because keys
3571 <     * and values in the map are never null, null serves as a reliable
3572 <     * atomic indicator of the current lack of any result.  To
3573 <     * maintain this property, null serves as an implicit basis for
3574 <     * all non-scalar reduction operations. For the double, long, and
3575 <     * int versions, the basis should be one that, when combined with
3576 <     * any other value, returns that other value (more formally, it
3577 <     * should be the identity element for the reduction). Most common
3578 <     * reductions have these properties; for example, computing a sum
3579 <     * with basis 0 or a minimum with basis MAX_VALUE.
3580 <     *
3581 <     * <p>Search and transformation functions provided as arguments
3582 <     * should similarly return null to indicate the lack of any result
3583 <     * (in which case it is not used). In the case of mapped
3584 <     * reductions, this also enables transformations to serve as
3585 <     * filters, returning null (or, in the case of primitive
3586 <     * specializations, the identity basis) if the element should not
3587 <     * be combined. You can create compound transformations and
3588 <     * filterings by composing them yourself under this "null means
3589 <     * there is nothing there now" rule before using them in search or
3590 <     * reduce operations.
3591 <     *
3592 <     * <p>Methods accepting and/or returning Entry arguments maintain
3593 <     * key-value associations. They may be useful for example when
3594 <     * finding the key for the greatest value. Note that "plain" Entry
3595 <     * arguments can be supplied using {@code new
3596 <     * AbstractMap.SimpleEntry(k,v)}.
3597 <     *
3598 <     * <p> Bulk operations may complete abruptly, throwing an
3599 <     * exception encountered in the application of a supplied
3600 <     * function. Bear in mind when handling such exceptions that other
3601 <     * concurrently executing functions could also have thrown
3602 <     * exceptions, or would have done so if the first exception had
3603 <     * not occurred.
3604 <     *
3605 <     * <p>Parallel speedups compared to sequential processing are
3606 <     * common but not guaranteed.  Operations involving brief
3607 <     * functions on small maps may execute more slowly than sequential
3608 <     * loops if the underlying work to parallelize the computation is
3609 <     * more expensive than the computation itself. Similarly,
3610 <     * parallelization may not lead to much actual parallelism if all
3611 <     * processors are busy performing unrelated tasks.
3612 <     *
3613 <     * <p> All arguments to all task methods must be non-null.
3614 <     *
3615 <     * <p><em>jsr166e note: During transition, this class
3616 <     * uses nested functional interfaces with different names but the
3617 <     * same forms as those expected for JDK8.<em>
2554 >     * Nodes for use in TreeBins
2555       */
2556 <    public class Parallel {
2557 <        final ForkJoinPool fjp;
2556 >    static final class TreeNode<K,V> extends Node<K,V> {
2557 >        TreeNode<K,V> parent;  // red-black tree links
2558 >        TreeNode<K,V> left;
2559 >        TreeNode<K,V> right;
2560 >        TreeNode<K,V> prev;    // needed to unlink next upon deletion
2561 >        boolean red;
2562  
2563 <        /**
2564 <         * Returns an extended view of this map using the given
2565 <         * executor for bulk parallel operations.
2566 <         *
3626 <         * @param executor the executor
3627 <         */
3628 <        public Parallel(ForkJoinPool executor)  {
3629 <            this.fjp = executor;
2563 >        TreeNode(int hash, K key, V val, Node<K,V> next,
2564 >                 TreeNode<K,V> parent) {
2565 >            super(hash, key, val, next);
2566 >            this.parent = parent;
2567          }
2568  
2569 <        /**
2570 <         * Performs the given action for each (key, value).
3634 <         *
3635 <         * @param action the action
3636 <         */
3637 <        public void forEach(BiAction<K,V> action) {
3638 <            fjp.invoke(ForkJoinTasks.forEach
3639 <                       (ConcurrentHashMapV8.this, action));
2569 >        Node<K,V> find(int h, Object k) {
2570 >            return findTreeNode(h, k, null);
2571          }
2572  
2573          /**
2574 <         * Performs the given action for each non-null transformation
2575 <         * of each (key, value).
3645 <         *
3646 <         * @param transformer a function returning the transformation
3647 <         * for an element, or null of there is no transformation (in
3648 <         * which case the action is not applied).
3649 <         * @param action the action
2574 >         * Returns the TreeNode (or null if not found) for the given key
2575 >         * starting at given root.
2576           */
2577 <        public <U> void forEach(BiFun<? super K, ? super V, ? extends U> transformer,
2578 <                                Action<U> action) {
2579 <            fjp.invoke(ForkJoinTasks.forEach
2580 <                       (ConcurrentHashMapV8.this, transformer, action));
2577 >        final TreeNode<K,V> findTreeNode(int h, Object k, Class<?> kc) {
2578 >            if (k != null) {
2579 >                TreeNode<K,V> p = this;
2580 >                do {
2581 >                    int ph, dir; K pk; TreeNode<K,V> q;
2582 >                    TreeNode<K,V> pl = p.left, pr = p.right;
2583 >                    if ((ph = p.hash) > h)
2584 >                        p = pl;
2585 >                    else if (ph < h)
2586 >                        p = pr;
2587 >                    else if ((pk = p.key) == k || (pk != null && k.equals(pk)))
2588 >                        return p;
2589 >                    else if (pl == null)
2590 >                        p = pr;
2591 >                    else if (pr == null)
2592 >                        p = pl;
2593 >                    else if ((kc != null ||
2594 >                              (kc = comparableClassFor(k)) != null) &&
2595 >                             (dir = compareComparables(kc, k, pk)) != 0)
2596 >                        p = (dir < 0) ? pl : pr;
2597 >                    else if ((q = pr.findTreeNode(h, k, kc)) != null)
2598 >                        return q;
2599 >                    else
2600 >                        p = pl;
2601 >                } while (p != null);
2602 >            }
2603 >            return null;
2604          }
2605 +    }
2606  
2607 <        /**
2608 <         * Returns a non-null result from applying the given search
2609 <         * function on each (key, value), or null if none.  Further
2610 <         * element processing is suppressed upon success. However,
2611 <         * this method does not return until other in-progress
2612 <         * parallel invocations of the search function also complete.
2613 <         *
2614 <         * @param searchFunction a function returning a non-null
2615 <         * result on success, else null
2616 <         * @return a non-null result from applying the given search
2617 <         * function on each (key, value), or null if none
2618 <         */
2619 <        public <U> U search(BiFun<? super K, ? super V, ? extends U> searchFunction) {
2620 <            return fjp.invoke(ForkJoinTasks.search
2621 <                              (ConcurrentHashMapV8.this, searchFunction));
2607 >    /* ---------------- TreeBins -------------- */
2608 >
2609 >    /**
2610 >     * TreeNodes used at the heads of bins. TreeBins do not hold user
2611 >     * keys or values, but instead point to list of TreeNodes and
2612 >     * their root. They also maintain a parasitic read-write lock
2613 >     * forcing writers (who hold bin lock) to wait for readers (who do
2614 >     * not) to complete before tree restructuring operations.
2615 >     */
2616 >    static final class TreeBin<K,V> extends Node<K,V> {
2617 >        TreeNode<K,V> root;
2618 >        volatile TreeNode<K,V> first;
2619 >        volatile Thread waiter;
2620 >        volatile int lockState;
2621 >        // values for lockState
2622 >        static final int WRITER = 1; // set while holding write lock
2623 >        static final int WAITER = 2; // set when waiting for write lock
2624 >        static final int READER = 4; // increment value for setting read lock
2625 >
2626 >        /**
2627 >         * Tie-breaking utility for ordering insertions when equal
2628 >         * hashCodes and non-comparable. We don't require a total
2629 >         * order, just a consistent insertion rule to maintain
2630 >         * equivalence across rebalancings. Tie-breaking further than
2631 >         * necessary simplifies testing a bit.
2632 >         */
2633 >        static int tieBreakOrder(Object a, Object b) {
2634 >            int d;
2635 >            if (a == null || b == null ||
2636 >                (d = a.getClass().getName().
2637 >                 compareTo(b.getClass().getName())) == 0)
2638 >                d = (System.identityHashCode(a) <= System.identityHashCode(b) ?
2639 >                     -1 : 1);
2640 >            return d;
2641          }
2642  
2643          /**
2644 <         * Returns the result of accumulating the given transformation
2645 <         * of all (key, value) pairs using the given reducer to
2646 <         * combine values, or null if none.
2647 <         *
2648 <         * @param transformer a function returning the transformation
2649 <         * for an element, or null of there is no transformation (in
2650 <         * which case it is not combined).
2651 <         * @param reducer a commutative associative combining function
2652 <         * @return the result of accumulating the given transformation
2653 <         * of all (key, value) pairs
2654 <         */
2655 <        public <U> U reduce(BiFun<? super K, ? super V, ? extends U> transformer,
2656 <                            BiFun<? super U, ? super U, ? extends U> reducer) {
2657 <            return fjp.invoke(ForkJoinTasks.reduce
2658 <                              (ConcurrentHashMapV8.this, transformer, reducer));
2644 >         * Creates bin with initial set of nodes headed by b.
2645 >         */
2646 >        TreeBin(TreeNode<K,V> b) {
2647 >            super(TREEBIN, null, null, null);
2648 >            this.first = b;
2649 >            TreeNode<K,V> r = null;
2650 >            for (TreeNode<K,V> x = b, next; x != null; x = next) {
2651 >                next = (TreeNode<K,V>)x.next;
2652 >                x.left = x.right = null;
2653 >                if (r == null) {
2654 >                    x.parent = null;
2655 >                    x.red = false;
2656 >                    r = x;
2657 >                }
2658 >                else {
2659 >                    K k = x.key;
2660 >                    int h = x.hash;
2661 >                    Class<?> kc = null;
2662 >                    for (TreeNode<K,V> p = r;;) {
2663 >                        int dir, ph;
2664 >                        K pk = p.key;
2665 >                        if ((ph = p.hash) > h)
2666 >                            dir = -1;
2667 >                        else if (ph < h)
2668 >                            dir = 1;
2669 >                        else if ((kc == null &&
2670 >                                  (kc = comparableClassFor(k)) == null) ||
2671 >                                 (dir = compareComparables(kc, k, pk)) == 0)
2672 >                            dir = tieBreakOrder(k, pk);
2673 >                            TreeNode<K,V> xp = p;
2674 >                        if ((p = (dir <= 0) ? p.left : p.right) == null) {
2675 >                            x.parent = xp;
2676 >                            if (dir <= 0)
2677 >                                xp.left = x;
2678 >                            else
2679 >                                xp.right = x;
2680 >                            r = balanceInsertion(r, x);
2681 >                            break;
2682 >                        }
2683 >                    }
2684 >                }
2685 >            }
2686 >            this.root = r;
2687 >            assert checkInvariants(root);
2688          }
2689  
2690          /**
2691 <         * Returns the result of accumulating the given transformation
3694 <         * of all (key, value) pairs using the given reducer to
3695 <         * combine values, and the given basis as an identity value.
3696 <         *
3697 <         * @param transformer a function returning the transformation
3698 <         * for an element
3699 <         * @param basis the identity (initial default value) for the reduction
3700 <         * @param reducer a commutative associative combining function
3701 <         * @return the result of accumulating the given transformation
3702 <         * of all (key, value) pairs
2691 >         * Acquires write lock for tree restructuring.
2692           */
2693 <        public double reduceToDouble(ObjectByObjectToDouble<? super K, ? super V> transformer,
2694 <                                     double basis,
2695 <                                     DoubleByDoubleToDouble reducer) {
3707 <            return fjp.invoke(ForkJoinTasks.reduceToDouble
3708 <                              (ConcurrentHashMapV8.this, transformer, basis, reducer));
2693 >        private final void lockRoot() {
2694 >            if (!U.compareAndSwapInt(this, LOCKSTATE, 0, WRITER))
2695 >                contendedLock(); // offload to separate method
2696          }
2697  
2698          /**
2699 <         * Returns the result of accumulating the given transformation
3713 <         * of all (key, value) pairs using the given reducer to
3714 <         * combine values, and the given basis as an identity value.
3715 <         *
3716 <         * @param transformer a function returning the transformation
3717 <         * for an element
3718 <         * @param basis the identity (initial default value) for the reduction
3719 <         * @param reducer a commutative associative combining function
3720 <         * @return the result of accumulating the given transformation
3721 <         * of all (key, value) pairs using the given reducer to
3722 <         * combine values, and the given basis as an identity value.
2699 >         * Releases write lock for tree restructuring.
2700           */
2701 <        public long reduceToLong(ObjectByObjectToLong<? super K, ? super V> transformer,
2702 <                                 long basis,
3726 <                                 LongByLongToLong reducer) {
3727 <            return fjp.invoke(ForkJoinTasks.reduceToLong
3728 <                              (ConcurrentHashMapV8.this, transformer, basis, reducer));
2701 >        private final void unlockRoot() {
2702 >            lockState = 0;
2703          }
2704  
2705          /**
2706 <         * Returns the result of accumulating the given transformation
3733 <         * of all (key, value) pairs using the given reducer to
3734 <         * combine values, and the given basis as an identity value.
3735 <         *
3736 <         * @param transformer a function returning the transformation
3737 <         * for an element
3738 <         * @param basis the identity (initial default value) for the reduction
3739 <         * @param reducer a commutative associative combining function
3740 <         * @return the result of accumulating the given transformation
3741 <         * of all (key, value) pairs
2706 >         * Possibly blocks awaiting root lock.
2707           */
2708 <        public int reduceToInt(ObjectByObjectToInt<? super K, ? super V> transformer,
2709 <                               int basis,
2710 <                               IntByIntToInt reducer) {
2711 <            return fjp.invoke(ForkJoinTasks.reduceToInt
2712 <                              (ConcurrentHashMapV8.this, transformer, basis, reducer));
2708 >        private final void contendedLock() {
2709 >            boolean waiting = false;
2710 >            for (int s;;) {
2711 >                if (((s = lockState) & ~WAITER) == 0) {
2712 >                    if (U.compareAndSwapInt(this, LOCKSTATE, s, WRITER)) {
2713 >                        if (waiting)
2714 >                            waiter = null;
2715 >                        return;
2716 >                    }
2717 >                }
2718 >                else if ((s & WAITER) == 0) {
2719 >                    if (U.compareAndSwapInt(this, LOCKSTATE, s, s | WAITER)) {
2720 >                        waiting = true;
2721 >                        waiter = Thread.currentThread();
2722 >                    }
2723 >                }
2724 >                else if (waiting)
2725 >                    LockSupport.park(this);
2726 >            }
2727          }
2728  
2729          /**
2730 <         * Performs the given action for each key.
2731 <         *
2732 <         * @param action the action
2730 >         * Returns matching node or null if none. Tries to search
2731 >         * using tree comparisons from root, but continues linear
2732 >         * search when lock not available.
2733           */
2734 <        public void forEachKey(Action<K> action) {
2735 <            fjp.invoke(ForkJoinTasks.forEachKey
2736 <                       (ConcurrentHashMapV8.this, action));
2734 >        final Node<K,V> find(int h, Object k) {
2735 >            if (k != null) {
2736 >                for (Node<K,V> e = first; e != null; ) {
2737 >                    int s; K ek;
2738 >                    if (((s = lockState) & (WAITER|WRITER)) != 0) {
2739 >                        if (e.hash == h &&
2740 >                            ((ek = e.key) == k || (ek != null && k.equals(ek))))
2741 >                            return e;
2742 >                        e = e.next;
2743 >                    }
2744 >                    else if (U.compareAndSwapInt(this, LOCKSTATE, s,
2745 >                                                 s + READER)) {
2746 >                        TreeNode<K,V> r, p;
2747 >                        try {
2748 >                            p = ((r = root) == null ? null :
2749 >                                 r.findTreeNode(h, k, null));
2750 >                        } finally {
2751 >                            Thread w;
2752 >                            int ls;
2753 >                            do {} while (!U.compareAndSwapInt
2754 >                                         (this, LOCKSTATE,
2755 >                                          ls = lockState, ls - READER));
2756 >                            if (ls == (READER|WAITER) && (w = waiter) != null)
2757 >                                LockSupport.unpark(w);
2758 >                        }
2759 >                        return p;
2760 >                    }
2761 >                }
2762 >            }
2763 >            return null;
2764          }
2765  
2766          /**
2767 <         * Performs the given action for each non-null transformation
2768 <         * of each key.
3763 <         *
3764 <         * @param transformer a function returning the transformation
3765 <         * for an element, or null of there is no transformation (in
3766 <         * which case the action is not applied).
3767 <         * @param action the action
2767 >         * Finds or adds a node.
2768 >         * @return null if added
2769           */
2770 <        public <U> void forEachKey(Fun<? super K, ? extends U> transformer,
2771 <                                   Action<U> action) {
2772 <            fjp.invoke(ForkJoinTasks.forEachKey
2773 <                       (ConcurrentHashMapV8.this, transformer, action));
2770 >        final TreeNode<K,V> putTreeVal(int h, K k, V v) {
2771 >            Class<?> kc = null;
2772 >            boolean searched = false;
2773 >            for (TreeNode<K,V> p = root;;) {
2774 >                int dir, ph; K pk;
2775 >                if (p == null) {
2776 >                    first = root = new TreeNode<K,V>(h, k, v, null, null);
2777 >                    break;
2778 >                }
2779 >                else if ((ph = p.hash) > h)
2780 >                    dir = -1;
2781 >                else if (ph < h)
2782 >                    dir = 1;
2783 >                else if ((pk = p.key) == k || (pk != null && k.equals(pk)))
2784 >                    return p;
2785 >                else if ((kc == null &&
2786 >                          (kc = comparableClassFor(k)) == null) ||
2787 >                         (dir = compareComparables(kc, k, pk)) == 0) {
2788 >                    if (!searched) {
2789 >                        TreeNode<K,V> q, ch;
2790 >                        searched = true;
2791 >                        if (((ch = p.left) != null &&
2792 >                             (q = ch.findTreeNode(h, k, kc)) != null) ||
2793 >                            ((ch = p.right) != null &&
2794 >                             (q = ch.findTreeNode(h, k, kc)) != null))
2795 >                            return q;
2796 >                    }
2797 >                    dir = tieBreakOrder(k, pk);
2798 >                }
2799 >
2800 >                TreeNode<K,V> xp = p;
2801 >                if ((p = (dir <= 0) ? p.left : p.right) == null) {
2802 >                    TreeNode<K,V> x, f = first;
2803 >                    first = x = new TreeNode<K,V>(h, k, v, f, xp);
2804 >                    if (f != null)
2805 >                        f.prev = x;
2806 >                    if (dir <= 0)
2807 >                        xp.left = x;
2808 >                    else
2809 >                        xp.right = x;
2810 >                    if (!xp.red)
2811 >                        x.red = true;
2812 >                    else {
2813 >                        lockRoot();
2814 >                        try {
2815 >                            root = balanceInsertion(root, x);
2816 >                        } finally {
2817 >                            unlockRoot();
2818 >                        }
2819 >                    }
2820 >                    break;
2821 >                }
2822 >            }
2823 >            assert checkInvariants(root);
2824 >            return null;
2825          }
2826  
2827          /**
2828 <         * Returns a non-null result from applying the given search
2829 <         * function on each key, or null if none.  Further element
2830 <         * processing is suppressed upon success. However, this method
2831 <         * does not return until other in-progress parallel
2832 <         * invocations of the search function also complete.
2828 >         * Removes the given node, that must be present before this
2829 >         * call.  This is messier than typical red-black deletion code
2830 >         * because we cannot swap the contents of an interior node
2831 >         * with a leaf successor that is pinned by "next" pointers
2832 >         * that are accessible independently of lock. So instead we
2833 >         * swap the tree linkages.
2834           *
2835 <         * @param searchFunction a function returning a non-null
3783 <         * result on success, else null
3784 <         * @return a non-null result from applying the given search
3785 <         * function on each key, or null if none
2835 >         * @return true if now too small, so should be untreeified
2836           */
2837 <        public <U> U searchKeys(Fun<? super K, ? extends U> searchFunction) {
2838 <            return fjp.invoke(ForkJoinTasks.searchKeys
2839 <                              (ConcurrentHashMapV8.this, searchFunction));
2837 >        final boolean removeTreeNode(TreeNode<K,V> p) {
2838 >            TreeNode<K,V> next = (TreeNode<K,V>)p.next;
2839 >            TreeNode<K,V> pred = p.prev;  // unlink traversal pointers
2840 >            TreeNode<K,V> r, rl;
2841 >            if (pred == null)
2842 >                first = next;
2843 >            else
2844 >                pred.next = next;
2845 >            if (next != null)
2846 >                next.prev = pred;
2847 >            if (first == null) {
2848 >                root = null;
2849 >                return true;
2850 >            }
2851 >            if ((r = root) == null || r.right == null || // too small
2852 >                (rl = r.left) == null || rl.left == null)
2853 >                return true;
2854 >            lockRoot();
2855 >            try {
2856 >                TreeNode<K,V> replacement;
2857 >                TreeNode<K,V> pl = p.left;
2858 >                TreeNode<K,V> pr = p.right;
2859 >                if (pl != null && pr != null) {
2860 >                    TreeNode<K,V> s = pr, sl;
2861 >                    while ((sl = s.left) != null) // find successor
2862 >                        s = sl;
2863 >                    boolean c = s.red; s.red = p.red; p.red = c; // swap colors
2864 >                    TreeNode<K,V> sr = s.right;
2865 >                    TreeNode<K,V> pp = p.parent;
2866 >                    if (s == pr) { // p was s's direct parent
2867 >                        p.parent = s;
2868 >                        s.right = p;
2869 >                    }
2870 >                    else {
2871 >                        TreeNode<K,V> sp = s.parent;
2872 >                        if ((p.parent = sp) != null) {
2873 >                            if (s == sp.left)
2874 >                                sp.left = p;
2875 >                            else
2876 >                                sp.right = p;
2877 >                        }
2878 >                        if ((s.right = pr) != null)
2879 >                            pr.parent = s;
2880 >                    }
2881 >                    p.left = null;
2882 >                    if ((p.right = sr) != null)
2883 >                        sr.parent = p;
2884 >                    if ((s.left = pl) != null)
2885 >                        pl.parent = s;
2886 >                    if ((s.parent = pp) == null)
2887 >                        r = s;
2888 >                    else if (p == pp.left)
2889 >                        pp.left = s;
2890 >                    else
2891 >                        pp.right = s;
2892 >                    if (sr != null)
2893 >                        replacement = sr;
2894 >                    else
2895 >                        replacement = p;
2896 >                }
2897 >                else if (pl != null)
2898 >                    replacement = pl;
2899 >                else if (pr != null)
2900 >                    replacement = pr;
2901 >                else
2902 >                    replacement = p;
2903 >                if (replacement != p) {
2904 >                    TreeNode<K,V> pp = replacement.parent = p.parent;
2905 >                    if (pp == null)
2906 >                        r = replacement;
2907 >                    else if (p == pp.left)
2908 >                        pp.left = replacement;
2909 >                    else
2910 >                        pp.right = replacement;
2911 >                    p.left = p.right = p.parent = null;
2912 >                }
2913 >
2914 >                root = (p.red) ? r : balanceDeletion(r, replacement);
2915 >
2916 >                if (p == replacement) {  // detach pointers
2917 >                    TreeNode<K,V> pp;
2918 >                    if ((pp = p.parent) != null) {
2919 >                        if (p == pp.left)
2920 >                            pp.left = null;
2921 >                        else if (p == pp.right)
2922 >                            pp.right = null;
2923 >                        p.parent = null;
2924 >                    }
2925 >                }
2926 >            } finally {
2927 >                unlockRoot();
2928 >            }
2929 >            assert checkInvariants(root);
2930 >            return false;
2931          }
2932  
2933 <        /**
2934 <         * Returns the result of accumulating all keys using the given
2935 <         * reducer to combine values, or null if none.
2936 <         *
2937 <         * @param reducer a commutative associative combining function
2938 <         * @return the result of accumulating all keys using the given
2939 <         * reducer to combine values, or null if none
2940 <         */
2941 <        public K reduceKeys(BiFun<? super K, ? super K, ? extends K> reducer) {
2942 <            return fjp.invoke(ForkJoinTasks.reduceKeys
2943 <                              (ConcurrentHashMapV8.this, reducer));
2933 >        /* ------------------------------------------------------------ */
2934 >        // Red-black tree methods, all adapted from CLR
2935 >
2936 >        static <K,V> TreeNode<K,V> rotateLeft(TreeNode<K,V> root,
2937 >                                              TreeNode<K,V> p) {
2938 >            TreeNode<K,V> r, pp, rl;
2939 >            if (p != null && (r = p.right) != null) {
2940 >                if ((rl = p.right = r.left) != null)
2941 >                    rl.parent = p;
2942 >                if ((pp = r.parent = p.parent) == null)
2943 >                    (root = r).red = false;
2944 >                else if (pp.left == p)
2945 >                    pp.left = r;
2946 >                else
2947 >                    pp.right = r;
2948 >                r.left = p;
2949 >                p.parent = r;
2950 >            }
2951 >            return root;
2952          }
2953  
2954 <        /**
2955 <         * Returns the result of accumulating the given transformation
2956 <         * of all keys using the given reducer to combine values, or
2957 <         * null if none.
2958 <         *
2959 <         * @param transformer a function returning the transformation
2960 <         * for an element, or null of there is no transformation (in
2961 <         * which case it is not combined).
2962 <         * @param reducer a commutative associative combining function
2963 <         * @return the result of accumulating the given transformation
2964 <         * of all keys
2965 <         */
2966 <        public <U> U reduceKeys(Fun<? super K, ? extends U> transformer,
2967 <                                BiFun<? super U, ? super U, ? extends U> reducer) {
2968 <            return fjp.invoke(ForkJoinTasks.reduceKeys
2969 <                              (ConcurrentHashMapV8.this, transformer, reducer));
2954 >        static <K,V> TreeNode<K,V> rotateRight(TreeNode<K,V> root,
2955 >                                               TreeNode<K,V> p) {
2956 >            TreeNode<K,V> l, pp, lr;
2957 >            if (p != null && (l = p.left) != null) {
2958 >                if ((lr = p.left = l.right) != null)
2959 >                    lr.parent = p;
2960 >                if ((pp = l.parent = p.parent) == null)
2961 >                    (root = l).red = false;
2962 >                else if (pp.right == p)
2963 >                    pp.right = l;
2964 >                else
2965 >                    pp.left = l;
2966 >                l.right = p;
2967 >                p.parent = l;
2968 >            }
2969 >            return root;
2970          }
2971  
2972 <        /**
2973 <         * Returns the result of accumulating the given transformation
2974 <         * of all keys using the given reducer to combine values, and
2975 <         * the given basis as an identity value.
2976 <         *
2977 <         * @param transformer a function returning the transformation
2978 <         * for an element
2979 <         * @param basis the identity (initial default value) for the reduction
2980 <         * @param reducer a commutative associative combining function
2981 <         * @return  the result of accumulating the given transformation
2982 <         * of all keys
2983 <         */
2984 <        public double reduceKeysToDouble(ObjectToDouble<? super K> transformer,
2985 <                                         double basis,
2986 <                                         DoubleByDoubleToDouble reducer) {
2987 <            return fjp.invoke(ForkJoinTasks.reduceKeysToDouble
2988 <                              (ConcurrentHashMapV8.this, transformer, basis, reducer));
2972 >        static <K,V> TreeNode<K,V> balanceInsertion(TreeNode<K,V> root,
2973 >                                                    TreeNode<K,V> x) {
2974 >            x.red = true;
2975 >            for (TreeNode<K,V> xp, xpp, xppl, xppr;;) {
2976 >                if ((xp = x.parent) == null) {
2977 >                    x.red = false;
2978 >                    return x;
2979 >                }
2980 >                else if (!xp.red || (xpp = xp.parent) == null)
2981 >                    return root;
2982 >                if (xp == (xppl = xpp.left)) {
2983 >                    if ((xppr = xpp.right) != null && xppr.red) {
2984 >                        xppr.red = false;
2985 >                        xp.red = false;
2986 >                        xpp.red = true;
2987 >                        x = xpp;
2988 >                    }
2989 >                    else {
2990 >                        if (x == xp.right) {
2991 >                            root = rotateLeft(root, x = xp);
2992 >                            xpp = (xp = x.parent) == null ? null : xp.parent;
2993 >                        }
2994 >                        if (xp != null) {
2995 >                            xp.red = false;
2996 >                            if (xpp != null) {
2997 >                                xpp.red = true;
2998 >                                root = rotateRight(root, xpp);
2999 >                            }
3000 >                        }
3001 >                    }
3002 >                }
3003 >                else {
3004 >                    if (xppl != null && xppl.red) {
3005 >                        xppl.red = false;
3006 >                        xp.red = false;
3007 >                        xpp.red = true;
3008 >                        x = xpp;
3009 >                    }
3010 >                    else {
3011 >                        if (x == xp.left) {
3012 >                            root = rotateRight(root, x = xp);
3013 >                            xpp = (xp = x.parent) == null ? null : xp.parent;
3014 >                        }
3015 >                        if (xp != null) {
3016 >                            xp.red = false;
3017 >                            if (xpp != null) {
3018 >                                xpp.red = true;
3019 >                                root = rotateLeft(root, xpp);
3020 >                            }
3021 >                        }
3022 >                    }
3023 >                }
3024 >            }
3025          }
3026  
3027 <        /**
3028 <         * Returns the result of accumulating the given transformation
3029 <         * of all keys using the given reducer to combine values, and
3030 <         * the given basis as an identity value.
3031 <         *
3032 <         * @param transformer a function returning the transformation
3033 <         * for an element
3034 <         * @param basis the identity (initial default value) for the reduction
3035 <         * @param reducer a commutative associative combining function
3036 <         * @return the result of accumulating the given transformation
3037 <         * of all keys
3038 <         */
3039 <        public long reduceKeysToLong(ObjectToLong<? super K> transformer,
3040 <                                     long basis,
3041 <                                     LongByLongToLong reducer) {
3042 <            return fjp.invoke(ForkJoinTasks.reduceKeysToLong
3043 <                              (ConcurrentHashMapV8.this, transformer, basis, reducer));
3027 >        static <K,V> TreeNode<K,V> balanceDeletion(TreeNode<K,V> root,
3028 >                                                   TreeNode<K,V> x) {
3029 >            for (TreeNode<K,V> xp, xpl, xpr;;) {
3030 >                if (x == null || x == root)
3031 >                    return root;
3032 >                else if ((xp = x.parent) == null) {
3033 >                    x.red = false;
3034 >                    return x;
3035 >                }
3036 >                else if (x.red) {
3037 >                    x.red = false;
3038 >                    return root;
3039 >                }
3040 >                else if ((xpl = xp.left) == x) {
3041 >                    if ((xpr = xp.right) != null && xpr.red) {
3042 >                        xpr.red = false;
3043 >                        xp.red = true;
3044 >                        root = rotateLeft(root, xp);
3045 >                        xpr = (xp = x.parent) == null ? null : xp.right;
3046 >                    }
3047 >                    if (xpr == null)
3048 >                        x = xp;
3049 >                    else {
3050 >                        TreeNode<K,V> sl = xpr.left, sr = xpr.right;
3051 >                        if ((sr == null || !sr.red) &&
3052 >                            (sl == null || !sl.red)) {
3053 >                            xpr.red = true;
3054 >                            x = xp;
3055 >                        }
3056 >                        else {
3057 >                            if (sr == null || !sr.red) {
3058 >                                if (sl != null)
3059 >                                    sl.red = false;
3060 >                                xpr.red = true;
3061 >                                root = rotateRight(root, xpr);
3062 >                                xpr = (xp = x.parent) == null ?
3063 >                                    null : xp.right;
3064 >                            }
3065 >                            if (xpr != null) {
3066 >                                xpr.red = (xp == null) ? false : xp.red;
3067 >                                if ((sr = xpr.right) != null)
3068 >                                    sr.red = false;
3069 >                            }
3070 >                            if (xp != null) {
3071 >                                xp.red = false;
3072 >                                root = rotateLeft(root, xp);
3073 >                            }
3074 >                            x = root;
3075 >                        }
3076 >                    }
3077 >                }
3078 >                else { // symmetric
3079 >                    if (xpl != null && xpl.red) {
3080 >                        xpl.red = false;
3081 >                        xp.red = true;
3082 >                        root = rotateRight(root, xp);
3083 >                        xpl = (xp = x.parent) == null ? null : xp.left;
3084 >                    }
3085 >                    if (xpl == null)
3086 >                        x = xp;
3087 >                    else {
3088 >                        TreeNode<K,V> sl = xpl.left, sr = xpl.right;
3089 >                        if ((sl == null || !sl.red) &&
3090 >                            (sr == null || !sr.red)) {
3091 >                            xpl.red = true;
3092 >                            x = xp;
3093 >                        }
3094 >                        else {
3095 >                            if (sl == null || !sl.red) {
3096 >                                if (sr != null)
3097 >                                    sr.red = false;
3098 >                                xpl.red = true;
3099 >                                root = rotateLeft(root, xpl);
3100 >                                xpl = (xp = x.parent) == null ?
3101 >                                    null : xp.left;
3102 >                            }
3103 >                            if (xpl != null) {
3104 >                                xpl.red = (xp == null) ? false : xp.red;
3105 >                                if ((sl = xpl.left) != null)
3106 >                                    sl.red = false;
3107 >                            }
3108 >                            if (xp != null) {
3109 >                                xp.red = false;
3110 >                                root = rotateRight(root, xp);
3111 >                            }
3112 >                            x = root;
3113 >                        }
3114 >                    }
3115 >                }
3116 >            }
3117          }
3118  
3119          /**
3120 <         * Returns the result of accumulating the given transformation
3863 <         * of all keys using the given reducer to combine values, and
3864 <         * the given basis as an identity value.
3865 <         *
3866 <         * @param transformer a function returning the transformation
3867 <         * for an element
3868 <         * @param basis the identity (initial default value) for the reduction
3869 <         * @param reducer a commutative associative combining function
3870 <         * @return the result of accumulating the given transformation
3871 <         * of all keys
3120 >         * Recursive invariant check
3121           */
3122 <        public int reduceKeysToInt(ObjectToInt<? super K> transformer,
3123 <                                   int basis,
3124 <                                   IntByIntToInt reducer) {
3125 <            return fjp.invoke(ForkJoinTasks.reduceKeysToInt
3126 <                              (ConcurrentHashMapV8.this, transformer, basis, reducer));
3122 >        static <K,V> boolean checkInvariants(TreeNode<K,V> t) {
3123 >            TreeNode<K,V> tp = t.parent, tl = t.left, tr = t.right,
3124 >                tb = t.prev, tn = (TreeNode<K,V>)t.next;
3125 >            if (tb != null && tb.next != t)
3126 >                return false;
3127 >            if (tn != null && tn.prev != t)
3128 >                return false;
3129 >            if (tp != null && t != tp.left && t != tp.right)
3130 >                return false;
3131 >            if (tl != null && (tl.parent != t || tl.hash > t.hash))
3132 >                return false;
3133 >            if (tr != null && (tr.parent != t || tr.hash < t.hash))
3134 >                return false;
3135 >            if (t.red && tl != null && tl.red && tr != null && tr.red)
3136 >                return false;
3137 >            if (tl != null && !checkInvariants(tl))
3138 >                return false;
3139 >            if (tr != null && !checkInvariants(tr))
3140 >                return false;
3141 >            return true;
3142          }
3143  
3144 <        /**
3145 <         * Performs the given action for each value.
3146 <         *
3147 <         * @param action the action
3148 <         */
3149 <        public void forEachValue(Action<V> action) {
3150 <            fjp.invoke(ForkJoinTasks.forEachValue
3151 <                       (ConcurrentHashMapV8.this, action));
3144 >        private static final sun.misc.Unsafe U;
3145 >        private static final long LOCKSTATE;
3146 >        static {
3147 >            try {
3148 >                U = getUnsafe();
3149 >                Class<?> k = TreeBin.class;
3150 >                LOCKSTATE = U.objectFieldOffset
3151 >                    (k.getDeclaredField("lockState"));
3152 >            } catch (Exception e) {
3153 >                throw new Error(e);
3154 >            }
3155          }
3156 +    }
3157  
3158 <        /**
3159 <         * Performs the given action for each non-null transformation
3160 <         * of each value.
3161 <         *
3162 <         * @param transformer a function returning the transformation
3163 <         * for an element, or null of there is no transformation (in
3164 <         * which case the action is not applied).
3165 <         */
3166 <        public <U> void forEachValue(Fun<? super V, ? extends U> transformer,
3167 <                                     Action<U> action) {
3168 <            fjp.invoke(ForkJoinTasks.forEachValue
3169 <                       (ConcurrentHashMapV8.this, transformer, action));
3158 >    /* ----------------Table Traversal -------------- */
3159 >
3160 >    /**
3161 >     * Records the table, its length, and current traversal index for a
3162 >     * traverser that must process a region of a forwarded table before
3163 >     * proceeding with current table.
3164 >     */
3165 >    static final class TableStack<K,V> {
3166 >        int length;
3167 >        int index;
3168 >        Node<K,V>[] tab;
3169 >        TableStack<K,V> next;
3170 >    }
3171 >
3172 >    /**
3173 >     * Encapsulates traversal for methods such as containsValue; also
3174 >     * serves as a base class for other iterators and spliterators.
3175 >     *
3176 >     * Method advance visits once each still-valid node that was
3177 >     * reachable upon iterator construction. It might miss some that
3178 >     * were added to a bin after the bin was visited, which is OK wrt
3179 >     * consistency guarantees. Maintaining this property in the face
3180 >     * of possible ongoing resizes requires a fair amount of
3181 >     * bookkeeping state that is difficult to optimize away amidst
3182 >     * volatile accesses.  Even so, traversal maintains reasonable
3183 >     * throughput.
3184 >     *
3185 >     * Normally, iteration proceeds bin-by-bin traversing lists.
3186 >     * However, if the table has been resized, then all future steps
3187 >     * must traverse both the bin at the current index as well as at
3188 >     * (index + baseSize); and so on for further resizings. To
3189 >     * paranoically cope with potential sharing by users of iterators
3190 >     * across threads, iteration terminates if a bounds checks fails
3191 >     * for a table read.
3192 >     */
3193 >    static class Traverser<K,V> {
3194 >        Node<K,V>[] tab;        // current table; updated if resized
3195 >        Node<K,V> next;         // the next entry to use
3196 >        TableStack<K,V> stack, spare; // to save/restore on ForwardingNodes
3197 >        int index;              // index of bin to use next
3198 >        int baseIndex;          // current index of initial table
3199 >        int baseLimit;          // index bound for initial table
3200 >        final int baseSize;     // initial table size
3201 >
3202 >        Traverser(Node<K,V>[] tab, int size, int index, int limit) {
3203 >            this.tab = tab;
3204 >            this.baseSize = size;
3205 >            this.baseIndex = this.index = index;
3206 >            this.baseLimit = limit;
3207 >            this.next = null;
3208          }
3209  
3210          /**
3211 <         * Returns a non-null result from applying the given search
3212 <         * function on each value, or null if none.  Further element
3213 <         * processing is suppressed upon success. However, this method
3214 <         * does not return until other in-progress parallel
3215 <         * invocations of the search function also complete.
3216 <         *
3217 <         * @param searchFunction a function returning a non-null
3218 <         * result on success, else null
3219 <         * @return a non-null result from applying the given search
3220 <         * function on each value, or null if none
3221 <         *
3222 <         */
3223 <        public <U> U searchValues(Fun<? super V, ? extends U> searchFunction) {
3224 <            return fjp.invoke(ForkJoinTasks.searchValues
3225 <                              (ConcurrentHashMapV8.this, searchFunction));
3211 >         * Advances if possible, returning next valid node, or null if none.
3212 >         */
3213 >        final Node<K,V> advance() {
3214 >            Node<K,V> e;
3215 >            if ((e = next) != null)
3216 >                e = e.next;
3217 >            for (;;) {
3218 >                Node<K,V>[] t; int i, n;  // must use locals in checks
3219 >                if (e != null)
3220 >                    return next = e;
3221 >                if (baseIndex >= baseLimit || (t = tab) == null ||
3222 >                    (n = t.length) <= (i = index) || i < 0)
3223 >                    return next = null;
3224 >                if ((e = tabAt(t, i)) != null && e.hash < 0) {
3225 >                    if (e instanceof ForwardingNode) {
3226 >                        tab = ((ForwardingNode<K,V>)e).nextTable;
3227 >                        e = null;
3228 >                        pushState(t, i, n);
3229 >                        continue;
3230 >                    }
3231 >                    else if (e instanceof TreeBin)
3232 >                        e = ((TreeBin<K,V>)e).first;
3233 >                    else
3234 >                        e = null;
3235 >                }
3236 >                if (stack != null)
3237 >                    recoverState(n);
3238 >                else if ((index = i + baseSize) >= n)
3239 >                    index = ++baseIndex; // visit upper slots if present
3240 >            }
3241          }
3242  
3243          /**
3244 <         * Returns the result of accumulating all values using the
3924 <         * given reducer to combine values, or null if none.
3925 <         *
3926 <         * @param reducer a commutative associative combining function
3927 <         * @return  the result of accumulating all values
3244 >         * Saves traversal state upon encountering a forwarding node.
3245           */
3246 <        public V reduceValues(BiFun<? super V, ? super V, ? extends V> reducer) {
3247 <            return fjp.invoke(ForkJoinTasks.reduceValues
3248 <                              (ConcurrentHashMapV8.this, reducer));
3246 >        private void pushState(Node<K,V>[] t, int i, int n) {
3247 >            TableStack<K,V> s = spare;  // reuse if possible
3248 >            if (s != null)
3249 >                spare = s.next;
3250 >            else
3251 >                s = new TableStack<K,V>();
3252 >            s.tab = t;
3253 >            s.length = n;
3254 >            s.index = i;
3255 >            s.next = stack;
3256 >            stack = s;
3257          }
3258  
3259          /**
3260 <         * Returns the result of accumulating the given transformation
3936 <         * of all values using the given reducer to combine values, or
3937 <         * null if none.
3260 >         * Possibly pops traversal state.
3261           *
3262 <         * @param transformer a function returning the transformation
3940 <         * for an element, or null of there is no transformation (in
3941 <         * which case it is not combined).
3942 <         * @param reducer a commutative associative combining function
3943 <         * @return the result of accumulating the given transformation
3944 <         * of all values
3262 >         * @param n length of current table
3263           */
3264 <        public <U> U reduceValues(Fun<? super V, ? extends U> transformer,
3265 <                                  BiFun<? super U, ? super U, ? extends U> reducer) {
3266 <            return fjp.invoke(ForkJoinTasks.reduceValues
3267 <                              (ConcurrentHashMapV8.this, transformer, reducer));
3264 >        private void recoverState(int n) {
3265 >            TableStack<K,V> s; int len;
3266 >            while ((s = stack) != null && (index += (len = s.length)) >= n) {
3267 >                n = len;
3268 >                index = s.index;
3269 >                tab = s.tab;
3270 >                s.tab = null;
3271 >                TableStack<K,V> next = s.next;
3272 >                s.next = spare; // save for reuse
3273 >                stack = next;
3274 >                spare = s;
3275 >            }
3276 >            if (s == null && (index += baseSize) >= n)
3277 >                index = ++baseIndex;
3278          }
3279 +    }
3280  
3281 <        /**
3282 <         * Returns the result of accumulating the given transformation
3283 <         * of all values using the given reducer to combine values,
3284 <         * and the given basis as an identity value.
3285 <         *
3286 <         * @param transformer a function returning the transformation
3287 <         * for an element
3288 <         * @param basis the identity (initial default value) for the reduction
3289 <         * @param reducer a commutative associative combining function
3290 <         * @return the result of accumulating the given transformation
3291 <         * of all values
3292 <         */
3964 <        public double reduceValuesToDouble(ObjectToDouble<? super V> transformer,
3965 <                                           double basis,
3966 <                                           DoubleByDoubleToDouble reducer) {
3967 <            return fjp.invoke(ForkJoinTasks.reduceValuesToDouble
3968 <                              (ConcurrentHashMapV8.this, transformer, basis, reducer));
3281 >    /**
3282 >     * Base of key, value, and entry Iterators. Adds fields to
3283 >     * Traverser to support iterator.remove.
3284 >     */
3285 >    static class BaseIterator<K,V> extends Traverser<K,V> {
3286 >        final ConcurrentHashMapV8<K,V> map;
3287 >        Node<K,V> lastReturned;
3288 >        BaseIterator(Node<K,V>[] tab, int size, int index, int limit,
3289 >                    ConcurrentHashMapV8<K,V> map) {
3290 >            super(tab, size, index, limit);
3291 >            this.map = map;
3292 >            advance();
3293          }
3294  
3295 <        /**
3296 <         * Returns the result of accumulating the given transformation
3297 <         * of all values using the given reducer to combine values,
3298 <         * and the given basis as an identity value.
3299 <         *
3300 <         * @param transformer a function returning the transformation
3301 <         * for an element
3302 <         * @param basis the identity (initial default value) for the reduction
3303 <         * @param reducer a commutative associative combining function
3980 <         * @return the result of accumulating the given transformation
3981 <         * of all values
3982 <         */
3983 <        public long reduceValuesToLong(ObjectToLong<? super V> transformer,
3984 <                                       long basis,
3985 <                                       LongByLongToLong reducer) {
3986 <            return fjp.invoke(ForkJoinTasks.reduceValuesToLong
3987 <                              (ConcurrentHashMapV8.this, transformer, basis, reducer));
3295 >        public final boolean hasNext() { return next != null; }
3296 >        public final boolean hasMoreElements() { return next != null; }
3297 >
3298 >        public final void remove() {
3299 >            Node<K,V> p;
3300 >            if ((p = lastReturned) == null)
3301 >                throw new IllegalStateException();
3302 >            lastReturned = null;
3303 >            map.replaceNode(p.key, null, null);
3304          }
3305 +    }
3306  
3307 <        /**
3308 <         * Returns the result of accumulating the given transformation
3309 <         * of all values using the given reducer to combine values,
3310 <         * and the given basis as an identity value.
3311 <         *
3995 <         * @param transformer a function returning the transformation
3996 <         * for an element
3997 <         * @param basis the identity (initial default value) for the reduction
3998 <         * @param reducer a commutative associative combining function
3999 <         * @return the result of accumulating the given transformation
4000 <         * of all values
4001 <         */
4002 <        public int reduceValuesToInt(ObjectToInt<? super V> transformer,
4003 <                                     int basis,
4004 <                                     IntByIntToInt reducer) {
4005 <            return fjp.invoke(ForkJoinTasks.reduceValuesToInt
4006 <                              (ConcurrentHashMapV8.this, transformer, basis, reducer));
3307 >    static final class KeyIterator<K,V> extends BaseIterator<K,V>
3308 >        implements Iterator<K>, Enumeration<K> {
3309 >        KeyIterator(Node<K,V>[] tab, int index, int size, int limit,
3310 >                    ConcurrentHashMapV8<K,V> map) {
3311 >            super(tab, index, size, limit, map);
3312          }
3313  
3314 <        /**
3315 <         * Performs the given action for each entry.
3316 <         *
3317 <         * @param action the action
3318 <         */
3319 <        public void forEachEntry(Action<Map.Entry<K,V>> action) {
3320 <            fjp.invoke(ForkJoinTasks.forEachEntry
3321 <                       (ConcurrentHashMapV8.this, action));
3314 >        public final K next() {
3315 >            Node<K,V> p;
3316 >            if ((p = next) == null)
3317 >                throw new NoSuchElementException();
3318 >            K k = p.key;
3319 >            lastReturned = p;
3320 >            advance();
3321 >            return k;
3322          }
3323  
3324 <        /**
3325 <         * Performs the given action for each non-null transformation
3326 <         * of each entry.
3327 <         *
3328 <         * @param transformer a function returning the transformation
3329 <         * for an element, or null of there is no transformation (in
3330 <         * which case the action is not applied).
3331 <         * @param action the action
4027 <         */
4028 <        public <U> void forEachEntry(Fun<Map.Entry<K,V>, ? extends U> transformer,
4029 <                                     Action<U> action) {
4030 <            fjp.invoke(ForkJoinTasks.forEachEntry
4031 <                       (ConcurrentHashMapV8.this, transformer, action));
3324 >        public final K nextElement() { return next(); }
3325 >    }
3326 >
3327 >    static final class ValueIterator<K,V> extends BaseIterator<K,V>
3328 >        implements Iterator<V>, Enumeration<V> {
3329 >        ValueIterator(Node<K,V>[] tab, int index, int size, int limit,
3330 >                      ConcurrentHashMapV8<K,V> map) {
3331 >            super(tab, index, size, limit, map);
3332          }
3333  
3334 <        /**
3335 <         * Returns a non-null result from applying the given search
3336 <         * function on each entry, or null if none.  Further element
3337 <         * processing is suppressed upon success. However, this method
3338 <         * does not return until other in-progress parallel
3339 <         * invocations of the search function also complete.
3340 <         *
3341 <         * @param searchFunction a function returning a non-null
4042 <         * result on success, else null
4043 <         * @return a non-null result from applying the given search
4044 <         * function on each entry, or null if none
4045 <         */
4046 <        public <U> U searchEntries(Fun<Map.Entry<K,V>, ? extends U> searchFunction) {
4047 <            return fjp.invoke(ForkJoinTasks.searchEntries
4048 <                              (ConcurrentHashMapV8.this, searchFunction));
3334 >        public final V next() {
3335 >            Node<K,V> p;
3336 >            if ((p = next) == null)
3337 >                throw new NoSuchElementException();
3338 >            V v = p.val;
3339 >            lastReturned = p;
3340 >            advance();
3341 >            return v;
3342          }
3343  
3344 <        /**
3345 <         * Returns the result of accumulating all entries using the
3346 <         * given reducer to combine values, or null if none.
3347 <         *
3348 <         * @param reducer a commutative associative combining function
3349 <         * @return the result of accumulating all entries
3350 <         */
3351 <        public Map.Entry<K,V> reduceEntries(BiFun<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer) {
4059 <            return fjp.invoke(ForkJoinTasks.reduceEntries
4060 <                              (ConcurrentHashMapV8.this, reducer));
3344 >        public final V nextElement() { return next(); }
3345 >    }
3346 >
3347 >    static final class EntryIterator<K,V> extends BaseIterator<K,V>
3348 >        implements Iterator<Map.Entry<K,V>> {
3349 >        EntryIterator(Node<K,V>[] tab, int index, int size, int limit,
3350 >                      ConcurrentHashMapV8<K,V> map) {
3351 >            super(tab, index, size, limit, map);
3352          }
3353  
3354 <        /**
3355 <         * Returns the result of accumulating the given transformation
3356 <         * of all entries using the given reducer to combine values,
3357 <         * or null if none.
3358 <         *
3359 <         * @param transformer a function returning the transformation
3360 <         * for an element, or null of there is no transformation (in
3361 <         * which case it is not combined).
3362 <         * @param reducer a commutative associative combining function
4072 <         * @return the result of accumulating the given transformation
4073 <         * of all entries
4074 <         */
4075 <        public <U> U reduceEntries(Fun<Map.Entry<K,V>, ? extends U> transformer,
4076 <                                   BiFun<? super U, ? super U, ? extends U> reducer) {
4077 <            return fjp.invoke(ForkJoinTasks.reduceEntries
4078 <                              (ConcurrentHashMapV8.this, transformer, reducer));
3354 >        public final Map.Entry<K,V> next() {
3355 >            Node<K,V> p;
3356 >            if ((p = next) == null)
3357 >                throw new NoSuchElementException();
3358 >            K k = p.key;
3359 >            V v = p.val;
3360 >            lastReturned = p;
3361 >            advance();
3362 >            return new MapEntry<K,V>(k, v, map);
3363          }
3364 +    }
3365  
3366 <        /**
3367 <         * Returns the result of accumulating the given transformation
3368 <         * of all entries using the given reducer to combine values,
3369 <         * and the given basis as an identity value.
3370 <         *
3371 <         * @param transformer a function returning the transformation
3372 <         * for an element
3373 <         * @param basis the identity (initial default value) for the reduction
3374 <         * @param reducer a commutative associative combining function
3375 <         * @return the result of accumulating the given transformation
3376 <         * of all entries
4092 <         */
4093 <        public double reduceEntriesToDouble(ObjectToDouble<Map.Entry<K,V>> transformer,
4094 <                                            double basis,
4095 <                                            DoubleByDoubleToDouble reducer) {
4096 <            return fjp.invoke(ForkJoinTasks.reduceEntriesToDouble
4097 <                              (ConcurrentHashMapV8.this, transformer, basis, reducer));
3366 >    /**
3367 >     * Exported Entry for EntryIterator
3368 >     */
3369 >    static final class MapEntry<K,V> implements Map.Entry<K,V> {
3370 >        final K key; // non-null
3371 >        V val;       // non-null
3372 >        final ConcurrentHashMapV8<K,V> map;
3373 >        MapEntry(K key, V val, ConcurrentHashMapV8<K,V> map) {
3374 >            this.key = key;
3375 >            this.val = val;
3376 >            this.map = map;
3377          }
3378 +        public K getKey()        { return key; }
3379 +        public V getValue()      { return val; }
3380 +        public int hashCode()    { return key.hashCode() ^ val.hashCode(); }
3381 +        public String toString() { return key + "=" + val; }
3382  
3383 <        /**
3384 <         * Returns the result of accumulating the given transformation
3385 <         * of all entries using the given reducer to combine values,
3386 <         * and the given basis as an identity value.
3387 <         *
3388 <         * @param transformer a function returning the transformation
3389 <         * for an element
4107 <         * @param basis the identity (initial default value) for the reduction
4108 <         * @param reducer a commutative associative combining function
4109 <         * @return  the result of accumulating the given transformation
4110 <         * of all entries
4111 <         */
4112 <        public long reduceEntriesToLong(ObjectToLong<Map.Entry<K,V>> transformer,
4113 <                                        long basis,
4114 <                                        LongByLongToLong reducer) {
4115 <            return fjp.invoke(ForkJoinTasks.reduceEntriesToLong
4116 <                              (ConcurrentHashMapV8.this, transformer, basis, reducer));
3383 >        public boolean equals(Object o) {
3384 >            Object k, v; Map.Entry<?,?> e;
3385 >            return ((o instanceof Map.Entry) &&
3386 >                    (k = (e = (Map.Entry<?,?>)o).getKey()) != null &&
3387 >                    (v = e.getValue()) != null &&
3388 >                    (k == key || k.equals(key)) &&
3389 >                    (v == val || v.equals(val)));
3390          }
3391  
3392          /**
3393 <         * Returns the result of accumulating the given transformation
3394 <         * of all entries using the given reducer to combine values,
3395 <         * and the given basis as an identity value.
3396 <         *
3397 <         * @param transformer a function returning the transformation
3398 <         * for an element
4126 <         * @param basis the identity (initial default value) for the reduction
4127 <         * @param reducer a commutative associative combining function
4128 <         * @return the result of accumulating the given transformation
4129 <         * of all entries
3393 >         * Sets our entry's value and writes through to the map. The
3394 >         * value to return is somewhat arbitrary here. Since we do not
3395 >         * necessarily track asynchronous changes, the most recent
3396 >         * "previous" value could be different from what we return (or
3397 >         * could even have been removed, in which case the put will
3398 >         * re-establish). We do not and cannot guarantee more.
3399           */
3400 <        public int reduceEntriesToInt(ObjectToInt<Map.Entry<K,V>> transformer,
3401 <                                      int basis,
3402 <                                      IntByIntToInt reducer) {
3403 <            return fjp.invoke(ForkJoinTasks.reduceEntriesToInt
3404 <                              (ConcurrentHashMapV8.this, transformer, basis, reducer));
3400 >        public V setValue(V value) {
3401 >            if (value == null) throw new NullPointerException();
3402 >            V v = val;
3403 >            val = value;
3404 >            map.put(key, value);
3405 >            return v;
3406          }
3407      }
3408  
3409 <    // ---------------------------------------------------------------------
3409 >    static final class KeySpliterator<K,V> extends Traverser<K,V>
3410 >        implements ConcurrentHashMapSpliterator<K> {
3411 >        long est;               // size estimate
3412 >        KeySpliterator(Node<K,V>[] tab, int size, int index, int limit,
3413 >                       long est) {
3414 >            super(tab, size, index, limit);
3415 >            this.est = est;
3416 >        }
3417 >
3418 >        public ConcurrentHashMapSpliterator<K> trySplit() {
3419 >            int i, f, h;
3420 >            return (h = ((i = baseIndex) + (f = baseLimit)) >>> 1) <= i ? null :
3421 >                new KeySpliterator<K,V>(tab, baseSize, baseLimit = h,
3422 >                                        f, est >>>= 1);
3423 >        }
3424  
3425 <    /**
3426 <     * Predefined tasks for performing bulk parallel operations on
3427 <     * ConcurrentHashMaps. These tasks follow the forms and rules used
3428 <     * in class {@link Parallel}. Each method has the same name, but
3429 <     * returns a task rather than invoking it. These methods may be
4146 <     * useful in custom applications such as submitting a task without
4147 <     * waiting for completion, or combining with other tasks.
4148 <     */
4149 <    public static class ForkJoinTasks {
4150 <        private ForkJoinTasks() {}
3425 >        public void forEachRemaining(Action<? super K> action) {
3426 >            if (action == null) throw new NullPointerException();
3427 >            for (Node<K,V> p; (p = advance()) != null;)
3428 >                action.apply(p.key);
3429 >        }
3430  
3431 <        /**
4153 <         * Returns a task that when invoked, performs the given
4154 <         * action for each (key, value)
4155 <         *
4156 <         * @param map the map
4157 <         * @param action the action
4158 <         * @return the task
4159 <         */
4160 <        public static <K,V> ForkJoinTask<Void> forEach
4161 <            (ConcurrentHashMapV8<K,V> map,
4162 <             BiAction<K,V> action) {
3431 >        public boolean tryAdvance(Action<? super K> action) {
3432              if (action == null) throw new NullPointerException();
3433 <            return new ForEachMappingTask<K,V>(map, action);
3433 >            Node<K,V> p;
3434 >            if ((p = advance()) == null)
3435 >                return false;
3436 >            action.apply(p.key);
3437 >            return true;
3438          }
3439  
3440 <        /**
3441 <         * Returns a task that when invoked, performs the given
3442 <         * action for each non-null transformation of each (key, value)
3443 <         *
3444 <         * @param map the map
3445 <         * @param transformer a function returning the transformation
3446 <         * for an element, or null of there is no transformation (in
3447 <         * which case the action is not applied).
3448 <         * @param action the action
3449 <         * @return the task
3450 <         */
4178 <        public static <K,V,U> ForkJoinTask<Void> forEach
4179 <            (ConcurrentHashMapV8<K,V> map,
4180 <             BiFun<? super K, ? super V, ? extends U> transformer,
4181 <             Action<U> action) {
4182 <            if (transformer == null || action == null)
4183 <                throw new NullPointerException();
4184 <            return new ForEachTransformedMappingTask<K,V,U>
4185 <                (map, transformer, action);
3440 >        public long estimateSize() { return est; }
3441 >
3442 >    }
3443 >
3444 >    static final class ValueSpliterator<K,V> extends Traverser<K,V>
3445 >        implements ConcurrentHashMapSpliterator<V> {
3446 >        long est;               // size estimate
3447 >        ValueSpliterator(Node<K,V>[] tab, int size, int index, int limit,
3448 >                         long est) {
3449 >            super(tab, size, index, limit);
3450 >            this.est = est;
3451          }
3452  
3453 <        /**
3454 <         * Returns a task that when invoked, returns a non-null
3455 <         * result from applying the given search function on each
3456 <         * (key, value), or null if none.  Further element processing
3457 <         * is suppressed upon success. However, this method does not
4193 <         * return until other in-progress parallel invocations of the
4194 <         * search function also complete.
4195 <         *
4196 <         * @param map the map
4197 <         * @param searchFunction a function returning a non-null
4198 <         * result on success, else null
4199 <         * @return the task
4200 <         */
4201 <        public static <K,V,U> ForkJoinTask<U> search
4202 <            (ConcurrentHashMapV8<K,V> map,
4203 <             BiFun<? super K, ? super V, ? extends U> searchFunction) {
4204 <            if (searchFunction == null) throw new NullPointerException();
4205 <            return new SearchMappingsTask<K,V,U>
4206 <                (map, searchFunction,
4207 <                 new AtomicReference<U>());
3453 >        public ConcurrentHashMapSpliterator<V> trySplit() {
3454 >            int i, f, h;
3455 >            return (h = ((i = baseIndex) + (f = baseLimit)) >>> 1) <= i ? null :
3456 >                new ValueSpliterator<K,V>(tab, baseSize, baseLimit = h,
3457 >                                          f, est >>>= 1);
3458          }
3459  
3460 <        /**
3461 <         * Returns a task that when invoked, returns the result of
3462 <         * accumulating the given transformation of all (key, value) pairs
3463 <         * using the given reducer to combine values, or null if none.
4214 <         *
4215 <         * @param map the map
4216 <         * @param transformer a function returning the transformation
4217 <         * for an element, or null of there is no transformation (in
4218 <         * which case it is not combined).
4219 <         * @param reducer a commutative associative combining function
4220 <         * @return the task
4221 <         */
4222 <        public static <K,V,U> ForkJoinTask<U> reduce
4223 <            (ConcurrentHashMapV8<K,V> map,
4224 <             BiFun<? super K, ? super V, ? extends U> transformer,
4225 <             BiFun<? super U, ? super U, ? extends U> reducer) {
4226 <            if (transformer == null || reducer == null)
4227 <                throw new NullPointerException();
4228 <            return new MapReduceMappingsTask<K,V,U>
4229 <                (map, transformer, reducer);
3460 >        public void forEachRemaining(Action<? super V> action) {
3461 >            if (action == null) throw new NullPointerException();
3462 >            for (Node<K,V> p; (p = advance()) != null;)
3463 >                action.apply(p.val);
3464          }
3465  
3466 <        /**
3467 <         * Returns a task that when invoked, returns the result of
3468 <         * accumulating the given transformation of all (key, value) pairs
3469 <         * using the given reducer to combine values, and the given
3470 <         * basis as an identity value.
3471 <         *
3472 <         * @param map the map
4239 <         * @param transformer a function returning the transformation
4240 <         * for an element
4241 <         * @param basis the identity (initial default value) for the reduction
4242 <         * @param reducer a commutative associative combining function
4243 <         * @return the task
4244 <         */
4245 <        public static <K,V> ForkJoinTask<Double> reduceToDouble
4246 <            (ConcurrentHashMapV8<K,V> map,
4247 <             ObjectByObjectToDouble<? super K, ? super V> transformer,
4248 <             double basis,
4249 <             DoubleByDoubleToDouble reducer) {
4250 <            if (transformer == null || reducer == null)
4251 <                throw new NullPointerException();
4252 <            return new MapReduceMappingsToDoubleTask<K,V>
4253 <                (map, transformer, basis, reducer);
3466 >        public boolean tryAdvance(Action<? super V> action) {
3467 >            if (action == null) throw new NullPointerException();
3468 >            Node<K,V> p;
3469 >            if ((p = advance()) == null)
3470 >                return false;
3471 >            action.apply(p.val);
3472 >            return true;
3473          }
3474  
3475 <        /**
3476 <         * Returns a task that when invoked, returns the result of
3477 <         * accumulating the given transformation of all (key, value) pairs
3478 <         * using the given reducer to combine values, and the given
3479 <         * basis as an identity value.
3480 <         *
3481 <         * @param map the map
3482 <         * @param transformer a function returning the transformation
3483 <         * for an element
3484 <         * @param basis the identity (initial default value) for the reduction
3485 <         * @param reducer a commutative associative combining function
3486 <         * @return the task
3487 <         */
4269 <        public static <K,V> ForkJoinTask<Long> reduceToLong
4270 <            (ConcurrentHashMapV8<K,V> map,
4271 <             ObjectByObjectToLong<? super K, ? super V> transformer,
4272 <             long basis,
4273 <             LongByLongToLong reducer) {
4274 <            if (transformer == null || reducer == null)
4275 <                throw new NullPointerException();
4276 <            return new MapReduceMappingsToLongTask<K,V>
4277 <                (map, transformer, basis, reducer);
3475 >        public long estimateSize() { return est; }
3476 >
3477 >    }
3478 >
3479 >    static final class EntrySpliterator<K,V> extends Traverser<K,V>
3480 >        implements ConcurrentHashMapSpliterator<Map.Entry<K,V>> {
3481 >        final ConcurrentHashMapV8<K,V> map; // To export MapEntry
3482 >        long est;               // size estimate
3483 >        EntrySpliterator(Node<K,V>[] tab, int size, int index, int limit,
3484 >                         long est, ConcurrentHashMapV8<K,V> map) {
3485 >            super(tab, size, index, limit);
3486 >            this.map = map;
3487 >            this.est = est;
3488          }
3489  
3490 <        /**
3491 <         * Returns a task that when invoked, returns the result of
3492 <         * accumulating the given transformation of all (key, value) pairs
3493 <         * using the given reducer to combine values, and the given
3494 <         * basis as an identity value.
4285 <         *
4286 <         * @param transformer a function returning the transformation
4287 <         * for an element
4288 <         * @param basis the identity (initial default value) for the reduction
4289 <         * @param reducer a commutative associative combining function
4290 <         * @return the task
4291 <         */
4292 <        public static <K,V> ForkJoinTask<Integer> reduceToInt
4293 <            (ConcurrentHashMapV8<K,V> map,
4294 <             ObjectByObjectToInt<? super K, ? super V> transformer,
4295 <             int basis,
4296 <             IntByIntToInt reducer) {
4297 <            if (transformer == null || reducer == null)
4298 <                throw new NullPointerException();
4299 <            return new MapReduceMappingsToIntTask<K,V>
4300 <                (map, transformer, basis, reducer);
3490 >        public ConcurrentHashMapSpliterator<Map.Entry<K,V>> trySplit() {
3491 >            int i, f, h;
3492 >            return (h = ((i = baseIndex) + (f = baseLimit)) >>> 1) <= i ? null :
3493 >                new EntrySpliterator<K,V>(tab, baseSize, baseLimit = h,
3494 >                                          f, est >>>= 1, map);
3495          }
3496  
3497 <        /**
4304 <         * Returns a task that when invoked, performs the given action
4305 <         * for each key.
4306 <         *
4307 <         * @param map the map
4308 <         * @param action the action
4309 <         * @return the task
4310 <         */
4311 <        public static <K,V> ForkJoinTask<Void> forEachKey
4312 <            (ConcurrentHashMapV8<K,V> map,
4313 <             Action<K> action) {
3497 >        public void forEachRemaining(Action<? super Map.Entry<K,V>> action) {
3498              if (action == null) throw new NullPointerException();
3499 <            return new ForEachKeyTask<K,V>(map, action);
3499 >            for (Node<K,V> p; (p = advance()) != null; )
3500 >                action.apply(new MapEntry<K,V>(p.key, p.val, map));
3501          }
3502  
3503 <        /**
3504 <         * Returns a task that when invoked, performs the given action
3505 <         * for each non-null transformation of each key.
3506 <         *
3507 <         * @param map the map
3508 <         * @param transformer a function returning the transformation
3509 <         * for an element, or null of there is no transformation (in
4325 <         * which case the action is not applied).
4326 <         * @param action the action
4327 <         * @return the task
4328 <         */
4329 <        public static <K,V,U> ForkJoinTask<Void> forEachKey
4330 <            (ConcurrentHashMapV8<K,V> map,
4331 <             Fun<? super K, ? extends U> transformer,
4332 <             Action<U> action) {
4333 <            if (transformer == null || action == null)
4334 <                throw new NullPointerException();
4335 <            return new ForEachTransformedKeyTask<K,V,U>
4336 <                (map, transformer, action);
3503 >        public boolean tryAdvance(Action<? super Map.Entry<K,V>> action) {
3504 >            if (action == null) throw new NullPointerException();
3505 >            Node<K,V> p;
3506 >            if ((p = advance()) == null)
3507 >                return false;
3508 >            action.apply(new MapEntry<K,V>(p.key, p.val, map));
3509 >            return true;
3510          }
3511  
3512 +        public long estimateSize() { return est; }
3513 +
3514 +    }
3515 +
3516 +    // Parallel bulk operations
3517 +
3518 +    /**
3519 +     * Computes initial batch value for bulk tasks. The returned value
3520 +     * is approximately exp2 of the number of times (minus one) to
3521 +     * split task by two before executing leaf action. This value is
3522 +     * faster to compute and more convenient to use as a guide to
3523 +     * splitting than is the depth, since it is used while dividing by
3524 +     * two anyway.
3525 +     */
3526 +    final int batchFor(long b) {
3527 +        long n;
3528 +        if (b == Long.MAX_VALUE || (n = sumCount()) <= 1L || n < b)
3529 +            return 0;
3530 +        int sp = ForkJoinPool.getCommonPoolParallelism() << 2; // slack of 4
3531 +        return (b <= 0L || (n /= b) >= sp) ? sp : (int)n;
3532 +    }
3533 +
3534 +    /**
3535 +     * Performs the given action for each (key, value).
3536 +     *
3537 +     * @param parallelismThreshold the (estimated) number of elements
3538 +     * needed for this operation to be executed in parallel
3539 +     * @param action the action
3540 +     * @since 1.8
3541 +     */
3542 +    public void forEach(long parallelismThreshold,
3543 +                        BiAction<? super K,? super V> action) {
3544 +        if (action == null) throw new NullPointerException();
3545 +        new ForEachMappingTask<K,V>
3546 +            (null, batchFor(parallelismThreshold), 0, 0, table,
3547 +             action).invoke();
3548 +    }
3549 +
3550 +    /**
3551 +     * Performs the given action for each non-null transformation
3552 +     * of each (key, value).
3553 +     *
3554 +     * @param parallelismThreshold the (estimated) number of elements
3555 +     * needed for this operation to be executed in parallel
3556 +     * @param transformer a function returning the transformation
3557 +     * for an element, or null if there is no transformation (in
3558 +     * which case the action is not applied)
3559 +     * @param action the action
3560 +     * @since 1.8
3561 +     */
3562 +    public <U> void forEach(long parallelismThreshold,
3563 +                            BiFun<? super K, ? super V, ? extends U> transformer,
3564 +                            Action<? super U> action) {
3565 +        if (transformer == null || action == null)
3566 +            throw new NullPointerException();
3567 +        new ForEachTransformedMappingTask<K,V,U>
3568 +            (null, batchFor(parallelismThreshold), 0, 0, table,
3569 +             transformer, action).invoke();
3570 +    }
3571 +
3572 +    /**
3573 +     * Returns a non-null result from applying the given search
3574 +     * function on each (key, value), or null if none.  Upon
3575 +     * success, further element processing is suppressed and the
3576 +     * results of any other parallel invocations of the search
3577 +     * function are ignored.
3578 +     *
3579 +     * @param parallelismThreshold the (estimated) number of elements
3580 +     * needed for this operation to be executed in parallel
3581 +     * @param searchFunction a function returning a non-null
3582 +     * result on success, else null
3583 +     * @return a non-null result from applying the given search
3584 +     * function on each (key, value), or null if none
3585 +     * @since 1.8
3586 +     */
3587 +    public <U> U search(long parallelismThreshold,
3588 +                        BiFun<? super K, ? super V, ? extends U> searchFunction) {
3589 +        if (searchFunction == null) throw new NullPointerException();
3590 +        return new SearchMappingsTask<K,V,U>
3591 +            (null, batchFor(parallelismThreshold), 0, 0, table,
3592 +             searchFunction, new AtomicReference<U>()).invoke();
3593 +    }
3594 +
3595 +    /**
3596 +     * Returns the result of accumulating the given transformation
3597 +     * of all (key, value) pairs using the given reducer to
3598 +     * combine values, or null if none.
3599 +     *
3600 +     * @param parallelismThreshold the (estimated) number of elements
3601 +     * needed for this operation to be executed in parallel
3602 +     * @param transformer a function returning the transformation
3603 +     * for an element, or null if there is no transformation (in
3604 +     * which case it is not combined)
3605 +     * @param reducer a commutative associative combining function
3606 +     * @return the result of accumulating the given transformation
3607 +     * of all (key, value) pairs
3608 +     * @since 1.8
3609 +     */
3610 +    public <U> U reduce(long parallelismThreshold,
3611 +                        BiFun<? super K, ? super V, ? extends U> transformer,
3612 +                        BiFun<? super U, ? super U, ? extends U> reducer) {
3613 +        if (transformer == null || reducer == null)
3614 +            throw new NullPointerException();
3615 +        return new MapReduceMappingsTask<K,V,U>
3616 +            (null, batchFor(parallelismThreshold), 0, 0, table,
3617 +             null, transformer, reducer).invoke();
3618 +    }
3619 +
3620 +    /**
3621 +     * Returns the result of accumulating the given transformation
3622 +     * of all (key, value) pairs using the given reducer to
3623 +     * combine values, and the given basis as an identity value.
3624 +     *
3625 +     * @param parallelismThreshold the (estimated) number of elements
3626 +     * needed for this operation to be executed in parallel
3627 +     * @param transformer a function returning the transformation
3628 +     * for an element
3629 +     * @param basis the identity (initial default value) for the reduction
3630 +     * @param reducer a commutative associative combining function
3631 +     * @return the result of accumulating the given transformation
3632 +     * of all (key, value) pairs
3633 +     * @since 1.8
3634 +     */
3635 +    public double reduceToDouble(long parallelismThreshold,
3636 +                                 ObjectByObjectToDouble<? super K, ? super V> transformer,
3637 +                                 double basis,
3638 +                                 DoubleByDoubleToDouble reducer) {
3639 +        if (transformer == null || reducer == null)
3640 +            throw new NullPointerException();
3641 +        return new MapReduceMappingsToDoubleTask<K,V>
3642 +            (null, batchFor(parallelismThreshold), 0, 0, table,
3643 +             null, transformer, basis, reducer).invoke();
3644 +    }
3645 +
3646 +    /**
3647 +     * Returns the result of accumulating the given transformation
3648 +     * of all (key, value) pairs using the given reducer to
3649 +     * combine values, and the given basis as an identity value.
3650 +     *
3651 +     * @param parallelismThreshold the (estimated) number of elements
3652 +     * needed for this operation to be executed in parallel
3653 +     * @param transformer a function returning the transformation
3654 +     * for an element
3655 +     * @param basis the identity (initial default value) for the reduction
3656 +     * @param reducer a commutative associative combining function
3657 +     * @return the result of accumulating the given transformation
3658 +     * of all (key, value) pairs
3659 +     * @since 1.8
3660 +     */
3661 +    public long reduceToLong(long parallelismThreshold,
3662 +                             ObjectByObjectToLong<? super K, ? super V> transformer,
3663 +                             long basis,
3664 +                             LongByLongToLong reducer) {
3665 +        if (transformer == null || reducer == null)
3666 +            throw new NullPointerException();
3667 +        return new MapReduceMappingsToLongTask<K,V>
3668 +            (null, batchFor(parallelismThreshold), 0, 0, table,
3669 +             null, transformer, basis, reducer).invoke();
3670 +    }
3671 +
3672 +    /**
3673 +     * Returns the result of accumulating the given transformation
3674 +     * of all (key, value) pairs using the given reducer to
3675 +     * combine values, and the given basis as an identity value.
3676 +     *
3677 +     * @param parallelismThreshold the (estimated) number of elements
3678 +     * needed for this operation to be executed in parallel
3679 +     * @param transformer a function returning the transformation
3680 +     * for an element
3681 +     * @param basis the identity (initial default value) for the reduction
3682 +     * @param reducer a commutative associative combining function
3683 +     * @return the result of accumulating the given transformation
3684 +     * of all (key, value) pairs
3685 +     * @since 1.8
3686 +     */
3687 +    public int reduceToInt(long parallelismThreshold,
3688 +                           ObjectByObjectToInt<? super K, ? super V> transformer,
3689 +                           int basis,
3690 +                           IntByIntToInt reducer) {
3691 +        if (transformer == null || reducer == null)
3692 +            throw new NullPointerException();
3693 +        return new MapReduceMappingsToIntTask<K,V>
3694 +            (null, batchFor(parallelismThreshold), 0, 0, table,
3695 +             null, transformer, basis, reducer).invoke();
3696 +    }
3697 +
3698 +    /**
3699 +     * Performs the given action for each key.
3700 +     *
3701 +     * @param parallelismThreshold the (estimated) number of elements
3702 +     * needed for this operation to be executed in parallel
3703 +     * @param action the action
3704 +     * @since 1.8
3705 +     */
3706 +    public void forEachKey(long parallelismThreshold,
3707 +                           Action<? super K> action) {
3708 +        if (action == null) throw new NullPointerException();
3709 +        new ForEachKeyTask<K,V>
3710 +            (null, batchFor(parallelismThreshold), 0, 0, table,
3711 +             action).invoke();
3712 +    }
3713 +
3714 +    /**
3715 +     * Performs the given action for each non-null transformation
3716 +     * of each key.
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, or null if there is no transformation (in
3722 +     * which case the action is not applied)
3723 +     * @param action the action
3724 +     * @since 1.8
3725 +     */
3726 +    public <U> void forEachKey(long parallelismThreshold,
3727 +                               Fun<? super K, ? extends U> transformer,
3728 +                               Action<? super U> action) {
3729 +        if (transformer == null || action == null)
3730 +            throw new NullPointerException();
3731 +        new ForEachTransformedKeyTask<K,V,U>
3732 +            (null, batchFor(parallelismThreshold), 0, 0, table,
3733 +             transformer, action).invoke();
3734 +    }
3735 +
3736 +    /**
3737 +     * Returns a non-null result from applying the given search
3738 +     * function on each key, or null if none. Upon success,
3739 +     * further element processing is suppressed and the results of
3740 +     * any other parallel invocations of the search function are
3741 +     * ignored.
3742 +     *
3743 +     * @param parallelismThreshold the (estimated) number of elements
3744 +     * needed for this operation to be executed in parallel
3745 +     * @param searchFunction a function returning a non-null
3746 +     * result on success, else null
3747 +     * @return a non-null result from applying the given search
3748 +     * function on each key, or null if none
3749 +     * @since 1.8
3750 +     */
3751 +    public <U> U searchKeys(long parallelismThreshold,
3752 +                            Fun<? super K, ? extends U> searchFunction) {
3753 +        if (searchFunction == null) throw new NullPointerException();
3754 +        return new SearchKeysTask<K,V,U>
3755 +            (null, batchFor(parallelismThreshold), 0, 0, table,
3756 +             searchFunction, new AtomicReference<U>()).invoke();
3757 +    }
3758 +
3759 +    /**
3760 +     * Returns the result of accumulating all keys using the given
3761 +     * reducer to combine values, or null if none.
3762 +     *
3763 +     * @param parallelismThreshold the (estimated) number of elements
3764 +     * needed for this operation to be executed in parallel
3765 +     * @param reducer a commutative associative combining function
3766 +     * @return the result of accumulating all keys using the given
3767 +     * reducer to combine values, or null if none
3768 +     * @since 1.8
3769 +     */
3770 +    public K reduceKeys(long parallelismThreshold,
3771 +                        BiFun<? super K, ? super K, ? extends K> reducer) {
3772 +        if (reducer == null) throw new NullPointerException();
3773 +        return new ReduceKeysTask<K,V>
3774 +            (null, batchFor(parallelismThreshold), 0, 0, table,
3775 +             null, reducer).invoke();
3776 +    }
3777 +
3778 +    /**
3779 +     * Returns the result of accumulating the given transformation
3780 +     * of all keys using the given reducer to combine values, or
3781 +     * null if none.
3782 +     *
3783 +     * @param parallelismThreshold the (estimated) number of elements
3784 +     * needed for this operation to be executed in parallel
3785 +     * @param transformer a function returning the transformation
3786 +     * for an element, or null if there is no transformation (in
3787 +     * which case it is not combined)
3788 +     * @param reducer a commutative associative combining function
3789 +     * @return the result of accumulating the given transformation
3790 +     * of all keys
3791 +     * @since 1.8
3792 +     */
3793 +    public <U> U reduceKeys(long parallelismThreshold,
3794 +                            Fun<? super K, ? extends U> transformer,
3795 +         BiFun<? super U, ? super U, ? extends U> reducer) {
3796 +        if (transformer == null || reducer == null)
3797 +            throw new NullPointerException();
3798 +        return new MapReduceKeysTask<K,V,U>
3799 +            (null, batchFor(parallelismThreshold), 0, 0, table,
3800 +             null, transformer, reducer).invoke();
3801 +    }
3802 +
3803 +    /**
3804 +     * Returns the result of accumulating the given transformation
3805 +     * of all keys using the given reducer to combine values, and
3806 +     * the given basis as an identity value.
3807 +     *
3808 +     * @param parallelismThreshold the (estimated) number of elements
3809 +     * needed for this operation to be executed in parallel
3810 +     * @param transformer a function returning the transformation
3811 +     * for an element
3812 +     * @param basis the identity (initial default value) for the reduction
3813 +     * @param reducer a commutative associative combining function
3814 +     * @return the result of accumulating the given transformation
3815 +     * of all keys
3816 +     * @since 1.8
3817 +     */
3818 +    public double reduceKeysToDouble(long parallelismThreshold,
3819 +                                     ObjectToDouble<? super K> transformer,
3820 +                                     double basis,
3821 +                                     DoubleByDoubleToDouble reducer) {
3822 +        if (transformer == null || reducer == null)
3823 +            throw new NullPointerException();
3824 +        return new MapReduceKeysToDoubleTask<K,V>
3825 +            (null, batchFor(parallelismThreshold), 0, 0, table,
3826 +             null, transformer, basis, reducer).invoke();
3827 +    }
3828 +
3829 +    /**
3830 +     * Returns the result of accumulating the given transformation
3831 +     * of all keys using the given reducer to combine values, and
3832 +     * the given basis as an identity value.
3833 +     *
3834 +     * @param parallelismThreshold the (estimated) number of elements
3835 +     * needed for this operation to be executed in parallel
3836 +     * @param transformer a function returning the transformation
3837 +     * for an element
3838 +     * @param basis the identity (initial default value) for the reduction
3839 +     * @param reducer a commutative associative combining function
3840 +     * @return the result of accumulating the given transformation
3841 +     * of all keys
3842 +     * @since 1.8
3843 +     */
3844 +    public long reduceKeysToLong(long parallelismThreshold,
3845 +                                 ObjectToLong<? super K> transformer,
3846 +                                 long basis,
3847 +                                 LongByLongToLong reducer) {
3848 +        if (transformer == null || reducer == null)
3849 +            throw new NullPointerException();
3850 +        return new MapReduceKeysToLongTask<K,V>
3851 +            (null, batchFor(parallelismThreshold), 0, 0, table,
3852 +             null, transformer, basis, reducer).invoke();
3853 +    }
3854 +
3855 +    /**
3856 +     * Returns the result of accumulating the given transformation
3857 +     * of all keys using the given reducer to combine values, and
3858 +     * the given basis as an identity value.
3859 +     *
3860 +     * @param parallelismThreshold the (estimated) number of elements
3861 +     * needed for this operation to be executed in parallel
3862 +     * @param transformer a function returning the transformation
3863 +     * for an element
3864 +     * @param basis the identity (initial default value) for the reduction
3865 +     * @param reducer a commutative associative combining function
3866 +     * @return the result of accumulating the given transformation
3867 +     * of all keys
3868 +     * @since 1.8
3869 +     */
3870 +    public int reduceKeysToInt(long parallelismThreshold,
3871 +                               ObjectToInt<? super K> transformer,
3872 +                               int basis,
3873 +                               IntByIntToInt reducer) {
3874 +        if (transformer == null || reducer == null)
3875 +            throw new NullPointerException();
3876 +        return new MapReduceKeysToIntTask<K,V>
3877 +            (null, batchFor(parallelismThreshold), 0, 0, table,
3878 +             null, transformer, basis, reducer).invoke();
3879 +    }
3880 +
3881 +    /**
3882 +     * Performs the given action for each value.
3883 +     *
3884 +     * @param parallelismThreshold the (estimated) number of elements
3885 +     * needed for this operation to be executed in parallel
3886 +     * @param action the action
3887 +     * @since 1.8
3888 +     */
3889 +    public void forEachValue(long parallelismThreshold,
3890 +                             Action<? super V> action) {
3891 +        if (action == null)
3892 +            throw new NullPointerException();
3893 +        new ForEachValueTask<K,V>
3894 +            (null, batchFor(parallelismThreshold), 0, 0, table,
3895 +             action).invoke();
3896 +    }
3897 +
3898 +    /**
3899 +     * Performs the given action for each non-null transformation
3900 +     * of each value.
3901 +     *
3902 +     * @param parallelismThreshold the (estimated) number of elements
3903 +     * needed for this operation to be executed in parallel
3904 +     * @param transformer a function returning the transformation
3905 +     * for an element, or null if there is no transformation (in
3906 +     * which case the action is not applied)
3907 +     * @param action the action
3908 +     * @since 1.8
3909 +     */
3910 +    public <U> void forEachValue(long parallelismThreshold,
3911 +                                 Fun<? super V, ? extends U> transformer,
3912 +                                 Action<? super U> action) {
3913 +        if (transformer == null || action == null)
3914 +            throw new NullPointerException();
3915 +        new ForEachTransformedValueTask<K,V,U>
3916 +            (null, batchFor(parallelismThreshold), 0, 0, table,
3917 +             transformer, action).invoke();
3918 +    }
3919 +
3920 +    /**
3921 +     * Returns a non-null result from applying the given search
3922 +     * function on each value, or null if none.  Upon success,
3923 +     * further element processing is suppressed and the results of
3924 +     * any other parallel invocations of the search function are
3925 +     * ignored.
3926 +     *
3927 +     * @param parallelismThreshold the (estimated) number of elements
3928 +     * needed for this operation to be executed in parallel
3929 +     * @param searchFunction a function returning a non-null
3930 +     * result on success, else null
3931 +     * @return a non-null result from applying the given search
3932 +     * function on each value, or null if none
3933 +     * @since 1.8
3934 +     */
3935 +    public <U> U searchValues(long parallelismThreshold,
3936 +                              Fun<? super V, ? extends U> searchFunction) {
3937 +        if (searchFunction == null) throw new NullPointerException();
3938 +        return new SearchValuesTask<K,V,U>
3939 +            (null, batchFor(parallelismThreshold), 0, 0, table,
3940 +             searchFunction, new AtomicReference<U>()).invoke();
3941 +    }
3942 +
3943 +    /**
3944 +     * Returns the result of accumulating all values using the
3945 +     * given reducer to combine values, or null if none.
3946 +     *
3947 +     * @param parallelismThreshold the (estimated) number of elements
3948 +     * needed for this operation to be executed in parallel
3949 +     * @param reducer a commutative associative combining function
3950 +     * @return the result of accumulating all values
3951 +     * @since 1.8
3952 +     */
3953 +    public V reduceValues(long parallelismThreshold,
3954 +                          BiFun<? super V, ? super V, ? extends V> reducer) {
3955 +        if (reducer == null) throw new NullPointerException();
3956 +        return new ReduceValuesTask<K,V>
3957 +            (null, batchFor(parallelismThreshold), 0, 0, table,
3958 +             null, reducer).invoke();
3959 +    }
3960 +
3961 +    /**
3962 +     * Returns the result of accumulating the given transformation
3963 +     * of all values using the given reducer to combine values, or
3964 +     * null if none.
3965 +     *
3966 +     * @param parallelismThreshold the (estimated) number of elements
3967 +     * needed for this operation to be executed in parallel
3968 +     * @param transformer a function returning the transformation
3969 +     * for an element, or null if there is no transformation (in
3970 +     * which case it is not combined)
3971 +     * @param reducer a commutative associative combining function
3972 +     * @return the result of accumulating the given transformation
3973 +     * of all values
3974 +     * @since 1.8
3975 +     */
3976 +    public <U> U reduceValues(long parallelismThreshold,
3977 +                              Fun<? super V, ? extends U> transformer,
3978 +                              BiFun<? super U, ? super U, ? extends U> reducer) {
3979 +        if (transformer == null || reducer == null)
3980 +            throw new NullPointerException();
3981 +        return new MapReduceValuesTask<K,V,U>
3982 +            (null, batchFor(parallelismThreshold), 0, 0, table,
3983 +             null, transformer, reducer).invoke();
3984 +    }
3985 +
3986 +    /**
3987 +     * Returns the result of accumulating the given transformation
3988 +     * of all values using the given reducer to combine values,
3989 +     * and the given basis as an identity value.
3990 +     *
3991 +     * @param parallelismThreshold the (estimated) number of elements
3992 +     * needed for this operation to be executed in parallel
3993 +     * @param transformer a function returning the transformation
3994 +     * for an element
3995 +     * @param basis the identity (initial default value) for the reduction
3996 +     * @param reducer a commutative associative combining function
3997 +     * @return the result of accumulating the given transformation
3998 +     * of all values
3999 +     * @since 1.8
4000 +     */
4001 +    public double reduceValuesToDouble(long parallelismThreshold,
4002 +                                       ObjectToDouble<? super V> transformer,
4003 +                                       double basis,
4004 +                                       DoubleByDoubleToDouble reducer) {
4005 +        if (transformer == null || reducer == null)
4006 +            throw new NullPointerException();
4007 +        return new MapReduceValuesToDoubleTask<K,V>
4008 +            (null, batchFor(parallelismThreshold), 0, 0, table,
4009 +             null, transformer, basis, reducer).invoke();
4010 +    }
4011 +
4012 +    /**
4013 +     * Returns the result of accumulating the given transformation
4014 +     * of all values using the given reducer to combine values,
4015 +     * and the given basis as an identity value.
4016 +     *
4017 +     * @param parallelismThreshold the (estimated) number of elements
4018 +     * needed for this operation to be executed in parallel
4019 +     * @param transformer a function returning the transformation
4020 +     * for an element
4021 +     * @param basis the identity (initial default value) for the reduction
4022 +     * @param reducer a commutative associative combining function
4023 +     * @return the result of accumulating the given transformation
4024 +     * of all values
4025 +     * @since 1.8
4026 +     */
4027 +    public long reduceValuesToLong(long parallelismThreshold,
4028 +                                   ObjectToLong<? super V> transformer,
4029 +                                   long basis,
4030 +                                   LongByLongToLong reducer) {
4031 +        if (transformer == null || reducer == null)
4032 +            throw new NullPointerException();
4033 +        return new MapReduceValuesToLongTask<K,V>
4034 +            (null, batchFor(parallelismThreshold), 0, 0, table,
4035 +             null, transformer, basis, reducer).invoke();
4036 +    }
4037 +
4038 +    /**
4039 +     * Returns the result of accumulating the given transformation
4040 +     * of all values using the given reducer to combine values,
4041 +     * and the given basis as an identity value.
4042 +     *
4043 +     * @param parallelismThreshold the (estimated) number of elements
4044 +     * needed for this operation to be executed in parallel
4045 +     * @param transformer a function returning the transformation
4046 +     * for an element
4047 +     * @param basis the identity (initial default value) for the reduction
4048 +     * @param reducer a commutative associative combining function
4049 +     * @return the result of accumulating the given transformation
4050 +     * of all values
4051 +     * @since 1.8
4052 +     */
4053 +    public int reduceValuesToInt(long parallelismThreshold,
4054 +                                 ObjectToInt<? super V> transformer,
4055 +                                 int basis,
4056 +                                 IntByIntToInt reducer) {
4057 +        if (transformer == null || reducer == null)
4058 +            throw new NullPointerException();
4059 +        return new MapReduceValuesToIntTask<K,V>
4060 +            (null, batchFor(parallelismThreshold), 0, 0, table,
4061 +             null, transformer, basis, reducer).invoke();
4062 +    }
4063 +
4064 +    /**
4065 +     * Performs the given action for each entry.
4066 +     *
4067 +     * @param parallelismThreshold the (estimated) number of elements
4068 +     * needed for this operation to be executed in parallel
4069 +     * @param action the action
4070 +     * @since 1.8
4071 +     */
4072 +    public void forEachEntry(long parallelismThreshold,
4073 +                             Action<? super Map.Entry<K,V>> action) {
4074 +        if (action == null) throw new NullPointerException();
4075 +        new ForEachEntryTask<K,V>(null, batchFor(parallelismThreshold), 0, 0, table,
4076 +                                  action).invoke();
4077 +    }
4078 +
4079 +    /**
4080 +     * Performs the given action for each non-null transformation
4081 +     * of each entry.
4082 +     *
4083 +     * @param parallelismThreshold the (estimated) number of elements
4084 +     * needed for this operation to be executed in parallel
4085 +     * @param transformer a function returning the transformation
4086 +     * for an element, or null if there is no transformation (in
4087 +     * which case the action is not applied)
4088 +     * @param action the action
4089 +     * @since 1.8
4090 +     */
4091 +    public <U> void forEachEntry(long parallelismThreshold,
4092 +                                 Fun<Map.Entry<K,V>, ? extends U> transformer,
4093 +                                 Action<? super U> action) {
4094 +        if (transformer == null || action == null)
4095 +            throw new NullPointerException();
4096 +        new ForEachTransformedEntryTask<K,V,U>
4097 +            (null, batchFor(parallelismThreshold), 0, 0, table,
4098 +             transformer, action).invoke();
4099 +    }
4100 +
4101 +    /**
4102 +     * Returns a non-null result from applying the given search
4103 +     * function on each entry, or null if none.  Upon success,
4104 +     * further element processing is suppressed and the results of
4105 +     * any other parallel invocations of the search function are
4106 +     * ignored.
4107 +     *
4108 +     * @param parallelismThreshold the (estimated) number of elements
4109 +     * needed for this operation to be executed in parallel
4110 +     * @param searchFunction a function returning a non-null
4111 +     * result on success, else null
4112 +     * @return a non-null result from applying the given search
4113 +     * function on each entry, or null if none
4114 +     * @since 1.8
4115 +     */
4116 +    public <U> U searchEntries(long parallelismThreshold,
4117 +                               Fun<Map.Entry<K,V>, ? extends U> searchFunction) {
4118 +        if (searchFunction == null) throw new NullPointerException();
4119 +        return new SearchEntriesTask<K,V,U>
4120 +            (null, batchFor(parallelismThreshold), 0, 0, table,
4121 +             searchFunction, new AtomicReference<U>()).invoke();
4122 +    }
4123 +
4124 +    /**
4125 +     * Returns the result of accumulating all entries using the
4126 +     * given reducer to combine values, or null if none.
4127 +     *
4128 +     * @param parallelismThreshold the (estimated) number of elements
4129 +     * needed for this operation to be executed in parallel
4130 +     * @param reducer a commutative associative combining function
4131 +     * @return the result of accumulating all entries
4132 +     * @since 1.8
4133 +     */
4134 +    public Map.Entry<K,V> reduceEntries(long parallelismThreshold,
4135 +                                        BiFun<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer) {
4136 +        if (reducer == null) throw new NullPointerException();
4137 +        return new ReduceEntriesTask<K,V>
4138 +            (null, batchFor(parallelismThreshold), 0, 0, table,
4139 +             null, reducer).invoke();
4140 +    }
4141 +
4142 +    /**
4143 +     * Returns the result of accumulating the given transformation
4144 +     * of all entries using the given reducer to combine values,
4145 +     * or null if none.
4146 +     *
4147 +     * @param parallelismThreshold the (estimated) number of elements
4148 +     * needed for this operation to be executed in parallel
4149 +     * @param transformer a function returning the transformation
4150 +     * for an element, or null if there is no transformation (in
4151 +     * which case it is not combined)
4152 +     * @param reducer a commutative associative combining function
4153 +     * @return the result of accumulating the given transformation
4154 +     * of all entries
4155 +     * @since 1.8
4156 +     */
4157 +    public <U> U reduceEntries(long parallelismThreshold,
4158 +                               Fun<Map.Entry<K,V>, ? extends U> transformer,
4159 +                               BiFun<? super U, ? super U, ? extends U> reducer) {
4160 +        if (transformer == null || reducer == null)
4161 +            throw new NullPointerException();
4162 +        return new MapReduceEntriesTask<K,V,U>
4163 +            (null, batchFor(parallelismThreshold), 0, 0, table,
4164 +             null, transformer, reducer).invoke();
4165 +    }
4166 +
4167 +    /**
4168 +     * Returns the result of accumulating the given transformation
4169 +     * of all entries using the given reducer to combine values,
4170 +     * and the given basis as an identity value.
4171 +     *
4172 +     * @param parallelismThreshold the (estimated) number of elements
4173 +     * needed for this operation to be executed in parallel
4174 +     * @param transformer a function returning the transformation
4175 +     * for an element
4176 +     * @param basis the identity (initial default value) for the reduction
4177 +     * @param reducer a commutative associative combining function
4178 +     * @return the result of accumulating the given transformation
4179 +     * of all entries
4180 +     * @since 1.8
4181 +     */
4182 +    public double reduceEntriesToDouble(long parallelismThreshold,
4183 +                                        ObjectToDouble<Map.Entry<K,V>> transformer,
4184 +                                        double basis,
4185 +                                        DoubleByDoubleToDouble reducer) {
4186 +        if (transformer == null || reducer == null)
4187 +            throw new NullPointerException();
4188 +        return new MapReduceEntriesToDoubleTask<K,V>
4189 +            (null, batchFor(parallelismThreshold), 0, 0, table,
4190 +             null, transformer, basis, reducer).invoke();
4191 +    }
4192 +
4193 +    /**
4194 +     * Returns the result of accumulating the given transformation
4195 +     * of all entries using the given reducer to combine values,
4196 +     * and the given basis as an identity value.
4197 +     *
4198 +     * @param parallelismThreshold the (estimated) number of elements
4199 +     * needed for this operation to be executed in parallel
4200 +     * @param transformer a function returning the transformation
4201 +     * for an element
4202 +     * @param basis the identity (initial default value) for the reduction
4203 +     * @param reducer a commutative associative combining function
4204 +     * @return the result of accumulating the given transformation
4205 +     * of all entries
4206 +     * @since 1.8
4207 +     */
4208 +    public long reduceEntriesToLong(long parallelismThreshold,
4209 +                                    ObjectToLong<Map.Entry<K,V>> transformer,
4210 +                                    long basis,
4211 +                                    LongByLongToLong reducer) {
4212 +        if (transformer == null || reducer == null)
4213 +            throw new NullPointerException();
4214 +        return new MapReduceEntriesToLongTask<K,V>
4215 +            (null, batchFor(parallelismThreshold), 0, 0, table,
4216 +             null, transformer, basis, reducer).invoke();
4217 +    }
4218 +
4219 +    /**
4220 +     * Returns the result of accumulating the given transformation
4221 +     * of all entries using the given reducer to combine values,
4222 +     * and the given basis as an identity value.
4223 +     *
4224 +     * @param parallelismThreshold the (estimated) number of elements
4225 +     * needed for this operation to be executed in parallel
4226 +     * @param transformer a function returning the transformation
4227 +     * for an element
4228 +     * @param basis the identity (initial default value) for the reduction
4229 +     * @param reducer a commutative associative combining function
4230 +     * @return the result of accumulating the given transformation
4231 +     * of all entries
4232 +     * @since 1.8
4233 +     */
4234 +    public int reduceEntriesToInt(long parallelismThreshold,
4235 +                                  ObjectToInt<Map.Entry<K,V>> transformer,
4236 +                                  int basis,
4237 +                                  IntByIntToInt reducer) {
4238 +        if (transformer == null || reducer == null)
4239 +            throw new NullPointerException();
4240 +        return new MapReduceEntriesToIntTask<K,V>
4241 +            (null, batchFor(parallelismThreshold), 0, 0, table,
4242 +             null, transformer, basis, reducer).invoke();
4243 +    }
4244 +
4245 +
4246 +    /* ----------------Views -------------- */
4247 +
4248 +    /**
4249 +     * Base class for views.
4250 +     */
4251 +    abstract static class CollectionView<K,V,E>
4252 +        implements Collection<E>, java.io.Serializable {
4253 +        private static final long serialVersionUID = 7249069246763182397L;
4254 +        final ConcurrentHashMapV8<K,V> map;
4255 +        CollectionView(ConcurrentHashMapV8<K,V> map)  { this.map = map; }
4256 +
4257          /**
4258 <         * Returns a task that when invoked, returns a non-null result
4341 <         * from applying the given search function on each key, or
4342 <         * null if none.  Further element processing is suppressed
4343 <         * upon success. However, this method does not return until
4344 <         * other in-progress parallel invocations of the search
4345 <         * function also complete.
4258 >         * Returns the map backing this view.
4259           *
4260 <         * @param map the map
4348 <         * @param searchFunction a function returning a non-null
4349 <         * result on success, else null
4350 <         * @return the task
4260 >         * @return the map backing this view
4261           */
4262 <        public static <K,V,U> ForkJoinTask<U> searchKeys
4353 <            (ConcurrentHashMapV8<K,V> map,
4354 <             Fun<? super K, ? extends U> searchFunction) {
4355 <            if (searchFunction == null) throw new NullPointerException();
4356 <            return new SearchKeysTask<K,V,U>
4357 <                (map, searchFunction,
4358 <                 new AtomicReference<U>());
4359 <        }
4262 >        public ConcurrentHashMapV8<K,V> getMap() { return map; }
4263  
4264          /**
4265 <         * Returns a task that when invoked, returns the result of
4266 <         * accumulating all keys using the given reducer to combine
4364 <         * values, or null if none.
4365 <         *
4366 <         * @param map the map
4367 <         * @param reducer a commutative associative combining function
4368 <         * @return the task
4265 >         * Removes all of the elements from this view, by removing all
4266 >         * the mappings from the map backing this view.
4267           */
4268 <        public static <K,V> ForkJoinTask<K> reduceKeys
4269 <            (ConcurrentHashMapV8<K,V> map,
4270 <             BiFun<? super K, ? super K, ? extends K> reducer) {
4271 <            if (reducer == null) throw new NullPointerException();
4272 <            return new ReduceKeysTask<K,V>
4273 <                (map, reducer);
4376 <        }
4268 >        public final void clear()      { map.clear(); }
4269 >        public final int size()        { return map.size(); }
4270 >        public final boolean isEmpty() { return map.isEmpty(); }
4271 >
4272 >        // implementations below rely on concrete classes supplying these
4273 >        // abstract methods
4274          /**
4275 <         * Returns a task that when invoked, returns the result of
4276 <         * accumulating the given transformation of all keys using the given
4277 <         * reducer to combine values, or null if none.
4278 <         *
4279 <         * @param map the map
4280 <         * @param transformer a function returning the transformation
4281 <         * for an element, or null of there is no transformation (in
4282 <         * which case it is not combined).
4283 <         * @param reducer a commutative associative combining function
4284 <         * @return the task
4285 <         */
4286 <        public static <K,V,U> ForkJoinTask<U> reduceKeys
4287 <            (ConcurrentHashMapV8<K,V> map,
4288 <             Fun<? super K, ? extends U> transformer,
4289 <             BiFun<? super U, ? super U, ? extends U> reducer) {
4290 <            if (transformer == null || reducer == null)
4291 <                throw new NullPointerException();
4292 <            return new MapReduceKeysTask<K,V,U>
4293 <                (map, transformer, reducer);
4275 >         * Returns a "weakly consistent" iterator that will never
4276 >         * throw {@link ConcurrentModificationException}, and
4277 >         * guarantees to traverse elements as they existed upon
4278 >         * construction of the iterator, and may (but is not
4279 >         * guaranteed to) reflect any modifications subsequent to
4280 >         * construction.
4281 >         */
4282 >        public abstract Iterator<E> iterator();
4283 >        public abstract boolean contains(Object o);
4284 >        public abstract boolean remove(Object o);
4285 >
4286 >        private static final String oomeMsg = "Required array size too large";
4287 >
4288 >        public final Object[] toArray() {
4289 >            long sz = map.mappingCount();
4290 >            if (sz > MAX_ARRAY_SIZE)
4291 >                throw new OutOfMemoryError(oomeMsg);
4292 >            int n = (int)sz;
4293 >            Object[] r = new Object[n];
4294 >            int i = 0;
4295 >            for (E e : this) {
4296 >                if (i == n) {
4297 >                    if (n >= MAX_ARRAY_SIZE)
4298 >                        throw new OutOfMemoryError(oomeMsg);
4299 >                    if (n >= MAX_ARRAY_SIZE - (MAX_ARRAY_SIZE >>> 1) - 1)
4300 >                        n = MAX_ARRAY_SIZE;
4301 >                    else
4302 >                        n += (n >>> 1) + 1;
4303 >                    r = Arrays.copyOf(r, n);
4304 >                }
4305 >                r[i++] = e;
4306 >            }
4307 >            return (i == n) ? r : Arrays.copyOf(r, i);
4308          }
4309  
4310 <        /**
4311 <         * Returns a task that when invoked, returns the result of
4312 <         * accumulating the given transformation of all keys using the given
4313 <         * reducer to combine values, and the given basis as an
4314 <         * identity value.
4315 <         *
4316 <         * @param map the map
4317 <         * @param transformer a function returning the transformation
4318 <         * for an element
4319 <         * @param basis the identity (initial default value) for the reduction
4320 <         * @param reducer a commutative associative combining function
4321 <         * @return the task
4322 <         */
4323 <        public static <K,V> ForkJoinTask<Double> reduceKeysToDouble
4324 <            (ConcurrentHashMapV8<K,V> map,
4325 <             ObjectToDouble<? super K> transformer,
4326 <             double basis,
4327 <             DoubleByDoubleToDouble reducer) {
4328 <            if (transformer == null || reducer == null)
4329 <                throw new NullPointerException();
4330 <            return new MapReduceKeysToDoubleTask<K,V>
4331 <                (map, transformer, basis, reducer);
4310 >        @SuppressWarnings("unchecked")
4311 >        public final <T> T[] toArray(T[] a) {
4312 >            long sz = map.mappingCount();
4313 >            if (sz > MAX_ARRAY_SIZE)
4314 >                throw new OutOfMemoryError(oomeMsg);
4315 >            int m = (int)sz;
4316 >            T[] r = (a.length >= m) ? a :
4317 >                (T[])java.lang.reflect.Array
4318 >                .newInstance(a.getClass().getComponentType(), m);
4319 >            int n = r.length;
4320 >            int i = 0;
4321 >            for (E e : this) {
4322 >                if (i == n) {
4323 >                    if (n >= MAX_ARRAY_SIZE)
4324 >                        throw new OutOfMemoryError(oomeMsg);
4325 >                    if (n >= MAX_ARRAY_SIZE - (MAX_ARRAY_SIZE >>> 1) - 1)
4326 >                        n = MAX_ARRAY_SIZE;
4327 >                    else
4328 >                        n += (n >>> 1) + 1;
4329 >                    r = Arrays.copyOf(r, n);
4330 >                }
4331 >                r[i++] = (T)e;
4332 >            }
4333 >            if (a == r && i < n) {
4334 >                r[i] = null; // null-terminate
4335 >                return r;
4336 >            }
4337 >            return (i == n) ? r : Arrays.copyOf(r, i);
4338          }
4339  
4340          /**
4341 <         * Returns a task that when invoked, returns the result of
4342 <         * accumulating the given transformation of all keys using the given
4343 <         * reducer to combine values, and the given basis as an
4344 <         * identity value.
4341 >         * Returns a string representation of this collection.
4342 >         * The string representation consists of the string representations
4343 >         * of the collection's elements in the order they are returned by
4344 >         * its iterator, enclosed in square brackets ({@code "[]"}).
4345 >         * Adjacent elements are separated by the characters {@code ", "}
4346 >         * (comma and space).  Elements are converted to strings as by
4347 >         * {@link String#valueOf(Object)}.
4348           *
4349 <         * @param map the map
4430 <         * @param transformer a function returning the transformation
4431 <         * for an element
4432 <         * @param basis the identity (initial default value) for the reduction
4433 <         * @param reducer a commutative associative combining function
4434 <         * @return the task
4349 >         * @return a string representation of this collection
4350           */
4351 <        public static <K,V> ForkJoinTask<Long> reduceKeysToLong
4352 <            (ConcurrentHashMapV8<K,V> map,
4353 <             ObjectToLong<? super K> transformer,
4354 <             long basis,
4355 <             LongByLongToLong reducer) {
4356 <            if (transformer == null || reducer == null)
4357 <                throw new NullPointerException();
4358 <            return new MapReduceKeysToLongTask<K,V>
4359 <                (map, transformer, basis, reducer);
4351 >        public final String toString() {
4352 >            StringBuilder sb = new StringBuilder();
4353 >            sb.append('[');
4354 >            Iterator<E> it = iterator();
4355 >            if (it.hasNext()) {
4356 >                for (;;) {
4357 >                    Object e = it.next();
4358 >                    sb.append(e == this ? "(this Collection)" : e);
4359 >                    if (!it.hasNext())
4360 >                        break;
4361 >                    sb.append(',').append(' ');
4362 >                }
4363 >            }
4364 >            return sb.append(']').toString();
4365          }
4366  
4367 <        /**
4368 <         * Returns a task that when invoked, returns the result of
4369 <         * accumulating the given transformation of all keys using the given
4370 <         * reducer to combine values, and the given basis as an
4371 <         * identity value.
4372 <         *
4373 <         * @param map the map
4374 <         * @param transformer a function returning the transformation
4455 <         * for an element
4456 <         * @param basis the identity (initial default value) for the reduction
4457 <         * @param reducer a commutative associative combining function
4458 <         * @return the task
4459 <         */
4460 <        public static <K,V> ForkJoinTask<Integer> reduceKeysToInt
4461 <            (ConcurrentHashMapV8<K,V> map,
4462 <             ObjectToInt<? super K> transformer,
4463 <             int basis,
4464 <             IntByIntToInt reducer) {
4465 <            if (transformer == null || reducer == null)
4466 <                throw new NullPointerException();
4467 <            return new MapReduceKeysToIntTask<K,V>
4468 <                (map, transformer, basis, reducer);
4367 >        public final boolean containsAll(Collection<?> c) {
4368 >            if (c != this) {
4369 >                for (Object e : c) {
4370 >                    if (e == null || !contains(e))
4371 >                        return false;
4372 >                }
4373 >            }
4374 >            return true;
4375          }
4376  
4377 <        /**
4378 <         * Returns a task that when invoked, performs the given action
4379 <         * for each value.
4380 <         *
4381 <         * @param map the map
4382 <         * @param action the action
4383 <         */
4384 <        public static <K,V> ForkJoinTask<Void> forEachValue
4385 <            (ConcurrentHashMapV8<K,V> map,
4480 <             Action<V> action) {
4481 <            if (action == null) throw new NullPointerException();
4482 <            return new ForEachValueTask<K,V>(map, action);
4377 >        public final boolean removeAll(Collection<?> c) {
4378 >            boolean modified = false;
4379 >            for (Iterator<E> it = iterator(); it.hasNext();) {
4380 >                if (c.contains(it.next())) {
4381 >                    it.remove();
4382 >                    modified = true;
4383 >                }
4384 >            }
4385 >            return modified;
4386          }
4387  
4388 <        /**
4389 <         * Returns a task that when invoked, performs the given action
4390 <         * for each non-null transformation of each value.
4391 <         *
4392 <         * @param map the map
4393 <         * @param transformer a function returning the transformation
4394 <         * for an element, or null of there is no transformation (in
4395 <         * which case the action is not applied).
4396 <         * @param action the action
4494 <         */
4495 <        public static <K,V,U> ForkJoinTask<Void> forEachValue
4496 <            (ConcurrentHashMapV8<K,V> map,
4497 <             Fun<? super V, ? extends U> transformer,
4498 <             Action<U> action) {
4499 <            if (transformer == null || action == null)
4500 <                throw new NullPointerException();
4501 <            return new ForEachTransformedValueTask<K,V,U>
4502 <                (map, transformer, action);
4388 >        public final boolean retainAll(Collection<?> c) {
4389 >            boolean modified = false;
4390 >            for (Iterator<E> it = iterator(); it.hasNext();) {
4391 >                if (!c.contains(it.next())) {
4392 >                    it.remove();
4393 >                    modified = true;
4394 >                }
4395 >            }
4396 >            return modified;
4397          }
4398  
4399 <        /**
4400 <         * Returns a task that when invoked, returns a non-null result
4401 <         * from applying the given search function on each value, or
4402 <         * null if none.  Further element processing is suppressed
4403 <         * upon success. However, this method does not return until
4404 <         * other in-progress parallel invocations of the search
4405 <         * function also complete.
4406 <         *
4407 <         * @param map the map
4408 <         * @param searchFunction a function returning a non-null
4409 <         * result on success, else null
4410 <         * @return the task
4411 <         *
4412 <         */
4413 <        public static <K,V,U> ForkJoinTask<U> searchValues
4414 <            (ConcurrentHashMapV8<K,V> map,
4415 <             Fun<? super V, ? extends U> searchFunction) {
4416 <            if (searchFunction == null) throw new NullPointerException();
4417 <            return new SearchValuesTask<K,V,U>
4418 <                (map, searchFunction,
4525 <                 new AtomicReference<U>());
4399 >    }
4400 >
4401 >    /**
4402 >     * A view of a ConcurrentHashMapV8 as a {@link Set} of keys, in
4403 >     * which additions may optionally be enabled by mapping to a
4404 >     * common value.  This class cannot be directly instantiated.
4405 >     * See {@link #keySet() keySet()},
4406 >     * {@link #keySet(Object) keySet(V)},
4407 >     * {@link #newKeySet() newKeySet()},
4408 >     * {@link #newKeySet(int) newKeySet(int)}.
4409 >     *
4410 >     * @since 1.8
4411 >     */
4412 >    public static class KeySetView<K,V> extends CollectionView<K,V,K>
4413 >        implements Set<K>, java.io.Serializable {
4414 >        private static final long serialVersionUID = 7249069246763182397L;
4415 >        private final V value;
4416 >        KeySetView(ConcurrentHashMapV8<K,V> map, V value) {  // non-public
4417 >            super(map);
4418 >            this.value = value;
4419          }
4420  
4421          /**
4422 <         * Returns a task that when invoked, returns the result of
4423 <         * accumulating all values using the given reducer to combine
4531 <         * values, or null if none.
4422 >         * Returns the default mapped value for additions,
4423 >         * or {@code null} if additions are not supported.
4424           *
4425 <         * @param map the map
4426 <         * @param reducer a commutative associative combining function
4535 <         * @return the task
4425 >         * @return the default mapped value for additions, or {@code null}
4426 >         * if not supported
4427           */
4428 <        public static <K,V> ForkJoinTask<V> reduceValues
4538 <            (ConcurrentHashMapV8<K,V> map,
4539 <             BiFun<? super V, ? super V, ? extends V> reducer) {
4540 <            if (reducer == null) throw new NullPointerException();
4541 <            return new ReduceValuesTask<K,V>
4542 <                (map, reducer);
4543 <        }
4428 >        public V getMappedValue() { return value; }
4429  
4430          /**
4431 <         * Returns a task that when invoked, returns the result of
4432 <         * accumulating the given transformation of all values using the
4548 <         * given reducer to combine values, or null if none.
4549 <         *
4550 <         * @param map the map
4551 <         * @param transformer a function returning the transformation
4552 <         * for an element, or null of there is no transformation (in
4553 <         * which case it is not combined).
4554 <         * @param reducer a commutative associative combining function
4555 <         * @return the task
4431 >         * {@inheritDoc}
4432 >         * @throws NullPointerException if the specified key is null
4433           */
4434 <        public static <K,V,U> ForkJoinTask<U> reduceValues
4558 <            (ConcurrentHashMapV8<K,V> map,
4559 <             Fun<? super V, ? extends U> transformer,
4560 <             BiFun<? super U, ? super U, ? extends U> reducer) {
4561 <            if (transformer == null || reducer == null)
4562 <                throw new NullPointerException();
4563 <            return new MapReduceValuesTask<K,V,U>
4564 <                (map, transformer, reducer);
4565 <        }
4434 >        public boolean contains(Object o) { return map.containsKey(o); }
4435  
4436          /**
4437 <         * Returns a task that when invoked, returns the result of
4438 <         * accumulating the given transformation of all values using the
4439 <         * given reducer to combine values, and the given basis as an
4571 <         * identity value.
4437 >         * Removes the key from this map view, by removing the key (and its
4438 >         * corresponding value) from the backing map.  This method does
4439 >         * nothing if the key is not in the map.
4440           *
4441 <         * @param map the map
4442 <         * @param transformer a function returning the transformation
4443 <         * for an element
4576 <         * @param basis the identity (initial default value) for the reduction
4577 <         * @param reducer a commutative associative combining function
4578 <         * @return the task
4441 >         * @param  o the key to be removed from the backing map
4442 >         * @return {@code true} if the backing map contained the specified key
4443 >         * @throws NullPointerException if the specified key is null
4444           */
4445 <        public static <K,V> ForkJoinTask<Double> reduceValuesToDouble
4581 <            (ConcurrentHashMapV8<K,V> map,
4582 <             ObjectToDouble<? super V> transformer,
4583 <             double basis,
4584 <             DoubleByDoubleToDouble reducer) {
4585 <            if (transformer == null || reducer == null)
4586 <                throw new NullPointerException();
4587 <            return new MapReduceValuesToDoubleTask<K,V>
4588 <                (map, transformer, basis, reducer);
4589 <        }
4445 >        public boolean remove(Object o) { return map.remove(o) != null; }
4446  
4447          /**
4448 <         * Returns a task that when invoked, returns the result of
4593 <         * accumulating the given transformation of all values using the
4594 <         * given reducer to combine values, and the given basis as an
4595 <         * identity value.
4596 <         *
4597 <         * @param map the map
4598 <         * @param transformer a function returning the transformation
4599 <         * for an element
4600 <         * @param basis the identity (initial default value) for the reduction
4601 <         * @param reducer a commutative associative combining function
4602 <         * @return the task
4448 >         * @return an iterator over the keys of the backing map
4449           */
4450 <        public static <K,V> ForkJoinTask<Long> reduceValuesToLong
4451 <            (ConcurrentHashMapV8<K,V> map,
4452 <             ObjectToLong<? super V> transformer,
4453 <             long basis,
4454 <             LongByLongToLong reducer) {
4609 <            if (transformer == null || reducer == null)
4610 <                throw new NullPointerException();
4611 <            return new MapReduceValuesToLongTask<K,V>
4612 <                (map, transformer, basis, reducer);
4450 >        public Iterator<K> iterator() {
4451 >            Node<K,V>[] t;
4452 >            ConcurrentHashMapV8<K,V> m = map;
4453 >            int f = (t = m.table) == null ? 0 : t.length;
4454 >            return new KeyIterator<K,V>(t, f, 0, f, m);
4455          }
4456  
4457          /**
4458 <         * Returns a task that when invoked, returns the result of
4459 <         * accumulating the given transformation of all values using the
4618 <         * given reducer to combine values, and the given basis as an
4619 <         * identity value.
4458 >         * Adds the specified key to this set view by mapping the key to
4459 >         * the default mapped value in the backing map, if defined.
4460           *
4461 <         * @param map the map
4462 <         * @param transformer a function returning the transformation
4463 <         * for an element
4464 <         * @param basis the identity (initial default value) for the reduction
4465 <         * @param reducer a commutative associative combining function
4626 <         * @return the task
4461 >         * @param e key to be added
4462 >         * @return {@code true} if this set changed as a result of the call
4463 >         * @throws NullPointerException if the specified key is null
4464 >         * @throws UnsupportedOperationException if no default mapped value
4465 >         * for additions was provided
4466           */
4467 <        public static <K,V> ForkJoinTask<Integer> reduceValuesToInt
4468 <            (ConcurrentHashMapV8<K,V> map,
4469 <             ObjectToInt<? super V> transformer,
4470 <             int basis,
4471 <             IntByIntToInt reducer) {
4633 <            if (transformer == null || reducer == null)
4634 <                throw new NullPointerException();
4635 <            return new MapReduceValuesToIntTask<K,V>
4636 <                (map, transformer, basis, reducer);
4467 >        public boolean add(K e) {
4468 >            V v;
4469 >            if ((v = value) == null)
4470 >                throw new UnsupportedOperationException();
4471 >            return map.putVal(e, v, true) == null;
4472          }
4473  
4474          /**
4475 <         * Returns a task that when invoked, perform the given action
4476 <         * for each entry.
4475 >         * Adds all of the elements in the specified collection to this set,
4476 >         * as if by calling {@link #add} on each one.
4477           *
4478 <         * @param map the map
4479 <         * @param action the action
4478 >         * @param c the elements to be inserted into this set
4479 >         * @return {@code true} if this set changed as a result of the call
4480 >         * @throws NullPointerException if the collection or any of its
4481 >         * elements are {@code null}
4482 >         * @throws UnsupportedOperationException if no default mapped value
4483 >         * for additions was provided
4484           */
4485 <        public static <K,V> ForkJoinTask<Void> forEachEntry
4486 <            (ConcurrentHashMapV8<K,V> map,
4487 <             Action<Map.Entry<K,V>> action) {
4488 <            if (action == null) throw new NullPointerException();
4489 <            return new ForEachEntryTask<K,V>(map, action);
4485 >        public boolean addAll(Collection<? extends K> c) {
4486 >            boolean added = false;
4487 >            V v;
4488 >            if ((v = value) == null)
4489 >                throw new UnsupportedOperationException();
4490 >            for (K e : c) {
4491 >                if (map.putVal(e, v, true) == null)
4492 >                    added = true;
4493 >            }
4494 >            return added;
4495          }
4496  
4497 <        /**
4498 <         * Returns a task that when invoked, perform the given action
4499 <         * for each non-null transformation of each entry.
4500 <         *
4501 <         * @param map the map
4658 <         * @param transformer a function returning the transformation
4659 <         * for an element, or null of there is no transformation (in
4660 <         * which case the action is not applied).
4661 <         * @param action the action
4662 <         */
4663 <        public static <K,V,U> ForkJoinTask<Void> forEachEntry
4664 <            (ConcurrentHashMapV8<K,V> map,
4665 <             Fun<Map.Entry<K,V>, ? extends U> transformer,
4666 <             Action<U> action) {
4667 <            if (transformer == null || action == null)
4668 <                throw new NullPointerException();
4669 <            return new ForEachTransformedEntryTask<K,V,U>
4670 <                (map, transformer, action);
4497 >        public int hashCode() {
4498 >            int h = 0;
4499 >            for (K e : this)
4500 >                h += e.hashCode();
4501 >            return h;
4502          }
4503  
4504 <        /**
4505 <         * Returns a task that when invoked, returns a non-null result
4506 <         * from applying the given search function on each entry, or
4507 <         * null if none.  Further element processing is suppressed
4508 <         * upon success. However, this method does not return until
4678 <         * other in-progress parallel invocations of the search
4679 <         * function also complete.
4680 <         *
4681 <         * @param map the map
4682 <         * @param searchFunction a function returning a non-null
4683 <         * result on success, else null
4684 <         * @return the task
4685 <         *
4686 <         */
4687 <        public static <K,V,U> ForkJoinTask<U> searchEntries
4688 <            (ConcurrentHashMapV8<K,V> map,
4689 <             Fun<Map.Entry<K,V>, ? extends U> searchFunction) {
4690 <            if (searchFunction == null) throw new NullPointerException();
4691 <            return new SearchEntriesTask<K,V,U>
4692 <                (map, searchFunction,
4693 <                 new AtomicReference<U>());
4504 >        public boolean equals(Object o) {
4505 >            Set<?> c;
4506 >            return ((o instanceof Set) &&
4507 >                    ((c = (Set<?>)o) == this ||
4508 >                     (containsAll(c) && c.containsAll(this))));
4509          }
4510  
4511 <        /**
4512 <         * Returns a task that when invoked, returns the result of
4513 <         * accumulating all entries using the given reducer to combine
4514 <         * values, or null if none.
4515 <         *
4516 <         * @param map the map
4702 <         * @param reducer a commutative associative combining function
4703 <         * @return the task
4704 <         */
4705 <        public static <K,V> ForkJoinTask<Map.Entry<K,V>> reduceEntries
4706 <            (ConcurrentHashMapV8<K,V> map,
4707 <             BiFun<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer) {
4708 <            if (reducer == null) throw new NullPointerException();
4709 <            return new ReduceEntriesTask<K,V>
4710 <                (map, reducer);
4511 >        public ConcurrentHashMapSpliterator<K> spliterator() {
4512 >            Node<K,V>[] t;
4513 >            ConcurrentHashMapV8<K,V> m = map;
4514 >            long n = m.sumCount();
4515 >            int f = (t = m.table) == null ? 0 : t.length;
4516 >            return new KeySpliterator<K,V>(t, f, 0, f, n < 0L ? 0L : n);
4517          }
4518  
4519 <        /**
4520 <         * Returns a task that when invoked, returns the result of
4521 <         * accumulating the given transformation of all entries using the
4522 <         * given reducer to combine values, or null if none.
4523 <         *
4524 <         * @param map the map
4525 <         * @param transformer a function returning the transformation
4526 <         * for an element, or null of there is no transformation (in
4721 <         * which case it is not combined).
4722 <         * @param reducer a commutative associative combining function
4723 <         * @return the task
4724 <         */
4725 <        public static <K,V,U> ForkJoinTask<U> reduceEntries
4726 <            (ConcurrentHashMapV8<K,V> map,
4727 <             Fun<Map.Entry<K,V>, ? extends U> transformer,
4728 <             BiFun<? super U, ? super U, ? extends U> reducer) {
4729 <            if (transformer == null || reducer == null)
4730 <                throw new NullPointerException();
4731 <            return new MapReduceEntriesTask<K,V,U>
4732 <                (map, transformer, reducer);
4519 >        public void forEach(Action<? super K> action) {
4520 >            if (action == null) throw new NullPointerException();
4521 >            Node<K,V>[] t;
4522 >            if ((t = map.table) != null) {
4523 >                Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length);
4524 >                for (Node<K,V> p; (p = it.advance()) != null; )
4525 >                    action.apply(p.key);
4526 >            }
4527          }
4528 +    }
4529  
4530 <        /**
4531 <         * Returns a task that when invoked, returns the result of
4532 <         * accumulating the given transformation of all entries using the
4533 <         * given reducer to combine values, and the given basis as an
4534 <         * identity value.
4535 <         *
4536 <         * @param map the map
4537 <         * @param transformer a function returning the transformation
4538 <         * for an element
4539 <         * @param basis the identity (initial default value) for the reduction
4540 <         * @param reducer a commutative associative combining function
4746 <         * @return the task
4747 <         */
4748 <        public static <K,V> ForkJoinTask<Double> reduceEntriesToDouble
4749 <            (ConcurrentHashMapV8<K,V> map,
4750 <             ObjectToDouble<Map.Entry<K,V>> transformer,
4751 <             double basis,
4752 <             DoubleByDoubleToDouble reducer) {
4753 <            if (transformer == null || reducer == null)
4754 <                throw new NullPointerException();
4755 <            return new MapReduceEntriesToDoubleTask<K,V>
4756 <                (map, transformer, basis, reducer);
4530 >    /**
4531 >     * A view of a ConcurrentHashMapV8 as a {@link Collection} of
4532 >     * values, in which additions are disabled. This class cannot be
4533 >     * directly instantiated. See {@link #values()}.
4534 >     */
4535 >    static final class ValuesView<K,V> extends CollectionView<K,V,V>
4536 >        implements Collection<V>, java.io.Serializable {
4537 >        private static final long serialVersionUID = 2249069246763182397L;
4538 >        ValuesView(ConcurrentHashMapV8<K,V> map) { super(map); }
4539 >        public final boolean contains(Object o) {
4540 >            return map.containsValue(o);
4541          }
4542  
4543 <        /**
4544 <         * Returns a task that when invoked, returns the result of
4545 <         * accumulating the given transformation of all entries using the
4546 <         * given reducer to combine values, and the given basis as an
4547 <         * identity value.
4548 <         *
4549 <         * @param map the map
4550 <         * @param transformer a function returning the transformation
4551 <         * for an element
4552 <         * @param basis the identity (initial default value) for the reduction
4769 <         * @param reducer a commutative associative combining function
4770 <         * @return the task
4771 <         */
4772 <        public static <K,V> ForkJoinTask<Long> reduceEntriesToLong
4773 <            (ConcurrentHashMapV8<K,V> map,
4774 <             ObjectToLong<Map.Entry<K,V>> transformer,
4775 <             long basis,
4776 <             LongByLongToLong reducer) {
4777 <            if (transformer == null || reducer == null)
4778 <                throw new NullPointerException();
4779 <            return new MapReduceEntriesToLongTask<K,V>
4780 <                (map, transformer, basis, reducer);
4543 >        public final boolean remove(Object o) {
4544 >            if (o != null) {
4545 >                for (Iterator<V> it = iterator(); it.hasNext();) {
4546 >                    if (o.equals(it.next())) {
4547 >                        it.remove();
4548 >                        return true;
4549 >                    }
4550 >                }
4551 >            }
4552 >            return false;
4553          }
4554  
4555 <        /**
4556 <         * Returns a task that when invoked, returns the result of
4557 <         * accumulating the given transformation of all entries using the
4558 <         * given reducer to combine values, and the given basis as an
4559 <         * identity value.
4788 <         *
4789 <         * @param map the map
4790 <         * @param transformer a function returning the transformation
4791 <         * for an element
4792 <         * @param basis the identity (initial default value) for the reduction
4793 <         * @param reducer a commutative associative combining function
4794 <         * @return the task
4795 <         */
4796 <        public static <K,V> ForkJoinTask<Integer> reduceEntriesToInt
4797 <            (ConcurrentHashMapV8<K,V> map,
4798 <             ObjectToInt<Map.Entry<K,V>> transformer,
4799 <             int basis,
4800 <             IntByIntToInt reducer) {
4801 <            if (transformer == null || reducer == null)
4802 <                throw new NullPointerException();
4803 <            return new MapReduceEntriesToIntTask<K,V>
4804 <                (map, transformer, basis, reducer);
4555 >        public final Iterator<V> iterator() {
4556 >            ConcurrentHashMapV8<K,V> m = map;
4557 >            Node<K,V>[] t;
4558 >            int f = (t = m.table) == null ? 0 : t.length;
4559 >            return new ValueIterator<K,V>(t, f, 0, f, m);
4560          }
4806    }
4561  
4562 <    // -------------------------------------------------------
4562 >        public final boolean add(V e) {
4563 >            throw new UnsupportedOperationException();
4564 >        }
4565 >        public final boolean addAll(Collection<? extends V> c) {
4566 >            throw new UnsupportedOperationException();
4567 >        }
4568  
4569 <    /**
4570 <     * Base for FJ tasks for bulk operations. This adds a variant of
4571 <     * CountedCompleters and some split and merge bookkeeping to
4572 <     * iterator functionality. The forEach and reduce methods are
4573 <     * similar to those illustrated in CountedCompleter documentation,
4574 <     * except that bottom-up reduction completions perform them within
4575 <     * their compute methods. The search methods are like forEach
4817 <     * except they continually poll for success and exit early.  Also,
4818 <     * exceptions are handled in a simpler manner, by just trying to
4819 <     * complete root task exceptionally.
4820 <     */
4821 <    static abstract class BulkTask<K,V,R> extends Traverser<K,V,R> {
4822 <        final BulkTask<K,V,?> parent;  // completion target
4823 <        int batch;                     // split control
4824 <        int pending;                   // completion control
4569 >        public ConcurrentHashMapSpliterator<V> spliterator() {
4570 >            Node<K,V>[] t;
4571 >            ConcurrentHashMapV8<K,V> m = map;
4572 >            long n = m.sumCount();
4573 >            int f = (t = m.table) == null ? 0 : t.length;
4574 >            return new ValueSpliterator<K,V>(t, f, 0, f, n < 0L ? 0L : n);
4575 >        }
4576  
4577 <        /** Constructor for root tasks */
4578 <        BulkTask(ConcurrentHashMapV8<K,V> map) {
4579 <            super(map);
4580 <            this.parent = null;
4581 <            this.batch = -1; // force call to batch() on execution
4577 >        public void forEach(Action<? super V> action) {
4578 >            if (action == null) throw new NullPointerException();
4579 >            Node<K,V>[] t;
4580 >            if ((t = map.table) != null) {
4581 >                Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length);
4582 >                for (Node<K,V> p; (p = it.advance()) != null; )
4583 >                    action.apply(p.val);
4584 >            }
4585          }
4586 +    }
4587  
4588 <        /** Constructor for subtasks */
4589 <        BulkTask(BulkTask<K,V,?> parent, int batch, boolean split) {
4590 <            super(parent, split);
4591 <            this.parent = parent;
4592 <            this.batch = batch;
4588 >    /**
4589 >     * A view of a ConcurrentHashMapV8 as a {@link Set} of (key, value)
4590 >     * entries.  This class cannot be directly instantiated. See
4591 >     * {@link #entrySet()}.
4592 >     */
4593 >    static final class EntrySetView<K,V> extends CollectionView<K,V,Map.Entry<K,V>>
4594 >        implements Set<Map.Entry<K,V>>, java.io.Serializable {
4595 >        private static final long serialVersionUID = 2249069246763182397L;
4596 >        EntrySetView(ConcurrentHashMapV8<K,V> map) { super(map); }
4597 >
4598 >        public boolean contains(Object o) {
4599 >            Object k, v, r; Map.Entry<?,?> e;
4600 >            return ((o instanceof Map.Entry) &&
4601 >                    (k = (e = (Map.Entry<?,?>)o).getKey()) != null &&
4602 >                    (r = map.get(k)) != null &&
4603 >                    (v = e.getValue()) != null &&
4604 >                    (v == r || v.equals(r)));
4605          }
4606  
4607 <        // FJ methods
4607 >        public boolean remove(Object o) {
4608 >            Object k, v; Map.Entry<?,?> e;
4609 >            return ((o instanceof Map.Entry) &&
4610 >                    (k = (e = (Map.Entry<?,?>)o).getKey()) != null &&
4611 >                    (v = e.getValue()) != null &&
4612 >                    map.remove(k, v));
4613 >        }
4614  
4615          /**
4616 <         * Propagates completion. Note that all reduce actions
4844 <         * bypass this method to combine while completing.
4616 >         * @return an iterator over the entries of the backing map
4617           */
4618 <        final void tryComplete() {
4619 <            BulkTask<K,V,?> a = this, s = a;
4620 <            for (int c;;) {
4621 <                if ((c = a.pending) == 0) {
4622 <                    if ((a = (s = a).parent) == null) {
4851 <                        s.quietlyComplete();
4852 <                        break;
4853 <                    }
4854 <                }
4855 <                else if (U.compareAndSwapInt(a, PENDING, c, c - 1))
4856 <                    break;
4857 <            }
4618 >        public Iterator<Map.Entry<K,V>> iterator() {
4619 >            ConcurrentHashMapV8<K,V> m = map;
4620 >            Node<K,V>[] t;
4621 >            int f = (t = m.table) == null ? 0 : t.length;
4622 >            return new EntryIterator<K,V>(t, f, 0, f, m);
4623          }
4624  
4625 <        /**
4626 <         * Forces root task to throw exception unless already complete.
4862 <         */
4863 <        final void tryAbortComputation(Throwable ex) {
4864 <            for (BulkTask<K,V,?> a = this;;) {
4865 <                BulkTask<K,V,?> p = a.parent;
4866 <                if (p == null) {
4867 <                    a.completeExceptionally(ex);
4868 <                    break;
4869 <                }
4870 <                a = p;
4871 <            }
4625 >        public boolean add(Entry<K,V> e) {
4626 >            return map.putVal(e.getKey(), e.getValue(), false) == null;
4627          }
4628  
4629 <        public final boolean exec() {
4630 <            try {
4631 <                compute();
4632 <            }
4633 <            catch (Throwable ex) {
4879 <                tryAbortComputation(ex);
4629 >        public boolean addAll(Collection<? extends Entry<K,V>> c) {
4630 >            boolean added = false;
4631 >            for (Entry<K,V> e : c) {
4632 >                if (add(e))
4633 >                    added = true;
4634              }
4635 <            return false;
4635 >            return added;
4636          }
4637  
4638 <        public abstract void compute();
4638 >        public final int hashCode() {
4639 >            int h = 0;
4640 >            Node<K,V>[] t;
4641 >            if ((t = map.table) != null) {
4642 >                Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length);
4643 >                for (Node<K,V> p; (p = it.advance()) != null; ) {
4644 >                    h += p.hashCode();
4645 >                }
4646 >            }
4647 >            return h;
4648 >        }
4649  
4650 <        // utilities
4650 >        public final boolean equals(Object o) {
4651 >            Set<?> c;
4652 >            return ((o instanceof Set) &&
4653 >                    ((c = (Set<?>)o) == this ||
4654 >                     (containsAll(c) && c.containsAll(this))));
4655 >        }
4656  
4657 <        /** CompareAndSet pending count */
4658 <        final boolean casPending(int cmp, int val) {
4659 <            return U.compareAndSwapInt(this, PENDING, cmp, val);
4657 >        public ConcurrentHashMapSpliterator<Map.Entry<K,V>> spliterator() {
4658 >            Node<K,V>[] t;
4659 >            ConcurrentHashMapV8<K,V> m = map;
4660 >            long n = m.sumCount();
4661 >            int f = (t = m.table) == null ? 0 : t.length;
4662 >            return new EntrySpliterator<K,V>(t, f, 0, f, n < 0L ? 0L : n, m);
4663          }
4664  
4665 <        /**
4666 <         * Returns approx exp2 of the number of times (minus one) to
4667 <         * split task by two before executing leaf action. This value
4668 <         * is faster to compute and more convenient to use as a guide
4669 <         * to splitting than is the depth, since it is used while
4670 <         * dividing by two anyway.
4671 <         */
4900 <        final int batch() {
4901 <            int b = batch;
4902 <            if (b < 0) {
4903 <                long n = map.counter.sum();
4904 <                int sp = getPool().getParallelism() << 3; // slack of 8
4905 <                b = batch = (n <= 0L) ? 0 : (n < (long)sp) ? (int)n : sp;
4665 >        public void forEach(Action<? super Map.Entry<K,V>> action) {
4666 >            if (action == null) throw new NullPointerException();
4667 >            Node<K,V>[] t;
4668 >            if ((t = map.table) != null) {
4669 >                Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length);
4670 >                for (Node<K,V> p; (p = it.advance()) != null; )
4671 >                    action.apply(new MapEntry<K,V>(p.key, p.val, map));
4672              }
4907            return b;
4673          }
4674  
4675 <        /**
4911 <         * Error message for hoisted null checks of functions
4912 <         */
4913 <        static final String NullFunctionMessage =
4914 <            "Unexpected null function";
4675 >    }
4676  
4677 <        /**
4678 <         * Returns exportable snapshot entry.
4679 <         */
4680 <        static <K,V> AbstractMap.SimpleEntry<K,V> entryFor(K k, V v) {
4681 <            return new AbstractMap.SimpleEntry(k, v);
4677 >    // -------------------------------------------------------
4678 >
4679 >    /**
4680 >     * Base class for bulk tasks. Repeats some fields and code from
4681 >     * class Traverser, because we need to subclass CountedCompleter.
4682 >     */
4683 >    abstract static class BulkTask<K,V,R> extends CountedCompleter<R> {
4684 >        Node<K,V>[] tab;        // same as Traverser
4685 >        Node<K,V> next;
4686 >        int index;
4687 >        int baseIndex;
4688 >        int baseLimit;
4689 >        final int baseSize;
4690 >        int batch;              // split control
4691 >
4692 >        BulkTask(BulkTask<K,V,?> par, int b, int i, int f, Node<K,V>[] t) {
4693 >            super(par);
4694 >            this.batch = b;
4695 >            this.index = this.baseIndex = i;
4696 >            if ((this.tab = t) == null)
4697 >                this.baseSize = this.baseLimit = 0;
4698 >            else if (par == null)
4699 >                this.baseSize = this.baseLimit = t.length;
4700 >            else {
4701 >                this.baseLimit = f;
4702 >                this.baseSize = par.baseSize;
4703 >            }
4704          }
4705  
4706 <        // Unsafe mechanics
4707 <        private static final sun.misc.Unsafe U;
4708 <        private static final long PENDING;
4709 <        static {
4710 <            try {
4711 <                U = sun.misc.Unsafe.getUnsafe();
4712 <                PENDING = U.objectFieldOffset
4713 <                    (BulkTask.class.getDeclaredField("pending"));
4714 <            } catch (Exception e) {
4715 <                throw new Error(e);
4706 >        /**
4707 >         * Same as Traverser version
4708 >         */
4709 >        final Node<K,V> advance() {
4710 >            Node<K,V> e;
4711 >            if ((e = next) != null)
4712 >                e = e.next;
4713 >            for (;;) {
4714 >                Node<K,V>[] t; int i, n; K ek;  // must use locals in checks
4715 >                if (e != null)
4716 >                    return next = e;
4717 >                if (baseIndex >= baseLimit || (t = tab) == null ||
4718 >                    (n = t.length) <= (i = index) || i < 0)
4719 >                    return next = null;
4720 >                if ((e = tabAt(t, index)) != null && e.hash < 0) {
4721 >                    if (e instanceof ForwardingNode) {
4722 >                        tab = ((ForwardingNode<K,V>)e).nextTable;
4723 >                        e = null;
4724 >                        continue;
4725 >                    }
4726 >                    else if (e instanceof TreeBin)
4727 >                        e = ((TreeBin<K,V>)e).first;
4728 >                    else
4729 >                        e = null;
4730 >                }
4731 >                if ((index += baseSize) >= n)
4732 >                    index = ++baseIndex;    // visit upper slots if present
4733              }
4734          }
4735      }
# Line 4937 | Line 4737 | public class ConcurrentHashMapV8<K, V>
4737      /*
4738       * Task classes. Coded in a regular but ugly format/style to
4739       * simplify checks that each variant differs in the right way from
4740 <     * others.
4740 >     * others. The null screenings exist because compilers cannot tell
4741 >     * that we've already null-checked task arguments, so we force
4742 >     * simplest hoisted bypass to help avoid convoluted traps.
4743       */
4744 <
4744 >    @SuppressWarnings("serial")
4745      static final class ForEachKeyTask<K,V>
4746          extends BulkTask<K,V,Void> {
4747 <        final Action<K> action;
4946 <        ForEachKeyTask
4947 <            (ConcurrentHashMapV8<K,V> m,
4948 <             Action<K> action) {
4949 <            super(m);
4950 <            this.action = action;
4951 <        }
4747 >        final Action<? super K> action;
4748          ForEachKeyTask
4749 <            (BulkTask<K,V,?> p, int b, boolean split,
4750 <             Action<K> action) {
4751 <            super(p, b, split);
4749 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
4750 >             Action<? super K> action) {
4751 >            super(p, b, i, f, t);
4752              this.action = action;
4753          }
4754          public final void compute() {
4755 <            final Action<K> action = this.action;
4756 <            if (action == null)
4757 <                throw new Error(NullFunctionMessage);
4758 <            int b = batch(), c;
4759 <            while (b > 1 && baseIndex != baseLimit) {
4760 <                do {} while (!casPending(c = pending, c+1));
4761 <                new ForEachKeyTask<K,V>(this, b >>>= 1, true, action).fork();
4762 <            }
4763 <            while (advance() != null)
4764 <                action.apply((K)nextKey);
4765 <            tryComplete();
4755 >            final Action<? super K> action;
4756 >            if ((action = this.action) != null) {
4757 >                for (int i = baseIndex, f, h; batch > 0 &&
4758 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
4759 >                    addToPendingCount(1);
4760 >                    new ForEachKeyTask<K,V>
4761 >                        (this, batch >>>= 1, baseLimit = h, f, tab,
4762 >                         action).fork();
4763 >                }
4764 >                for (Node<K,V> p; (p = advance()) != null;)
4765 >                    action.apply(p.key);
4766 >                propagateCompletion();
4767 >            }
4768          }
4769      }
4770  
4771 +    @SuppressWarnings("serial")
4772      static final class ForEachValueTask<K,V>
4773          extends BulkTask<K,V,Void> {
4774 <        final Action<V> action;
4976 <        ForEachValueTask
4977 <            (ConcurrentHashMapV8<K,V> m,
4978 <             Action<V> action) {
4979 <            super(m);
4980 <            this.action = action;
4981 <        }
4774 >        final Action<? super V> action;
4775          ForEachValueTask
4776 <            (BulkTask<K,V,?> p, int b, boolean split,
4777 <             Action<V> action) {
4778 <            super(p, b, split);
4776 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
4777 >             Action<? super V> action) {
4778 >            super(p, b, i, f, t);
4779              this.action = action;
4780          }
4781          public final void compute() {
4782 <            final Action<V> action = this.action;
4783 <            if (action == null)
4784 <                throw new Error(NullFunctionMessage);
4785 <            int b = batch(), c;
4786 <            while (b > 1 && baseIndex != baseLimit) {
4787 <                do {} while (!casPending(c = pending, c+1));
4788 <                new ForEachValueTask<K,V>(this, b >>>= 1, true, action).fork();
4789 <            }
4790 <            Object v;
4791 <            while ((v = advance()) != null)
4792 <                action.apply((V)v);
4793 <            tryComplete();
4782 >            final Action<? super V> action;
4783 >            if ((action = this.action) != null) {
4784 >                for (int i = baseIndex, f, h; batch > 0 &&
4785 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
4786 >                    addToPendingCount(1);
4787 >                    new ForEachValueTask<K,V>
4788 >                        (this, batch >>>= 1, baseLimit = h, f, tab,
4789 >                         action).fork();
4790 >                }
4791 >                for (Node<K,V> p; (p = advance()) != null;)
4792 >                    action.apply(p.val);
4793 >                propagateCompletion();
4794 >            }
4795          }
4796      }
4797  
4798 +    @SuppressWarnings("serial")
4799      static final class ForEachEntryTask<K,V>
4800          extends BulkTask<K,V,Void> {
4801 <        final Action<Entry<K,V>> action;
5007 <        ForEachEntryTask
5008 <            (ConcurrentHashMapV8<K,V> m,
5009 <             Action<Entry<K,V>> action) {
5010 <            super(m);
5011 <            this.action = action;
5012 <        }
4801 >        final Action<? super Entry<K,V>> action;
4802          ForEachEntryTask
4803 <            (BulkTask<K,V,?> p, int b, boolean split,
4804 <             Action<Entry<K,V>> action) {
4805 <            super(p, b, split);
4803 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
4804 >             Action<? super Entry<K,V>> action) {
4805 >            super(p, b, i, f, t);
4806              this.action = action;
4807          }
4808          public final void compute() {
4809 <            final Action<Entry<K,V>> action = this.action;
4810 <            if (action == null)
4811 <                throw new Error(NullFunctionMessage);
4812 <            int b = batch(), c;
4813 <            while (b > 1 && baseIndex != baseLimit) {
4814 <                do {} while (!casPending(c = pending, c+1));
4815 <                new ForEachEntryTask<K,V>(this, b >>>= 1, true, action).fork();
4816 <            }
4817 <            Object v;
4818 <            while ((v = advance()) != null)
4819 <                action.apply(entryFor((K)nextKey, (V)v));
4820 <            tryComplete();
4809 >            final Action<? super Entry<K,V>> action;
4810 >            if ((action = this.action) != null) {
4811 >                for (int i = baseIndex, f, h; batch > 0 &&
4812 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
4813 >                    addToPendingCount(1);
4814 >                    new ForEachEntryTask<K,V>
4815 >                        (this, batch >>>= 1, baseLimit = h, f, tab,
4816 >                         action).fork();
4817 >                }
4818 >                for (Node<K,V> p; (p = advance()) != null; )
4819 >                    action.apply(p);
4820 >                propagateCompletion();
4821 >            }
4822          }
4823      }
4824  
4825 +    @SuppressWarnings("serial")
4826      static final class ForEachMappingTask<K,V>
4827          extends BulkTask<K,V,Void> {
4828 <        final BiAction<K,V> action;
5038 <        ForEachMappingTask
5039 <            (ConcurrentHashMapV8<K,V> m,
5040 <             BiAction<K,V> action) {
5041 <            super(m);
5042 <            this.action = action;
5043 <        }
4828 >        final BiAction<? super K, ? super V> action;
4829          ForEachMappingTask
4830 <            (BulkTask<K,V,?> p, int b, boolean split,
4831 <             BiAction<K,V> action) {
4832 <            super(p, b, split);
4830 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
4831 >             BiAction<? super K,? super V> action) {
4832 >            super(p, b, i, f, t);
4833              this.action = action;
4834          }
5050
4835          public final void compute() {
4836 <            final BiAction<K,V> action = this.action;
4837 <            if (action == null)
4838 <                throw new Error(NullFunctionMessage);
4839 <            int b = batch(), c;
4840 <            while (b > 1 && baseIndex != baseLimit) {
4841 <                do {} while (!casPending(c = pending, c+1));
4842 <                new ForEachMappingTask<K,V>(this, b >>>= 1, true,
4843 <                                            action).fork();
4844 <            }
4845 <            Object v;
4846 <            while ((v = advance()) != null)
4847 <                action.apply((K)nextKey, (V)v);
4848 <            tryComplete();
4836 >            final BiAction<? super K, ? super V> action;
4837 >            if ((action = this.action) != null) {
4838 >                for (int i = baseIndex, f, h; batch > 0 &&
4839 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
4840 >                    addToPendingCount(1);
4841 >                    new ForEachMappingTask<K,V>
4842 >                        (this, batch >>>= 1, baseLimit = h, f, tab,
4843 >                         action).fork();
4844 >                }
4845 >                for (Node<K,V> p; (p = advance()) != null; )
4846 >                    action.apply(p.key, p.val);
4847 >                propagateCompletion();
4848 >            }
4849          }
4850      }
4851  
4852 +    @SuppressWarnings("serial")
4853      static final class ForEachTransformedKeyTask<K,V,U>
4854          extends BulkTask<K,V,Void> {
4855          final Fun<? super K, ? extends U> transformer;
4856 <        final Action<U> action;
4856 >        final Action<? super U> action;
4857          ForEachTransformedKeyTask
4858 <            (ConcurrentHashMapV8<K,V> m,
4859 <             Fun<? super K, ? extends U> transformer,
4860 <             Action<U> action) {
4861 <            super(m);
5077 <            this.transformer = transformer;
5078 <            this.action = action;
5079 <
5080 <        }
5081 <        ForEachTransformedKeyTask
5082 <            (BulkTask<K,V,?> p, int b, boolean split,
5083 <             Fun<? super K, ? extends U> transformer,
5084 <             Action<U> action) {
5085 <            super(p, b, split);
5086 <            this.transformer = transformer;
5087 <            this.action = action;
4858 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
4859 >             Fun<? super K, ? extends U> transformer, Action<? super U> action) {
4860 >            super(p, b, i, f, t);
4861 >            this.transformer = transformer; this.action = action;
4862          }
4863          public final void compute() {
4864 <            final Fun<? super K, ? extends U> transformer =
4865 <                this.transformer;
4866 <            final Action<U> action = this.action;
4867 <            if (transformer == null || action == null)
4868 <                throw new Error(NullFunctionMessage);
4869 <            int b = batch(), c;
4870 <            while (b > 1 && baseIndex != baseLimit) {
4871 <                do {} while (!casPending(c = pending, c+1));
4872 <                new ForEachTransformedKeyTask<K,V,U>
4873 <                    (this, b >>>= 1, true, transformer, action).fork();
4874 <            }
4875 <            U u;
4876 <            while (advance() != null) {
4877 <                if ((u = transformer.apply((K)nextKey)) != null)
4878 <                    action.apply(u);
4864 >            final Fun<? super K, ? extends U> transformer;
4865 >            final Action<? super U> action;
4866 >            if ((transformer = this.transformer) != null &&
4867 >                (action = this.action) != null) {
4868 >                for (int i = baseIndex, f, h; batch > 0 &&
4869 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
4870 >                    addToPendingCount(1);
4871 >                    new ForEachTransformedKeyTask<K,V,U>
4872 >                        (this, batch >>>= 1, baseLimit = h, f, tab,
4873 >                         transformer, action).fork();
4874 >                }
4875 >                for (Node<K,V> p; (p = advance()) != null; ) {
4876 >                    U u;
4877 >                    if ((u = transformer.apply(p.key)) != null)
4878 >                        action.apply(u);
4879 >                }
4880 >                propagateCompletion();
4881              }
5106            tryComplete();
4882          }
4883      }
4884  
4885 +    @SuppressWarnings("serial")
4886      static final class ForEachTransformedValueTask<K,V,U>
4887          extends BulkTask<K,V,Void> {
4888          final Fun<? super V, ? extends U> transformer;
4889 <        final Action<U> action;
5114 <        ForEachTransformedValueTask
5115 <            (ConcurrentHashMapV8<K,V> m,
5116 <             Fun<? super V, ? extends U> transformer,
5117 <             Action<U> action) {
5118 <            super(m);
5119 <            this.transformer = transformer;
5120 <            this.action = action;
5121 <
5122 <        }
4889 >        final Action<? super U> action;
4890          ForEachTransformedValueTask
4891 <            (BulkTask<K,V,?> p, int b, boolean split,
4892 <             Fun<? super V, ? extends U> transformer,
4893 <             Action<U> action) {
4894 <            super(p, b, split);
5128 <            this.transformer = transformer;
5129 <            this.action = action;
4891 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
4892 >             Fun<? super V, ? extends U> transformer, Action<? super U> action) {
4893 >            super(p, b, i, f, t);
4894 >            this.transformer = transformer; this.action = action;
4895          }
4896          public final void compute() {
4897 <            final Fun<? super V, ? extends U> transformer =
4898 <                this.transformer;
4899 <            final Action<U> action = this.action;
4900 <            if (transformer == null || action == null)
4901 <                throw new Error(NullFunctionMessage);
4902 <            int b = batch(), c;
4903 <            while (b > 1 && baseIndex != baseLimit) {
4904 <                do {} while (!casPending(c = pending, c+1));
4905 <                new ForEachTransformedValueTask<K,V,U>
4906 <                    (this, b >>>= 1, true, transformer, action).fork();
4907 <            }
4908 <            Object v; U u;
4909 <            while ((v = advance()) != null) {
4910 <                if ((u = transformer.apply((V)v)) != null)
4911 <                    action.apply(u);
4897 >            final Fun<? super V, ? extends U> transformer;
4898 >            final Action<? super U> action;
4899 >            if ((transformer = this.transformer) != null &&
4900 >                (action = this.action) != null) {
4901 >                for (int i = baseIndex, f, h; batch > 0 &&
4902 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
4903 >                    addToPendingCount(1);
4904 >                    new ForEachTransformedValueTask<K,V,U>
4905 >                        (this, batch >>>= 1, baseLimit = h, f, tab,
4906 >                         transformer, action).fork();
4907 >                }
4908 >                for (Node<K,V> p; (p = advance()) != null; ) {
4909 >                    U u;
4910 >                    if ((u = transformer.apply(p.val)) != null)
4911 >                        action.apply(u);
4912 >                }
4913 >                propagateCompletion();
4914              }
5148            tryComplete();
4915          }
4916      }
4917  
4918 +    @SuppressWarnings("serial")
4919      static final class ForEachTransformedEntryTask<K,V,U>
4920          extends BulkTask<K,V,Void> {
4921          final Fun<Map.Entry<K,V>, ? extends U> transformer;
4922 <        final Action<U> action;
4922 >        final Action<? super U> action;
4923          ForEachTransformedEntryTask
4924 <            (ConcurrentHashMapV8<K,V> m,
4925 <             Fun<Map.Entry<K,V>, ? extends U> transformer,
4926 <             Action<U> action) {
4927 <            super(m);
5161 <            this.transformer = transformer;
5162 <            this.action = action;
5163 <
5164 <        }
5165 <        ForEachTransformedEntryTask
5166 <            (BulkTask<K,V,?> p, int b, boolean split,
5167 <             Fun<Map.Entry<K,V>, ? extends U> transformer,
5168 <             Action<U> action) {
5169 <            super(p, b, split);
5170 <            this.transformer = transformer;
5171 <            this.action = action;
4924 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
4925 >             Fun<Map.Entry<K,V>, ? extends U> transformer, Action<? super U> action) {
4926 >            super(p, b, i, f, t);
4927 >            this.transformer = transformer; this.action = action;
4928          }
4929          public final void compute() {
4930 <            final Fun<Map.Entry<K,V>, ? extends U> transformer =
4931 <                this.transformer;
4932 <            final Action<U> action = this.action;
4933 <            if (transformer == null || action == null)
4934 <                throw new Error(NullFunctionMessage);
4935 <            int b = batch(), c;
4936 <            while (b > 1 && baseIndex != baseLimit) {
4937 <                do {} while (!casPending(c = pending, c+1));
4938 <                new ForEachTransformedEntryTask<K,V,U>
4939 <                    (this, b >>>= 1, true, transformer, action).fork();
4940 <            }
4941 <            Object v; U u;
4942 <            while ((v = advance()) != null) {
4943 <                if ((u = transformer.apply(entryFor((K)nextKey, (V)v))) != null)
4944 <                    action.apply(u);
4930 >            final Fun<Map.Entry<K,V>, ? extends U> transformer;
4931 >            final Action<? super U> action;
4932 >            if ((transformer = this.transformer) != null &&
4933 >                (action = this.action) != null) {
4934 >                for (int i = baseIndex, f, h; batch > 0 &&
4935 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
4936 >                    addToPendingCount(1);
4937 >                    new ForEachTransformedEntryTask<K,V,U>
4938 >                        (this, batch >>>= 1, baseLimit = h, f, tab,
4939 >                         transformer, action).fork();
4940 >                }
4941 >                for (Node<K,V> p; (p = advance()) != null; ) {
4942 >                    U u;
4943 >                    if ((u = transformer.apply(p)) != null)
4944 >                        action.apply(u);
4945 >                }
4946 >                propagateCompletion();
4947              }
5190            tryComplete();
4948          }
4949      }
4950  
4951 +    @SuppressWarnings("serial")
4952      static final class ForEachTransformedMappingTask<K,V,U>
4953          extends BulkTask<K,V,Void> {
4954          final BiFun<? super K, ? super V, ? extends U> transformer;
4955 <        final Action<U> action;
4955 >        final Action<? super U> action;
4956          ForEachTransformedMappingTask
4957 <            (ConcurrentHashMapV8<K,V> m,
4957 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
4958               BiFun<? super K, ? super V, ? extends U> transformer,
4959 <             Action<U> action) {
4960 <            super(m);
4961 <            this.transformer = transformer;
5204 <            this.action = action;
5205 <
5206 <        }
5207 <        ForEachTransformedMappingTask
5208 <            (BulkTask<K,V,?> p, int b, boolean split,
5209 <             BiFun<? super K, ? super V, ? extends U> transformer,
5210 <             Action<U> action) {
5211 <            super(p, b, split);
5212 <            this.transformer = transformer;
5213 <            this.action = action;
4959 >             Action<? super U> action) {
4960 >            super(p, b, i, f, t);
4961 >            this.transformer = transformer; this.action = action;
4962          }
4963          public final void compute() {
4964 <            final BiFun<? super K, ? super V, ? extends U> transformer =
4965 <                this.transformer;
4966 <            final Action<U> action = this.action;
4967 <            if (transformer == null || action == null)
4968 <                throw new Error(NullFunctionMessage);
4969 <            int b = batch(), c;
4970 <            while (b > 1 && baseIndex != baseLimit) {
4971 <                do {} while (!casPending(c = pending, c+1));
4972 <                new ForEachTransformedMappingTask<K,V,U>
4973 <                    (this, b >>>= 1, true, transformer, action).fork();
4974 <            }
4975 <            Object v; U u;
4976 <            while ((v = advance()) != null) {
4977 <                if ((u = transformer.apply((K)nextKey, (V)v)) != null)
4978 <                    action.apply(u);
4964 >            final BiFun<? super K, ? super V, ? extends U> transformer;
4965 >            final Action<? super U> action;
4966 >            if ((transformer = this.transformer) != null &&
4967 >                (action = this.action) != null) {
4968 >                for (int i = baseIndex, f, h; batch > 0 &&
4969 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
4970 >                    addToPendingCount(1);
4971 >                    new ForEachTransformedMappingTask<K,V,U>
4972 >                        (this, batch >>>= 1, baseLimit = h, f, tab,
4973 >                         transformer, action).fork();
4974 >                }
4975 >                for (Node<K,V> p; (p = advance()) != null; ) {
4976 >                    U u;
4977 >                    if ((u = transformer.apply(p.key, p.val)) != null)
4978 >                        action.apply(u);
4979 >                }
4980 >                propagateCompletion();
4981              }
5232            tryComplete();
4982          }
4983      }
4984  
4985 +    @SuppressWarnings("serial")
4986      static final class SearchKeysTask<K,V,U>
4987          extends BulkTask<K,V,U> {
4988          final Fun<? super K, ? extends U> searchFunction;
4989          final AtomicReference<U> result;
4990          SearchKeysTask
4991 <            (ConcurrentHashMapV8<K,V> m,
5242 <             Fun<? super K, ? extends U> searchFunction,
5243 <             AtomicReference<U> result) {
5244 <            super(m);
5245 <            this.searchFunction = searchFunction; this.result = result;
5246 <        }
5247 <        SearchKeysTask
5248 <            (BulkTask<K,V,?> p, int b, boolean split,
4991 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
4992               Fun<? super K, ? extends U> searchFunction,
4993               AtomicReference<U> result) {
4994 <            super(p, b, split);
4994 >            super(p, b, i, f, t);
4995              this.searchFunction = searchFunction; this.result = result;
4996          }
4997 +        public final U getRawResult() { return result.get(); }
4998          public final void compute() {
4999 <            AtomicReference<U> result = this.result;
5000 <            final Fun<? super K, ? extends U> searchFunction =
5001 <                this.searchFunction;
5002 <            if (searchFunction == null || result == null)
5003 <                throw new Error(NullFunctionMessage);
5004 <            int b = batch(), c;
5005 <            while (b > 1 && baseIndex != baseLimit && result.get() == null) {
5006 <                do {} while (!casPending(c = pending, c+1));
5007 <                new SearchKeysTask<K,V,U>(this, b >>>= 1, true,
5008 <                                          searchFunction, result).fork();
5009 <            }
5010 <            U u;
5011 <            while (result.get() == null && advance() != null) {
5012 <                if ((u = searchFunction.apply((K)nextKey)) != null) {
5013 <                    result.compareAndSet(null, u);
5014 <                    break;
4999 >            final Fun<? super K, ? extends U> searchFunction;
5000 >            final AtomicReference<U> result;
5001 >            if ((searchFunction = this.searchFunction) != null &&
5002 >                (result = this.result) != null) {
5003 >                for (int i = baseIndex, f, h; batch > 0 &&
5004 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5005 >                    if (result.get() != null)
5006 >                        return;
5007 >                    addToPendingCount(1);
5008 >                    new SearchKeysTask<K,V,U>
5009 >                        (this, batch >>>= 1, baseLimit = h, f, tab,
5010 >                         searchFunction, result).fork();
5011 >                }
5012 >                while (result.get() == null) {
5013 >                    U u;
5014 >                    Node<K,V> p;
5015 >                    if ((p = advance()) == null) {
5016 >                        propagateCompletion();
5017 >                        break;
5018 >                    }
5019 >                    if ((u = searchFunction.apply(p.key)) != null) {
5020 >                        if (result.compareAndSet(null, u))
5021 >                            quietlyCompleteRoot();
5022 >                        break;
5023 >                    }
5024                  }
5025              }
5273            tryComplete();
5026          }
5275        public final U getRawResult() { return result.get(); }
5027      }
5028  
5029 +    @SuppressWarnings("serial")
5030      static final class SearchValuesTask<K,V,U>
5031          extends BulkTask<K,V,U> {
5032          final Fun<? super V, ? extends U> searchFunction;
5033          final AtomicReference<U> result;
5034          SearchValuesTask
5035 <            (ConcurrentHashMapV8<K,V> m,
5035 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5036               Fun<? super V, ? extends U> searchFunction,
5037               AtomicReference<U> result) {
5038 <            super(m);
5287 <            this.searchFunction = searchFunction; this.result = result;
5288 <        }
5289 <        SearchValuesTask
5290 <            (BulkTask<K,V,?> p, int b, boolean split,
5291 <             Fun<? super V, ? extends U> searchFunction,
5292 <             AtomicReference<U> result) {
5293 <            super(p, b, split);
5038 >            super(p, b, i, f, t);
5039              this.searchFunction = searchFunction; this.result = result;
5040          }
5041 +        public final U getRawResult() { return result.get(); }
5042          public final void compute() {
5043 <            AtomicReference<U> result = this.result;
5044 <            final Fun<? super V, ? extends U> searchFunction =
5045 <                this.searchFunction;
5046 <            if (searchFunction == null || result == null)
5047 <                throw new Error(NullFunctionMessage);
5048 <            int b = batch(), c;
5049 <            while (b > 1 && baseIndex != baseLimit && result.get() == null) {
5050 <                do {} while (!casPending(c = pending, c+1));
5051 <                new SearchValuesTask<K,V,U>(this, b >>>= 1, true,
5052 <                                            searchFunction, result).fork();
5053 <            }
5054 <            Object v; U u;
5055 <            while (result.get() == null && (v = advance()) != null) {
5056 <                if ((u = searchFunction.apply((V)v)) != null) {
5057 <                    result.compareAndSet(null, u);
5058 <                    break;
5043 >            final Fun<? super V, ? extends U> searchFunction;
5044 >            final AtomicReference<U> result;
5045 >            if ((searchFunction = this.searchFunction) != null &&
5046 >                (result = this.result) != null) {
5047 >                for (int i = baseIndex, f, h; batch > 0 &&
5048 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5049 >                    if (result.get() != null)
5050 >                        return;
5051 >                    addToPendingCount(1);
5052 >                    new SearchValuesTask<K,V,U>
5053 >                        (this, batch >>>= 1, baseLimit = h, f, tab,
5054 >                         searchFunction, result).fork();
5055 >                }
5056 >                while (result.get() == null) {
5057 >                    U u;
5058 >                    Node<K,V> p;
5059 >                    if ((p = advance()) == null) {
5060 >                        propagateCompletion();
5061 >                        break;
5062 >                    }
5063 >                    if ((u = searchFunction.apply(p.val)) != null) {
5064 >                        if (result.compareAndSet(null, u))
5065 >                            quietlyCompleteRoot();
5066 >                        break;
5067 >                    }
5068                  }
5069              }
5315            tryComplete();
5070          }
5317        public final U getRawResult() { return result.get(); }
5071      }
5072  
5073 +    @SuppressWarnings("serial")
5074      static final class SearchEntriesTask<K,V,U>
5075          extends BulkTask<K,V,U> {
5076          final Fun<Entry<K,V>, ? extends U> searchFunction;
5077          final AtomicReference<U> result;
5078          SearchEntriesTask
5079 <            (ConcurrentHashMapV8<K,V> m,
5079 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5080               Fun<Entry<K,V>, ? extends U> searchFunction,
5081               AtomicReference<U> result) {
5082 <            super(m);
5329 <            this.searchFunction = searchFunction; this.result = result;
5330 <        }
5331 <        SearchEntriesTask
5332 <            (BulkTask<K,V,?> p, int b, boolean split,
5333 <             Fun<Entry<K,V>, ? extends U> searchFunction,
5334 <             AtomicReference<U> result) {
5335 <            super(p, b, split);
5082 >            super(p, b, i, f, t);
5083              this.searchFunction = searchFunction; this.result = result;
5084          }
5085 +        public final U getRawResult() { return result.get(); }
5086          public final void compute() {
5087 <            AtomicReference<U> result = this.result;
5088 <            final Fun<Entry<K,V>, ? extends U> searchFunction =
5089 <                this.searchFunction;
5090 <            if (searchFunction == null || result == null)
5091 <                throw new Error(NullFunctionMessage);
5092 <            int b = batch(), c;
5093 <            while (b > 1 && baseIndex != baseLimit && result.get() == null) {
5094 <                do {} while (!casPending(c = pending, c+1));
5095 <                new SearchEntriesTask<K,V,U>(this, b >>>= 1, true,
5096 <                                             searchFunction, result).fork();
5097 <            }
5098 <            Object v; U u;
5099 <            while (result.get() == null && (v = advance()) != null) {
5100 <                if ((u = searchFunction.apply(entryFor((K)nextKey, (V)v))) != null) {
5101 <                    result.compareAndSet(null, u);
5102 <                    break;
5087 >            final Fun<Entry<K,V>, ? extends U> searchFunction;
5088 >            final AtomicReference<U> result;
5089 >            if ((searchFunction = this.searchFunction) != null &&
5090 >                (result = this.result) != null) {
5091 >                for (int i = baseIndex, f, h; batch > 0 &&
5092 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5093 >                    if (result.get() != null)
5094 >                        return;
5095 >                    addToPendingCount(1);
5096 >                    new SearchEntriesTask<K,V,U>
5097 >                        (this, batch >>>= 1, baseLimit = h, f, tab,
5098 >                         searchFunction, result).fork();
5099 >                }
5100 >                while (result.get() == null) {
5101 >                    U u;
5102 >                    Node<K,V> p;
5103 >                    if ((p = advance()) == null) {
5104 >                        propagateCompletion();
5105 >                        break;
5106 >                    }
5107 >                    if ((u = searchFunction.apply(p)) != null) {
5108 >                        if (result.compareAndSet(null, u))
5109 >                            quietlyCompleteRoot();
5110 >                        return;
5111 >                    }
5112                  }
5113              }
5357            tryComplete();
5114          }
5359        public final U getRawResult() { return result.get(); }
5115      }
5116  
5117 +    @SuppressWarnings("serial")
5118      static final class SearchMappingsTask<K,V,U>
5119          extends BulkTask<K,V,U> {
5120          final BiFun<? super K, ? super V, ? extends U> searchFunction;
5121          final AtomicReference<U> result;
5122          SearchMappingsTask
5123 <            (ConcurrentHashMapV8<K,V> m,
5123 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5124               BiFun<? super K, ? super V, ? extends U> searchFunction,
5125               AtomicReference<U> result) {
5126 <            super(m);
5371 <            this.searchFunction = searchFunction; this.result = result;
5372 <        }
5373 <        SearchMappingsTask
5374 <            (BulkTask<K,V,?> p, int b, boolean split,
5375 <             BiFun<? super K, ? super V, ? extends U> searchFunction,
5376 <             AtomicReference<U> result) {
5377 <            super(p, b, split);
5126 >            super(p, b, i, f, t);
5127              this.searchFunction = searchFunction; this.result = result;
5128          }
5129 +        public final U getRawResult() { return result.get(); }
5130          public final void compute() {
5131 <            AtomicReference<U> result = this.result;
5132 <            final BiFun<? super K, ? super V, ? extends U> searchFunction =
5133 <                this.searchFunction;
5134 <            if (searchFunction == null || result == null)
5135 <                throw new Error(NullFunctionMessage);
5136 <            int b = batch(), c;
5137 <            while (b > 1 && baseIndex != baseLimit && result.get() == null) {
5138 <                do {} while (!casPending(c = pending, c+1));
5139 <                new SearchMappingsTask<K,V,U>(this, b >>>= 1, true,
5140 <                                              searchFunction, result).fork();
5141 <            }
5142 <            Object v; U u;
5143 <            while (result.get() == null && (v = advance()) != null) {
5144 <                if ((u = searchFunction.apply((K)nextKey, (V)v)) != null) {
5145 <                    result.compareAndSet(null, u);
5146 <                    break;
5131 >            final BiFun<? super K, ? super V, ? extends U> searchFunction;
5132 >            final AtomicReference<U> result;
5133 >            if ((searchFunction = this.searchFunction) != null &&
5134 >                (result = this.result) != null) {
5135 >                for (int i = baseIndex, f, h; batch > 0 &&
5136 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5137 >                    if (result.get() != null)
5138 >                        return;
5139 >                    addToPendingCount(1);
5140 >                    new SearchMappingsTask<K,V,U>
5141 >                        (this, batch >>>= 1, baseLimit = h, f, tab,
5142 >                         searchFunction, result).fork();
5143 >                }
5144 >                while (result.get() == null) {
5145 >                    U u;
5146 >                    Node<K,V> p;
5147 >                    if ((p = advance()) == null) {
5148 >                        propagateCompletion();
5149 >                        break;
5150 >                    }
5151 >                    if ((u = searchFunction.apply(p.key, p.val)) != null) {
5152 >                        if (result.compareAndSet(null, u))
5153 >                            quietlyCompleteRoot();
5154 >                        break;
5155 >                    }
5156                  }
5157              }
5399            tryComplete();
5158          }
5401        public final U getRawResult() { return result.get(); }
5159      }
5160  
5161 +    @SuppressWarnings("serial")
5162      static final class ReduceKeysTask<K,V>
5163          extends BulkTask<K,V,K> {
5164          final BiFun<? super K, ? super K, ? extends K> reducer;
5165          K result;
5166 <        ReduceKeysTask<K,V> sibling;
5166 >        ReduceKeysTask<K,V> rights, nextRight;
5167          ReduceKeysTask
5168 <            (ConcurrentHashMapV8<K,V> m,
5168 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5169 >             ReduceKeysTask<K,V> nextRight,
5170               BiFun<? super K, ? super K, ? extends K> reducer) {
5171 <            super(m);
5171 >            super(p, b, i, f, t); this.nextRight = nextRight;
5172              this.reducer = reducer;
5173          }
5174 <        ReduceKeysTask
5416 <            (BulkTask<K,V,?> p, int b, boolean split,
5417 <             BiFun<? super K, ? super K, ? extends K> reducer) {
5418 <            super(p, b, split);
5419 <            this.reducer = reducer;
5420 <        }
5421 <
5174 >        public final K getRawResult() { return result; }
5175          public final void compute() {
5176 <            ReduceKeysTask<K,V> t = this;
5177 <            final BiFun<? super K, ? super K, ? extends K> reducer =
5178 <                this.reducer;
5179 <            if (reducer == null)
5180 <                throw new Error(NullFunctionMessage);
5181 <            int b = batch();
5182 <            while (b > 1 && t.baseIndex != t.baseLimit) {
5183 <                b >>>= 1;
5184 <                t.pending = 1;
5185 <                ReduceKeysTask<K,V> rt =
5186 <                    new ReduceKeysTask<K,V>
5187 <                    (t, b, true, reducer);
5188 <                t = new ReduceKeysTask<K,V>
5189 <                    (t, b, false, reducer);
5190 <                t.sibling = rt;
5191 <                rt.sibling = t;
5192 <                rt.fork();
5193 <            }
5194 <            K r = null;
5195 <            while (t.advance() != null) {
5196 <                K u = (K)t.nextKey;
5197 <                r = (r == null) ? u : reducer.apply(r, u);
5198 <            }
5199 <            t.result = r;
5200 <            for (;;) {
5201 <                int c; BulkTask<K,V,?> par; ReduceKeysTask<K,V> s, p; K u;
5202 <                if ((par = t.parent) == null ||
5450 <                    !(par instanceof ReduceKeysTask)) {
5451 <                    t.quietlyComplete();
5452 <                    break;
5453 <                }
5454 <                else if ((c = (p = (ReduceKeysTask<K,V>)par).pending) == 0) {
5455 <                    if ((s = t.sibling) != null && (u = s.result) != null)
5456 <                        r = (r == null) ? u : reducer.apply(r, u);
5457 <                    (t = p).result = r;
5176 >            final BiFun<? super K, ? super K, ? extends K> reducer;
5177 >            if ((reducer = this.reducer) != null) {
5178 >                for (int i = baseIndex, f, h; batch > 0 &&
5179 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5180 >                    addToPendingCount(1);
5181 >                    (rights = new ReduceKeysTask<K,V>
5182 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
5183 >                      rights, reducer)).fork();
5184 >                }
5185 >                K r = null;
5186 >                for (Node<K,V> p; (p = advance()) != null; ) {
5187 >                    K u = p.key;
5188 >                    r = (r == null) ? u : u == null ? r : reducer.apply(r, u);
5189 >                }
5190 >                result = r;
5191 >                CountedCompleter<?> c;
5192 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5193 >                    @SuppressWarnings("unchecked") ReduceKeysTask<K,V>
5194 >                        t = (ReduceKeysTask<K,V>)c,
5195 >                        s = t.rights;
5196 >                    while (s != null) {
5197 >                        K tr, sr;
5198 >                        if ((sr = s.result) != null)
5199 >                            t.result = (((tr = t.result) == null) ? sr :
5200 >                                        reducer.apply(tr, sr));
5201 >                        s = t.rights = s.nextRight;
5202 >                    }
5203                  }
5459                else if (p.casPending(c, 0))
5460                    break;
5204              }
5205          }
5463        public final K getRawResult() { return result; }
5206      }
5207  
5208 +    @SuppressWarnings("serial")
5209      static final class ReduceValuesTask<K,V>
5210          extends BulkTask<K,V,V> {
5211          final BiFun<? super V, ? super V, ? extends V> reducer;
5212          V result;
5213 <        ReduceValuesTask<K,V> sibling;
5471 <        ReduceValuesTask
5472 <            (ConcurrentHashMapV8<K,V> m,
5473 <             BiFun<? super V, ? super V, ? extends V> reducer) {
5474 <            super(m);
5475 <            this.reducer = reducer;
5476 <        }
5213 >        ReduceValuesTask<K,V> rights, nextRight;
5214          ReduceValuesTask
5215 <            (BulkTask<K,V,?> p, int b, boolean split,
5215 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5216 >             ReduceValuesTask<K,V> nextRight,
5217               BiFun<? super V, ? super V, ? extends V> reducer) {
5218 <            super(p, b, split);
5218 >            super(p, b, i, f, t); this.nextRight = nextRight;
5219              this.reducer = reducer;
5220          }
5221 <
5221 >        public final V getRawResult() { return result; }
5222          public final void compute() {
5223 <            ReduceValuesTask<K,V> t = this;
5224 <            final BiFun<? super V, ? super V, ? extends V> reducer =
5225 <                this.reducer;
5226 <            if (reducer == null)
5227 <                throw new Error(NullFunctionMessage);
5228 <            int b = batch();
5229 <            while (b > 1 && t.baseIndex != t.baseLimit) {
5230 <                b >>>= 1;
5231 <                t.pending = 1;
5232 <                ReduceValuesTask<K,V> rt =
5233 <                    new ReduceValuesTask<K,V>
5234 <                    (t, b, true, reducer);
5235 <                t = new ReduceValuesTask<K,V>
5236 <                    (t, b, false, reducer);
5237 <                t.sibling = rt;
5238 <                rt.sibling = t;
5239 <                rt.fork();
5240 <            }
5241 <            V r = null;
5242 <            Object v;
5243 <            while ((v = t.advance()) != null) {
5244 <                V u = (V)v;
5245 <                r = (r == null) ? u : reducer.apply(r, u);
5246 <            }
5247 <            t.result = r;
5248 <            for (;;) {
5249 <                int c; BulkTask<K,V,?> par; ReduceValuesTask<K,V> s, p; V u;
5512 <                if ((par = t.parent) == null ||
5513 <                    !(par instanceof ReduceValuesTask)) {
5514 <                    t.quietlyComplete();
5515 <                    break;
5516 <                }
5517 <                else if ((c = (p = (ReduceValuesTask<K,V>)par).pending) == 0) {
5518 <                    if ((s = t.sibling) != null && (u = s.result) != null)
5519 <                        r = (r == null) ? u : reducer.apply(r, u);
5520 <                    (t = p).result = r;
5223 >            final BiFun<? super V, ? super V, ? extends V> reducer;
5224 >            if ((reducer = this.reducer) != null) {
5225 >                for (int i = baseIndex, f, h; batch > 0 &&
5226 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5227 >                    addToPendingCount(1);
5228 >                    (rights = new ReduceValuesTask<K,V>
5229 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
5230 >                      rights, reducer)).fork();
5231 >                }
5232 >                V r = null;
5233 >                for (Node<K,V> p; (p = advance()) != null; ) {
5234 >                    V v = p.val;
5235 >                    r = (r == null) ? v : reducer.apply(r, v);
5236 >                }
5237 >                result = r;
5238 >                CountedCompleter<?> c;
5239 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5240 >                    @SuppressWarnings("unchecked") ReduceValuesTask<K,V>
5241 >                        t = (ReduceValuesTask<K,V>)c,
5242 >                        s = t.rights;
5243 >                    while (s != null) {
5244 >                        V tr, sr;
5245 >                        if ((sr = s.result) != null)
5246 >                            t.result = (((tr = t.result) == null) ? sr :
5247 >                                        reducer.apply(tr, sr));
5248 >                        s = t.rights = s.nextRight;
5249 >                    }
5250                  }
5522                else if (p.casPending(c, 0))
5523                    break;
5251              }
5252          }
5526        public final V getRawResult() { return result; }
5253      }
5254  
5255 +    @SuppressWarnings("serial")
5256      static final class ReduceEntriesTask<K,V>
5257          extends BulkTask<K,V,Map.Entry<K,V>> {
5258          final BiFun<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer;
5259          Map.Entry<K,V> result;
5260 <        ReduceEntriesTask<K,V> sibling;
5260 >        ReduceEntriesTask<K,V> rights, nextRight;
5261          ReduceEntriesTask
5262 <            (ConcurrentHashMapV8<K,V> m,
5262 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5263 >             ReduceEntriesTask<K,V> nextRight,
5264               BiFun<Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer) {
5265 <            super(m);
5538 <            this.reducer = reducer;
5539 <        }
5540 <        ReduceEntriesTask
5541 <            (BulkTask<K,V,?> p, int b, boolean split,
5542 <             BiFun<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer) {
5543 <            super(p, b, split);
5265 >            super(p, b, i, f, t); this.nextRight = nextRight;
5266              this.reducer = reducer;
5267          }
5268 <
5268 >        public final Map.Entry<K,V> getRawResult() { return result; }
5269          public final void compute() {
5270 <            ReduceEntriesTask<K,V> t = this;
5271 <            final BiFun<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer =
5272 <                this.reducer;
5273 <            if (reducer == null)
5274 <                throw new Error(NullFunctionMessage);
5275 <            int b = batch();
5276 <            while (b > 1 && t.baseIndex != t.baseLimit) {
5277 <                b >>>= 1;
5278 <                t.pending = 1;
5279 <                ReduceEntriesTask<K,V> rt =
5280 <                    new ReduceEntriesTask<K,V>
5281 <                    (t, b, true, reducer);
5282 <                t = new ReduceEntriesTask<K,V>
5283 <                    (t, b, false, reducer);
5284 <                t.sibling = rt;
5285 <                rt.sibling = t;
5286 <                rt.fork();
5287 <            }
5288 <            Map.Entry<K,V> r = null;
5289 <            Object v;
5290 <            while ((v = t.advance()) != null) {
5291 <                Map.Entry<K,V> u = entryFor((K)t.nextKey, (V)v);
5292 <                r = (r == null) ? u : reducer.apply(r, u);
5293 <            }
5294 <            t.result = r;
5573 <            for (;;) {
5574 <                int c; BulkTask<K,V,?> par; ReduceEntriesTask<K,V> s, p;
5575 <                Map.Entry<K,V> u;
5576 <                if ((par = t.parent) == null ||
5577 <                    !(par instanceof ReduceEntriesTask)) {
5578 <                    t.quietlyComplete();
5579 <                    break;
5580 <                }
5581 <                else if ((c = (p = (ReduceEntriesTask<K,V>)par).pending) == 0) {
5582 <                    if ((s = t.sibling) != null && (u = s.result) != null)
5583 <                        r = (r == null) ? u : reducer.apply(r, u);
5584 <                    (t = p).result = r;
5270 >            final BiFun<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer;
5271 >            if ((reducer = this.reducer) != null) {
5272 >                for (int i = baseIndex, f, h; batch > 0 &&
5273 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5274 >                    addToPendingCount(1);
5275 >                    (rights = new ReduceEntriesTask<K,V>
5276 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
5277 >                      rights, reducer)).fork();
5278 >                }
5279 >                Map.Entry<K,V> r = null;
5280 >                for (Node<K,V> p; (p = advance()) != null; )
5281 >                    r = (r == null) ? p : reducer.apply(r, p);
5282 >                result = r;
5283 >                CountedCompleter<?> c;
5284 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5285 >                    @SuppressWarnings("unchecked") ReduceEntriesTask<K,V>
5286 >                        t = (ReduceEntriesTask<K,V>)c,
5287 >                        s = t.rights;
5288 >                    while (s != null) {
5289 >                        Map.Entry<K,V> tr, sr;
5290 >                        if ((sr = s.result) != null)
5291 >                            t.result = (((tr = t.result) == null) ? sr :
5292 >                                        reducer.apply(tr, sr));
5293 >                        s = t.rights = s.nextRight;
5294 >                    }
5295                  }
5586                else if (p.casPending(c, 0))
5587                    break;
5296              }
5297          }
5590        public final Map.Entry<K,V> getRawResult() { return result; }
5298      }
5299  
5300 +    @SuppressWarnings("serial")
5301      static final class MapReduceKeysTask<K,V,U>
5302          extends BulkTask<K,V,U> {
5303          final Fun<? super K, ? extends U> transformer;
5304          final BiFun<? super U, ? super U, ? extends U> reducer;
5305          U result;
5306 <        MapReduceKeysTask<K,V,U> sibling;
5599 <        MapReduceKeysTask
5600 <            (ConcurrentHashMapV8<K,V> m,
5601 <             Fun<? super K, ? extends U> transformer,
5602 <             BiFun<? super U, ? super U, ? extends U> reducer) {
5603 <            super(m);
5604 <            this.transformer = transformer;
5605 <            this.reducer = reducer;
5606 <        }
5306 >        MapReduceKeysTask<K,V,U> rights, nextRight;
5307          MapReduceKeysTask
5308 <            (BulkTask<K,V,?> p, int b, boolean split,
5308 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5309 >             MapReduceKeysTask<K,V,U> nextRight,
5310               Fun<? super K, ? extends U> transformer,
5311               BiFun<? super U, ? super U, ? extends U> reducer) {
5312 <            super(p, b, split);
5312 >            super(p, b, i, f, t); this.nextRight = nextRight;
5313              this.transformer = transformer;
5314              this.reducer = reducer;
5315          }
5316 +        public final U getRawResult() { return result; }
5317          public final void compute() {
5318 <            MapReduceKeysTask<K,V,U> t = this;
5319 <            final Fun<? super K, ? extends U> transformer =
5320 <                this.transformer;
5321 <            final BiFun<? super U, ? super U, ? extends U> reducer =
5322 <                this.reducer;
5323 <            if (transformer == null || reducer == null)
5324 <                throw new Error(NullFunctionMessage);
5325 <            int b = batch();
5326 <            while (b > 1 && t.baseIndex != t.baseLimit) {
5327 <                b >>>= 1;
5328 <                t.pending = 1;
5329 <                MapReduceKeysTask<K,V,U> rt =
5330 <                    new MapReduceKeysTask<K,V,U>
5331 <                    (t, b, true, transformer, reducer);
5332 <                t = new MapReduceKeysTask<K,V,U>
5631 <                    (t, b, false, transformer, reducer);
5632 <                t.sibling = rt;
5633 <                rt.sibling = t;
5634 <                rt.fork();
5635 <            }
5636 <            U r = null, u;
5637 <            while (t.advance() != null) {
5638 <                if ((u = transformer.apply((K)t.nextKey)) != null)
5639 <                    r = (r == null) ? u : reducer.apply(r, u);
5640 <            }
5641 <            t.result = r;
5642 <            for (;;) {
5643 <                int c; BulkTask<K,V,?> par; MapReduceKeysTask<K,V,U> s, p;
5644 <                if ((par = t.parent) == null ||
5645 <                    !(par instanceof MapReduceKeysTask)) {
5646 <                    t.quietlyComplete();
5647 <                    break;
5648 <                }
5649 <                else if ((c = (p = (MapReduceKeysTask<K,V,U>)par).pending) == 0) {
5650 <                    if ((s = t.sibling) != null && (u = s.result) != null)
5318 >            final Fun<? super K, ? extends U> transformer;
5319 >            final BiFun<? super U, ? super U, ? extends U> reducer;
5320 >            if ((transformer = this.transformer) != null &&
5321 >                (reducer = this.reducer) != null) {
5322 >                for (int i = baseIndex, f, h; batch > 0 &&
5323 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5324 >                    addToPendingCount(1);
5325 >                    (rights = new MapReduceKeysTask<K,V,U>
5326 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
5327 >                      rights, transformer, reducer)).fork();
5328 >                }
5329 >                U r = null;
5330 >                for (Node<K,V> p; (p = advance()) != null; ) {
5331 >                    U u;
5332 >                    if ((u = transformer.apply(p.key)) != null)
5333                          r = (r == null) ? u : reducer.apply(r, u);
5652                    (t = p).result = r;
5334                  }
5335 <                else if (p.casPending(c, 0))
5336 <                    break;
5335 >                result = r;
5336 >                CountedCompleter<?> c;
5337 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5338 >                    @SuppressWarnings("unchecked") MapReduceKeysTask<K,V,U>
5339 >                        t = (MapReduceKeysTask<K,V,U>)c,
5340 >                        s = t.rights;
5341 >                    while (s != null) {
5342 >                        U tr, sr;
5343 >                        if ((sr = s.result) != null)
5344 >                            t.result = (((tr = t.result) == null) ? sr :
5345 >                                        reducer.apply(tr, sr));
5346 >                        s = t.rights = s.nextRight;
5347 >                    }
5348 >                }
5349              }
5350          }
5658        public final U getRawResult() { return result; }
5351      }
5352  
5353 +    @SuppressWarnings("serial")
5354      static final class MapReduceValuesTask<K,V,U>
5355          extends BulkTask<K,V,U> {
5356          final Fun<? super V, ? extends U> transformer;
5357          final BiFun<? super U, ? super U, ? extends U> reducer;
5358          U result;
5359 <        MapReduceValuesTask<K,V,U> sibling;
5359 >        MapReduceValuesTask<K,V,U> rights, nextRight;
5360          MapReduceValuesTask
5361 <            (ConcurrentHashMapV8<K,V> m,
5361 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5362 >             MapReduceValuesTask<K,V,U> nextRight,
5363               Fun<? super V, ? extends U> transformer,
5364               BiFun<? super U, ? super U, ? extends U> reducer) {
5365 <            super(m);
5672 <            this.transformer = transformer;
5673 <            this.reducer = reducer;
5674 <        }
5675 <        MapReduceValuesTask
5676 <            (BulkTask<K,V,?> p, int b, boolean split,
5677 <             Fun<? super V, ? extends U> transformer,
5678 <             BiFun<? super U, ? super U, ? extends U> reducer) {
5679 <            super(p, b, split);
5365 >            super(p, b, i, f, t); this.nextRight = nextRight;
5366              this.transformer = transformer;
5367              this.reducer = reducer;
5368          }
5369 +        public final U getRawResult() { return result; }
5370          public final void compute() {
5371 <            MapReduceValuesTask<K,V,U> t = this;
5372 <            final Fun<? super V, ? extends U> transformer =
5373 <                this.transformer;
5374 <            final BiFun<? super U, ? super U, ? extends U> reducer =
5375 <                this.reducer;
5376 <            if (transformer == null || reducer == null)
5377 <                throw new Error(NullFunctionMessage);
5378 <            int b = batch();
5379 <            while (b > 1 && t.baseIndex != t.baseLimit) {
5380 <                b >>>= 1;
5381 <                t.pending = 1;
5382 <                MapReduceValuesTask<K,V,U> rt =
5383 <                    new MapReduceValuesTask<K,V,U>
5384 <                    (t, b, true, transformer, reducer);
5385 <                t = new MapReduceValuesTask<K,V,U>
5699 <                    (t, b, false, transformer, reducer);
5700 <                t.sibling = rt;
5701 <                rt.sibling = t;
5702 <                rt.fork();
5703 <            }
5704 <            U r = null, u;
5705 <            Object v;
5706 <            while ((v = t.advance()) != null) {
5707 <                if ((u = transformer.apply((V)v)) != null)
5708 <                    r = (r == null) ? u : reducer.apply(r, u);
5709 <            }
5710 <            t.result = r;
5711 <            for (;;) {
5712 <                int c; BulkTask<K,V,?> par; MapReduceValuesTask<K,V,U> s, p;
5713 <                if ((par = t.parent) == null ||
5714 <                    !(par instanceof MapReduceValuesTask)) {
5715 <                    t.quietlyComplete();
5716 <                    break;
5717 <                }
5718 <                else if ((c = (p = (MapReduceValuesTask<K,V,U>)par).pending) == 0) {
5719 <                    if ((s = t.sibling) != null && (u = s.result) != null)
5371 >            final Fun<? super V, ? extends U> transformer;
5372 >            final BiFun<? super U, ? super U, ? extends U> reducer;
5373 >            if ((transformer = this.transformer) != null &&
5374 >                (reducer = this.reducer) != null) {
5375 >                for (int i = baseIndex, f, h; batch > 0 &&
5376 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5377 >                    addToPendingCount(1);
5378 >                    (rights = new MapReduceValuesTask<K,V,U>
5379 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
5380 >                      rights, transformer, reducer)).fork();
5381 >                }
5382 >                U r = null;
5383 >                for (Node<K,V> p; (p = advance()) != null; ) {
5384 >                    U u;
5385 >                    if ((u = transformer.apply(p.val)) != null)
5386                          r = (r == null) ? u : reducer.apply(r, u);
5721                    (t = p).result = r;
5387                  }
5388 <                else if (p.casPending(c, 0))
5389 <                    break;
5388 >                result = r;
5389 >                CountedCompleter<?> c;
5390 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5391 >                    @SuppressWarnings("unchecked") MapReduceValuesTask<K,V,U>
5392 >                        t = (MapReduceValuesTask<K,V,U>)c,
5393 >                        s = t.rights;
5394 >                    while (s != null) {
5395 >                        U tr, sr;
5396 >                        if ((sr = s.result) != null)
5397 >                            t.result = (((tr = t.result) == null) ? sr :
5398 >                                        reducer.apply(tr, sr));
5399 >                        s = t.rights = s.nextRight;
5400 >                    }
5401 >                }
5402              }
5403          }
5727        public final U getRawResult() { return result; }
5404      }
5405  
5406 +    @SuppressWarnings("serial")
5407      static final class MapReduceEntriesTask<K,V,U>
5408          extends BulkTask<K,V,U> {
5409          final Fun<Map.Entry<K,V>, ? extends U> transformer;
5410          final BiFun<? super U, ? super U, ? extends U> reducer;
5411          U result;
5412 <        MapReduceEntriesTask<K,V,U> sibling;
5412 >        MapReduceEntriesTask<K,V,U> rights, nextRight;
5413          MapReduceEntriesTask
5414 <            (ConcurrentHashMapV8<K,V> m,
5414 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5415 >             MapReduceEntriesTask<K,V,U> nextRight,
5416               Fun<Map.Entry<K,V>, ? extends U> transformer,
5417               BiFun<? super U, ? super U, ? extends U> reducer) {
5418 <            super(m);
5741 <            this.transformer = transformer;
5742 <            this.reducer = reducer;
5743 <        }
5744 <        MapReduceEntriesTask
5745 <            (BulkTask<K,V,?> p, int b, boolean split,
5746 <             Fun<Map.Entry<K,V>, ? extends U> transformer,
5747 <             BiFun<? super U, ? super U, ? extends U> reducer) {
5748 <            super(p, b, split);
5418 >            super(p, b, i, f, t); this.nextRight = nextRight;
5419              this.transformer = transformer;
5420              this.reducer = reducer;
5421          }
5422 +        public final U getRawResult() { return result; }
5423          public final void compute() {
5424 <            MapReduceEntriesTask<K,V,U> t = this;
5425 <            final Fun<Map.Entry<K,V>, ? extends U> transformer =
5426 <                this.transformer;
5427 <            final BiFun<? super U, ? super U, ? extends U> reducer =
5428 <                this.reducer;
5429 <            if (transformer == null || reducer == null)
5430 <                throw new Error(NullFunctionMessage);
5431 <            int b = batch();
5432 <            while (b > 1 && t.baseIndex != t.baseLimit) {
5433 <                b >>>= 1;
5434 <                t.pending = 1;
5435 <                MapReduceEntriesTask<K,V,U> rt =
5436 <                    new MapReduceEntriesTask<K,V,U>
5437 <                    (t, b, true, transformer, reducer);
5438 <                t = new MapReduceEntriesTask<K,V,U>
5768 <                    (t, b, false, transformer, reducer);
5769 <                t.sibling = rt;
5770 <                rt.sibling = t;
5771 <                rt.fork();
5772 <            }
5773 <            U r = null, u;
5774 <            Object v;
5775 <            while ((v = t.advance()) != null) {
5776 <                if ((u = transformer.apply(entryFor((K)t.nextKey, (V)v))) != null)
5777 <                    r = (r == null) ? u : reducer.apply(r, u);
5778 <            }
5779 <            t.result = r;
5780 <            for (;;) {
5781 <                int c; BulkTask<K,V,?> par; MapReduceEntriesTask<K,V,U> s, p;
5782 <                if ((par = t.parent) == null ||
5783 <                    !(par instanceof MapReduceEntriesTask)) {
5784 <                    t.quietlyComplete();
5785 <                    break;
5786 <                }
5787 <                else if ((c = (p = (MapReduceEntriesTask<K,V,U>)par).pending) == 0) {
5788 <                    if ((s = t.sibling) != null && (u = s.result) != null)
5424 >            final Fun<Map.Entry<K,V>, ? extends U> transformer;
5425 >            final BiFun<? super U, ? super U, ? extends U> reducer;
5426 >            if ((transformer = this.transformer) != null &&
5427 >                (reducer = this.reducer) != null) {
5428 >                for (int i = baseIndex, f, h; batch > 0 &&
5429 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5430 >                    addToPendingCount(1);
5431 >                    (rights = new MapReduceEntriesTask<K,V,U>
5432 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
5433 >                      rights, transformer, reducer)).fork();
5434 >                }
5435 >                U r = null;
5436 >                for (Node<K,V> p; (p = advance()) != null; ) {
5437 >                    U u;
5438 >                    if ((u = transformer.apply(p)) != null)
5439                          r = (r == null) ? u : reducer.apply(r, u);
5790                    (t = p).result = r;
5440                  }
5441 <                else if (p.casPending(c, 0))
5442 <                    break;
5441 >                result = r;
5442 >                CountedCompleter<?> c;
5443 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5444 >                    @SuppressWarnings("unchecked") MapReduceEntriesTask<K,V,U>
5445 >                        t = (MapReduceEntriesTask<K,V,U>)c,
5446 >                        s = t.rights;
5447 >                    while (s != null) {
5448 >                        U tr, sr;
5449 >                        if ((sr = s.result) != null)
5450 >                            t.result = (((tr = t.result) == null) ? sr :
5451 >                                        reducer.apply(tr, sr));
5452 >                        s = t.rights = s.nextRight;
5453 >                    }
5454 >                }
5455              }
5456          }
5796        public final U getRawResult() { return result; }
5457      }
5458  
5459 +    @SuppressWarnings("serial")
5460      static final class MapReduceMappingsTask<K,V,U>
5461          extends BulkTask<K,V,U> {
5462          final BiFun<? super K, ? super V, ? extends U> transformer;
5463          final BiFun<? super U, ? super U, ? extends U> reducer;
5464          U result;
5465 <        MapReduceMappingsTask<K,V,U> sibling;
5805 <        MapReduceMappingsTask
5806 <            (ConcurrentHashMapV8<K,V> m,
5807 <             BiFun<? super K, ? super V, ? extends U> transformer,
5808 <             BiFun<? super U, ? super U, ? extends U> reducer) {
5809 <            super(m);
5810 <            this.transformer = transformer;
5811 <            this.reducer = reducer;
5812 <        }
5465 >        MapReduceMappingsTask<K,V,U> rights, nextRight;
5466          MapReduceMappingsTask
5467 <            (BulkTask<K,V,?> p, int b, boolean split,
5467 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5468 >             MapReduceMappingsTask<K,V,U> nextRight,
5469               BiFun<? super K, ? super V, ? extends U> transformer,
5470               BiFun<? super U, ? super U, ? extends U> reducer) {
5471 <            super(p, b, split);
5471 >            super(p, b, i, f, t); this.nextRight = nextRight;
5472              this.transformer = transformer;
5473              this.reducer = reducer;
5474          }
5475 +        public final U getRawResult() { return result; }
5476          public final void compute() {
5477 <            MapReduceMappingsTask<K,V,U> t = this;
5478 <            final BiFun<? super K, ? super V, ? extends U> transformer =
5479 <                this.transformer;
5480 <            final BiFun<? super U, ? super U, ? extends U> reducer =
5481 <                this.reducer;
5482 <            if (transformer == null || reducer == null)
5483 <                throw new Error(NullFunctionMessage);
5484 <            int b = batch();
5485 <            while (b > 1 && t.baseIndex != t.baseLimit) {
5486 <                b >>>= 1;
5487 <                t.pending = 1;
5488 <                MapReduceMappingsTask<K,V,U> rt =
5489 <                    new MapReduceMappingsTask<K,V,U>
5490 <                    (t, b, true, transformer, reducer);
5491 <                t = new MapReduceMappingsTask<K,V,U>
5837 <                    (t, b, false, transformer, reducer);
5838 <                t.sibling = rt;
5839 <                rt.sibling = t;
5840 <                rt.fork();
5841 <            }
5842 <            U r = null, u;
5843 <            Object v;
5844 <            while ((v = t.advance()) != null) {
5845 <                if ((u = transformer.apply((K)t.nextKey, (V)v)) != null)
5846 <                    r = (r == null) ? u : reducer.apply(r, u);
5847 <            }
5848 <            for (;;) {
5849 <                int c; BulkTask<K,V,?> par; MapReduceMappingsTask<K,V,U> s, p;
5850 <                if ((par = t.parent) == null ||
5851 <                    !(par instanceof MapReduceMappingsTask)) {
5852 <                    t.quietlyComplete();
5853 <                    break;
5854 <                }
5855 <                else if ((c = (p = (MapReduceMappingsTask<K,V,U>)par).pending) == 0) {
5856 <                    if ((s = t.sibling) != null && (u = s.result) != null)
5477 >            final BiFun<? super K, ? super V, ? extends U> transformer;
5478 >            final BiFun<? super U, ? super U, ? extends U> reducer;
5479 >            if ((transformer = this.transformer) != null &&
5480 >                (reducer = this.reducer) != null) {
5481 >                for (int i = baseIndex, f, h; batch > 0 &&
5482 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5483 >                    addToPendingCount(1);
5484 >                    (rights = new MapReduceMappingsTask<K,V,U>
5485 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
5486 >                      rights, transformer, reducer)).fork();
5487 >                }
5488 >                U r = null;
5489 >                for (Node<K,V> p; (p = advance()) != null; ) {
5490 >                    U u;
5491 >                    if ((u = transformer.apply(p.key, p.val)) != null)
5492                          r = (r == null) ? u : reducer.apply(r, u);
5858                    (t = p).result = r;
5493                  }
5494 <                else if (p.casPending(c, 0))
5495 <                    break;
5494 >                result = r;
5495 >                CountedCompleter<?> c;
5496 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5497 >                    @SuppressWarnings("unchecked") MapReduceMappingsTask<K,V,U>
5498 >                        t = (MapReduceMappingsTask<K,V,U>)c,
5499 >                        s = t.rights;
5500 >                    while (s != null) {
5501 >                        U tr, sr;
5502 >                        if ((sr = s.result) != null)
5503 >                            t.result = (((tr = t.result) == null) ? sr :
5504 >                                        reducer.apply(tr, sr));
5505 >                        s = t.rights = s.nextRight;
5506 >                    }
5507 >                }
5508              }
5509          }
5864        public final U getRawResult() { return result; }
5510      }
5511  
5512 +    @SuppressWarnings("serial")
5513      static final class MapReduceKeysToDoubleTask<K,V>
5514          extends BulkTask<K,V,Double> {
5515          final ObjectToDouble<? super K> transformer;
5516          final DoubleByDoubleToDouble reducer;
5517          final double basis;
5518          double result;
5519 <        MapReduceKeysToDoubleTask<K,V> sibling;
5874 <        MapReduceKeysToDoubleTask
5875 <            (ConcurrentHashMapV8<K,V> m,
5876 <             ObjectToDouble<? super K> transformer,
5877 <             double basis,
5878 <             DoubleByDoubleToDouble reducer) {
5879 <            super(m);
5880 <            this.transformer = transformer;
5881 <            this.basis = basis; this.reducer = reducer;
5882 <        }
5519 >        MapReduceKeysToDoubleTask<K,V> rights, nextRight;
5520          MapReduceKeysToDoubleTask
5521 <            (BulkTask<K,V,?> p, int b, boolean split,
5521 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5522 >             MapReduceKeysToDoubleTask<K,V> nextRight,
5523               ObjectToDouble<? super K> transformer,
5524               double basis,
5525               DoubleByDoubleToDouble reducer) {
5526 <            super(p, b, split);
5526 >            super(p, b, i, f, t); this.nextRight = nextRight;
5527              this.transformer = transformer;
5528              this.basis = basis; this.reducer = reducer;
5529          }
5530 +        public final Double getRawResult() { return result; }
5531          public final void compute() {
5532 <            MapReduceKeysToDoubleTask<K,V> t = this;
5533 <            final ObjectToDouble<? super K> transformer =
5534 <                this.transformer;
5535 <            final DoubleByDoubleToDouble reducer = this.reducer;
5536 <            if (transformer == null || reducer == null)
5537 <                throw new Error(NullFunctionMessage);
5538 <            final double id = this.basis;
5539 <            int b = batch();
5540 <            while (b > 1 && t.baseIndex != t.baseLimit) {
5541 <                b >>>= 1;
5542 <                t.pending = 1;
5543 <                MapReduceKeysToDoubleTask<K,V> rt =
5544 <                    new MapReduceKeysToDoubleTask<K,V>
5545 <                    (t, b, true, transformer, id, reducer);
5546 <                t = new MapReduceKeysToDoubleTask<K,V>
5547 <                    (t, b, false, transformer, id, reducer);
5548 <                t.sibling = rt;
5549 <                rt.sibling = t;
5550 <                rt.fork();
5551 <            }
5552 <            double r = id;
5553 <            while (t.advance() != null)
5554 <                r = reducer.apply(r, transformer.apply((K)t.nextKey));
5555 <            t.result = r;
5917 <            for (;;) {
5918 <                int c; BulkTask<K,V,?> par; MapReduceKeysToDoubleTask<K,V> s, p;
5919 <                if ((par = t.parent) == null ||
5920 <                    !(par instanceof MapReduceKeysToDoubleTask)) {
5921 <                    t.quietlyComplete();
5922 <                    break;
5923 <                }
5924 <                else if ((c = (p = (MapReduceKeysToDoubleTask<K,V>)par).pending) == 0) {
5925 <                    if ((s = t.sibling) != null)
5926 <                        r = reducer.apply(r, s.result);
5927 <                    (t = p).result = r;
5532 >            final ObjectToDouble<? super K> transformer;
5533 >            final DoubleByDoubleToDouble reducer;
5534 >            if ((transformer = this.transformer) != null &&
5535 >                (reducer = this.reducer) != null) {
5536 >                double r = this.basis;
5537 >                for (int i = baseIndex, f, h; batch > 0 &&
5538 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5539 >                    addToPendingCount(1);
5540 >                    (rights = new MapReduceKeysToDoubleTask<K,V>
5541 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
5542 >                      rights, transformer, r, reducer)).fork();
5543 >                }
5544 >                for (Node<K,V> p; (p = advance()) != null; )
5545 >                    r = reducer.apply(r, transformer.apply(p.key));
5546 >                result = r;
5547 >                CountedCompleter<?> c;
5548 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5549 >                    @SuppressWarnings("unchecked") MapReduceKeysToDoubleTask<K,V>
5550 >                        t = (MapReduceKeysToDoubleTask<K,V>)c,
5551 >                        s = t.rights;
5552 >                    while (s != null) {
5553 >                        t.result = reducer.apply(t.result, s.result);
5554 >                        s = t.rights = s.nextRight;
5555 >                    }
5556                  }
5929                else if (p.casPending(c, 0))
5930                    break;
5557              }
5558          }
5933        public final Double getRawResult() { return result; }
5559      }
5560  
5561 +    @SuppressWarnings("serial")
5562      static final class MapReduceValuesToDoubleTask<K,V>
5563          extends BulkTask<K,V,Double> {
5564          final ObjectToDouble<? super V> transformer;
5565          final DoubleByDoubleToDouble reducer;
5566          final double basis;
5567          double result;
5568 <        MapReduceValuesToDoubleTask<K,V> sibling;
5568 >        MapReduceValuesToDoubleTask<K,V> rights, nextRight;
5569          MapReduceValuesToDoubleTask
5570 <            (ConcurrentHashMapV8<K,V> m,
5570 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5571 >             MapReduceValuesToDoubleTask<K,V> nextRight,
5572               ObjectToDouble<? super V> transformer,
5573               double basis,
5574               DoubleByDoubleToDouble reducer) {
5575 <            super(m);
5949 <            this.transformer = transformer;
5950 <            this.basis = basis; this.reducer = reducer;
5951 <        }
5952 <        MapReduceValuesToDoubleTask
5953 <            (BulkTask<K,V,?> p, int b, boolean split,
5954 <             ObjectToDouble<? super V> transformer,
5955 <             double basis,
5956 <             DoubleByDoubleToDouble reducer) {
5957 <            super(p, b, split);
5575 >            super(p, b, i, f, t); this.nextRight = nextRight;
5576              this.transformer = transformer;
5577              this.basis = basis; this.reducer = reducer;
5578          }
5579 +        public final Double getRawResult() { return result; }
5580          public final void compute() {
5581 <            MapReduceValuesToDoubleTask<K,V> t = this;
5582 <            final ObjectToDouble<? super V> transformer =
5583 <                this.transformer;
5584 <            final DoubleByDoubleToDouble reducer = this.reducer;
5585 <            if (transformer == null || reducer == null)
5586 <                throw new Error(NullFunctionMessage);
5587 <            final double id = this.basis;
5588 <            int b = batch();
5589 <            while (b > 1 && t.baseIndex != t.baseLimit) {
5590 <                b >>>= 1;
5591 <                t.pending = 1;
5592 <                MapReduceValuesToDoubleTask<K,V> rt =
5593 <                    new MapReduceValuesToDoubleTask<K,V>
5594 <                    (t, b, true, transformer, id, reducer);
5595 <                t = new MapReduceValuesToDoubleTask<K,V>
5596 <                    (t, b, false, transformer, id, reducer);
5597 <                t.sibling = rt;
5598 <                rt.sibling = t;
5599 <                rt.fork();
5600 <            }
5601 <            double r = id;
5602 <            Object v;
5603 <            while ((v = t.advance()) != null)
5604 <                r = reducer.apply(r, transformer.apply((V)v));
5986 <            t.result = r;
5987 <            for (;;) {
5988 <                int c; BulkTask<K,V,?> par; MapReduceValuesToDoubleTask<K,V> s, p;
5989 <                if ((par = t.parent) == null ||
5990 <                    !(par instanceof MapReduceValuesToDoubleTask)) {
5991 <                    t.quietlyComplete();
5992 <                    break;
5993 <                }
5994 <                else if ((c = (p = (MapReduceValuesToDoubleTask<K,V>)par).pending) == 0) {
5995 <                    if ((s = t.sibling) != null)
5996 <                        r = reducer.apply(r, s.result);
5997 <                    (t = p).result = r;
5581 >            final ObjectToDouble<? super V> transformer;
5582 >            final DoubleByDoubleToDouble reducer;
5583 >            if ((transformer = this.transformer) != null &&
5584 >                (reducer = this.reducer) != null) {
5585 >                double r = this.basis;
5586 >                for (int i = baseIndex, f, h; batch > 0 &&
5587 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5588 >                    addToPendingCount(1);
5589 >                    (rights = new MapReduceValuesToDoubleTask<K,V>
5590 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
5591 >                      rights, transformer, r, reducer)).fork();
5592 >                }
5593 >                for (Node<K,V> p; (p = advance()) != null; )
5594 >                    r = reducer.apply(r, transformer.apply(p.val));
5595 >                result = r;
5596 >                CountedCompleter<?> c;
5597 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5598 >                    @SuppressWarnings("unchecked") MapReduceValuesToDoubleTask<K,V>
5599 >                        t = (MapReduceValuesToDoubleTask<K,V>)c,
5600 >                        s = t.rights;
5601 >                    while (s != null) {
5602 >                        t.result = reducer.apply(t.result, s.result);
5603 >                        s = t.rights = s.nextRight;
5604 >                    }
5605                  }
5999                else if (p.casPending(c, 0))
6000                    break;
5606              }
5607          }
6003        public final Double getRawResult() { return result; }
5608      }
5609  
5610 +    @SuppressWarnings("serial")
5611      static final class MapReduceEntriesToDoubleTask<K,V>
5612          extends BulkTask<K,V,Double> {
5613          final ObjectToDouble<Map.Entry<K,V>> transformer;
5614          final DoubleByDoubleToDouble reducer;
5615          final double basis;
5616          double result;
5617 <        MapReduceEntriesToDoubleTask<K,V> sibling;
5617 >        MapReduceEntriesToDoubleTask<K,V> rights, nextRight;
5618          MapReduceEntriesToDoubleTask
5619 <            (ConcurrentHashMapV8<K,V> m,
5619 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5620 >             MapReduceEntriesToDoubleTask<K,V> nextRight,
5621               ObjectToDouble<Map.Entry<K,V>> transformer,
5622               double basis,
5623               DoubleByDoubleToDouble reducer) {
5624 <            super(m);
6019 <            this.transformer = transformer;
6020 <            this.basis = basis; this.reducer = reducer;
6021 <        }
6022 <        MapReduceEntriesToDoubleTask
6023 <            (BulkTask<K,V,?> p, int b, boolean split,
6024 <             ObjectToDouble<Map.Entry<K,V>> transformer,
6025 <             double basis,
6026 <             DoubleByDoubleToDouble reducer) {
6027 <            super(p, b, split);
5624 >            super(p, b, i, f, t); this.nextRight = nextRight;
5625              this.transformer = transformer;
5626              this.basis = basis; this.reducer = reducer;
5627          }
5628 +        public final Double getRawResult() { return result; }
5629          public final void compute() {
5630 <            MapReduceEntriesToDoubleTask<K,V> t = this;
5631 <            final ObjectToDouble<Map.Entry<K,V>> transformer =
5632 <                this.transformer;
5633 <            final DoubleByDoubleToDouble reducer = this.reducer;
5634 <            if (transformer == null || reducer == null)
5635 <                throw new Error(NullFunctionMessage);
5636 <            final double id = this.basis;
5637 <            int b = batch();
5638 <            while (b > 1 && t.baseIndex != t.baseLimit) {
5639 <                b >>>= 1;
5640 <                t.pending = 1;
5641 <                MapReduceEntriesToDoubleTask<K,V> rt =
5642 <                    new MapReduceEntriesToDoubleTask<K,V>
5643 <                    (t, b, true, transformer, id, reducer);
5644 <                t = new MapReduceEntriesToDoubleTask<K,V>
5645 <                    (t, b, false, transformer, id, reducer);
5646 <                t.sibling = rt;
5647 <                rt.sibling = t;
5648 <                rt.fork();
5649 <            }
5650 <            double r = id;
5651 <            Object v;
5652 <            while ((v = t.advance()) != null)
5653 <                r = reducer.apply(r, transformer.apply(entryFor((K)t.nextKey, (V)v)));
6056 <            t.result = r;
6057 <            for (;;) {
6058 <                int c; BulkTask<K,V,?> par; MapReduceEntriesToDoubleTask<K,V> s, p;
6059 <                if ((par = t.parent) == null ||
6060 <                    !(par instanceof MapReduceEntriesToDoubleTask)) {
6061 <                    t.quietlyComplete();
6062 <                    break;
6063 <                }
6064 <                else if ((c = (p = (MapReduceEntriesToDoubleTask<K,V>)par).pending) == 0) {
6065 <                    if ((s = t.sibling) != null)
6066 <                        r = reducer.apply(r, s.result);
6067 <                    (t = p).result = r;
5630 >            final ObjectToDouble<Map.Entry<K,V>> transformer;
5631 >            final DoubleByDoubleToDouble reducer;
5632 >            if ((transformer = this.transformer) != null &&
5633 >                (reducer = this.reducer) != null) {
5634 >                double r = this.basis;
5635 >                for (int i = baseIndex, f, h; batch > 0 &&
5636 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5637 >                    addToPendingCount(1);
5638 >                    (rights = new MapReduceEntriesToDoubleTask<K,V>
5639 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
5640 >                      rights, transformer, r, reducer)).fork();
5641 >                }
5642 >                for (Node<K,V> p; (p = advance()) != null; )
5643 >                    r = reducer.apply(r, transformer.apply(p));
5644 >                result = r;
5645 >                CountedCompleter<?> c;
5646 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5647 >                    @SuppressWarnings("unchecked") MapReduceEntriesToDoubleTask<K,V>
5648 >                        t = (MapReduceEntriesToDoubleTask<K,V>)c,
5649 >                        s = t.rights;
5650 >                    while (s != null) {
5651 >                        t.result = reducer.apply(t.result, s.result);
5652 >                        s = t.rights = s.nextRight;
5653 >                    }
5654                  }
6069                else if (p.casPending(c, 0))
6070                    break;
5655              }
5656          }
6073        public final Double getRawResult() { return result; }
5657      }
5658  
5659 +    @SuppressWarnings("serial")
5660      static final class MapReduceMappingsToDoubleTask<K,V>
5661          extends BulkTask<K,V,Double> {
5662          final ObjectByObjectToDouble<? super K, ? super V> transformer;
5663          final DoubleByDoubleToDouble reducer;
5664          final double basis;
5665          double result;
5666 <        MapReduceMappingsToDoubleTask<K,V> sibling;
6083 <        MapReduceMappingsToDoubleTask
6084 <            (ConcurrentHashMapV8<K,V> m,
6085 <             ObjectByObjectToDouble<? super K, ? super V> transformer,
6086 <             double basis,
6087 <             DoubleByDoubleToDouble reducer) {
6088 <            super(m);
6089 <            this.transformer = transformer;
6090 <            this.basis = basis; this.reducer = reducer;
6091 <        }
5666 >        MapReduceMappingsToDoubleTask<K,V> rights, nextRight;
5667          MapReduceMappingsToDoubleTask
5668 <            (BulkTask<K,V,?> p, int b, boolean split,
5668 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5669 >             MapReduceMappingsToDoubleTask<K,V> nextRight,
5670               ObjectByObjectToDouble<? super K, ? super V> transformer,
5671               double basis,
5672               DoubleByDoubleToDouble reducer) {
5673 <            super(p, b, split);
5673 >            super(p, b, i, f, t); this.nextRight = nextRight;
5674              this.transformer = transformer;
5675              this.basis = basis; this.reducer = reducer;
5676          }
5677 +        public final Double getRawResult() { return result; }
5678          public final void compute() {
5679 <            MapReduceMappingsToDoubleTask<K,V> t = this;
5680 <            final ObjectByObjectToDouble<? super K, ? super V> transformer =
5681 <                this.transformer;
5682 <            final DoubleByDoubleToDouble reducer = this.reducer;
5683 <            if (transformer == null || reducer == null)
5684 <                throw new Error(NullFunctionMessage);
5685 <            final double id = this.basis;
5686 <            int b = batch();
5687 <            while (b > 1 && t.baseIndex != t.baseLimit) {
5688 <                b >>>= 1;
5689 <                t.pending = 1;
5690 <                MapReduceMappingsToDoubleTask<K,V> rt =
5691 <                    new MapReduceMappingsToDoubleTask<K,V>
5692 <                    (t, b, true, transformer, id, reducer);
5693 <                t = new MapReduceMappingsToDoubleTask<K,V>
5694 <                    (t, b, false, transformer, id, reducer);
5695 <                t.sibling = rt;
5696 <                rt.sibling = t;
5697 <                rt.fork();
5698 <            }
5699 <            double r = id;
5700 <            Object v;
5701 <            while ((v = t.advance()) != null)
5702 <                r = reducer.apply(r, transformer.apply((K)t.nextKey, (V)v));
6126 <            t.result = r;
6127 <            for (;;) {
6128 <                int c; BulkTask<K,V,?> par; MapReduceMappingsToDoubleTask<K,V> s, p;
6129 <                if ((par = t.parent) == null ||
6130 <                    !(par instanceof MapReduceMappingsToDoubleTask)) {
6131 <                    t.quietlyComplete();
6132 <                    break;
6133 <                }
6134 <                else if ((c = (p = (MapReduceMappingsToDoubleTask<K,V>)par).pending) == 0) {
6135 <                    if ((s = t.sibling) != null)
6136 <                        r = reducer.apply(r, s.result);
6137 <                    (t = p).result = r;
5679 >            final ObjectByObjectToDouble<? super K, ? super V> transformer;
5680 >            final DoubleByDoubleToDouble reducer;
5681 >            if ((transformer = this.transformer) != null &&
5682 >                (reducer = this.reducer) != null) {
5683 >                double r = this.basis;
5684 >                for (int i = baseIndex, f, h; batch > 0 &&
5685 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5686 >                    addToPendingCount(1);
5687 >                    (rights = new MapReduceMappingsToDoubleTask<K,V>
5688 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
5689 >                      rights, transformer, r, reducer)).fork();
5690 >                }
5691 >                for (Node<K,V> p; (p = advance()) != null; )
5692 >                    r = reducer.apply(r, transformer.apply(p.key, p.val));
5693 >                result = r;
5694 >                CountedCompleter<?> c;
5695 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5696 >                    @SuppressWarnings("unchecked") MapReduceMappingsToDoubleTask<K,V>
5697 >                        t = (MapReduceMappingsToDoubleTask<K,V>)c,
5698 >                        s = t.rights;
5699 >                    while (s != null) {
5700 >                        t.result = reducer.apply(t.result, s.result);
5701 >                        s = t.rights = s.nextRight;
5702 >                    }
5703                  }
6139                else if (p.casPending(c, 0))
6140                    break;
5704              }
5705          }
6143        public final Double getRawResult() { return result; }
5706      }
5707  
5708 +    @SuppressWarnings("serial")
5709      static final class MapReduceKeysToLongTask<K,V>
5710          extends BulkTask<K,V,Long> {
5711          final ObjectToLong<? super K> transformer;
5712          final LongByLongToLong reducer;
5713          final long basis;
5714          long result;
5715 <        MapReduceKeysToLongTask<K,V> sibling;
6153 <        MapReduceKeysToLongTask
6154 <            (ConcurrentHashMapV8<K,V> m,
6155 <             ObjectToLong<? super K> transformer,
6156 <             long basis,
6157 <             LongByLongToLong reducer) {
6158 <            super(m);
6159 <            this.transformer = transformer;
6160 <            this.basis = basis; this.reducer = reducer;
6161 <        }
5715 >        MapReduceKeysToLongTask<K,V> rights, nextRight;
5716          MapReduceKeysToLongTask
5717 <            (BulkTask<K,V,?> p, int b, boolean split,
5717 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5718 >             MapReduceKeysToLongTask<K,V> nextRight,
5719               ObjectToLong<? super K> transformer,
5720               long basis,
5721               LongByLongToLong reducer) {
5722 <            super(p, b, split);
5722 >            super(p, b, i, f, t); this.nextRight = nextRight;
5723              this.transformer = transformer;
5724              this.basis = basis; this.reducer = reducer;
5725          }
5726 +        public final Long getRawResult() { return result; }
5727          public final void compute() {
5728 <            MapReduceKeysToLongTask<K,V> t = this;
5729 <            final ObjectToLong<? super K> transformer =
5730 <                this.transformer;
5731 <            final LongByLongToLong reducer = this.reducer;
5732 <            if (transformer == null || reducer == null)
5733 <                throw new Error(NullFunctionMessage);
5734 <            final long id = this.basis;
5735 <            int b = batch();
5736 <            while (b > 1 && t.baseIndex != t.baseLimit) {
5737 <                b >>>= 1;
5738 <                t.pending = 1;
5739 <                MapReduceKeysToLongTask<K,V> rt =
5740 <                    new MapReduceKeysToLongTask<K,V>
5741 <                    (t, b, true, transformer, id, reducer);
5742 <                t = new MapReduceKeysToLongTask<K,V>
5743 <                    (t, b, false, transformer, id, reducer);
5744 <                t.sibling = rt;
5745 <                rt.sibling = t;
5746 <                rt.fork();
5747 <            }
5748 <            long r = id;
5749 <            while (t.advance() != null)
5750 <                r = reducer.apply(r, transformer.apply((K)t.nextKey));
5751 <            t.result = r;
6196 <            for (;;) {
6197 <                int c; BulkTask<K,V,?> par; MapReduceKeysToLongTask<K,V> s, p;
6198 <                if ((par = t.parent) == null ||
6199 <                    !(par instanceof MapReduceKeysToLongTask)) {
6200 <                    t.quietlyComplete();
6201 <                    break;
6202 <                }
6203 <                else if ((c = (p = (MapReduceKeysToLongTask<K,V>)par).pending) == 0) {
6204 <                    if ((s = t.sibling) != null)
6205 <                        r = reducer.apply(r, s.result);
6206 <                    (t = p).result = r;
5728 >            final ObjectToLong<? super K> transformer;
5729 >            final LongByLongToLong reducer;
5730 >            if ((transformer = this.transformer) != null &&
5731 >                (reducer = this.reducer) != null) {
5732 >                long r = this.basis;
5733 >                for (int i = baseIndex, f, h; batch > 0 &&
5734 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5735 >                    addToPendingCount(1);
5736 >                    (rights = new MapReduceKeysToLongTask<K,V>
5737 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
5738 >                      rights, transformer, r, reducer)).fork();
5739 >                }
5740 >                for (Node<K,V> p; (p = advance()) != null; )
5741 >                    r = reducer.apply(r, transformer.apply(p.key));
5742 >                result = r;
5743 >                CountedCompleter<?> c;
5744 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5745 >                    @SuppressWarnings("unchecked") MapReduceKeysToLongTask<K,V>
5746 >                        t = (MapReduceKeysToLongTask<K,V>)c,
5747 >                        s = t.rights;
5748 >                    while (s != null) {
5749 >                        t.result = reducer.apply(t.result, s.result);
5750 >                        s = t.rights = s.nextRight;
5751 >                    }
5752                  }
6208                else if (p.casPending(c, 0))
6209                    break;
5753              }
5754          }
6212        public final Long getRawResult() { return result; }
5755      }
5756  
5757 +    @SuppressWarnings("serial")
5758      static final class MapReduceValuesToLongTask<K,V>
5759          extends BulkTask<K,V,Long> {
5760          final ObjectToLong<? super V> transformer;
5761          final LongByLongToLong reducer;
5762          final long basis;
5763          long result;
5764 <        MapReduceValuesToLongTask<K,V> sibling;
5764 >        MapReduceValuesToLongTask<K,V> rights, nextRight;
5765          MapReduceValuesToLongTask
5766 <            (ConcurrentHashMapV8<K,V> m,
5766 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5767 >             MapReduceValuesToLongTask<K,V> nextRight,
5768               ObjectToLong<? super V> transformer,
5769               long basis,
5770               LongByLongToLong reducer) {
5771 <            super(m);
6228 <            this.transformer = transformer;
6229 <            this.basis = basis; this.reducer = reducer;
6230 <        }
6231 <        MapReduceValuesToLongTask
6232 <            (BulkTask<K,V,?> p, int b, boolean split,
6233 <             ObjectToLong<? super V> transformer,
6234 <             long basis,
6235 <             LongByLongToLong reducer) {
6236 <            super(p, b, split);
5771 >            super(p, b, i, f, t); this.nextRight = nextRight;
5772              this.transformer = transformer;
5773              this.basis = basis; this.reducer = reducer;
5774          }
5775 +        public final Long getRawResult() { return result; }
5776          public final void compute() {
5777 <            MapReduceValuesToLongTask<K,V> t = this;
5778 <            final ObjectToLong<? super V> transformer =
5779 <                this.transformer;
5780 <            final LongByLongToLong reducer = this.reducer;
5781 <            if (transformer == null || reducer == null)
5782 <                throw new Error(NullFunctionMessage);
5783 <            final long id = this.basis;
5784 <            int b = batch();
5785 <            while (b > 1 && t.baseIndex != t.baseLimit) {
5786 <                b >>>= 1;
5787 <                t.pending = 1;
5788 <                MapReduceValuesToLongTask<K,V> rt =
5789 <                    new MapReduceValuesToLongTask<K,V>
5790 <                    (t, b, true, transformer, id, reducer);
5791 <                t = new MapReduceValuesToLongTask<K,V>
5792 <                    (t, b, false, transformer, id, reducer);
5793 <                t.sibling = rt;
5794 <                rt.sibling = t;
5795 <                rt.fork();
5796 <            }
5797 <            long r = id;
5798 <            Object v;
5799 <            while ((v = t.advance()) != null)
5800 <                r = reducer.apply(r, transformer.apply((V)v));
6265 <            t.result = r;
6266 <            for (;;) {
6267 <                int c; BulkTask<K,V,?> par; MapReduceValuesToLongTask<K,V> s, p;
6268 <                if ((par = t.parent) == null ||
6269 <                    !(par instanceof MapReduceValuesToLongTask)) {
6270 <                    t.quietlyComplete();
6271 <                    break;
6272 <                }
6273 <                else if ((c = (p = (MapReduceValuesToLongTask<K,V>)par).pending) == 0) {
6274 <                    if ((s = t.sibling) != null)
6275 <                        r = reducer.apply(r, s.result);
6276 <                    (t = p).result = r;
5777 >            final ObjectToLong<? super V> transformer;
5778 >            final LongByLongToLong reducer;
5779 >            if ((transformer = this.transformer) != null &&
5780 >                (reducer = this.reducer) != null) {
5781 >                long r = this.basis;
5782 >                for (int i = baseIndex, f, h; batch > 0 &&
5783 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5784 >                    addToPendingCount(1);
5785 >                    (rights = new MapReduceValuesToLongTask<K,V>
5786 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
5787 >                      rights, transformer, r, reducer)).fork();
5788 >                }
5789 >                for (Node<K,V> p; (p = advance()) != null; )
5790 >                    r = reducer.apply(r, transformer.apply(p.val));
5791 >                result = r;
5792 >                CountedCompleter<?> c;
5793 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5794 >                    @SuppressWarnings("unchecked") MapReduceValuesToLongTask<K,V>
5795 >                        t = (MapReduceValuesToLongTask<K,V>)c,
5796 >                        s = t.rights;
5797 >                    while (s != null) {
5798 >                        t.result = reducer.apply(t.result, s.result);
5799 >                        s = t.rights = s.nextRight;
5800 >                    }
5801                  }
6278                else if (p.casPending(c, 0))
6279                    break;
5802              }
5803          }
6282        public final Long getRawResult() { return result; }
5804      }
5805  
5806 +    @SuppressWarnings("serial")
5807      static final class MapReduceEntriesToLongTask<K,V>
5808          extends BulkTask<K,V,Long> {
5809          final ObjectToLong<Map.Entry<K,V>> transformer;
5810          final LongByLongToLong reducer;
5811          final long basis;
5812          long result;
5813 <        MapReduceEntriesToLongTask<K,V> sibling;
5813 >        MapReduceEntriesToLongTask<K,V> rights, nextRight;
5814          MapReduceEntriesToLongTask
5815 <            (ConcurrentHashMapV8<K,V> m,
5815 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5816 >             MapReduceEntriesToLongTask<K,V> nextRight,
5817               ObjectToLong<Map.Entry<K,V>> transformer,
5818               long basis,
5819               LongByLongToLong reducer) {
5820 <            super(m);
6298 <            this.transformer = transformer;
6299 <            this.basis = basis; this.reducer = reducer;
6300 <        }
6301 <        MapReduceEntriesToLongTask
6302 <            (BulkTask<K,V,?> p, int b, boolean split,
6303 <             ObjectToLong<Map.Entry<K,V>> transformer,
6304 <             long basis,
6305 <             LongByLongToLong reducer) {
6306 <            super(p, b, split);
5820 >            super(p, b, i, f, t); this.nextRight = nextRight;
5821              this.transformer = transformer;
5822              this.basis = basis; this.reducer = reducer;
5823          }
5824 +        public final Long getRawResult() { return result; }
5825          public final void compute() {
5826 <            MapReduceEntriesToLongTask<K,V> t = this;
5827 <            final ObjectToLong<Map.Entry<K,V>> transformer =
5828 <                this.transformer;
5829 <            final LongByLongToLong reducer = this.reducer;
5830 <            if (transformer == null || reducer == null)
5831 <                throw new Error(NullFunctionMessage);
5832 <            final long id = this.basis;
5833 <            int b = batch();
5834 <            while (b > 1 && t.baseIndex != t.baseLimit) {
5835 <                b >>>= 1;
5836 <                t.pending = 1;
5837 <                MapReduceEntriesToLongTask<K,V> rt =
5838 <                    new MapReduceEntriesToLongTask<K,V>
5839 <                    (t, b, true, transformer, id, reducer);
5840 <                t = new MapReduceEntriesToLongTask<K,V>
5841 <                    (t, b, false, transformer, id, reducer);
5842 <                t.sibling = rt;
5843 <                rt.sibling = t;
5844 <                rt.fork();
5845 <            }
5846 <            long r = id;
5847 <            Object v;
5848 <            while ((v = t.advance()) != null)
5849 <                r = reducer.apply(r, transformer.apply(entryFor((K)t.nextKey, (V)v)));
6335 <            t.result = r;
6336 <            for (;;) {
6337 <                int c; BulkTask<K,V,?> par; MapReduceEntriesToLongTask<K,V> s, p;
6338 <                if ((par = t.parent) == null ||
6339 <                    !(par instanceof MapReduceEntriesToLongTask)) {
6340 <                    t.quietlyComplete();
6341 <                    break;
6342 <                }
6343 <                else if ((c = (p = (MapReduceEntriesToLongTask<K,V>)par).pending) == 0) {
6344 <                    if ((s = t.sibling) != null)
6345 <                        r = reducer.apply(r, s.result);
6346 <                    (t = p).result = r;
5826 >            final ObjectToLong<Map.Entry<K,V>> transformer;
5827 >            final LongByLongToLong reducer;
5828 >            if ((transformer = this.transformer) != null &&
5829 >                (reducer = this.reducer) != null) {
5830 >                long r = this.basis;
5831 >                for (int i = baseIndex, f, h; batch > 0 &&
5832 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5833 >                    addToPendingCount(1);
5834 >                    (rights = new MapReduceEntriesToLongTask<K,V>
5835 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
5836 >                      rights, transformer, r, reducer)).fork();
5837 >                }
5838 >                for (Node<K,V> p; (p = advance()) != null; )
5839 >                    r = reducer.apply(r, transformer.apply(p));
5840 >                result = r;
5841 >                CountedCompleter<?> c;
5842 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5843 >                    @SuppressWarnings("unchecked") MapReduceEntriesToLongTask<K,V>
5844 >                        t = (MapReduceEntriesToLongTask<K,V>)c,
5845 >                        s = t.rights;
5846 >                    while (s != null) {
5847 >                        t.result = reducer.apply(t.result, s.result);
5848 >                        s = t.rights = s.nextRight;
5849 >                    }
5850                  }
6348                else if (p.casPending(c, 0))
6349                    break;
5851              }
5852          }
6352        public final Long getRawResult() { return result; }
5853      }
5854  
5855 +    @SuppressWarnings("serial")
5856      static final class MapReduceMappingsToLongTask<K,V>
5857          extends BulkTask<K,V,Long> {
5858          final ObjectByObjectToLong<? super K, ? super V> transformer;
5859          final LongByLongToLong reducer;
5860          final long basis;
5861          long result;
5862 <        MapReduceMappingsToLongTask<K,V> sibling;
5862 >        MapReduceMappingsToLongTask<K,V> rights, nextRight;
5863          MapReduceMappingsToLongTask
5864 <            (ConcurrentHashMapV8<K,V> m,
5864 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5865 >             MapReduceMappingsToLongTask<K,V> nextRight,
5866               ObjectByObjectToLong<? super K, ? super V> transformer,
5867               long basis,
5868               LongByLongToLong reducer) {
5869 <            super(m);
6368 <            this.transformer = transformer;
6369 <            this.basis = basis; this.reducer = reducer;
6370 <        }
6371 <        MapReduceMappingsToLongTask
6372 <            (BulkTask<K,V,?> p, int b, boolean split,
6373 <             ObjectByObjectToLong<? super K, ? super V> transformer,
6374 <             long basis,
6375 <             LongByLongToLong reducer) {
6376 <            super(p, b, split);
5869 >            super(p, b, i, f, t); this.nextRight = nextRight;
5870              this.transformer = transformer;
5871              this.basis = basis; this.reducer = reducer;
5872          }
5873 +        public final Long getRawResult() { return result; }
5874          public final void compute() {
5875 <            MapReduceMappingsToLongTask<K,V> t = this;
5876 <            final ObjectByObjectToLong<? super K, ? super V> transformer =
5877 <                this.transformer;
5878 <            final LongByLongToLong reducer = this.reducer;
5879 <            if (transformer == null || reducer == null)
5880 <                throw new Error(NullFunctionMessage);
5881 <            final long id = this.basis;
5882 <            int b = batch();
5883 <            while (b > 1 && t.baseIndex != t.baseLimit) {
5884 <                b >>>= 1;
5885 <                t.pending = 1;
5886 <                MapReduceMappingsToLongTask<K,V> rt =
5887 <                    new MapReduceMappingsToLongTask<K,V>
5888 <                    (t, b, true, transformer, id, reducer);
5889 <                t = new MapReduceMappingsToLongTask<K,V>
5890 <                    (t, b, false, transformer, id, reducer);
5891 <                t.sibling = rt;
5892 <                rt.sibling = t;
5893 <                rt.fork();
5894 <            }
5895 <            long r = id;
5896 <            Object v;
5897 <            while ((v = t.advance()) != null)
5898 <                r = reducer.apply(r, transformer.apply((K)t.nextKey, (V)v));
6405 <            t.result = r;
6406 <            for (;;) {
6407 <                int c; BulkTask<K,V,?> par; MapReduceMappingsToLongTask<K,V> s, p;
6408 <                if ((par = t.parent) == null ||
6409 <                    !(par instanceof MapReduceMappingsToLongTask)) {
6410 <                    t.quietlyComplete();
6411 <                    break;
6412 <                }
6413 <                else if ((c = (p = (MapReduceMappingsToLongTask<K,V>)par).pending) == 0) {
6414 <                    if ((s = t.sibling) != null)
6415 <                        r = reducer.apply(r, s.result);
6416 <                    (t = p).result = r;
5875 >            final ObjectByObjectToLong<? super K, ? super V> transformer;
5876 >            final LongByLongToLong reducer;
5877 >            if ((transformer = this.transformer) != null &&
5878 >                (reducer = this.reducer) != null) {
5879 >                long r = this.basis;
5880 >                for (int i = baseIndex, f, h; batch > 0 &&
5881 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5882 >                    addToPendingCount(1);
5883 >                    (rights = new MapReduceMappingsToLongTask<K,V>
5884 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
5885 >                      rights, transformer, r, reducer)).fork();
5886 >                }
5887 >                for (Node<K,V> p; (p = advance()) != null; )
5888 >                    r = reducer.apply(r, transformer.apply(p.key, p.val));
5889 >                result = r;
5890 >                CountedCompleter<?> c;
5891 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5892 >                    @SuppressWarnings("unchecked") MapReduceMappingsToLongTask<K,V>
5893 >                        t = (MapReduceMappingsToLongTask<K,V>)c,
5894 >                        s = t.rights;
5895 >                    while (s != null) {
5896 >                        t.result = reducer.apply(t.result, s.result);
5897 >                        s = t.rights = s.nextRight;
5898 >                    }
5899                  }
6418                else if (p.casPending(c, 0))
6419                    break;
5900              }
5901          }
6422        public final Long getRawResult() { return result; }
5902      }
5903  
5904 +    @SuppressWarnings("serial")
5905      static final class MapReduceKeysToIntTask<K,V>
5906          extends BulkTask<K,V,Integer> {
5907          final ObjectToInt<? super K> transformer;
5908          final IntByIntToInt reducer;
5909          final int basis;
5910          int result;
5911 <        MapReduceKeysToIntTask<K,V> sibling;
5911 >        MapReduceKeysToIntTask<K,V> rights, nextRight;
5912          MapReduceKeysToIntTask
5913 <            (ConcurrentHashMapV8<K,V> m,
5913 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5914 >             MapReduceKeysToIntTask<K,V> nextRight,
5915               ObjectToInt<? super K> transformer,
5916               int basis,
5917               IntByIntToInt reducer) {
5918 <            super(m);
6438 <            this.transformer = transformer;
6439 <            this.basis = basis; this.reducer = reducer;
6440 <        }
6441 <        MapReduceKeysToIntTask
6442 <            (BulkTask<K,V,?> p, int b, boolean split,
6443 <             ObjectToInt<? super K> transformer,
6444 <             int basis,
6445 <             IntByIntToInt reducer) {
6446 <            super(p, b, split);
5918 >            super(p, b, i, f, t); this.nextRight = nextRight;
5919              this.transformer = transformer;
5920              this.basis = basis; this.reducer = reducer;
5921          }
5922 +        public final Integer getRawResult() { return result; }
5923          public final void compute() {
5924 <            MapReduceKeysToIntTask<K,V> t = this;
5925 <            final ObjectToInt<? super K> transformer =
5926 <                this.transformer;
5927 <            final IntByIntToInt reducer = this.reducer;
5928 <            if (transformer == null || reducer == null)
5929 <                throw new Error(NullFunctionMessage);
5930 <            final int id = this.basis;
5931 <            int b = batch();
5932 <            while (b > 1 && t.baseIndex != t.baseLimit) {
5933 <                b >>>= 1;
5934 <                t.pending = 1;
5935 <                MapReduceKeysToIntTask<K,V> rt =
5936 <                    new MapReduceKeysToIntTask<K,V>
5937 <                    (t, b, true, transformer, id, reducer);
5938 <                t = new MapReduceKeysToIntTask<K,V>
5939 <                    (t, b, false, transformer, id, reducer);
5940 <                t.sibling = rt;
5941 <                rt.sibling = t;
5942 <                rt.fork();
5943 <            }
5944 <            int r = id;
5945 <            while (t.advance() != null)
5946 <                r = reducer.apply(r, transformer.apply((K)t.nextKey));
5947 <            t.result = r;
6475 <            for (;;) {
6476 <                int c; BulkTask<K,V,?> par; MapReduceKeysToIntTask<K,V> s, p;
6477 <                if ((par = t.parent) == null ||
6478 <                    !(par instanceof MapReduceKeysToIntTask)) {
6479 <                    t.quietlyComplete();
6480 <                    break;
6481 <                }
6482 <                else if ((c = (p = (MapReduceKeysToIntTask<K,V>)par).pending) == 0) {
6483 <                    if ((s = t.sibling) != null)
6484 <                        r = reducer.apply(r, s.result);
6485 <                    (t = p).result = r;
5924 >            final ObjectToInt<? super K> transformer;
5925 >            final IntByIntToInt reducer;
5926 >            if ((transformer = this.transformer) != null &&
5927 >                (reducer = this.reducer) != null) {
5928 >                int r = this.basis;
5929 >                for (int i = baseIndex, f, h; batch > 0 &&
5930 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5931 >                    addToPendingCount(1);
5932 >                    (rights = new MapReduceKeysToIntTask<K,V>
5933 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
5934 >                      rights, transformer, r, reducer)).fork();
5935 >                }
5936 >                for (Node<K,V> p; (p = advance()) != null; )
5937 >                    r = reducer.apply(r, transformer.apply(p.key));
5938 >                result = r;
5939 >                CountedCompleter<?> c;
5940 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5941 >                    @SuppressWarnings("unchecked") MapReduceKeysToIntTask<K,V>
5942 >                        t = (MapReduceKeysToIntTask<K,V>)c,
5943 >                        s = t.rights;
5944 >                    while (s != null) {
5945 >                        t.result = reducer.apply(t.result, s.result);
5946 >                        s = t.rights = s.nextRight;
5947 >                    }
5948                  }
6487                else if (p.casPending(c, 0))
6488                    break;
5949              }
5950          }
6491        public final Integer getRawResult() { return result; }
5951      }
5952  
5953 +    @SuppressWarnings("serial")
5954      static final class MapReduceValuesToIntTask<K,V>
5955          extends BulkTask<K,V,Integer> {
5956          final ObjectToInt<? super V> transformer;
5957          final IntByIntToInt reducer;
5958          final int basis;
5959          int result;
5960 <        MapReduceValuesToIntTask<K,V> sibling;
6501 <        MapReduceValuesToIntTask
6502 <            (ConcurrentHashMapV8<K,V> m,
6503 <             ObjectToInt<? super V> transformer,
6504 <             int basis,
6505 <             IntByIntToInt reducer) {
6506 <            super(m);
6507 <            this.transformer = transformer;
6508 <            this.basis = basis; this.reducer = reducer;
6509 <        }
5960 >        MapReduceValuesToIntTask<K,V> rights, nextRight;
5961          MapReduceValuesToIntTask
5962 <            (BulkTask<K,V,?> p, int b, boolean split,
5962 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5963 >             MapReduceValuesToIntTask<K,V> nextRight,
5964               ObjectToInt<? super V> transformer,
5965               int basis,
5966               IntByIntToInt reducer) {
5967 <            super(p, b, split);
5967 >            super(p, b, i, f, t); this.nextRight = nextRight;
5968              this.transformer = transformer;
5969              this.basis = basis; this.reducer = reducer;
5970          }
5971 +        public final Integer getRawResult() { return result; }
5972          public final void compute() {
5973 <            MapReduceValuesToIntTask<K,V> t = this;
5974 <            final ObjectToInt<? super V> transformer =
5975 <                this.transformer;
5976 <            final IntByIntToInt reducer = this.reducer;
5977 <            if (transformer == null || reducer == null)
5978 <                throw new Error(NullFunctionMessage);
5979 <            final int id = this.basis;
5980 <            int b = batch();
5981 <            while (b > 1 && t.baseIndex != t.baseLimit) {
5982 <                b >>>= 1;
5983 <                t.pending = 1;
5984 <                MapReduceValuesToIntTask<K,V> rt =
5985 <                    new MapReduceValuesToIntTask<K,V>
5986 <                    (t, b, true, transformer, id, reducer);
5987 <                t = new MapReduceValuesToIntTask<K,V>
5988 <                    (t, b, false, transformer, id, reducer);
5989 <                t.sibling = rt;
5990 <                rt.sibling = t;
5991 <                rt.fork();
5992 <            }
5993 <            int r = id;
5994 <            Object v;
5995 <            while ((v = t.advance()) != null)
5996 <                r = reducer.apply(r, transformer.apply((V)v));
6544 <            t.result = r;
6545 <            for (;;) {
6546 <                int c; BulkTask<K,V,?> par; MapReduceValuesToIntTask<K,V> s, p;
6547 <                if ((par = t.parent) == null ||
6548 <                    !(par instanceof MapReduceValuesToIntTask)) {
6549 <                    t.quietlyComplete();
6550 <                    break;
6551 <                }
6552 <                else if ((c = (p = (MapReduceValuesToIntTask<K,V>)par).pending) == 0) {
6553 <                    if ((s = t.sibling) != null)
6554 <                        r = reducer.apply(r, s.result);
6555 <                    (t = p).result = r;
5973 >            final ObjectToInt<? super V> transformer;
5974 >            final IntByIntToInt reducer;
5975 >            if ((transformer = this.transformer) != null &&
5976 >                (reducer = this.reducer) != null) {
5977 >                int r = this.basis;
5978 >                for (int i = baseIndex, f, h; batch > 0 &&
5979 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5980 >                    addToPendingCount(1);
5981 >                    (rights = new MapReduceValuesToIntTask<K,V>
5982 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
5983 >                      rights, transformer, r, reducer)).fork();
5984 >                }
5985 >                for (Node<K,V> p; (p = advance()) != null; )
5986 >                    r = reducer.apply(r, transformer.apply(p.val));
5987 >                result = r;
5988 >                CountedCompleter<?> c;
5989 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5990 >                    @SuppressWarnings("unchecked") MapReduceValuesToIntTask<K,V>
5991 >                        t = (MapReduceValuesToIntTask<K,V>)c,
5992 >                        s = t.rights;
5993 >                    while (s != null) {
5994 >                        t.result = reducer.apply(t.result, s.result);
5995 >                        s = t.rights = s.nextRight;
5996 >                    }
5997                  }
6557                else if (p.casPending(c, 0))
6558                    break;
5998              }
5999          }
6561        public final Integer getRawResult() { return result; }
6000      }
6001  
6002 +    @SuppressWarnings("serial")
6003      static final class MapReduceEntriesToIntTask<K,V>
6004          extends BulkTask<K,V,Integer> {
6005          final ObjectToInt<Map.Entry<K,V>> transformer;
6006          final IntByIntToInt reducer;
6007          final int basis;
6008          int result;
6009 <        MapReduceEntriesToIntTask<K,V> sibling;
6009 >        MapReduceEntriesToIntTask<K,V> rights, nextRight;
6010          MapReduceEntriesToIntTask
6011 <            (ConcurrentHashMapV8<K,V> m,
6011 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
6012 >             MapReduceEntriesToIntTask<K,V> nextRight,
6013               ObjectToInt<Map.Entry<K,V>> transformer,
6014               int basis,
6015               IntByIntToInt reducer) {
6016 <            super(m);
6577 <            this.transformer = transformer;
6578 <            this.basis = basis; this.reducer = reducer;
6579 <        }
6580 <        MapReduceEntriesToIntTask
6581 <            (BulkTask<K,V,?> p, int b, boolean split,
6582 <             ObjectToInt<Map.Entry<K,V>> transformer,
6583 <             int basis,
6584 <             IntByIntToInt reducer) {
6585 <            super(p, b, split);
6016 >            super(p, b, i, f, t); this.nextRight = nextRight;
6017              this.transformer = transformer;
6018              this.basis = basis; this.reducer = reducer;
6019          }
6020 +        public final Integer getRawResult() { return result; }
6021          public final void compute() {
6022 <            MapReduceEntriesToIntTask<K,V> t = this;
6023 <            final ObjectToInt<Map.Entry<K,V>> transformer =
6024 <                this.transformer;
6025 <            final IntByIntToInt reducer = this.reducer;
6026 <            if (transformer == null || reducer == null)
6027 <                throw new Error(NullFunctionMessage);
6028 <            final int id = this.basis;
6029 <            int b = batch();
6030 <            while (b > 1 && t.baseIndex != t.baseLimit) {
6031 <                b >>>= 1;
6032 <                t.pending = 1;
6033 <                MapReduceEntriesToIntTask<K,V> rt =
6034 <                    new MapReduceEntriesToIntTask<K,V>
6035 <                    (t, b, true, transformer, id, reducer);
6036 <                t = new MapReduceEntriesToIntTask<K,V>
6037 <                    (t, b, false, transformer, id, reducer);
6038 <                t.sibling = rt;
6039 <                rt.sibling = t;
6040 <                rt.fork();
6041 <            }
6042 <            int r = id;
6043 <            Object v;
6044 <            while ((v = t.advance()) != null)
6045 <                r = reducer.apply(r, transformer.apply(entryFor((K)t.nextKey, (V)v)));
6614 <            t.result = r;
6615 <            for (;;) {
6616 <                int c; BulkTask<K,V,?> par; MapReduceEntriesToIntTask<K,V> s, p;
6617 <                if ((par = t.parent) == null ||
6618 <                    !(par instanceof MapReduceEntriesToIntTask)) {
6619 <                    t.quietlyComplete();
6620 <                    break;
6621 <                }
6622 <                else if ((c = (p = (MapReduceEntriesToIntTask<K,V>)par).pending) == 0) {
6623 <                    if ((s = t.sibling) != null)
6624 <                        r = reducer.apply(r, s.result);
6625 <                    (t = p).result = r;
6022 >            final ObjectToInt<Map.Entry<K,V>> transformer;
6023 >            final IntByIntToInt reducer;
6024 >            if ((transformer = this.transformer) != null &&
6025 >                (reducer = this.reducer) != null) {
6026 >                int r = this.basis;
6027 >                for (int i = baseIndex, f, h; batch > 0 &&
6028 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
6029 >                    addToPendingCount(1);
6030 >                    (rights = new MapReduceEntriesToIntTask<K,V>
6031 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
6032 >                      rights, transformer, r, reducer)).fork();
6033 >                }
6034 >                for (Node<K,V> p; (p = advance()) != null; )
6035 >                    r = reducer.apply(r, transformer.apply(p));
6036 >                result = r;
6037 >                CountedCompleter<?> c;
6038 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
6039 >                    @SuppressWarnings("unchecked") MapReduceEntriesToIntTask<K,V>
6040 >                        t = (MapReduceEntriesToIntTask<K,V>)c,
6041 >                        s = t.rights;
6042 >                    while (s != null) {
6043 >                        t.result = reducer.apply(t.result, s.result);
6044 >                        s = t.rights = s.nextRight;
6045 >                    }
6046                  }
6627                else if (p.casPending(c, 0))
6628                    break;
6047              }
6048          }
6631        public final Integer getRawResult() { return result; }
6049      }
6050  
6051 +    @SuppressWarnings("serial")
6052      static final class MapReduceMappingsToIntTask<K,V>
6053          extends BulkTask<K,V,Integer> {
6054          final ObjectByObjectToInt<? super K, ? super V> transformer;
6055          final IntByIntToInt reducer;
6056          final int basis;
6057          int result;
6058 <        MapReduceMappingsToIntTask<K,V> sibling;
6058 >        MapReduceMappingsToIntTask<K,V> rights, nextRight;
6059          MapReduceMappingsToIntTask
6060 <            (ConcurrentHashMapV8<K,V> m,
6060 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
6061 >             MapReduceMappingsToIntTask<K,V> nextRight,
6062               ObjectByObjectToInt<? super K, ? super V> transformer,
6063               int basis,
6064               IntByIntToInt reducer) {
6065 <            super(m);
6647 <            this.transformer = transformer;
6648 <            this.basis = basis; this.reducer = reducer;
6649 <        }
6650 <        MapReduceMappingsToIntTask
6651 <            (BulkTask<K,V,?> p, int b, boolean split,
6652 <             ObjectByObjectToInt<? super K, ? super V> transformer,
6653 <             int basis,
6654 <             IntByIntToInt reducer) {
6655 <            super(p, b, split);
6065 >            super(p, b, i, f, t); this.nextRight = nextRight;
6066              this.transformer = transformer;
6067              this.basis = basis; this.reducer = reducer;
6068          }
6069 +        public final Integer getRawResult() { return result; }
6070          public final void compute() {
6071 <            MapReduceMappingsToIntTask<K,V> t = this;
6072 <            final ObjectByObjectToInt<? super K, ? super V> transformer =
6073 <                this.transformer;
6074 <            final IntByIntToInt reducer = this.reducer;
6075 <            if (transformer == null || reducer == null)
6076 <                throw new Error(NullFunctionMessage);
6077 <            final int id = this.basis;
6078 <            int b = batch();
6079 <            while (b > 1 && t.baseIndex != t.baseLimit) {
6080 <                b >>>= 1;
6081 <                t.pending = 1;
6082 <                MapReduceMappingsToIntTask<K,V> rt =
6083 <                    new MapReduceMappingsToIntTask<K,V>
6084 <                    (t, b, true, transformer, id, reducer);
6085 <                t = new MapReduceMappingsToIntTask<K,V>
6086 <                    (t, b, false, transformer, id, reducer);
6087 <                t.sibling = rt;
6088 <                rt.sibling = t;
6089 <                rt.fork();
6090 <            }
6091 <            int r = id;
6092 <            Object v;
6093 <            while ((v = t.advance()) != null)
6094 <                r = reducer.apply(r, transformer.apply((K)t.nextKey, (V)v));
6095 <            t.result = r;
6096 <            for (;;) {
6097 <                int c; BulkTask<K,V,?> par; MapReduceMappingsToIntTask<K,V> s, p;
6098 <                if ((par = t.parent) == null ||
6099 <                    !(par instanceof MapReduceMappingsToIntTask)) {
6100 <                    t.quietlyComplete();
6071 >            final ObjectByObjectToInt<? super K, ? super V> transformer;
6072 >            final IntByIntToInt reducer;
6073 >            if ((transformer = this.transformer) != null &&
6074 >                (reducer = this.reducer) != null) {
6075 >                int r = this.basis;
6076 >                for (int i = baseIndex, f, h; batch > 0 &&
6077 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
6078 >                    addToPendingCount(1);
6079 >                    (rights = new MapReduceMappingsToIntTask<K,V>
6080 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
6081 >                      rights, transformer, r, reducer)).fork();
6082 >                }
6083 >                for (Node<K,V> p; (p = advance()) != null; )
6084 >                    r = reducer.apply(r, transformer.apply(p.key, p.val));
6085 >                result = r;
6086 >                CountedCompleter<?> c;
6087 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
6088 >                    @SuppressWarnings("unchecked") MapReduceMappingsToIntTask<K,V>
6089 >                        t = (MapReduceMappingsToIntTask<K,V>)c,
6090 >                        s = t.rights;
6091 >                    while (s != null) {
6092 >                        t.result = reducer.apply(t.result, s.result);
6093 >                        s = t.rights = s.nextRight;
6094 >                    }
6095 >                }
6096 >            }
6097 >        }
6098 >    }
6099 >
6100 >    /* ---------------- Counters -------------- */
6101 >
6102 >    // Adapted from LongAdder and Striped64.
6103 >    // See their internal docs for explanation.
6104 >
6105 >    // A padded cell for distributing counts
6106 >    static final class CounterCell {
6107 >        volatile long p0, p1, p2, p3, p4, p5, p6;
6108 >        volatile long value;
6109 >        volatile long q0, q1, q2, q3, q4, q5, q6;
6110 >        CounterCell(long x) { value = x; }
6111 >    }
6112 >
6113 >    /**
6114 >     * Holder for the thread-local hash code determining which
6115 >     * CounterCell to use. The code is initialized via the
6116 >     * counterHashCodeGenerator, but may be moved upon collisions.
6117 >     */
6118 >    static final class CounterHashCode {
6119 >        int code;
6120 >    }
6121 >
6122 >    /**
6123 >     * Generates initial value for per-thread CounterHashCodes.
6124 >     */
6125 >    static final AtomicInteger counterHashCodeGenerator = new AtomicInteger();
6126 >
6127 >    /**
6128 >     * Increment for counterHashCodeGenerator. See class ThreadLocal
6129 >     * for explanation.
6130 >     */
6131 >    static final int SEED_INCREMENT = 0x61c88647;
6132 >
6133 >    /**
6134 >     * Per-thread counter hash codes. Shared across all instances.
6135 >     */
6136 >    static final ThreadLocal<CounterHashCode> threadCounterHashCode =
6137 >        new ThreadLocal<CounterHashCode>();
6138 >
6139 >
6140 >    final long sumCount() {
6141 >        CounterCell[] as = counterCells; CounterCell a;
6142 >        long sum = baseCount;
6143 >        if (as != null) {
6144 >            for (int i = 0; i < as.length; ++i) {
6145 >                if ((a = as[i]) != null)
6146 >                    sum += a.value;
6147 >            }
6148 >        }
6149 >        return sum;
6150 >    }
6151 >
6152 >    // See LongAdder version for explanation
6153 >    private final void fullAddCount(long x, CounterHashCode hc,
6154 >                                    boolean wasUncontended) {
6155 >        int h;
6156 >        if (hc == null) {
6157 >            hc = new CounterHashCode();
6158 >            int s = counterHashCodeGenerator.addAndGet(SEED_INCREMENT);
6159 >            h = hc.code = (s == 0) ? 1 : s; // Avoid zero
6160 >            threadCounterHashCode.set(hc);
6161 >        }
6162 >        else
6163 >            h = hc.code;
6164 >        boolean collide = false;                // True if last slot nonempty
6165 >        for (;;) {
6166 >            CounterCell[] as; CounterCell a; int n; long v;
6167 >            if ((as = counterCells) != null && (n = as.length) > 0) {
6168 >                if ((a = as[(n - 1) & h]) == null) {
6169 >                    if (cellsBusy == 0) {            // Try to attach new Cell
6170 >                        CounterCell r = new CounterCell(x); // Optimistic create
6171 >                        if (cellsBusy == 0 &&
6172 >                            U.compareAndSwapInt(this, CELLSBUSY, 0, 1)) {
6173 >                            boolean created = false;
6174 >                            try {               // Recheck under lock
6175 >                                CounterCell[] rs; int m, j;
6176 >                                if ((rs = counterCells) != null &&
6177 >                                    (m = rs.length) > 0 &&
6178 >                                    rs[j = (m - 1) & h] == null) {
6179 >                                    rs[j] = r;
6180 >                                    created = true;
6181 >                                }
6182 >                            } finally {
6183 >                                cellsBusy = 0;
6184 >                            }
6185 >                            if (created)
6186 >                                break;
6187 >                            continue;           // Slot is now non-empty
6188 >                        }
6189 >                    }
6190 >                    collide = false;
6191 >                }
6192 >                else if (!wasUncontended)       // CAS already known to fail
6193 >                    wasUncontended = true;      // Continue after rehash
6194 >                else if (U.compareAndSwapLong(a, CELLVALUE, v = a.value, v + x))
6195                      break;
6196 +                else if (counterCells != as || n >= NCPU)
6197 +                    collide = false;            // At max size or stale
6198 +                else if (!collide)
6199 +                    collide = true;
6200 +                else if (cellsBusy == 0 &&
6201 +                         U.compareAndSwapInt(this, CELLSBUSY, 0, 1)) {
6202 +                    try {
6203 +                        if (counterCells == as) {// Expand table unless stale
6204 +                            CounterCell[] rs = new CounterCell[n << 1];
6205 +                            for (int i = 0; i < n; ++i)
6206 +                                rs[i] = as[i];
6207 +                            counterCells = rs;
6208 +                        }
6209 +                    } finally {
6210 +                        cellsBusy = 0;
6211 +                    }
6212 +                    collide = false;
6213 +                    continue;                   // Retry with expanded table
6214                  }
6215 <                else if ((c = (p = (MapReduceMappingsToIntTask<K,V>)par).pending) == 0) {
6216 <                    if ((s = t.sibling) != null)
6217 <                        r = reducer.apply(r, s.result);
6218 <                    (t = p).result = r;
6215 >                h ^= h << 13;                   // Rehash
6216 >                h ^= h >>> 17;
6217 >                h ^= h << 5;
6218 >            }
6219 >            else if (cellsBusy == 0 && counterCells == as &&
6220 >                     U.compareAndSwapInt(this, CELLSBUSY, 0, 1)) {
6221 >                boolean init = false;
6222 >                try {                           // Initialize table
6223 >                    if (counterCells == as) {
6224 >                        CounterCell[] rs = new CounterCell[2];
6225 >                        rs[h & 1] = new CounterCell(x);
6226 >                        counterCells = rs;
6227 >                        init = true;
6228 >                    }
6229 >                } finally {
6230 >                    cellsBusy = 0;
6231                  }
6232 <                else if (p.casPending(c, 0))
6232 >                if (init)
6233                      break;
6234              }
6235 +            else if (U.compareAndSwapLong(this, BASECOUNT, v = baseCount, v + x))
6236 +                break;                          // Fall back on using base
6237          }
6238 <        public final Integer getRawResult() { return result; }
6238 >        hc.code = h;                            // Record index for next time
6239      }
6240  
6704
6241      // Unsafe mechanics
6242 <    private static final sun.misc.Unsafe UNSAFE;
6243 <    private static final long counterOffset;
6244 <    private static final long sizeCtlOffset;
6242 >    private static final sun.misc.Unsafe U;
6243 >    private static final long SIZECTL;
6244 >    private static final long TRANSFERINDEX;
6245 >    private static final long BASECOUNT;
6246 >    private static final long CELLSBUSY;
6247 >    private static final long CELLVALUE;
6248      private static final long ABASE;
6249      private static final int ASHIFT;
6250  
6251      static {
6713        int ss;
6252          try {
6253 <            UNSAFE = getUnsafe();
6253 >            U = getUnsafe();
6254              Class<?> k = ConcurrentHashMapV8.class;
6255 <            counterOffset = UNSAFE.objectFieldOffset
6718 <                (k.getDeclaredField("counter"));
6719 <            sizeCtlOffset = UNSAFE.objectFieldOffset
6255 >            SIZECTL = U.objectFieldOffset
6256                  (k.getDeclaredField("sizeCtl"));
6257 <            Class<?> sc = Node[].class;
6258 <            ABASE = UNSAFE.arrayBaseOffset(sc);
6259 <            ss = UNSAFE.arrayIndexScale(sc);
6257 >            TRANSFERINDEX = U.objectFieldOffset
6258 >                (k.getDeclaredField("transferIndex"));
6259 >            BASECOUNT = U.objectFieldOffset
6260 >                (k.getDeclaredField("baseCount"));
6261 >            CELLSBUSY = U.objectFieldOffset
6262 >                (k.getDeclaredField("cellsBusy"));
6263 >            Class<?> ck = CounterCell.class;
6264 >            CELLVALUE = U.objectFieldOffset
6265 >                (ck.getDeclaredField("value"));
6266 >            Class<?> ak = Node[].class;
6267 >            ABASE = U.arrayBaseOffset(ak);
6268 >            int scale = U.arrayIndexScale(ak);
6269 >            if ((scale & (scale - 1)) != 0)
6270 >                throw new Error("data type scale not a power of two");
6271 >            ASHIFT = 31 - Integer.numberOfLeadingZeros(scale);
6272          } catch (Exception e) {
6273              throw new Error(e);
6274          }
6727        if ((ss & (ss-1)) != 0)
6728            throw new Error("data type scale not a power of two");
6729        ASHIFT = 31 - Integer.numberOfLeadingZeros(ss);
6275      }
6276  
6277      /**
# Line 6739 | Line 6284 | public class ConcurrentHashMapV8<K, V>
6284      private static sun.misc.Unsafe getUnsafe() {
6285          try {
6286              return sun.misc.Unsafe.getUnsafe();
6287 <        } catch (SecurityException se) {
6288 <            try {
6289 <                return java.security.AccessController.doPrivileged
6290 <                    (new java.security
6291 <                     .PrivilegedExceptionAction<sun.misc.Unsafe>() {
6292 <                        public sun.misc.Unsafe run() throws Exception {
6293 <                            java.lang.reflect.Field f = sun.misc
6294 <                                .Unsafe.class.getDeclaredField("theUnsafe");
6295 <                            f.setAccessible(true);
6296 <                            return (sun.misc.Unsafe) f.get(null);
6297 <                        }});
6298 <            } catch (java.security.PrivilegedActionException e) {
6299 <                throw new RuntimeException("Could not initialize intrinsics",
6300 <                                           e.getCause());
6301 <            }
6287 >        } catch (SecurityException tryReflectionInstead) {}
6288 >        try {
6289 >            return java.security.AccessController.doPrivileged
6290 >            (new java.security.PrivilegedExceptionAction<sun.misc.Unsafe>() {
6291 >                public sun.misc.Unsafe run() throws Exception {
6292 >                    Class<sun.misc.Unsafe> k = sun.misc.Unsafe.class;
6293 >                    for (java.lang.reflect.Field f : k.getDeclaredFields()) {
6294 >                        f.setAccessible(true);
6295 >                        Object x = f.get(null);
6296 >                        if (k.isInstance(x))
6297 >                            return k.cast(x);
6298 >                    }
6299 >                    throw new NoSuchFieldError("the Unsafe");
6300 >                }});
6301 >        } catch (java.security.PrivilegedActionException e) {
6302 >            throw new RuntimeException("Could not initialize intrinsics",
6303 >                                       e.getCause());
6304          }
6305      }
6306   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines